mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Honor explicit Unix socket grants in the Linux managed sandbox (#45534)
## Why Linux proxy-routed sandboxing denied standalone Unix sockets even when the effective network policy enabled `dangerously_allow_all_unix_sockets`. ## What changed - Carry Unix socket permissions in `ManagedNetworkSandboxContext` and pass the prepared context through Linux sandbox launches with `--managed-network`. - Allow `AF_UNIX` socket creation in proxy-routed mode when `dangerously_allow_all_unix_sockets` is enabled, while preserving network namespace isolation and restrictions on other socket families. - Keep standalone Unix sockets denied by default and for path-only grants. Default missing fields in older serialized contexts to restrictive values. ## Testing Add coverage for policy preparation and transport, legacy deserialization, and malformed policy rejection. Add a Linux integration test covering default denial, path-only denial, and explicit allow-all access, while checking that direct TCP access and `AF_NETLINK`/`AF_VSOCK` sockets remain blocked. GitOrigin-RevId: 2695b945ad3e59fcb3faf7662d852a26650af16c
This commit is contained in:
1 parent
d39cfa8a2d
commit
99914f4950
18 files changed
+599
-68
No files matched your search
@@ -5,6 +5,7 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
|
||||
use codex_network_proxy::ManagedNetworkSandboxContext;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result;
|
||||
use codex_protocol::error::SandboxErr;
|
||||
@@ -43,15 +44,15 @@ pub(crate) fn apply_permission_profile_to_current_thread(
|
||||
permission_profile: &PermissionProfile,
|
||||
cwd: &Path,
|
||||
apply_landlock_fs: bool,
|
||||
allow_network_for_proxy: bool,
|
||||
proxy_routed_network: bool,
|
||||
managed_network: Option<&ManagedNetworkSandboxContext>,
|
||||
proxy_routing_active: bool,
|
||||
) -> Result<()> {
|
||||
let (file_system_sandbox_policy, network_sandbox_policy) =
|
||||
permission_profile.to_runtime_permissions();
|
||||
let network_seccomp_mode = network_seccomp_mode(
|
||||
network_sandbox_policy,
|
||||
allow_network_for_proxy,
|
||||
proxy_routed_network,
|
||||
managed_network.is_some(),
|
||||
proxy_routing_active,
|
||||
)
|
||||
.or_else(|| {
|
||||
// VM sockets can reach host services outside the filesystem sandbox.
|
||||
@@ -73,7 +74,7 @@ pub(crate) fn apply_permission_profile_to_current_thread(
|
||||
}
|
||||
|
||||
if let Some(mode) = network_seccomp_mode {
|
||||
install_network_seccomp_filter_on_current_thread(mode)?;
|
||||
install_network_seccomp_filter_on_current_thread(mode, managed_network)?;
|
||||
}
|
||||
|
||||
if apply_landlock_fs && !file_system_sandbox_policy.has_full_disk_write_access() {
|
||||
@@ -177,6 +178,7 @@ fn install_filesystem_landlock_rules_on_current_thread(
|
||||
/// inherits it.
|
||||
fn install_network_seccomp_filter_on_current_thread(
|
||||
mode: NetworkSeccompMode,
|
||||
managed_network: Option<&ManagedNetworkSandboxContext>,
|
||||
) -> std::result::Result<(), SandboxErr> {
|
||||
fn deny_syscall(rules: &mut BTreeMap<i64, Vec<SeccompRule>>, nr: i64) {
|
||||
rules.insert(nr, vec![]); // empty rule vec = unconditional match
|
||||
@@ -230,12 +232,10 @@ fn install_network_seccomp_filter_on_current_thread(
|
||||
}
|
||||
NetworkSeccompMode::ProxyRouted => {
|
||||
// In proxy-routed mode we allow IP sockets in the isolated
|
||||
// namespace (used to reach the local TCP bridge) but deny socket()
|
||||
// for all other families, including AF_UNIX. Only AF_UNIX
|
||||
// socketpair() remains available for process-local IPC because it
|
||||
// cannot connect to a socket outside the sandbox or bypass the
|
||||
// bridge.
|
||||
let deny_non_ip_socket = SeccompRule::new(vec![
|
||||
// namespace (used to reach the local TCP bridge). Standalone Unix
|
||||
// sockets require an explicit managed-policy grant; all other
|
||||
// socket families remain denied.
|
||||
let mut denied_socket_conditions = vec![
|
||||
SeccompCondition::new(
|
||||
0,
|
||||
SeccompCmpArgLen::Dword,
|
||||
@@ -248,7 +248,16 @@ fn install_network_seccomp_filter_on_current_thread(
|
||||
SeccompCmpOp::Ne,
|
||||
libc::AF_INET6 as u64,
|
||||
)?,
|
||||
])?;
|
||||
];
|
||||
if managed_network.is_some_and(|context| context.dangerously_allow_all_unix_sockets) {
|
||||
denied_socket_conditions.push(SeccompCondition::new(
|
||||
0,
|
||||
SeccompCmpArgLen::Dword,
|
||||
SeccompCmpOp::Ne,
|
||||
libc::AF_UNIX as u64,
|
||||
)?);
|
||||
}
|
||||
let deny_non_ip_socket = SeccompRule::new(denied_socket_conditions)?;
|
||||
let deny_non_unix_socketpair = SeccompRule::new(vec![SeccompCondition::new(
|
||||
0,
|
||||
SeccompCmpArgLen::Dword,
|
||||
|
||||
@@ -29,6 +29,7 @@ use crate::launcher::exec_bwrap;
|
||||
use crate::launcher::preferred_bwrap_supports_argv0;
|
||||
use crate::proxy_routing::activate_proxy_routes_in_netns;
|
||||
use crate::proxy_routing::prepare_host_proxy_route_spec;
|
||||
use codex_network_proxy::ManagedNetworkSandboxContext;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::FileSystemAccessMode;
|
||||
@@ -123,13 +124,13 @@ pub struct LandlockCommand {
|
||||
#[arg(long = "apply-seccomp-then-exec", hide = true, default_value_t = false)]
|
||||
pub apply_seccomp_then_exec: bool,
|
||||
|
||||
/// Internal compatibility flag.
|
||||
///
|
||||
/// By default, restricted-network sandboxing uses isolated networking.
|
||||
/// If set, sandbox setup switches to proxy-only network mode with
|
||||
/// managed routing bridges.
|
||||
#[arg(long = "allow-network-for-proxy", hide = true, default_value_t = false)]
|
||||
pub allow_network_for_proxy: bool,
|
||||
/// Effective managed-network policy prepared for this command launch.
|
||||
#[arg(
|
||||
long,
|
||||
hide = true,
|
||||
value_parser = parse_managed_network
|
||||
)]
|
||||
pub managed_network: Option<ManagedNetworkSandboxContext>,
|
||||
|
||||
/// Internal route spec used for managed proxy routing in bwrap mode.
|
||||
#[arg(long = "proxy-route-spec", hide = true)]
|
||||
@@ -164,12 +165,13 @@ pub fn run_main() -> ! {
|
||||
permission_profile,
|
||||
use_legacy_landlock,
|
||||
apply_seccomp_then_exec,
|
||||
allow_network_for_proxy,
|
||||
managed_network,
|
||||
proxy_route_spec,
|
||||
verify_fd_mounts,
|
||||
no_proc,
|
||||
command,
|
||||
} = LandlockCommand::parse();
|
||||
let allow_network_for_proxy = managed_network.is_some();
|
||||
|
||||
if command.is_empty() {
|
||||
panic!("No command specified to execute.");
|
||||
@@ -236,7 +238,7 @@ pub fn run_main() -> ! {
|
||||
&permission_profile,
|
||||
&sandbox_policy_cwd,
|
||||
/*apply_landlock_fs*/ false,
|
||||
allow_network_for_proxy,
|
||||
managed_network.as_ref(),
|
||||
proxy_routing_active,
|
||||
) {
|
||||
panic!("error applying Linux sandbox restrictions: {e:?}");
|
||||
@@ -282,8 +284,8 @@ pub fn run_main() -> ! {
|
||||
&permission_profile,
|
||||
&sandbox_policy_cwd,
|
||||
/*apply_landlock_fs*/ false,
|
||||
allow_network_for_proxy,
|
||||
/*proxy_routed_network*/ false,
|
||||
managed_network.as_ref(),
|
||||
/*proxy_routing_active*/ false,
|
||||
) {
|
||||
panic!("error applying Linux sandbox restrictions: {e:?}");
|
||||
}
|
||||
@@ -305,7 +307,7 @@ pub fn run_main() -> ! {
|
||||
sandbox_policy_cwd: &sandbox_policy_cwd,
|
||||
command_cwd: command_cwd.as_deref(),
|
||||
permission_profile: &permission_profile,
|
||||
allow_network_for_proxy,
|
||||
managed_network,
|
||||
proxy_route_spec,
|
||||
command,
|
||||
});
|
||||
@@ -325,8 +327,8 @@ pub fn run_main() -> ! {
|
||||
&permission_profile,
|
||||
&sandbox_policy_cwd,
|
||||
/*apply_landlock_fs*/ true,
|
||||
allow_network_for_proxy,
|
||||
/*proxy_routed_network*/ false,
|
||||
managed_network.as_ref(),
|
||||
/*proxy_routing_active*/ false,
|
||||
) {
|
||||
panic!("error applying legacy Linux sandbox restrictions: {e:?}");
|
||||
}
|
||||
@@ -357,6 +359,11 @@ fn parse_permission_profile(value: &str) -> std::result::Result<PermissionProfil
|
||||
serde_json::from_str(value).map_err(|err| format!("invalid permission profile JSON: {err}"))
|
||||
}
|
||||
|
||||
fn parse_managed_network(value: &str) -> std::result::Result<ManagedNetworkSandboxContext, String> {
|
||||
serde_json::from_str(value)
|
||||
.map_err(|err| format!("invalid managed network context JSON: {err}"))
|
||||
}
|
||||
|
||||
fn resolve_permission_profile(
|
||||
permission_profile: Option<PermissionProfile>,
|
||||
) -> Result<EffectivePermissions, ResolvePermissionProfileError> {
|
||||
@@ -1521,7 +1528,7 @@ struct InnerSeccompCommandArgs<'a> {
|
||||
sandbox_policy_cwd: &'a Path,
|
||||
command_cwd: Option<&'a Path>,
|
||||
permission_profile: &'a PermissionProfile,
|
||||
allow_network_for_proxy: bool,
|
||||
managed_network: Option<ManagedNetworkSandboxContext>,
|
||||
proxy_route_spec: Option<String>,
|
||||
command: Vec<String>,
|
||||
}
|
||||
@@ -1532,7 +1539,7 @@ fn build_inner_seccomp_command(args: InnerSeccompCommandArgs<'_>) -> Vec<String>
|
||||
sandbox_policy_cwd,
|
||||
command_cwd,
|
||||
permission_profile,
|
||||
allow_network_for_proxy,
|
||||
managed_network,
|
||||
proxy_route_spec,
|
||||
command,
|
||||
} = args;
|
||||
@@ -1559,8 +1566,12 @@ fn build_inner_seccomp_command(args: InnerSeccompCommandArgs<'_>) -> Vec<String>
|
||||
permission_profile_json,
|
||||
"--apply-seccomp-then-exec".to_string(),
|
||||
]);
|
||||
if allow_network_for_proxy {
|
||||
inner.push("--allow-network-for-proxy".to_string());
|
||||
if let Some(managed_network) = managed_network {
|
||||
inner.push("--managed-network".to_string());
|
||||
inner.push(
|
||||
serde_json::to_string(&managed_network)
|
||||
.unwrap_or_else(|err| panic!("failed to serialize managed network context: {err}")),
|
||||
);
|
||||
let proxy_route_spec = proxy_route_spec
|
||||
.unwrap_or_else(|| panic!("managed proxy mode requires a proxy route spec"));
|
||||
inner.push("--proxy-route-spec".to_string());
|
||||
|
||||
@@ -5,6 +5,8 @@ use crate::linux_run_main::install_bwrap_signal_forwarders;
|
||||
#[cfg(test)]
|
||||
use crate::linux_run_main::wait_for_bwrap_child;
|
||||
#[cfg(test)]
|
||||
use codex_network_proxy::ManagedNetworkSandboxContext;
|
||||
#[cfg(test)]
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
#[cfg(test)]
|
||||
use codex_protocol::protocol::FileSystemSandboxPolicy;
|
||||
@@ -553,17 +555,63 @@ fn run_bwrap_signal_forwarder_test_supervisor() -> ! {
|
||||
#[test]
|
||||
fn managed_proxy_inner_command_includes_route_spec() {
|
||||
let permission_profile = read_only_permission_profile();
|
||||
let args = build_inner_seccomp_command(InnerSeccompCommandArgs {
|
||||
sandbox_policy_cwd: Path::new("/tmp"),
|
||||
command_cwd: Some(Path::new("/tmp/link")),
|
||||
permission_profile: &permission_profile,
|
||||
allow_network_for_proxy: true,
|
||||
proxy_route_spec: Some("{\"routes\":[]}".to_string()),
|
||||
command: vec!["/bin/true".to_string()],
|
||||
});
|
||||
for managed_network in [
|
||||
ManagedNetworkSandboxContext::default(),
|
||||
ManagedNetworkSandboxContext {
|
||||
loopback_ports: vec![8080, 9090],
|
||||
allow_local_binding: true,
|
||||
allow_unix_sockets: vec!["/tmp/daemon.sock".to_string()],
|
||||
dangerously_allow_all_unix_sockets: true,
|
||||
},
|
||||
] {
|
||||
let args = build_inner_seccomp_command(InnerSeccompCommandArgs {
|
||||
sandbox_policy_cwd: Path::new("/tmp"),
|
||||
command_cwd: Some(Path::new("/tmp/link")),
|
||||
permission_profile: &permission_profile,
|
||||
managed_network: Some(managed_network.clone()),
|
||||
proxy_route_spec: Some("{\"routes\":[]}".to_string()),
|
||||
command: vec!["/bin/true".to_string()],
|
||||
});
|
||||
|
||||
assert!(args.iter().any(|arg| arg == "--proxy-route-spec"));
|
||||
assert!(args.iter().any(|arg| arg == "{\"routes\":[]}"));
|
||||
assert!(args.iter().any(|arg| arg == "--proxy-route-spec"));
|
||||
assert!(args.iter().any(|arg| arg == "{\"routes\":[]}"));
|
||||
let parsed = LandlockCommand::try_parse_from(args)
|
||||
.expect("inner command should preserve the managed network policy");
|
||||
assert_eq!(parsed.managed_network, Some(managed_network));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn managed_network_policy_alone_enables_proxy_mode() {
|
||||
let parsed = LandlockCommand::try_parse_from([
|
||||
"codex-linux-sandbox",
|
||||
"--sandbox-policy-cwd",
|
||||
"/tmp",
|
||||
"--managed-network",
|
||||
"{}",
|
||||
"--",
|
||||
"/bin/true",
|
||||
])
|
||||
.expect("managed network context should enable proxy mode on its own");
|
||||
assert_eq!(
|
||||
parsed.managed_network,
|
||||
Some(ManagedNetworkSandboxContext::default())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_managed_network_policy_is_rejected() {
|
||||
let error = LandlockCommand::try_parse_from([
|
||||
"codex-linux-sandbox",
|
||||
"--sandbox-policy-cwd",
|
||||
"/tmp",
|
||||
"--managed-network",
|
||||
"{\"dangerouslyAllowAllUnixSockets\":\"true\"}",
|
||||
"--",
|
||||
"/bin/true",
|
||||
])
|
||||
.expect_err("managed network policy should reject a non-boolean grant");
|
||||
assert_eq!(error.kind(), clap::error::ErrorKind::ValueValidation);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -573,7 +621,7 @@ fn inner_command_includes_permission_profile_flag() {
|
||||
sandbox_policy_cwd: Path::new("/tmp"),
|
||||
command_cwd: Some(Path::new("/tmp/link")),
|
||||
permission_profile: &permission_profile,
|
||||
allow_network_for_proxy: false,
|
||||
managed_network: None,
|
||||
proxy_route_spec: None,
|
||||
command: vec!["/bin/true".to_string()],
|
||||
});
|
||||
@@ -592,12 +640,15 @@ fn non_managed_inner_command_omits_route_spec() {
|
||||
sandbox_policy_cwd: Path::new("/tmp"),
|
||||
command_cwd: Some(Path::new("/tmp/link")),
|
||||
permission_profile: &permission_profile,
|
||||
allow_network_for_proxy: false,
|
||||
managed_network: None,
|
||||
proxy_route_spec: None,
|
||||
command: vec!["/bin/true".to_string()],
|
||||
});
|
||||
|
||||
assert!(!args.iter().any(|arg| arg == "--proxy-route-spec"));
|
||||
let parsed = LandlockCommand::try_parse_from(args)
|
||||
.expect("unmanaged inner command should preserve ordinary sandbox mode");
|
||||
assert_eq!(parsed.managed_network, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -608,7 +659,7 @@ fn managed_proxy_inner_command_requires_route_spec() {
|
||||
sandbox_policy_cwd: Path::new("/tmp"),
|
||||
command_cwd: Some(Path::new("/tmp/link")),
|
||||
permission_profile: &permission_profile,
|
||||
allow_network_for_proxy: true,
|
||||
managed_network: Some(ManagedNetworkSandboxContext::default()),
|
||||
proxy_route_spec: None,
|
||||
command: vec!["/bin/true".to_string()],
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user