From d7ac7f4deb8f8a9da93f3e8fc4893f7978cce516 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Fri, 15 May 2020 18:21:56 +0100 Subject: [PATCH] unix domain: fix path name length Unlike any other sockaddr variant it turns out when sockaddr_un reports its sizeof() to connect() or listen(), it is trimmed to the used length of the sun_path[] member not including any trailing 0x00. Until now we worked fine, but our actual UDS paths have a large number of trailing 0x00 (shown as @ in most tools). Clients and servers can still interoperate if they both have this broken name. This patch fixes it to trim the sockaddr_un to the path length so the name is as you would expect. --- lib/core-net/network.c | 4 +++- lib/roles/http/client/client-handshake.c | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/core-net/network.c b/lib/core-net/network.c index 99e561995..e087cd100 100644 --- a/lib/core-net/network.c +++ b/lib/core-net/network.c @@ -215,7 +215,6 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port, #if defined(LWS_WITH_UNIX_SOCK) if (!port && LWS_UNIX_SOCK_ENABLED(vhost)) { v = (struct sockaddr *)&serv_unix; - n = sizeof(struct sockaddr_un); memset(&serv_unix, 0, sizeof(serv_unix)); serv_unix.sun_family = AF_UNIX; if (!iface) @@ -225,12 +224,15 @@ lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port, iface); return LWS_ITOSA_NOT_EXIST; } + n = sizeof(uint16_t) + strlen(iface); strcpy(serv_unix.sun_path, iface); if (serv_unix.sun_path[0] == '@') serv_unix.sun_path[0] = '\0'; else unlink(serv_unix.sun_path); + // lwsl_hexdump_notice(v, n); + } else #endif #if defined(LWS_WITH_IPV6) && !defined(LWS_PLAT_FREERTOS) diff --git a/lib/roles/http/client/client-handshake.c b/lib/roles/http/client/client-handshake.c index fcc1ecf8c..f1f525cda 100644 --- a/lib/roles/http/client/client-handshake.c +++ b/lib/roles/http/client/client-handshake.c @@ -684,7 +684,10 @@ ads_known: #if defined(LWS_WITH_UNIX_SOCK) if (wsi->unix_skt) { psa = (const struct sockaddr *)&sau; - n = sizeof(sau); + if (sau.sun_path[0]) + n = sizeof(uint16_t) + strlen(sau.sun_path); + else + n = sizeof(uint16_t) + strlen(&sau.sun_path[1]) + 1; } else #endif