Skip to content
Open
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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ test: $(BIN)
# --- Basic sanity tests ---
$(call run_test, ./nsjail -q -Mo --chroot / --user 99999 --group 99999 -- /bin/true, 0)
$(call run_test, ./nsjail -q -Mo --chroot / --user 99999 --group 99999 -- /bin/false, 1)
$(call run_test, strace -f -qq -e inject=setgroups:error=EPERM ./nsjail -q -Mo --disable_clone_newuser --chroot / --user 99999 --group 99999 -- /bin/true, 255)
ifeq ($(UID),0)
$(call run_test, setpriv --reuid 1000 --regid 1000 --groups 1234 -- ./nsjail -q -Mo --user 65534 --group 65534 --disable_clone_newnet --disable_clone_newcgroup --disable_clone_newns --disable_clone_newpid --disable_clone_newipc --disable_clone_newuts --disable_proc -- /bin/true, 255)
$(call run_test, setpriv --reuid 1000 --regid 1000 --clear-groups -- ./nsjail -q -Mo --user 65534 --group 65534 --disable_clone_newnet --disable_clone_newcgroup --disable_clone_newns --disable_clone_newpid --disable_clone_newipc --disable_clone_newuts --disable_proc -- /bin/true, 0)
endif
$(call run_test, ./nsjail --config tests/seccomp.cfg -q -t 2 -- /bin/bash -c 'strace -o /dev/null /bin/true || exit 77', 77)
$(call run_test, ./nsjail --config tests/basic.cfg -q -t 2 -- /bin/bash -c 'strace -o /dev/null /bin/true && exit 77', 77)
$(call run_test, ./nsjail --config tests/pasta-nat.cfg -q -t 3 -- /bin/bash -c 'sleep 0.2; ping -W 1 -c 1 8.8.8.8 && exit 77', 77)
Expand Down
26 changes: 23 additions & 3 deletions user.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ static bool hasGidMapSelf(nsj_t* nsj) {
return false;
}

static bool shouldDenySetGroups(nsj_t* nsj) {
return nsj->njc.clone_newuser() && nsj->orig_euid != 0 && hasGidMapSelf(nsj);
}

static bool setGroupsDeny(nsj_t* nsj, pid_t pid) {
/*
* No need to write 'deny' to /proc/pid/setgroups if our euid==0, as writing to
* uid_map/gid_map will succeed anyway
*/
if (!nsj->njc.clone_newuser() || nsj->orig_euid == 0 || !hasGidMapSelf(nsj)) {
if (!shouldDenySetGroups(nsj)) {
return true;
}

Expand Down Expand Up @@ -288,11 +292,27 @@ bool initNsFromChild(nsj_t* nsj) {

LOG_D("setgroups(%zu, %s)", groups.size(), groupsString.c_str());
if (setgroups(groups.size(), groups.data()) == -1) {
/* Indicate error if specific groups were requested */
if (groups.size() > 0) {
const int setgroupsErrno = errno;
/* EPERM is expected after the parent writes "deny" to /proc/pid/setgroups. */
if (!shouldDenySetGroups(nsj) || setgroupsErrno != EPERM) {
errno = setgroupsErrno;
PLOG_E("setgroups(%zu, %s) failed", groups.size(), groupsString.c_str());
return false;
}

const int remainingGroups = getgroups(0, nullptr);
if (remainingGroups == -1) {
PLOG_E("getgroups(0, nullptr) failed");
return false;
}
if (remainingGroups != 0) {
errno = setgroupsErrno;
PLOG_E("setgroups(%zu, %s) failed and %d supplementary group(s) remain",
groups.size(), groupsString.c_str(), remainingGroups);
return false;
}

errno = setgroupsErrno;
PLOG_D("setgroups(%zu, %s) failed", groups.size(), groupsString.c_str());
}

Expand Down