mirror of
https://github.com/openai/codex.git
synced 2026-11-29 00:00:54 +00:00
Seal synthetic mounts in read-restricted Linux sandboxes (#64398)
## Why Read-restricted sandboxes build a synthetic filesystem around paths granted by policy. Leaving the backing mounts writable lets commands modify synthetic directories outside explicitly writable paths. ## What changed Remount the synthetic root, minimal `/dev`, and applicable WSL masks read-only after bubblewrap finishes setting up mounts. The remounts are nonrecursive, so explicitly writable child mounts stay writable. ## Testing Extend the sandbox regression test to check that commands can read an approved file through a symlink, write to `/dev/null`, and write to an allowed subdirectory under a denied parent. The test also checks that commands cannot rename the synthetic directory containing the approved file or create an ungranted directory under `/dev`. GitOrigin-RevId: e2ddc6c4a215d052adf12e2644480a2eb9c72640
This commit is contained in:
@@ -338,19 +338,6 @@ fn create_bwrap_flags(
|
||||
args.push("--new-session".to_string());
|
||||
args.push("--die-with-parent".to_string());
|
||||
args.extend(filesystem_args);
|
||||
if options.mask_wsl_interop {
|
||||
// WSL's Windows-process bridge uses Unix sockets under /run/WSL.
|
||||
// Mask them after all filesystem binds so network access cannot bypass
|
||||
// the Linux filesystem policy by launching an unrestricted Windows process.
|
||||
args.push("--tmpfs".to_string());
|
||||
args.push(WSL_INTEROP_DIR.to_string());
|
||||
}
|
||||
if (options.mask_wsl_interop || options.mask_wslg_distro) && !options.mount_proc {
|
||||
// Without a fresh procfs, the root bind retains the host procfs.
|
||||
// Hide it so host process roots cannot restore the masked WSL views.
|
||||
args.push("--tmpfs".to_string());
|
||||
args.push("/proc".to_string());
|
||||
}
|
||||
// Request a user namespace explicitly rather than relying on bubblewrap's
|
||||
// auto-enable behavior, which is skipped when the caller runs as uid 0.
|
||||
// This also blocks host procfs root/cwd/fd links through ptrace permission
|
||||
@@ -363,11 +350,6 @@ fn create_bwrap_flags(
|
||||
if options.network_mode.should_unshare_network() {
|
||||
args.push("--unshare-net".to_string());
|
||||
}
|
||||
// Mount a fresh /proc unless the caller explicitly disables it.
|
||||
if options.mount_proc {
|
||||
args.push("--proc".to_string());
|
||||
args.push("/proc".to_string());
|
||||
}
|
||||
let normalized_command_cwd = if options.require_command_cwd {
|
||||
command_cwd.to_path_buf()
|
||||
} else {
|
||||
@@ -491,6 +473,7 @@ fn create_filesystem_args(
|
||||
unreadable_roots.sort();
|
||||
unreadable_roots.dedup();
|
||||
|
||||
let mut synthetic_roots = Vec::new();
|
||||
let mut args = if resolved.has_full_disk_read_access {
|
||||
// Read-only root, then mount a minimal device tree.
|
||||
// In bubblewrap (`bubblewrap.c`, `SETUP_MOUNT_DEV`), `--dev /dev`
|
||||
@@ -545,6 +528,7 @@ fn create_filesystem_args(
|
||||
"/dev".to_string(),
|
||||
];
|
||||
} else {
|
||||
synthetic_roots.extend([Path::new("/dev"), Path::new("/")]);
|
||||
for root in readable_roots {
|
||||
if !root.exists() {
|
||||
continue;
|
||||
@@ -561,6 +545,7 @@ fn create_filesystem_args(
|
||||
} else {
|
||||
root
|
||||
};
|
||||
synthetic_roots.retain(|path| !path.starts_with(&mount_root));
|
||||
args.push("--ro-bind".to_string());
|
||||
args.push(path_to_string(&mount_root));
|
||||
args.push(path_to_string(&mount_root));
|
||||
@@ -570,7 +555,8 @@ fn create_filesystem_args(
|
||||
|
||||
args
|
||||
};
|
||||
if args[0] == "--ro-bind" {
|
||||
let restricted_reads = args[0] == "--tmpfs";
|
||||
if !restricted_reads {
|
||||
append_daemon_socket_masks(&mut args, Path::new("/"), &daemon_directories)?;
|
||||
}
|
||||
let mut bwrap_args = BwrapArgs {
|
||||
@@ -634,6 +620,7 @@ fn create_filesystem_args(
|
||||
let redundant_root_alias =
|
||||
binds_file_system_root && root != Path::new("/") && mount_root == Path::new("/");
|
||||
if !redundant_root_alias {
|
||||
synthetic_roots.retain(|path| !path.starts_with(mount_root));
|
||||
bwrap_args.args.push("--bind".to_string());
|
||||
bwrap_args.args.push(path_to_string(mount_root));
|
||||
bwrap_args.args.push(path_to_string(mount_root));
|
||||
@@ -786,6 +773,42 @@ fn create_filesystem_args(
|
||||
]);
|
||||
}
|
||||
|
||||
// Directory deny masks are already sealed. A mask over / also hides /dev.
|
||||
synthetic_roots.retain(|path| {
|
||||
!unreadable_roots
|
||||
.iter()
|
||||
.any(|denied| path.starts_with(denied))
|
||||
});
|
||||
if options.mask_wsl_interop {
|
||||
// WSL's Windows-process bridge uses Unix sockets under /run/WSL.
|
||||
// Mask them after all filesystem binds so network access cannot bypass
|
||||
// the Linux filesystem policy by launching an unrestricted Windows process.
|
||||
bwrap_args.args.push("--tmpfs".to_string());
|
||||
bwrap_args.args.push(WSL_INTEROP_DIR.to_string());
|
||||
if restricted_reads {
|
||||
synthetic_roots.push(Path::new(WSL_INTEROP_DIR));
|
||||
}
|
||||
}
|
||||
if (options.mask_wsl_interop || options.mask_wslg_distro) && !options.mount_proc {
|
||||
// Without a fresh procfs, the root bind retains the host procfs.
|
||||
// Hide it so host process roots cannot restore the masked WSL views.
|
||||
bwrap_args.args.push("--tmpfs".to_string());
|
||||
bwrap_args.args.push("/proc".to_string());
|
||||
if restricted_reads {
|
||||
synthetic_roots.push(Path::new("/proc"));
|
||||
}
|
||||
}
|
||||
// Mount a fresh /proc unless the caller explicitly disables it.
|
||||
if options.mount_proc {
|
||||
bwrap_args.args.push("--proc".to_string());
|
||||
bwrap_args.args.push("/proc".to_string());
|
||||
}
|
||||
// Bubblewrap must finish creating mount points before sealing their backing
|
||||
// directories. --remount-ro is nonrecursive, so writable child mounts survive.
|
||||
for path in synthetic_roots {
|
||||
bwrap_args.args.push("--remount-ro".to_string());
|
||||
bwrap_args.args.push(path_to_string(path));
|
||||
}
|
||||
Ok(bwrap_args)
|
||||
}
|
||||
|
||||
|
||||
@@ -1584,6 +1584,11 @@ async fn sandbox_reenables_writable_subpaths_under_unreadable_parents() {
|
||||
let allowed = blocked.join("allowed");
|
||||
std::fs::create_dir_all(&allowed).expect("create blocked/allowed dir");
|
||||
let allowed_target = allowed.join("note.txt");
|
||||
let sources = tempfile::tempdir_in("/tmp").expect("source directory");
|
||||
let approved = sources.path().join("approved.txt");
|
||||
std::fs::write(&approved, "approved").expect("write source file");
|
||||
let logical = allowed.join("logical");
|
||||
std::os::unix::fs::symlink(&approved, &logical).expect("link to source");
|
||||
// These tests bypass the usual legacy-policy bridge, so explicitly keep
|
||||
// the sandbox helper binary and minimal runtime paths readable.
|
||||
let sandbox_helper_dir = codex_linux_sandbox_exe()
|
||||
@@ -1592,6 +1597,12 @@ async fn sandbox_reenables_writable_subpaths_under_unreadable_parents() {
|
||||
.to_path_buf();
|
||||
|
||||
let file_system_sandbox_policy = FileSystemSandboxPolicy::restricted(vec![
|
||||
FileSystemSandboxEntry::new(
|
||||
AbsolutePathBuf::try_from(approved.as_path())
|
||||
.unwrap()
|
||||
.into(),
|
||||
FileSystemAccessMode::Read,
|
||||
),
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::Minimal,
|
||||
@@ -1644,11 +1655,19 @@ async fn sandbox_reenables_writable_subpaths_under_unreadable_parents() {
|
||||
&[
|
||||
"bash",
|
||||
"-lc",
|
||||
&format!(
|
||||
"printf allowed > {} && cat {}",
|
||||
allowed_target.to_string_lossy(),
|
||||
allowed_target.to_string_lossy()
|
||||
),
|
||||
r#"
|
||||
set -eu
|
||||
[ "$(cat "$2")" = approved ]
|
||||
if mv "$3" "$3.moved"; then exit 1; fi
|
||||
if mkdir /dev/ungranted; then exit 1; fi
|
||||
printf x > /dev/null
|
||||
printf allowed > "$1"
|
||||
cat "$1"
|
||||
"#,
|
||||
"bash",
|
||||
allowed_target.to_str().unwrap(),
|
||||
logical.to_str().unwrap(),
|
||||
sources.path().to_str().unwrap(),
|
||||
],
|
||||
permission_profile,
|
||||
LONG_TIMEOUT_MS,
|
||||
|
||||
Reference in New Issue
Block a user