Skip to content

Commit 949bea6

Browse files
Handle deeply nested local helper scans without skipping aliases
1 parent e76ccc4 commit 949bea6

2 files changed

Lines changed: 122 additions & 1 deletion

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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()?;

‎packages/codex/patches/series.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@
159159
{
160160
"path": "0040-protect-physical-local-write-roots.patch",
161161
"sha256": "8f6395a1b8d253170db019319cdaeff855ce1798efcc20eed704689fe070d994"
162+
},
163+
{
164+
"path": "0044-scan-deep-helper-hard-links.patch",
165+
"sha256": "dbdc0621cb4011fab3ca7ddd6812193c53c6b49851ddb4b24fdadb0cc0d93223"
162166
}
163167
],
164-
"result_tree": "508adc6a0777c6b61a2abbeeafbc24a5f2e39914"
168+
"result_tree": "0429c4208ab3424b70262ee45e748b95092f9285"
165169
}

0 commit comments

Comments
 (0)