Restrict macOS scratch access to process sandboxes (#40961)

## Why

Filesystem helpers should not inherit access to system scratch directories beyond the paths granted by their filesystem policy, while ordinary processes still need scratch access for compatibility.

## What changed

- Move the `/tmp` and `/var/tmp` read/write grants, including their `/private` aliases, from the shared platform defaults into the process-only Seatbelt defaults.
- Keep process access to `/Applications` alongside the scratch-directory grants.

## Testing

Add a macOS Seatbelt test that verifies the generated policies and, when nested Seatbelt execution is available, confirms process scratch access, helper denial, and helper access to an approved project path.

GitOrigin-RevId: 26d83f09c131c1f1d000163b2f8929f601749719
This commit is contained in:
dank-openai
2026-08-26 19:38:42 +00:00
committed by copyberry
parent d47e5cc0e2
commit 7f82397363
3 changed files with 177 additions and 9 deletions
@@ -90,12 +90,6 @@
(allow file-read* file-test-existence file-write-data file-ioctl
(literal "/dev/dtracehelper"))
; Scratch space so tools can create temp files.
(allow file-read* file-test-existence file-write* (subpath "/tmp"))
(allow file-read* file-write* (subpath "/private/tmp"))
(allow file-read* file-write* (subpath "/var/tmp"))
(allow file-read* file-write* (subpath "/private/var/tmp"))
; Allow reading standard config directories.
(allow file-read* (subpath "/etc"))
(allow file-read* (subpath "/private/etc"))
+10 -3
View File
@@ -23,8 +23,15 @@ const MACOS_SEATBELT_NETWORK_POLICY: &str = include_str!("seatbelt_network_polic
const MACOS_SEATBELT_PREFERENCES_POLICY: &str = include_str!("seatbelt_preferences_policy.sbpl");
const MACOS_RESTRICTED_READ_ONLY_PLATFORM_DEFAULTS: &str =
include_str!("restricted_read_only_platform_defaults.sbpl");
const MACOS_PROCESS_APPLICATIONS_READ_POLICY: &str =
r#"(allow file-read* (subpath "/Applications"))"#;
// Ordinary processes need system scratch directories for compatibility, but filesystem helpers
// must not inherit scratch access beyond the paths their permission profile explicitly grants.
const MACOS_PROCESS_PLATFORM_DEFAULTS: &str = r#"
(allow file-read* (subpath "/Applications"))
(allow file-read* file-test-existence file-write* (subpath "/tmp"))
(allow file-read* file-write* (subpath "/private/tmp"))
(allow file-read* file-write* (subpath "/var/tmp"))
(allow file-read* file-write* (subpath "/private/var/tmp"))
"#;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) enum MacosSeatbeltProfile {
@@ -994,7 +1001,7 @@ pub(crate) fn create_seatbelt_command_args_with_profile(
if include_platform_defaults {
policy_sections.push(MACOS_RESTRICTED_READ_ONLY_PLATFORM_DEFAULTS.to_string());
if profile == MacosSeatbeltProfile::Process {
policy_sections.push(MACOS_PROCESS_APPLICATIONS_READ_POLICY.to_string());
policy_sections.push(MACOS_PROCESS_PLATFORM_DEFAULTS.to_string());
}
}
policy_sections.push(deny_read_policy);
+167
View File
@@ -258,6 +258,173 @@ fn filesystem_helper_platform_defaults_do_not_grant_applications_directory() {
);
}
#[test]
fn process_platform_defaults_allow_scratch_without_granting_it_to_filesystem_helpers() {
let workspace = tempfile::Builder::new()
.prefix("codex-seatbelt-approved-project-")
.tempdir_in("/private/tmp")
.expect("approved project directory");
let approved_file = workspace.path().join("approved.txt");
fs::write(&approved_file, "approved-project").expect("write approved project file");
let workspace_root = AbsolutePathBuf::from_absolute_path(workspace.path())
.expect("workspace path should be absolute");
let file_system_policy = FileSystemSandboxPolicy::restricted(vec![
FileSystemSandboxEntry::new(
FileSystemPath::Path {
path: workspace_root.into(),
},
FileSystemAccessMode::Read,
),
FileSystemSandboxEntry::new(
FileSystemPath::Special {
value: FileSystemSpecialPath::Minimal,
},
FileSystemAccessMode::Read,
),
]);
let sandboxed_args = |command: Vec<String>, profile: MacosSeatbeltProfile| {
create_seatbelt_command_args_with_profile(
CreateSeatbeltCommandArgsParams {
command,
file_system_sandbox_policy: &file_system_policy,
network_sandbox_policy: NetworkSandboxPolicy::Restricted,
sandbox_policy_cwd: workspace.path(),
enforce_managed_network: false,
managed_network: None,
environment_id: None,
network: None,
extra_allow_unix_sockets: &[],
},
profile,
)
.expect("build restricted seatbelt command")
};
let scratch_grants = [
(
"/tmp",
r#"(allow file-read* file-test-existence file-write* (subpath "/tmp"))"#,
),
(
"/private/tmp",
r#"(allow file-read* file-write* (subpath "/private/tmp"))"#,
),
(
"/var/tmp",
r#"(allow file-read* file-write* (subpath "/var/tmp"))"#,
),
(
"/private/var/tmp",
r#"(allow file-read* file-write* (subpath "/private/var/tmp"))"#,
),
];
for profile in [
MacosSeatbeltProfile::Process,
MacosSeatbeltProfile::FileSystemHelper,
] {
let args = sandboxed_args(vec!["/usr/bin/true".to_string()], profile);
let policy = seatbelt_policy_arg(&args);
for (scratch_root, scratch_grant) in scratch_grants {
match profile {
MacosSeatbeltProfile::Process => assert!(
policy.contains(scratch_grant),
"processes should retain scratch read/write access to {scratch_root}"
),
MacosSeatbeltProfile::FileSystemHelper => assert!(
!policy.contains(&format!(r#"(subpath "{scratch_root}")"#)),
"filesystem helpers should not inherit scratch access to {scratch_root}"
),
}
}
}
let run_sandboxed = |command: Vec<String>, profile: MacosSeatbeltProfile| {
Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE)
.args(sandboxed_args(command, profile))
.current_dir(workspace.path())
.output()
.expect("run restricted seatbelt command")
};
for scratch_root in ["/private/tmp", "/private/var/tmp"] {
let scratch = tempfile::Builder::new()
.prefix("codex-seatbelt-process-scratch-")
.tempdir_in(scratch_root)
.expect("scratch directory");
let scratch_file = scratch.path().join("scratch.txt");
let process_result = run_sandboxed(
vec![
"/bin/sh".to_string(),
"-c".to_string(),
"printf '%s' 'scratch-access' > \"$1\" && /bin/cat \"$1\"".to_string(),
"seatbelt-scratch".to_string(),
scratch_file.display().to_string(),
],
MacosSeatbeltProfile::Process,
);
let process_stderr = String::from_utf8_lossy(&process_result.stderr);
if !process_result.status.success()
&& process_stderr.contains("sandbox-exec: sandbox_apply: Operation not permitted")
{
eprintln!(
"nested Seatbelt is unavailable; generated policies verified every scratch path"
);
break;
}
assert!(
process_result.status.success(),
"processes should retain scratch read/write access to {scratch_root}: {process_stderr}"
);
assert_eq!(process_result.stdout, b"scratch-access");
let helper_result = run_sandboxed(
vec!["/bin/cat".to_string(), scratch_file.display().to_string()],
MacosSeatbeltProfile::FileSystemHelper,
);
let helper_stderr = String::from_utf8_lossy(&helper_result.stderr);
assert!(
!helper_result.status.success() && helper_stderr.contains("Operation not permitted"),
"filesystem helpers should not inherit scratch access to {scratch_root}: {helper_stderr}"
);
if scratch_root == "/private/tmp" {
let approved_read = run_sandboxed(
vec!["/bin/cat".to_string(), approved_file.display().to_string()],
MacosSeatbeltProfile::FileSystemHelper,
);
let approved_stderr = String::from_utf8_lossy(&approved_read.stderr);
assert!(
approved_read.status.success(),
"filesystem helpers should read files in the approved project: {approved_stderr}"
);
assert_eq!(approved_read.stdout, b"approved-project");
let canonicalized = run_sandboxed(
vec![
"/bin/realpath".to_string(),
approved_file.display().to_string(),
],
MacosSeatbeltProfile::FileSystemHelper,
);
let canonicalize_stderr = String::from_utf8_lossy(&canonicalized.stderr);
assert!(
canonicalized.status.success(),
"filesystem helpers should canonicalize approved project files: {canonicalize_stderr}"
);
let expected_path = approved_file
.canonicalize()
.expect("canonicalize approved project file");
assert_eq!(
canonicalized.stdout,
format!("{}\n", expected_path.display()).as_bytes()
);
}
}
}
#[test]
fn create_seatbelt_args_routes_network_through_proxy_ports() {
let policy = dynamic_network_policy(