Blame SOURCES/0725-sd-dameon-also-sent-ucred-when-our-UID-differs-from-.patch

17b0f1
From d06dfdde758e178d1ae20756890302a5c265ac08 Mon Sep 17 00:00:00 2001
17b0f1
From: Lennart Poettering <lennart@poettering.net>
17b0f1
Date: Fri, 5 Jan 2018 13:24:58 +0100
17b0f1
Subject: [PATCH] sd-dameon: also sent ucred when our UID differs from EUID
17b0f1
17b0f1
Let's be explicit, and always send the messages from our UID and never
17b0f1
our EUID. Previously this behaviour was conditionalized only on whether
17b0f1
the PID was specified, which made this non-obvious.
17b0f1
17b0f1
(cherry picked from commit 9e1d021ee3f147486c5cfac69b3cbf6f4b36eb79)
17b0f1
17b0f1
Related: #1663143
17b0f1
---
17b0f1
 src/libsystemd/sd-daemon/sd-daemon.c | 39 +++++++++++++++++++---------
17b0f1
 1 file changed, 27 insertions(+), 12 deletions(-)
17b0f1
17b0f1
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
17b0f1
index 2c4dd9d225..82483a38e6 100644
17b0f1
--- a/src/libsystemd/sd-daemon/sd-daemon.c
17b0f1
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
17b0f1
@@ -40,6 +40,8 @@
17b0f1
 #include "socket-util.h"
17b0f1
 #include "sd-daemon.h"
17b0f1
 
17b0f1
+#define SNDBUF_SIZE (8*1024*1024)
17b0f1
+
17b0f1
 _public_ int sd_listen_fds(int unset_environment) {
17b0f1
         const char *e;
17b0f1
         unsigned n;
17b0f1
@@ -340,7 +342,13 @@ _public_ int sd_is_mq(int fd, const char *path) {
17b0f1
         return 1;
17b0f1
 }
17b0f1
 
17b0f1
-_public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds) {
17b0f1
+_public_ int sd_pid_notify_with_fds(
17b0f1
+                pid_t pid,
17b0f1
+                int unset_environment,
17b0f1
+                const char *state,
17b0f1
+                const int *fds,
17b0f1
+                unsigned n_fds) {
17b0f1
+
17b0f1
         union sockaddr_union sockaddr = {
17b0f1
                 .sa.sa_family = AF_UNIX,
17b0f1
         };
17b0f1
@@ -355,7 +363,7 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
17b0f1
         _cleanup_close_ int fd = -1;
17b0f1
         struct cmsghdr *cmsg = NULL;
17b0f1
         const char *e;
17b0f1
-        bool have_pid;
17b0f1
+        bool send_ucred;
17b0f1
         int r;
17b0f1
 
17b0f1
         if (!state) {
17b0f1
@@ -384,6 +392,8 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
17b0f1
                 goto finish;
17b0f1
         }
17b0f1
 
17b0f1
+        (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
17b0f1
+
17b0f1
         iovec.iov_len = strlen(state);
17b0f1
 
17b0f1
         strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
17b0f1
@@ -394,13 +404,18 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
17b0f1
         if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
17b0f1
                 msghdr.msg_namelen = sizeof(struct sockaddr_un);
17b0f1
 
17b0f1
-        have_pid = pid != 0 && pid != getpid();
17b0f1
+        send_ucred =
17b0f1
+                (pid != 0 && pid != getpid()) ||
17b0f1
+                getuid() != geteuid() ||
17b0f1
+                getgid() != getegid();
17b0f1
+
17b0f1
+        if (n_fds > 0 || send_ucred) {
17b0f1
+                /* CMSG_SPACE(0) may return value different than zero, which results in miscalculated controllen. */
17b0f1
+                msghdr.msg_controllen =
17b0f1
+                        (n_fds > 0 ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
17b0f1
+                        (send_ucred ? CMSG_SPACE(sizeof(struct ucred)) : 0);
17b0f1
 
17b0f1
-        if (n_fds > 0 || have_pid) {
17b0f1
-                /* CMSG_SPACE(0) may return value different then zero, which results in miscalculated controllen. */
17b0f1
-                msghdr.msg_controllen = (n_fds ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
17b0f1
-                                        CMSG_SPACE(sizeof(struct ucred)) * have_pid;
17b0f1
-                msghdr.msg_control = alloca(msghdr.msg_controllen);
17b0f1
+                msghdr.msg_control = alloca0(msghdr.msg_controllen);
17b0f1
 
17b0f1
                 cmsg = CMSG_FIRSTHDR(&msghdr);
17b0f1
                 if (n_fds > 0) {
17b0f1
@@ -410,11 +425,11 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
17b0f1
 
17b0f1
                         memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
17b0f1
 
17b0f1
-                        if (have_pid)
17b0f1
+                        if (send_ucred)
17b0f1
                                 assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
17b0f1
                 }
17b0f1
 
17b0f1
-                if (have_pid) {
17b0f1
+                if (send_ucred) {
17b0f1
                         struct ucred *ucred;
17b0f1
 
17b0f1
                         cmsg->cmsg_level = SOL_SOCKET;
17b0f1
@@ -422,7 +437,7 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
17b0f1
                         cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
17b0f1
 
17b0f1
                         ucred = (struct ucred*) CMSG_DATA(cmsg);
17b0f1
-                        ucred->pid = pid;
17b0f1
+                        ucred->pid = pid != 0 ? pid : getpid();
17b0f1
                         ucred->uid = getuid();
17b0f1
                         ucred->gid = getgid();
17b0f1
                 }
17b0f1
@@ -435,7 +450,7 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
17b0f1
         }
17b0f1
 
17b0f1
         /* If that failed, try with our own ucred instead */
17b0f1
-        if (have_pid) {
17b0f1
+        if (send_ucred) {
17b0f1
                 msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
17b0f1
                 if (msghdr.msg_controllen == 0)
17b0f1
                         msghdr.msg_control = NULL;