mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Replant sandbox coverage onto used paths
This commit is contained in:
@@ -8,6 +8,8 @@ use super::merge_file_system_policy_with_additional_permissions;
|
||||
use super::normalize_additional_permissions;
|
||||
use super::sandbox_policy_with_additional_permissions;
|
||||
use super::should_require_platform_sandbox;
|
||||
use crate::exec::ExecCapturePolicy;
|
||||
use crate::exec::ExecExpiration;
|
||||
use crate::exec::SandboxType;
|
||||
use crate::protocol::NetworkAccess;
|
||||
use crate::protocol::ReadOnlyAccess;
|
||||
@@ -32,11 +34,17 @@ use codex_protocol::permissions::FileSystemSandboxPolicy;
|
||||
use codex_protocol::permissions::FileSystemSpecialPath;
|
||||
use codex_protocol::permissions::NetworkSandboxPolicy;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
#[cfg(target_os = "linux")]
|
||||
use core_test_support::codex_linux_sandbox_exe_or_skip;
|
||||
#[cfg(unix)]
|
||||
use core_test_support::skip_if_sandbox;
|
||||
use dunce::canonicalize;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::collections::HashMap;
|
||||
#[cfg(unix)]
|
||||
use std::path::Path;
|
||||
#[cfg(unix)]
|
||||
use std::path::PathBuf;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[cfg(unix)]
|
||||
@@ -44,6 +52,81 @@ fn symlink_dir(original: &Path, link: &Path) -> std::io::Result<()> {
|
||||
std::os::unix::fs::symlink(original, link)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn sandbox_command_spec(
|
||||
command: Vec<String>,
|
||||
cwd: PathBuf,
|
||||
env: HashMap<String, String>,
|
||||
) -> super::CommandSpec {
|
||||
let mut args = command.into_iter();
|
||||
let program = args.next().expect("command should not be empty");
|
||||
super::CommandSpec {
|
||||
program,
|
||||
args: args.collect(),
|
||||
cwd,
|
||||
env,
|
||||
expiration: ExecExpiration::DefaultTimeout,
|
||||
capture_policy: ExecCapturePolicy::ShellTool,
|
||||
sandbox_permissions: super::SandboxPermissions::UseDefault,
|
||||
additional_permissions: None,
|
||||
justification: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
async fn execute_command_under_platform_sandbox(
|
||||
command: Vec<String>,
|
||||
command_cwd: PathBuf,
|
||||
sandbox_policy: &SandboxPolicy,
|
||||
env: HashMap<String, String>,
|
||||
codex_linux_sandbox_exe: Option<&PathBuf>,
|
||||
) -> crate::error::Result<crate::exec::ExecToolCallOutput> {
|
||||
let file_system_policy =
|
||||
FileSystemSandboxPolicy::from_legacy_sandbox_policy(sandbox_policy, command_cwd.as_path());
|
||||
let network_policy = NetworkSandboxPolicy::from(sandbox_policy);
|
||||
let manager = SandboxManager::new();
|
||||
let exec_request = manager
|
||||
.transform(super::SandboxTransformRequest {
|
||||
spec: sandbox_command_spec(command, command_cwd.clone(), env),
|
||||
policy: sandbox_policy,
|
||||
file_system_policy: &file_system_policy,
|
||||
network_policy,
|
||||
#[cfg(target_os = "macos")]
|
||||
sandbox: SandboxType::MacosSeatbelt,
|
||||
#[cfg(target_os = "linux")]
|
||||
sandbox: SandboxType::LinuxSeccomp,
|
||||
enforce_managed_network: false,
|
||||
network: None,
|
||||
sandbox_policy_cwd: command_cwd.as_path(),
|
||||
#[cfg(target_os = "macos")]
|
||||
macos_seatbelt_profile_extensions: None,
|
||||
codex_linux_sandbox_exe,
|
||||
use_legacy_landlock: false,
|
||||
windows_sandbox_level: WindowsSandboxLevel::Disabled,
|
||||
windows_sandbox_private_desktop: false,
|
||||
})
|
||||
.expect("transform");
|
||||
super::execute_env(exec_request, None).await
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
async fn linux_sandbox_is_enforceable(codex_linux_sandbox_exe: &PathBuf) -> bool {
|
||||
let command_cwd = match std::env::current_dir() {
|
||||
Ok(cwd) => cwd,
|
||||
Err(_) => return false,
|
||||
};
|
||||
execute_command_under_platform_sandbox(
|
||||
vec!["/usr/bin/true".to_string()],
|
||||
command_cwd,
|
||||
&SandboxPolicy::new_read_only_policy(),
|
||||
HashMap::new(),
|
||||
Some(codex_linux_sandbox_exe),
|
||||
)
|
||||
.await
|
||||
.map(|output| output.exit_code == 0)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn danger_full_access_defaults_to_no_sandbox_without_network_requirements() {
|
||||
let manager = SandboxManager::new();
|
||||
@@ -191,6 +274,101 @@ fn transform_preserves_unrestricted_file_system_policy_for_restricted_network()
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn transform_wraps_command_for_seatbelt_execution() {
|
||||
let manager = SandboxManager::new();
|
||||
let cwd = std::env::current_dir().expect("current dir");
|
||||
let policy = SandboxPolicy::new_read_only_policy();
|
||||
let file_system_policy =
|
||||
FileSystemSandboxPolicy::from_legacy_sandbox_policy(&policy, cwd.as_path());
|
||||
let exec_request = manager
|
||||
.transform(super::SandboxTransformRequest {
|
||||
spec: sandbox_command_spec(
|
||||
vec!["/bin/echo".to_string(), "hello".to_string()],
|
||||
cwd.clone(),
|
||||
HashMap::new(),
|
||||
),
|
||||
policy: &policy,
|
||||
file_system_policy: &file_system_policy,
|
||||
network_policy: NetworkSandboxPolicy::from(&policy),
|
||||
sandbox: SandboxType::MacosSeatbelt,
|
||||
enforce_managed_network: false,
|
||||
network: None,
|
||||
sandbox_policy_cwd: cwd.as_path(),
|
||||
macos_seatbelt_profile_extensions: None,
|
||||
codex_linux_sandbox_exe: None,
|
||||
use_legacy_landlock: false,
|
||||
windows_sandbox_level: WindowsSandboxLevel::Disabled,
|
||||
windows_sandbox_private_desktop: false,
|
||||
})
|
||||
.expect("transform");
|
||||
|
||||
assert_eq!(
|
||||
exec_request.command.first().map(String::as_str),
|
||||
Some(codex_sandboxing::seatbelt::MACOS_PATH_TO_SEATBELT_EXECUTABLE)
|
||||
);
|
||||
assert_eq!(exec_request.arg0, None);
|
||||
assert_eq!(
|
||||
exec_request
|
||||
.env
|
||||
.get(crate::spawn::CODEX_SANDBOX_ENV_VAR)
|
||||
.map(String::as_str),
|
||||
Some("seatbelt")
|
||||
);
|
||||
assert_eq!(
|
||||
exec_request
|
||||
.env
|
||||
.get(crate::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR)
|
||||
.map(String::as_str),
|
||||
Some("1")
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn transform_wraps_command_for_linux_sandbox_execution() {
|
||||
let manager = SandboxManager::new();
|
||||
let cwd = std::env::current_dir().expect("current dir");
|
||||
let policy = SandboxPolicy::new_read_only_policy();
|
||||
let file_system_policy =
|
||||
FileSystemSandboxPolicy::from_legacy_sandbox_policy(&policy, cwd.as_path());
|
||||
let sandbox_exe = PathBuf::from("/tmp/codex-linux-sandbox");
|
||||
let exec_request = manager
|
||||
.transform(super::SandboxTransformRequest {
|
||||
spec: sandbox_command_spec(
|
||||
vec!["/bin/echo".to_string(), "hello".to_string()],
|
||||
cwd.clone(),
|
||||
HashMap::new(),
|
||||
),
|
||||
policy: &policy,
|
||||
file_system_policy: &file_system_policy,
|
||||
network_policy: NetworkSandboxPolicy::from(&policy),
|
||||
sandbox: SandboxType::LinuxSeccomp,
|
||||
enforce_managed_network: false,
|
||||
network: None,
|
||||
sandbox_policy_cwd: cwd.as_path(),
|
||||
codex_linux_sandbox_exe: Some(&sandbox_exe),
|
||||
use_legacy_landlock: false,
|
||||
windows_sandbox_level: WindowsSandboxLevel::Disabled,
|
||||
windows_sandbox_private_desktop: false,
|
||||
})
|
||||
.expect("transform");
|
||||
|
||||
assert_eq!(
|
||||
exec_request.command.first().map(String::as_str),
|
||||
sandbox_exe.to_str()
|
||||
);
|
||||
assert_eq!(
|
||||
exec_request
|
||||
.env
|
||||
.get(crate::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR)
|
||||
.map(String::as_str),
|
||||
Some("1")
|
||||
);
|
||||
assert_eq!(exec_request.arg0.as_deref(), Some("codex-linux-sandbox"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_additional_permissions_preserves_network() {
|
||||
let temp_dir = TempDir::new().expect("create temp dir");
|
||||
@@ -769,3 +947,139 @@ fn effective_file_system_sandbox_policy_merges_additional_write_roots() {
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn python_multiprocessing_lock_works_under_platform_sandbox() {
|
||||
skip_if_sandbox!();
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
let codex_linux_sandbox_exe = codex_linux_sandbox_exe_or_skip!();
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
let codex_linux_sandbox_exe: Option<PathBuf> = None;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
if !linux_sandbox_is_enforceable(
|
||||
codex_linux_sandbox_exe
|
||||
.as_ref()
|
||||
.expect("linux sandbox binary should be set"),
|
||||
)
|
||||
.await
|
||||
{
|
||||
eprintln!("Skipping test: Landlock is not enforceable on this host.");
|
||||
return;
|
||||
}
|
||||
|
||||
if std::process::Command::new("python3")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.is_err()
|
||||
{
|
||||
eprintln!("python3 not found in PATH, skipping test.");
|
||||
return;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
let writable_roots = Vec::<AbsolutePathBuf>::new();
|
||||
#[cfg(target_os = "linux")]
|
||||
let writable_roots: Vec<AbsolutePathBuf> = vec!["/dev/shm".try_into().expect("/dev/shm")];
|
||||
|
||||
let policy = SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots,
|
||||
read_only_access: Default::default(),
|
||||
network_access: false,
|
||||
exclude_tmpdir_env_var: false,
|
||||
exclude_slash_tmp: false,
|
||||
};
|
||||
let python_code = r#"import multiprocessing
|
||||
from multiprocessing import Lock, Process
|
||||
|
||||
def f(lock):
|
||||
with lock:
|
||||
print("Lock acquired in child process")
|
||||
|
||||
if __name__ == '__main__':
|
||||
lock = Lock()
|
||||
p = Process(target=f, args=(lock,))
|
||||
p.start()
|
||||
p.join()
|
||||
"#;
|
||||
let command_cwd = std::env::current_dir().expect("current dir");
|
||||
let output = execute_command_under_platform_sandbox(
|
||||
vec![
|
||||
"python3".to_string(),
|
||||
"-c".to_string(),
|
||||
python_code.to_string(),
|
||||
],
|
||||
command_cwd,
|
||||
&policy,
|
||||
HashMap::new(),
|
||||
codex_linux_sandbox_exe.as_ref(),
|
||||
)
|
||||
.await
|
||||
.expect("should execute python under sandbox");
|
||||
|
||||
assert_eq!(
|
||||
output.exit_code, 0,
|
||||
"python exited with {}\nstdout: {}\nstderr: {}",
|
||||
output.exit_code, output.stdout.text, output.stderr.text
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn python_getpwuid_works_under_platform_sandbox() {
|
||||
skip_if_sandbox!();
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
let codex_linux_sandbox_exe = codex_linux_sandbox_exe_or_skip!();
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
let codex_linux_sandbox_exe: Option<PathBuf> = None;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
if !linux_sandbox_is_enforceable(
|
||||
codex_linux_sandbox_exe
|
||||
.as_ref()
|
||||
.expect("linux sandbox binary should be set"),
|
||||
)
|
||||
.await
|
||||
{
|
||||
eprintln!("Skipping test: Landlock is not enforceable on this host.");
|
||||
return;
|
||||
}
|
||||
|
||||
if std::process::Command::new("python3")
|
||||
.arg("--version")
|
||||
.output()
|
||||
.is_err()
|
||||
{
|
||||
eprintln!("python3 not found in PATH, skipping test.");
|
||||
return;
|
||||
}
|
||||
|
||||
let command_cwd = std::env::current_dir().expect("current dir");
|
||||
let output = execute_command_under_platform_sandbox(
|
||||
vec![
|
||||
"python3".to_string(),
|
||||
"-c".to_string(),
|
||||
"import pwd, os; print(pwd.getpwuid(os.getuid()))".to_string(),
|
||||
],
|
||||
command_cwd,
|
||||
&SandboxPolicy::new_read_only_policy(),
|
||||
HashMap::new(),
|
||||
codex_linux_sandbox_exe.as_ref(),
|
||||
)
|
||||
.await
|
||||
.expect("should execute python under sandbox");
|
||||
|
||||
assert_eq!(
|
||||
output.exit_code, 0,
|
||||
"python exited with {}\nstdout: {}\nstderr: {}",
|
||||
output.exit_code, output.stdout.text, output.stderr.text
|
||||
);
|
||||
assert!(
|
||||
!output.stdout.text.trim().is_empty(),
|
||||
"expected pwd.getpwuid output, stderr: {}",
|
||||
output.stderr.text
|
||||
);
|
||||
}
|
||||
|
||||
@@ -52,6 +52,10 @@ fn seatbelt_policy_arg(args: &[String]) -> &str {
|
||||
.expect("seatbelt args should include policy text")
|
||||
}
|
||||
|
||||
fn sandbox_apply_failed(stderr: &[u8]) -> bool {
|
||||
String::from_utf8_lossy(stderr).contains("sandbox-exec: sandbox_apply: Operation not permitted")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn base_policy_allows_node_cpu_sysctls() {
|
||||
assert!(
|
||||
@@ -274,6 +278,100 @@ sys.exit(0 if allowed else 13)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openpty_works_under_seatbelt() {
|
||||
if Command::new("python3").arg("--version").output().is_err() {
|
||||
eprintln!("python3 not found in PATH, skipping test.");
|
||||
return;
|
||||
}
|
||||
|
||||
let cwd = std::env::current_dir().expect("getcwd");
|
||||
let args = create_seatbelt_command_args_with_extensions(
|
||||
vec![
|
||||
"python3".to_string(),
|
||||
"-c".to_string(),
|
||||
r#"import os
|
||||
|
||||
master, slave = os.openpty()
|
||||
os.write(slave, b"ping")
|
||||
assert os.read(master, 4) == b"ping""#
|
||||
.to_string(),
|
||||
],
|
||||
&SandboxPolicy::new_read_only_policy(),
|
||||
cwd.as_path(),
|
||||
/*enforce_managed_network*/ false,
|
||||
None,
|
||||
/*extensions*/ None,
|
||||
);
|
||||
let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE)
|
||||
.args(&args)
|
||||
.current_dir(&cwd)
|
||||
.output()
|
||||
.expect("execute seatbelt command");
|
||||
if sandbox_apply_failed(&output.stderr) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"python exited with {:?}, stderr: {}",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn java_home_finds_runtime_under_seatbelt() {
|
||||
let java_home_path = Path::new("/usr/libexec/java_home");
|
||||
if !java_home_path.exists() {
|
||||
eprintln!("/usr/libexec/java_home is not present, skipping test.");
|
||||
return;
|
||||
}
|
||||
|
||||
let baseline_output = Command::new(java_home_path)
|
||||
.env_remove("JAVA_HOME")
|
||||
.output()
|
||||
.expect("invoke java_home outside seatbelt");
|
||||
if !baseline_output.status.success() {
|
||||
eprintln!(
|
||||
"java_home exited with {:?} outside seatbelt, skipping test",
|
||||
baseline_output.status
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let cwd = std::env::current_dir().expect("getcwd");
|
||||
let args = create_seatbelt_command_args_with_extensions(
|
||||
vec![java_home_path.to_string_lossy().to_string()],
|
||||
&SandboxPolicy::new_read_only_policy(),
|
||||
cwd.as_path(),
|
||||
/*enforce_managed_network*/ false,
|
||||
None,
|
||||
/*extensions*/ None,
|
||||
);
|
||||
let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE)
|
||||
.args(&args)
|
||||
.current_dir(&cwd)
|
||||
.env_remove("JAVA_HOME")
|
||||
.env_remove("CODEX_SANDBOX")
|
||||
.output()
|
||||
.expect("execute seatbelt command");
|
||||
if sandbox_apply_failed(&output.stderr) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"java_home under seatbelt exited with {:?}, stderr: {}",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert!(
|
||||
!String::from_utf8_lossy(&output.stdout).trim().is_empty(),
|
||||
"java_home stdout unexpectedly empty under seatbelt"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seatbelt_args_without_extension_profile_keep_legacy_preferences_read_access() {
|
||||
let cwd = std::env::temp_dir();
|
||||
|
||||
Reference in New Issue
Block a user