daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
4bff0a
From bc7c550c444210aa8decf98ac0c1dcd051fcc532 Mon Sep 17 00:00:00 2001
4bff0a
From: Filipe Brandenburger <filbranden@google.com>
4bff0a
Date: Tue, 24 Jul 2018 18:46:01 -0700
4bff0a
Subject: [PATCH] socket-util: Introduce send_one_fd_iov() and
4bff0a
 receive_one_fd_iov()
4bff0a
4bff0a
These take a struct iovec to send data together with the passed FD.
4bff0a
4bff0a
The receive function returns the FD through an output argument. In case data is
4bff0a
received, but no FD is passed, the receive function will set the output
4bff0a
argument to -1 explicitly.
4bff0a
4bff0a
Update code in dynamic-user to use the new helpers.
4bff0a
4bff0a
(cherry picked from commit d34673ecb825aa9ecf6958b0caab792f5061c56a)
4bff0a
4bff0a
Resolves: #1683319
4bff0a
---
4bff0a
 src/basic/socket-util.c | 97 ++++++++++++++++++++++++++++++++---------
4bff0a
 src/basic/socket-util.h | 10 ++++-
4bff0a
 src/core/dynamic-user.c | 57 ++----------------------
4bff0a
 3 files changed, 90 insertions(+), 74 deletions(-)
4bff0a
4bff0a
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
4bff0a
index 3f90a81d35..986bc6e67f 100644
4bff0a
--- a/src/basic/socket-util.c
4bff0a
+++ b/src/basic/socket-util.c
4bff0a
@@ -1011,9 +1011,10 @@ int getpeergroups(int fd, gid_t **ret) {
4bff0a
         return (int) n;
4bff0a
 }
4bff0a
 
4bff0a
-int send_one_fd_sa(
4bff0a
+ssize_t send_one_fd_iov_sa(
4bff0a
                 int transport_fd,
4bff0a
                 int fd,
4bff0a
+                struct iovec *iov, size_t iovlen,
4bff0a
                 const struct sockaddr *sa, socklen_t len,
4bff0a
                 int flags) {
4bff0a
 
4bff0a
@@ -1024,28 +1025,58 @@ int send_one_fd_sa(
4bff0a
         struct msghdr mh = {
4bff0a
                 .msg_name = (struct sockaddr*) sa,
4bff0a
                 .msg_namelen = len,
4bff0a
-                .msg_control = &control,
4bff0a
-                .msg_controllen = sizeof(control),
4bff0a
+                .msg_iov = iov,
4bff0a
+                .msg_iovlen = iovlen,
4bff0a
         };
4bff0a
-        struct cmsghdr *cmsg;
4bff0a
+        ssize_t k;
4bff0a
 
4bff0a
         assert(transport_fd >= 0);
4bff0a
-        assert(fd >= 0);
4bff0a
 
4bff0a
-        cmsg = CMSG_FIRSTHDR(&mh);
4bff0a
-        cmsg->cmsg_level = SOL_SOCKET;
4bff0a
-        cmsg->cmsg_type = SCM_RIGHTS;
4bff0a
-        cmsg->cmsg_len = CMSG_LEN(sizeof(int));
4bff0a
-        memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
4bff0a
+        /*
4bff0a
+         * We need either an FD or data to send.
4bff0a
+         * If there's nothing, return an error.
4bff0a
+         */
4bff0a
+        if (fd < 0 && !iov)
4bff0a
+                return -EINVAL;
4bff0a
 
4bff0a
-        mh.msg_controllen = CMSG_SPACE(sizeof(int));
4bff0a
-        if (sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags) < 0)
4bff0a
-                return -errno;
4bff0a
+        if (fd >= 0) {
4bff0a
+                struct cmsghdr *cmsg;
4bff0a
 
4bff0a
-        return 0;
4bff0a
+                mh.msg_control = &control;
4bff0a
+                mh.msg_controllen = sizeof(control);
4bff0a
+
4bff0a
+                cmsg = CMSG_FIRSTHDR(&mh);
4bff0a
+                cmsg->cmsg_level = SOL_SOCKET;
4bff0a
+                cmsg->cmsg_type = SCM_RIGHTS;
4bff0a
+                cmsg->cmsg_len = CMSG_LEN(sizeof(int));
4bff0a
+                memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
4bff0a
+
4bff0a
+                mh.msg_controllen = CMSG_SPACE(sizeof(int));
4bff0a
+        }
4bff0a
+        k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags);
4bff0a
+        if (k < 0)
4bff0a
+                return (ssize_t) -errno;
4bff0a
+
4bff0a
+        return k;
4bff0a
 }
4bff0a
 
4bff0a
-int receive_one_fd(int transport_fd, int flags) {
4bff0a
+int send_one_fd_sa(
4bff0a
+                int transport_fd,
4bff0a
+                int fd,
4bff0a
+                const struct sockaddr *sa, socklen_t len,
4bff0a
+                int flags) {
4bff0a
+
4bff0a
+        assert(fd >= 0);
4bff0a
+
4bff0a
+        return (int) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, sa, len, flags);
4bff0a
+}
4bff0a
+
4bff0a
+ssize_t receive_one_fd_iov(
4bff0a
+                int transport_fd,
4bff0a
+                struct iovec *iov, size_t iovlen,
4bff0a
+                int flags,
4bff0a
+                int *ret_fd) {
4bff0a
+
4bff0a
         union {
4bff0a
                 struct cmsghdr cmsghdr;
4bff0a
                 uint8_t buf[CMSG_SPACE(sizeof(int))];
4bff0a
@@ -1053,10 +1084,14 @@ int receive_one_fd(int transport_fd, int flags) {
4bff0a
         struct msghdr mh = {
4bff0a
                 .msg_control = &control,
4bff0a
                 .msg_controllen = sizeof(control),
4bff0a
+                .msg_iov = iov,
4bff0a
+                .msg_iovlen = iovlen,
4bff0a
         };
4bff0a
         struct cmsghdr *cmsg, *found = NULL;
4bff0a
+        ssize_t k;
4bff0a
 
4bff0a
         assert(transport_fd >= 0);
4bff0a
+        assert(ret_fd);
4bff0a
 
4bff0a
         /*
4bff0a
          * Receive a single FD via @transport_fd. We don't care for
4bff0a
@@ -1066,8 +1101,9 @@ int receive_one_fd(int transport_fd, int flags) {
4bff0a
          * combination with send_one_fd().
4bff0a
          */
4bff0a
 
4bff0a
-        if (recvmsg(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags) < 0)
4bff0a
-                return -errno;
4bff0a
+        k = recvmsg(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags);
4bff0a
+        if (k < 0)
4bff0a
+                return (ssize_t) -errno;
4bff0a
 
4bff0a
         CMSG_FOREACH(cmsg, &mh) {
4bff0a
                 if (cmsg->cmsg_level == SOL_SOCKET &&
4bff0a
@@ -1079,12 +1115,33 @@ int receive_one_fd(int transport_fd, int flags) {
4bff0a
                 }
4bff0a
         }
4bff0a
 
4bff0a
-        if (!found) {
4bff0a
+        if (!found)
4bff0a
                 cmsg_close_all(&mh);
4bff0a
+
4bff0a
+        /* If didn't receive an FD or any data, return an error. */
4bff0a
+        if (k == 0 && !found)
4bff0a
                 return -EIO;
4bff0a
-        }
4bff0a
 
4bff0a
-        return *(int*) CMSG_DATA(found);
4bff0a
+        if (found)
4bff0a
+                *ret_fd = *(int*) CMSG_DATA(found);
4bff0a
+        else
4bff0a
+                *ret_fd = -1;
4bff0a
+
4bff0a
+        return k;
4bff0a
+}
4bff0a
+
4bff0a
+int receive_one_fd(int transport_fd, int flags) {
4bff0a
+        int fd;
4bff0a
+        ssize_t k;
4bff0a
+
4bff0a
+        k = receive_one_fd_iov(transport_fd, NULL, 0, flags, &fd;;
4bff0a
+        if (k == 0)
4bff0a
+                return fd;
4bff0a
+
4bff0a
+        /* k must be negative, since receive_one_fd_iov() only returns
4bff0a
+         * a positive value if data was received through the iov. */
4bff0a
+        assert(k < 0);
4bff0a
+        return (int) k;
4bff0a
 }
4bff0a
 
4bff0a
 ssize_t next_datagram_size_fd(int fd) {
4bff0a
diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h
4bff0a
index 8e23cf2dbd..82781a0de1 100644
4bff0a
--- a/src/basic/socket-util.h
4bff0a
+++ b/src/basic/socket-util.h
4bff0a
@@ -130,11 +130,19 @@ int getpeercred(int fd, struct ucred *ucred);
4bff0a
 int getpeersec(int fd, char **ret);
4bff0a
 int getpeergroups(int fd, gid_t **ret);
4bff0a
 
4bff0a
+ssize_t send_one_fd_iov_sa(
4bff0a
+                int transport_fd,
4bff0a
+                int fd,
4bff0a
+                struct iovec *iov, size_t iovlen,
4bff0a
+                const struct sockaddr *sa, socklen_t len,
4bff0a
+                int flags);
4bff0a
 int send_one_fd_sa(int transport_fd,
4bff0a
                    int fd,
4bff0a
                    const struct sockaddr *sa, socklen_t len,
4bff0a
                    int flags);
4bff0a
-#define send_one_fd(transport_fd, fd, flags) send_one_fd_sa(transport_fd, fd, NULL, 0, flags)
4bff0a
+#define send_one_fd_iov(transport_fd, fd, iov, iovlen, flags) send_one_fd_iov_sa(transport_fd, fd, iov, iovlen, NULL, 0, flags)
4bff0a
+#define send_one_fd(transport_fd, fd, flags) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, NULL, 0, flags)
4bff0a
+ssize_t receive_one_fd_iov(int transport_fd, struct iovec *iov, size_t iovlen, int flags, int *ret_fd);
4bff0a
 int receive_one_fd(int transport_fd, int flags);
4bff0a
 
4bff0a
 ssize_t next_datagram_size_fd(int fd);
4bff0a
diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c
4bff0a
index 7c5111ddf6..021fd93a76 100644
4bff0a
--- a/src/core/dynamic-user.c
4bff0a
+++ b/src/core/dynamic-user.c
4bff0a
@@ -312,20 +312,8 @@ static int pick_uid(char **suggested_paths, const char *name, uid_t *ret_uid) {
4bff0a
 static int dynamic_user_pop(DynamicUser *d, uid_t *ret_uid, int *ret_lock_fd) {
4bff0a
         uid_t uid = UID_INVALID;
4bff0a
         struct iovec iov = IOVEC_INIT(&uid, sizeof(uid));
4bff0a
-        union {
4bff0a
-                struct cmsghdr cmsghdr;
4bff0a
-                uint8_t buf[CMSG_SPACE(sizeof(int))];
4bff0a
-        } control = {};
4bff0a
-        struct msghdr mh = {
4bff0a
-                .msg_control = &control,
4bff0a
-                .msg_controllen = sizeof(control),
4bff0a
-                .msg_iov = &iov,
4bff0a
-                .msg_iovlen = 1,
4bff0a
-        };
4bff0a
-        struct cmsghdr *cmsg;
4bff0a
-
4bff0a
+        int lock_fd;
4bff0a
         ssize_t k;
4bff0a
-        int lock_fd = -1;
4bff0a
 
4bff0a
         assert(d);
4bff0a
         assert(ret_uid);
4bff0a
@@ -334,15 +322,9 @@ static int dynamic_user_pop(DynamicUser *d, uid_t *ret_uid, int *ret_lock_fd) {
4bff0a
         /* Read the UID and lock fd that is stored in the storage AF_UNIX socket. This should be called with the lock
4bff0a
          * on the socket taken. */
4bff0a
 
4bff0a
-        k = recvmsg(d->storage_socket[0], &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
4bff0a
+        k = receive_one_fd_iov(d->storage_socket[0], &iov, 1, MSG_DONTWAIT, &lock_fd);
4bff0a
         if (k < 0)
4bff0a
-                return -errno;
4bff0a
-
4bff0a
-        cmsg = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, CMSG_LEN(sizeof(int)));
4bff0a
-        if (cmsg)
4bff0a
-                lock_fd = *(int*) CMSG_DATA(cmsg);
4bff0a
-        else
4bff0a
-                cmsg_close_all(&mh); /* just in case... */
4bff0a
+                return (int) k;
4bff0a
 
4bff0a
         *ret_uid = uid;
4bff0a
         *ret_lock_fd = lock_fd;
4bff0a
@@ -352,42 +334,11 @@ static int dynamic_user_pop(DynamicUser *d, uid_t *ret_uid, int *ret_lock_fd) {
4bff0a
 
4bff0a
 static int dynamic_user_push(DynamicUser *d, uid_t uid, int lock_fd) {
4bff0a
         struct iovec iov = IOVEC_INIT(&uid, sizeof(uid));
4bff0a
-        union {
4bff0a
-                struct cmsghdr cmsghdr;
4bff0a
-                uint8_t buf[CMSG_SPACE(sizeof(int))];
4bff0a
-        } control = {};
4bff0a
-        struct msghdr mh = {
4bff0a
-                .msg_control = &control,
4bff0a
-                .msg_controllen = sizeof(control),
4bff0a
-                .msg_iov = &iov,
4bff0a
-                .msg_iovlen = 1,
4bff0a
-        };
4bff0a
-        ssize_t k;
4bff0a
 
4bff0a
         assert(d);
4bff0a
 
4bff0a
         /* Store the UID and lock_fd in the storage socket. This should be called with the socket pair lock taken. */
4bff0a
-
4bff0a
-        if (lock_fd >= 0) {
4bff0a
-                struct cmsghdr *cmsg;
4bff0a
-
4bff0a
-                cmsg = CMSG_FIRSTHDR(&mh);
4bff0a
-                cmsg->cmsg_level = SOL_SOCKET;
4bff0a
-                cmsg->cmsg_type = SCM_RIGHTS;
4bff0a
-                cmsg->cmsg_len = CMSG_LEN(sizeof(int));
4bff0a
-                memcpy(CMSG_DATA(cmsg), &lock_fd, sizeof(int));
4bff0a
-
4bff0a
-                mh.msg_controllen = CMSG_SPACE(sizeof(int));
4bff0a
-        } else {
4bff0a
-                mh.msg_control = NULL;
4bff0a
-                mh.msg_controllen = 0;
4bff0a
-        }
4bff0a
-
4bff0a
-        k = sendmsg(d->storage_socket[1], &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
4bff0a
-        if (k < 0)
4bff0a
-                return -errno;
4bff0a
-
4bff0a
-        return 0;
4bff0a
+        return send_one_fd_iov(d->storage_socket[1], lock_fd, &iov, 1, MSG_DONTWAIT);
4bff0a
 }
4bff0a
 
4bff0a
 static void unlink_uid_lock(int lock_fd, uid_t uid, const char *name) {