[PATCH v4 RESEND 3/5] Don't add a trailing '\0' for abstract Unix domain sockets.

Alejandro Colomar alx.manpages at gmail.com
Tue Aug 23 23:02:36 UTC 2022


Since '\0' doesn't have a special meaning for abstract Unix domain
sockets, the '\0' would be part of the name.  This makes it hard
to use tools that can handle abstract sockets, such as
curl(1) --abstract-unix-sock, since it's not possible to embed a
'\0' in a string passed to execve(2).

Signed-off-by: Alejandro Colomar <alx.manpages at gmail.com>
Cc: Andrew Clayton <andrew at digital-domain.net>
Cc: Bjornar Ness <bjornar.ness at gmail.com>
---
 src/core/ngx_inet.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c
index 0be875a3..4a455e5b 100644
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -745,7 +745,14 @@ ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
     saun = (struct sockaddr_un *) &u->sockaddr;
     saun->sun_family = AF_UNIX;
     memcpy(saun->sun_path, path, len - 1);
-    saun->sun_path[len] = '\0';
+    switch (path[0]) {
+    case '\0':
+        u->socklen--;
+        break;
+    default:
+        saun->sun_path[len] = '\0';
+        break;
+    }
 
     u->addrs = ngx_pcalloc(pool, sizeof(ngx_addr_t));
     if (u->addrs == NULL) {
@@ -760,15 +767,22 @@ ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
     u->family = AF_UNIX;
     u->naddrs = 1;
 
-    saun->sun_family = AF_UNIX;
-    memcpy(saun->sun_path, path, len - 1);
-    saun->sun_path[len] = '\0';
-
     u->addrs[0].sockaddr = (struct sockaddr *) saun;
     u->addrs[0].socklen = offsetof(struct sockaddr_un, sun_path) + len;
     u->addrs[0].name.len = len + 4;
     u->addrs[0].name.data = u->url.data;
 
+    saun->sun_family = AF_UNIX;
+    memcpy(saun->sun_path, path, len - 1);
+    switch (path[0]) {
+    case '\0':
+        u->addrs[0].socklen--;
+        break;
+    default:
+        saun->sun_path[len] = '\0';
+        break;
+    }
+
     return NGX_OK;
 
 #else
-- 
2.37.2



More information about the nginx-devel mailing list