anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
803fb7
From f065b88b17bd569dda412b2e6f34d921f7badb79 Mon Sep 17 00:00:00 2001
803fb7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
803fb7
Date: Fri, 13 Mar 2015 21:22:05 -0500
803fb7
Subject: [PATCH] sd-daemon: simplify sd_pid_notify_with_fds
803fb7
803fb7
Coverity was complaining that CMSG_NXTHDR is used without
803fb7
checking the return value. In this case it cannot fail, but
803fb7
it is a good excuse to simplify the function a bit.
803fb7
803fb7
CID #1261726.
803fb7
803fb7
(cherry picked from commit 64144440a5d2d94482f882b992fd2a4e0dca7a05)
803fb7
803fb7
http://lists.freedesktop.org/archives/systemd-devel/2015-April/031348.html
803fb7
803fb7
Cherry-picked from: c1258d6
803fb7
Resolves: #1222517
803fb7
---
de8967
 src/libsystemd/sd-daemon/sd-daemon.c | 61 ++++++++++++----------------
803fb7
 1 file changed, 27 insertions(+), 34 deletions(-)
803fb7
803fb7
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
803fb7
index 22a3a5347..1474321c9 100644
803fb7
--- a/src/libsystemd/sd-daemon/sd-daemon.c
803fb7
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
803fb7
@@ -352,12 +352,10 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
803fb7
                 .msg_iovlen = 1,
803fb7
                 .msg_name = &sockaddr,
803fb7
         };
803fb7
-        struct cmsghdr *control;
803fb7
         _cleanup_close_ int fd = -1;
803fb7
         struct cmsghdr *cmsg = NULL;
803fb7
         const char *e;
803fb7
-        size_t controllen_without_ucred = 0;
803fb7
-        bool try_without_ucred = false;
803fb7
+        bool have_pid;
803fb7
         int r;
803fb7
 
803fb7
         if (!state) {
803fb7
@@ -396,42 +394,37 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
803fb7
         if (msghdr.msg_namelen > sizeof(struct sockaddr_un))
803fb7
                 msghdr.msg_namelen = sizeof(struct sockaddr_un);
803fb7
 
803fb7
-        control = alloca(CMSG_SPACE(sizeof(struct ucred)) + CMSG_SPACE(sizeof(int) * n_fds));
803fb7
+        have_pid = pid != 0 && pid != getpid();
803fb7
 
803fb7
-        if (n_fds > 0) {
803fb7
-                msghdr.msg_control = control;
803fb7
-                msghdr.msg_controllen = CMSG_LEN(sizeof(int) * n_fds);
803fb7
+        if (n_fds > 0 || have_pid) {
803fb7
+                msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds) +
803fb7
+                                        CMSG_SPACE(sizeof(struct ucred) * have_pid);
803fb7
+                msghdr.msg_control = alloca(msghdr.msg_controllen);
803fb7
 
803fb7
                 cmsg = CMSG_FIRSTHDR(&msghdr);
803fb7
-                cmsg->cmsg_level = SOL_SOCKET;
803fb7
-                cmsg->cmsg_type = SCM_RIGHTS;
803fb7
-                cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
803fb7
+                if (n_fds > 0) {
803fb7
+                        cmsg->cmsg_level = SOL_SOCKET;
803fb7
+                        cmsg->cmsg_type = SCM_RIGHTS;
803fb7
+                        cmsg->cmsg_len = CMSG_LEN(sizeof(int) * n_fds);
803fb7
 
803fb7
-                memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
803fb7
-        }
803fb7
-
803fb7
-        if (pid != 0 && pid != getpid()) {
803fb7
-                struct ucred *ucred;
803fb7
-
803fb7
-                try_without_ucred = true;
803fb7
-                controllen_without_ucred = msghdr.msg_controllen;
803fb7
+                        memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * n_fds);
803fb7
 
803fb7
-                msghdr.msg_control = control;
803fb7
-                msghdr.msg_controllen += CMSG_LEN(sizeof(struct ucred));
803fb7
+                        if (have_pid)
803fb7
+                                assert_se(cmsg = CMSG_NXTHDR(&msghdr, cmsg));
803fb7
+                }
803fb7
 
803fb7
-                if (cmsg)
803fb7
-                        cmsg = CMSG_NXTHDR(&msghdr, cmsg);
803fb7
-                else
803fb7
-                        cmsg = CMSG_FIRSTHDR(&msghdr);
803fb7
+                if (have_pid) {
803fb7
+                        struct ucred *ucred;
803fb7
 
803fb7
-                cmsg->cmsg_level = SOL_SOCKET;
803fb7
-                cmsg->cmsg_type = SCM_CREDENTIALS;
803fb7
-                cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
803fb7
+                        cmsg->cmsg_level = SOL_SOCKET;
803fb7
+                        cmsg->cmsg_type = SCM_CREDENTIALS;
803fb7
+                        cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
803fb7
 
803fb7
-                ucred = (struct ucred*) CMSG_DATA(cmsg);
803fb7
-                ucred->pid = pid;
803fb7
-                ucred->uid = getuid();
803fb7
-                ucred->gid = getgid();
803fb7
+                        ucred = (struct ucred*) CMSG_DATA(cmsg);
803fb7
+                        ucred->pid = pid;
803fb7
+                        ucred->uid = getuid();
803fb7
+                        ucred->gid = getgid();
803fb7
+                }
803fb7
         }
803fb7
 
803fb7
         /* First try with fake ucred data, as requested */
803fb7
@@ -441,10 +434,10 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
803fb7
         }
803fb7
 
803fb7
         /* If that failed, try with our own ucred instead */
803fb7
-        if (try_without_ucred) {
803fb7
-                if (controllen_without_ucred <= 0)
803fb7
+        if (have_pid) {
803fb7
+                msghdr.msg_controllen -= CMSG_SPACE(sizeof(struct ucred));
803fb7
+                if (msghdr.msg_controllen == 0)
803fb7
                         msghdr.msg_control = NULL;
803fb7
-                msghdr.msg_controllen = controllen_without_ucred;
803fb7
 
803fb7
                 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) >= 0) {
803fb7
                         r = 1;