mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
chore: merge origin/main into symlink hardening
Co-authored-by: Codex noreply@openai.com
This commit is contained in:
@@ -177,11 +177,12 @@ fn find_system_bwrap_in_search_paths(
|
||||
) -> Option<PathBuf> {
|
||||
let search_path = std::env::join_paths(search_paths).ok()?;
|
||||
let cwd = std::fs::canonicalize(cwd).unwrap_or_else(|_| cwd.to_path_buf());
|
||||
let cwd_is_root = cwd.parent().is_none();
|
||||
which::which_in_all(SYSTEM_BWRAP_PROGRAM, Some(search_path), &cwd)
|
||||
.ok()?
|
||||
.find_map(|path| {
|
||||
let path = std::fs::canonicalize(path).ok()?;
|
||||
if path.starts_with(&cwd) {
|
||||
if !cwd_is_root && path.starts_with(&cwd) {
|
||||
None
|
||||
} else {
|
||||
Some(path)
|
||||
|
||||
@@ -149,6 +149,20 @@ fn skips_workspace_local_bwrap_in_joined_search_path() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_cwd_does_not_hide_system_bwrap_candidates() {
|
||||
let temp_dir = tempdir().expect("temp dir");
|
||||
let bin_dir = temp_dir.path().join("bin");
|
||||
std::fs::create_dir_all(&bin_dir).expect("create bin dir");
|
||||
let expected_bwrap = write_named_fake_bwrap_in(&bin_dir);
|
||||
let search_path = std::env::join_paths([bin_dir]).expect("join search path");
|
||||
|
||||
assert_eq!(
|
||||
find_system_bwrap_in_search_paths(std::env::split_paths(&search_path), Path::new("/")),
|
||||
Some(expected_bwrap)
|
||||
);
|
||||
}
|
||||
|
||||
fn write_fake_bwrap(contents: &str) -> tempfile::TempPath {
|
||||
write_fake_bwrap_in(
|
||||
&std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
|
||||
|
||||
@@ -183,7 +183,7 @@ fn transform_additional_permissions_preserves_denied_entries() {
|
||||
path: FileSystemPath::Path {
|
||||
path: denied_path.clone(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
]);
|
||||
let permissions = PermissionProfile::from_runtime_permissions(
|
||||
@@ -228,7 +228,7 @@ fn transform_additional_permissions_preserves_denied_entries() {
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: denied_path },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: allowed_path },
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn normalize_additional_permissions(
|
||||
let glob_scan_max_depth = file_system.glob_scan_max_depth;
|
||||
for entry in file_system.entries {
|
||||
if matches!(&entry.path, FileSystemPath::GlobPattern { .. })
|
||||
&& entry.access != FileSystemAccessMode::None
|
||||
&& entry.access != FileSystemAccessMode::Deny
|
||||
{
|
||||
return Err(
|
||||
"glob file system permissions only support deny-read entries".to_string(),
|
||||
@@ -221,7 +221,7 @@ fn effective_glob_scan_depth(
|
||||
entries
|
||||
.iter()
|
||||
.any(|entry| {
|
||||
entry.access == FileSystemAccessMode::None
|
||||
entry.access == FileSystemAccessMode::Deny
|
||||
&& matches!(&entry.path, FileSystemPath::GlobPattern { .. })
|
||||
})
|
||||
.then_some(match depth {
|
||||
@@ -273,7 +273,7 @@ fn retain_constraining_deny_entries(
|
||||
let mut retained_entries = Vec::new();
|
||||
for entry in source_entries
|
||||
.iter()
|
||||
.filter(|entry| entry.access == FileSystemAccessMode::None)
|
||||
.filter(|entry| entry.access == FileSystemAccessMode::Deny)
|
||||
{
|
||||
if !deny_entry_constrains_accepted_grant(entry, accepted_entries, cwd) {
|
||||
continue;
|
||||
@@ -340,7 +340,7 @@ fn access_covers(requested: FileSystemAccessMode, granted: FileSystemAccessMode)
|
||||
match granted {
|
||||
FileSystemAccessMode::Read => requested.can_read(),
|
||||
FileSystemAccessMode::Write => requested.can_write(),
|
||||
FileSystemAccessMode::None => false,
|
||||
FileSystemAccessMode::Deny => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ fn root_write_policy_with_carveouts_still_uses_platform_sandbox() {
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: blocked },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
]);
|
||||
|
||||
@@ -187,7 +187,7 @@ fn normalize_additional_permissions_preserves_deny_globs() {
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
}],
|
||||
glob_scan_max_depth: std::num::NonZeroUsize::new(2),
|
||||
}),
|
||||
@@ -203,7 +203,7 @@ fn normalize_additional_permissions_preserves_deny_globs() {
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
}],
|
||||
glob_scan_max_depth: std::num::NonZeroUsize::new(2),
|
||||
}),
|
||||
@@ -427,7 +427,7 @@ fn intersect_permission_profiles_materializes_cwd_deny_entries() {
|
||||
path: FileSystemPath::Special {
|
||||
value: FileSystemSpecialPath::project_roots(/*subpath*/ None),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
],
|
||||
glob_scan_max_depth: None,
|
||||
@@ -448,7 +448,7 @@ fn intersect_permission_profiles_materializes_cwd_deny_entries() {
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: request_cwd },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
],
|
||||
glob_scan_max_depth: None,
|
||||
@@ -480,7 +480,7 @@ fn intersect_permission_profiles_drops_deny_entries_without_filesystem_grants()
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: secret },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
],
|
||||
glob_scan_max_depth: None,
|
||||
@@ -520,7 +520,7 @@ fn intersect_permission_profiles_rejects_concrete_grants_matched_by_requested_de
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
],
|
||||
glob_scan_max_depth: std::num::NonZeroUsize::new(2),
|
||||
@@ -558,7 +558,7 @@ fn intersect_permission_profiles_materializes_relative_deny_globs_for_reuse() {
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
};
|
||||
let permissions = PermissionProfile {
|
||||
file_system: Some(FileSystemPermissions {
|
||||
@@ -586,7 +586,7 @@ fn intersect_permission_profiles_materializes_relative_deny_globs_for_reuse() {
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: request_cwd.join("**/*.env").to_string_lossy().into_owned(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
],
|
||||
glob_scan_max_depth: std::num::NonZeroUsize::new(2),
|
||||
@@ -657,7 +657,7 @@ fn intersect_permission_profiles_uses_granted_bounded_glob_scan_depth() {
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
};
|
||||
let requested = PermissionProfile {
|
||||
file_system: Some(FileSystemPermissions {
|
||||
@@ -689,7 +689,7 @@ fn intersect_permission_profiles_uses_granted_bounded_glob_scan_depth() {
|
||||
.to_string_lossy()
|
||||
.into_owned(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
],
|
||||
glob_scan_max_depth: std::num::NonZeroUsize::new(4),
|
||||
@@ -712,7 +712,7 @@ fn intersect_permission_profiles_uses_granted_unbounded_glob_scan_depth() {
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
};
|
||||
let requested = PermissionProfile {
|
||||
file_system: Some(FileSystemPermissions {
|
||||
@@ -744,7 +744,7 @@ fn intersect_permission_profiles_uses_granted_unbounded_glob_scan_depth() {
|
||||
.to_string_lossy()
|
||||
.into_owned(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
],
|
||||
glob_scan_max_depth: None,
|
||||
@@ -775,7 +775,7 @@ fn merge_file_system_policy_with_additional_permissions_preserves_unreadable_roo
|
||||
path: FileSystemPath::Path {
|
||||
path: denied_path.clone(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
]),
|
||||
&FileSystemPermissions::from_read_write_roots(
|
||||
@@ -787,7 +787,7 @@ fn merge_file_system_policy_with_additional_permissions_preserves_unreadable_roo
|
||||
assert_eq!(
|
||||
merged_policy.entries.contains(&FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: denied_path },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
}),
|
||||
true
|
||||
);
|
||||
@@ -806,7 +806,7 @@ fn merge_file_system_policy_with_additional_permissions_carries_bounded_glob_sca
|
||||
path: FileSystemPath::GlobPattern {
|
||||
pattern: "**/*.env".to_string(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
};
|
||||
let merged_policy = merge_file_system_policy_with_additional_permissions(
|
||||
&FileSystemSandboxPolicy::restricted(vec![FileSystemSandboxEntry {
|
||||
@@ -853,7 +853,7 @@ fn effective_file_system_sandbox_policy_returns_base_policy_without_additional_p
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: denied_path },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
]);
|
||||
|
||||
@@ -883,7 +883,7 @@ fn effective_file_system_sandbox_policy_merges_additional_write_roots() {
|
||||
path: FileSystemPath::Path {
|
||||
path: denied_path.clone(),
|
||||
},
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
]);
|
||||
let additional_permissions = PermissionProfile {
|
||||
@@ -900,7 +900,7 @@ fn effective_file_system_sandbox_policy_merges_additional_write_roots() {
|
||||
assert_eq!(
|
||||
effective_policy.entries.contains(&FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: denied_path },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
}),
|
||||
true
|
||||
);
|
||||
|
||||
@@ -12,7 +12,6 @@ use std::collections::BTreeMap;
|
||||
use std::collections::BTreeSet;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::VecDeque;
|
||||
use std::ffi::CStr;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use tracing::warn;
|
||||
@@ -725,7 +724,6 @@ pub fn create_seatbelt_command_args(args: CreateSeatbeltCommandArgsParams<'_>) -
|
||||
let dir_params = [
|
||||
file_read_dir_params,
|
||||
file_write_dir_params,
|
||||
macos_dir_params(),
|
||||
unix_socket_dir_params(&proxy),
|
||||
]
|
||||
.concat();
|
||||
@@ -742,32 +740,6 @@ pub fn create_seatbelt_command_args(args: CreateSeatbeltCommandArgsParams<'_>) -
|
||||
seatbelt_args
|
||||
}
|
||||
|
||||
/// Wraps libc::confstr to return a String.
|
||||
fn confstr(name: libc::c_int) -> Option<String> {
|
||||
let mut buf = vec![0_i8; (libc::PATH_MAX as usize) + 1];
|
||||
let len = unsafe { libc::confstr(name, buf.as_mut_ptr(), buf.len()) };
|
||||
if len == 0 {
|
||||
return None;
|
||||
}
|
||||
// confstr guarantees NUL-termination when len > 0.
|
||||
let cstr = unsafe { CStr::from_ptr(buf.as_ptr()) };
|
||||
cstr.to_str().ok().map(ToString::to_string)
|
||||
}
|
||||
|
||||
/// Wraps confstr to return a canonicalized PathBuf.
|
||||
fn confstr_path(name: libc::c_int) -> Option<PathBuf> {
|
||||
let s = confstr(name)?;
|
||||
let path = PathBuf::from(s);
|
||||
path.canonicalize().ok().or(Some(path))
|
||||
}
|
||||
|
||||
fn macos_dir_params() -> Vec<(String, PathBuf)> {
|
||||
if let Some(p) = confstr_path(libc::_CS_DARWIN_USER_CACHE_DIR) {
|
||||
return vec![("DARWIN_USER_CACHE_DIR".to_string(), p)];
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "seatbelt_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
)
|
||||
|
||||
(allow mach-lookup
|
||||
; Used to look up the _CS_DARWIN_USER_CACHE_DIR in the sandbox.
|
||||
; Used by platform helpers that resolve user directory locations.
|
||||
(global-name "com.apple.bsd.dirhelper")
|
||||
(global-name "com.apple.system.opendirectoryd.membership")
|
||||
|
||||
@@ -29,7 +29,3 @@
|
||||
(allow sysctl-read
|
||||
(sysctl-name-regex #"^net.routetable")
|
||||
)
|
||||
|
||||
(allow file-write*
|
||||
(subpath (param "DARWIN_USER_CACHE_DIR"))
|
||||
)
|
||||
|
||||
@@ -7,7 +7,6 @@ use super::build_seatbelt_unreadable_glob_policy;
|
||||
use super::create_seatbelt_command_args;
|
||||
use super::create_seatbelt_command_args_for_legacy_policy;
|
||||
use super::dynamic_network_policy;
|
||||
use super::macos_dir_params;
|
||||
use super::normalize_path_for_sandbox;
|
||||
use super::seatbelt_regex_for_unreadable_glob;
|
||||
use super::unix_socket_dir_params;
|
||||
@@ -161,6 +160,29 @@ fn create_seatbelt_args_routes_network_through_proxy_ports() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dynamic_network_policy_allows_tls_without_darwin_user_cache_write() {
|
||||
let policy = dynamic_network_policy(
|
||||
&SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: true,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
},
|
||||
/*enforce_managed_network*/ false,
|
||||
&ProxyPolicyInputs::default(),
|
||||
);
|
||||
|
||||
assert!(
|
||||
policy.contains("(global-name \"com.apple.trustd.agent\")"),
|
||||
"policy should keep trustd agent access for TLS certificate verification:\n{policy}"
|
||||
);
|
||||
assert!(
|
||||
!policy.contains("DARWIN_USER_CACHE_DIR"),
|
||||
"network policy should not grant broad user cache writes:\n{policy}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_unreadable_paths_are_excluded_from_full_disk_read_and_write_access() {
|
||||
let unreadable = absolute_path("/tmp/codex-unreadable");
|
||||
@@ -173,7 +195,7 @@ fn explicit_unreadable_paths_are_excluded_from_full_disk_read_and_write_access()
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: unreadable },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
]);
|
||||
|
||||
@@ -245,7 +267,7 @@ fn explicit_unreadable_paths_are_excluded_from_readable_roots() {
|
||||
},
|
||||
FileSystemSandboxEntry {
|
||||
path: FileSystemPath::Path { path: unreadable },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
},
|
||||
]);
|
||||
|
||||
@@ -350,7 +372,7 @@ fn unreadable_glob_policy_includes_canonicalized_static_prefix() {
|
||||
let mut policy = FileSystemSandboxPolicy::default();
|
||||
policy.entries.push(FileSystemSandboxEntry {
|
||||
path: FileSystemPath::GlobPattern { pattern },
|
||||
access: FileSystemAccessMode::None,
|
||||
access: FileSystemAccessMode::Deny,
|
||||
});
|
||||
|
||||
let seatbelt_policy = build_seatbelt_unreadable_glob_policy(&policy, temp_dir.path());
|
||||
@@ -417,8 +439,8 @@ fn dynamic_network_policy_preserves_restricted_policy_when_proxy_config_without_
|
||||
&SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: true,
|
||||
exclude_tmpdir_env_var: false,
|
||||
exclude_slash_tmp: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
},
|
||||
/*enforce_managed_network*/ false,
|
||||
&ProxyPolicyInputs {
|
||||
@@ -453,8 +475,8 @@ fn dynamic_network_policy_blocks_dns_when_local_binding_has_no_proxy_ports() {
|
||||
&SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: true,
|
||||
exclude_tmpdir_env_var: false,
|
||||
exclude_slash_tmp: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
},
|
||||
/*enforce_managed_network*/ false,
|
||||
&ProxyPolicyInputs {
|
||||
@@ -481,8 +503,8 @@ fn dynamic_network_policy_preserves_restricted_policy_for_managed_network_withou
|
||||
&SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: true,
|
||||
exclude_tmpdir_env_var: false,
|
||||
exclude_slash_tmp: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
},
|
||||
/*enforce_managed_network*/ true,
|
||||
&ProxyPolicyInputs {
|
||||
@@ -771,8 +793,8 @@ fn create_seatbelt_args_full_network_with_proxy_is_still_proxy_only() {
|
||||
&SandboxPolicy::WorkspaceWrite {
|
||||
writable_roots: vec![],
|
||||
network_access: true,
|
||||
exclude_tmpdir_env_var: false,
|
||||
exclude_slash_tmp: false,
|
||||
exclude_tmpdir_env_var: true,
|
||||
exclude_slash_tmp: true,
|
||||
},
|
||||
/*enforce_managed_network*/ false,
|
||||
&ProxyPolicyInputs {
|
||||
@@ -946,14 +968,6 @@ fn create_seatbelt_args_with_read_only_git_and_codex_subpaths() {
|
||||
writable_definitions, expected_definitions,
|
||||
"unexpected writable-root parameter definitions in {args:#?}"
|
||||
);
|
||||
for (key, value) in macos_dir_params() {
|
||||
let expected_definition = format!("-D{key}={}", value.to_string_lossy());
|
||||
assert!(
|
||||
args.contains(&expected_definition),
|
||||
"expected definition arg `{expected_definition}` in {args:#?}"
|
||||
);
|
||||
}
|
||||
|
||||
let command_index = args
|
||||
.iter()
|
||||
.position(|arg| arg == "--")
|
||||
@@ -1316,14 +1330,6 @@ fn create_seatbelt_args_for_cwd_as_git_repo() {
|
||||
args.contains(&expected_slash_tmp),
|
||||
"missing {expected_slash_tmp}: {args:#?}"
|
||||
);
|
||||
for (key, value) in macos_dir_params() {
|
||||
let expected_definition = format!("-D{key}={}", value.to_string_lossy());
|
||||
assert!(
|
||||
args.contains(&expected_definition),
|
||||
"expected definition arg `{expected_definition}` in {args:#?}"
|
||||
);
|
||||
}
|
||||
|
||||
let command_index = args
|
||||
.iter()
|
||||
.position(|arg| arg == "--")
|
||||
|
||||
Reference in New Issue
Block a user