Home > Software design >  SUNPATHLEN on Linux. Where is it defined?
SUNPATHLEN on Linux. Where is it defined?

Time:12-25

Recently I begun to port some my TCP code from FreeBSD to Linux. Already had a bunch of questions ;) So, here is another one.

The C struct sockaddr_un on Linux have some different definition than of that on FreeBSD. But, to the question. I have such code in my project:

 }else if(AF_UNIX == domain){
  if(SUNPATHLEN == strnlen(a, SUNPATHLEN)){
   return -ENAMETOOLONG;
  }

The above tests that Maximum path should be no more than SUNPATHLEN constant. The SUNPATHLEN is defined on FreeBSD, but apparently not on Linux.

Looking through the gcc -E source.c | grep -n4 sockaddr_un, the struct definition is following:

1724:struct sockaddr_un
1725-  {
1726-    sa_family_t sun_family;
1727-    char sun_path[108];
1728-  };

Here the length of a buffer is explicitly set to be of 108.

What is a general rule to check for buffer being trimmed/overflowed in Linux, for the case?

CodePudding user response:

Looking through the gcc -E source.c | grep -n4 sockaddr_un, the struct definition is following:

You don't have to (and shouldn't) trawl the source for this kind of information. You should be looking at user-facing documentation to determine the interface characteristics on which you can rely. In this case, you're looking for unix(7):

A UNIX domain socket address is represented in the following structure:

struct sockaddr_un {
    sa_family_t sun_family;               /* AF_UNIX */
    char        sun_path[108];            /* Pathname */
};

The sun_family field always contains AF_UNIX. On Linux, sun_path is 108 bytes in size

(emphasis added).

What is a general rule to check for buffer being trimmed/overflowed in Linux, for the case?

No macro is defined for it, but the capacity of the path buffer is explicitly documented as 108 bytes. You can (and probably should) define your own macro for this if you're going to perform tests related to it.


You could possibly do some variation on this to remove system dependencies:

static struct sockaddr_un dummy_sockaddr_un_;
#define MY_SUN_PATH_SIZE (sizeof(dummy_sockaddr_un_.sun_path))
  • Related