a19bc6
From 5a282fc000a52fe98a31ac69832678b1d1d5778d Mon Sep 17 00:00:00 2001
a19bc6
From: Maciej Wereski <m.wereski@partner.samsung.com>
a19bc6
Date: Tue, 8 Sep 2015 15:36:30 +0200
a19bc6
Subject: [PATCH] sd_pid_notify_with_fds: fix computing msg_controllen
a19bc6
a19bc6
CMSG_SPACE(0) may return value other than 0. This caused sendmsg to fail
a19bc6
with EINVAL, when have_pid or n_fds was 0.
a19bc6
a19bc6
Cherry-picked from: a5bd3c32abb00ad945282568fd1a97c180b68047
a19bc6
Resolves: #1381743
a19bc6
---
a19bc6
 src/libsystemd/sd-daemon/sd-daemon.c | 5 +++--
a19bc6
 1 file changed, 3 insertions(+), 2 deletions(-)
a19bc6
a19bc6
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
a19bc6
index 1474321..2c4dd9d 100644
a19bc6
--- a/src/libsystemd/sd-daemon/sd-daemon.c
a19bc6
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
a19bc6
@@ -397,8 +397,9 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
a19bc6
         have_pid = pid != 0 && pid != getpid();
a19bc6
 
a19bc6
         if (n_fds > 0 || have_pid) {
a19bc6
-                msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds) +
a19bc6
-                                        CMSG_SPACE(sizeof(struct ucred) * have_pid);
a19bc6
+                /* CMSG_SPACE(0) may return value different then zero, which results in miscalculated controllen. */
a19bc6
+                msghdr.msg_controllen = (n_fds ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
a19bc6
+                                        CMSG_SPACE(sizeof(struct ucred)) * have_pid;
a19bc6
                 msghdr.msg_control = alloca(msghdr.msg_controllen);
a19bc6
 
a19bc6
                 cmsg = CMSG_FIRSTHDR(&msghdr);