1ff636
From d491fd1068446f74992e76154e5e9d57bd67e7ac 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 21:24:45 -0400
1ff636
Subject: [PATCH] sd-bus: store selinux context at connection time
1ff636
1ff636
This appears to be the right time to do it for SOCK_STREAM
1ff636
unix sockets.
1ff636
1ff636
Also: condition bus_get_owner_creds_dbus1 was reversed. Split
1ff636
it out to a separate variable for clarity and fix.
1ff636
1ff636
https://bugzilla.redhat.com/show_bug.cgi?id=1224211
1ff636
1ff636
Cherry-picked from: c4e6556
1ff636
Related: #1230190
1ff636
---
1ff636
 src/libsystemd/sd-bus/bus-control.c  | 6 ++++--
1ff636
 src/libsystemd/sd-bus/bus-internal.h | 2 +-
1ff636
 src/libsystemd/sd-bus/bus-socket.c   | 7 +++++++
1ff636
 src/libsystemd/sd-bus/sd-bus.c       | 1 +
1ff636
 4 files changed, 13 insertions(+), 3 deletions(-)
1ff636
1ff636
diff --git a/src/libsystemd/sd-bus/bus-control.c b/src/libsystemd/sd-bus/bus-control.c
181b3f
index 06e5b4fd9..8b84b9496 100644
1ff636
--- a/src/libsystemd/sd-bus/bus-control.c
1ff636
+++ b/src/libsystemd/sd-bus/bus-control.c
1ff636
@@ -945,8 +945,10 @@ static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **
1ff636
         _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
1ff636
         pid_t pid = 0;
1ff636
         int r;
1ff636
+        bool do_label = bus->label && (mask & SD_BUS_CREDS_SELINUX_CONTEXT);
1ff636
 
1ff636
-        if (!bus->ucred_valid && !isempty(bus->label))
1ff636
+        /* Avoid allocating anything if we have no chance of returning useful data */
1ff636
+        if (!bus->ucred_valid && !do_label)
1ff636
                 return -ENODATA;
1ff636
 
1ff636
         c = bus_creds_new();
1ff636
@@ -970,7 +972,7 @@ static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **
1ff636
                 }
1ff636
         }
1ff636
 
1ff636
-        if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
1ff636
+        if (do_label) {
1ff636
                 c->label = strdup(bus->label);
1ff636
                 if (!c->label)
1ff636
                         return -ENOMEM;
1ff636
diff --git a/src/libsystemd/sd-bus/bus-internal.h b/src/libsystemd/sd-bus/bus-internal.h
181b3f
index e9f1a816a..071b3da79 100644
1ff636
--- a/src/libsystemd/sd-bus/bus-internal.h
1ff636
+++ b/src/libsystemd/sd-bus/bus-internal.h
1ff636
@@ -262,7 +262,7 @@ struct sd_bus {
1ff636
         usec_t auth_timeout;
1ff636
 
1ff636
         struct ucred ucred;
1ff636
-        char label[NAME_MAX];
1ff636
+        char *label;
1ff636
 
1ff636
         uint64_t creds_mask;
1ff636
 
1ff636
diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c
181b3f
index abd9ece01..d00cd014e 100644
1ff636
--- a/src/libsystemd/sd-bus/bus-socket.c
1ff636
+++ b/src/libsystemd/sd-bus/bus-socket.c
1ff636
@@ -600,10 +600,17 @@ void bus_socket_setup(sd_bus *b) {
1ff636
 }
1ff636
 
1ff636
 static void bus_get_peercred(sd_bus *b) {
1ff636
+        int r;
1ff636
+
1ff636
         assert(b);
1ff636
 
1ff636
         /* Get the peer for socketpair() sockets */
1ff636
         b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
1ff636
+
1ff636
+        /* Get the SELinux context of the peer */
1ff636
+        r = getpeersec(b->input_fd, &b->label);
1ff636
+        if (r < 0 && r != -EOPNOTSUPP)
1ff636
+                log_debug_errno(r, "Failed to determine peer security context: %m");
1ff636
 }
1ff636
 
1ff636
 static int bus_socket_start_auth_client(sd_bus *b) {
1ff636
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
181b3f
index cac9b6560..b0a323792 100644
1ff636
--- a/src/libsystemd/sd-bus/sd-bus.c
1ff636
+++ b/src/libsystemd/sd-bus/sd-bus.c
1ff636
@@ -121,6 +121,7 @@ static void bus_free(sd_bus *b) {
1ff636
         if (b->kdbus_buffer)
1ff636
                 munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
1ff636
 
1ff636
+        free(b->label);
1ff636
         free(b->rbuffer);
1ff636
         free(b->unique_name);
1ff636
         free(b->auth_buffer);