From: Lennart Poettering Date: Mon, 15 Oct 2018 16:22:04 +0000 (+0200) Subject: sd-bus: rework how we initialize struct sockaddr_un X-Git-Tag: v240~538^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=95cb14b0681692d372e7b0bfb3c9a9bbc6ffbd0d;p=thirdparty%2Fsystemd.git sd-bus: rework how we initialize struct sockaddr_un Let's use structured initialization, but more importantly, let's increase salen by 1, if we reference AF_UNIX sockets in the file system, so that they also contain the trailing NUL byte. This is what unix(7) suggests to do, hence follow it. --- diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index d6c00951613..85558d9c056 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -733,17 +733,25 @@ static int parse_unix_address(sd_bus *b, const char **p, char **guid) { if (l >= sizeof(b->sockaddr.un.sun_path)) /* We insist on NUL termination */ return -E2BIG; - b->sockaddr.un.sun_family = AF_UNIX; - strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path)); - b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l; - } else if (abstract) { + b->sockaddr.un = (struct sockaddr_un) { + .sun_family = AF_UNIX, + }; + + memcpy(b->sockaddr.un.sun_path, path, l); + b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 1; + + } else { + assert(abstract); + l = strlen(abstract); if (l >= sizeof(b->sockaddr.un.sun_path) - 1) /* We insist on NUL termination */ return -E2BIG; - b->sockaddr.un.sun_family = AF_UNIX; - b->sockaddr.un.sun_path[0] = 0; - strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1); + b->sockaddr.un = (struct sockaddr_un) { + .sun_family = AF_UNIX, + }; + + memcpy(b->sockaddr.un.sun_path+1, abstract, l); b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l; }