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.
This commit is contained in:
Lennart Poettering 2018-10-15 18:22:04 +02:00
parent 1d261418e2
commit 95cb14b068
1 changed files with 15 additions and 7 deletions

View File

@ -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;
}