Skip to content

Commit c6c5e39

Browse files
committed
test(sandbox): isolate Landlock descriptor-limit regression
Signed-off-by: Gordon Sim <[email protected]>
1 parent ca18712 commit c6c5e39

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

‎crates/openshell-sandbox/src/sandbox/linux/landlock.rs‎

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -664,27 +664,47 @@ mod tests {
664664
}
665665

666666
#[test]
667-
fn capability_free_baseline_drops_each_root_entry_before_opening_the_next() {
667+
fn capability_free_baseline_prepares_under_low_descriptor_limit() {
668+
const CHILD_MARKER: &str = "OPENSHELL_TEST_LANDLOCK_FD_LIMIT_CHILD";
669+
670+
if !matches!(probe_availability(), LandlockAvailability::Available { abi } if abi >= 3) {
671+
return;
672+
}
673+
674+
// Descriptor limits are process-wide. Isolate this test from the other
675+
// tests' sockets and files, and lower the limit only in the child.
676+
if std::env::var_os(CHILD_MARKER).is_none() {
677+
let output = std::process::Command::new(std::env::current_exe().unwrap())
678+
.args([
679+
"--exact",
680+
"sandbox::linux::landlock::tests::capability_free_baseline_prepares_under_low_descriptor_limit",
681+
"--nocapture",
682+
"--test-threads=1",
683+
])
684+
.env(CHILD_MARKER, "1")
685+
.output()
686+
.expect("run isolated Landlock descriptor-limit test");
687+
assert!(
688+
output.status.success(),
689+
"isolated Landlock descriptor-limit test failed: {}\n{}",
690+
String::from_utf8_lossy(&output.stdout),
691+
String::from_utf8_lossy(&output.stderr),
692+
);
693+
return;
694+
}
695+
668696
let root = tempfile::tempdir().unwrap();
669697
for index in 0..128 {
670698
std::fs::write(root.path().join(format!("entry-{index:03}")), b"public").unwrap();
671699
}
672700
std::fs::create_dir(root.path().join(".openshell")).unwrap();
673701

674-
let baseline = std::fs::read_dir("/proc/self/fd").unwrap().count();
675-
let mut visited = 0;
676-
let mut peak = baseline;
677-
for entry in capability_free_baseline_entries(root.path()).unwrap() {
678-
let _entry = entry.unwrap();
679-
visited += 1;
680-
peak = peak.max(std::fs::read_dir("/proc/self/fd").unwrap().count());
681-
}
682-
683-
assert_eq!(visited, 128);
684-
assert!(
685-
peak <= baseline + 16,
686-
"streamed baseline preparation retained too many descriptors: baseline={baseline} peak={peak}"
687-
);
702+
// More entries than available descriptors forces the production path
703+
// to release each entry after adding its rule instead of collecting FDs.
704+
nix::sys::resource::setrlimit(nix::sys::resource::Resource::RLIMIT_NOFILE, 64, 64)
705+
.expect("lower child descriptor limit");
706+
let _prepared = prepare_capability_free_baseline_at(root.path())
707+
.expect("prepare 128 baseline entries with a 64-descriptor limit");
688708
}
689709

690710
#[test]

0 commit comments

Comments
 (0)