gem5-dev@gem5.org

The gem5 Developer List

View all threads

[S] Change in gem5/gem5[develop]: base: fatal() if a socket path doesn't fit in sockaddr_un.sun_path.

GB
Gabe Black (Gerrit)
Wed, Apr 12, 2023 7:38 AM

Gabe Black has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/69677?usp=email )

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

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

M src/base/socket.cc
M src/base/socket.hh
2 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/src/base/socket.cc b/src/base/socket.cc
index 76dc73f..62f2071 100644
--- a/src/base/socket.cc
+++ b/src/base/socket.cc
@@ -261,15 +261,12 @@
});
}

-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 @@

    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::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
diff --git a/src/base/socket.hh b/src/base/socket.hh
index b8828e7..bc17213 100644
--- a/src/base/socket.hh
+++ b/src/base/socket.hh
@@ -162,7 +162,7 @@
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) {}

--
To view, visit
https://gem5-review.googlesource.com/c/public/gem5/+/69677?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I8702cf02c03053b5d0b6133f25b0e588de666f15
Gerrit-Change-Number: 69677
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black gabe.black@gmail.com
Gerrit-CC: Gabe Black gabeblack@google.com
Gerrit-MessageType: newchange

Gabe Black has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/69677?usp=email ) Change subject: base: fatal() if a socket path doesn't fit in sockaddr_un.sun_path. ...................................................................... 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 --- M src/base/socket.cc M src/base/socket.hh 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/base/socket.cc b/src/base/socket.cc index 76dc73f..62f2071 100644 --- a/src/base/socket.cc +++ b/src/base/socket.cc @@ -261,15 +261,12 @@ }); } -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 @@ 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::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 diff --git a/src/base/socket.hh b/src/base/socket.hh index b8828e7..bc17213 100644 --- a/src/base/socket.hh +++ b/src/base/socket.hh @@ -162,7 +162,7 @@ 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) {} -- To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/69677?usp=email To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings Gerrit-Project: public/gem5 Gerrit-Branch: develop Gerrit-Change-Id: I8702cf02c03053b5d0b6133f25b0e588de666f15 Gerrit-Change-Number: 69677 Gerrit-PatchSet: 1 Gerrit-Owner: Gabe Black <gabe.black@gmail.com> Gerrit-CC: Gabe Black <gabeblack@google.com> Gerrit-MessageType: newchange