Honor prepared Unix socket permissions in Seatbelt (#45548)

## Why

Seatbelt ignored Unix socket permissions in `ManagedNetworkSandboxContext`, which could omit allowed sockets or inherit a live proxy's broader permissions.

## What changed

Use the prepared context's `allow_unix_sockets` and `dangerously_allow_all_unix_sockets` settings when present, falling back to the live proxy only when no prepared context exists. Normalize allowlisted paths and preserve explicit extra socket allowances.

## Testing

Add regression coverage for prepared-policy precedence, empty allowlists, invalid relative paths, and explicit extra allowances. Extend macOS exec-server tests to cover prepared and executor-local proxy socket permissions, including explicit allow-all behavior without unrestricted network access.

GitOrigin-RevId: 3372e1f2f1b161e60bde585db8d70b4e3a0b1854
This commit is contained in:
viyatb-oai authored and copyberry committed 2026-09-14 23:52:16 +00:00
1 parent 923c6028b6
commit c18db9ba69
3 files changed
+314 -32

No files matched your search

+34 -21
View File
@@ -130,6 +130,27 @@ impl Default for UnixDomainSocketPolicy {
}
}
impl UnixDomainSocketPolicy {
fn from_allowlist(paths: &[String], extra_allowed: Vec<AbsolutePathBuf>) -> Self {
let mut allowed = paths
.iter()
.filter_map(|socket_path| {
match normalize_path_for_sandbox(Path::new(socket_path)) {
Some(path) => Some(path),
None => {
warn!(
"ignoring network.allow_unix_sockets entry because it could not be normalized: {socket_path}"
);
None
}
}
})
.collect::<Vec<_>>();
allowed.extend(extra_allowed);
Self::Restricted { allowed }
}
}
#[derive(Debug, Clone)]
struct UnixSocketPathParam {
index: usize,
@@ -147,30 +168,22 @@ fn proxy_policy_inputs(
.filter_map(|socket_path| normalize_path_for_sandbox(socket_path.as_path()))
.collect::<Vec<_>>();
let unix_domain_socket_policy = match network {
Some(network) if network.dangerously_allow_all_unix_sockets() => {
// Prefer this command's prepared Unix-socket policy.
// Fall back to the live proxy only when no prepared context exists.
let unix_domain_socket_policy = match (managed_network, network) {
(Some(context), _) if context.dangerously_allow_all_unix_sockets => {
UnixDomainSocketPolicy::AllowAll
}
Some(network) => {
let mut allowed = network
.allow_unix_sockets()
.iter()
.filter_map(|socket_path| {
match normalize_path_for_sandbox(Path::new(socket_path)) {
Some(path) => Some(path),
None => {
warn!(
"ignoring network.allow_unix_sockets entry because it could not be normalized: {socket_path}"
);
None
}
}
})
.collect::<Vec<_>>();
allowed.extend(extra_allowed);
UnixDomainSocketPolicy::Restricted { allowed }
(Some(context), _) => {
UnixDomainSocketPolicy::from_allowlist(&context.allow_unix_sockets, extra_allowed)
}
None => UnixDomainSocketPolicy::Restricted {
(None, Some(network)) if network.dangerously_allow_all_unix_sockets() => {
UnixDomainSocketPolicy::AllowAll
}
(None, Some(network)) => {
UnixDomainSocketPolicy::from_allowlist(&network.allow_unix_sockets(), extra_allowed)
}
(None, None) => UnixDomainSocketPolicy::Restricted {
allowed: extra_allowed,
},
};
+103
View File
@@ -798,6 +798,109 @@ fn prepared_managed_network_context_allows_only_its_proxy_ports() {
assert!(!policy.contains("(allow network-outbound (remote ip \"localhost:9999\"))"));
assert!(!policy.contains("(allow network-bind (local ip \"*:*\"))"));
assert!(!policy.contains("(allow network-outbound)\n"));
assert!(!policy.contains("(allow system-socket (socket-domain AF_UNIX))"));
assert!(!policy.contains("(allow network-outbound (remote unix-socket))"));
}
#[tokio::test]
async fn prepared_managed_network_context_takes_precedence_over_live_proxy_socket_policy()
-> anyhow::Result<()> {
let cwd = TempDir::new().expect("temp cwd");
let file_system_policy = FileSystemSandboxPolicy::from_legacy_sandbox_policy_for_cwd(
&SandboxPolicy::new_read_only_policy(),
cwd.path(),
);
let network_config = NetworkProxyConfig {
enabled: true,
mode: NetworkMode::Full,
dangerously_allow_all_unix_sockets: true,
..Default::default()
};
let state = build_config_state(network_config, NetworkProxyConstraints::default())?;
let network_proxy = NetworkProxy::builder()
.state(Arc::new(NetworkProxyState::with_reloader(
state,
Arc::new(TestConfigReloader),
)))
.managed_by_codex(/*managed_by_codex*/ false)
.build()
.await?;
let prepared_socket = "/tmp/codex-prepared-use";
let explicit_socket = "/tmp/codex-browser-use";
let managed_network = ManagedNetworkSandboxContext {
loopback_ports: vec![43123],
allow_unix_sockets: vec![prepared_socket.to_string(), "relative.sock".to_string()],
..Default::default()
};
let extra_allow_unix_sockets = vec![absolute_path(explicit_socket)];
let args = create_seatbelt_command_args(CreateSeatbeltCommandArgsParams {
command: vec!["/usr/bin/true".to_string()],
file_system_sandbox_policy: &file_system_policy,
network_sandbox_policy: NetworkSandboxPolicy::Restricted,
sandbox_policy_cwd: cwd.path(),
enforce_managed_network: true,
managed_network: Some(&managed_network),
environment_id: None,
network: Some(&network_proxy),
extra_allow_unix_sockets: &extra_allow_unix_sockets,
})
.expect("create seatbelt args");
let policy = seatbelt_policy_arg(&args);
assert!(policy.contains("(allow network-outbound (remote ip \"localhost:43123\"))"));
assert!(policy.contains("(allow system-socket (socket-domain AF_UNIX))"));
assert!(!policy.contains("(allow network-bind (local unix-socket))"));
assert!(!policy.contains("(allow network-outbound (remote unix-socket))"));
let expected_explicit_socket = normalize_path_for_sandbox(Path::new(explicit_socket))
.expect("explicit socket root should normalize");
let expected_prepared_socket = normalize_path_for_sandbox(Path::new(prepared_socket))
.expect("prepared socket root should normalize");
assert_eq!(
args.iter()
.filter(|arg| arg.starts_with("-DUNIX_SOCKET_PATH_"))
.cloned()
.collect::<Vec<_>>(),
vec![
format!(
"-DUNIX_SOCKET_PATH_0={}",
expected_explicit_socket.display()
),
format!(
"-DUNIX_SOCKET_PATH_1={}",
expected_prepared_socket.display()
),
]
);
// An empty prepared policy must not inherit the live proxy's allow-all grant.
let managed_network = ManagedNetworkSandboxContext {
loopback_ports: vec![43123],
..Default::default()
};
let args = create_seatbelt_command_args(CreateSeatbeltCommandArgsParams {
command: vec!["/usr/bin/true".to_string()],
file_system_sandbox_policy: &file_system_policy,
network_sandbox_policy: NetworkSandboxPolicy::Restricted,
sandbox_policy_cwd: cwd.path(),
enforce_managed_network: true,
managed_network: Some(&managed_network),
environment_id: None,
network: Some(&network_proxy),
extra_allow_unix_sockets: &[],
})
.expect("create seatbelt args for empty prepared policy");
let policy = seatbelt_policy_arg(&args);
assert!(policy.contains("(allow network-outbound (remote ip \"localhost:43123\"))"));
assert!(!policy.contains("(allow system-socket (socket-domain AF_UNIX))"));
assert!(!policy.contains("(allow network-bind (local unix-socket))"));
assert!(!policy.contains("(allow network-outbound (remote unix-socket))"));
assert!(
!args
.iter()
.any(|arg| arg.starts_with("-DUNIX_SOCKET_PATH_"))
);
Ok(())
}
#[test]