572a44
From 96bb682b6fa33faaff5c3db7506e9864fe5c09dc Mon Sep 17 00:00:00 2001
572a44
From: Dan Walsh <dwalsh@redhat.com>
572a44
Date: Thu, 16 Jan 2014 12:59:10 +0100
572a44
Subject: [PATCH] Fix SELinux check for transient units. (#1008864)
572a44
572a44
SELinux does not have a path to check for a snapshot servic creation.
572a44
This ends up giving us a bogus check.
572a44
572a44
On snapshot creation we should check if the remote process type, has the ability to start a service with the type that systemd is running with.
572a44
---
572a44
 src/core/dbus-manager.c   |  6 +++---
572a44
 src/core/selinux-access.c |  9 +++++----
572a44
 src/core/selinux-access.h | 13 +++++++++++++
572a44
 3 files changed, 21 insertions(+), 7 deletions(-)
572a44
572a44
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
572a44
index 8f4d017..7be5d13 100644
572a44
--- a/src/core/dbus-manager.c
572a44
+++ b/src/core/dbus-manager.c
572a44
@@ -1102,7 +1102,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
572a44
                 dbus_bool_t cleanup;
572a44
                 Snapshot *s;
572a44
 
572a44
-                SELINUX_ACCESS_CHECK(connection, message, "start");
572a44
+                SELINUX_RUNTIME_UNIT_ACCESS_CHECK(connection, message, "start");
572a44
 
572a44
                 if (!dbus_message_get_args(
572a44
                                     message,
572a44
@@ -1155,7 +1155,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
572a44
                         return bus_send_error_reply(connection, message, &error, -ENOENT);
572a44
                 }
572a44
 
572a44
-                SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "stop");
572a44
+                SELINUX_RUNTIME_UNIT_ACCESS_CHECK(connection, message, "stop");
572a44
                 snapshot_remove(SNAPSHOT(u));
572a44
 
572a44
                 reply = dbus_message_new_method_return(message);
572a44
@@ -1765,7 +1765,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
572a44
                 if (r < 0)
572a44
                         return bus_send_error_reply(connection, message, &error, r);
572a44
 
572a44
-                SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "start");
572a44
+                SELINUX_RUNTIME_UNIT_ACCESS_CHECK(connection, message, "start");
572a44
 
572a44
                 if (u->load_state != UNIT_NOT_FOUND || set_size(u->dependencies[UNIT_REFERENCED_BY]) > 0) {
572a44
                         dbus_set_error(&error, BUS_ERROR_UNIT_EXISTS, "Unit %s already exists.", name);
572a44
diff --git a/src/core/selinux-access.c b/src/core/selinux-access.c
572a44
index 0a3ee18..5d85b26 100644
572a44
--- a/src/core/selinux-access.c
572a44
+++ b/src/core/selinux-access.c
572a44
@@ -374,8 +374,9 @@ int selinux_access_check(
572a44
                 goto finish;
572a44
         }
572a44
 
572a44
-        if (path) {
572a44
-                tclass = "service";
572a44
+
572a44
+        tclass = "service";
572a44
+        if (path && !strneq(path,"system", strlen("system"))) {
572a44
                 /* get the file context of the unit file */
572a44
                 r = getfilecon(path, &fcon);
572a44
                 if (r < 0) {
572a44
@@ -384,9 +385,9 @@ int selinux_access_check(
572a44
                         log_error("Failed to get security context on %s: %m",path);
572a44
                         goto finish;
572a44
                 }
572a44
-
572a44
         } else {
572a44
-                tclass = "system";
572a44
+                if (path)
572a44
+                        tclass = "system";
572a44
                 r = getcon(&fcon);
572a44
                 if (r < 0) {
572a44
                         dbus_set_error(error, DBUS_ERROR_ACCESS_DENIED, "Failed to get current context.");
572a44
diff --git a/src/core/selinux-access.h b/src/core/selinux-access.h
572a44
index 2d7ac64..92acd69 100644
572a44
--- a/src/core/selinux-access.h
572a44
+++ b/src/core/selinux-access.h
572a44
@@ -36,6 +36,18 @@ int selinux_access_check(DBusConnection *connection, DBusMessage *message, const
572a44
                 DBusConnection *_c = (connection);                      \
572a44
                 DBusMessage *_m = (message);                            \
572a44
                 dbus_error_init(&_error);                               \
572a44
+                _r = selinux_access_check(_c, _m, "system", (permission), &_error); \
572a44
+                if (_r < 0)                                             \
572a44
+                        return bus_send_error_reply(_c, _m, &_error, _r); \
572a44
+        } while (false)
572a44
+
572a44
+#define SELINUX_RUNTIME_UNIT_ACCESS_CHECK(connection, message, permission) \
572a44
+        do {                                                            \
572a44
+                DBusError _error;                                       \
572a44
+                int _r;                                                 \
572a44
+                DBusConnection *_c = (connection);                      \
572a44
+                DBusMessage *_m = (message);                            \
572a44
+                dbus_error_init(&_error);                               \
572a44
                 _r = selinux_access_check(_c, _m, NULL, (permission), &_error); \
572a44
                 if (_r < 0)                                             \
572a44
                         return bus_send_error_reply(_c, _m, &_error, _r); \
572a44
@@ -57,6 +69,7 @@ int selinux_access_check(DBusConnection *connection, DBusMessage *message, const
572a44
 #else
572a44
 
572a44
 #define SELINUX_ACCESS_CHECK(connection, message, permission) do { } while (false)
572a44
+#define SELINUX_RUNTIME_UNIT_ACCESS_CHECK(connection, message, permission) do { } while (false)
572a44
 #define SELINUX_UNIT_ACCESS_CHECK(unit, connection, message, permission) do { } while (false)
572a44
 
572a44
 #endif