mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Avoid redundant filesystem sandbox path resolution (#42870)
## Why Preparing a filesystem sandbox could synchronously probe unrelated permission roots on the executor runtime thread and repeatedly resolve the same filesystem aliases while deriving writable roots. ## What changed - On Linux, leave permission-root alias resolution to the sandbox helper. - Filter effective permission entries once and cache their resolved paths for writable-root and read-only carveout construction. - Return early when the policy has no effective writable entries. GitOrigin-RevId: 305364173ca55f967c1701aad94d2374f78999d3
This commit is contained in:
1 parent
d2d5b70241
commit
3b2d9a69e6
2 files changed
+56
-17
No files matched your search
@@ -18,6 +18,7 @@ use codex_sandboxing::SandboxTransformRequest;
|
|||||||
use codex_sandboxing::SandboxType;
|
use codex_sandboxing::SandboxType;
|
||||||
use codex_sandboxing::SandboxablePreference;
|
use codex_sandboxing::SandboxablePreference;
|
||||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
use codex_utils_absolute_path::canonicalize_preserving_symlinks;
|
use codex_utils_absolute_path::canonicalize_preserving_symlinks;
|
||||||
use codex_utils_path_uri::PathUri;
|
use codex_utils_path_uri::PathUri;
|
||||||
#[cfg(any(windows, test))]
|
#[cfg(any(windows, test))]
|
||||||
@@ -110,6 +111,9 @@ impl FileSystemSandboxRunner {
|
|||||||
&helper_read_roots,
|
&helper_read_roots,
|
||||||
cwd.native.as_path(),
|
cwd.native.as_path(),
|
||||||
);
|
);
|
||||||
|
// Linux resolves aliases in the sandbox helper. Doing it here also probes
|
||||||
|
// unrelated permission roots synchronously on the executor's runtime thread.
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
normalize_file_system_policy_root_aliases(&mut file_system_policy);
|
normalize_file_system_policy_root_aliases(&mut file_system_policy);
|
||||||
let network_policy = NetworkSandboxPolicy::Restricted;
|
let network_policy = NetworkSandboxPolicy::Restricted;
|
||||||
let permission_profile = PermissionProfile::from_runtime_permissions_with_enforcement(
|
let permission_profile = PermissionProfile::from_runtime_permissions_with_enforcement(
|
||||||
@@ -248,6 +252,7 @@ fn add_helper_runtime_permissions(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
fn normalize_file_system_policy_root_aliases(file_system_policy: &mut FileSystemSandboxPolicy) {
|
fn normalize_file_system_policy_root_aliases(file_system_policy: &mut FileSystemSandboxPolicy) {
|
||||||
for entry in &mut file_system_policy.entries {
|
for entry in &mut file_system_policy.entries {
|
||||||
// Alias normalization uses this executor's filesystem; leave foreign
|
// Alias normalization uses this executor's filesystem; leave foreign
|
||||||
@@ -260,6 +265,7 @@ fn normalize_file_system_policy_root_aliases(file_system_policy: &mut FileSystem
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
fn normalize_top_level_alias(path: AbsolutePathBuf) -> AbsolutePathBuf {
|
fn normalize_top_level_alias(path: AbsolutePathBuf) -> AbsolutePathBuf {
|
||||||
let raw_path = path.to_path_buf();
|
let raw_path = path.to_path_buf();
|
||||||
for ancestor in raw_path.ancestors() {
|
for ancestor in raw_path.ancestors() {
|
||||||
|
|||||||
@@ -275,6 +275,11 @@ struct ResolvedFileSystemEntry {
|
|||||||
access: FileSystemAccessMode,
|
access: FileSystemAccessMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct PreparedFileSystemEntry<'a> {
|
||||||
|
entry: &'a ResolvedFileSystemEntry,
|
||||||
|
effective_path: AbsolutePathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
struct FileSystemSemanticSignature {
|
struct FileSystemSemanticSignature {
|
||||||
has_full_disk_read_access: bool,
|
has_full_disk_read_access: bool,
|
||||||
@@ -1489,18 +1494,47 @@ impl FileSystemSandboxPolicy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let resolved_entries = self.resolved_entries_with_cwd(cwd);
|
let resolved_entries = self.resolved_entries_with_cwd(cwd);
|
||||||
let writable_entries: Vec<AbsolutePathBuf> = resolved_entries
|
if !resolved_entries
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|entry| entry.access.can_write())
|
.any(|entry| entry.access.can_write())
|
||||||
.filter(|entry| self.can_write_local_path_with_cwd(entry.path.as_path(), cwd))
|
{
|
||||||
.map(|entry| entry.path.clone())
|
return Vec::new();
|
||||||
|
}
|
||||||
|
// Resolve precedence and filesystem aliases once per entry, rather than repeating
|
||||||
|
// that work for every writable root while collecting its read-only carveouts.
|
||||||
|
let effective_entries: Vec<&ResolvedFileSystemEntry> = resolved_entries
|
||||||
|
.iter()
|
||||||
|
.filter(|entry| {
|
||||||
|
entry.access.can_write()
|
||||||
|
== self.can_write_local_path_with_cwd(entry.path.as_path(), cwd)
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
if !effective_entries
|
||||||
|
.iter()
|
||||||
|
.any(|entry| entry.access.can_write())
|
||||||
|
{
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
let prepared_entries: Vec<PreparedFileSystemEntry<'_>> = effective_entries
|
||||||
|
.into_iter()
|
||||||
|
.map(|entry| PreparedFileSystemEntry {
|
||||||
|
entry,
|
||||||
|
effective_path: path_resolution.resolve(entry.path.clone()),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let writable_entries: Vec<&PreparedFileSystemEntry<'_>> = prepared_entries
|
||||||
|
.iter()
|
||||||
|
.filter(|entry| entry.entry.access.can_write())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let effective_cwd = AbsolutePathBuf::from_absolute_path(cwd)
|
||||||
|
.ok()
|
||||||
|
.map(|cwd| path_resolution.resolve(cwd));
|
||||||
|
|
||||||
dedup_absolute_paths(
|
dedup_absolute_paths(
|
||||||
writable_entries
|
writable_entries
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.map(|entry| entry.effective_path.clone())
|
||||||
.map(|root| path_resolution.resolve(root))
|
|
||||||
.collect(),
|
.collect(),
|
||||||
/*normalize_effective_paths*/ false,
|
/*normalize_effective_paths*/ false,
|
||||||
)
|
)
|
||||||
@@ -1516,13 +1550,12 @@ impl FileSystemSandboxPolicy {
|
|||||||
let preserve_raw_carveout_paths = root.as_path().parent().is_some();
|
let preserve_raw_carveout_paths = root.as_path().parent().is_some();
|
||||||
let raw_writable_roots: Vec<&AbsolutePathBuf> = writable_entries
|
let raw_writable_roots: Vec<&AbsolutePathBuf> = writable_entries
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|path| path_resolution.resolve((*path).clone()) == root)
|
.filter(|entry| entry.effective_path == root)
|
||||||
|
.map(|entry| &entry.entry.path)
|
||||||
.collect();
|
.collect();
|
||||||
let protected_metadata_names =
|
let protected_metadata_names =
|
||||||
protected_metadata_names_for_writable_root(self, &root, &raw_writable_roots, cwd);
|
protected_metadata_names_for_writable_root(self, &root, &raw_writable_roots, cwd);
|
||||||
let protect_missing_dot_codex = AbsolutePathBuf::from_absolute_path(cwd)
|
let protect_missing_dot_codex = effective_cwd.as_ref() == Some(&root);
|
||||||
.ok()
|
|
||||||
.is_some_and(|cwd| path_resolution.resolve(cwd) == root);
|
|
||||||
let mut read_only_subpaths: Vec<AbsolutePathBuf> =
|
let mut read_only_subpaths: Vec<AbsolutePathBuf> =
|
||||||
default_read_only_subpaths_for_writable_root(&root, protect_missing_dot_codex)
|
default_read_only_subpaths_for_writable_root(&root, protect_missing_dot_codex)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -1536,12 +1569,12 @@ impl FileSystemSandboxPolicy {
|
|||||||
// Example: if `<root>/.codex -> <root>/decoy`, bwrap must still see
|
// Example: if `<root>/.codex -> <root>/decoy`, bwrap must still see
|
||||||
// `<root>/.codex`, not only the resolved `<root>/decoy`.
|
// `<root>/.codex`, not only the resolved `<root>/decoy`.
|
||||||
read_only_subpaths.extend(
|
read_only_subpaths.extend(
|
||||||
resolved_entries
|
prepared_entries
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|entry| !entry.access.can_write())
|
.filter(|entry| !entry.entry.access.can_write())
|
||||||
.filter(|entry| !self.can_write_local_path_with_cwd(entry.path.as_path(), cwd))
|
.filter_map(|prepared| {
|
||||||
.filter_map(|entry| {
|
let entry = prepared.entry;
|
||||||
let effective_path = path_resolution.resolve(entry.path.clone());
|
let effective_path = &prepared.effective_path;
|
||||||
// Preserve the literal in-root path whenever the
|
// Preserve the literal in-root path whenever the
|
||||||
// carveout itself lives under this writable root, even
|
// carveout itself lives under this writable root, even
|
||||||
// if following symlinks would resolve back to the root
|
// if following symlinks would resolve back to the root
|
||||||
@@ -1577,13 +1610,13 @@ impl FileSystemSandboxPolicy {
|
|||||||
return Some(raw_carveout_path);
|
return Some(raw_carveout_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if effective_path == root
|
if effective_path == &root
|
||||||
|| !effective_path.as_path().starts_with(root.as_path())
|
|| !effective_path.as_path().starts_with(root.as_path())
|
||||||
{
|
{
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(effective_path)
|
Some(effective_path.clone())
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
WritableRoot {
|
WritableRoot {
|
||||||
|
|||||||
Reference in new issue
Block a user