summaryrefslogtreecommitdiff
path: root/libguile
diff options
context:
space:
mode:
authorLiliana Marie Prikler <liliana.prikler@gmail.com>2022-02-10 21:42:31 +0100
committerLudovic Courtès <ludo@gnu.org>2022-06-16 09:54:29 +0200
commit01b686b701dc06f6623f0cc75acd4583c0296333 (patch)
treeb7f146644f8a3443dd79d8320595ded75e3c8eca /libguile
parente4e8afd6c8eeb9be1564e1be8e33362e7e987a3c (diff)
downloadguile-01b686b701dc06f6623f0cc75acd4583c0296333.tar.gz
Allow null bytes in UNIX sockets.
The current socket address constructors all assume, that there are no null bytes in the socket path. This assumption does not hold in Linux, which uses an initial null byte to demarcate abstract sockets and ignores all further null bytes [1]. [1] https://www.man7.org/linux/man-pages/man7/unix.7.html * libguile/sockets.c (scm_fill_sockaddr)[HAVE_UNIX_DOMAIN_SOCKETS]: Use scm_to_locale_stringn to construct c_address. Use memcpy instead of strcpy and calculate size directly instead of using SUN_LEN. (_scm_from_sockaddr): Copy the entire path up to the limits imposed by addr_size. * test-suite/tests/00-socket.test: ("make-socket-address"): Add case for abstract unix sockets. ("AF_UNIX/SOCK_STREAM"): Add abstract socket versions of bind, listen, connect and accept. Signed-off-by: Ludovic Courtès <ludo@gnu.org>
Diffstat (limited to 'libguile')
-rw-r--r--libguile/socket.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/libguile/socket.c b/libguile/socket.c
index b137a7932..4818d62bd 100644
--- a/libguile/socket.c
+++ b/libguile/socket.c
@@ -813,10 +813,11 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
struct sockaddr_un *soka;
int addr_size;
char *c_address;
+ size_t c_address_size;
scm_dynwind_begin (0);
- c_address = scm_to_locale_string (address);
+ c_address = scm_to_locale_stringn (address, &c_address_size);
scm_dynwind_free (c_address);
/* the static buffer size in sockaddr_un seems to be arbitrary
@@ -826,12 +827,14 @@ scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg,
connect/bind etc., to fail. sun_path is always the last
member of the structure. */
addr_size = sizeof (struct sockaddr_un)
- + MAX (0, strlen (c_address) + 1 - (sizeof soka->sun_path));
+ + MAX (0, c_address_size + 1 - (sizeof soka->sun_path));
soka = (struct sockaddr_un *) scm_malloc (addr_size);
- memset (soka, 0, addr_size); /* for sun_len: see sin_len above. */
+ memset (soka, 0, addr_size);
soka->sun_family = AF_UNIX;
- strcpy (soka->sun_path, c_address);
- *size = SUN_LEN (soka);
+ /* we accept 0-bytes here (used for abstract sockets in Linux);
+ therefore do not use strlen() or SUN_LEN! */
+ memcpy (soka->sun_path, c_address, c_address_size);
+ *size = offsetof (struct sockaddr_un, sun_path) + c_address_size;
scm_dynwind_end ();
return (struct sockaddr *) soka;
@@ -1045,7 +1048,12 @@ _scm_from_sockaddr (const scm_t_max_sockaddr *address, unsigned addr_size,
if (addr_size <= offsetof (struct sockaddr_un, sun_path))
SCM_SIMPLE_VECTOR_SET(result, 1, SCM_BOOL_F);
else
- SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (nad->sun_path));
+ {
+ size_t path_size = addr_size - offsetof (struct sockaddr_un, sun_path);
+ SCM_SIMPLE_VECTOR_SET (result, 1,
+ scm_from_locale_stringn (nad->sun_path,
+ path_size));
+ }
}
break;
#endif