Files
rust-discovery/crates/updater-linux/src/tests.rs
T
2026-11-14 21:48:21 +01:00

159 lines
5.7 KiB
Rust

//! Real OS adverse cases, confined to a new /run unit fixture and owned sleep
//! child. Never calls systemctl, real install paths, RMM ledger or networking.
use crate::{
Budget, digest,
files::{Directory, FileProof},
process::HeldProcess,
};
use std::{
ffi::OsStr,
fs::{self, OpenOptions},
io::Write,
os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt, symlink},
path::PathBuf,
process::{Command, Stdio},
time::{Duration, Instant},
};
use uuid::Uuid;
struct Fixture(PathBuf);
impl Fixture {
fn new() -> Self {
assert_eq!(
unsafe { libc::geteuid() },
0,
"Run OS contract tests in the isolated root-owned test container, never as a host daemon"
);
let path = PathBuf::from(format!("/run/rmm-updater-os-unit-{}", Uuid::new_v4()));
fs::create_dir(&path).unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
Self(path)
}
fn dir(&self) -> Directory {
Directory::open(&self.0, 0, true).unwrap()
}
fn file(&self, name: &str, bytes: &[u8]) {
let mut file = OpenOptions::new()
.create_new(true)
.write(true)
.mode(0o600)
.open(self.0.join(name))
.unwrap();
file.write_all(bytes).unwrap();
file.sync_all().unwrap();
}
}
impl Drop for Fixture {
fn drop(&mut self) {
fs::remove_dir_all(&self.0).unwrap();
}
}
#[test]
fn held_file_rejects_symlink_and_replaced_name_but_preserves_bound_hardlinks() {
let f = Fixture::new();
f.file("original", b"original-public-fixture");
fs::hard_link(f.0.join("original"), f.0.join("historical-alias")).unwrap();
let dir = f.dir();
let mut held = dir.inspect(OsStr::new("original"), 0, 1024).unwrap();
assert_eq!(held.proof().identity.links, 2);
let proof: FileProof = held.proof().clone();
assert_eq!(held.bytes(1024).unwrap(), b"original-public-fixture");
symlink("original", f.0.join("symlink")).unwrap();
assert!(dir.inspect(OsStr::new("symlink"), 0, 1024).is_err());
fs::rename(f.0.join("original"), f.0.join("retained")).unwrap();
f.file("original", b"replacement");
assert!(dir.file(OsStr::new("original"), &proof, 1024).is_err());
assert_eq!(held.bytes(1024).unwrap(), b"original-public-fixture");
assert_eq!(
fs::metadata(f.0.join("retained")).unwrap().ino(),
proof.identity.inode
);
assert_eq!(
fs::read(f.0.join("historical-alias")).unwrap(),
b"original-public-fixture"
);
fs::set_permissions(f.0.join("original"), fs::Permissions::from_mode(0o622)).unwrap();
assert!(dir.inspect(OsStr::new("original"), 0, 1024).is_err());
assert!(Directory::open(&f.0.join("symlink"), 0, true).is_err());
}
#[test]
fn pidfd_image_birth_account_and_exit_are_independently_observed() {
let image = fs::read("/usr/bin/sleep").unwrap();
let sha = digest(&image);
let mut child = Command::new("/usr/bin/sleep").arg("60").spawn().unwrap();
let process = HeldProcess::open(child.id(), 0, 0, &sha).unwrap();
process.verify().unwrap();
assert!(!process.exited().unwrap());
assert!(HeldProcess::open(child.id(), 1, 0, &sha).is_err());
assert!(HeldProcess::open(child.id(), 0, 0, &"0".repeat(64)).is_err());
let before = process.proof().clone();
child.kill().unwrap();
child.wait().unwrap();
let deadline = Instant::now() + Duration::from_secs(2);
while !process.exited().unwrap() {
assert!(Instant::now() < deadline);
std::thread::yield_now();
}
assert!(process.verify().is_err());
assert_eq!(process.proof(), &before);
let boot = fs::read_to_string("/proc/sys/kernel/random/boot_id")
.unwrap()
.trim()
.parse()
.unwrap();
assert!(Budget::new(Uuid::nil(), Duration::from_secs(1)).is_err());
assert!(Budget::new(boot, Duration::from_secs(601)).is_err());
let budget = Budget::new(boot, Duration::from_millis(100)).unwrap();
std::thread::sleep(Duration::from_millis(150));
assert!(budget.check().is_err());
}
#[test]
fn same_pid_exec_rejects_changed_image_despite_retained_pidfd_birth_and_credentials() {
struct ChildCleanup(std::process::Child);
impl Drop for ChildCleanup {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
let old_image = fs::read("/usr/bin/bash").unwrap();
let new_image = fs::read("/usr/bin/sleep").unwrap();
let mut child = ChildCleanup(
Command::new("/usr/bin/bash")
.env_clear()
.args(["-c", "read -r token; exec /usr/bin/sleep 60"])
.stdin(Stdio::piped())
.spawn()
.unwrap(),
);
let held = HeldProcess::open(child.0.id(), 0, 0, &digest(&old_image)).unwrap();
let before = held.proof().clone();
held.verify().unwrap();
child.0.stdin.take().unwrap().write_all(b"go\n").unwrap();
let deadline = Instant::now() + Duration::from_secs(3);
let fresh = loop {
assert!(
Instant::now() < deadline,
"owned fixture child never completed exec"
);
match HeldProcess::open(child.0.id(), 0, 0, &digest(&new_image)) {
Ok(process) => break process,
Err(_) => std::thread::yield_now(),
}
};
assert_eq!(fresh.proof().pid, before.pid);
assert_eq!(fresh.proof().birth, before.birth);
assert_eq!(
(fresh.proof().uid, fresh.proof().gid),
(before.uid, before.gid)
);
assert_ne!(fresh.proof().image_sha256, before.image_sha256);
assert!(!held.exited().unwrap());
assert!(held.verify().is_err());
child.0.kill().unwrap();
child.0.wait().unwrap();
}