aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / tools / aien-test / src / audit.rs
| 1 | //! Input audit (L335): does a gate read only what its manifest declares? |
| 2 | //! |
| 3 | //! A gate that says `cache: inputs_complete` lends its result to every head |
| 4 | //! whose declared inputs are byte-identical (`cache::cross_head`). If the gate |
| 5 | //! reads a file it does not declare, a head that changed only that file gets |
| 6 | //! another head's verdict: a false PASS. This module checks the promise |
| 7 | //! against a real run: `aien-audit-inputs` runs the gate once under |
| 8 | //! `strace -f -y -Y` (every file opened, with the path the kernel resolved and |
| 9 | //! the program that opened it) and lists each opened file the manifest does |
| 10 | //! not cover. |
| 11 | //! |
| 12 | //! What counts as covered: |
| 13 | //! - inside the repository: a declared input (file, directory or `*` pattern), |
| 14 | //! a build file every key carries ([`runner::IMPLIED_BUILD_FILES`]), the |
| 15 | //! manifest itself and `run.exec`; |
| 16 | //! - outside it: an `outside_inputs` entry, or the toolchain and system |
| 17 | //! (`/usr`, `/lib`, `/etc`, `/proc`, `/sys`, `/dev`, `/run`, `~/.rustup`, |
| 18 | //! `~/.cargo`). The toolchain is pinned by `rust-toolchain.toml` and the |
| 19 | //! registry sources by `Cargo.lock`, both in every key; a system library |
| 20 | //! upgrade is not (a limit of this audit, stated in the report). |
| 21 | //! |
| 22 | //! Not judged as reads (each reported with a count, never silently): |
| 23 | //! - `git` opening anything (the commit, its config and objects) and `tar` |
| 24 | //! opening the tree (the copy step). What the |
| 25 | //! copy's consumers read is mapped back (next item). A script that branches |
| 26 | //! on git output is not caught here; read the script. |
| 27 | //! - files under the private `TMPDIR` the audit gives the run: a path |
| 28 | //! `TMPDIR/<dir>/REL` whose `REL` exists in the repository is judged as the |
| 29 | //! repository file `REL` (the PREFILL mutants build a copy of the tree); |
| 30 | //! anything else there is the run's own scratch. |
| 31 | //! - the cargo target dir, `.git/`, `/tmp`, and opens for writing only. |
| 32 | //! |
| 33 | //! An audit only sees what one run read: run it with a cold target dir, or |
| 34 | //! cargo skips reading the sources of crates it considers fresh. |
| 35 | use crate::manifest::Manifest; |
| 36 | use crate::runner; |
| 37 | use std::collections::{BTreeMap, BTreeSet}; |
| 38 | use std::path::{Path, PathBuf}; |
| 39 | |
| 40 | /// One successful open (or exec) from an strace log. |
| 41 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 42 | pub struct Opened { |
| 43 | /// The program's name as the kernel reports it (15 characters at most). |
| 44 | pub comm: String, |
| 45 | /// The path the kernel resolved (the `-y` decoration), absolute. |
| 46 | pub path: String, |
| 47 | /// Opened only for writing (`O_WRONLY`): not a read. |
| 48 | pub write_only: bool, |
| 49 | /// Opened as a directory (`O_DIRECTORY`): a listing of its names. |
| 50 | pub dir: bool, |
| 51 | /// Opened with `O_TRUNC`: the run replaced the file, so a later read of |
| 52 | /// it reads the run's own output. |
| 53 | pub truncates: bool, |
| 54 | } |
| 55 | |
| 56 | /// Parse `strace -f -qq -Y -y -e trace=open,openat,openat2,execve |
| 57 | /// -e status=successful` output. Lines that are not a successful open or |
| 58 | /// exec of a named file are skipped. |
| 59 | pub fn parse_strace(text: &str) -> Vec<Opened> { |
| 60 | let mut out = Vec::new(); |
| 61 | for line in text.lines() { |
| 62 | let Some(lt) = line.find('<') else { continue }; |
| 63 | let Some(gt) = line[lt..].find("> ") else { |
| 64 | continue; |
| 65 | }; |
| 66 | let comm = line[lt + 1..lt + gt].to_string(); |
| 67 | let call = &line[lt + gt + 2..]; |
| 68 | if call.starts_with("execve(") { |
| 69 | // execve("/abs/path", ...) = 0: the program file itself is read. |
| 70 | if let Some(p) = quoted(call) { |
| 71 | if p.starts_with('/') && call.trim_end().ends_with("= 0") { |
| 72 | out.push(Opened { |
| 73 | comm, |
| 74 | path: p, |
| 75 | write_only: false, |
| 76 | dir: false, |
| 77 | truncates: false, |
| 78 | }); |
| 79 | } |
| 80 | } |
| 81 | continue; |
| 82 | } |
| 83 | if !(call.starts_with("openat(") |
| 84 | || call.starts_with("open(") |
| 85 | || call.starts_with("openat2(")) |
| 86 | { |
| 87 | continue; |
| 88 | } |
| 89 | // ... ) = 3</resolved/path> |
| 90 | let Some(eq) = call.rfind(") = ") else { |
| 91 | continue; |
| 92 | }; |
| 93 | let ret = &call[eq + 4..]; |
| 94 | let (Some(a), Some(b)) = (ret.find('<'), ret.rfind('>')) else { |
| 95 | continue; |
| 96 | }; |
| 97 | if b <= a + 1 { |
| 98 | continue; |
| 99 | } |
| 100 | let path = ret[a + 1..b].to_string(); |
| 101 | // A pipe or socket (`pipe:[123]`) is not a file. |
| 102 | if !path.starts_with('/') { |
| 103 | continue; |
| 104 | } |
| 105 | let write_only = call[..eq].contains("O_WRONLY"); |
| 106 | let dir = call[..eq].contains("O_DIRECTORY"); |
| 107 | let truncates = call[..eq].contains("O_TRUNC"); |
| 108 | out.push(Opened { |
| 109 | comm, |
| 110 | path, |
| 111 | write_only, |
| 112 | dir, |
| 113 | truncates, |
| 114 | }); |
| 115 | } |
| 116 | out |
| 117 | } |
| 118 | |
| 119 | /// The first double-quoted string of a syscall line (no escapes expected in |
| 120 | /// the paths we judge; a path with `"` is skipped). |
| 121 | fn quoted(call: &str) -> Option<String> { |
| 122 | let a = call.find('"')?; |
| 123 | let b = call[a + 1..].find('"')?; |
| 124 | Some(call[a + 1..a + 1 + b].to_string()) |
| 125 | } |
| 126 | |
| 127 | /// Where the audited run lived. |
| 128 | #[derive(Debug, Clone)] |
| 129 | pub struct Places { |
| 130 | /// The repository root (canonical). |
| 131 | pub root: PathBuf, |
| 132 | /// The repository-relative path of the manifest. |
| 133 | pub manifest_rel: String, |
| 134 | /// The cargo target dir of the run (canonical). |
| 135 | pub target_dir: PathBuf, |
| 136 | /// The private `TMPDIR` given to the run (canonical). |
| 137 | pub tmpdir: PathBuf, |
| 138 | /// The judge's home, for `~/` entries and `~/.rustup`, `~/.cargo`. |
| 139 | pub home: PathBuf, |
| 140 | /// The files git tracks at HEAD (repo-relative). A repository path that is |
| 141 | /// neither one of them nor a directory holding one was made by the run |
| 142 | /// (build outputs in the tree, say `build/`), so it is not an input. None: |
| 143 | /// every path is judged. |
| 144 | pub tracked: Option<BTreeSet<String>>, |
| 145 | /// Sibling checkouts the judge provides (`../<repo>`, canonical). Their |
| 146 | /// commits are in every key as pins, so reading them is covered. |
| 147 | pub siblings: Vec<PathBuf>, |
| 148 | } |
| 149 | |
| 150 | /// The audit's findings. |
| 151 | #[derive(Debug, Default, Clone)] |
| 152 | pub struct Report { |
| 153 | /// Repository files read but not declared (repo-relative), with a reader. |
| 154 | pub undeclared: BTreeMap<String, String>, |
| 155 | /// Files outside the repository read but not declared, with a reader. |
| 156 | pub undeclared_outside: BTreeMap<String, String>, |
| 157 | /// Counts of opens not judged as reads, by reason. |
| 158 | pub ignored: BTreeMap<&'static str, usize>, |
| 159 | /// Declared repository files the run did read (for the record). |
| 160 | pub covered: BTreeSet<String>, |
| 161 | /// The run started cargo, rustc, clippy or rustdoc. |
| 162 | pub uses_cargo: bool, |
| 163 | } |
| 164 | |
| 165 | impl Report { |
| 166 | /// The promise holds: nothing undeclared, and a `target_lock: none` gate |
| 167 | /// never started cargo. |
| 168 | pub fn ok(&self, m: &Manifest) -> bool { |
| 169 | self.undeclared.is_empty() |
| 170 | && self.undeclared_outside.is_empty() |
| 171 | && (m.target_lock || !self.uses_cargo) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | const SYSTEM: [&str; 9] = [ |
| 176 | "/usr/", "/lib/", "/lib64/", "/bin/", "/sbin/", "/etc/", "/proc/", "/sys/", "/dev/", |
| 177 | ]; |
| 178 | const CARGO_PROGRAMS: [&str; 6] = [ |
| 179 | "cargo", |
| 180 | "rustc", |
| 181 | "clippy-driver", |
| 182 | "cargo-clippy", |
| 183 | "rustdoc", |
| 184 | "cargo-fmt", |
| 185 | ]; |
| 186 | |
| 187 | /// Is the repository path `rel` covered by the manifest's declarations? |
| 188 | pub fn covered(m: &Manifest, manifest_rel: &str, rel: &str) -> bool { |
| 189 | let rel = rel.trim_end_matches('/'); |
| 190 | if rel == manifest_rel || rel == m.exec || runner::IMPLIED_BUILD_FILES.contains(&rel) { |
| 191 | return true; |
| 192 | } |
| 193 | m.inputs.iter().any(|i| input_covers(i, rel)) |
| 194 | } |
| 195 | |
| 196 | /// A directory listing is covered when a `listed` entry names that very |
| 197 | /// directory (component by component, `*` allowed): its names are in the key. |
| 198 | fn listed_covers(m: &Manifest, rel: &str) -> bool { |
| 199 | let rp: Vec<&str> = rel.split('/').collect(); |
| 200 | m.listed.iter().any(|l| { |
| 201 | let lp: Vec<&str> = l.trim_end_matches('/').split('/').collect(); |
| 202 | lp.len() == rp.len() |
| 203 | && lp |
| 204 | .iter() |
| 205 | .zip(&rp) |
| 206 | .all(|(a, b)| runner::wildcard_match(a, b)) |
| 207 | }) |
| 208 | } |
| 209 | |
| 210 | /// One declared input against one path: equal, inside a declared directory, |
| 211 | /// or matched component by component by a `*` pattern (whose last component |
| 212 | /// may itself be a directory the path is inside). |
| 213 | fn input_covers(input: &str, rel: &str) -> bool { |
| 214 | let i = input.trim().trim_start_matches("./").trim_end_matches('/'); |
| 215 | if i.is_empty() || i == "." { |
| 216 | return true; |
| 217 | } |
| 218 | let ip: Vec<&str> = i.split('/').collect(); |
| 219 | let rp: Vec<&str> = rel.split('/').collect(); |
| 220 | if rp.len() < ip.len() { |
| 221 | return false; |
| 222 | } |
| 223 | ip.iter() |
| 224 | .zip(&rp) |
| 225 | .all(|(a, b)| runner::wildcard_match(a, b)) |
| 226 | } |
| 227 | |
| 228 | /// Judge every open of an audited run. |
| 229 | pub fn judge(m: &Manifest, places: &Places, opened: &[Opened]) -> Report { |
| 230 | let mut r = Report::default(); |
| 231 | let root = places.root.to_string_lossy().into_owned() + "/"; |
| 232 | let target = places.target_dir.to_string_lossy().into_owned() + "/"; |
| 233 | let tmpdir = places.tmpdir.to_string_lossy().into_owned() + "/"; |
| 234 | let home = places.home.to_string_lossy().into_owned(); |
| 235 | let toolchain = [ |
| 236 | format!("{home}/.rustup/"), |
| 237 | format!("{home}/.cargo/"), |
| 238 | "/var/lib/aien-run/rustup/".into(), |
| 239 | "/var/lib/aien-run/cargo/".into(), |
| 240 | ]; |
| 241 | let outside: Vec<String> = m |
| 242 | .outside_inputs |
| 243 | .iter() |
| 244 | .map(|o| { |
| 245 | let o = o.trim_end_matches('/'); |
| 246 | match o.strip_prefix("~/") { |
| 247 | Some(rest) => format!("{home}/{rest}"), |
| 248 | None => o.to_string(), |
| 249 | } |
| 250 | }) |
| 251 | .collect(); |
| 252 | // Files the run truncated (a log it writes, then greps): reads after that |
| 253 | // see its own output, not an input. |
| 254 | let mut made: BTreeSet<&str> = BTreeSet::new(); |
| 255 | for o in opened { |
| 256 | if CARGO_PROGRAMS.contains(&o.comm.as_str()) { |
| 257 | r.uses_cargo = true; |
| 258 | } |
| 259 | if o.truncates { |
| 260 | made.insert(o.path.as_str()); |
| 261 | } |
| 262 | if o.write_only { |
| 263 | bump(&mut r, "opened for writing only"); |
| 264 | continue; |
| 265 | } |
| 266 | // git reads its own records wherever they live (a worktree's common |
| 267 | // dir, a mirror, ~/.gitconfig): the commit, not a gate input. |
| 268 | if o.comm == "git" || o.comm.starts_with("git-") { |
| 269 | bump(&mut r, "git (commit metadata, config, objects)"); |
| 270 | continue; |
| 271 | } |
| 272 | // rm opens directories to delete what is inside them, never to read. |
| 273 | if o.comm == "rm" { |
| 274 | bump(&mut r, "rm (deleting, not reading)"); |
| 275 | continue; |
| 276 | } |
| 277 | // Any program reading a git directory (cargo lists a path package's |
| 278 | // files through the git index; gix reads the excludes) reads the tree's |
| 279 | // own record of itself, which the declared files already pin. |
| 280 | if o.path.contains("/.git/") || o.path.ends_with("/.git") { |
| 281 | bump(&mut r, "git metadata (.git)"); |
| 282 | continue; |
| 283 | } |
| 284 | let p = o.path.as_str(); |
| 285 | let p_dir = format!("{}/", p.trim_end_matches('/')); |
| 286 | if p_dir.starts_with(&target) { |
| 287 | bump(&mut r, "cargo target dir (outputs)"); |
| 288 | continue; |
| 289 | } |
| 290 | // A file of the private TMPDIR whose tail names a repository file is |
| 291 | // that file (a copy of the tree); the rest is the run's scratch. |
| 292 | if let Some(t) = p.strip_prefix(&tmpdir) { |
| 293 | let tail = t.split_once('/').map(|x| x.1).unwrap_or(""); |
| 294 | if o.comm == "tar" { |
| 295 | // tar -x making the copy: it writes, it does not read. |
| 296 | bump(&mut r, "tar writing the copy"); |
| 297 | } else if !tail.is_empty() |
| 298 | && places.root.join(tail).exists() |
| 299 | && !tail.starts_with(".git") |
| 300 | { |
| 301 | judge_repo(m, places, tail, o, &mut r); |
| 302 | } else { |
| 303 | bump(&mut r, "private TMPDIR scratch"); |
| 304 | } |
| 305 | continue; |
| 306 | } |
| 307 | if places |
| 308 | .siblings |
| 309 | .iter() |
| 310 | .any(|s| p_dir.starts_with(&format!("{}/", s.display()))) |
| 311 | { |
| 312 | bump(&mut r, "sibling checkout (its commit is a pin in the key)"); |
| 313 | continue; |
| 314 | } |
| 315 | if p_dir.starts_with(&root) || p_dir == root { |
| 316 | let rel = p_dir[root.len().min(p_dir.len())..] |
| 317 | .trim_end_matches('/') |
| 318 | .to_string(); |
| 319 | if rel.is_empty() { |
| 320 | bump(&mut r, "the repository root directory itself"); |
| 321 | } else if rel == ".git" || rel.starts_with(".git/") { |
| 322 | bump(&mut r, "git metadata (.git)"); |
| 323 | } else if o.comm == "tar" { |
| 324 | bump(&mut r, "tar reading the tree (the copy step)"); |
| 325 | } else { |
| 326 | judge_repo(m, places, &rel, o, &mut r); |
| 327 | } |
| 328 | continue; |
| 329 | } |
| 330 | // Only outside the tree and its TMPDIR copy: a copied repository file |
| 331 | // is written by tar first and is still that repository file. |
| 332 | if made.contains(p) { |
| 333 | bump(&mut r, "made earlier by the run (truncated, then read)"); |
| 334 | continue; |
| 335 | } |
| 336 | if outside |
| 337 | .iter() |
| 338 | .any(|d| p == d || p.starts_with(&format!("{d}/"))) |
| 339 | { |
| 340 | bump(&mut r, "declared outside_inputs"); |
| 341 | continue; |
| 342 | } |
| 343 | if toolchain.iter().any(|t| p_dir.starts_with(t.as_str())) { |
| 344 | bump( |
| 345 | &mut r, |
| 346 | "toolchain and registry (rust-toolchain.toml, Cargo.lock)", |
| 347 | ); |
| 348 | continue; |
| 349 | } |
| 350 | // cargo reads git's user config for git dependencies and fetches; |
| 351 | // with every dependency locked by Cargo.lock it does not change a build. |
| 352 | if p == format!("{home}/.gitconfig") || p == format!("{home}/.gitignore_global") { |
| 353 | bump( |
| 354 | &mut r, |
| 355 | "~/.gitconfig, ~/.gitignore_global (git user config)", |
| 356 | ); |
| 357 | continue; |
| 358 | } |
| 359 | if SYSTEM.iter().any(|s| p_dir.starts_with(s)) || p == "/" { |
| 360 | bump(&mut r, "system (not in the key: a limit)"); |
| 361 | continue; |
| 362 | } |
| 363 | if p.starts_with("/tmp/") || p.starts_with("/run/") || p.starts_with("/var/tmp/") { |
| 364 | bump(&mut r, "/tmp, /run (locks and scratch)"); |
| 365 | continue; |
| 366 | } |
| 367 | r.undeclared_outside |
| 368 | .entry(p.to_string()) |
| 369 | .or_insert_with(|| o.comm.clone()); |
| 370 | } |
| 371 | r |
| 372 | } |
| 373 | |
| 374 | fn bump(r: &mut Report, why: &'static str) { |
| 375 | *r.ignored.entry(why).or_insert(0) += 1; |
| 376 | } |
| 377 | |
| 378 | fn judge_repo(m: &Manifest, places: &Places, rel: &str, o: &Opened, r: &mut Report) { |
| 379 | let comm = o.comm.as_str(); |
| 380 | if let Some(t) = &places.tracked { |
| 381 | let dir = format!("{rel}/"); |
| 382 | let in_tree = t.contains(rel) |
| 383 | || t.range(dir.clone()..) |
| 384 | .next() |
| 385 | .is_some_and(|f| f.starts_with(&dir)); |
| 386 | if !in_tree { |
| 387 | bump(r, "not in the tree at HEAD (made by the run)"); |
| 388 | return; |
| 389 | } |
| 390 | } |
| 391 | if covered(m, &places.manifest_rel, rel) || (o.dir && listed_covers(m, rel)) { |
| 392 | r.covered.insert(rel.to_string()); |
| 393 | } else { |
| 394 | r.undeclared |
| 395 | .entry(rel.to_string()) |
| 396 | .or_insert_with(|| comm.to_string()); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /// The report as text, one finding per line; `ok` decides the last line. |
| 401 | pub fn render(m: &Manifest, r: &Report) -> String { |
| 402 | let mut s = String::new(); |
| 403 | s.push_str(&format!( |
| 404 | "AUDIT {} covered_repo_files={} uses_cargo={}\n", |
| 405 | m.gate, |
| 406 | r.covered.len(), |
| 407 | r.uses_cargo |
| 408 | )); |
| 409 | for (why, n) in &r.ignored { |
| 410 | s.push_str(&format!("IGNORED {n} {why}\n")); |
| 411 | } |
| 412 | for (p, who) in &r.undeclared { |
| 413 | s.push_str(&format!("UNDECLARED {p} (read by {who})\n")); |
| 414 | } |
| 415 | for (p, who) in &r.undeclared_outside { |
| 416 | s.push_str(&format!("UNDECLARED_OUTSIDE {p} (read by {who})\n")); |
| 417 | } |
| 418 | if !m.target_lock && r.uses_cargo { |
| 419 | s.push_str("TARGET_LOCK_BROKEN target_lock: none but the run started cargo or rustc\n"); |
| 420 | } |
| 421 | s.push_str(if r.ok(m) { |
| 422 | "RESULT PASS inputs complete\n" |
| 423 | } else { |
| 424 | "RESULT FAIL inputs incomplete\n" |
| 425 | }); |
| 426 | s |
| 427 | } |
| 428 | |
| 429 | /// Canonical form of `p`, or `p` itself when it does not exist yet. |
| 430 | pub fn canon(p: &Path) -> PathBuf { |
| 431 | std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()) |
| 432 | } |
| 433 | |
| 434 | #[cfg(test)] |
| 435 | mod tests { |
| 436 | use super::*; |
| 437 | use crate::manifest; |
| 438 | |
| 439 | fn gate(extra: &str) -> Manifest { |
| 440 | let text = format!( |
| 441 | "gate: G\nmanifest_version: 1\nowner: t\nrequires:\n - host\ninputs:\n - crates/a/\n - crates/*/Cargo.toml\n{extra}run:\n exec: tests/run.sh\nexpects:\n exit: 0\n verdict: PASS\ntimeout: 1m\ncache: inputs_complete\n" |
| 442 | ); |
| 443 | manifest::parse(text.as_bytes()).unwrap() |
| 444 | } |
| 445 | |
| 446 | fn places(root: &Path) -> Places { |
| 447 | Places { |
| 448 | root: root.to_path_buf(), |
| 449 | manifest_rel: "tests/g.gate".into(), |
| 450 | target_dir: root.join("target"), |
| 451 | tmpdir: PathBuf::from("/audit/tmp"), |
| 452 | home: PathBuf::from("/home/op"), |
| 453 | tracked: None, |
| 454 | siblings: Vec::new(), |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | fn line(comm: &str, path: &str) -> String { |
| 459 | format!("7<{comm}> openat(AT_FDCWD</r>, \"x\", O_RDONLY|O_CLOEXEC) = 3<{path}>\n") |
| 460 | } |
| 461 | |
| 462 | #[test] |
| 463 | fn parses_opens_execs_and_writes() { |
| 464 | let t = "12<sh> openat(AT_FDCWD</r>, \"a\", O_RDONLY) = 3</r/a>\n\ |
| 465 | 12<sh> openat(AT_FDCWD</r>, \"o\", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 4</r/o>\n\ |
| 466 | 13<sh> execve(\"/usr/bin/cargo\", [\"cargo\"], 0xffff /* 3 vars */) = 0\n\ |
| 467 | 13<cargo> +++ exited with 0 +++\n"; |
| 468 | let o = parse_strace(t); |
| 469 | assert_eq!(o.len(), 3); |
| 470 | assert_eq!( |
| 471 | o[0], |
| 472 | Opened { |
| 473 | comm: "sh".into(), |
| 474 | path: "/r/a".into(), |
| 475 | write_only: false, |
| 476 | dir: false, |
| 477 | truncates: false |
| 478 | } |
| 479 | ); |
| 480 | assert!(o[1].write_only); |
| 481 | assert!(o[1].truncates); |
| 482 | assert_eq!(o[2].path, "/usr/bin/cargo"); |
| 483 | } |
| 484 | |
| 485 | #[test] |
| 486 | fn a_dropped_input_is_caught_and_a_declared_one_is_not() { |
| 487 | let dir = tempdir(); |
| 488 | let root = dir.as_path(); |
| 489 | for f in [ |
| 490 | "crates/a/src/lib.rs", |
| 491 | "crates/b/src/lib.rs", |
| 492 | "crates/b/Cargo.toml", |
| 493 | ] { |
| 494 | std::fs::create_dir_all(root.join(f).parent().unwrap()).unwrap(); |
| 495 | std::fs::write(root.join(f), "x").unwrap(); |
| 496 | } |
| 497 | let r = root.display(); |
| 498 | let trace = line("rustc", &format!("{r}/crates/a/src/lib.rs")) |
| 499 | + &line("rustc", &format!("{r}/crates/b/src/lib.rs")) |
| 500 | + &line("cargo", &format!("{r}/crates/b/Cargo.toml")) |
| 501 | + &line("rustc", &format!("{r}/target/release/deps/liba.rlib")) |
| 502 | + &line("rustc", "/usr/lib/libc.so.6"); |
| 503 | let opened = parse_strace(&trace); |
| 504 | // crates/b/ is read but only its Cargo.toml is declared: FAIL. |
| 505 | let m = gate(""); |
| 506 | let rep = judge(&m, &places(root), &opened); |
| 507 | assert_eq!( |
| 508 | rep.undeclared.keys().collect::<Vec<_>>(), |
| 509 | vec!["crates/b/src/lib.rs"] |
| 510 | ); |
| 511 | assert!(!rep.ok(&m)); |
| 512 | // Declare it: PASS. |
| 513 | let m2 = gate(" - crates/b/\n"); |
| 514 | let rep2 = judge(&m2, &places(root), &opened); |
| 515 | assert!(rep2.ok(&m2), "{}", render(&m2, &rep2)); |
| 516 | assert!( |
| 517 | rep2.covered.contains("crates/b/Cargo.toml"), |
| 518 | "the * pattern covers it" |
| 519 | ); |
| 520 | } |
| 521 | |
| 522 | #[test] |
| 523 | fn outside_reads_need_outside_inputs() { |
| 524 | let dir = tempdir(); |
| 525 | let opened = parse_strace(&line( |
| 526 | "prefill_e2e2-4", |
| 527 | "/home/op/models/T/model.safetensors", |
| 528 | )); |
| 529 | let m = gate(""); |
| 530 | assert_eq!( |
| 531 | judge(&m, &places(&dir), &opened).undeclared_outside.len(), |
| 532 | 1 |
| 533 | ); |
| 534 | let m2 = gate("outside_inputs:\n - ~/models/T/\n"); |
| 535 | let rep = judge(&m2, &places(&dir), &opened); |
| 536 | assert!(rep.ok(&m2), "{}", render(&m2, &rep)); |
| 537 | } |
| 538 | |
| 539 | #[test] |
| 540 | fn a_file_the_run_truncated_is_its_output_but_a_read_before_is_an_input() { |
| 541 | let dir = tempdir(); |
| 542 | let log = "/home/op/logs/run.log"; |
| 543 | let write = format!( |
| 544 | "7<sh> openat(AT_FDCWD</r>, \"x\", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3<{log}>\n" |
| 545 | ); |
| 546 | // Written, then grepped: its own output. |
| 547 | let after = parse_strace(&(write.clone() + &line("grep", log))); |
| 548 | let m = gate(""); |
| 549 | let rep = judge(&m, &places(&dir), &after); |
| 550 | assert!(rep.ok(&m), "{}", render(&m, &rep)); |
| 551 | // Read before the run replaced it: the old bytes were an input. |
| 552 | let before = parse_strace(&(line("grep", log) + &write)); |
| 553 | assert_eq!( |
| 554 | judge(&m, &places(&dir), &before).undeclared_outside.len(), |
| 555 | 1 |
| 556 | ); |
| 557 | // The TMPDIR copy of a repository file is written by tar, then read: |
| 558 | // still that repository file. |
| 559 | std::fs::create_dir_all(dir.join("crates/z")).unwrap(); |
| 560 | std::fs::write(dir.join("crates/z/lib.rs"), "x").unwrap(); |
| 561 | let copy = "/audit/tmp/tmp.Ab12/crates/z/lib.rs"; |
| 562 | let t = format!( |
| 563 | "7<tar> openat(AT_FDCWD</r>, \"x\", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3<{copy}>\n" |
| 564 | ) + &line("rustc", copy); |
| 565 | let rep = judge(&m, &places(&dir), &parse_strace(&t)); |
| 566 | assert_eq!( |
| 567 | rep.undeclared.keys().collect::<Vec<_>>(), |
| 568 | vec!["crates/z/lib.rs"] |
| 569 | ); |
| 570 | } |
| 571 | |
| 572 | #[test] |
| 573 | fn a_copy_in_the_private_tmpdir_is_judged_as_the_repository_file() { |
| 574 | let dir = tempdir(); |
| 575 | std::fs::create_dir_all(dir.join("crates/z")).unwrap(); |
| 576 | std::fs::write(dir.join("crates/z/lib.rs"), "x").unwrap(); |
| 577 | let trace = line("tar", &format!("{}/crates/z/lib.rs", dir.display())) |
| 578 | + &line("rustc", "/audit/tmp/tmp.Ab12/crates/z/lib.rs") |
| 579 | + &line("rustc", "/audit/tmp/tmp.Ab12/scratch.o"); |
| 580 | let rep = judge(&gate(""), &places(&dir), &parse_strace(&trace)); |
| 581 | assert_eq!( |
| 582 | rep.undeclared.keys().collect::<Vec<_>>(), |
| 583 | vec!["crates/z/lib.rs"] |
| 584 | ); |
| 585 | assert_eq!(rep.ignored.get("private TMPDIR scratch"), Some(&1)); |
| 586 | } |
| 587 | |
| 588 | #[test] |
| 589 | fn target_lock_none_with_cargo_fails() { |
| 590 | let dir = tempdir(); |
| 591 | let m = gate("target_lock: none\n"); |
| 592 | let opened = parse_strace("9<sh> execve(\"/usr/bin/cargo\", [\"cargo\"], 0x1 /* 1 vars */) = 0\n9<cargo> openat(AT_FDCWD</r>, \"/etc/x\", O_RDONLY) = 3</etc/x>\n"); |
| 593 | let rep = judge(&m, &places(&dir), &opened); |
| 594 | assert!(rep.uses_cargo && !rep.ok(&m)); |
| 595 | assert!(render(&m, &rep).contains("TARGET_LOCK_BROKEN")); |
| 596 | } |
| 597 | |
| 598 | #[test] |
| 599 | fn files_the_run_made_in_the_tree_are_not_inputs() { |
| 600 | let dir = tempdir(); |
| 601 | let mut p = places(&dir); |
| 602 | p.tracked = Some(["src/x.c".to_string()].into_iter().collect()); |
| 603 | let r = dir.display(); |
| 604 | let trace = line("cc", &format!("{r}/src/x.c")) |
| 605 | + &line("ld", &format!("{r}/build/x.o")) |
| 606 | + &line("make", &format!("{r}/src")); |
| 607 | let rep = judge(&gate(""), &p, &parse_strace(&trace)); |
| 608 | // src/x.c and the src listing are tracked and undeclared; build/x.o is the run's own. |
| 609 | assert_eq!( |
| 610 | rep.undeclared.keys().collect::<Vec<_>>(), |
| 611 | vec!["src", "src/x.c"] |
| 612 | ); |
| 613 | assert_eq!( |
| 614 | rep.ignored.get("not in the tree at HEAD (made by the run)"), |
| 615 | Some(&1) |
| 616 | ); |
| 617 | } |
| 618 | |
| 619 | #[test] |
| 620 | fn a_listing_is_covered_by_listed_and_only_by_it() { |
| 621 | let dir = tempdir(); |
| 622 | std::fs::create_dir_all(dir.join("crates/b/tests")).unwrap(); |
| 623 | std::fs::write(dir.join("crates/b/tests/t.rs"), "x").unwrap(); |
| 624 | let t = format!( |
| 625 | "9<cargo> openat(AT_FDCWD</r>, \"x\", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3<{}/crates/b/tests>\n", |
| 626 | dir.display() |
| 627 | ); |
| 628 | let opened = parse_strace(&t); |
| 629 | assert!(opened[0].dir); |
| 630 | let rep = judge(&gate(""), &places(&dir), &opened); |
| 631 | assert_eq!( |
| 632 | rep.undeclared.keys().collect::<Vec<_>>(), |
| 633 | vec!["crates/b/tests"] |
| 634 | ); |
| 635 | let m = gate("listed:\n - crates/*/tests\n"); |
| 636 | assert!(judge(&m, &places(&dir), &opened).ok(&m)); |
| 637 | // A file read inside a listed directory is not covered by the listing. |
| 638 | let f = format!( |
| 639 | "9<rustc> openat(AT_FDCWD</r>, \"x\", O_RDONLY) = 3<{}/crates/b/tests/t.rs>\n", |
| 640 | dir.display() |
| 641 | ); |
| 642 | assert!(!judge(&m, &places(&dir), &parse_strace(&f)).ok(&m)); |
| 643 | } |
| 644 | |
| 645 | fn tempdir() -> PathBuf { |
| 646 | let d = std::env::temp_dir().join(format!( |
| 647 | "aien-audit-{}-{:?}", |
| 648 | std::process::id(), |
| 649 | std::thread::current().id() |
| 650 | )); |
| 651 | let _ = std::fs::remove_dir_all(&d); |
| 652 | std::fs::create_dir_all(&d).unwrap(); |
| 653 | canon(&d) |
| 654 | } |
| 655 | } |