mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Fix relative TMPDIR resolution in the Linux filesystem sandbox (#64976)
## Why When `TMPDIR` is relative, `environment/info` resolves it against the executor's working directory. The filesystem helper starts at the filesystem root, so its `:tmpdir` rules could apply to a different directory. ## What changed Use the executor's resolved temporary directory when binding `:tmpdir` rules. Reject requests whose supplied temporary directories no longer match the executor, and preserve the helper's original `TMPDIR` for scratch files. ## Testing Add a Linux integration test covering `:tmpdir` read grants and denials, rejection of stale temporary directory information after the executor's working directory moves, and a write without a `:tmpdir` rule. GitOrigin-RevId: 6772c17251500dae3f6b0a2b17ee95955289e89b
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
#[cfg(any(windows, test))]
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_exec_server_protocol::JSONRPCErrorError;
|
||||
use codex_protocol::permissions::FileSystemPath;
|
||||
use codex_protocol::permissions::FileSystemSpecialPath;
|
||||
use codex_sandboxing::SandboxCommand;
|
||||
use codex_sandboxing::SandboxExecRequest;
|
||||
use codex_sandboxing::SandboxExecutables;
|
||||
@@ -106,11 +109,47 @@ impl FileSystemSandboxRunner {
|
||||
.ancestors()
|
||||
.last()
|
||||
.ok_or_else(|| invalid_request("filesystem sandbox cwd has no root".to_string()))?;
|
||||
// The Linux binder freezes :tmpdir into the policy before the helper starts at /.
|
||||
// Resolve against the executor's current cwd, just as environment/info does.
|
||||
let tmpdir_rule = FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::Tmpdir,
|
||||
};
|
||||
let mut policy_env = self.helper_env.clone();
|
||||
let original_tmpdir = if cfg!(target_os = "linux")
|
||||
&& let Some(tmpdir) = policy_env.get_mut("TMPDIR")
|
||||
&& !tmpdir.is_empty()
|
||||
&& Path::new(tmpdir).is_relative()
|
||||
&& sandbox
|
||||
.permissions
|
||||
.file_system_sandbox_policy()
|
||||
.entries
|
||||
.iter()
|
||||
.any(|entry| entry.path == tmpdir_rule)
|
||||
{
|
||||
let directories = crate::EnvironmentInfo::local_temporary_directories();
|
||||
if let Some(expected) = &sandbox.temporary_directories
|
||||
&& *expected != directories
|
||||
{
|
||||
return Err(invalid_request(
|
||||
"temporary directories do not match the executor".into(),
|
||||
));
|
||||
}
|
||||
let resolved = directories
|
||||
.first()
|
||||
.and_then(|uri| uri.to_abs_path().ok())
|
||||
.and_then(|path| path.as_path().to_str().map(str::to_owned))
|
||||
.ok_or_else(|| {
|
||||
invalid_request("cannot resolve relative TMPDIR on the executor".into())
|
||||
})?;
|
||||
Some(std::mem::replace(tmpdir, resolved))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let command = SandboxCommand {
|
||||
program: helper.as_os_str().to_owned(),
|
||||
args: vec![CODEX_FS_HELPER_ARG1.to_string()],
|
||||
cwd: PathUri::from_abs_path(&helper_cwd),
|
||||
env: self.helper_env.clone(),
|
||||
env: policy_env,
|
||||
managed_network: None,
|
||||
additional_permissions: None,
|
||||
};
|
||||
@@ -141,6 +180,10 @@ impl FileSystemSandboxRunner {
|
||||
})?
|
||||
.into_command_request()
|
||||
.map_err(io_error)?;
|
||||
// Only the policy uses the resolved path. Keep the helper's scratch directory unchanged.
|
||||
if let Some(tmpdir) = original_tmpdir {
|
||||
request.env.insert("TMPDIR".into(), tmpdir);
|
||||
}
|
||||
request.sandbox_override = sandbox.sandbox_override;
|
||||
Ok(request)
|
||||
}
|
||||
|
||||
@@ -317,6 +317,9 @@ fn linux_sandbox_exe(
|
||||
) -> Option<PathBuf> {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
if let Some(path) = env::var_os("CODEX_EXEC_SERVER_TEST_LINUX_SANDBOX_EXE") {
|
||||
return Some(path.into());
|
||||
}
|
||||
guard
|
||||
.and_then(|guard| guard.paths().codex_linux_sandbox_exe.clone())
|
||||
.or_else(|| Some(current_exe.to_path_buf()))
|
||||
|
||||
@@ -544,6 +544,105 @@ async fn file_system_sandboxed_canonicalize_resolves_directory_symlink(
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn sandboxed_file_system_helper_resolves_relative_tmpdir_from_executor_cwd() -> Result<()> {
|
||||
let workspace = TempDir::new()?;
|
||||
let workspace_path = workspace.path().canonicalize()?;
|
||||
let executor_cwd = workspace_path.join("executor");
|
||||
// Both interpretations must exist so an unrelated setup failure cannot hide a bad deny.
|
||||
let root_relative = TempDir::new()?;
|
||||
let relative_tmpdir = root_relative.path().strip_prefix("/")?;
|
||||
let tmpdir = executor_cwd.join(relative_tmpdir);
|
||||
std::fs::create_dir_all(&tmpdir)?;
|
||||
let inside = tmpdir.join("inside");
|
||||
let outside = executor_cwd.join("outside");
|
||||
std::fs::write(&inside, b"inside")?;
|
||||
std::fs::write(&outside, b"outside")?;
|
||||
|
||||
// The test binary normally puts its dispatch alias inside TMPDIR, which we deny below.
|
||||
let helper_paths = crate::common::exec_server::test_codex_helper_paths()?;
|
||||
let linux_sandbox = workspace_path.join(codex_sandboxing::linux::CODEX_LINUX_SANDBOX_ARG0);
|
||||
symlink(&helper_paths.codex_exe, &linux_sandbox)?;
|
||||
let mut command = tokio::process::Command::new(helper_paths.codex_exe);
|
||||
command
|
||||
.args(["exec-server", "--listen", "ws://127.0.0.1:0"])
|
||||
.current_dir(&executor_cwd)
|
||||
.env("CODEX_EXEC_SERVER_TEST_LINUX_SANDBOX_EXE", linux_sandbox)
|
||||
.env("TMPDIR", relative_tmpdir);
|
||||
let server = crate::common::exec_server::ExecServerHarness::start(command).await?;
|
||||
let environment = Environment::create_for_tests(Some(server.websocket_url().to_string()))?;
|
||||
let fs = environment.get_filesystem();
|
||||
let info = environment.info().await?;
|
||||
assert_eq!(
|
||||
info.temporary_directories,
|
||||
Some(vec![PathUri::from_host_native_path(&tmpdir)?])
|
||||
);
|
||||
let cwd = PathUri::from_host_native_path(&workspace_path)?;
|
||||
let sandbox = |entries| {
|
||||
let policy = FileSystemSandboxPolicy::restricted(entries);
|
||||
let mut sandbox = FileSystemSandboxContext::from_permission_profile(
|
||||
PermissionProfile::from_runtime_permissions(&policy, NetworkSandboxPolicy::Restricted),
|
||||
cwd.clone(),
|
||||
);
|
||||
sandbox.temporary_directories = info.temporary_directories.clone();
|
||||
sandbox
|
||||
};
|
||||
let read = async |path: &Path, sandbox: &FileSystemSandboxContext| {
|
||||
fs.read_file(
|
||||
&PathUri::from_host_native_path(path)?,
|
||||
Default::default(),
|
||||
Some(sandbox),
|
||||
)
|
||||
.await
|
||||
};
|
||||
let tmp = FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::Tmpdir,
|
||||
};
|
||||
let deny = sandbox(vec![
|
||||
FileSystemSandboxEntry::new(cwd.clone().into(), FileSystemAccessMode::Read),
|
||||
FileSystemSandboxEntry::new(tmp.clone(), FileSystemAccessMode::Deny),
|
||||
]);
|
||||
assert_eq!(read(&outside, &deny).await?, b"outside");
|
||||
assert_sandbox_denied(&read(&inside, &deny).await.expect_err("denied :tmpdir"));
|
||||
|
||||
// Moving the executor cwd changes the advertised path. Never apply a stale rule.
|
||||
let moved = workspace_path.join("moved");
|
||||
std::fs::rename(&executor_cwd, &moved)?;
|
||||
let moved_inside = moved.join(relative_tmpdir).join("inside");
|
||||
let error = read(&moved_inside, &deny).await.expect_err("stale :tmpdir");
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("temporary directories do not match the executor"),
|
||||
"{error}"
|
||||
);
|
||||
|
||||
// A write with no :tmpdir rule still uses the helper's original scratch directory.
|
||||
fs.create_directory(
|
||||
&cwd.join("created")?,
|
||||
CreateDirectoryOptions {
|
||||
recursive: false,
|
||||
follow_symlinks: true,
|
||||
},
|
||||
Some(&workspace_write_sandbox(workspace_path.clone())),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut grant = sandbox(vec![FileSystemSandboxEntry::new(
|
||||
tmp,
|
||||
FileSystemAccessMode::Read,
|
||||
)]);
|
||||
grant.temporary_directories = environment.force_info().await?.temporary_directories;
|
||||
assert_eq!(read(&moved_inside, &grant).await?, b"inside");
|
||||
assert_sandbox_denied(
|
||||
&read(&moved.join("outside"), &grant)
|
||||
.await
|
||||
.expect_err("outside :tmpdir"),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn sandboxed_file_system_helper_finds_bwrap_on_preserved_path() -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user