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
4 changes: 2 additions & 2 deletions crates/kiri-init/src/linux/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 34 additions & 6 deletions crates/kiri-init/src/linux/hostopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<libc::gid_t>> {
/// 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<Vec<libc::gid_t>> {
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<String>, gid: u32, add: &[String]) -> io::Result<Vec<libc::gid_t>> {
let groups: Vec<Vec<&str>> = 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::<u32>().is_ok() { user_name(uid) } else { Some(name.to_owned()) };
let name = if name.is_empty() || name.parse::<u32>().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)) {
Expand All @@ -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)
}
Expand All @@ -313,6 +323,24 @@ fn user_name(uid: u32) -> Option<String> {
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<String> = 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<u8> {
let mut buf = Vec::new();
kiri_proto::encode_retain(&mut buf, lines);
Expand Down
3 changes: 3 additions & 0 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down