From 5049c32c351584e640903b3e381d3d5ff5fcd1fc Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 13 Nov 2026 16:46:12 +0100 Subject: [PATCH] service: lift the limit of four supplementary groups A service listing more than four supplementary groups, as in @user:group,sup1,sup2,..., got only the first four: too many supplementary groups, max 4 The names were stored in a fixed array of four. It was also only reset when the new configuration listed groups, so groups dropped from a service lingered until reboot. Store the list as one string, split when the service starts, and set it on every (re)registration. A list longer than 511 characters drops the name it cuts off, which may otherwise match another group. Signed-off-by: Joachim Wiberg (cherry picked from commit de73b06415dd474ce30b2099e762f75eed07d86d) Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 5 +++++ src/service.c | 37 ++++++++++++++++++------------------- src/svc.h | 5 ++--- test/Makefile.am | 2 ++ test/conf-groups.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 22 deletions(-) create mode 100755 test/conf-groups.sh diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index bfb06445..50fb5eed 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -34,6 +34,11 @@ All relevant changes are documented in this file. or `pidof`. It was taken as the `log` or `pid` option and the next argument ran as the command, which also broke the `logger` example in finit.conf(5) +- A service can have more than four supplementary groups, as in + `@user:group,sup1,sup2,...`. The list may now be up to 511 + characters. Groups dropped from a service's configuration are now + also dropped the next time it starts, before they lingered 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 8601b712..67b2d8d0 100644 --- a/src/service.c +++ b/src/service.c @@ -625,20 +625,22 @@ static pid_t service_fork(svc_t *svc) { gid_t supgids[NGROUPS_MAX]; int ngroups = NGROUPS_MAX; - int i, j, n = 0; + char *sup, *ptr; + int j, n = 0; /* Get user's supplementary groups from /etc/group */ if (getgrouplist(svc->username, gid, supgids, &ngroups) >= 0) n = ngroups; /* Add explicitly configured supplementary groups */ - for (i = 0; i < svc->num_supgroups && n < NGROUPS_MAX; i++) { - int g = getgroup(svc->supgroups[i]); + for (sup = strtok_r(strdupa(svc->supgroups), ",", &ptr); + sup && n < NGROUPS_MAX; sup = strtok_r(NULL, ",", &ptr)) { + int g = getgroup(sup); int found = 0; if (g < 0) { - warn("%s: unknown supplementary group '%s'", - svc_ident(svc, NULL, 0), svc->supgroups[i]); + warnx("%s: unknown supplementary group '%s'", + svc_ident(svc, NULL, 0), sup); continue; } /* Skip if already in list from /etc/group */ @@ -2140,10 +2142,12 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) svc_unblock(svc); } + svc->supgroups[0] = 0; if (username) { char *ptr = strchr(username, ':'); if (ptr) { + size_t len = sizeof(svc->supgroups); char *sup; *ptr++ = 0; @@ -2151,20 +2155,15 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file) sup = strchr(ptr, ','); if (sup) { *sup++ = 0; - svc->num_supgroups = 0; - while (sup) { - char *next = strchr(sup, ','); - if (next) - *next++ = 0; - if (svc->num_supgroups >= MAX_NUM_SUPGROUPS) { - warn("%s: too many supplementary groups, max %d", - svc->cmd, MAX_NUM_SUPGROUPS); - break; - } - strlcpy(svc->supgroups[svc->num_supgroups], sup, - sizeof(svc->supgroups[0])); - svc->num_supgroups++; - sup = next; + if (strlcpy(svc->supgroups, sup, len) >= len) { + /* Drop the cut-off name, it may match another group */ + sup = strrchr(svc->supgroups, ','); + if (sup) + *sup = 0; + else + svc->supgroups[0] = 0; + warnx("%s: supplementary group list too long, max %zu characters", + svc->cmd, len - 1); } } strlcpy(svc->group, ptr, sizeof(svc->group)); diff --git a/src/svc.h b/src/svc.h index fc3d5a13..1362c1e7 100644 --- a/src/svc.h +++ b/src/svc.h @@ -101,7 +101,7 @@ typedef enum { #define MAX_STR_LEN 64 #define MAX_COND_LEN (MAX_ARG_LEN * 3) #define MAX_USER_LEN MAX_ARG_LEN -#define MAX_NUM_SUPGROUPS 4 +#define MAX_SUPGROUPS_LEN (MAX_CMD_LEN * 2) #define MAX_NUM_FDS 64 /* Max number of I/O plugins */ #define MAX_NUM_SVC_ARGS 64 @@ -195,8 +195,7 @@ typedef struct svc { /* Identity */ char username[MAX_USER_LEN]; char group[MAX_USER_LEN]; - char supgroups[MAX_NUM_SUPGROUPS][MAX_USER_LEN]; - int num_supgroups; + char supgroups[MAX_SUPGROUPS_LEN]; /* sup1,sup2,... */ char capabilities[MAX_CMD_LEN]; /* Command, arguments and service description */ diff --git a/test/Makefile.am b/test/Makefile.am index 95aba16d..c34d4227 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -42,6 +42,7 @@ EXTRA_DIST += global-envs.sh EXTRA_DIST += initctl-status-subset.sh EXTRA_DIST += notify.sh EXTRA_DIST += pidfile.sh +EXTRA_DIST += conf-groups.sh EXTRA_DIST += option-lookalike.sh EXTRA_DIST += pidfile-stopped.sh EXTRA_DIST += stale-pidfile.sh @@ -90,6 +91,7 @@ TESTS += global-envs.sh TESTS += initctl-status-subset.sh TESTS += notify.sh TESTS += pidfile.sh +TESTS += conf-groups.sh TESTS += option-lookalike.sh TESTS += pidfile-stopped.sh TESTS += stale-pidfile.sh diff --git a/test/conf-groups.sh b/test/conf-groups.sh new file mode 100755 index 00000000..6e12ec7a --- /dev/null +++ b/test/conf-groups.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# Verify supplementary groups, @user:group,sup1,sup2,..., are all set on +# the service, and that dropping them drops the groups. +set -eu + +TEST_DIR=$(dirname "$0") + +# Optional argument is the supplementary group list, ",sup1,sup2,..." +write_conf() +{ + run "echo 'service @root:root${1:-} name:grp serv -np -i grp -- Groups' > $FINIT_CONF" +} + +# Kernel sorts the list, so it compares as a plain string. +assert_groups() +{ + pid=$(texec initctl -j status grp | jq .pid) + assert "Service groups == $1" \ + "$(texec cat "/proc/$pid/status" | sed -n 's/^Groups:[[:space:]]*//p' | xargs)" = "$1" +} + +test_teardown() +{ + say "Running test teardown." + run "rm -f $FINIT_CONF" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +say 'Seven supplementary groups' +write_conf ',daemon,bin,sys,adm,tty,disk,dialout' +run "initctl reload" + +retry 'assert_num_children 1 serv' +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 +run "initctl reload" +run "initctl restart grp" + +retry 'assert_groups "0"'