|
| 1 | +diff --git a/codex-rs/sandboxing/src/local_write_roots.rs b/codex-rs/sandboxing/src/local_write_roots.rs |
| 2 | +index c823db8a7a4d2588ec5763049fa36a78469a9d72..c14243236326fe38da7c143d21153f7ff74346f7 100644 |
| 3 | +--- a/codex-rs/sandboxing/src/local_write_roots.rs |
| 4 | ++++ b/codex-rs/sandboxing/src/local_write_roots.rs |
| 5 | +@@ -5,9 +5,13 @@ use std::collections::HashSet; |
| 6 | + use std::fs; |
| 7 | + use std::io; |
| 8 | + #[cfg(target_os = "linux")] |
| 9 | ++use std::os::fd::AsRawFd; |
| 10 | ++#[cfg(target_os = "linux")] |
| 11 | + use std::os::unix::ffi::OsStringExt; |
| 12 | + #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 13 | + use std::os::unix::fs::MetadataExt; |
| 14 | ++#[cfg(target_os = "linux")] |
| 15 | ++use std::os::unix::fs::OpenOptionsExt; |
| 16 | + use std::path::Path; |
| 17 | + use std::path::PathBuf; |
| 18 | + |
| 19 | +@@ -79,15 +83,19 @@ impl<'a> LocalWriteRoots<'a> { |
| 20 | + .iter() |
| 21 | + .any(|other| root != other && root.as_path().starts_with(other.as_path())) |
| 22 | + { |
| 23 | +- directories.push(root.as_path().to_path_buf()); |
| 24 | ++ directories.push((root.as_path(), root.as_path().to_path_buf())); |
| 25 | + } |
| 26 | + } |
| 27 | +- while let Some(directory) = directories.pop() { |
| 28 | +- for entry in fs::read_dir(directory)? { |
| 29 | ++ while let Some((root, directory)) = directories.pop() { |
| 30 | ++ #[cfg(target_os = "linux")] |
| 31 | ++ let (_directory_fd, entries) = read_helper_directory(root, &directory)?; |
| 32 | ++ #[cfg(not(target_os = "linux"))] |
| 33 | ++ let entries = fs::read_dir(&directory)?; |
| 34 | ++ for entry in entries { |
| 35 | + let entry = entry?; |
| 36 | + let kind = entry.file_type()?; |
| 37 | + if kind.is_dir() { |
| 38 | +- directories.push(entry.path()); |
| 39 | ++ directories.push((root, directory.join(entry.file_name()))); |
| 40 | + } else if kind.is_file() { |
| 41 | + let metadata = entry.metadata()?; |
| 42 | + if linked_helpers.contains(&(metadata.dev(), metadata.ino())) { |
| 43 | +@@ -124,6 +132,36 @@ impl<'a> LocalWriteRoots<'a> { |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | ++// A finite bind view can exceed PATH_MAX even when its original tree does not. |
| 48 | ++// Keep the ordinary path walk fast; reopen only overlong paths one component at |
| 49 | ++// a time. Retain the final descriptor until its ReadDir entries have been used. |
| 50 | ++#[cfg(target_os = "linux")] |
| 51 | ++fn read_helper_directory( |
| 52 | ++ root: &Path, |
| 53 | ++ directory: &Path, |
| 54 | ++) -> io::Result<(Option<fs::File>, fs::ReadDir)> { |
| 55 | ++ match fs::read_dir(directory) { |
| 56 | ++ Ok(entries) => Ok((None, entries)), |
| 57 | ++ Err(error) if error.raw_os_error() == Some(libc::ENAMETOOLONG) => { |
| 58 | ++ let relative = directory.strip_prefix(root).map_err(io::Error::other)?; |
| 59 | ++ // Preserve the existing grant-root lookup, including an explicit |
| 60 | ++ // root alias. Descendants are still never followed through symlinks. |
| 61 | ++ let mut opened = fs::File::open(root)?; |
| 62 | ++ for component in relative { |
| 63 | ++ let path = |
| 64 | ++ PathBuf::from(format!("/proc/self/fd/{}", opened.as_raw_fd())).join(component); |
| 65 | ++ opened = fs::OpenOptions::new() |
| 66 | ++ .read(true) |
| 67 | ++ .custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW) |
| 68 | ++ .open(path)?; |
| 69 | ++ } |
| 70 | ++ let entries = fs::read_dir(format!("/proc/self/fd/{}", opened.as_raw_fd()))?; |
| 71 | ++ Ok((Some(opened), entries)) |
| 72 | ++ } |
| 73 | ++ Err(error) => Err(error), |
| 74 | ++ } |
| 75 | ++} |
| 76 | ++ |
| 77 | + #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 78 | + type EntryIdentity = (u64, u64); |
| 79 | + |
| 80 | +@@ -421,6 +459,37 @@ mod tests { |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | + |
| 84 | ++ #[cfg(target_os = "linux")] |
| 85 | ++ #[test] |
| 86 | ++ fn hard_link_scan_handles_deep_directories_without_skipping_helpers() -> io::Result<()> { |
| 87 | ++ let temp = tempfile::tempdir()?; |
| 88 | ++ let helper = temp.path().join("helper"); |
| 89 | ++ fs::write(&helper, "synthetic helper")?; |
| 90 | ++ let store = temp.path().join("store"); |
| 91 | ++ fs::create_dir(&store)?; |
| 92 | ++ fs::hard_link(&helper, store.join("store-entry"))?; |
| 93 | ++ for (name, depth, width) in [("ordinary", 600, 1), ("overlong", 19, 240)] { |
| 94 | ++ let workspace = temp.path().join(name); |
| 95 | ++ fs::create_dir(&workspace)?; |
| 96 | ++ let mut directory = fs::File::open(&workspace)?; |
| 97 | ++ for _ in 0..depth { |
| 98 | ++ let path = PathBuf::from(format!("/proc/self/fd/{}", directory.as_raw_fd())) |
| 99 | ++ .join("d".repeat(width)); |
| 100 | ++ fs::create_dir(&path)?; |
| 101 | ++ directory = fs::File::open(path)?; |
| 102 | ++ } |
| 103 | ++ let deepest = PathBuf::from(format!("/proc/self/fd/{}", directory.as_raw_fd())); |
| 104 | ++ std::os::unix::fs::symlink(&store, deepest.join("outside-store"))?; |
| 105 | ++ let roots = [AbsolutePathBuf::from_absolute_path(&workspace)?]; |
| 106 | ++ let grants = LocalWriteRoots::new(&roots)?; |
| 107 | ++ let helpers = [helper.clone()]; |
| 108 | ++ assert!(!grants.contains_hard_link_to_any(&helpers)?, "{name}"); |
| 109 | ++ fs::hard_link(&helper, deepest.join("helper-alias"))?; |
| 110 | ++ assert!(grants.contains_hard_link_to_any(&helpers)?, "{name}"); |
| 111 | ++ } |
| 112 | ++ Ok(()) |
| 113 | ++ } |
| 114 | ++ |
| 115 | + #[test] |
| 116 | + fn ordinary_hard_link_to_a_helper_must_be_inside_a_grant() -> io::Result<()> { |
| 117 | + let temp = tempfile::tempdir()?; |
0 commit comments