Protect macOS Seatbelt writable root anchors (#39599)

## Why

A sandboxed process could replace a writable directory root, changing the
authority boundary used to construct a later sandbox policy.

## What changed

- Deny unlinking or renaming directory roots granted write access by Seatbelt,
  while preserving replacement and deletion behavior for writable files.
- Reject writable roots with nested symlink components and report these as
  Seatbelt preparation errors instead of network proxy failures.
- Normalize writable roots before adding them to the generated Seatbelt policy.

## Testing

Add coverage for symlink rejection, error classification, directory-root
replacement and rename protection, newly created roots, and writable file
replacement.

GitOrigin-RevId: ab1ed4e55f20034bc43e028e6529d3d1f0d8181c
This commit is contained in:
jif authored and copyberry committed 2026-08-20 05:36:23 +00:00
1 parent d75c85f651
commit f6950546e5
6 files changed
+415 -25

No files matched your search

+56
View File
@@ -122,6 +122,62 @@ fn unsandboxed_transform_preserves_foreign_cwd_and_unrestricted_file_system_poli
);
}
#[cfg(target_os = "macos")]
#[test]
fn symlinked_workspace_reports_seatbelt_preparation_error() {
use std::os::unix::fs::symlink;
let manager = SandboxManager::new();
let temp_dir = TempDir::new().expect("create temp dir");
let target = temp_dir.path().join("target");
let workspace = temp_dir.path().join("workspace");
std::fs::create_dir(&target).expect("create target");
symlink(&target, &workspace).expect("create symlinked workspace");
let workspace = AbsolutePathBuf::from_absolute_path(workspace).expect("absolute workspace");
let workspace_uri = PathUri::from_abs_path(&workspace);
let permissions = PermissionProfile::from_runtime_permissions(
&FileSystemSandboxPolicy::workspace_write(
&[],
/*exclude_tmpdir_env_var*/ true,
/*exclude_slash_tmp*/ true,
),
NetworkSandboxPolicy::Restricted,
);
let error = manager
.transform(SandboxTransformRequest {
command: SandboxCommand {
program: "true".into(),
args: Vec::new(),
cwd: workspace_uri.clone(),
env: HashMap::new(),
managed_network: None,
additional_permissions: None,
},
permissions: &permissions,
sandbox: SandboxType::MacosSeatbelt,
enforce_managed_network: false,
environment_id: None,
network: None,
sandbox_policy_cwd: &workspace_uri,
codex_linux_sandbox_exe: None,
use_legacy_landlock: false,
windows_sandbox_level: WindowsSandboxLevel::Disabled,
windows_sandbox_private_desktop: false,
})
.expect_err("symlinked workspace should be rejected");
assert!(matches!(
&error,
super::SandboxTransformError::SeatbeltPreparation(message)
if message.contains("symlinked writable roots are not supported")
));
assert!(
!error.to_string().contains("network proxy"),
"filesystem error should not be attributed to network proxy: {error}"
);
}
#[test]
fn transform_additional_permissions_enable_network_for_external_sandbox() {
let manager = SandboxManager::new();