Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libct/nsenter: become root after joining userns #4473

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libcontainer/nsenter/nsexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,17 @@ void join_namespaces(char *nslist)
if (setns(ns->fd, flag) < 0)
bail("failed to setns into %s namespace", ns->type);

/*
* If we change user namespaces, make sure we switch to root in the
* namespace (this matches the logic for unshare(CLONE_NEWUSER)), lots
* of things can break if we aren't the right user. See
* <https://github.com/opencontainers/runc/issues/4466> for one example.
*/
if (flag == CLONE_NEWUSER) {
if (setresuid(0, 0, 0) < 0)
bail("failed to become root in user namespace");
}

close(ns->fd);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/integration/userns.bats
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ function teardown() {
fi
}

# issue: https://github.com/opencontainers/runc/issues/4466
@test "userns join other container userns[selinux enabled]" {
if ! selinuxenabled; then
skip "requires SELinux enabled and in enforcing mode"
fi
# Create a detached container with the id-mapping we want.
update_config '.process.args = ["sleep", "infinity"]'
runc run -d --console-socket "$CONSOLE_SOCKET" target_userns
[ "$status" -eq 0 ]

# Configure our container to attach to the first container's userns.
target_pid="$(__runc state target_userns | jq .pid)"
update_config '.linux.namespaces |= map(if .type == "user" then (.path = "/proc/'"$target_pid"'/ns/" + .type) else . end)
| del(.linux.uidMappings)
| del(.linux.gidMappings)
| .linux.mountLabel="system_u:object_r:container_file_t:s0:c344,c805"'
runc run -d --console-socket "$CONSOLE_SOCKET" in_userns
[ "$status" -eq 0 ]
}

@test "userns join other container userns [bind-mounted nsfd]" {
requires root

Expand Down
Loading