mirror of
https://github.com/openai/codex.git
synced 2026-11-20 23:08:21 +00:00
Preserve parent repository discovery through sandbox metadata mounts (#39629)
## Why The Linux sandbox represents missing protected metadata paths such as `.git` with empty read-only directories. Repository and project discovery treated any `.git` directory as a checkout root, so this synthetic directory could hide a real parent repository and its trust configuration. ## What changed - Treat a `.git` directory as repository metadata only when it contains `HEAD`, while continuing to recognize file-based `.git` entries. - Protect missing `.git` paths with the same read-only synthetic mounts as other workspace metadata without disrupting parent-repository discovery. - Canonicalize and read-only bind the synthetic-mount registry into the sandbox, and make protected-path cleanup handle read-only directory trees. ## Testing Added coverage for repository, project-root, trust, synthetic-mount, and Landlock behavior, including nested incomplete `.git` directories and a redirected `TMPDIR`. GitOrigin-RevId: 0724c54d56531143bb28011e4ca414cd3b0212be
This commit is contained in:
1 parent
e396ef3fc1
commit
9ca99b5171
15 files changed
+305
-116
No files matched your search
@@ -8,9 +8,12 @@ use std::io::Read;
|
||||
use std::os::fd::AsRawFd;
|
||||
use std::os::fd::FromRawFd;
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::AtomicI32;
|
||||
use std::sync::atomic::Ordering;
|
||||
@@ -564,7 +567,7 @@ fn run_or_exec_bwrap(bwrap_args: crate::bwrap::BwrapArgs) -> ! {
|
||||
|
||||
fn run_bwrap_in_child_with_synthetic_mount_cleanup(bwrap_args: crate::bwrap::BwrapArgs) -> ! {
|
||||
let crate::bwrap::BwrapArgs {
|
||||
args,
|
||||
mut args,
|
||||
preserved_files,
|
||||
synthetic_mount_targets,
|
||||
protected_create_targets,
|
||||
@@ -573,6 +576,20 @@ fn run_bwrap_in_child_with_synthetic_mount_cleanup(bwrap_args: crate::bwrap::Bwr
|
||||
let synthetic_mount_registrations = register_synthetic_mount_targets(&synthetic_mount_targets);
|
||||
let protected_create_registrations =
|
||||
register_protected_create_targets(&protected_create_targets);
|
||||
let registry_root = synthetic_mount_registry_root()
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
let Some(command_separator) = args.iter().position(|arg| arg == "--") else {
|
||||
panic!("bubblewrap argv is missing command separator '--'");
|
||||
};
|
||||
args.splice(
|
||||
command_separator..command_separator,
|
||||
[
|
||||
"--ro-bind".to_string(),
|
||||
registry_root.clone(),
|
||||
registry_root,
|
||||
],
|
||||
);
|
||||
let exec_start_pipe = create_exec_start_pipe(!protected_create_targets.is_empty());
|
||||
let parent_pid = unsafe { libc::getpid() };
|
||||
let pid = unsafe { libc::fork() };
|
||||
@@ -1136,12 +1153,6 @@ fn cleanup_protected_create_targets(targets: &[ProtectedCreateTargetRegistration
|
||||
|
||||
let mut violation = false;
|
||||
for target in targets.iter().rev() {
|
||||
if synthetic_mount_marker_dir_has_active_process(&target.marker_dir) {
|
||||
if target.target.path().exists() {
|
||||
violation = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
violation |= remove_protected_create_target(&target.target);
|
||||
match fs::remove_dir(&target.marker_dir) {
|
||||
Ok(()) => {}
|
||||
@@ -1206,7 +1217,13 @@ fn try_remove_protected_create_target(
|
||||
ProtectedCreateRemoval::Other
|
||||
};
|
||||
let result = if removal == ProtectedCreateRemoval::Directory {
|
||||
fs::remove_dir_all(path)
|
||||
match fs::remove_dir_all(path) {
|
||||
Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => {
|
||||
make_directory_tree_writable(path)?;
|
||||
fs::remove_dir_all(path)
|
||||
}
|
||||
result => result,
|
||||
}
|
||||
} else {
|
||||
fs::remove_file(path)
|
||||
};
|
||||
@@ -1222,6 +1239,28 @@ fn try_remove_protected_create_target(
|
||||
Ok(Some(removal))
|
||||
}
|
||||
|
||||
fn make_directory_tree_writable(path: &Path) -> std::io::Result<()> {
|
||||
let metadata = match fs::symlink_metadata(path) {
|
||||
Ok(metadata) => metadata,
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
if !metadata.is_dir() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let directory = OpenOptions::new()
|
||||
.read(true)
|
||||
.custom_flags(libc::O_PATH | libc::O_DIRECTORY | libc::O_NOFOLLOW)
|
||||
.open(path)?;
|
||||
let directory_path = PathBuf::from(format!("/proc/self/fd/{}", directory.as_raw_fd()));
|
||||
fs::set_permissions(&directory_path, fs::Permissions::from_mode(0o700))?;
|
||||
for entry in fs::read_dir(directory_path)? {
|
||||
make_directory_tree_writable(&entry?.path())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove_synthetic_mount_target(target: &crate::bwrap::SyntheticMountTarget) {
|
||||
let path = target.path();
|
||||
let metadata = match fs::symlink_metadata(path) {
|
||||
@@ -1309,10 +1348,23 @@ fn synthetic_mount_marker_dir(path: &Path) -> PathBuf {
|
||||
}
|
||||
|
||||
fn synthetic_mount_registry_root() -> PathBuf {
|
||||
let effective_uid = unsafe { libc::geteuid() };
|
||||
std::env::temp_dir().join(format!(
|
||||
"codex-bwrap-synthetic-mount-targets-{effective_uid}"
|
||||
))
|
||||
static REGISTRY_ROOT: OnceLock<PathBuf> = OnceLock::new();
|
||||
|
||||
REGISTRY_ROOT
|
||||
.get_or_init(|| {
|
||||
let effective_uid = unsafe { libc::geteuid() };
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let temp_dir = temp_dir.canonicalize().unwrap_or_else(|err| {
|
||||
panic!(
|
||||
"failed to resolve synthetic mount registry temp directory {}: {err}",
|
||||
temp_dir.display()
|
||||
)
|
||||
});
|
||||
temp_dir.join(format!(
|
||||
"codex-bwrap-synthetic-mount-targets-{effective_uid}"
|
||||
))
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
fn hash_path(path: &Path) -> u64 {
|
||||
|
||||
Reference in new issue
Block a user