mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<()> {
|
||||
|
||||
Reference in New Issue
Block a user