From 073a59264a4284a53ebad56d7e273f022a43dff7 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. --- CMakeLists.txt | 2 +- lib/core-net/network.c | 4 +++- lib/roles/http/client/client-handshake.c | 5 ++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fdd17729e..efadb52a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -327,7 +327,7 @@ set(PACKAGE "libwebsockets") set(CPACK_PACKAGE_NAME "${PACKAGE}") set(CPACK_PACKAGE_VERSION_MAJOR "4") set(CPACK_PACKAGE_VERSION_MINOR "0") -set(CPACK_PACKAGE_VERSION_PATCH "9") +set(CPACK_PACKAGE_VERSION_PATCH "10") set(CPACK_PACKAGE_RELEASE 1) set(CPACK_GENERATOR "RPM") set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}") diff --git a/lib/core-net/network.c b/lib/core-net/network.c index 99e561995..c5340baa2 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 c5867178f..cfca321e3 100644 --- a/lib/roles/http/client/client-handshake.c +++ b/lib/roles/http/client/client-handshake.c @@ -644,7 +644,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