service: lift the limit of four supplementary groups

A service listing more than four supplementary groups, in extra-groups
or as @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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-17 06:21:24 +01:00
1 parent 8451c4087a
commit de73b06415
5 files changed
+76 -22

No files matched your search

+4
View File
@@ -143,6 +143,10 @@ All relevant changes are documented in this file.
- Service `user:` and `group:` names, including supplementary groups, can - Service `user:` and `group:` names, including supplementary groups, can
now be up to 63 characters, up from 15. Longer names were silently now be up to 63 characters, up from 15. Longer names were silently
truncated before the lookup, so the service never started. By Anees Rehman truncated before the lookup, so the service never started. By Anees Rehman
- A service can have more than four supplementary groups, `extra-groups` or
`@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
- `stop:` and `reload:` scripts take a timeout of their own, `stop:5,/path`, - `stop:` and `reload:` scripts take a timeout of their own, `stop:5,/path`,
defaulting to the kill delay as before. Such a line used to crash Finit at defaulting to the kill delay as before. Such a line used to crash Finit at
config load config load
+18 -19
View File
@@ -780,20 +780,22 @@ static pid_t service_fork(svc_t *svc)
{ {
gid_t supgids[NGROUPS_MAX]; gid_t supgids[NGROUPS_MAX];
int ngroups = 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 */ /* Get user's supplementary groups from /etc/group */
if (getgrouplist(svc->username, gid, supgids, &ngroups) >= 0) if (getgrouplist(svc->username, gid, supgids, &ngroups) >= 0)
n = ngroups; n = ngroups;
/* Add explicitly configured supplementary groups */ /* Add explicitly configured supplementary groups */
for (i = 0; i < svc->num_supgroups && n < NGROUPS_MAX; i++) { for (sup = strtok_r(strdupa(svc->supgroups), ",", &ptr);
int g = getgroup(svc->supgroups[i]); sup && n < NGROUPS_MAX; sup = strtok_r(NULL, ",", &ptr)) {
int g = getgroup(sup);
int found = 0; int found = 0;
if (g < 0) { if (g < 0) {
warn("%s: unknown supplementary group '%s'", warnx("%s: unknown supplementary group '%s'",
svc_ident(svc, NULL, 0), svc->supgroups[i]); svc_ident(svc, NULL, 0), sup);
continue; continue;
} }
/* Skip if already in list from /etc/group */ /* Skip if already in list from /etc/group */
@@ -2490,10 +2492,12 @@ svc_t *service_register_desc(int type, char *cfg, const char *desc,
svc_unblock(svc); svc_unblock(svc);
} }
svc->supgroups[0] = 0;
if (username) { if (username) {
char *ptr = strchr(username, ':'); char *ptr = strchr(username, ':');
if (ptr) { if (ptr) {
size_t len = sizeof(svc->supgroups);
char *sup; char *sup;
*ptr++ = 0; *ptr++ = 0;
@@ -2501,20 +2505,15 @@ svc_t *service_register_desc(int type, char *cfg, const char *desc,
sup = strchr(ptr, ','); sup = strchr(ptr, ',');
if (sup) { if (sup) {
*sup++ = 0; *sup++ = 0;
svc->num_supgroups = 0; if (strlcpy(svc->supgroups, sup, len) >= len) {
while (sup) { /* Drop the cut-off name, it may match another group */
char *next = strchr(sup, ','); sup = strrchr(svc->supgroups, ',');
if (next) if (sup)
*next++ = 0; *sup = 0;
if (svc->num_supgroups >= MAX_NUM_SUPGROUPS) { else
warn("%s: too many supplementary groups, max %d", svc->supgroups[0] = 0;
svc->cmd, MAX_NUM_SUPGROUPS); warnx("%s: supplementary group list too long, max %zu characters",
break; svc->cmd, len - 1);
}
strlcpy(svc->supgroups[svc->num_supgroups], sup,
sizeof(svc->supgroups[0]));
svc->num_supgroups++;
sup = next;
} }
} }
strlcpy(svc->group, ptr, sizeof(svc->group)); strlcpy(svc->group, ptr, sizeof(svc->group));
+2 -3
View File
@@ -101,7 +101,7 @@ typedef enum {
#define MAX_STR_LEN 64 #define MAX_STR_LEN 64
#define MAX_COND_LEN (MAX_ARG_LEN * 3) #define MAX_COND_LEN (MAX_ARG_LEN * 3)
#define MAX_USER_LEN MAX_ARG_LEN #define MAX_USER_LEN MAX_ARG_LEN
#define MAX_NUM_SUPGROUPS 4 #define MAX_SUPGROUPS_LEN (MAX_CMD_LEN * 2)
#define MAX_NUM_PROVIDES 4 #define MAX_NUM_PROVIDES 4
#define MAX_NUM_FDS 64 /* Max number of I/O plugins */ #define MAX_NUM_FDS 64 /* Max number of I/O plugins */
#define MAX_NUM_SVC_ARGS 64 #define MAX_NUM_SVC_ARGS 64
@@ -200,8 +200,7 @@ typedef struct svc {
/* Identity */ /* Identity */
char username[MAX_USER_LEN]; char username[MAX_USER_LEN];
char group[MAX_USER_LEN]; char group[MAX_USER_LEN];
char supgroups[MAX_NUM_SUPGROUPS][MAX_USER_LEN]; char supgroups[MAX_SUPGROUPS_LEN]; /* sup1,sup2,... */
int num_supgroups;
char capabilities[MAX_CMD_LEN]; char capabilities[MAX_CMD_LEN];
char pam[MAX_ARG_LEN]; /* /etc/pam.d/NAME, block format only */ char pam[MAX_ARG_LEN]; /* /etc/pam.d/NAME, block format only */
+2
View File
@@ -38,6 +38,7 @@ EXTRA_DIST += conf-provides.sh
EXTRA_DIST += conf-tty.sh EXTRA_DIST += conf-tty.sh
EXTRA_DIST += conf-dup-title.sh EXTRA_DIST += conf-dup-title.sh
EXTRA_DIST += conf-dirs.sh EXTRA_DIST += conf-dirs.sh
EXTRA_DIST += conf-groups.sh
EXTRA_DIST += conf-if.sh EXTRA_DIST += conf-if.sh
EXTRA_DIST += conf-template.sh EXTRA_DIST += conf-template.sh
EXTRA_DIST += script-timeout.sh EXTRA_DIST += script-timeout.sh
@@ -106,6 +107,7 @@ TESTS += conf-provides.sh
TESTS += conf-tty.sh TESTS += conf-tty.sh
TESTS += conf-dup-title.sh TESTS += conf-dup-title.sh
TESTS += conf-dirs.sh TESTS += conf-dirs.sh
TESTS += conf-groups.sh
TESTS += conf-if.sh TESTS += conf-if.sh
TESTS += conf-template.sh TESTS += conf-template.sh
TESTS += script-timeout.sh TESTS += script-timeout.sh
+50
View File
@@ -0,0 +1,50 @@
#!/bin/sh
# Verify extra-groups sets every listed group on the service, and that
# dropping the setting drops the groups.
set -eu
TEST_DIR=$(dirname "$0")
# Optional argument is the extra-groups list
write_conf()
{
run "echo 'service grp {' > $FINIT_CONF"
run "echo ' group = \"root\"' >> $FINIT_CONF"
if [ -n "${1:-}" ]; then
run "echo ' extra-groups = { $1 }' >> $FINIT_CONF"
fi
run "echo ' command = \"serv -np -i grp\"' >> $FINIT_CONF"
run "echo '}' >> $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 extra 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 extra-groups drops the groups'
write_conf
run "initctl reload"
run "initctl restart grp"
retry 'assert_groups "0"'