daandemeyer / rpms / systemd

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