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

オープン
#1,606 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
linux, rust
領域
security

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
831
フォーク
56
平均マージ
13時間 37分
マージ済み PR(30日)
571

環境構築

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートあり
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/nvx のほかの issue

microsoft/nvx の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。