Skip to content

Commit 7a152fa

Browse files
committed
fix(sandbox): make drop_privileges idempotent
When the current euid/egid already equal the policy's resolved target (e.g. a container entrypoint pre-dropped before exec'ing the sandbox), skip initgroups(3), which otherwise fails without CAP_SETGID. No behavioural change when a privilege drop is actually needed. Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>
1 parent 82d03f1 commit 7a152fa

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎crates/openshell-supervisor-process/src/process.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,13 @@ pub fn drop_privileges(policy: &SandboxPolicy) -> Result<()> {
934934
.ok_or_else(|| miette::miette!("Failed to resolve user primary group"))?
935935
};
936936

937+
// Idempotent fast-path: if euid/egid already match the target (e.g. a
938+
// container entrypoint pre-dropped before exec'ing the sandbox), skip
939+
// initgroups(3), which would otherwise fail without CAP_SETGID.
940+
if nix::unistd::geteuid() == user.uid && nix::unistd::getegid() == group.gid {
941+
return Ok(());
942+
}
943+
937944
if user_name.is_some() {
938945
let user_cstr =
939946
CString::new(user.name.clone()).map_err(|_| miette::miette!("Invalid user name"))?;

0 commit comments

Comments
 (0)