From f160a38904423ce6b97ecbf11203b6eefa890eb8 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 21 Sep 2017 10:26:28 +0200 Subject: [PATCH] resolver: set the correct address size for local/abstract sockets Limit the socklen_t to the real path length, and omit the trailing null terminator in abstract sockets. --- src/net/resolver.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/net/resolver.c b/src/net/resolver.c index 952c2c9..2df1b5a 100644 --- a/src/net/resolver.c +++ b/src/net/resolver.c @@ -29,6 +29,7 @@ #include "resolver.h" #include "config.h" +#include #include #include #include @@ -78,14 +79,17 @@ resolver_new(const char *host, unsigned port) if (host[0] == '/' || host[0] == '@') { #ifndef WIN32 - size_t path_length = strlen(host); - if (path_length >= sizeof(resolver->saun.sun_path)) { + const bool is_abstract = *host == '@'; + /* sun_path must be null-terminated unless it's an abstract + socket */ + const size_t path_length = strlen(host) + !is_abstract; + if (path_length > sizeof(resolver->saun.sun_path)) { free(resolver); return NULL; } resolver->saun.sun_family = AF_UNIX; - memcpy(resolver->saun.sun_path, host, path_length + 1); + memcpy(resolver->saun.sun_path, host, path_length); if (host[0] == '@') /* abstract socket */ @@ -93,7 +97,8 @@ resolver_new(const char *host, unsigned port) resolver->current.family = PF_UNIX; resolver->current.protocol = 0; - resolver->current.addrlen = sizeof(resolver->saun); + resolver->current.addrlen = sizeof(resolver->saun) + - sizeof(resolver->saun.sun_path) + path_length; resolver->current.addr = (const struct sockaddr *)&resolver->saun; resolver->type = TYPE_ONE; #else /* WIN32 */ -- 2.30.2