Reap orphaned processes in Linux sandboxes (#38396)

## Why

Sandboxed descendants can outlive their immediate parent and must be collected by
PID 1 in the Bubblewrap namespace.

## What changed

- Launch `codex-linux-sandbox` with Bubblewrap's `--as-pid-1` option, and fall
  back to the bundled Bubblewrap when the system version does not support it.
- Run the sandboxed command as a child, forward signals to it, reap other exited
  descendants, and preserve the command's exit status.
- Verify proxy bridge parent identity when arming its parent-death signal.

## Testing

Added Linux sandbox coverage for the filtered namespace reaper, orphan
collection, and fallback from an incompatible system Bubblewrap.

GitOrigin-RevId: 379f08d6c2732ea0a4caeb61f93ae302e16d2458
This commit is contained in:
viyatb-oai authored and copyberry committed 2026-08-13 15:45:51 +00:00
1 parent ef596c68ca
commit 779e9114ae
6 files changed
+261 -12

No files matched your search

@@ -118,17 +118,17 @@ fn cleanup_proxy_socket_dir(socket_dir: &Path) -> io::Result<()> {
}
}
pub(crate) fn harden_bridge_process() -> io::Result<()> {
pub(crate) fn harden_bridge_process(expected_parent_pid: libc::pid_t) -> io::Result<()> {
detach_bridge_stdio()?;
set_parent_death_signal()?;
set_parent_death_signal(expected_parent_pid)?;
codex_process_hardening::disable_process_dumping()
}
fn set_parent_death_signal() -> io::Result<()> {
fn set_parent_death_signal(expected_parent_pid: libc::pid_t) -> io::Result<()> {
let res = unsafe { libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) };
if res != 0 {
Err(io::Error::last_os_error())
} else if unsafe { libc::getppid() } == 1 {
} else if unsafe { libc::getppid() } != expected_parent_pid {
Err(io::Error::other("parent process already exited"))
} else {
Ok(())