Skip to content

Commit 22106a4

Browse files
committed
merge #4473 into opencontainers/runc:main
lifubang: test join other container userns with selinux enabled libct/nsenter: become root after joining userns LGTMs: AkihiroSuda cyphar
2 parents e37371e + 34a9285 commit 22106a4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

libcontainer/nsenter/nsexec.c

+11
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,17 @@ void join_namespaces(char *nslist)
505505
if (setns(ns->fd, flag) < 0)
506506
bail("failed to setns into %s namespace", ns->type);
507507

508+
/*
509+
* If we change user namespaces, make sure we switch to root in the
510+
* namespace (this matches the logic for unshare(CLONE_NEWUSER)), lots
511+
* of things can break if we aren't the right user. See
512+
* <https://github.com/opencontainers/runc/issues/4466> for one example.
513+
*/
514+
if (flag == CLONE_NEWUSER) {
515+
if (setresuid(0, 0, 0) < 0)
516+
bail("failed to become root in user namespace");
517+
}
518+
508519
close(ns->fd);
509520
}
510521

tests/integration/userns.bats

+20
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ function teardown() {
125125
fi
126126
}
127127

128+
# issue: https://github.com/opencontainers/runc/issues/4466
129+
@test "userns join other container userns[selinux enabled]" {
130+
if ! selinuxenabled; then
131+
skip "requires SELinux enabled and in enforcing mode"
132+
fi
133+
# Create a detached container with the id-mapping we want.
134+
update_config '.process.args = ["sleep", "infinity"]'
135+
runc run -d --console-socket "$CONSOLE_SOCKET" target_userns
136+
[ "$status" -eq 0 ]
137+
138+
# Configure our container to attach to the first container's userns.
139+
target_pid="$(__runc state target_userns | jq .pid)"
140+
update_config '.linux.namespaces |= map(if .type == "user" then (.path = "/proc/'"$target_pid"'/ns/" + .type) else . end)
141+
| del(.linux.uidMappings)
142+
| del(.linux.gidMappings)
143+
| .linux.mountLabel="system_u:object_r:container_file_t:s0:c344,c805"'
144+
runc run -d --console-socket "$CONSOLE_SOCKET" in_userns
145+
[ "$status" -eq 0 ]
146+
}
147+
128148
@test "userns join other container userns [bind-mounted nsfd]" {
129149
requires root
130150

0 commit comments

Comments
 (0)