chiark / gitweb /
sd_pid_notify_with_fds: fix computing msg_controllen
authorMaciej Wereski <m.wereski@partner.samsung.com>
Tue, 8 Sep 2015 13:36:30 +0000 (15:36 +0200)
committerSven Eden <yamakuzure@gmx.net>
Wed, 29 Mar 2017 08:45:09 +0000 (10:45 +0200)
CMSG_SPACE(0) may return value other than 0. This caused sendmsg to fail
with EINVAL, when have_pid or n_fds was 0.

src/libelogind/sd-daemon/sd-daemon.c

index 7639b6d3bfe5ae8d833b4696945fc1d200d6fd46..f74f44c65142ec4d31339dad400919c0b1523b25 100644 (file)
@@ -406,8 +406,9 @@ _public_ int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char
         have_pid = pid != 0 && pid != getpid();
 
         if (n_fds > 0 || have_pid) {
-                msghdr.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds) +
-                                        CMSG_SPACE(sizeof(struct ucred) * have_pid);
+                /* CMSG_SPACE(0) may return value different then zero, which results in miscalculated controllen. */
+                msghdr.msg_controllen = (n_fds ? CMSG_SPACE(sizeof(int) * n_fds) : 0) +
+                                        CMSG_SPACE(sizeof(struct ucred)) * have_pid;
                 msghdr.msg_control = alloca(msghdr.msg_controllen);
 
                 cmsg = CMSG_FIRSTHDR(&msghdr);