diff --git a/codex-rs/linux-sandbox/README.md b/codex-rs/linux-sandbox/README.md index 0d9204f0e7..23cb92856d 100644 --- a/codex-rs/linux-sandbox/README.md +++ b/codex-rs/linux-sandbox/README.md @@ -22,7 +22,9 @@ codex sandbox --transparent-tcp --permission-profile db -- psql -h pg.example The Rust sandbox helper includes the TCP stack. Codex starts it automatically. The Linux host needs distro `bwrap` on `PATH`, user and network namespaces, IPv6, and read/write access to `/dev/net/tun`. -Use full managed SOCKS5 mode for native database ports and an isolated PID namespace. +Use full managed SOCKS5 mode for native database ports. Transparent TCP uses +PID isolation by default and also supports exec-server's +`--linux-sandbox-pid-namespace=inherit` in dedicated environments. For proxy configuration, executor-wide capture and HTTP CONNECT routing, see the [exec-server setup](../exec-server/README.md#transparent-tcp). The private DNS stub maps up to 8,192 names to stable addresses in `198.18.0.0/15` @@ -33,7 +35,20 @@ connections. When the TCP stack is full, clients retry their opening SYN. The re native socket by both endpoints and its inode in the private namespace TCP tables. Closing that socket cancels its pending approval connection, including when other clients use the same destination. A client that shuts down only its write side can still receive a reply. General UDP, -QUIC, ICMP, legacy Landlock, host PID inheritance, and procfs fallback are outside this mode. +QUIC, ICMP, legacy Landlock, and procfs fallback are outside this mode. + +With PID isolation, the relay runs in the namespace's PID 1 process. When it +exits, the kernel kills the remaining processes in the PID namespace. +With PID inheritance, a supervisor runs the relay and the command as separate +children. Cleanup depends on which process exits first. + +- If the relay dies first, the supervisor sends `SIGKILL` to the command's + process group and the command itself. Descendants that left the group can + survive. +- If the supervisor dies first, the relay and the command receive the + parent-death `SIGKILL`. Background descendants can survive even if they + stayed in the command's process group. This is an existing limitation of + inherited-PID mode when the launcher dies. With transparent TCP off, Codex prefers the first `bwrap` found on `PATH` outside the current working directory whenever it is available. If `bwrap` is @@ -122,7 +137,9 @@ commands that would enter the bubblewrap path. sockets unless `dangerously_allow_all_unix_sockets` is granted. Unix socket pairs remain allowed for communication between related processes. - When bubblewrap is active, it mounts a fresh `/proc` via `--proc /proc` by default. - Transparent TCP requires this mount and aborts the launch if it fails. + Transparent TCP with PID isolation requires this mount and aborts the launch + if it fails. With PID inheritance it uses the existing `/proc` view and checks + that the relay can see socket identities before starting the command. In other modes, if that mount is denied, it retains the inherited `/proc` and still creates a PID namespace, preserving the existing fallback. `--no-proc` also retains the inherited `/proc` without disabling PID isolation. In these cases, process IDs diff --git a/codex-rs/linux-sandbox/src/linux_run_main.rs b/codex-rs/linux-sandbox/src/linux_run_main.rs index 4a8774fbc1..d580b0d972 100644 --- a/codex-rs/linux-sandbox/src/linux_run_main.rs +++ b/codex-rs/linux-sandbox/src/linux_run_main.rs @@ -167,10 +167,8 @@ pub fn run_main() -> ! { } else { ProxyRouting::Explicit }; - if transparent_tcp - && (!allow_network_for_proxy || use_legacy_landlock || inherit_pid_namespace || no_proc) - { - panic!("transparent TCP requires managed networking and private bubblewrap namespaces"); + if transparent_tcp && (!allow_network_for_proxy || use_legacy_landlock || no_proc) { + panic!("transparent TCP requires managed networking, bubblewrap, and procfs"); } if transparent_tcp_fd.is_empty() == (transparent_tcp && apply_seccomp_then_exec) { panic!("invalid transparent TCP namespace handoff"); @@ -229,6 +227,12 @@ pub fn run_main() -> ! { Relay::prepare(transparent_tcp_fd, socks_bridge) .unwrap_or_else(|err| panic!("failed to prepare transparent TCP: {err}")) }); + if transparent_tcp && inherit_pid_namespace { + // The relay's open descriptors must be private even when its PID is visible. + codex_process_hardening::disable_process_dumping() + .unwrap_or_else(|err| panic!("failed to protect transparent TCP processes: {err}")); + } + let supervisor_pid = unsafe { libc::getpid() }; let signal_mask = ForwardedSignalMask::block(); let command_pid = unsafe { libc::fork() }; if command_pid < 0 { @@ -237,6 +241,9 @@ pub fn run_main() -> ! { } if command_pid == 0 { + if transparent_tcp && inherit_pid_namespace { + kill_on_parent_death(supervisor_pid); + } if let Some((relay, mut reader)) = transport { drop(relay); reader @@ -248,6 +255,52 @@ pub fn run_main() -> ! { exec_or_panic(command); } + if inherit_pid_namespace && let Some((relay, reader)) = transport { + drop(reader); + // Bubblewrap starts a new session. Give the workload its own group + // within it so relay failure can kill the group while we stay alive + // to reap the command. The child is still waiting for relay readiness. + if unsafe { libc::setpgid(command_pid, command_pid) } < 0 { + let err = std::io::Error::last_os_error(); + panic!("failed to set transparent TCP workload process group: {err}"); + } + let signal_forwarders = install_bwrap_signal_forwarders(command_pid); + let relay_pid = unsafe { libc::fork() }; + if relay_pid < 0 { + let err = std::io::Error::last_os_error(); + panic!("failed to fork transparent TCP relay: {err}"); + } + if relay_pid == 0 { + reset_forwarded_signal_handlers_to_default(); + kill_on_parent_death(supervisor_pid); + signal_mask.restore(); + relay + .serve() + .unwrap_or_else(|err| panic!("failed to start transparent TCP relay: {err}")); + loop { + std::thread::park(); + } + } + // The supervisor must release its copies of the TUN and readiness + // writer so early relay failure also closes the startup pipe. + drop(relay); + signal_mask.restore(); + let (pid, status) = wait_for_child(/*pid*/ -1); + let exit_signal_mask = ForwardedSignalMask::block(); + if pid == relay_pid { + eprintln!("transparent TCP relay exited before the command"); + send_signal_to_bwrap_child(command_pid, libc::SIGKILL); + wait_for_child(command_pid); + } else { + debug_assert_eq!(pid, command_pid); + unsafe { libc::kill(relay_pid, libc::SIGKILL) }; + wait_for_child(relay_pid); + } + signal_forwarders.restore(); + exit_signal_mask.restore(); + exit_with_wait_status(status); + } + let signal_forwarders = install_bwrap_signal_forwarders(command_pid); if let Some((relay, reader)) = transport { drop(reader); @@ -257,22 +310,13 @@ pub fn run_main() -> ! { } signal_mask.restore(); loop { - let mut status = 0; - let reaped_pid = unsafe { libc::waitpid(-1, &mut status, 0) }; + let (reaped_pid, status) = wait_for_child(/*pid*/ -1); 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}"); - } } } @@ -335,7 +379,7 @@ pub fn run_main() -> ! { crate::wslg::ensure_supported_path(&executable) .unwrap_or_else(|err| exit_with_bwrap_build_error(err)); } - let inner = build_inner_seccomp_command(InnerSeccompCommandArgs { + let mut inner = build_inner_seccomp_command(InnerSeccompCommandArgs { sandbox_policy_cwd: &sandbox_policy_cwd, command_cwd: command_cwd.as_deref(), permission_profile: &permission_profile, @@ -343,6 +387,9 @@ pub fn run_main() -> ! { proxy_route_spec, command, }); + if transparent_tcp && inherit_pid_namespace { + inner.insert(1, "--inherit-pid-namespace".to_string()); + } run_bwrap_with_proc_fallback( caller_pid, &sandbox_policy_cwd, @@ -454,8 +501,9 @@ fn run_bwrap_with_proc_fallback( transparent_tcp, ); - // Transparent TCP must mount procfs for the private PID namespace. Let the - // real bwrap launch fail if it cannot, since a root bind can expose host procfs. + // Transparent TCP with PID isolation requires a fresh procfs. With PID + // inheritance it uses the policy's existing procfs view. Relay readiness + // verifies that socket identities are visible before the command starts. if !transparent_tcp && options.mount_proc && !preflight_proc_mount_support(options) @@ -685,7 +733,7 @@ fn supervise_child( ) -> ! { let signal_forwarders = install_bwrap_signal_forwarders(pid); setup_signal_mask.restore(); - let status = wait_for_bwrap_child(pid); + let (_, status) = wait_for_child(pid); let cleanup_signal_mask = ForwardedSignalMask::block(); BWRAP_CHILD_PID.store(0, Ordering::SeqCst); cleanup(); @@ -816,18 +864,18 @@ fn reset_forwarded_signal_handlers_to_default() { } } -fn wait_for_bwrap_child(pid: libc::pid_t) -> libc::c_int { +fn wait_for_child(pid: libc::pid_t) -> (libc::pid_t, libc::c_int) { loop { let mut status: libc::c_int = 0; let wait_res = unsafe { libc::waitpid(pid, &mut status as *mut libc::c_int, 0) }; if wait_res >= 0 { - return status; + return (wait_res, status); } let err = std::io::Error::last_os_error(); if err.raw_os_error() == Some(libc::EINTR) { continue; } - panic!("waitpid failed for bubblewrap child: {err}"); + panic!("waitpid failed for child {pid}: {err}"); } } @@ -1190,7 +1238,7 @@ fn run_bwrap_in_child_capture_stderr(bwrap_args: crate::bwrap::BwrapArgs) -> Str panic!("failed to read bubblewrap stderr: {err}"); } - let status = wait_for_bwrap_child(pid); + let (_, status) = wait_for_child(pid); let cleanup_signal_mask = ForwardedSignalMask::block(); BWRAP_CHILD_PID.store(0, Ordering::SeqCst); cleanup_synthetic_mount_targets(&synthetic_mount_registrations); diff --git a/codex-rs/linux-sandbox/src/linux_run_main_tests.rs b/codex-rs/linux-sandbox/src/linux_run_main_tests.rs index 2909c19ebf..142ab71872 100644 --- a/codex-rs/linux-sandbox/src/linux_run_main_tests.rs +++ b/codex-rs/linux-sandbox/src/linux_run_main_tests.rs @@ -3,7 +3,7 @@ use super::*; #[cfg(test)] use crate::linux_run_main::install_bwrap_signal_forwarders; #[cfg(test)] -use crate::linux_run_main::wait_for_bwrap_child; +use crate::linux_run_main::wait_for_child; #[cfg(test)] use codex_network_proxy::ManagedNetworkSandboxContext; #[cfg(test)] @@ -462,7 +462,7 @@ fn bwrap_signal_forwarder_terminates_child_and_keeps_parent_alive() { run_bwrap_signal_forwarder_test_supervisor(); } - let status = wait_for_bwrap_child(supervisor_pid); + let (_, status) = wait_for_child(supervisor_pid); assert!(libc::WIFEXITED(status), "supervisor status: {status}"); assert_eq!(libc::WEXITSTATUS(status), 0); } @@ -489,7 +489,7 @@ fn run_bwrap_signal_forwarder_test_supervisor() -> ! { libc::raise(libc::SIGTERM); } - let status = wait_for_bwrap_child(child_pid); + let (_, status) = wait_for_child(child_pid); let child_terminated_by_sigterm = libc::WIFSIGNALED(status) && libc::WTERMSIG(status) == libc::SIGTERM; unsafe { diff --git a/codex-rs/linux-sandbox/src/managed_tcp.rs b/codex-rs/linux-sandbox/src/managed_tcp.rs index 272850254c..e58c6f36c2 100644 --- a/codex-rs/linux-sandbox/src/managed_tcp.rs +++ b/codex-rs/linux-sandbox/src/managed_tcp.rs @@ -63,7 +63,7 @@ impl Relay { crate::managed_tcp_dns::serve(Arc::clone(&names), self.dns_udp, self.dns_tcp)?; let (started, readiness) = std::sync::mpsc::sync_channel(1); thread::Builder::new().spawn(move || { - // This is the namespace reaper. Losing the data path must end the workload. + // Relay exit triggers workload cleanup in the PID namespace or its supervisor. if let Ok(Err(error)) = std::panic::catch_unwind(|| stack::run(self.tun, self.proxy, names, started)) { diff --git a/codex-rs/linux-sandbox/tests/suite/managed_tcp.rs b/codex-rs/linux-sandbox/tests/suite/managed_tcp.rs index 7beae59d50..b26db64566 100644 --- a/codex-rs/linux-sandbox/tests/suite/managed_tcp.rs +++ b/codex-rs/linux-sandbox/tests/suite/managed_tcp.rs @@ -5,13 +5,14 @@ use codex_utils_cargo_bin::runfiles; use pretty_assertions::assert_eq; use socks5_protocol::CommandRequest; use socks5_protocol::sync::FromIO; +use tokio::io::AsyncBufReadExt; use tokio::io::AsyncReadExt; use tokio::io::AsyncWriteExt; +use tokio::io::BufReader; #[tokio::test] async fn transparent_tcp_rejects_incompatible_options_and_untrusted_proxy_ports() { - let incompatible = - "transparent TCP requires managed networking and private bubblewrap namespaces"; + let incompatible = "transparent TCP requires managed networking, bubblewrap, and procfs"; let untrusted = "outside the managed sandbox context"; let mut env = create_env_from_core_vars(); strip_proxy_env(&mut env); @@ -19,7 +20,7 @@ async fn transparent_tcp_rejects_incompatible_options_and_untrusted_proxy_ports( for (context, extra, reason) in [ (None, None, incompatible), (Some("{}"), Some("--use-legacy-landlock"), incompatible), - (Some("{}"), Some("--inherit-pid-namespace"), incompatible), + (Some("{}"), Some("--inherit-pid-namespace"), untrusted), (Some("{}"), Some("--no-proc"), incompatible), (Some("{}"), None, untrusted), (Some(r#"{"loopbackPorts":[32102]}"#), None, untrusted), @@ -44,8 +45,14 @@ async fn transparent_tcp_rejects_incompatible_options_and_untrusted_proxy_ports( } } +#[test_case::test_case(&[], false; "isolated")] +#[test_case::test_case(&["--inherit-pid-namespace"], false; "inherited")] +#[test_case::test_case(&["--inherit-pid-namespace"], true; "inherited_relay_failure")] #[tokio::test] -async fn managed_tcp_native_client_uses_attributed_socks_and_preserves_restrictions() { +async fn managed_tcp_native_client_uses_attributed_socks_and_preserves_restrictions( + pid_options: &[&str], + kill_relay: bool, +) { // Container device policies can expose the node but deny opening it. if let Err(err) = std::fs::OpenOptions::new() .read(true) @@ -97,6 +104,16 @@ async fn managed_tcp_native_client_uses_attributed_socks_and_preserves_restricti PROXY_ATTRIBUTION_TOKEN_ENV_KEY.into(), "native-client".into(), ); + if !pid_options.is_empty() { + let pid_namespace = std::fs::read_link("/proc/self/ns/pid").unwrap(); + env.insert( + "CODEX_TEST_PID_NAMESPACE".into(), + pid_namespace.display().to_string(), + ); + } + if kill_relay { + env.insert("CODEX_TEST_RELAY_FAILURE".into(), "1".into()); + } let serve = |mut stdin: tokio::process::ChildStdin| async move { let mut kept = None; for (host, allow) in [ @@ -146,6 +163,9 @@ async fn managed_tcp_native_client_uses_attributed_socks_and_preserves_restricti }; let script = r#" unset ALL_PROXY all_proxy + if [ -n "${CODEX_TEST_PID_NAMESPACE:-}" ]; then + test "$(readlink /proc/self/ns/pid)" = "$CODEX_TEST_PID_NAMESPACE" + fi if command -v ip >/dev/null; then ip link show codextun >/dev/null if ip link set codextun down 2>/dev/null; then exit 15; fi @@ -176,7 +196,16 @@ PY fi if (exec 5<>/dev/tcp/127.0.0.2/"$1") 2>/dev/null; then exit 13; fi if (exec 5>>"$2") 2>/dev/null; then exit 14; fi + if [ -n "${CODEX_TEST_RELAY_FAILURE:-}" ]; then + # Keep stdout open in both processes so the test only sees EOF + # after cleanup reaches the whole process group. + sleep 60 & + printf '%s %s\n' "$PPID" "$!" + wait + fi "#; + let mut options = vec!["--transparent-tcp"]; + options.extend_from_slice(pid_options); let mut command = linux_sandbox_command_with_options( &[ "bash", @@ -189,7 +218,7 @@ PY &PermissionProfile::read_only(), Some(&format!(r#"{{"loopbackPorts":[{}]}}"#, proxy_addr.port())), env, - &["--transparent-tcp"], + &options, ); let mut child = command .stdin(std::process::Stdio::piped()) @@ -197,15 +226,49 @@ PY .spawn() .unwrap(); let peer = tokio::spawn(serve(child.stdin.take().unwrap())); + if kill_relay { + let mut stdout = BufReader::new(child.stdout.take().unwrap()); + let mut line = String::new(); + tokio::time::timeout(SANDBOX_TIMEOUT, stdout.read_line(&mut line)) + .await + .unwrap() + .unwrap(); + let (supervisor, background) = line.trim().split_once(' ').expect("workload PIDs"); + let children_path = format!("/proc/{supervisor}/task/{supervisor}/children"); + let children = std::fs::read_to_string(children_path).expect("supervisor children"); + let background: libc::pid_t = background.parse().unwrap(); + let workload_group = unsafe { libc::getpgid(background) }; + assert!(workload_group > 0 && workload_group != background); + let supervisor: libc::pid_t = supervisor.parse().unwrap(); + let relay = children + .split_whitespace() + .map(|pid| pid.parse::().unwrap()) + .find(|&pid| pid != workload_group) + .expect("relay sibling"); + assert_eq!(unsafe { libc::getpgid(relay) }, supervisor); + assert_eq!(unsafe { libc::kill(relay, libc::SIGKILL) }, 0); + let mut remaining = Vec::new(); + tokio::time::timeout(OPERATION_TIMEOUT, stdout.read_to_end(&mut remaining)) + .await + .expect("relay failure should close stdout for both workload processes") + .unwrap(); + } let output = tokio::time::timeout(SANDBOX_TIMEOUT, child.wait_with_output()) .await .unwrap() .unwrap(); - assert!( - output.status.success(), + assert_eq!( + output.status.code(), + Some(if kill_relay { 128 + libc::SIGKILL } else { 0 }), "{}", String::from_utf8_lossy(&output.stderr) ); + if kill_relay { + assert!( + String::from_utf8_lossy(&output.stderr) + .contains("transparent TCP relay exited before the command") + ); + } tokio::time::timeout(OPERATION_TIMEOUT, peer) .await .unwrap()