1ff636
From d0986e46b74de3b131fccbf79bd00de5ff054f71 Mon Sep 17 00:00:00 2001
1ff636
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
1ff636
Date: Sat, 6 Jun 2015 18:59:27 -0400
1ff636
Subject: [PATCH] sd-bus: do not use per-datagram auxiliary information
1ff636
1ff636
SELinux information cannot be retrieved this way, since we are
1ff636
using stream unix sockets and SCM_SECURITY does not work for
1ff636
them.
1ff636
1ff636
SCM_CREDENTIALS use dropped to be consistent. We also should
1ff636
get this information at connection time.
1ff636
1ff636
https://bugzilla.redhat.com/show_bug.cgi?id=1224211
1ff636
"SCM_SECURITY was only added for datagram sockets."
1ff636
1ff636
Cherry-picked from: d868f2a
1ff636
Related: #1230190
1ff636
---
23b3cf
 src/libsystemd/sd-bus/bus-socket.c | 70 +++++-------------------------
1ff636
 1 file changed, 12 insertions(+), 58 deletions(-)
1ff636
1ff636
diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c
c62b8e
index 52883fa8cd..abd9ece016 100644
1ff636
--- a/src/libsystemd/sd-bus/bus-socket.c
1ff636
+++ b/src/libsystemd/sd-bus/bus-socket.c
1ff636
@@ -501,9 +501,7 @@ static int bus_socket_read_auth(sd_bus *b) {
1ff636
         void *p;
1ff636
         union {
1ff636
                 struct cmsghdr cmsghdr;
1ff636
-                uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX) +
1ff636
-                            CMSG_SPACE(sizeof(struct ucred)) +
1ff636
-                            CMSG_SPACE(NAME_MAX)]; /*selinux label */
1ff636
+                uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
1ff636
         } control;
1ff636
         struct cmsghdr *cmsg;
1ff636
         bool handle_cmsg = false;
1ff636
@@ -556,8 +554,8 @@ static int bus_socket_read_auth(sd_bus *b) {
1ff636
 
1ff636
         b->rbuffer_size += k;
1ff636
 
1ff636
-        if (handle_cmsg) {
1ff636
-                for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
1ff636
+        if (handle_cmsg)
1ff636
+                for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
1ff636
                         if (cmsg->cmsg_level == SOL_SOCKET &&
1ff636
                             cmsg->cmsg_type == SCM_RIGHTS) {
1ff636
                                 int j;
1ff636
@@ -568,31 +566,9 @@ static int bus_socket_read_auth(sd_bus *b) {
1ff636
                                 j = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
1ff636
                                 close_many((int*) CMSG_DATA(cmsg), j);
1ff636
                                 return -EIO;
1ff636
-
1ff636
-                        } else if (cmsg->cmsg_level == SOL_SOCKET &&
1ff636
-                                   cmsg->cmsg_type == SCM_CREDENTIALS &&
1ff636
-                                   cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
1ff636
-
1ff636
-                                /* Ignore bogus data, which we might
1ff636
-                                 * get on socketpair() sockets */
1ff636
-                                if (((struct ucred*) CMSG_DATA(cmsg))->pid != 0) {
1ff636
-                                        memcpy(&b->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
1ff636
-                                        b->ucred_valid = true;
1ff636
-                                }
1ff636
-
1ff636
-                        } else if (cmsg->cmsg_level == SOL_SOCKET &&
1ff636
-                                   cmsg->cmsg_type == SCM_SECURITY) {
1ff636
-
1ff636
-                                size_t l;
1ff636
-
1ff636
-                                l = cmsg->cmsg_len - CMSG_LEN(0);
1ff636
-                                if (l > 0) {
1ff636
-                                        memcpy(&b->label, CMSG_DATA(cmsg), l);
1ff636
-                                        b->label[l] = 0;
1ff636
-                                }
1ff636
-                        }
1ff636
-                }
1ff636
-        }
1ff636
+                        } else
1ff636
+                                log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1ff636
+                                          cmsg->cmsg_level, cmsg->cmsg_type);
1ff636
 
1ff636
         r = bus_socket_auth_verify(b);
1ff636
         if (r != 0)
1ff636
@@ -945,9 +921,7 @@ int bus_socket_read_message(sd_bus *bus) {
1ff636
         void *b;
1ff636
         union {
1ff636
                 struct cmsghdr cmsghdr;
1ff636
-                uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX) +
1ff636
-                            CMSG_SPACE(sizeof(struct ucred)) +
1ff636
-                            CMSG_SPACE(NAME_MAX)]; /*selinux label */
1ff636
+                uint8_t buf[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX)];
1ff636
         } control;
1ff636
         struct cmsghdr *cmsg;
1ff636
         bool handle_cmsg = false;
1ff636
@@ -995,8 +969,8 @@ int bus_socket_read_message(sd_bus *bus) {
1ff636
 
1ff636
         bus->rbuffer_size += k;
1ff636
 
1ff636
-        if (handle_cmsg) {
1ff636
-                for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
1ff636
+        if (handle_cmsg)
1ff636
+                for (cmsg = CMSG_FIRSTHDR(&mh); cmsg; cmsg = CMSG_NXTHDR(&mh, cmsg))
1ff636
                         if (cmsg->cmsg_level == SOL_SOCKET &&
1ff636
                             cmsg->cmsg_type == SCM_RIGHTS) {
1ff636
                                 int n, *f;
1ff636
@@ -1021,29 +995,9 @@ int bus_socket_read_message(sd_bus *bus) {
1ff636
                                 memcpy(f + bus->n_fds, CMSG_DATA(cmsg), n * sizeof(int));
1ff636
                                 bus->fds = f;
1ff636
                                 bus->n_fds += n;
1ff636
-                        } else if (cmsg->cmsg_level == SOL_SOCKET &&
1ff636
-                                   cmsg->cmsg_type == SCM_CREDENTIALS &&
1ff636
-                                   cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
1ff636
-
1ff636
-                                /* Ignore bogus data, which we might
1ff636
-                                 * get on socketpair() sockets */
1ff636
-                                if (((struct ucred*) CMSG_DATA(cmsg))->pid != 0) {
1ff636
-                                        memcpy(&bus->ucred, CMSG_DATA(cmsg), sizeof(struct ucred));
1ff636
-                                        bus->ucred_valid = true;
1ff636
-                                }
1ff636
-
1ff636
-                        } else if (cmsg->cmsg_level == SOL_SOCKET &&
1ff636
-                                   cmsg->cmsg_type == SCM_SECURITY) {
1ff636
-
1ff636
-                                size_t l;
1ff636
-                                l = cmsg->cmsg_len - CMSG_LEN(0);
1ff636
-                                if (l > 0) {
1ff636
-                                        memcpy(&bus->label, CMSG_DATA(cmsg), l);
1ff636
-                                        bus->label[l] = 0;
1ff636
-                                }
1ff636
-                        }
1ff636
-                }
1ff636
-        }
1ff636
+                        } else
1ff636
+                                log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1ff636
+                                          cmsg->cmsg_level, cmsg->cmsg_type);
1ff636
 
1ff636
         r = bus_socket_read_message_need(bus, &need);
1ff636
         if (r < 0)