base: fatal() if a socket path doesn't fit in sockaddr_un.sun_path.

Normally this would just generate a warning, but a warning is easy to
miss, and truncating the path to fit would be surprising. Since the max
length isn't likely to change, a path which has to be truncated is
essentially fundementally wrong, and could be defined as something
else which is short enough before being used in the config.

Note that this only applies to either the abstract path which is just
a string, or the file name and not the directory path on a file based
socket.

Change-Id: I8702cf02c03053b5d0b6133f25b0e588de666f15
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/69677
Maintainer: Gabe Black <gabeblack@google.com>
Reviewed-by: Earl Ou <shunhsingou@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2023-04-12 00:32:51 -07:00
parent 716c154b51
commit 0af4b60acb
2 changed files with 10 additions and 13 deletions

View File

@@ -261,15 +261,12 @@ listenSocketInetConfig(int port)
});
}
std::string
ListenSocketUnix::truncate(const std::string &original, size_t max_len)
void
ListenSocketUnix::checkPathLength(const std::string &original, size_t max_len)
{
if (original.size() <= max_len)
return original;
std::string truncated = original.substr(0, max_len);
warn("%s: Truncated \"%s\" to \"%s\"", name(), original, truncated);
return truncated;
fatal_if(original.size() > max_len,
"Length of socket path '%s' is %d, greater than max %d.",
original, original.size(), max_len);
}
void
@@ -303,9 +300,9 @@ ListenSocketUnix::listen()
ListenSocketUnixFile::ListenSocketUnixFile(const std::string &_name,
const std::string &_dir, const std::string &_fname) :
ListenSocketUnix(_name), dir(_dir),
fname(truncate(_fname, sizeof(sockaddr_un::sun_path) - 1))
ListenSocketUnix(_name), dir(_dir), fname(_fname)
{
checkPathLength(fname, sizeof(sockaddr_un::sun_path) - 1);
}
ListenSocketUnixFile::~ListenSocketUnixFile()
@@ -385,9 +382,9 @@ ListenSocketUnixAbstract::prepSockaddrUn(sockaddr_un &addr) const
ListenSocketUnixAbstract::ListenSocketUnixAbstract(
const std::string &_name, const std::string &_path) :
ListenSocketUnix(_name),
path(truncate(_path, sizeof(sockaddr_un::sun_path) - 1))
ListenSocketUnix(_name), path(_path)
{
checkPathLength(path, sizeof(sockaddr_un::sun_path) - 1);
}
void

View File

@@ -162,7 +162,7 @@ class ListenSocketUnix : public ListenSocket
protected:
virtual size_t prepSockaddrUn(sockaddr_un &addr) const = 0;
std::string truncate(const std::string &original, size_t max_len);
void checkPathLength(const std::string &original, size_t max_len);
ListenSocketUnix(const std::string &_name) : ListenSocket(_name) {}