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

+34 -1
View File
@@ -198,7 +198,40 @@ pub fn run_main() -> ! {
) {
panic!("error applying Linux sandbox restrictions: {e:?}");
}
exec_or_panic(command);
let signal_mask = ForwardedSignalMask::block();
let command_pid = unsafe { libc::fork() };
if command_pid < 0 {
let err = std::io::Error::last_os_error();
panic!("failed to fork sandboxed command: {err}");
}
if command_pid == 0 {
reset_forwarded_signal_handlers_to_default();
signal_mask.restore();
exec_or_panic(command);
}
let signal_forwarders = install_bwrap_signal_forwarders(command_pid);
signal_mask.restore();
loop {
let mut status = 0;
let reaped_pid = unsafe { libc::waitpid(-1, &mut status, 0) };
if reaped_pid == command_pid {
let exit_signal_mask = ForwardedSignalMask::block();
signal_forwarders.restore();
exit_signal_mask.restore();
exit_with_wait_status(status);
}
if reaped_pid >= 0 {
continue;
}
let err = std::io::Error::last_os_error();
if err.raw_os_error() != Some(libc::EINTR) {
panic!("failed to reap sandboxed child: {err}");
}
}
}
if file_system_sandbox_policy.has_full_disk_write_access() && !allow_network_for_proxy {