use crate::containment::enumerate::process_parents;
use crate::identity::HostKernel;
use crate::test_child::namespaces as ns;
use crate::test_child::{await_member_ready, fixture_path, member_command};
use crate::test_groups::{namespaces, Group};
#[skuld::test]
fn namespaces_an_outer_procfs_is_unassessable(#[fixture(namespaces)] _group: &Group) {
ns::run(fixture_path!(fixture_enumerate_outer));
}
#[skuld::test]
fn fixture_enumerate_outer() {
if !ns::is_child() {
return;
}
ns::enter_new_pid_ns_for_children();
ns::run(fixture_path!(fixture_enumerate_inner));
}
#[skuld::test]
fn fixture_enumerate_inner() {
if !ns::is_child_in_new_pid_ns() {
return;
}
match process_parents(&HostKernel) {
Err(crate::error::Error::Unassessable { detail, .. }) => {
assert!(detail.contains("outer pid namespace"), "{detail}")
}
other => panic!("an outer procfs must be Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn namespaces_a_stat_mounted_over_is_unassessable(#[fixture(namespaces)] _group: &Group) {
ns::run(fixture_path!(fixture_enumerate_stat_overmount));
}
#[skuld::test]
fn fixture_enumerate_stat_overmount() {
if !ns::is_child() {
return;
}
ns::enter_private_mount_ns();
let mut child = crate::test_child::StdChild::spawn(&mut member_command(0));
await_member_ready(&mut child);
let pid = child.id();
let scratch = tempfile::tempdir().expect("tempdir");
let fake = scratch.path().join("stat");
let zeros = ["0"; 16].join(" ");
std::fs::write(&fake, format!("{pid} (fake) S 4242 999 {zeros} 1 0\n")).expect("write the fake stat");
ns::bind_over(&fake, std::path::Path::new(&format!("/proc/{pid}/stat")));
match process_parents(&HostKernel) {
Err(error @ crate::error::Error::Unassessable { .. }) => {
let text = crate::error::said(&error);
assert!(
text.contains(&format!("{pid}/stat lies beyond a mount in /proc")),
"the error must name the pid and the mount: {text}"
);
}
other => panic!("a stat behind a mount must fail the snapshot, got {other:?}"),
}
}