Skip to content

Commit 29876c0

Browse files
authored
fix(sandbox): verify effective UID/GID after privilege drop (#132)
Closes #32 Add post-condition checks in drop_privileges() to verify that setgid() and setuid() actually changed the effective IDs. Also verify that setuid(0) fails after dropping privileges, confirming root cannot be re-acquired. This is a defense-in-depth hardening measure per CWE-250 and CERT POS37-C. All added syscalls (geteuid, getegid, setuid) are async-signal-safe, so they are safe in the pre_exec context. Co-authored-by: John Myers <johntmyers@users.noreply.github.com>
1 parent 94d3fcd commit 29876c0

3 files changed

Lines changed: 131 additions & 3 deletions

File tree

‎architecture/sandbox.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,15 @@ Wraps `tokio::process::Child` + PID. Platform-specific `spawn()` methods delegat
950950
Resolves user/group names from policy, then:
951951
1. `initgroups()` to set supplementary groups (Linux only, not macOS)
952952
2. `setgid()` to target group
953-
3. `setuid()` to target user
953+
3. Verify `getegid()` matches the target GID
954+
4. `setuid()` to target user
955+
5. Verify `geteuid()` matches the target UID
956+
6. Verify `setuid(0)` fails (confirms root cannot be re-acquired)
954957

955958
The ordering is significant: `initgroups`/`setgid` must happen before `setuid` because switching user may drop the privileges needed for group manipulation. Similarly, privilege dropping must happen before Landlock because Landlock may block access to `/etc/passwd` and `/etc/group`.
956959

960+
Steps 3, 5, and 6 are defense-in-depth post-condition checks (CWE-250 / CERT POS37-C). All three syscalls (`geteuid`, `getegid`, `setuid`) are async-signal-safe, so they are safe to call in the `pre_exec` context. The checks add negligible overhead while guarding against hypothetical kernel-level defects that could cause `setuid`/`setgid` to return success without actually changing the effective IDs.
961+
957962
### `ProcessStatus`
958963

959964
Exit code is `code` if the process exited normally, or `128 + signal` if killed by a signal (standard Unix convention). Returns `-1` if neither is available.

‎architecture/security-policy.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,12 @@ Controls privilege dropping for the sandboxed process. **Static field** -- immut
364364

365365
1. `initgroups()` -- set supplementary groups for the target user
366366
2. `setgid()` -- switch to the target group
367-
3. `setuid()` -- switch to the target user
367+
3. Verify `getegid()` matches the target GID (defense-in-depth, CWE-250 / CERT POS37-C)
368+
4. `setuid()` -- switch to the target user
369+
5. Verify `geteuid()` matches the target UID
370+
6. Verify `setuid(0)` fails -- confirms root cannot be re-acquired
368371

369-
This happens before Landlock and seccomp are applied because `initgroups` needs access to `/etc/group` and `/etc/passwd`, which Landlock may subsequently block. See `crates/navigator-sandbox/src/process.rs` -- `drop_privileges()`.
372+
This happens before Landlock and seccomp are applied because `initgroups` needs access to `/etc/group` and `/etc/passwd`, which Landlock may subsequently block. The post-condition checks (steps 3, 5, 6) are async-signal-safe and add negligible overhead while guarding against hypothetical kernel-level defects. See `crates/navigator-sandbox/src/process.rs` -- `drop_privileges()`.
370373

371374
```yaml
372375
process:

‎crates/navigator-sandbox/src/process.rs‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,39 @@ pub fn drop_privileges(policy: &SandboxPolicy) -> Result<()> {
408408

409409
nix::unistd::setgid(group.gid).into_diagnostic()?;
410410

411+
// Verify effective GID actually changed (defense-in-depth, CWE-250 / CERT POS37-C)
412+
let effective_gid = nix::unistd::getegid();
413+
if effective_gid != group.gid {
414+
return Err(miette::miette!(
415+
"Privilege drop verification failed: expected effective GID {}, got {}",
416+
group.gid,
417+
effective_gid
418+
));
419+
}
420+
411421
if user_name.is_some() {
412422
nix::unistd::setuid(user.uid).into_diagnostic()?;
423+
424+
// Verify effective UID actually changed (defense-in-depth, CWE-250 / CERT POS37-C)
425+
let effective_uid = nix::unistd::geteuid();
426+
if effective_uid != user.uid {
427+
return Err(miette::miette!(
428+
"Privilege drop verification failed: expected effective UID {}, got {}",
429+
user.uid,
430+
effective_uid
431+
));
432+
}
433+
434+
// Verify root cannot be re-acquired (CERT POS37-C hardening).
435+
// If we dropped from root, setuid(0) must fail; success means privileges
436+
// were not fully relinquished.
437+
if nix::unistd::setuid(nix::unistd::Uid::from_raw(0)).is_ok() && user.uid.as_raw() != 0 {
438+
return Err(miette::miette!(
439+
"Privilege drop verification failed: process can still re-acquire root (UID 0) \
440+
after switching to UID {}",
441+
user.uid
442+
));
443+
}
413444
}
414445

415446
Ok(())
@@ -464,3 +495,92 @@ impl From<std::process::ExitStatus> for ProcessStatus {
464495
}
465496
}
466497
}
498+
499+
#[cfg(test)]
500+
mod tests {
501+
use super::*;
502+
use crate::policy::{
503+
FilesystemPolicy, LandlockPolicy, NetworkPolicy, ProcessPolicy, SandboxPolicy,
504+
};
505+
506+
/// Helper to create a minimal `SandboxPolicy` with the given process policy.
507+
fn policy_with_process(process: ProcessPolicy) -> SandboxPolicy {
508+
SandboxPolicy {
509+
version: 1,
510+
filesystem: FilesystemPolicy::default(),
511+
network: NetworkPolicy::default(),
512+
landlock: LandlockPolicy::default(),
513+
process,
514+
}
515+
}
516+
517+
#[test]
518+
fn drop_privileges_noop_when_no_user_or_group() {
519+
let policy = policy_with_process(ProcessPolicy {
520+
run_as_user: None,
521+
run_as_group: None,
522+
});
523+
assert!(drop_privileges(&policy).is_ok());
524+
}
525+
526+
#[test]
527+
fn drop_privileges_noop_when_empty_strings() {
528+
let policy = policy_with_process(ProcessPolicy {
529+
run_as_user: Some(String::new()),
530+
run_as_group: Some(String::new()),
531+
});
532+
assert!(drop_privileges(&policy).is_ok());
533+
}
534+
535+
#[test]
536+
fn drop_privileges_succeeds_for_current_user() {
537+
// Resolve the current user's name so we can ask drop_privileges to
538+
// "switch" to the user we're already running as. This exercises the
539+
// full verification path (getegid/geteuid checks) without needing root.
540+
let current_user = User::from_uid(nix::unistd::geteuid())
541+
.expect("getpwuid")
542+
.expect("current user entry");
543+
let current_group = Group::from_gid(nix::unistd::getegid())
544+
.expect("getgrgid")
545+
.expect("current group entry");
546+
547+
let policy = policy_with_process(ProcessPolicy {
548+
run_as_user: Some(current_user.name),
549+
run_as_group: Some(current_group.name),
550+
});
551+
552+
assert!(drop_privileges(&policy).is_ok());
553+
}
554+
555+
#[test]
556+
fn drop_privileges_fails_for_nonexistent_user() {
557+
let policy = policy_with_process(ProcessPolicy {
558+
run_as_user: Some("__nonexistent_test_user_42__".to_string()),
559+
run_as_group: None,
560+
});
561+
562+
let result = drop_privileges(&policy);
563+
assert!(result.is_err());
564+
let msg = format!("{}", result.unwrap_err());
565+
assert!(
566+
msg.contains("not found"),
567+
"expected 'not found' in error: {msg}"
568+
);
569+
}
570+
571+
#[test]
572+
fn drop_privileges_fails_for_nonexistent_group() {
573+
let policy = policy_with_process(ProcessPolicy {
574+
run_as_user: None,
575+
run_as_group: Some("__nonexistent_test_group_42__".to_string()),
576+
});
577+
578+
let result = drop_privileges(&policy);
579+
assert!(result.is_err());
580+
let msg = format!("{}", result.unwrap_err());
581+
assert!(
582+
msg.contains("not found"),
583+
"expected 'not found' in error: {msg}"
584+
);
585+
}
586+
}

0 commit comments

Comments
 (0)