Skip to content

openvmm: lxutil: with_fs_identity retains unmapped host supplementary groups when target GID equals overflowgid #1600

Open

Description

@tangruize

Summary

lxutil::with_fs_identity skips clearing supplementary groups whenever every GID returned by getgroups(2) equals identity.gid. In a Linux user namespace that leaves the VMM thread's host supplementary groups unmapped in gid_map, getgroups(2) reports every unmapped group as the kernel overflow GID (/proc/sys/kernel/overflowgid, 65534 by default). When identity.gid equals that overflow GID (reachable via a guest FUSE request with gid == 65534 under --mount-owner caller in a user namespace mapping 65534), with_fs_identity skips setgroups(&[]) and runs operation with Ok(...) while the thread retains all of its original unmapped host supplementary groups. Found by testing.

Root Cause

In vm/devices/support/fs/lxutil/src/unix/credentials.rs, SwitchedCredentials::switch (invoked by with_fs_identity) decides whether to drop supplementary groups by comparing the gid_t values returned by groups() against identity.gid:

        // 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);
        }

On Linux, getgroups(2) converts each kernel kgid_t in the thread's cred->group_info to the caller's user namespace via from_kgid_munged, which maps every kgid_t lacking an entry in /proc/self/gid_map to /proc/sys/kernel/overflowgid (65534 by default) while leaving the underlying host kgid_t values intact in cred->group_info for VFS permission checks (in_group_p). When OpenVMM runs in a user namespace that maps 65534 (for example unshare --user --map-user=0 --map-group=65534) and the host user belongs to unmapped supplementary groups, groups() returns [65534, 65534, ...]. For a target identity with gid == 65534 — which CallerIdentity::host_identity and run_as pass directly from any guest FUSE request with request.gid() == 65534 (nobody/nogroup) under --mount-owner caller — groups.iter().any(|&group| group != identity.gid) is false. SwitchedCredentials::switch skips set_groups(&[]) (never encountering the namespace's /proc/self/setgroups == "deny" restriction), switches fsuid/fsgid, clears effective capabilities, and executes operation with the host's supplementary groups still active in the kernel.

Consequence

with_fs_identity explicitly guarantees that operation "runs without effective capabilities and without supplementary groups other than the GID of identity, so that it gains no access through the privileges or groups of the thread's original identity" and fails closed with EPERM when those groups cannot be dropped. Instead, in a rootless user namespace that maps 65534 while leaving the VMM user's extra host supplementary groups unmapped, a guest process issuing virtio-fs requests with gid = 65534 retains group-based DAC access (in_group_p, group rwx permissions, and S_ISGID preservation on chmod) for every unmapped host supplementary group of the VMM process across the shared directory tree.

Reproduction

On a Linux host where the current user belongs to at least one supplementary group besides its effective GID, enter a user namespace via unshare --user --map-user=0 --map-group=65534 (so /proc/self/setgroups is "deny" and unmapped supplementary groups report as 65534) and call lxutil::with_fs_identity(FsIdentity { uid: 0, gid: 65534 }, || ...). On commit 9d64daad3cbe14fb0591081b73c6c9648e18d0fb, with_fs_identity returns Ok(...) instead of Err(lx::Error::EPERM), /proc/thread-self/status inside the closure still lists Groups: 65534 ... with CapEff: 0x0, and chmod(0o2755) on a file owned by one of the host user's unmapped supplementary groups preserves S_ISGID. Running cargo nextest run --profile agent -p lxutil -E 'test(unmapped_supplementary_groups_are_dropped_or_refused)' reproduces the failure before fix.patch and passes afterward.

Related: #1606.

Activity

  1. tangruize commented on Nov 15, 2026

    @tangruize
    ContributorAuthor

    Suggested Fix

    Read the kernel overflow GID from /proc/sys/kernel/overflowgid (defaulting to 65534 if unreadable) and, in SwitchedCredentials::switch, also clear supplementary groups via set_groups(&[]) whenever !groups.is_empty() && identity.gid == overflow_gid(). Because any overflow_gid() entry returned by getgroups(2) may represent an arbitrary unmapped host group, with_fs_identity then either drops the supplementary groups (when setgroups is permitted) or fails closed with lx::Error::EPERM without running operation (when the user namespace denies setgroups).

    Patch against OpenVMM 9d64daad3cbe14fb0591081b73c6c9648e18d0fb. Save the contents below as fix.patch and run git apply fix.patch.

    fix.patch
    diff --git a/vm/devices/support/fs/lxutil/src/unix/credentials.rs b/vm/devices/support/fs/lxutil/src/unix/credentials.rs
    index 3cd9cee0d..617712d01 100644
    --- a/vm/devices/support/fs/lxutil/src/unix/credentials.rs
    +++ b/vm/devices/support/fs/lxutil/src/unix/credentials.rs
    @@ -63,7 +63,9 @@ pub fn current() -> Self {
     /// than the GID of `identity`, so that it gains no access through the privileges or groups of
     /// the thread's original identity. Assuming a different identity, or dropping supplementary
     /// groups, requires `CAP_SETUID` and `CAP_SETGID`. A thread that already has `identity` and no
    -/// other supplementary groups needs no privilege.
    +/// other supplementary groups needs no privilege, unless the GID of `identity` is the kernel's
    +/// overflow GID and the thread has a supplementary group: `getgroups` reports a group that the
    +/// thread's user namespace does not map as that GID, so such a group is dropped as another one.
     ///
     /// # Errors
     ///
    @@ -111,7 +113,11 @@ fn switch(identity: FsIdentity) -> lx::Result<Self> {
             };
             // On an error below, dropping `credentials` restores whatever was already changed.
             let groups = groups()?;
    -        if groups.iter().any(|&group| group != identity.gid) {
    +        // `getgroups` reports a group that the thread's user namespace does not map as the
    +        // overflow GID, so a reported overflow GID may stand for any other group: drop it too.
    +        if groups.iter().any(|&group| group != identity.gid)
    +            || (!groups.is_empty() && identity.gid == overflow_gid())
    +        {
                 set_groups(&[])?;
                 credentials.groups = Some(groups);
             }
    @@ -193,6 +199,16 @@ fn groups() -> lx::Result<Vec<lx::gid_t>> {
         Ok(groups)
     }
     
    +/// The GID that the kernel reports for a group that the caller's user namespace does not map
    +/// (`/proc/sys/kernel/overflowgid`, 65534 unless configured otherwise).
    +fn overflow_gid() -> lx::gid_t {
    +    const DEFAULT_OVERFLOW_GID: lx::gid_t = 65534;
    +    std::fs::read_to_string("/proc/sys/kernel/overflowgid")
    +        .ok()
    +        .and_then(|value| value.trim().parse().ok())
    +        .unwrap_or(DEFAULT_OVERFLOW_GID)
    +}
    +
     /// Sets the calling thread's supplementary groups.
     fn set_groups(groups: &[lx::gid_t]) -> lx::Result<()> {
         let count = libc::c_long::try_from(groups.len()).map_err(|_| lx::Error::EINVAL)?;
    diff --git a/vm/devices/support/fs/lxutil/tests/switch_identity_unmapped_groups.rs b/vm/devices/support/fs/lxutil/tests/switch_identity_unmapped_groups.rs
    new file mode 100644
    index 000000000..91c15e1f1
    --- /dev/null
    +++ b/vm/devices/support/fs/lxutil/tests/switch_identity_unmapped_groups.rs
    @@ -0,0 +1,132 @@
    +// Copyright (c) Microsoft Corporation.
    +// Licensed under the MIT License.
    +
    +//! Regression test for `lxutil::with_fs_identity` in a user namespace that
    +//! leaves the thread's supplementary groups unmapped.
    +
    +#![cfg(target_os = "linux")]
    +
    +use lxutil::FsIdentity;
    +use lxutil::with_fs_identity;
    +use std::os::unix::fs::MetadataExt;
    +use std::os::unix::fs::PermissionsExt;
    +use std::path::PathBuf;
    +use std::process::Command;
    +
    +const TEST_NAME: &str = "unmapped_supplementary_groups_are_dropped_or_refused";
    +const CHILD_ENV: &str = "LXUTIL_SWITCH_IDENTITY_IN_USERNS";
    +const PROBE_ENV: &str = "LXUTIL_SWITCH_IDENTITY_PROBE_FILE";
    +const S_ISGID: u32 = 0o2000;
    +
    +/// The kernel's overflow GID (`/proc/sys/kernel/overflowgid`, 65534 by
    +/// default), which `unshare --map-group` maps here.
    +fn overflow_gid() -> u32 {
    +    std::fs::read_to_string("/proc/sys/kernel/overflowgid")
    +        .ok()
    +        .and_then(|s| s.trim().parse().ok())
    +        .unwrap_or(65534)
    +}
    +
    +/// The calling thread's effective GID, supplementary groups, and effective
    +/// capabilities, from `/proc/thread-self/status`.
    +fn thread_status() -> (u32, Vec<u32>, u64) {
    +    let status = std::fs::read_to_string("/proc/thread-self/status").unwrap();
    +    let mut egid = None;
    +    let mut groups = None;
    +    let mut cap_eff = None;
    +    for line in status.lines() {
    +        if let Some(rest) = line.strip_prefix("Gid:") {
    +            egid = rest.split_whitespace().nth(1).map(|s| s.parse().unwrap());
    +        } else if let Some(rest) = line.strip_prefix("Groups:") {
    +            groups = Some(
    +                rest.split_whitespace()
    +                    .map(|s| s.parse().unwrap())
    +                    .collect(),
    +            );
    +        } else if let Some(rest) = line.strip_prefix("CapEff:") {
    +            cap_eff = Some(u64::from_str_radix(rest.trim(), 16).unwrap());
    +        }
    +    }
    +    (egid.unwrap(), groups.unwrap(), cap_eff.unwrap())
    +}
    +
    +/// In a user namespace that maps only the overflow GID, `getgroups` reports
    +/// every unmapped supplementary group as the overflow GID. A switch to that GID
    +/// must still drop them, and since the namespace denies `setgroups`, it must
    +/// fail with `EPERM`. Before the fix it returned `Ok` and the thread kept its
    +/// original groups: a file of one of them kept `S_ISGID` through `chmod`.
    +#[test]
    +fn unmapped_supplementary_groups_are_dropped_or_refused() {
    +    if std::env::var_os(CHILD_ENV).is_some() {
    +        in_user_namespace();
    +        return;
    +    }
    +
    +    let (egid, groups, _) = thread_status();
    +    let Some(extra_gid) = groups.iter().copied().find(|&g| g != egid) else {
    +        eprintln!("skipped: the test process has no supplementary group other than its GID");
    +        return;
    +    };
    +    let overflow = overflow_gid();
    +    let map_group_arg = format!("--map-group={overflow}");
    +    let usable = Command::new("unshare")
    +        .args(["--user", "--map-user=0", &map_group_arg, "true"])
    +        .output()
    +        .is_ok_and(|output| output.status.success());
    +    if !usable {
    +        eprintln!("skipped: `unshare --user` is unavailable here");
    +        return;
    +    }
    +
    +    let directory = tempfile::tempdir().unwrap();
    +    let probe = directory.path().join("probe");
    +    std::fs::write(&probe, b"").unwrap();
    +    let probe_chowned = std::os::unix::fs::chown(&probe, None, Some(extra_gid)).is_ok();
    +
    +    let mut command = Command::new("unshare");
    +    command
    +        .args(["--user", "--map-user=0", &map_group_arg])
    +        .arg(std::env::current_exe().unwrap())
    +        .args(["--exact", TEST_NAME, "--nocapture"])
    +        .env(CHILD_ENV, "1");
    +    if probe_chowned {
    +        command.env(PROBE_ENV, &probe);
    +    }
    +    let output = command.output().unwrap();
    +    assert!(
    +        output.status.success(),
    +        "the switch inside the user namespace failed its check:\nstdout:\n{}\nstderr:\n{}",
    +        String::from_utf8_lossy(&output.stdout),
    +        String::from_utf8_lossy(&output.stderr),
    +    );
    +}
    +
    +fn in_user_namespace() {
    +    let setgroups = std::fs::read_to_string("/proc/self/setgroups").unwrap();
    +    assert_eq!(setgroups.trim(), "deny");
    +    let target = FsIdentity {
    +        uid: 0,
    +        gid: overflow_gid(),
    +    };
    +    let result = with_fs_identity(target, || {
    +        let (_, groups, cap_eff) = thread_status();
    +        // Without `CAP_FSETID`, `chmod` keeps `S_ISGID` only if the file's
    +        // group is the thread's fsgid or one of its supplementary groups.
    +        let kept_sgid = std::env::var_os(PROBE_ENV).map(|probe| {
    +            let probe = PathBuf::from(probe);
    +            std::fs::set_permissions(&probe, std::fs::Permissions::from_mode(0o2755)).unwrap();
    +            std::fs::metadata(&probe).unwrap().mode() & S_ISGID != 0
    +        });
    +        (groups, cap_eff, kept_sgid)
    +    });
    +    match result {
    +        Err(err) => assert_eq!(err, lx::Error::EPERM),
    +        Ok((groups, cap_eff, kept_sgid)) => {
    +            panic!(
    +                "with_fs_identity({target:?}) returned Ok in a namespace that denies setgroups: \
    +                 groups {groups:?}, CapEff {cap_eff:#x}, S_ISGID kept through an original group: \
    +                 {kept_sgid:?}"
    +            );
    +        }
    +    }
    +}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: openvmmInitial title-keyword triage suggestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions