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
This commit is contained in:
viyatb-oai
2026-11-16 23:26:42 +00:00
committed by copyberry
parent cff061fca9
commit c9df641eac
30 changed files with 346 additions and 108 deletions
+1
View File
@@ -5639,6 +5639,7 @@ dependencies = [
"lazy_static",
"libc",
"log",
"nix 0.28.0",
"portable-pty",
"pretty_assertions",
"shared_library",
+12 -19
View File
@@ -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;
+1 -1
View File
@@ -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,
+2 -2
View File
@@ -81,7 +81,7 @@ async fn rejected_start(
expected: &str,
) -> anyhow::Result<String> {
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,
+14 -2
View File
@@ -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<ExecToolCallOutput> {
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<OsString>,
network_sandbox_policy: NetworkSandboxPolicy,
stdout_stream: Option<StdoutStream>,
after_spawn: Option<Box<dyn FnOnce() + Send>>,
@@ -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<OsString>,
network_sandbox_policy: NetworkSandboxPolicy,
stdout_stream: Option<StdoutStream>,
after_spawn: Option<Box<dyn FnOnce() + Send>>,
@@ -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,
+4
View File
@@ -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,
+17
View File
@@ -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<String>,
pub(crate) native_program: Option<OsString>,
pub cwd: PathUri,
pub env: HashMap<String, String>,
pub(crate) exec_server_env_config: Option<ExecServerEnvConfig>,
@@ -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<String>,
@@ -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,
+3 -1
View File
@@ -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 {
+1
View File
@@ -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,
+1
View File
@@ -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,
+43 -3
View File
@@ -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?;
@@ -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,
@@ -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([
+6 -13
View File
@@ -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<Child, JSONRPCErrorError> {
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);
@@ -235,6 +235,7 @@ fn powershell_command(script: &str, path: &Path) -> anyhow::Result<SandboxExecRe
let cwd = PathUri::from_host_native_path(std::env::current_dir()?)?;
Ok(SandboxExecRequest {
native_program: None,
sandbox_override: SandboxOverride::NoOverride,
command: vec![
powershell.to_string_lossy().into_owned(),
+5 -4
View File
@@ -400,9 +400,9 @@ impl LocalProcess {
&self.inner.credential_brokers,
)
.await?;
if prepared.command.is_empty() {
return Err(invalid_params("argv must not be empty".to_string()));
}
let (program, args) = prepared
.program_and_args()
.ok_or_else(|| invalid_params("argv must not be empty".to_string()))?;
let sandbox_type = match prepared.sandbox {
SandboxType::None => 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,
@@ -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<String>,
native_program: Option<OsString>,
pub(crate) cwd: AbsolutePathBuf,
pub(crate) env: HashMap<String, String>,
pub(crate) arg0: Option<String>,
@@ -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(&params.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,
@@ -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),
@@ -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(),
@@ -33,7 +33,7 @@ pub(super) fn codex_and_launcher_dependencies(
request: &codex_sandboxing::SandboxExecRequest,
) -> Vec<Dependency> {
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
+4 -4
View File
@@ -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)
+4 -12
View File
@@ -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<OsString>,
}
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);
}
+18 -6
View File
@@ -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<String>,
/// Exact host executable when its native path cannot be represented in `command`.
pub native_program: Option<OsString>,
pub cwd: PathUri,
pub sandbox_policy_cwd: PathUri,
pub env: HashMap<String, String>,
@@ -115,6 +117,19 @@ pub struct SandboxExecRequest {
pub arg0: Option<String>,
}
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(
+35 -3
View File
@@ -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(),
+19 -12
View File
@@ -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<String, String>,
pub arg0: &'a Option<String>,
@@ -68,7 +71,15 @@ pub async fn spawn_process(request: SpawnRequest<'_>) -> Result<SpawnedProcess>
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<SpawnedProcess>
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<SpawnedProcess>
.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<SpawnedProcess>
.await
} else {
codex_utils_pty::pipe::spawn_process_no_stdin(
program,
args,
request.program,
request.args,
request.cwd,
request.env,
request.arg0,
@@ -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,
+1
View File
@@ -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
+75 -15
View File
@@ -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<dyn portable_pty::PtySystem + Send> {
/// 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<OsStr>,
args: &[String],
cwd: &Path,
env: &HashMap<String, String>,
@@ -153,12 +156,19 @@ pub async fn spawn_process(
size: TerminalSize,
inherited_fds: ChildFds<'_>,
) -> Result<SpawnedProcess> {
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<String, String>,
arg0: &Option<String>,
size: TerminalSize,
) -> Result<SpawnedProcess> {
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<String, String>,
) -> 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<String, String>,
arg0: &Option<String>,
arg0: Option<&str>,
size: TerminalSize,
descriptors: ChildFds<'_>,
) -> Result<SpawnedProcess> {
// 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);
}
+6 -3
View File
@@ -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);
}
+47
View File
@@ -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<()> {