richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
594167
From f23d0328e563fa5534377297153daa098664147f Mon Sep 17 00:00:00 2001
594167
From: Michal Sekletar <msekleta@redhat.com>
594167
Date: Wed, 1 Jun 2022 10:15:06 +0200
594167
Subject: [PATCH] scope: allow unprivileged delegation on scopes
594167
594167
Previously it was possible to set delegate property for scope, but you
594167
were not able to allow unprivileged process to manage the scope's cgroup
594167
hierarchy. This is useful when launching manager process that  will run
594167
unprivileged but is supposed to manage its own (scope) sub-hierarchy.
594167
594167
Fixes #21683
594167
594167
(cherry picked from commit 03860190fefce8bbea3a6f0e77919b882ade517c)
594167
594167
Resolves: #2120604
594167
---
594167
 src/basic/unit-def.c       |   1 +
594167
 src/basic/unit-def.h       |   1 +
594167
 src/core/dbus-scope.c      |   6 ++
594167
 src/core/scope.c           | 130 ++++++++++++++++++++++++++++++++-----
594167
 src/core/scope.h           |   3 +
594167
 src/shared/bus-unit-util.c |   5 ++
594167
 test/units/testsuite-19.sh |  14 ++++
594167
 7 files changed, 143 insertions(+), 17 deletions(-)
594167
594167
diff --git a/src/basic/unit-def.c b/src/basic/unit-def.c
594167
index 2667e61dc4..9408141842 100644
594167
--- a/src/basic/unit-def.c
594167
+++ b/src/basic/unit-def.c
594167
@@ -170,6 +170,7 @@ DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
594167
 static const char* const scope_state_table[_SCOPE_STATE_MAX] = {
594167
         [SCOPE_DEAD] = "dead",
594167
         [SCOPE_RUNNING] = "running",
594167
+        [SCOPE_START_CHOWN] = "start-chown",
594167
         [SCOPE_ABANDONED] = "abandoned",
594167
         [SCOPE_STOP_SIGTERM] = "stop-sigterm",
594167
         [SCOPE_STOP_SIGKILL] = "stop-sigkill",
594167
diff --git a/src/basic/unit-def.h b/src/basic/unit-def.h
594167
index f80e554d2b..5fcd51c095 100644
594167
--- a/src/basic/unit-def.h
594167
+++ b/src/basic/unit-def.h
594167
@@ -114,6 +114,7 @@ typedef enum PathState {
594167
 
594167
 typedef enum ScopeState {
594167
         SCOPE_DEAD,
594167
+        SCOPE_START_CHOWN,
594167
         SCOPE_RUNNING,
594167
         SCOPE_ABANDONED,
594167
         SCOPE_STOP_SIGTERM,
594167
diff --git a/src/core/dbus-scope.c b/src/core/dbus-scope.c
594167
index 109ad6f2ef..0f59622166 100644
594167
--- a/src/core/dbus-scope.c
594167
+++ b/src/core/dbus-scope.c
594167
@@ -186,6 +186,12 @@ int bus_scope_set_property(
594167
                 r = bus_kill_context_set_transient_property(u, &s->kill_context, name, message, flags, error);
594167
                 if (r != 0)
594167
                         return r;
594167
+
594167
+                if (streq(name, "User"))
594167
+                        return bus_set_transient_user_relaxed(u, name, &s->user, message, flags, error);
594167
+
594167
+                if (streq(name, "Group"))
594167
+                        return bus_set_transient_user_relaxed(u, name, &s->group, message, flags, error);
594167
         }
594167
 
594167
         return 0;
594167
diff --git a/src/core/scope.c b/src/core/scope.c
594167
index 63d3288caf..a4da45ac43 100644
594167
--- a/src/core/scope.c
594167
+++ b/src/core/scope.c
594167
@@ -6,6 +6,7 @@
594167
 #include "alloc-util.h"
594167
 #include "dbus-scope.h"
594167
 #include "dbus-unit.h"
594167
+#include "exit-status.h"
594167
 #include "load-dropin.h"
594167
 #include "log.h"
594167
 #include "process-util.h"
594167
@@ -18,9 +19,11 @@
594167
 #include "strv.h"
594167
 #include "unit-name.h"
594167
 #include "unit.h"
594167
+#include "user-util.h"
594167
 
594167
 static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
594167
         [SCOPE_DEAD] = UNIT_INACTIVE,
594167
+        [SCOPE_START_CHOWN] = UNIT_ACTIVATING,
594167
         [SCOPE_RUNNING] = UNIT_ACTIVE,
594167
         [SCOPE_ABANDONED] = UNIT_ACTIVE,
594167
         [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
594167
@@ -39,6 +42,7 @@ static void scope_init(Unit *u) {
594167
         s->runtime_max_usec = USEC_INFINITY;
594167
         s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
594167
         u->ignore_on_isolate = true;
594167
+        s->user = s->group = NULL;
594167
 }
594167
 
594167
 static void scope_done(Unit *u) {
594167
@@ -50,6 +54,9 @@ static void scope_done(Unit *u) {
594167
         s->controller_track = sd_bus_track_unref(s->controller_track);
594167
 
594167
         s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
594167
+
594167
+        s->user = mfree(s->user);
594167
+        s->group = mfree(s->group);
594167
 }
594167
 
594167
 static usec_t scope_running_timeout(Scope *s) {
594167
@@ -107,7 +114,7 @@ static void scope_set_state(Scope *s, ScopeState state) {
594167
         old_state = s->state;
594167
         s->state = state;
594167
 
594167
-        if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
594167
+        if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL, SCOPE_START_CHOWN))
594167
                 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
594167
 
594167
         if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) {
594167
@@ -353,26 +360,72 @@ fail:
594167
         scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
594167
 }
594167
 
594167
-static int scope_start(Unit *u) {
594167
-        Scope *s = SCOPE(u);
594167
+static int scope_enter_start_chown(Scope *s) {
594167
+        Unit *u = UNIT(s);
594167
+        pid_t pid;
594167
         int r;
594167
 
594167
         assert(s);
594167
+        assert(s->user);
594167
 
594167
-        if (unit_has_name(u, SPECIAL_INIT_SCOPE))
594167
-                return -EPERM;
594167
+        r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), u->manager->default_timeout_start_usec));
594167
+        if (r < 0)
594167
+                return r;
594167
 
594167
-        if (s->state == SCOPE_FAILED)
594167
-                return -EPERM;
594167
+        r = unit_fork_helper_process(u, "(sd-chown-cgroup)", &pid;;
594167
+        if (r < 0)
594167
+                goto fail;
594167
 
594167
-        /* We can't fulfill this right now, please try again later */
594167
-        if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
594167
-                return -EAGAIN;
594167
+        if (r == 0) {
594167
+                uid_t uid = UID_INVALID;
594167
+                gid_t gid = GID_INVALID;
594167
 
594167
-        assert(s->state == SCOPE_DEAD);
594167
+                if (!isempty(s->user)) {
594167
+                        const char *user = s->user;
594167
 
594167
-        if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
594167
-                return -ENOENT;
594167
+                        r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
594167
+                        if (r < 0) {
594167
+                                log_unit_error_errno(UNIT(s), r, "Failed to resolve user \"%s\": %m", user);
594167
+                                _exit(EXIT_USER);
594167
+                        }
594167
+                }
594167
+
594167
+                if (!isempty(s->group)) {
594167
+                        const char *group = s->group;
594167
+
594167
+                        r = get_group_creds(&group, &gid, 0);
594167
+                        if (r < 0) {
594167
+                                log_unit_error_errno(UNIT(s), r, "Failed to resolve group \"%s\": %m", group);
594167
+                                _exit(EXIT_GROUP);
594167
+                        }
594167
+                }
594167
+
594167
+                r = cg_set_access(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, uid, gid);
594167
+                if (r < 0) {
594167
+                        log_unit_error_errno(UNIT(s), r, "Failed to adjust control group access: %m");
594167
+                        _exit(EXIT_CGROUP);
594167
+                }
594167
+
594167
+                _exit(EXIT_SUCCESS);
594167
+        }
594167
+
594167
+        r = unit_watch_pid(UNIT(s), pid, true);
594167
+        if (r < 0)
594167
+                goto fail;
594167
+
594167
+        scope_set_state(s, SCOPE_START_CHOWN);
594167
+
594167
+        return 1;
594167
+fail:
594167
+        s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
594167
+        return r;
594167
+}
594167
+
594167
+static int scope_enter_running(Scope *s) {
594167
+        Unit *u = UNIT(s);
594167
+        int r;
594167
+
594167
+        assert(s);
594167
 
594167
         (void) bus_scope_track_controller(s);
594167
 
594167
@@ -380,9 +433,6 @@ static int scope_start(Unit *u) {
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
-        (void) unit_realize_cgroup(u);
594167
-        (void) unit_reset_accounting(u);
594167
-
594167
         unit_export_state_files(u);
594167
 
594167
         r = unit_attach_pids_to_cgroup(u, u->pids, NULL);
594167
@@ -416,6 +466,37 @@ static int scope_start(Unit *u) {
594167
         return 1;
594167
 }
594167
 
594167
+static int scope_start(Unit *u) {
594167
+        Scope *s = SCOPE(u);
594167
+
594167
+        assert(s);
594167
+
594167
+        if (unit_has_name(u, SPECIAL_INIT_SCOPE))
594167
+                return -EPERM;
594167
+
594167
+        if (s->state == SCOPE_FAILED)
594167
+                return -EPERM;
594167
+
594167
+        /* We can't fulfill this right now, please try again later */
594167
+        if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
594167
+                return -EAGAIN;
594167
+
594167
+        assert(s->state == SCOPE_DEAD);
594167
+
594167
+        if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
594167
+                return -ENOENT;
594167
+
594167
+        (void) unit_realize_cgroup(u);
594167
+        (void) unit_reset_accounting(u);
594167
+
594167
+        /* We check only for User= option to keep behavior consistent with logic for service units,
594167
+         * i.e. having 'Delegate=true Group=foo' w/o specifing User= has no effect. */
594167
+        if (s->user && unit_cgroup_delegate(u))
594167
+                return scope_enter_start_chown(s);
594167
+
594167
+        return scope_enter_running(s);
594167
+}
594167
+
594167
 static int scope_stop(Unit *u) {
594167
         Scope *s = SCOPE(u);
594167
 
594167
@@ -547,7 +628,17 @@ static void scope_notify_cgroup_empty_event(Unit *u) {
594167
 }
594167
 
594167
 static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
594167
-        assert(u);
594167
+        Scope *s = SCOPE(u);
594167
+
594167
+        assert(s);
594167
+
594167
+        if (s->state == SCOPE_START_CHOWN) {
594167
+                if (!is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
594167
+                        scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
594167
+                else
594167
+                        scope_enter_running(s);
594167
+                return;
594167
+        }
594167
 
594167
         /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
594167
          * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
594167
@@ -585,6 +676,11 @@ static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *user
594167
                 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
594167
                 break;
594167
 
594167
+        case SCOPE_START_CHOWN:
594167
+                log_unit_warning(UNIT(s), "User lookup timed out. Entering failed state.");
594167
+                scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
594167
+                break;
594167
+
594167
         default:
594167
                 assert_not_reached();
594167
         }
594167
diff --git a/src/core/scope.h b/src/core/scope.h
594167
index 03a9ba4324..def1541652 100644
594167
--- a/src/core/scope.h
594167
+++ b/src/core/scope.h
594167
@@ -34,6 +34,9 @@ struct Scope {
594167
         bool was_abandoned;
594167
 
594167
         sd_event_source *timer_event_source;
594167
+
594167
+        char *user;
594167
+        char *group;
594167
 };
594167
 
594167
 extern const UnitVTable scope_vtable;
594167
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
594167
index c211fe34d5..33b0b947b7 100644
594167
--- a/src/shared/bus-unit-util.c
594167
+++ b/src/shared/bus-unit-util.c
594167
@@ -2130,6 +2130,11 @@ static int bus_append_scope_property(sd_bus_message *m, const char *field, const
594167
         if (streq(field, "TimeoutStopSec"))
594167
                 return bus_append_parse_sec_rename(m, field, eq);
594167
 
594167
+        /* Scope units don't have execution context but we still want to allow setting these two,
594167
+         * so let's handle them separately. */
594167
+        if (STR_IN_SET(field, "User", "Group"))
594167
+                return bus_append_string(m, field, eq);
594167
+
594167
         return 0;
594167
 }
594167
 
594167
diff --git a/test/units/testsuite-19.sh b/test/units/testsuite-19.sh
594167
index ee4eb8431e..6ce6d3d429 100755
594167
--- a/test/units/testsuite-19.sh
594167
+++ b/test/units/testsuite-19.sh
594167
@@ -3,6 +3,16 @@
594167
 set -eux
594167
 set -o pipefail
594167
 
594167
+test_scope_unpriv_delegation() {
594167
+    useradd test ||:
594167
+    trap "userdel -r test" RETURN
594167
+
594167
+    systemd-run --uid=test -p User=test -p Delegate=yes --slice workload.slice --unit workload0.scope --scope \
594167
+            test -w /sys/fs/cgroup/workload.slice/workload0.scope -a \
594167
+            -w /sys/fs/cgroup/workload.slice/workload0.scope/cgroup.procs -a \
594167
+            -w /sys/fs/cgroup/workload.slice/workload0.scope/cgroup.subtree_control
594167
+}
594167
+
594167
 if grep -q cgroup2 /proc/filesystems ; then
594167
     systemd-run --wait --unit=test0.service -p "DynamicUser=1" -p "Delegate=" \
594167
                 test -w /sys/fs/cgroup/system.slice/test0.service/ -a \
594167
@@ -31,6 +41,10 @@ if grep -q cgroup2 /proc/filesystems ; then
594167
 
594167
     # And now check again, "io" should have vanished
594167
     grep -qv io /sys/fs/cgroup/system.slice/cgroup.controllers
594167
+
594167
+    # Check that unprivileged delegation works for scopes
594167
+    test_scope_unpriv_delegation
594167
+
594167
 else
594167
     echo "Skipping TEST-19-DELEGATE, as the kernel doesn't actually support cgroup v2" >&2
594167
 fi