dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
923a60
From d491fd1068446f74992e76154e5e9d57bd67e7ac Mon Sep 17 00:00:00 2001
923a60
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
923a60
Date: Sat, 6 Jun 2015 21:24:45 -0400
923a60
Subject: [PATCH] sd-bus: store selinux context at connection time
923a60
923a60
This appears to be the right time to do it for SOCK_STREAM
923a60
unix sockets.
923a60
923a60
Also: condition bus_get_owner_creds_dbus1 was reversed. Split
923a60
it out to a separate variable for clarity and fix.
923a60
923a60
https://bugzilla.redhat.com/show_bug.cgi?id=1224211
923a60
923a60
Cherry-picked from: c4e6556
923a60
Related: #1230190
923a60
---
923a60
 src/libsystemd/sd-bus/bus-control.c  | 6 ++++--
923a60
 src/libsystemd/sd-bus/bus-internal.h | 2 +-
923a60
 src/libsystemd/sd-bus/bus-socket.c   | 7 +++++++
923a60
 src/libsystemd/sd-bus/sd-bus.c       | 1 +
923a60
 4 files changed, 13 insertions(+), 3 deletions(-)
923a60
923a60
diff --git a/src/libsystemd/sd-bus/bus-control.c b/src/libsystemd/sd-bus/bus-control.c
923a60
index 06e5b4fd9a..8b84b9496f 100644
923a60
--- a/src/libsystemd/sd-bus/bus-control.c
923a60
+++ b/src/libsystemd/sd-bus/bus-control.c
923a60
@@ -945,8 +945,10 @@ static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **
923a60
         _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
923a60
         pid_t pid = 0;
923a60
         int r;
923a60
+        bool do_label = bus->label && (mask & SD_BUS_CREDS_SELINUX_CONTEXT);
923a60
 
923a60
-        if (!bus->ucred_valid && !isempty(bus->label))
923a60
+        /* Avoid allocating anything if we have no chance of returning useful data */
923a60
+        if (!bus->ucred_valid && !do_label)
923a60
                 return -ENODATA;
923a60
 
923a60
         c = bus_creds_new();
923a60
@@ -970,7 +972,7 @@ static int bus_get_owner_creds_dbus1(sd_bus *bus, uint64_t mask, sd_bus_creds **
923a60
                 }
923a60
         }
923a60
 
923a60
-        if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
923a60
+        if (do_label) {
923a60
                 c->label = strdup(bus->label);
923a60
                 if (!c->label)
923a60
                         return -ENOMEM;
923a60
diff --git a/src/libsystemd/sd-bus/bus-internal.h b/src/libsystemd/sd-bus/bus-internal.h
923a60
index e9f1a816aa..071b3da790 100644
923a60
--- a/src/libsystemd/sd-bus/bus-internal.h
923a60
+++ b/src/libsystemd/sd-bus/bus-internal.h
923a60
@@ -262,7 +262,7 @@ struct sd_bus {
923a60
         usec_t auth_timeout;
923a60
 
923a60
         struct ucred ucred;
923a60
-        char label[NAME_MAX];
923a60
+        char *label;
923a60
 
923a60
         uint64_t creds_mask;
923a60
 
923a60
diff --git a/src/libsystemd/sd-bus/bus-socket.c b/src/libsystemd/sd-bus/bus-socket.c
923a60
index abd9ece016..d00cd014eb 100644
923a60
--- a/src/libsystemd/sd-bus/bus-socket.c
923a60
+++ b/src/libsystemd/sd-bus/bus-socket.c
923a60
@@ -600,10 +600,17 @@ void bus_socket_setup(sd_bus *b) {
923a60
 }
923a60
 
923a60
 static void bus_get_peercred(sd_bus *b) {
923a60
+        int r;
923a60
+
923a60
         assert(b);
923a60
 
923a60
         /* Get the peer for socketpair() sockets */
923a60
         b->ucred_valid = getpeercred(b->input_fd, &b->ucred) >= 0;
923a60
+
923a60
+        /* Get the SELinux context of the peer */
923a60
+        r = getpeersec(b->input_fd, &b->label);
923a60
+        if (r < 0 && r != -EOPNOTSUPP)
923a60
+                log_debug_errno(r, "Failed to determine peer security context: %m");
923a60
 }
923a60
 
923a60
 static int bus_socket_start_auth_client(sd_bus *b) {
923a60
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
923a60
index cac9b65601..b0a323792b 100644
923a60
--- a/src/libsystemd/sd-bus/sd-bus.c
923a60
+++ b/src/libsystemd/sd-bus/sd-bus.c
923a60
@@ -121,6 +121,7 @@ static void bus_free(sd_bus *b) {
923a60
         if (b->kdbus_buffer)
923a60
                 munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
923a60
 
923a60
+        free(b->label);
923a60
         free(b->rbuffer);
923a60
         free(b->unique_name);
923a60
         free(b->auth_buffer);