From 8a5102e7140462c32310a39a311fb990e87d4beb Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 13 Nov 2026 16:47:15 +0100 Subject: [PATCH] service: drop a service's group when the configuration drops it Changing a service from @alice:wheel to @alice and reloading kept the service running as group wheel, also after a restart. The group is only set when the configuration has one, and on reload the service is updated in place, so the old one stayed. Clear it before parsing, like a new service gets it. Signed-off-by: Joachim Wiberg (cherry picked from commit bf42811a13dcafb5f0fc5e8e86e56d079fd0a90b) Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 3 +++ src/service.c | 1 + test/conf-groups.sh | 35 +++++++++++++++++++++++++++++------ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 50fb5eed..7ac80d04 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -39,6 +39,9 @@ All relevant changes are documented in this file. characters. Groups dropped from a service's configuration are now also dropped the next time it starts, before they lingered until reboot +- Fix a service's group lingering after it was dropped from the + configuration, e.g. `@alice:wheel` changed to `@alice` kept running + as `wheel` until reboot - Remove stale daemon-owned (`pid:!`) pidfiles after unclean exits. When a daemon dies via SIGKILL/OOM/segfault, or exits early during startup, its pidfile lingers and can prevent the next instance from diff --git a/src/service.c b/src/service.c index 67b2d8d0..12c34123 100644 --- a/src/service.c +++ b/src/service.c @@ -2142,6 +2142,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) svc_unblock(svc); } + svc->group[0] = 0; svc->supgroups[0] = 0; if (username) { char *ptr = strchr(username, ':'); diff --git a/test/conf-groups.sh b/test/conf-groups.sh index 6e12ec7a..3f23491b 100755 --- a/test/conf-groups.sh +++ b/test/conf-groups.sh @@ -1,14 +1,19 @@ #!/bin/sh -# Verify supplementary groups, @user:group,sup1,sup2,..., are all set on -# the service, and that dropping them drops the groups. +# Verify group and supplementary groups, @user:group,sup1,sup2,..., are +# set on the service, and that dropping either drops the group(s) too. set -eu TEST_DIR=$(dirname "$0") -# Optional argument is the supplementary group list, ",sup1,sup2,..." +# Optional arguments are the group and the supplementary group list, +# ",sup1,sup2,...", the latter requires a group write_conf() { - run "echo 'service @root:root${1:-} name:grp serv -np -i grp -- Groups' > $FINIT_CONF" + user="@root" + if [ -n "${1:-}" ]; then + user="@root:$1${2:-}" + fi + run "echo 'service $user name:grp serv -np -i grp -- Groups' > $FINIT_CONF" } # Kernel sorts the list, so it compares as a plain string. @@ -19,6 +24,13 @@ assert_groups() "$(texec cat "/proc/$pid/status" | sed -n 's/^Groups:[[:space:]]*//p' | xargs)" = "$1" } +assert_gid() +{ + pid=$(texec initctl -j status grp | jq .pid) + assert "Service gid == $1" \ + "$(texec cat "/proc/$pid/status" | awk '/^Gid:/ {print $2}')" = "$1" +} + test_teardown() { say "Running test teardown." @@ -29,7 +41,7 @@ test_teardown() . "$TEST_DIR/lib/setup.sh" say 'Seven supplementary groups' -write_conf ',daemon,bin,sys,adm,tty,disk,dialout' +write_conf root ',daemon,bin,sys,adm,tty,disk,dialout' run "initctl reload" retry 'assert_num_children 1 serv' @@ -37,8 +49,19 @@ retry 'assert_groups "0 1 2 3 4 5 6 20"' # The running process keeps its groups, the next start drops them say 'Dropping supplementary groups drops the groups' -write_conf +write_conf root run "initctl reload" run "initctl restart grp" retry 'assert_groups "0"' + +say 'Dropping group drops the group' +write_conf adm +run "initctl reload" +run "initctl restart grp" +retry 'assert_gid 4' + +write_conf +run "initctl reload" +run "initctl restart grp" +retry 'assert_gid 0'