[PATCH v4 RESEND 2/5] Use a minimal socklen.

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


Instead of using a socklen that is the size of the structure,
limit the value to the part of the structure that is really used.

This is necessary for supporting abstract Unix domain sockets,
since trailing null bytes are part of the socket name, and having
a socket name with null bytes in it makes it basically unusable,
since most tools (e.g., curl(1)) can't refer to them.

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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c
index 2b062b88..0be875a3 100644
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -741,7 +741,7 @@ ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
         return NGX_ERROR;
     }
 
-    u->socklen = sizeof(struct sockaddr_un);
+    u->socklen = offsetof(struct sockaddr_un, sun_path) + len;
     saun = (struct sockaddr_un *) &u->sockaddr;
     saun->sun_family = AF_UNIX;
     memcpy(saun->sun_path, path, len - 1);
@@ -765,7 +765,7 @@ ngx_parse_unix_domain_url(ngx_pool_t *pool, ngx_url_t *u)
     saun->sun_path[len] = '\0';
 
     u->addrs[0].sockaddr = (struct sockaddr *) saun;
-    u->addrs[0].socklen = sizeof(struct sockaddr_un);
+    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;
 
-- 
2.37.2



More information about the nginx-devel mailing list