Skip to content

Commit 34f139c

Browse files
authored
Merge pull request systemd#4543 from endocode/djalal/fix-dynamicuser-supplementary-groups
core: intialize user aux groups and SupplementaryGroups= when DynamicUser= is set
2 parents b2c82a7 + 5c67067 commit 34f139c

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed

Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,9 @@ EXTRA_DIST += \
16261626
test/test-execute/exec-supplementarygroups-multiple-groups-default-group-user.service \
16271627
test/test-execute/exec-supplementarygroups-multiple-groups-withgid.service \
16281628
test/test-execute/exec-supplementarygroups-multiple-groups-withuid.service \
1629+
test/test-execute/exec-dynamicuser-fixeduser.service \
1630+
test/test-execute/exec-dynamicuser-fixeduser-one-supplementarygroup.service \
1631+
test/test-execute/exec-dynamicuser-supplementarygroups.service \
16291632
test/test-execute/exec-ignoresigpipe-no.service \
16301633
test/test-execute/exec-ignoresigpipe-yes.service \
16311634
test/test-execute/exec-personality-x86-64.service \

src/core/execute.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,9 @@ static int get_fixed_group(const ExecContext *c, const char **group, gid_t *gid)
773773
return 0;
774774
}
775775

776-
static int get_fixed_supplementary_groups(const ExecContext *c,
777-
const char *user,
778-
const char *group,
779-
gid_t gid,
780-
gid_t **supplementary_gids, int *ngids) {
776+
static int get_supplementary_groups(const ExecContext *c, const char *user,
777+
const char *group, gid_t gid,
778+
gid_t **supplementary_gids, int *ngids) {
781779
char **i;
782780
int r, k = 0;
783781
int ngroups_max;
@@ -790,8 +788,8 @@ static int get_fixed_supplementary_groups(const ExecContext *c,
790788
/*
791789
* If user is given, then lookup GID and supplementary groups list.
792790
* We avoid NSS lookups for gid=0. Also we have to initialize groups
793-
* as early as possible so we keep the list of supplementary groups
794-
* of the caller.
791+
* here and as early as possible so we keep the list of supplementary
792+
* groups of the caller.
795793
*/
796794
if (user && gid_is_valid(gid) && gid != 0) {
797795
/* First step, initialize groups from /etc/groups */
@@ -2347,13 +2345,14 @@ static int exec_child(
23472345
*exit_status = EXIT_GROUP;
23482346
return r;
23492347
}
2348+
}
23502349

2351-
r = get_fixed_supplementary_groups(context, username, groupname,
2352-
gid, &supplementary_gids, &ngids);
2353-
if (r < 0) {
2354-
*exit_status = EXIT_GROUP;
2355-
return r;
2356-
}
2350+
/* Initialize user supplementary groups and get SupplementaryGroups= ones */
2351+
r = get_supplementary_groups(context, username, groupname, gid,
2352+
&supplementary_gids, &ngids);
2353+
if (r < 0) {
2354+
*exit_status = EXIT_GROUP;
2355+
return r;
23572356
}
23582357

23592358
r = send_user_lookup(unit, user_lookup_fd, uid, gid);

src/test/test-execute.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ static void test_exec_supplementary_groups(Manager *m) {
259259
test(m, "exec-supplementarygroups-multiple-groups-withuid.service", 0, CLD_EXITED);
260260
}
261261

262+
static void test_exec_dynamic_user(Manager *m) {
263+
test(m, "exec-dynamicuser-fixeduser.service", 0, CLD_EXITED);
264+
test(m, "exec-dynamicuser-fixeduser-one-supplementarygroup.service", 0, CLD_EXITED);
265+
test(m, "exec-dynamicuser-supplementarygroups.service", 0, CLD_EXITED);
266+
}
267+
262268
static void test_exec_environment(Manager *m) {
263269
test(m, "exec-environment.service", 0, CLD_EXITED);
264270
test(m, "exec-environment-multiple.service", 0, CLD_EXITED);
@@ -432,6 +438,7 @@ int main(int argc, char *argv[]) {
432438
test_exec_user,
433439
test_exec_group,
434440
test_exec_supplementary_groups,
441+
test_exec_dynamic_user,
435442
test_exec_environment,
436443
test_exec_environmentfile,
437444
test_exec_passenvironment,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Test DynamicUser with User= and SupplementaryGroups=
3+
4+
[Service]
5+
ExecStart=/bin/sh -x -c 'test "$$(id -G)" = "1" && test "$$(id -g)" = "1" && test "$$(id -u)" = "1"'
6+
Type=oneshot
7+
User=1
8+
DynamicUser=yes
9+
SupplementaryGroups=1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Unit]
2+
Description=Test DynamicUser with User=
3+
4+
[Service]
5+
ExecStart=/bin/sh -x -c 'test "$$(id -G)" = "1" && test "$$(id -g)" = "1" && test "$$(id -u)" = "1"'
6+
Type=oneshot
7+
User=1
8+
DynamicUser=yes
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Unit]
2+
Description=Test DynamicUser with SupplementaryGroups=
3+
4+
[Service]
5+
ExecStart=/bin/sh -x -c 'test "$$(id -G | cut -d " " --complement -f 1)" = "1 2 3"'
6+
Type=oneshot
7+
DynamicUser=yes
8+
SupplementaryGroups=1 2 3

0 commit comments

Comments
 (0)