Merge pull request #10412 from poettering/sockaddr-sun-path
various fixes related to struct sockaddr_un handling
This commit is contained in:
@@ -730,20 +730,28 @@ static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
|
||||
|
||||
if (path) {
|
||||
l = strlen(path);
|
||||
if (l > sizeof(b->sockaddr.un.sun_path))
|
||||
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)
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -964,9 +972,11 @@ static int parse_container_unix_address(sd_bus *b, const char **p, char **guid)
|
||||
} else
|
||||
b->nspid = 0;
|
||||
|
||||
b->sockaddr.un.sun_family = AF_UNIX;
|
||||
/* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */
|
||||
strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
|
||||
b->sockaddr.un = (struct sockaddr_un) {
|
||||
.sun_family = AF_UNIX,
|
||||
/* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */
|
||||
.sun_path = "/var/run/dbus/system_bus_socket",
|
||||
};
|
||||
b->sockaddr_size = SOCKADDR_UN_LEN(b->sockaddr.un);
|
||||
b->is_local = false;
|
||||
|
||||
|
||||
@@ -40,10 +40,9 @@ static const sd_bus_vtable vtable[] = {
|
||||
static void* thread_server(void *p) {
|
||||
_cleanup_free_ char *suffixed = NULL, *suffixed2 = NULL, *d = NULL;
|
||||
_cleanup_close_ int fd = -1;
|
||||
union sockaddr_union u = {
|
||||
.un.sun_family = AF_UNIX,
|
||||
};
|
||||
union sockaddr_union u = {};
|
||||
const char *path = p;
|
||||
int salen;
|
||||
|
||||
log_debug("Initializing server");
|
||||
|
||||
@@ -66,12 +65,13 @@ static void* thread_server(void *p) {
|
||||
assert_se(symlink(basename(suffixed), suffixed2) >= 0);
|
||||
(void) usleep(100 * USEC_PER_MSEC);
|
||||
|
||||
strncpy(u.un.sun_path, path, sizeof(u.un.sun_path));
|
||||
salen = sockaddr_un_set_path(&u.un, path);
|
||||
assert_se(salen >= 0);
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
|
||||
assert_se(fd >= 0);
|
||||
|
||||
assert_se(bind(fd, &u.sa, SOCKADDR_UN_LEN(u.un)) >= 0);
|
||||
assert_se(bind(fd, &u.sa, salen) >= 0);
|
||||
usleep(100 * USEC_PER_MSEC);
|
||||
|
||||
assert_se(listen(fd, SOMAXCONN) >= 0);
|
||||
|
||||
@@ -444,9 +444,7 @@ _public_ int sd_pid_notify_with_fds(
|
||||
const int *fds,
|
||||
unsigned n_fds) {
|
||||
|
||||
union sockaddr_union sockaddr = {
|
||||
.sa.sa_family = AF_UNIX,
|
||||
};
|
||||
union sockaddr_union sockaddr = {};
|
||||
struct iovec iovec = {
|
||||
.iov_base = (char*) state,
|
||||
};
|
||||
@@ -459,7 +457,7 @@ _public_ int sd_pid_notify_with_fds(
|
||||
struct cmsghdr *cmsg = NULL;
|
||||
const char *e;
|
||||
bool send_ucred;
|
||||
int r;
|
||||
int r, salen;
|
||||
|
||||
if (!state) {
|
||||
r = -EINVAL;
|
||||
@@ -475,14 +473,9 @@ _public_ int sd_pid_notify_with_fds(
|
||||
if (!e)
|
||||
return 0;
|
||||
|
||||
/* Must be an abstract socket, or an absolute path */
|
||||
if (!IN_SET(e[0], '@', '/') || e[1] == 0) {
|
||||
r = -EINVAL;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
if (strlen(e) > sizeof(sockaddr.un.sun_path)) {
|
||||
r = -EINVAL;
|
||||
salen = sockaddr_un_set_path(&sockaddr.un, e);
|
||||
if (salen < 0) {
|
||||
r = salen;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
@@ -495,12 +488,7 @@ _public_ int sd_pid_notify_with_fds(
|
||||
(void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
|
||||
|
||||
iovec.iov_len = strlen(state);
|
||||
|
||||
strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
|
||||
if (sockaddr.un.sun_path[0] == '@')
|
||||
sockaddr.un.sun_path[0] = 0;
|
||||
|
||||
msghdr.msg_namelen = SOCKADDR_UN_LEN(sockaddr.un);
|
||||
msghdr.msg_namelen = salen;
|
||||
|
||||
send_ucred =
|
||||
(pid != 0 && pid != getpid_cached()) ||
|
||||
|
||||
@@ -88,9 +88,9 @@ static int broadcast_groups_get(sd_netlink *nl) {
|
||||
|
||||
int socket_bind(sd_netlink *nl) {
|
||||
socklen_t addrlen;
|
||||
int r, one = 1;
|
||||
int r;
|
||||
|
||||
r = setsockopt(nl->fd, SOL_NETLINK, NETLINK_PKTINFO, &one, sizeof(one));
|
||||
r = setsockopt(nl->fd, SOL_NETLINK, NETLINK_PKTINFO, &const_int_one, sizeof(const_int_one));
|
||||
if (r < 0)
|
||||
return -errno;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user