Skip to content

Commit 9effaeb

Browse files
committed
On exec, honor additional_gids from the process spec, not the container definition
The code was using the process defined in the container definition to find additional_gids, not the one passed on the command line or created by default. Fixes #644 Signed-off-by: Owen W. Taylor <otaylor@fishsoup.net>
1 parent 99e2dc7 commit 9effaeb

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

src/libcrun/container.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3177,7 +3177,7 @@ libcrun_container_exec (libcrun_context_t *context, const char *id, runtime_spec
31773177
close_and_reset (&seccomp_receiver_fd);
31783178
}
31793179

3180-
ret = libcrun_container_setgroups (container, err);
3180+
ret = libcrun_container_setgroups (container, process, err);
31813181
if (UNLIKELY (ret < 0))
31823182
return ret;
31833183

src/libcrun/linux.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,18 +2090,19 @@ can_setgroups (libcrun_container_t *container, libcrun_error_t *err)
20902090
}
20912091

20922092
int
2093-
libcrun_container_setgroups (libcrun_container_t *container, libcrun_error_t *err)
2093+
libcrun_container_setgroups (libcrun_container_t *container,
2094+
runtime_spec_schema_config_schema_process *process,
2095+
libcrun_error_t *err)
20942096
{
2095-
runtime_spec_schema_config_schema *def = container->container_def;
20962097
gid_t *additional_gids = NULL;
20972098
size_t additional_gids_len = 0;
20982099
int can_do_setgroups;
20992100
int ret;
21002101

2101-
if (def->process != NULL && def->process->user != NULL)
2102+
if (process != NULL && process->user != NULL)
21022103
{
2103-
additional_gids = def->process->user->additional_gids;
2104-
additional_gids_len = def->process->user->additional_gids_len;
2104+
additional_gids = process->user->additional_gids;
2105+
additional_gids_len = process->user->additional_gids_len;
21052106
}
21062107

21072108
can_do_setgroups = can_setgroups (container, err);
@@ -3217,7 +3218,7 @@ init_container (libcrun_container_t *container, int sync_socket_container, struc
32173218
return ret;
32183219
}
32193220

3220-
ret = libcrun_container_setgroups (container, err);
3221+
ret = libcrun_container_setgroups (container, container->container_def->process, err);
32213222
if (UNLIKELY (ret < 0))
32223223
return ret;
32233224

src/libcrun/linux.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ int libcrun_container_restore_linux (libcrun_container_status_t *status, libcrun
6868

6969
int libcrun_find_namespace (const char *name);
7070
char *libcrun_get_external_descriptors (libcrun_container_t *container);
71-
int libcrun_container_setgroups (libcrun_container_t *container, libcrun_error_t *err);
71+
int libcrun_container_setgroups (libcrun_container_t *container,
72+
runtime_spec_schema_config_schema_process *process,
73+
libcrun_error_t *err);
7274
int libcrun_kill_linux (libcrun_container_status_t *status, int signal, libcrun_error_t *err);
7375
int libcrun_create_final_userns (libcrun_container_t *container, libcrun_error_t *err);
7476
#endif

tests/init.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ int main (int argc, char **argv)
207207
exit (0);
208208
}
209209

210+
if (strcmp (argv[1], "groups") == 0)
211+
{
212+
gid_t groups[10];
213+
int max_groups = sizeof(groups) / sizeof(groups[0]);
214+
int n_groups, i;
215+
n_groups = getgroups(max_groups, groups);
216+
fputs("GROUPS=[", stdout);
217+
for (i = 0; i < n_groups; i++)
218+
printf("%s%d", i == 0 ? "" : " ", groups[i]);
219+
fputs("]\n", stdout);
220+
exit (0);
221+
}
222+
210223
if (strcmp (argv[1], "cat") == 0)
211224
{
212225
if (argc < 3)

tests/test_exec.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import os
2222
import shutil
2323
import sys
24+
import tempfile
2425
from tests_utils import *
2526

2627
def test_exec():
@@ -62,11 +63,50 @@ def test_exec_not_exists():
6263

6364
def test_exec_detach_not_exists():
6465
return test_exec_not_exists_helper(False)
65-
66+
67+
def test_exec_additional_gids():
68+
conf = base_config()
69+
conf['process']['args'] = ['/init', 'pause']
70+
add_all_namespaces(conf)
71+
cid = None
72+
tempdir = tempfile.mkdtemp()
73+
try:
74+
_, cid = run_and_get_output(conf, command='run', detach=True)
75+
76+
process_file = os.path.join(tempdir, "process.json")
77+
with open(process_file, "w") as f:
78+
json.dump({
79+
"user": {
80+
"uid": 0,
81+
"gid": 0,
82+
"additionalGids": [432]
83+
},
84+
"terminal": False,
85+
"args": [
86+
"/init",
87+
"groups"
88+
],
89+
"env": [
90+
"PATH=/bin",
91+
"TERM=xterm"
92+
],
93+
"cwd": "/",
94+
"noNewPrivileges": True
95+
}, f)
96+
out = run_crun_command(["exec", "--process", process_file, cid])
97+
if "432" not in out:
98+
return -1
99+
finally:
100+
if cid is not None:
101+
run_crun_command(["delete", "-f", cid])
102+
shutil.rmtree(tempdir)
103+
return 0
104+
66105
all_tests = {
67106
"exec" : test_exec,
68107
"exec-not-exists" : test_exec_not_exists,
69108
"exec-detach-not-exists" : test_exec_detach_not_exists,
109+
"exec-detach-additional-gids" : test_exec_additional_gids,
70110
}
71111

72112
if __name__ == "__main__":

0 commit comments

Comments
 (0)