From 4a4128025f196010f7339a9463f8f1d82a29995d Mon Sep 17 00:00:00 2001 From: Alex Alvord <325058966+cloudlabworks@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:18:12 -0800 Subject: [PATCH] A user's own group is one of its supplementary groups, as with Docker docker run --user 1000:1000 gave a process with no supplementary groups; Docker gives it 1000, the user's group. The same for a named user (--user nobody: none on Kiri, 65534 on Docker) and for docker exec --user. Docker starts the list with the user's gid (usr.AdditionalGids in moby's getUser, daemon/oci_linux.go), then adds the /etc/group memberships and --group-add. Kiri now does the same in supplementary_groups. The list is sorted and deduplicated, so root shows 0 once where Docker lists it twice. Access through the primary group already worked; what differed is getgroups(), id, and Groups: in /proc/self/status. Left alone: for a numeric user that /etc/passwd doesn't have (--user 1000 in alpine), Docker uses gid 0 and Kiri uses the uid as the gid (resolve_user). That choice looks deliberate, so this change keeps it. Tests: - unit: the_users_own_group_is_a_supplementary_group_as_with_docker (groups_from takes the group file's text, no container needed) - e2e: user-groups-primary (-u 1000:1000 --group-add 2000 gives 1000 2000) and user-groups-exec (exec -u nobody gives 65534); both fail on 0.2.1 and pass with this change Tested: cargo test -p kiri-init in rust:1-alpine (aarch64, Rust 1.100.0): 71 passed, 1 failed; main gives 70 passed, 1 failed (the same layers::tests::whiteout_becomes_char_device, unprivileged container). cargo clippy and cargo fmt --check: same findings as main, none new. Then cargo xtask host + guest on Apple silicon, macOS 27.0.1, compared with Docker Engine 29.8.2 (OrbStack). Run only on Apple silicon so far; kiri-init is the runtime on all three platforms. --- crates/kiri-init/src/linux/container.rs | 4 +-- crates/kiri-init/src/linux/hostopts.rs | 40 +++++++++++++++++++++---- tests/e2e.sh | 3 ++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/crates/kiri-init/src/linux/container.rs b/crates/kiri-init/src/linux/container.rs index 0dc67695..7327c48e 100644 --- a/crates/kiri-init/src/linux/container.rs +++ b/crates/kiri-init/src/linux/container.rs @@ -1277,8 +1277,8 @@ fn finish_child(p: &Prepared, io: &ChildStdio, console: bool) -> ! { // namespaced, and privileges inside the VM are no reason to reach the host. super::seccomp::install(p.seccomp.then_some(p.caps)).unwrap_or_else(|e| fail("seccomp", e)); let (uid, gid) = resolve_user(&p.user).unwrap_or_else(|e| fail("user", e)); - // The user's /etc/group memberships and --group-add, as Docker gives them. - let groups = super::hostopts::supplementary_groups(&p.user, uid, &p.host.groups).unwrap_or_else(|e| fail("groups", e)); + // The user's group, /etc/group memberships and --group-add, as Docker gives them. + let groups = super::hostopts::supplementary_groups(&p.user, uid, gid, &p.host.groups).unwrap_or_else(|e| fail("groups", e)); give_stdio([0, 1, 2], uid).unwrap_or_else(|e| fail("stdio", e)); // SAFETY: plain id-setting syscalls; `groups` outlives setgroups. unsafe { diff --git a/crates/kiri-init/src/linux/hostopts.rs b/crates/kiri-init/src/linux/hostopts.rs index ce140bc3..c26b6e4c 100644 --- a/crates/kiri-init/src/linux/hostopts.rs +++ b/crates/kiri-init/src/linux/hostopts.rs @@ -265,16 +265,25 @@ pub fn errno_text(e: &io::Error) -> String { c.next().map(|f| f.to_lowercase().chain(c).collect()).unwrap_or(s) } -/// The supplementary groups for a user, read from the container's -/// /etc/passwd and /etc/group as runc does: every group that lists the -/// user, unless the run named a group (`user:group`), then `--group-add`. -pub fn supplementary_groups(user: &str, uid: u32, add: &[String]) -> io::Result> { +/// The supplementary groups for a user, as Docker gives them: the user's +/// own group `gid` first, then, read from the container's /etc/passwd and +/// /etc/group as runc does, every group that lists the user, unless the run +/// named a group (`user:group`), then `--group-add`. +pub fn supplementary_groups(user: &str, uid: u32, gid: u32, add: &[String]) -> io::Result> { let group_file = fs::read_to_string("/etc/group").unwrap_or_default(); + groups_from(&group_file, user, || user_name(uid), gid, add) +} + +/// [`supplementary_groups`] over `group_file`'s text; `uid_name` gives the +/// name /etc/passwd has for a numeric user. +fn groups_from(group_file: &str, user: &str, uid_name: impl FnOnce() -> Option, gid: u32, add: &[String]) -> io::Result> { let groups: Vec> = group_file.lines().map(|l| l.split(':').collect()).filter(|f: &Vec<&str>| f.len() >= 3).collect(); - let mut out = Vec::new(); + // Docker always lists the primary group, so getgroups() includes it + // (moby's getUser starts AdditionalGids with the user's gid). + let mut out = vec![gid]; let (name, group) = user.split_once(':').map_or((user, None), |(u, g)| (u, Some(g))); if group.is_none() { - let name = if name.is_empty() || name.parse::().is_ok() { user_name(uid) } else { Some(name.to_owned()) }; + let name = if name.is_empty() || name.parse::().is_ok() { uid_name() } else { Some(name.to_owned()) }; if let Some(name) = name { for f in &groups { if f.get(3).is_some_and(|members| members.split(',').any(|m| m.trim() == name)) { @@ -296,6 +305,7 @@ pub fn supplementary_groups(user: &str, uid: u32, add: &[String]) -> io::Result< }; out.push(gid); } + out.sort_unstable(); out.dedup(); Ok(out) } @@ -313,6 +323,24 @@ fn user_name(uid: u32) -> Option { mod tests { use super::*; + #[test] + fn the_users_own_group_is_a_supplementary_group_as_with_docker() { + let group = "root:x:0:root\nwheel:x:10:root,app\napp:x:1000:\nextra:x:2000:\n"; + let groups = |user: &str, gid: u32, add: &[&str]| { + let add: Vec = add.iter().map(|g| g.to_string()).collect(); + groups_from(group, user, || Some("app".to_owned()), gid, &add).unwrap() + }; + // --user 1000:1000: the named group, nothing from /etc/group. + assert_eq!(groups("1000:1000", 1000, &[]), [1000]); + // --user app (and its uid alone): its group, then its memberships. + assert_eq!(groups("app", 1000, &[]), [10, 1000]); + assert_eq!(groups("1000", 1000, &[]), [10, 1000]); + // --group-add beside it; a group given twice is listed once. + assert_eq!(groups("1000:1000", 1000, &["2000", "app"]), [1000, 2000]); + assert_eq!(groups("65534:1234", 1234, &[]), [1234]); + assert!(groups_from(group, "1000:1000", || None, 1000, &["nope".to_owned()]).is_err()); + } + fn list(lines: &[&str]) -> Vec { let mut buf = Vec::new(); kiri_proto::encode_retain(&mut buf, lines); diff --git a/tests/e2e.sh b/tests/e2e.sh index 46888f9c..91dba467 100755 --- a/tests/e2e.sh +++ b/tests/e2e.sh @@ -133,6 +133,9 @@ t workdir-created "/made/here" $D run --rm -w /made/here alpine pwd t user-numeric "uid=1000" $D run --rm -u 1000 alpine id t user-name "uid=70(postgres)" $D run --rm -u postgres postgres:17-alpine id t user-group "gid=0(root)" $D run --rm -u 1000:0 alpine id +# The user's own group is one of its supplementary groups, as Docker sets them. +t user-groups-primary "1000 2000" $D run --rm -u 1000:1000 --group-add 2000 alpine sh -c 'grep Groups /proc/self/status | cut -f2' +t user-groups-exec "65534" bash -c "$D run -d --name groupsexec alpine sleep 60 >/dev/null && $D exec -u nobody groupsexec sh -c 'grep Groups /proc/self/status | cut -f2'; $D rm -f groupsexec >/dev/null" t entrypoint "x" $D run --rm --entrypoint echo alpine x t entrypoint-image "nginx version" $D run --rm nginx:alpine nginx -v t hostname-is-id "" $D run --rm alpine hostname