This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: AF_UNIX sockets do not handle Linux-specific addressing
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: akuchling, anthonybaxter, arigo, insomnike, irmen, ppergame
Priority: normal Keywords:

Created on 2003-07-02 07:13 by ppergame, last changed 2022-04-10 16:09 by admin. This issue is now closed.

Messages (9)
msg16781 - (view) Author: Pavel Pergamenshchik (ppergame) Date: 2003-07-02 07:13
As described in unix(7) manpage, Linux allows for 
special "kernel namespace" AF_UNIX sockets defined. 
With such sockets, the first byte of the path is \x00, 
and the rest is the address. These sockets do not show 
up in the filesystem.
socketmodule.c:makesockaddr (as called by recvfrom) 
uses code like
PyString_FromString(a->sun_path)
to retrieve the address. This is incorrect -- on Linux, if 
the first byte of a->sun_path is null, the function should 
use PyString_FromStringAndSize to retrieve the full 108-
byte buffer.
I am not entirely sure that this is the only thing that 
needs to be fixed, but bind() and sendto() work with 
these sort of socket paths just fine.
msg16782 - (view) Author: Anthony Baxter (anthonybaxter) (Python triager) Date: 2003-11-25 08:13
Logged In: YES 
user_id=29957

Eek. What a totally mental design decision on the part of
the Linux kernel developers. Is there a magic C preprocessor
symbol we can use to detect that this insanity is available? 

(FWIW, Perl also has problems with this:
http://www.alexhudson.com/code/abstract-sockets-and-perl
)
msg16783 - (view) Author: Aaron Brady (insomnike) Date: 2004-06-05 19:06
Logged In: YES 
user_id=1057404

The below patch adds the functionality if UNIX_PATH_MAX is
defined (in Linux it's in sys/un.h).

###
--- socketmodule.c      3 Jun 2004 09:24:42 -0000       1.291
+++ socketmodule.c      5 Jun 2004 18:08:47 -0000
@@ -942,6 +942,11 @@
        case AF_UNIX:
        {
                struct sockaddr_un *a = (struct sockaddr_un
*) addr;
+#if defined(UNIX_PATH_MAX)
+               if (*a->sun_path == 0) {
+                       return
PyString_FromStringAndSize(a->sun_path, UNIX_PATH_MAX);
+               }
+#endif /* UNIX_PATH_MAX */
                return PyString_FromString(a->sun_path);
        }
 #endif /* AF_UNIX */
###
msg16784 - (view) Author: Aaron Brady (insomnike) Date: 2004-06-05 19:16
Logged In: YES 
user_id=1057404

Also checks for "linux" to be defined, on Mondragon's
recommendation.

###
--- socketmodule.c      3 Jun 2004 09:24:42 -0000       1.291
+++ socketmodule.c      5 Jun 2004 18:17:51 -0000
@@ -942,6 +942,11 @@
        case AF_UNIX:
        {
                struct sockaddr_un *a = (struct sockaddr_un
*) addr;
+#if defined(UNIX_PATH_MAX) && defined(linux)
+               if (*a->sun_path == 0) {
+                       return
PyString_FromStringAndSize(a->sun_path, UNIX_PATH_MAX);
+               }
+#endif /* UNIX_PATH_MAX && linux */
                return PyString_FromString(a->sun_path);
        }
 #endif /* AF_UNIX */
###
msg16785 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2004-07-07 14:04
Logged In: YES 
user_id=11375

Should it use UNIX_PATH_MAX as the length of the string, or
1+ strlen(a->sun_path+1)?  (The leading null byte, plus the
following null-terminated string?)
msg16786 - (view) Author: Pavel Pergamenshchik (ppergame) Date: 2004-07-07 14:37
Logged In: YES 
user_id=595126

"""The socket’s address in this namespace is given by the
rest of the bytes in sun_path.  Note that names in the 
abstract namespace are not zeroterminated."""

The length would be UNIX_PATH_MAX in this case.
msg16787 - (view) Author: Aaron Brady (insomnike) Date: 2004-07-07 14:39
Logged In: YES 
user_id=1057404

It should use UNIX_PATH_MAX, but it should also check that
the length of the buffer supplied is correct, my bad.
msg16788 - (view) Author: Irmen de Jong (irmen) (Python triager) Date: 2004-11-08 11:03
Logged In: YES 
user_id=129426

Patch is at 1062014

The comments below state that UNIX_PATH_MAX is defined in
sys/un.h, but it isn't. The patch gets it in another
(hopefully portable) way (and also exposes it as a new
constant in the socket module)
msg16789 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2006-04-13 20:03
Logged In: YES 
user_id=4771

I'm about to check in a slightly different patch
(also at 1062014) which tries to preserve the length
of the abstract namespace addresses (the kernel saves
this length even though it is not zero-terminated,
so that the names '\x00abc' and '\x00abc\x00' and
'\x00abc\x00\x00' are all different in theory).
The patch no longer exposes UNIX_PATH_MAX because
I'm not sure it's useful any more.  If anyone who
is relying on this Linux extension more than myself
has comments, now would be a good time :-)
History
Date User Action Args
2022-04-10 16:09:37adminsetgithub: 38766
2003-07-02 07:13:01ppergamecreate