From c1a9f01f226133e3b8539ce678871fe1c0a68270 Mon Sep 17 00:00:00 2001 From: Itay Bookstein Date: Thu, 27 Mar 2025 09:48:56 +0200 Subject: [PATCH] linux: allow nul bytes in abstract socket address (#4737) Documentation on Linux explains that nul bytes have no special significance in abstract namespace socket names. Avoid precluding such addresses. Signed-off-by: Itay Bookstein --- src/unix/pipe.c | 14 ++++++++------ test/test-pipe-getsockname.c | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/unix/pipe.c b/src/unix/pipe.c index bd57b17f..6bfe6cf2 100644 --- a/src/unix/pipe.c +++ b/src/unix/pipe.c @@ -31,13 +31,15 @@ /* Does the file path contain embedded nul bytes? */ -static int includes_nul(const char *s, size_t n) { +static int includes_invalid_nul(const char *s, size_t n) { if (n == 0) return 0; #ifdef __linux__ - /* Accept abstract socket namespace path ("\0/virtual/path"). */ - s++; - n--; + /* Accept abstract socket namespace paths, throughout which nul bytes have + * no special significance ("\0foo\0bar"). + */ + if (s[0] == '\0') + return 0; #endif return NULL != memchr(s, '\0', n); } @@ -84,7 +86,7 @@ int uv_pipe_bind2(uv_pipe_t* handle, return UV_EINVAL; #endif - if (includes_nul(name, namelen)) + if (includes_invalid_nul(name, namelen)) return UV_EINVAL; if (flags & UV_PIPE_NO_TRUNCATE) @@ -271,7 +273,7 @@ int uv_pipe_connect2(uv_connect_t* req, if (namelen == 0) return UV_EINVAL; - if (includes_nul(name, namelen)) + if (includes_invalid_nul(name, namelen)) return UV_EINVAL; if (flags & UV_PIPE_NO_TRUNCATE) diff --git a/test/test-pipe-getsockname.c b/test/test-pipe-getsockname.c index cc345ccd..3be30e67 100644 --- a/test/test-pipe-getsockname.c +++ b/test/test-pipe-getsockname.c @@ -59,7 +59,7 @@ static void pipe_client_connect_cb(uv_connect_t* req, int status) { ASSERT_OK(r); if (*buf == '\0') { /* Linux abstract socket. */ - const char expected[] = "\0" TEST_PIPENAME; + const char expected[] = "\0" TEST_PIPENAME "\0"; ASSERT_EQ(len, sizeof(expected) - 1); ASSERT_MEM_EQ(buf, expected, len); } else { @@ -223,7 +223,7 @@ TEST_IMPL(pipe_getsockname) { TEST_IMPL(pipe_getsockname_abstract) { /* TODO(bnoordhuis) Use unique name, susceptible to concurrent test runs. */ - static const char name[] = "\0" TEST_PIPENAME; + static const char name[] = "\0" TEST_PIPENAME "\0"; #if defined(__linux__) char buf[256]; size_t buflen;