Skip to content

Commit 492225b

Browse files
committed
fix(sandbox): preserve CDI-injected GIDs across privilege drop
initgroups(3) replaces all supplemental groups with the user's entries from /etc/group, discarding GIDs injected by the container runtime via CDI (e.g. GID 44/video needed for /dev/nvmap on Tegra). Snapshot the container-level GIDs before initgroups runs and merge them back afterwards, excluding GID 0 (root) to avoid privilege retention. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent b0ac9ef commit 492225b

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

crates/openshell-sandbox/src/process.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,26 @@ pub fn drop_privileges(policy: &SandboxPolicy) -> Result<()> {
421421
target_os = "redox"
422422
)))]
423423
{
424+
// Snapshot the container-level supplemental GIDs (e.g. injected by
425+
// CDI for GPU device access) before initgroups replaces them.
426+
// Exclude GID 0 (root) to avoid inadvertent privilege retention.
427+
let root_gid = nix::unistd::Gid::from_raw(0);
428+
let container_gids: Vec<nix::unistd::Gid> = nix::unistd::getgroups()
429+
.unwrap_or_default()
430+
.into_iter()
431+
.filter(|&g| g != root_gid)
432+
.collect();
424433
nix::unistd::initgroups(user_cstr.as_c_str(), group.gid).into_diagnostic()?;
434+
// Merge back any CDI-injected GIDs that initgroups dropped so that
435+
// exec'd processes retain access to GPU devices (e.g. /dev/nvmap on
436+
// Tegra requires the video GID).
437+
let mut merged: Vec<nix::unistd::Gid> = nix::unistd::getgroups().unwrap_or_default();
438+
for gid in container_gids {
439+
if !merged.contains(&gid) {
440+
merged.push(gid);
441+
}
442+
}
443+
nix::unistd::setgroups(&merged).into_diagnostic()?;
425444
}
426445
}
427446

0 commit comments

Comments
 (0)