Tie Linux sandbox proxy bridges to the launcher lifetime (#61898)

## Why

Host proxy bridges must close active connections when a sandbox exits or is cancelled. Proxy descriptor exhaustion must also leave synthetic mount cleanup able to run.

## What changed

- Run host proxy bridges as threads in an outer supervisor that forwards signals and preserves the sandbox command's exit status. Use parent-death signals to terminate supervised children when their parent exits.
- Give bridge routes private descriptor tables where supported, retry accepts after descriptor exhaustion, and keep mount cleanup in a separate supervisor.
- Create the transparent TCP namespace in the bubblewrap child after re-executing the sandbox helper, clearing inherited credentials from memory and restoring dumpability for user namespace ID mapping.

## Testing

Add regression tests for bridge teardown after normal exit, process-group cancellation, launcher termination, and caller-thread exit; exit status propagation with inherited `SIGCHLD` ignored; and SOCKS connectivity and synthetic mount cleanup under descriptor pressure.

GitOrigin-RevId: dd297af98e9383942098fab5683ef2474c5ae554
This commit is contained in:
viyatb-oai
2026-11-16 22:27:42 +00:00
committed by copyberry
parent ce7079b506
commit 641b4cac88
7 changed files with 613 additions and 261 deletions
+106 -81
View File
@@ -1,6 +1,7 @@
use crate::proxy_lifecycle::close_fd;
use crate::proxy_lifecycle::harden_bridge_process;
use crate::proxy_lifecycle::detach_bridge_stdio;
use crate::proxy_lifecycle::move_fd_above_stdio;
use crate::proxy_lifecycle::private_bridge_control;
use crate::proxy_lifecycle::receive_listener;
use crate::proxy_lifecycle::send_listener;
use crate::seccomp::ProxyRouting;
@@ -11,6 +12,7 @@ use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::btree_map::Entry;
use std::fs::File;
use std::io;
use std::io::Read;
@@ -21,9 +23,9 @@ use std::net::Shutdown;
use std::net::SocketAddr;
use std::net::TcpListener;
use std::net::TcpStream;
use std::os::fd::AsFd;
use std::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::os::unix::net::UnixStream;
use std::sync::Arc;
use std::sync::mpsc;
@@ -57,13 +59,84 @@ struct ProxyRoutePlan {
has_proxy_config: bool,
}
/// One bootstrap socketpair per upstream endpoint. Only the sandbox end crosses exec.
pub(crate) struct HostProxyBridge {
endpoint: SocketAddr,
routing: ProxyRouting,
host_control: UnixStream,
sandbox_control: File,
attribution_token: Option<String>,
}
impl HostProxyBridge {
pub(crate) fn into_sandbox_control(self) -> File {
self.sandbox_control
}
}
/// Start bridge threads only in the outer supervisor, after the child inherits
/// the command's stdio. If setup fails, closing a host socket fails the handoff.
pub(crate) fn start_host_proxy_bridges(bridges: Vec<HostProxyBridge>) {
let mut startup_stderr = None;
let result: io::Result<()> = bridges
.into_iter()
.enumerate()
.try_for_each(|(index, bridge)| {
drop(bridge.sandbox_control);
if index == 0 {
// Reuse the slot freed above for diagnostics, even at the FD limit.
// The child already inherited its stdio; detach ours before starting any worker.
startup_stderr = io::stderr()
.as_fd()
.try_clone_to_owned()
.ok()
.map(File::from);
detach_bridge_stdio()?;
}
let control_fd = bridge.host_control.as_raw_fd();
let token = bridge.attribution_token.clone();
let (ready, initialized) = mpsc::channel();
let worker = std::thread::Builder::new()
.name("codex-proxy".to_string())
.spawn(move || {
// SAFETY: this new thread owns no FDs. The supervisor holds the socket
// until ready and its own descriptor table is unaffected.
let control = unsafe { private_bridge_control(control_fd)? };
ready.send(()).map_err(io::Error::other)?;
run_host_bridge(bridge.endpoint, bridge.routing, control, token)
})?;
if initialized.recv().is_ok() {
return Ok(());
}
// Reclaim the failed worker's task slot before falling back on hosts that
// prohibit private FD tables.
let _ = worker.join();
std::thread::Builder::new()
.name("codex-proxy".to_string())
.spawn(move || {
run_host_bridge(
bridge.endpoint,
bridge.routing,
bridge.host_control,
bridge.attribution_token,
)
})?;
Ok(())
});
if let (Err(err), Some(mut stderr)) = (result, startup_stderr) {
let _ = writeln!(stderr, "failed to start host proxy bridges: {err}");
}
}
pub(crate) fn prepare_host_proxy_route_spec(
port_allowlist: Option<&[u16]>,
routing: ProxyRouting,
) -> io::Result<(String, Vec<File>)> {
) -> io::Result<(String, Vec<HostProxyBridge>)> {
// The outer supervisor holds the upstream credential and host sockets.
codex_process_hardening::disable_process_dumping()?;
let (attribution_token, plan) = extract_attribution_token_and_plan(std::env::vars().collect());
// SAFETY: the sandbox helper is single-threaded here, before it forks bridge workers or
// executes the user command.
// SAFETY: bubblewrap and the host bridge threads have not been started yet.
unsafe {
std::env::remove_var(PROXY_ATTRIBUTION_TOKEN_ENV_KEY);
}
@@ -77,7 +150,7 @@ pub(crate) fn prepare_host_proxy_route_spec(
return Err(io::Error::new(io::ErrorKind::InvalidInput, message));
}
// Validate the entire plan before forking any host-side workers or sending
// Validate the entire plan before starting any host-side workers or sending
// attribution. An empty enforced list grants no routes.
if let Some(ports) = port_allowlist
&& plan
@@ -91,36 +164,30 @@ pub(crate) fn prepare_host_proxy_route_spec(
));
}
let mut control_by_endpoint = BTreeMap::new();
for route in &plan.routes {
if control_by_endpoint.contains_key(&route.endpoint) {
continue;
}
let control = spawn_host_bridge(
route.endpoint,
routing,
attribution_token.as_deref(),
&mut control_by_endpoint,
)?;
control_by_endpoint.insert(route.endpoint, control);
}
let mut bridges = BTreeMap::new();
let mut routes = Vec::with_capacity(plan.routes.len());
for route in plan.routes {
let Some(control) = control_by_endpoint.get(&route.endpoint) else {
return Err(io::Error::other(format!(
"missing bootstrap channel for endpoint {}",
route.endpoint
)));
let bridge = match bridges.entry(route.endpoint) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let (host, sandbox) = UnixStream::pair()?;
entry.insert(HostProxyBridge {
endpoint: route.endpoint,
routing,
host_control: UnixStream::from(move_fd_above_stdio(host.into())?),
sandbox_control: File::from(move_fd_above_stdio(sandbox.into())?),
attribution_token: attribution_token.clone(),
})
}
};
routes.push(ProxyRouteEntry {
env_key: route.env_key,
control_fd: control.as_raw_fd(),
control_fd: bridge.sandbox_control.as_raw_fd(),
});
}
let spec = serde_json::to_string(&ProxyRouteSpec { routes }).map_err(io::Error::other)?;
Ok((spec, control_by_endpoint.into_values().collect()))
Ok((spec, bridges.into_values().collect()))
}
fn extract_attribution_token_and_plan(
@@ -329,51 +396,6 @@ fn rewrite_proxy_env_value(proxy_url: &str, local_port: u16) -> Option<String> {
Some(rewritten)
}
fn spawn_host_bridge(
endpoint: SocketAddr,
routing: ProxyRouting,
attribution_token: Option<&str>,
inherited_controls: &mut BTreeMap<SocketAddr, File>,
) -> io::Result<File> {
let (host_control, control) = UnixStream::pair()?;
let host_control = UnixStream::from(move_fd_above_stdio(host_control.into())?);
let mut control = UnixStream::from(move_fd_above_stdio(control.into())?);
control.set_read_timeout(Some(HANDOFF_TIMEOUT))?;
let parent_pid = unsafe { libc::getpid() };
let pid = unsafe { libc::fork() };
if pid < 0 {
return Err(io::Error::last_os_error());
}
if pid == 0 {
drop(control);
// A worker must not retain another route's inner bootstrap endpoint.
// Drop this child's inherited owners; the parent's copies are unaffected by fork.
inherited_controls.clear();
let result = run_host_bridge(
endpoint,
routing,
host_control,
attribution_token,
parent_pid,
);
if result.is_err() {
unsafe { libc::_exit(1) };
}
unsafe { libc::_exit(0) };
}
drop(host_control);
let mut ready = [0_u8; 1];
control.read_exact(&mut ready)?;
if ready != [HOST_BRIDGE_READY] {
return Err(io::Error::other(
"host bridge did not acknowledge readiness",
));
}
Ok(File::from(OwnedFd::from(control)))
}
/// Each connection worker holds a permit until it exits.
pub(super) struct ConnectionSlots(Arc<Semaphore>);
@@ -403,22 +425,25 @@ fn run_host_bridge(
endpoint: SocketAddr,
routing: ProxyRouting,
mut control: UnixStream,
attribution_token: Option<&str>,
parent_pid: libc::pid_t,
attribution_token: Option<String>,
) -> io::Result<()> {
harden_bridge_process(parent_pid)?;
control.write_all(&[HOST_BRIDGE_READY])?;
// The outer helper must launch bubblewrap before this listener can arrive.
// The socket keeps its isolated network namespace even though we accept in
// the host process; newly opened upstream sockets use our host namespace.
// The listener stays in the sandbox network namespace. Connections to the
// upstream proxy are opened here in the host network namespace.
let listener = receive_listener(&control)?;
control.write_all(&[HOST_BRIDGE_READY])?;
drop(control);
let attribution_token = attribution_token.map(str::to_owned);
let slots = ConnectionSlots::new();
loop {
let (client_stream, _) = listener.accept()?;
let client_stream = match listener.accept() {
Ok((stream, _)) => stream,
// Keep the listener available when descriptor pressure subsides.
Err(err) if matches!(err.raw_os_error(), Some(libc::EMFILE | libc::ENFILE)) => {
std::thread::sleep(Duration::from_millis(/*millis*/ 100));
continue;
}
Err(err) => return Err(err),
};
let attribution_token = attribution_token.clone();
slots.spawn(routing, move || {
let mut tcp_stream = match TcpStream::connect(endpoint) {