Isolate app-server Unix sockets from filesystem-restricted commands (#45984)

## Why

Network access and Unix-socket allowlists must not let commands with filesystem restrictions reach the privileged app-server RPC transport.

## What changed

- Bind Unix control sockets in a fixed, user-owned directory with mode `0700`, independent of environment settings, and expose the advertised paths as symlinks. Preserve existing parent permissions, reject unsafe parents, and serialize socket setup and publication.
- Mask the socket directory in Linux bubblewrap sandboxes after each bind that exposes it. Reject host mount aliases and nested mounts that compromise isolation.
- Deny access to the directory and outbound connections to its sockets in macOS Seatbelt policies, including when network access or Unix-socket allowlists grant broader access.
- Require bubblewrap for filesystem-restricted Linux execution. Users with `features.use_legacy_landlock` enabled must disable it for these policies.

## Testing

Add regression coverage for direct and symlink socket access, hardlink attempts, Linux host-process links and bind-mount aliases, and continued use of unrelated and sandbox-local sockets. Add transport coverage for parent permissions, concurrent restart after a stale symlink, and cleanup that preserves a replacement at the advertised path.

GitOrigin-RevId: 53372c27eea278d964f2cf68aed24323ef3b7082
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-16 16:56:41 +00:00
1 parent 0666c12e78
commit 49305d74b4
25 files changed
+828 -191

No files matched your search

+16
View File
@@ -1040,6 +1040,18 @@ pub(crate) fn create_seatbelt_command_args_with_profile(
file_write_policy,
network_policy,
];
// Network grants and Unix-socket allowlists must never reopen the
// privileged app-server RPC transport to filesystem-restricted commands.
if !file_system_sandbox_policy.has_full_disk_write_access() {
let directory = codex_uds::shared_daemon_socket_directory()
.map_err(|error| SeatbeltPreparationError::FileSystem(error.to_string()))?;
let directory = serde_json::to_string(&directory.to_string_lossy())
.map_err(|error| SeatbeltPreparationError::FileSystem(error.to_string()))?;
policy_sections.push(format!(
"(deny file-read* file-write* (subpath {directory}))\n\
(deny network-outbound (remote unix-socket (subpath {directory})))"
));
}
if file_system_sandbox_policy.has_full_disk_read_access() {
policy_sections.push(MACOS_SEATBELT_PREFERENCES_POLICY.to_string());
}
@@ -1086,3 +1098,7 @@ pub(crate) fn create_seatbelt_command_args_with_profile(
#[cfg(test)]
#[path = "seatbelt_tests.rs"]
mod tests;
#[cfg(test)]
#[path = "seatbelt_daemon_socket_tests.rs"]
mod daemon_socket_tests;
@@ -0,0 +1,86 @@
use super::CreateSeatbeltCommandArgsParams;
use super::MACOS_PATH_TO_SEATBELT_EXECUTABLE;
use super::create_seatbelt_command_args;
use codex_protocol::permissions::FileSystemSandboxPolicy;
use codex_protocol::permissions::NetworkSandboxPolicy;
use codex_utils_absolute_path::AbsolutePathBuf;
use pretty_assertions::assert_eq;
use std::os::unix::net::UnixListener;
use std::os::unix::net::UnixStream;
use std::process::Command;
#[test]
fn daemon_sockets_are_denied_despite_network_and_tmp_write_grants() {
let root = codex_uds::prepare_shared_daemon_socket_directory().unwrap();
let private = tempfile::tempdir_in(&root).unwrap();
let workspace = tempfile::tempdir().unwrap();
let socket = private.path().join("rpc.sock");
let alias = workspace.path().join("alias.sock");
let unrelated = workspace.path().join("other.sock");
let _daemon = UnixListener::bind(&socket).unwrap();
let _other = UnixListener::bind(&unrelated).unwrap();
std::os::unix::fs::symlink(&socket, &alias).unwrap();
UnixStream::connect(&alias).expect("host can reach daemon");
let script = r#"
import os, socket, sys
for path in sys.argv[1:3]:
try:
socket.socket(socket.AF_UNIX).connect(path)
except OSError:
pass
else:
raise AssertionError('daemon reachable: ' + path)
try:
os.link(sys.argv[1], sys.argv[4] + '/hardlink')
except OSError:
pass
else:
raise AssertionError('daemon hardlink created')
socket.socket(socket.AF_UNIX).connect(sys.argv[3])
s = socket.socket(socket.AF_UNIX)
s.bind(sys.argv[4] + '/local.sock')
s.listen(1)
c = socket.socket(socket.AF_UNIX)
c.connect(sys.argv[4] + '/local.sock')
p, _ = s.accept()
c.sendall(b'ok')
assert p.recv(2) == b'ok'
"#;
let command = vec![
"/usr/bin/python3".to_string(),
"-c".to_string(),
script.to_string(),
socket.display().to_string(),
alias.display().to_string(),
unrelated.display().to_string(),
workspace.path().display().to_string(),
];
let policy = FileSystemSandboxPolicy::workspace_write(
&[AbsolutePathBuf::from_absolute_path("/tmp").unwrap()],
/*exclude_tmpdir_env_var*/ false,
/*exclude_slash_tmp*/ false,
);
let args = create_seatbelt_command_args(CreateSeatbeltCommandArgsParams {
command,
file_system_sandbox_policy: &policy,
network_sandbox_policy: NetworkSandboxPolicy::Enabled,
sandbox_policy_cwd: workspace.path(),
enforce_managed_network: false,
managed_network: None,
environment_id: None,
network: None,
extra_allow_unix_sockets: &[AbsolutePathBuf::from_absolute_path(root).unwrap()],
})
.unwrap();
let output = Command::new(MACOS_PATH_TO_SEATBELT_EXECUTABLE)
.args(args)
.env("CODEX_HOME", workspace.path())
.output()
.unwrap();
assert_eq!(
output.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}