From c9df641eac7631027d5e933feca1b10e1cc25a86 Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Mon, 16 Nov 2026 23:01:45 +0000 Subject: [PATCH] Preserve native sandbox executable paths and PTY dispatch names (#61910) ## Why Sandbox helpers with non-UTF-8 paths can lose their exact executable path when converted into command requests. PTY launches with a custom `argv[0]` can also try to execute the dispatch name instead of the configured program. ## What changed - Carry native executable paths through sandbox requests, local execution, shell snapshots, and sandbox integrity checks. - Use native Unix PTY spawning when `argv[0]` differs from the executable, preserving the dispatch name and controlling terminal setup. - Preserve PTY executable lookup through the child environment's `PATH` and fallback to its home directory when the requested working directory is missing. ## Testing Add regression coverage for non-UTF-8 sandbox helper paths, PTY dispatch names, child `HOME`/`PATH` lookup, controlling terminals, and exit-code preservation. GitOrigin-RevId: 6a2af0486220d98195f5d67fde2597e3c4515e11 --- codex-rs/Cargo.lock | 1 + codex-rs/app-server/src/command_exec.rs | 31 +++---- codex-rs/cli/tests/daemon_startup.rs | 2 +- codex-rs/cli/tests/worktree.rs | 4 +- codex-rs/core/src/exec.rs | 16 +++- codex-rs/core/src/exec_tests.rs | 4 + codex-rs/core/src/sandboxing/mod.rs | 17 ++++ codex-rs/core/src/spawn.rs | 4 +- codex-rs/core/src/tasks/user_shell.rs | 1 + codex-rs/core/src/tools/sandboxing.rs | 1 + codex-rs/core/src/unified_exec/mod_tests.rs | 46 +++++++++- .../core/src/unified_exec/process_manager.rs | 9 +- .../src/unified_exec/process_manager_tests.rs | 1 + codex-rs/exec-server/src/fs_sandbox.rs | 19 ++-- .../src/fs_sandbox_windows_tests.rs | 1 + codex-rs/exec-server/src/local_process.rs | 9 +- codex-rs/exec-server/src/process_sandbox.rs | 15 ++++ .../src/sandbox_integrity/backend_tests.rs | 1 + .../src/sandbox_integrity/bubblewrap.rs | 2 +- .../src/sandbox_integrity/dependencies.rs | 2 +- codex-rs/exec-server/src/shell_snapshot.rs | 8 +- codex-rs/sandboxing/src/launch.rs | 16 +--- codex-rs/sandboxing/src/manager.rs | 24 +++-- codex-rs/sandboxing/src/manager_tests.rs | 38 +++++++- codex-rs/sandboxing/src/spawn.rs | 31 ++++--- .../sandboxing/src/terminal_queries_tests.rs | 4 +- codex-rs/utils/pty/Cargo.toml | 1 + codex-rs/utils/pty/src/pty.rs | 90 +++++++++++++++---- codex-rs/utils/pty/src/spawn_helper_tests.rs | 9 +- codex-rs/utils/pty/src/tests.rs | 47 ++++++++++ 30 files changed, 346 insertions(+), 108 deletions(-) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 6cb83ba453..1098d48e34 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -5639,6 +5639,7 @@ dependencies = [ "lazy_static", "libc", "log", + "nix 0.28.0", "portable-pty", "pretty_assertions", "shared_library", diff --git a/codex-rs/app-server/src/command_exec.rs b/codex-rs/app-server/src/command_exec.rs index c71aff3ddf..8f79751b0f 100644 --- a/codex-rs/app-server/src/command_exec.rs +++ b/codex-rs/app-server/src/command_exec.rs @@ -228,17 +228,11 @@ impl CommandExecManager { return Ok(()); } - let ExecRequest { - command, - cwd, - env, - expiration, - sandbox: _sandbox, - arg0, - .. - } = exec_request; + let env = &exec_request.env; + let arg0 = &exec_request.arg0; // TODO(anp): Keep PathUri through the local command launch boundary. - let cwd = cwd + let cwd = exec_request + .cwd .to_abs_path() .map_err(|err| invalid_request(format!("invalid command cwd: {err}")))?; @@ -251,8 +245,8 @@ impl CommandExecManager { }; let sessions = Arc::clone(&self.sessions); - let (program, args) = command - .split_first() + let (program, args) = exec_request + .program_and_args() .ok_or_else(|| invalid_request("command must not be empty"))?; { let mut sessions = self.sessions.lock().await; @@ -272,22 +266,21 @@ impl CommandExecManager { program, args, cwd.as_path(), - &env, - &arg0, + env, + arg0, size.unwrap_or_default(), codex_utils_pty::ChildFds::Inherited(&[]), ) .await } else if stream_stdin { - codex_utils_pty::spawn_pipe_process(program, args, cwd.as_path(), &env, &arg0, &[]) - .await + codex_utils_pty::spawn_pipe_process(program, args, cwd.as_path(), env, arg0, &[]).await } else { codex_utils_pty::spawn_pipe_process_no_stdin( program, args, cwd.as_path(), - &env, - &arg0, + env, + arg0, &[], ) .await @@ -309,7 +302,7 @@ impl CommandExecManager { control_rx, stream_stdin, stream_stdout_stderr, - expiration, + expiration: exec_request.expiration, output_bytes_cap, }) .await; diff --git a/codex-rs/cli/tests/daemon_startup.rs b/codex-rs/cli/tests/daemon_startup.rs index 56a7cc76b3..4b994936fa 100644 --- a/codex-rs/cli/tests/daemon_startup.rs +++ b/codex-rs/cli/tests/daemon_startup.rs @@ -416,7 +416,7 @@ async fn daemon_startup(command: &str) -> Result<()> { codex.clone() }; let spawned = codex_utils_pty::spawn_pty_process( - &program.to_string_lossy(), + program.as_os_str(), &args, &workspace_path, &env, diff --git a/codex-rs/cli/tests/worktree.rs b/codex-rs/cli/tests/worktree.rs index 13b6914968..aea4463087 100644 --- a/codex-rs/cli/tests/worktree.rs +++ b/codex-rs/cli/tests/worktree.rs @@ -81,7 +81,7 @@ async fn rejected_start( expected: &str, ) -> anyhow::Result { let spawned = codex_utils_pty::spawn_pty_process( - &program.to_string_lossy(), + program.as_os_str(), args, cwd, env, @@ -443,7 +443,7 @@ trust_level = "trusted" assert!(!home.join("worktrees").exists()); } let spawned = codex_utils_pty::spawn_pty_process( - &program.to_string_lossy(), + program.as_os_str(), &args, &launcher, &env, diff --git a/codex-rs/core/src/exec.rs b/codex-rs/core/src/exec.rs index 029eb4ae01..7a72820225 100644 --- a/codex-rs/core/src/exec.rs +++ b/codex-rs/core/src/exec.rs @@ -2,6 +2,7 @@ use std::os::unix::process::ExitStatusExt; use std::collections::HashMap; +use std::ffi::OsString; use std::io; #[cfg(target_os = "windows")] use std::path::Path; @@ -454,6 +455,7 @@ pub(crate) async fn execute_exec_request( ) -> Result { let ExecRequest { command, + native_program, cwd, env, exec_server_env_config: _, @@ -502,6 +504,7 @@ pub(crate) async fn execute_exec_request( let start = Instant::now(); let raw_output_result = get_raw_output_result( params, + native_program, network_sandbox_policy, stdout_stream, after_spawn, @@ -519,6 +522,7 @@ pub(crate) async fn execute_exec_request( #[allow(clippy::too_many_arguments)] async fn get_raw_output_result( params: ExecParams, + native_program: Option, network_sandbox_policy: NetworkSandboxPolicy, stdout_stream: Option, after_spawn: Option>, @@ -543,7 +547,14 @@ async fn get_raw_output_result( .await; } - exec(params, network_sandbox_policy, stdout_stream, after_spawn).await + exec( + params, + native_program, + network_sandbox_policy, + stdout_stream, + after_spawn, + ) + .await } #[cfg(target_os = "windows")] @@ -925,6 +936,7 @@ fn aggregate_output( /// wrapper args, as appropriate. async fn exec( params: ExecParams, + native_program: Option, network_sandbox_policy: NetworkSandboxPolicy, stdout_stream: Option, after_spawn: Option>, @@ -962,7 +974,7 @@ async fn exec( })?; let arg0_ref = arg0.as_deref(); let child = spawn_child_async(SpawnChildRequest { - program: PathBuf::from(program), + program: native_program.map_or_else(|| PathBuf::from(program), PathBuf::from), args: args.into(), arg0: arg0_ref, cwd, diff --git a/codex-rs/core/src/exec_tests.rs b/codex-rs/core/src/exec_tests.rs index 0721a905cb..08eb56aa1b 100644 --- a/codex-rs/core/src/exec_tests.rs +++ b/codex-rs/core/src/exec_tests.rs @@ -287,6 +287,7 @@ async fn exec_full_buffer_capture_ignores_expiration() -> Result<()> { justification: None, arg0: None, }, + /*native_program*/ None, NetworkSandboxPolicy::Enabled, /*stdout_stream*/ None, /*after_spawn*/ None, @@ -323,6 +324,7 @@ async fn exec_full_buffer_capture_keeps_io_drain_timeout_when_descendant_holds_p justification: None, arg0: None, }, + /*native_program*/ None, NetworkSandboxPolicy::Enabled, /*stdout_stream*/ None, /*after_spawn*/ None, @@ -1311,6 +1313,7 @@ fn command_runs_after_integrity_check_failure(time_out: bool) -> anyhow::Result< // This boundary receives prepared argv. Exercise a real child without requiring // a native sandbox installation to test the checker's failure isolation. let request = SandboxExecRequest { + native_program: None, command: if cfg!(windows) { vec![ "cmd.exe".into(), @@ -1429,6 +1432,7 @@ async fn kill_child_process_group_kills_grandchildren_on_timeout() -> Result<()> let output = exec( params, + /*native_program*/ None, NetworkSandboxPolicy::Restricted, /*stdout_stream*/ None, /*after_spawn*/ None, diff --git a/codex-rs/core/src/sandboxing/mod.rs b/codex-rs/core/src/sandboxing/mod.rs index a5f61153d9..312da44b28 100644 --- a/codex-rs/core/src/sandboxing/mod.rs +++ b/codex-rs/core/src/sandboxing/mod.rs @@ -30,6 +30,8 @@ use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_path_uri::PathUri; use codex_utils_string::truncate_middle_with_token_budget; use std::collections::HashMap; +use std::ffi::OsStr; +use std::ffi::OsString; #[derive(Debug)] pub(crate) struct ExecOptions { @@ -46,6 +48,7 @@ pub(crate) struct ExecServerEnvConfig { #[derive(Debug)] pub struct ExecRequest { pub command: Vec, + pub(crate) native_program: Option, pub cwd: PathUri, pub env: HashMap, pub(crate) exec_server_env_config: Option, @@ -70,6 +73,17 @@ pub struct ExecRequest { } impl ExecRequest { + /// Borrow the host executable and arguments for a local launch. + pub fn program_and_args(&self) -> Option<(&OsStr, &[String])> { + let (program, args) = self.command.split_first()?; + Some(( + self.native_program + .as_deref() + .unwrap_or_else(|| OsStr::new(program)), + args, + )) + } + #[allow(clippy::too_many_arguments)] pub fn new( command: Vec, @@ -89,6 +103,7 @@ impl ExecRequest { let windows_sandbox_policy_cwd = cwd.clone(); Self { command, + native_program: None, cwd, env, exec_server_env_config: None, @@ -119,6 +134,7 @@ impl ExecRequest { codex_exec_server::run_integrity_checks(&request).await; let SandboxExecRequest { command, + native_program, cwd, sandbox_policy_cwd: windows_sandbox_policy_cwd, mut env, @@ -167,6 +183,7 @@ impl ExecRequest { } Ok(Self { command, + native_program, cwd, env, exec_server_env_config: None, diff --git a/codex-rs/core/src/spawn.rs b/codex-rs/core/src/spawn.rs index 260adc595d..d0b399b500 100644 --- a/codex-rs/core/src/spawn.rs +++ b/codex-rs/core/src/spawn.rs @@ -82,7 +82,9 @@ pub(crate) async fn spawn_child_async(request: SpawnChildRequest<'_>) -> std::io StdioPolicy::Inherit => Command::new(&program), }; #[cfg(unix)] - cmd.arg0(arg0.map_or_else(|| program.to_string_lossy().to_string(), String::from)); + if let Some(arg0) = arg0 { + cmd.arg0(arg0); + } cmd.args(args); cmd.current_dir(cwd); if let Some(network) = network { diff --git a/codex-rs/core/src/tasks/user_shell.rs b/codex-rs/core/src/tasks/user_shell.rs index 1ce6ebfbf4..8569320048 100644 --- a/codex-rs/core/src/tasks/user_shell.rs +++ b/codex-rs/core/src/tasks/user_shell.rs @@ -210,6 +210,7 @@ pub(crate) async fn execute_user_shell_command( let permission_profile = PermissionProfile::Disabled; let exec_env = ExecRequest { + native_program: None, command: exec_command.clone(), cwd: cwd.clone().into(), env: exec_env_map, diff --git a/codex-rs/core/src/tools/sandboxing.rs b/codex-rs/core/src/tools/sandboxing.rs index 9f37891109..52d962fbac 100644 --- a/codex-rs/core/src/tools/sandboxing.rs +++ b/codex-rs/core/src/tools/sandboxing.rs @@ -542,6 +542,7 @@ impl<'a> SandboxAttempt<'a> { let exec_server_permissions = effective_permission_profile(self.exec_server_permissions, additional_permissions); let request = SandboxExecRequest { + native_program: None, command: argv, cwd, env, diff --git a/codex-rs/core/src/unified_exec/mod_tests.rs b/codex-rs/core/src/unified_exec/mod_tests.rs index 78099ddbec..02e7d9576e 100644 --- a/codex-rs/core/src/unified_exec/mod_tests.rs +++ b/codex-rs/core/src/unified_exec/mod_tests.rs @@ -790,17 +790,57 @@ async fn terminating_during_stdin_poll_returns_exited_response( Ok(()) } +#[test_case::test_case(false; "pipe")] +#[cfg_attr(target_os = "linux", test_case::test_case(true; "PTY with non-UTF-8 sandbox launcher"))] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn completed_pipe_commands_preserve_exit_code() -> anyhow::Result<()> { +async fn completed_local_commands_preserve_exit_code(tty: bool) -> anyhow::Result<()> { let (_, turn) = make_session_and_context().await; #[allow(deprecated)] let cwd = turn.cwd.clone(); let request = test_exec_request( &turn, vec!["bash".to_string(), "-lc".to_string(), "exit 17".to_string()], - cwd, + cwd.clone(), shell_env(), ); + #[cfg(target_os = "linux")] + let dir = tempfile::tempdir()?; + #[cfg(target_os = "linux")] + let request = if tty { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::PermissionsExt; + // The probe stands in for the configured helper. The lossy spelling names a different executable. + let helper = dir.path().join(OsStr::from_bytes(b"helper-\xff")); + std::fs::write(&helper, "#!/bin/sh\nexit 17\n")?; + std::fs::set_permissions(&helper, std::fs::Permissions::from_mode(0o700))?; + std::os::unix::fs::symlink("/bin/false", helper.to_string_lossy().as_ref())?; + crate::exec::build_exec_request( + crate::exec::ExecParams { + command: vec!["/bin/false".to_owned()], + cwd: cwd.clone(), + expiration: request.expiration, + capture_policy: request.capture_policy, + env: request.env, + network: None, + network_environment_id: None, + sandbox_permissions: SandboxPermissions::UseDefault, + windows_sandbox_level: turn.windows_sandbox_level, + justification: None, + arg0: None, + }, + &codex_protocol::models::PermissionProfile::read_only(), + &cwd, + &[], + &Some(helper), + /*codex_self_exe*/ &None, + SandboxType::None, + /*use_legacy_landlock*/ false, + ) + .await? + } else { + request + }; let environment = codex_exec_server::Environment::default_for_tests(); let process = UnifiedExecProcessManager::default() @@ -810,7 +850,7 @@ async fn completed_pipe_commands_preserve_exit_code() -> anyhow::Result<()> { /*tool_ctx*/ None, codex_sandboxing::WindowsSandboxProxySettingsMode::Reconcile, /*network_policy_decider*/ None, - /*tty*/ false, + tty, &environment, ) .await?; diff --git a/codex-rs/core/src/unified_exec/process_manager.rs b/codex-rs/core/src/unified_exec/process_manager.rs index 48fbd2c13d..9da4ede137 100644 --- a/codex-rs/core/src/unified_exec/process_manager.rs +++ b/codex-rs/core/src/unified_exec/process_manager.rs @@ -1415,9 +1415,9 @@ impl UnifiedExecProcessManager { path: request.cwd.clone(), })?; - if request.command.is_empty() { - return Err(UnifiedExecError::MissingCommandLine); - } + let (program, args) = request + .program_and_args() + .ok_or(UnifiedExecError::MissingCommandLine)?; let network_proxy_restricting_sid = { #[cfg(target_os = "windows")] { @@ -1462,7 +1462,8 @@ impl UnifiedExecProcessManager { None }; let spawn_result = codex_sandboxing::spawn_process(codex_sandboxing::SpawnRequest { - command: &request.command, + program, + args, cwd: native_cwd.as_path(), env: &request.env, arg0: &request.arg0, diff --git a/codex-rs/core/src/unified_exec/process_manager_tests.rs b/codex-rs/core/src/unified_exec/process_manager_tests.rs index b1cec18525..a7911ed49b 100644 --- a/codex-rs/core/src/unified_exec/process_manager_tests.rs +++ b/codex-rs/core/src/unified_exec/process_manager_tests.rs @@ -189,6 +189,7 @@ fn exec_server_params_use_path_uri_and_env_policy_overlay_contract() { ..Default::default() }; let mut request = ExecRequest { + native_program: None, command: vec!["bash".to_string(), "-lc".to_string(), "true".to_string()], cwd: cwd.clone().into(), env: HashMap::from([ diff --git a/codex-rs/exec-server/src/fs_sandbox.rs b/codex-rs/exec-server/src/fs_sandbox.rs index 1035220e64..a4411fbcdf 100644 --- a/codex-rs/exec-server/src/fs_sandbox.rs +++ b/codex-rs/exec-server/src/fs_sandbox.rs @@ -325,29 +325,22 @@ pub(crate) async fn wait_for_helper_output( } pub(crate) fn spawn_command( - SandboxExecRequest { - command: argv, - cwd, - mut env, - arg0, - .. - }: SandboxExecRequest, + request: SandboxExecRequest, stdin: ChildStdin, ) -> Result { - let Some((program, args)) = argv.split_first() else { + let Some((program, args)) = request.program_and_args() else { return Err(invalid_request("fs sandbox command was empty".to_string())); }; let mut command = Command::new(program); + command.args(args); #[cfg(unix)] - if let Some(arg0) = arg0 { + if let Some(arg0) = request.arg0 { command.arg0(arg0); } - #[cfg(not(unix))] - let _ = arg0; - command.args(args); // TODO(anp): Keep PathUri through the filesystem helper launch boundary. - let cwd = cwd.to_abs_path().map_err(io_error)?; + let cwd = request.cwd.to_abs_path().map_err(io_error)?; command.current_dir(cwd.as_path()); + let mut env = request.env; env.retain(|name, _| !codex_protocol::shell_environment::is_non_inheritable_env_var(name)); command.envs(env); command.stdin(stdin); diff --git a/codex-rs/exec-server/src/fs_sandbox_windows_tests.rs b/codex-rs/exec-server/src/fs_sandbox_windows_tests.rs index d21e72cf32..9bcaa8311c 100644 --- a/codex-rs/exec-server/src/fs_sandbox_windows_tests.rs +++ b/codex-rs/exec-server/src/fs_sandbox_windows_tests.rs @@ -235,6 +235,7 @@ fn powershell_command(script: &str, path: &Path) -> anyhow::Result Some(ProcessSandboxType::None), SandboxType::MacosSeatbelt => Some(ProcessSandboxType::MacosSeatbelt), @@ -433,7 +433,8 @@ impl LocalProcess { #[cfg(not(unix))] let inherited_fds = Vec::new(); let spawned_result = codex_sandboxing::spawn_process(codex_sandboxing::SpawnRequest { - command: &prepared.command, + program, + args, cwd: prepared.cwd.as_path(), env: &prepared.env, arg0: &prepared.arg0, diff --git a/codex-rs/exec-server/src/process_sandbox.rs b/codex-rs/exec-server/src/process_sandbox.rs index 5f5f30f52b..9e9e4bd1ce 100644 --- a/codex-rs/exec-server/src/process_sandbox.rs +++ b/codex-rs/exec-server/src/process_sandbox.rs @@ -1,4 +1,6 @@ use std::collections::HashMap; +use std::ffi::OsStr; +use std::ffi::OsString; use std::path::Path; use std::sync::Arc; @@ -46,6 +48,7 @@ const CREDENTIAL_MASKING_FAILURE_MODE: CredentialMaskingFailureMode = pub(crate) struct PreparedExecRequest { pub(crate) command: Vec, + native_program: Option, pub(crate) cwd: AbsolutePathBuf, pub(crate) env: HashMap, pub(crate) arg0: Option, @@ -65,6 +68,16 @@ struct PreparedWindowsSandboxRequest { } impl PreparedExecRequest { + pub(crate) fn program_and_args(&self) -> Option<(&OsStr, &[String])> { + let (program, args) = self.command.split_first()?; + Some(( + self.native_program + .as_deref() + .unwrap_or_else(|| OsStr::new(program)), + args, + )) + } + #[cfg(unix)] pub(crate) fn credential_snapshot_proxy(&self, params: &ExecParams) -> Option<&NetworkProxy> { let launch = params.network_proxy.as_ref()?; @@ -151,6 +164,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry( let Some(sandbox_context) = params.sandbox.as_ref() else { return Ok(PreparedExecRequest { command: params.argv.clone(), + native_program: None, cwd: native_path(¶ms.cwd, "cwd")?, env, arg0: params.arg0.clone(), @@ -300,6 +314,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry( }; Ok(PreparedExecRequest { command: request.command, + native_program: request.native_program, cwd: native_path(&request.cwd, "cwd")?, env: request.env, arg0: request.arg0, diff --git a/codex-rs/exec-server/src/sandbox_integrity/backend_tests.rs b/codex-rs/exec-server/src/sandbox_integrity/backend_tests.rs index f7156be645..395959d4d0 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/backend_tests.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/backend_tests.rs @@ -51,6 +51,7 @@ pub(super) fn sandbox_request( policy: &FileSystemSandboxPolicy, ) -> SandboxExecRequest { SandboxExecRequest { + native_program: None, sandbox_override: SandboxOverride::NoOverride, command: vec![cwd.join("not-executed").to_string_lossy().into_owned()], cwd: PathUri::from_abs_path(cwd), diff --git a/codex-rs/exec-server/src/sandbox_integrity/bubblewrap.rs b/codex-rs/exec-server/src/sandbox_integrity/bubblewrap.rs index 13c3396146..2600d7aee9 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/bubblewrap.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/bubblewrap.rs @@ -34,7 +34,7 @@ pub(super) fn critical_dependencies( .transpose() .map_err(io::Error::other)?; let mut dependencies = codex_and_launcher_dependencies(request); - if let Some(exe) = request.command.first() + if let Some((exe, _)) = request.program_and_args() && let Some(bwrap) = codex_linux_sandbox::find_bundled_bwrap_for_exe( &command_cwd.as_path().join(exe), command_cwd.as_path(), diff --git a/codex-rs/exec-server/src/sandbox_integrity/dependencies.rs b/codex-rs/exec-server/src/sandbox_integrity/dependencies.rs index c07ab51f52..8dc976dbce 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/dependencies.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/dependencies.rs @@ -33,7 +33,7 @@ pub(super) fn codex_and_launcher_dependencies( request: &codex_sandboxing::SandboxExecRequest, ) -> Vec { let mut dependencies = vec![codex_executable()]; - if let Some(exe) = request.command.first() { + if let Some((exe, _)) = request.program_and_args() { dependencies.push(("sandbox_launcher", Ok(PathBuf::from(exe)))); } dependencies diff --git a/codex-rs/exec-server/src/shell_snapshot.rs b/codex-rs/exec-server/src/shell_snapshot.rs index 450dea62ef..a4392576a4 100644 --- a/codex-rs/exec-server/src/shell_snapshot.rs +++ b/codex-rs/exec-server/src/shell_snapshot.rs @@ -448,9 +448,7 @@ done format!("printf '\\0%s\\0%s\\0' '{SOURCE_MARKER}' 0\n{script}") }; let shell_start = prepared.command.len() - params.argv.len(); - let mut argv = prepared.command.clone(); - argv[shell_start + 2] = script; - let (program, args) = argv.split_first().ok_or_else(|| { + let (program, args) = prepared.program_and_args().ok_or_else(|| { ( "missing_command", internal_error("missing shell snapshot command".to_string()), @@ -459,7 +457,9 @@ done let mut command = Command::new(program); command - .args(args) + .args(&args[..shell_start + 1]) + .arg(script) + .args(&args[shell_start + 2..]) .current_dir(prepared.cwd.as_path()) .env_clear() .envs(&prepared.env) diff --git a/codex-rs/sandboxing/src/launch.rs b/codex-rs/sandboxing/src/launch.rs index 1725a9ec6f..b1b4b3f3e9 100644 --- a/codex-rs/sandboxing/src/launch.rs +++ b/codex-rs/sandboxing/src/launch.rs @@ -2,8 +2,6 @@ use crate::SandboxExecRequest; use crate::SandboxType; -use std::ffi::OsStr; -use std::ffi::OsString; use std::io; use std::process::Command; @@ -29,12 +27,10 @@ use codex_utils_path_uri::PathUri; #[must_use] pub struct WrappedSandboxCommand { pub(super) request: SandboxExecRequest, - // UTF-8 argv cannot represent every native executable path. - pub(super) native_program: Option, } impl WrappedSandboxCommand { - /// Return the request with UTF-8 argv for callers that spawn the command themselves. + /// Return the request for a caller that supplies its own process launcher. pub fn into_command_request(self) -> SandboxExecRequest { self.request } @@ -48,16 +44,12 @@ impl WrappedSandboxCommand { "a native Windows sandbox requires its session launcher", )); } - let (program, args) = request.command.split_first().ok_or_else(|| { + let (program, args) = request.program_and_args().ok_or_else(|| { io::Error::new(io::ErrorKind::InvalidInput, "sandbox command is empty") })?; - let mut command = Command::new( - self.native_program - .as_deref() - .unwrap_or_else(|| OsStr::new(program)), - ); + let mut command = Command::new(program); #[cfg(unix)] - if let Some(arg0) = request.arg0 { + if let Some(arg0) = &request.arg0 { use std::os::unix::process::CommandExt; command.arg0(arg0); } diff --git a/codex-rs/sandboxing/src/manager.rs b/codex-rs/sandboxing/src/manager.rs index b95a232328..4806ca5f8f 100644 --- a/codex-rs/sandboxing/src/manager.rs +++ b/codex-rs/sandboxing/src/manager.rs @@ -102,6 +102,8 @@ pub struct SandboxExecRequest { /// Controller-selected override, retained for observations without altering the sandbox command. pub sandbox_override: SandboxOverride, pub command: Vec, + /// Exact host executable when its native path cannot be represented in `command`. + pub native_program: Option, pub cwd: PathUri, pub sandbox_policy_cwd: PathUri, pub env: HashMap, @@ -115,6 +117,19 @@ pub struct SandboxExecRequest { pub arg0: Option, } +impl SandboxExecRequest { + /// Borrow the executable and arguments to pass to a host process launcher. + pub fn program_and_args(&self) -> Option<(&OsStr, &[String])> { + let (program, args) = self.command.split_first()?; + Some(( + self.native_program + .as_deref() + .unwrap_or_else(|| OsStr::new(program)), + args, + )) + } +} + /// How the execution host selects a backend for this launch. #[derive(Clone, Copy)] pub enum SandboxSelection { @@ -440,10 +455,6 @@ impl SandboxManager { } .filter(|path| path.to_str().is_none()) .map(OsStr::to_owned); - let wrapped = |request| WrappedSandboxCommand { - request, - native_program, - }; let managed_network = command .managed_network .filter(|_| request.enforce_managed_network || backend == SandboxType::MacosSeatbelt); @@ -456,6 +467,7 @@ impl SandboxManager { let mut exec = SandboxExecRequest { sandbox_override: SandboxOverride::NoOverride, command: argv, + native_program, cwd: command.cwd, sandbox_policy_cwd: request.sandbox_policy_cwd.clone(), env: command.env, @@ -467,7 +479,7 @@ impl SandboxManager { arg0: None, }; if backend == SandboxType::None { - return Ok(wrapped(exec)); + return Ok(WrappedSandboxCommand { request: exec }); } let pending = PendingSandboxedExecRequest::new( &exec.cwd, @@ -638,7 +650,7 @@ impl SandboxManager { } } } - Ok(wrapped(exec)) + Ok(WrappedSandboxCommand { request: exec }) } fn wrap_linux( diff --git a/codex-rs/sandboxing/src/manager_tests.rs b/codex-rs/sandboxing/src/manager_tests.rs index 64dd5a4a7c..a52d1f01cf 100644 --- a/codex-rs/sandboxing/src/manager_tests.rs +++ b/codex-rs/sandboxing/src/manager_tests.rs @@ -557,8 +557,8 @@ fn transform_linux_seccomp_uses_helper_alias_when_launcher_is_not_helper_path() } #[cfg(unix)] -#[test] -fn inherited_terminal_preserves_native_linux_helper_and_uses_its_dispatch_name() +#[tokio::test] +async fn inherited_terminal_preserves_native_linux_helper_and_uses_its_dispatch_name() -> anyhow::Result<()> { use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; @@ -581,12 +581,43 @@ fn inherited_terminal_preserves_native_linux_helper_and_uses_its_dispatch_name() let mut wrapped = transform_linux_seccomp_request(&helper, SandboxManager::for_inherited_terminal(&[])); // Run a shell probe with the executable and argv[0] produced by the Linux backend. + let probe = ["-c", r#"printf '%s' "$0""#]; wrapped.request.command.truncate(1); let mut child = wrapped.into_direct_command()?; assert_eq!(child.get_program(), helper.as_os_str()); - let output = child.args(["-c", r#"printf '%s' "$0""#]).output()?; + let output = child.args(probe).output()?; assert!(output.status.success(), "{output:?}"); assert_eq!(output.stdout, expected_arg0.as_bytes()); + + let mut wrapped = transform_linux_seccomp_request(&helper, SandboxManager::new()); + wrapped + .request + .command + .splice(1.., probe.map(str::to_owned)); + let request = wrapped.into_command_request(); + let (program, args) = request.program_and_args().expect("prepared command"); + let cwd = request.cwd.to_abs_path()?; + let mut child = crate::spawn_process(crate::SpawnRequest { + program, + args, + cwd: cwd.as_path(), + env: &request.env, + arg0: &request.arg0, + sandbox: request.sandbox, + windows_sandbox: None, + tty: true, + stdin_open: true, + inherited_fds: codex_utils_pty::ChildFds::Inherited(&[]), + }) + .await?; + let mut output = Vec::new(); + while let Some(bytes) = child.stdout_rx.recv().await { + output.extend(bytes); + } + assert_eq!( + (child.exit_rx.await?, output), + (0, expected_arg0.as_bytes().to_vec()) + ); } Ok(()) } @@ -838,6 +869,7 @@ fn windows_file_system_wrapper_materializes_inner_helper() { ) .expect("resolve helper policy"); let mut exec_request = super::SandboxExecRequest { + native_program: None, sandbox_override: super::SandboxOverride::NoOverride, command: vec![ configured_helper.display().to_string(), diff --git a/codex-rs/sandboxing/src/spawn.rs b/codex-rs/sandboxing/src/spawn.rs index 86d2878fbd..1a76ca708c 100644 --- a/codex-rs/sandboxing/src/spawn.rs +++ b/codex-rs/sandboxing/src/spawn.rs @@ -1,6 +1,8 @@ use std::collections::HashMap; +use std::ffi::OsStr; use std::path::Path; +#[cfg(windows)] use anyhow::Context; use anyhow::Result; use codex_protocol::config_types::WindowsSandboxLevel; @@ -29,7 +31,8 @@ pub struct WindowsSandboxSpawnRequest<'a> { /// Executor-native process launch request shared by local and exec-server execution. pub struct SpawnRequest<'a> { - pub command: &'a [String], + pub program: &'a OsStr, + pub args: &'a [String], pub cwd: &'a Path, pub env: &'a HashMap, pub arg0: &'a Option, @@ -68,7 +71,15 @@ pub async fn spawn_process(request: SpawnRequest<'_>) -> Result permission_profile: windows.permission_profile, workspace_roots: windows.workspace_roots, codex_home: codex_home.as_path(), - command: request.command.to_vec(), + command: std::iter::once( + request + .program + .to_str() + .context("Windows sandbox program path is not UTF-8")? + .to_owned(), + ) + .chain(request.args.iter().cloned()) + .collect(), cwd: request.cwd, env_map: request.env.clone(), windows_sandbox_level: windows.windows_sandbox_level, @@ -102,14 +113,10 @@ pub async fn spawn_process(request: SpawnRequest<'_>) -> Result anyhow::bail!("Windows sandbox process spawn is unavailable on this platform"); } - let (program, args) = request - .command - .split_first() - .context("missing program for process spawn")?; let spawned = if tty { codex_utils_pty::pty::spawn_process( - program, - args, + request.program, + request.args, request.cwd, request.env, request.arg0, @@ -119,8 +126,8 @@ pub async fn spawn_process(request: SpawnRequest<'_>) -> Result .await } else if request.stdin_open { codex_utils_pty::pipe::spawn_process( - program, - args, + request.program, + request.args, request.cwd, request.env, request.arg0, @@ -129,8 +136,8 @@ pub async fn spawn_process(request: SpawnRequest<'_>) -> Result .await } else { codex_utils_pty::pipe::spawn_process_no_stdin( - program, - args, + request.program, + request.args, request.cwd, request.env, request.arg0, diff --git a/codex-rs/sandboxing/src/terminal_queries_tests.rs b/codex-rs/sandboxing/src/terminal_queries_tests.rs index caf61ea1a2..ac57fb527d 100644 --- a/codex-rs/sandboxing/src/terminal_queries_tests.rs +++ b/codex-rs/sandboxing/src/terminal_queries_tests.rs @@ -68,10 +68,10 @@ async fn driver_backed_terminal_queries_are_answered() -> anyhow::Result<()> { async fn direct_terminal_queries_are_answered() -> anyhow::Result<()> { let script = "stty -echo -icanon; printf 'alpha\\033[6n'; dd bs=1 count=6 2>/dev/null; printf '\\nok'"; - let command = vec!["/bin/sh".to_string(), "-c".to_string(), script.to_string()]; let env = std::env::vars().collect(); let spawned = spawn_process(SpawnRequest { - command: &command, + program: "/bin/sh".as_ref(), + args: &["-c".to_string(), script.to_string()], cwd: Path::new("."), env: &env, arg0: &None, diff --git a/codex-rs/utils/pty/Cargo.toml b/codex-rs/utils/pty/Cargo.toml index f7cabb6975..de7e1d6a68 100644 --- a/codex-rs/utils/pty/Cargo.toml +++ b/codex-rs/utils/pty/Cargo.toml @@ -38,6 +38,7 @@ winapi = { version = "0.3.9", features = [ ] } [target.'cfg(unix)'.dependencies] libc = { workspace = true } +nix = { version = "0.28", features = ["fs", "user"] } [lib] doctest = false diff --git a/codex-rs/utils/pty/src/pty.rs b/codex-rs/utils/pty/src/pty.rs index 7bee13adee..8b46fdd35c 100644 --- a/codex-rs/utils/pty/src/pty.rs +++ b/codex-rs/utils/pty/src/pty.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::ffi::OsStr; #[cfg(unix)] use std::fs::File; use std::io::ErrorKind; @@ -11,6 +12,8 @@ use std::os::fd::RawFd; #[cfg(all(unix, not(target_os = "linux")))] use std::os::unix::process::CommandExt; use std::path::Path; +#[cfg(unix)] +use std::path::PathBuf; #[cfg(all(unix, not(target_os = "linux")))] use std::process::Command as StdCommand; #[cfg(all(unix, not(target_os = "linux")))] @@ -145,7 +148,7 @@ fn platform_native_pty_system() -> Box { /// Spawn a process attached to a PTY, preserving selected inherited file /// descriptors across exec on Unix. pub async fn spawn_process( - program: &str, + program: impl AsRef, args: &[String], cwd: &Path, env: &HashMap, @@ -153,12 +156,19 @@ pub async fn spawn_process( size: TerminalSize, inherited_fds: ChildFds<'_>, ) -> Result { + let program = program.as_ref(); if program.is_empty() { anyhow::bail!("missing program for PTY spawn"); } #[cfg(not(unix))] - let _ = inherited_fds; + let program = { + let _ = inherited_fds; + arg0.as_deref().map(OsStr::new).unwrap_or(program) + }; + + #[cfg(unix)] + let arg0 = arg0.as_deref().filter(|arg0| OsStr::new(arg0) != program); #[cfg(target_os = "linux")] if crate::spawn_helper::is_available() @@ -167,26 +177,26 @@ pub async fn spawn_process( && arg0.is_none() && cwd.is_dir() { - // Keep portable-pty's PATH, argv0, and missing-cwd behavior for requests - // whose launch settings require its command builder. + // Keep portable-pty's PATH and missing-cwd behavior for ordinary launches. return spawn_process_preserving_fds(program, args, cwd, env, arg0, size, inherited_fds) .await; } #[cfg(unix)] - if !inherited_fds.as_slice().is_empty() { + if arg0.is_some() || !inherited_fds.as_slice().is_empty() { + // portable-pty resolves argv[0] as the executable. Only a native spawn + // can pass a dispatch name while executing the configured program. return spawn_process_preserving_fds(program, args, cwd, env, arg0, size, inherited_fds) .await; } - spawn_process_portable(program, args, cwd, env, arg0, size).await + spawn_process_portable(program, args, cwd, env, size).await } async fn spawn_process_portable( - program: &str, + program: &OsStr, args: &[String], cwd: &Path, env: &HashMap, - arg0: &Option, size: TerminalSize, ) -> Result { let pty_system = platform_native_pty_system(); @@ -198,7 +208,7 @@ async fn spawn_process_portable( .ok_or_else(|| anyhow::anyhow!("PTY master has no file descriptor"))?, )?; - let mut command_builder = CommandBuilder::new(arg0.as_ref().unwrap_or(&program.to_string())); + let mut command_builder = CommandBuilder::new(program); command_builder.cwd(cwd); command_builder.env_clear(); for arg in args { @@ -318,16 +328,67 @@ async fn spawn_process_portable( }) } +#[cfg(unix)] +fn portable_launch_paths( + program: &OsStr, + cwd: &Path, + env: &HashMap, +) -> Result<(PathBuf, PathBuf)> { + use anyhow::Context; + use nix::unistd::AccessFlags; + use nix::unistd::User; + use nix::unistd::access; + use nix::unistd::getuid; + use std::path::Component; + + let cwd = if cwd.is_dir() { + cwd.to_owned() + } else if let Some(home) = env.get("HOME") { + PathBuf::from(home) + } else if let Ok(Some(user)) = User::from_uid(getuid()) { + PathBuf::from(user.dir.to_str().context("failed to resolve home dir")?) + } else { + PathBuf::from("/") + }; + let program = Path::new(program); + // portable-pty searches even names containing slashes unless they start with /, ./, or ../. + let program = if matches!(program.components().next(), Some(Component::Normal(_))) { + let path = env.get("PATH").context("Unable to resolve the PATH")?; + let base = std::path::absolute(&cwd)?; + std::env::split_paths(path) + .map(|dir| base.join(dir).join(program)) + .find(|candidate| !candidate.is_dir() && access(candidate, AccessFlags::X_OK).is_ok()) + .with_context(|| { + format!( + "Unable to find executable {} in PTY PATH", + program.display() + ) + })? + } else { + program.to_owned() + }; + Ok((program, cwd)) +} + #[cfg(unix)] async fn spawn_process_preserving_fds( - program: &str, + program: &OsStr, args: &[String], cwd: &Path, env: &HashMap, - arg0: &Option, + arg0: Option<&str>, size: TerminalSize, descriptors: ChildFds<'_>, ) -> Result { + // A distinct argv[0] needs a native spawn. Keep the lookup behavior that + // portable-pty supplies for launches without extra descriptors. + let resolved; + let (program, cwd) = if arg0.is_some() && descriptors.as_slice().is_empty() { + resolved = portable_launch_paths(program, cwd, env)?; + (resolved.0.as_os_str(), resolved.1.as_path()) + } else { + (program, cwd) + }; let (master, slave) = open_unix_pty(size)?; let io = crate::unix_io::PtyIo::new(master.as_raw_fd())?; let uses_portable_status = match descriptors { @@ -367,17 +428,16 @@ async fn spawn_process_preserving_fds( command.stderr_file = Some(slave.try_clone()?.into()); match crate::spawn_helper::spawn(&command, crate::spawn_helper::Setup::Pty).await? { Some(child) => child, - None if inherited_fds.is_empty() => { + None if inherited_fds.is_empty() && arg0.is_none() => { // Free the first PTY before allocating its portable replacement. drop(command); drop(io); drop(slave); drop(master); - return spawn_process_portable(program, args, cwd, env, arg0, size).await; + return spawn_process_portable(program, args, cwd, env, size).await; } None => { - // Explicit descriptors select the shared fallback on Linux. Keep - // terminal setup there when this executable cannot run the helper. + // Keep both the descriptor policy and argv[0] when the helper is unavailable. unsafe { command.inner.pre_exec(configure_child_terminal); } diff --git a/codex-rs/utils/pty/src/spawn_helper_tests.rs b/codex-rs/utils/pty/src/spawn_helper_tests.rs index e858831ae8..c35e6b1894 100644 --- a/codex-rs/utils/pty/src/spawn_helper_tests.rs +++ b/codex-rs/utils/pty/src/spawn_helper_tests.rs @@ -575,11 +575,11 @@ async fn pty_helper_establishes_a_controlling_terminal_without_forking() -> anyh "/bin/sh", &[ "-c".to_owned(), - "test -t 0 && test -t 1 && test -t 2 && printf ready >/dev/tty".to_owned(), + r#"test -t 0 && test -t 1 && test -t 2 && printf '%s' "$0" >/dev/tty"#.to_owned(), ], std::path::Path::new("."), &std::env::vars().collect(), - /*arg0*/ &None, + &Some("pty-dispatch".to_owned()), crate::TerminalSize::default(), crate::ChildFds::Inherited(&[]), ) @@ -588,7 +588,10 @@ async fn pty_helper_establishes_a_controlling_terminal_without_forking() -> anyh while let Some(chunk) = spawned.stdout_rx.recv().await { output.extend(chunk); } - assert_eq!((spawned.exit_rx.await?, output), (0, b"ready".to_vec())); + assert_eq!( + (spawned.exit_rx.await?, output), + (0, b"pty-dispatch".to_vec()) + ); if native_helper_available().await? { assert_eq!(FORKS.with(Cell::get), 0); } diff --git a/codex-rs/utils/pty/src/tests.rs b/codex-rs/utils/pty/src/tests.rs index 20cf4d5b26..d34ad0a673 100644 --- a/codex-rs/utils/pty/src/tests.rs +++ b/codex-rs/utils/pty/src/tests.rs @@ -1275,6 +1275,53 @@ fn pty_terminate_reaps_child_when_waiter_is_queued() -> anyhow::Result<()> { }) } +#[cfg(unix)] +#[tokio::test] +async fn pty_with_dispatch_name_uses_child_home_and_path() -> anyhow::Result<()> { + let home = tempfile::tempdir()?; + std::fs::create_dir_all(home.path().join("bin/tools"))?; + std::os::unix::fs::symlink("/bin/sh", home.path().join("bin/tools/sh"))?; + let missing_cwd = home.path().join("missing"); + let env = HashMap::from([( + "HOME".to_owned(), + home.path().to_str().expect("temp path").to_owned(), + )]); + let mut search_env = env.clone(); + search_env.insert("PATH".to_owned(), "bin".to_owned()); + let arg0 = Some("pty-dispatch".to_owned()); + let args = [ + "-c".to_owned(), + r#"test -x bin/tools/sh && printf '%s' "$0""#.to_owned(), + ]; + let spawn = |program, env| { + spawn_pty_process( + program, + &args, + &missing_cwd, + env, + &arg0, + TerminalSize::default(), + crate::ChildFds::Inherited(&[]), + ) + }; + assert!( + spawn("sh", &env).await.is_err(), + "PTY searched outside its environment" + ); + for (program, env) in [("./bin/tools/sh", &env), ("tools/sh", &search_env)] { + let mut child = spawn(program, env).await?; + let mut output = Vec::new(); + while let Some(chunk) = child.stdout_rx.recv().await { + output.extend(chunk); + } + assert_eq!( + (child.exit_rx.await?, output), + (0, b"pty-dispatch".to_vec()) + ); + } + Ok(()) +} + #[cfg(unix)] #[tokio::test] async fn pty_attachments_preserve_shell_environment_and_signal_status() -> anyhow::Result<()> {