Require a resolved filesystem policy before probing Bubblewrap (#64422)

## Why

Codex checks candidate Bubblewrap executables before sandbox confinement.
At that point, the metadata protections applied by the sandbox are not
yet in effect.

## What changed

Evaluate candidate paths against configured filesystem restrictions
without counting sandbox-applied metadata protections or discovered Git
metadata carveouts. Skip executable lookup when permission resolution
fails.

## Testing

Extend the managed-network regression test to verify that writable
Bubblewrap candidates are skipped with a legacy Git metadata carveout
or a metadata resolution error.

GitOrigin-RevId: 17045604caadfb41cac78a252cf3a519b7149aed
This commit is contained in:
viyatb-oai
2026-11-28 05:03:13 +00:00
committed by copyberry
parent 8cae463a69
commit 965ee845b1
5 changed files with 108 additions and 28 deletions
@@ -94,7 +94,7 @@ fn child_workdir_does_not_probe_or_launch_writable_parent_bwrap() {
}
#[test]
fn full_disk_write_with_managed_network_does_not_probe_writable_bwrap() {
fn managed_network_does_not_probe_writable_bwrap() {
let temp = tempfile::tempdir_in(std::env::current_dir().unwrap()).unwrap();
let bin = temp.path().join("bin");
let cwd = temp.path().join("cwd");
@@ -123,7 +123,7 @@ fn full_disk_write_with_managed_network_does_not_probe_writable_bwrap() {
.unwrap()
};
// Confirm this host reaches the capability probe without needing working
// namespaces or bundled bubblewrap. Both invocations use managed networking.
// namespaces or bundled bubblewrap.
let control_marker = temp.path().join("control");
let control = run(&PermissionProfile::read_only(), &control_marker);
assert!(
@@ -137,4 +137,41 @@ fn full_disk_write_with_managed_network_does_not_probe_writable_bwrap() {
!marker.exists(),
"writable bwrap ran before network confinement: {output:?}"
);
std::fs::write(temp.path().join(".git"), "gitdir: bin\n").unwrap();
let legacy = PermissionProfile::workspace_write_with(
&[AbsolutePathBuf::try_from(temp.path()).unwrap()],
NetworkSandboxPolicy::Restricted,
/*exclude_tmpdir_env_var*/ true,
/*exclude_slash_tmp*/ true,
)
.materialize_project_roots_with_workspace_roots(&[
AbsolutePathBuf::try_from(cwd.as_path()).unwrap()
]);
let output = run(&legacy, &marker);
assert!(
!marker.exists(),
"legacy gitfile carveout authorized its own bwrap: {output:?}"
);
std::fs::remove_file(temp.path().join(".git")).unwrap();
// A failed metadata walk must not make a writable PATH look read-only.
let mut link = temp.path().join(".codex");
for i in 0..48 {
let next = temp.path().join(format!("hop-{i}"));
std::os::unix::fs::symlink(&next, &link).unwrap();
link = next;
}
std::fs::create_dir(&link).unwrap();
let policy = PermissionProfile::read_only()
.file_system_sandbox_policy()
.with_additional_writable_roots(&cwd, &[AbsolutePathBuf::try_from(temp.path()).unwrap()]);
let profile =
PermissionProfile::from_runtime_permissions(&policy, NetworkSandboxPolicy::Restricted);
let marker = temp.path().join("unresolved");
let output = run(&profile, &marker);
assert!(
!marker.exists(),
"unresolved writable bwrap was probed: {output:?}"
);
}
+18 -19
View File
@@ -1474,18 +1474,6 @@ impl FileSystemSandboxPolicy {
self.get_writable_roots_with_cwd_impl(cwd, FileSystemPolicyProjection::Native)
}
/// Omits redundant root-metadata mounts when the filesystem root is writable.
///
/// Explicit root metadata writes still disable their default protection. Linux
/// inherits those writes from its root bind so a metadata symlink cannot cause
/// a second bind to reopen its target under an explicitly denied directory.
pub fn get_writable_roots_with_cwd_inheriting_root_metadata(
&self,
cwd: &Path,
) -> Vec<WritableRoot> {
self.get_writable_roots_with_cwd_impl(cwd, FileSystemPolicyProjection::LinuxMounts)
}
/// Reports full-disk writes when every explicit writable path can be projected locally.
/// Preserves the legacy fallback used by startup checks and diagnostic labels,
/// using this host's path convention.
@@ -1615,6 +1603,7 @@ impl FileSystemSandboxPolicy {
cfg!(target_os = "linux")
}
FileSystemPolicyProjection::PreserveMutableRoots => cfg!(target_os = "macos"),
FileSystemPolicyProjection::PreSandboxExecutable => false,
};
let resolved_gitdir_entries: Vec<ResolvedFileSystemEntry> = if include_resolved_gitdirs {
effective_entries
@@ -1676,7 +1665,7 @@ impl FileSystemSandboxPolicy {
.ok()
.map(|cwd| projection.resolve_writable_path(cwd));
dedup_absolute_paths(
let mut roots: Vec<WritableRoot> = dedup_absolute_paths(
writable_entries
.iter()
.map(|entry| entry.effective_path.clone())
@@ -1701,11 +1690,15 @@ impl FileSystemSandboxPolicy {
let protected_metadata_names =
protected_metadata_names_for_writable_root(matching, &root, &raw_writable_roots);
let protect_missing_dot_codex = effective_cwd.as_ref() == Some(&root);
let mut read_only_subpaths: Vec<AbsolutePathBuf> =
default_read_only_subpaths_for_writable_root(&root, protect_missing_dot_codex)
.into_iter()
.filter(|path| !has_explicit_resolved_path_entry(resolved_entries, path))
.collect();
let mut read_only_subpaths =
if projection == FileSystemPolicyProjection::PreSandboxExecutable {
Vec::new()
} else {
default_read_only_subpaths_for_writable_root(&root, protect_missing_dot_codex)
.into_iter()
.filter(|path| !has_explicit_resolved_path_entry(resolved_entries, path))
.collect()
};
// Narrower explicit non-write entries carve out broader writable roots.
// More specific write entries still remain writable because they appear
// as separate WritableRoot values and are checked independently.
@@ -1776,7 +1769,13 @@ impl FileSystemSandboxPolicy {
),
}
})
.collect()
.collect();
if projection == FileSystemPolicyProjection::PreSandboxExecutable {
for root in &mut roots {
root.protected_metadata_names.clear();
}
}
roots
}
/// Returns explicit unreadable roots resolved against the provided cwd.
@@ -1,5 +1,7 @@
//! Projects filesystem permissions for each sandbox, preserving mutable paths where the backend binds them.
use super::FileSystemAccessMode;
use super::FileSystemSandboxEntry;
use super::FileSystemSandboxPolicy;
use super::LocalPolicyContext;
use super::normalize_effective_absolute_path;
@@ -17,12 +19,16 @@ pub enum FileSystemPolicyProjection {
PreserveMutableRoots,
/// Inherit root metadata writes from the root mount and retain literal paths for read denials.
LinuxMounts,
/// Only configured restrictions can make a host executable eligible to run before confinement.
PreSandboxExecutable,
}
impl FileSystemPolicyProjection {
pub(super) fn resolve_writable_path(self, path: AbsolutePathBuf) -> AbsolutePathBuf {
match self {
Self::Native | Self::LinuxMounts => normalize_effective_absolute_path(path),
Self::Native | Self::LinuxMounts | Self::PreSandboxExecutable => {
normalize_effective_absolute_path(path)
}
Self::PreserveMutableRoots => path.normalize_system_aliases().unwrap_or(path),
}
}
@@ -50,6 +56,28 @@ impl FileSystemSandboxPolicy {
&self,
cwd: &Path,
projection: FileSystemPolicyProjection,
) -> io::Result<ResolvedFileSystemPermissions> {
// Legacy workspace policies encode discovered gitdirs as optional read entries. A
// repository cannot use one of those carveouts to authorize its own host-side helper.
let mut policy = std::borrow::Cow::Borrowed(self);
let optional_read = |entry: &FileSystemSandboxEntry| {
entry.access == FileSystemAccessMode::Read && entry.skips_missing_path()
};
if projection == FileSystemPolicyProjection::PreSandboxExecutable
&& self.entries.iter().any(optional_read)
{
policy
.to_mut()
.entries
.retain(|entry| !optional_read(entry));
}
policy.resolve_for_enforcement_impl(cwd, projection)
}
fn resolve_for_enforcement_impl(
&self,
cwd: &Path,
projection: FileSystemPolicyProjection,
) -> io::Result<ResolvedFileSystemPermissions> {
let mut resolved = ResolvedFileSystemPermissions {
has_full_disk_read_access: self.has_full_disk_read_access(),
@@ -159,7 +159,16 @@ fn linux_root_metadata_writes_inherit_the_root_without_reopening_other_paths() {
Deny,
),
]);
let roots = policy.get_writable_roots_with_cwd_inheriting_root_metadata(&cwd);
let linux_roots = |policy: &FileSystemSandboxPolicy| {
policy
.resolve_for_enforcement(
&cwd,
crate::permissions::FileSystemPolicyProjection::LinuxMounts,
)
.unwrap()
.writable_roots
};
let roots = linux_roots(&policy);
assert_eq!(
roots[0].protected_metadata_names,
PROTECTED_METADATA_PATH_NAMES
@@ -173,7 +182,7 @@ fn linux_root_metadata_writes_inherit_the_root_without_reopening_other_paths() {
.map(|name| entry(root.join_descendant(name).unwrap(), Write))
.collect::<Vec<_>>();
policy.entries.extend(metadata.clone());
let roots = policy.get_writable_roots_with_cwd_inheriting_root_metadata(&cwd);
let roots = linux_roots(&policy);
assert_eq!(
roots
.iter()
@@ -185,7 +194,7 @@ fn linux_root_metadata_writes_inherit_the_root_without_reopening_other_paths() {
let standalone = FileSystemSandboxPolicy::restricted(metadata);
assert_eq!(
standalone.get_writable_roots_with_cwd_inheriting_root_metadata(&cwd),
linux_roots(&standalone),
standalone.get_writable_roots_with_cwd(&cwd),
);
}
+10 -3
View File
@@ -1,5 +1,6 @@
use crate::policy_transforms::should_require_platform_sandbox;
use codex_protocol::models::PermissionProfile;
use codex_protocol::permissions::FileSystemPolicyProjection;
use codex_protocol::permissions::FileSystemSandboxPolicy;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::ffi::CString;
@@ -238,9 +239,15 @@ pub fn find_executable_in_search_paths(
let cwd = std::fs::canonicalize(cwd).unwrap_or_else(|_| cwd.to_path_buf());
let cwd_is_root = cwd.parent().is_none();
// Full-disk policies intentionally have no enumerated writable roots.
let full_disk_write = file_system_policy.has_full_disk_write_access();
let writable_roots = file_system_policy
.get_writable_roots_with_cwd_inheriting_root_metadata(sandbox_policy_cwd)
let resolved = file_system_policy
.resolve_for_enforcement(
sandbox_policy_cwd,
FileSystemPolicyProjection::PreSandboxExecutable,
)
.ok()?;
let full_disk_write = resolved.has_full_disk_write_access;
let writable_roots = resolved
.writable_roots
.into_iter()
.map(|mut root| {
if let Ok(canonical) = std::fs::canonicalize(&root.root)