Repository navigation
Description
Summary
In a Linux user namespace where setgroups(2) is permitted (such as a rootless environment configured with newuidmap(1)/newgidmap(1), where /proc/self/setgroups remains "allow"), SwitchedCredentials::switch records the Vec<gid_t> returned by getgroups(2) before clearing supplementary groups with set_groups(&[]), and SwitchedCredentials::drop restores that vector with set_groups(groups).expect("failed to restore the thread's supplementary groups"). If the VMM worker thread holds any host supplementary group that is unmapped in /proc/self/gid_map, getgroups(2) irreversibly munges that group to overflowgid (/proc/sys/kernel/overflowgid, 65534 by default), causing Drop either to panic with EINVAL and abort the VMM process (when 65534 is unmapped in gid_map) or to silently replace the worker thread's host supplementary groups with whatever single host group 65534 maps to (when 65534 is mapped in gid_map). This issue was identified through static code inspection of credentials.rs and Linux getgroups/setgroups user-namespace semantics and has not yet been tested live under newgidmap.
Root Cause
In vm/devices/support/fs/lxutil/src/unix/credentials.rs and vm/devices/support/fs/lxutil/src/unix/credentials.rs, SwitchedCredentials assumes that set_groups (SYS_setgroups) is the exact inverse of groups (libc::getgroups):
// On an error below, dropping `credentials` restores whatever was already changed.
let groups = groups()?;
if groups.iter().any(|&group| group != identity.gid) {
set_groups(&[])?;
credentials.groups = Some(groups);
} if let Some(groups) = &self.groups {
set_groups(groups).expect("failed to restore the thread's supplementary groups");
}Inside a Linux user namespace, getgroups(2) (groups_to_user) formats each kgid_t in cred->group_info using from_kgid_munged(current_user_ns(), kgid), which substitutes /proc/sys/kernel/overflowgid (65534 by default) for every host kgid_t without an entry in /proc/self/gid_map. By contrast, setgroups(2) (groups_from_user) translates each supplied gid_t back into a kgid_t via make_kgid(current_user_ns(), gid) and rejects any unmapped ID with -EINVAL (!gid_valid(kgid)). In single-user unshare --user --map-group=... namespaces, the kernel forces /proc/self/setgroups to "deny", so set_groups(&[]) fails with EPERM before credentials.groups is set. However, in multi-ID user namespaces set up with privileged newuidmap(1) and newgidmap(1) helpers (used by rootless container runtimes and sandboxed VMM launchers), /proc/self/setgroups remains "allow" and the worker thread holds CAP_SETGID inside the user namespace while still inheriting host supplementary groups (such as adm, kvm, or docker) that have no entry in gid_map. For any target identity.gid != 65534 (and for identity.gid == 65534 once overflow-GID switches also drop supplementary groups), set_groups(&[]) succeeds, erasing the unmapped kgid_t entries from cred->group_info and saving 65534 in credentials.groups.
Consequence
When with_fs_identity drops SwitchedCredentials after handling a guest filesystem request under --mount-owner caller (vm/devices/virtio/virtiofs/src/microvm/owner.rs), one of two failures occurs depending on whether overflowgid (65534) is itself mapped in /proc/self/gid_map:
- VMM abort (
65534unmapped ingid_map):make_kgid(user_ns, 65534)is invalid,set_groups(groups)fails withlx::Error::EINVAL, and.expect("failed to restore the thread's supplementary groups")panics — immediately aborting the entire VMM process becausepanic = 'abort'is set in[profile.dev]and[profile.release](Cargo.toml). - Silent worker-thread credential corruption (
65534mapped ingid_map): In a typical 65536-IDnewgidmapmapping (for example namespace GIDs0 -> 1000and1..=65535 -> 100000..=165534),make_kgid(user_ns, 65534)resolves to subordinate host GID165533;set_groups(groups)succeeds and permanently replaces all of the worker thread's original unmapped host supplementary groups with that subordinate GID for subsequent VMM-identity operations.
Reproduction
Note: This issue was found by static code inspection of credentials.rs against Linux's groups_to_user / groups_from_user user-namespace semantics and has not yet been tested live with newgidmap.
To reproduce on a Linux host with uidmap (newuidmap/newgidmap) installed and /etc/subuid + /etc/subgid configured for a user who belongs to at least one supplementary group outside the mapped host GIDs:
- Spawn a process in a new user namespace (
unshare --userwithout--map-group, leaving/proc/self/setgroupsset to"allow"). - From the parent namespace, run
newuidmap <pid> 0 <host_uid> 1 1 <subuid_start> 65535and eithernewgidmap <pid> 0 <host_gid> 1 1 <subgid_start> 65533(leaving65534unmapped) ornewgidmap <pid> 0 <host_gid> 1 1 <subgid_start> 65535(mapping65534to<subgid_start + 65533>). - Inside the child, call
lxutil::with_fs_identity(FsIdentity { uid: 1, gid: 1 }, || {}). When65534is unmapped,SwitchedCredentials::droppanics atvm/devices/support/fs/lxutil/src/unix/credentials.rs:161withEINVAL; when65534is mapped,with_fs_identityreturnsOk(())after replacing the thread's original unmapped host supplementary groups with the mapped GID for65534.
Suggested Fix
Because Linux provides no way inside a user namespace to read or restore unmapped kgid_t supplementary groups via getgroups(2) and setgroups(2) once set_groups(&[]) clears them (nor to distinguish an unmapped group reported as overflowgid from a mapped overflowgid), check groups.contains(&overflow_gid()) in SwitchedCredentials::switch whenever supplementary groups must be dropped and return Err(lx::Error::EPERM) before calling set_groups(&[]). Failing closed before mutating thread credentials avoids both the irreversible loss/corruption of host supplementary groups and the post-operation Drop panic.
Related: #1600.
Activity