openvmm: lxutil: with_fs_identity cannot safely restore unmapped supplementary groups when setgroups is permitted in a user namespace

Open
#1,606 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
linux, rust
Domain
security

Research direction

Start in vm/devices/support/fs/lxutil/src/unix/credentials.rs, reading SwitchedCredentials::switch (~line 113) and drop (~line 159) together with the groups/set_groups helpers they wrap. Then follow the caller in vm/devices/virtio/virtiofs/src/microvm/owner.rs (96-130) to see how with_fs_identity brackets guest requests, and reproduce under newuidmap/newgidmap as the reproduction section describes. Done means the supplementary-group drop fails closed with EPERM before set_groups(&[]) whenever an unmapped group would be lost, plus a test covering both the overflow-GID-mapped and unmapped cases.

Written by the indexing model from the issue text.

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:

  1. VMM abort (65534 unmapped in gid_map): make_kgid(user_ns, 65534) is invalid, set_groups(groups) fails with lx::Error::EINVAL, and .expect("failed to restore the thread's supplementary groups") panics — immediately aborting the entire VMM process because panic = 'abort' is set in [profile.dev] and [profile.release] (Cargo.toml).
  2. Silent worker-thread credential corruption (65534 mapped in gid_map): In a typical 65536-ID newgidmap mapping (for example namespace GIDs 0 -> 1000 and 1..=65535 -> 100000..=165534), make_kgid(user_ns, 65534) resolves to subordinate host GID 165533; 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:

  1. Spawn a process in a new user namespace (unshare --user without --map-group, leaving /proc/self/setgroups set to "allow").
  2. From the parent namespace, run newuidmap <pid> 0 <host_uid> 1 1 <subuid_start> 65535 and either newgidmap <pid> 0 <host_gid> 1 1 <subgid_start> 65533 (leaving 65534 unmapped) or newgidmap <pid> 0 <host_gid> 1 1 <subgid_start> 65535 (mapping 65534 to <subgid_start + 65533>).
  3. Inside the child, call lxutil::with_fs_identity(FsIdentity { uid: 1, gid: 1 }, || {}). When 65534 is unmapped, SwitchedCredentials::drop panics at vm/devices/support/fs/lxutil/src/unix/credentials.rs:161 with EINVAL; when 65534 is mapped, with_fs_identity returns Ok(()) after replacing the thread's original unmapped host supplementary groups with the mapped GID for 65534.

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.

Dominant language
Rust
Stars
831
Forks
56
Avg merge
13h 37m
Merged PRs (30d)
571

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/nvx

All issues in microsoft/nvx

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.