diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..e04df2d --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,29 @@ +name: lint + +on: + push: + pull_request: + +jobs: + clippy: + # R-I23 (Self's ruling, Cut 1's eighth fix batch, F1): this repo had no + # CI surface at all before this batch, so "denied in CI" had nowhere to + # attach. This is that surface: `clippy.toml`'s `disallowed-methods` + # list only stops a bypass of the frozen-symlink resolver's + # `FrozenSymlinkFs` port (see `src/drivers.rs`, `frozen_symlink_port`) + # if something actually runs clippy with the lint promoted to an error. + # + # R-I26 (Self's ruling, migration-hazard follow-up): the pre-existing + # `clippy::never_loop` this batch's parent scoped the workflow around is + # fixed (`host_actuator.rs`'s answerer thread used a `while let` whose + # every path returned; it is an `if let` now, since the outer `for` was + # already what retried across polls). Test code is exactly where a + # `disallowed_methods` bypass would hide, so `--all-targets` is back: + # this job now reaches it too, not just the library crate. + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + with: + components: clippy + - run: cargo clippy --all-targets --all-features -- -D clippy::disallowed_methods diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..05b9aa3 --- /dev/null +++ b/clippy.toml @@ -0,0 +1,25 @@ +# R-I23 (Self's ruling, Cut 1's eighth fix batch, F1): these four methods are +# how the frozen-symlink resolver's authority leaks. Soul pass 8 proved it by +# existence -- `open_frozen_symlink_frame` called `fs::read_link` directly +# while everything else in the resolver went through `FrozenSymlinkFs`, and +# restoring the pass-7 cubic bug as `candidate.canonicalize()?` (or +# `fs::canonicalize(&candidate)?`) at `resolve_frozen_source_symlink`'s +# `Normal` arm passed all 193 tests green, because nothing stopped the +# resolver from reaching past the port. +# +# This list is crate-wide because `disallowed-methods` has no per-module +# scope of its own. `src/drivers.rs` blanket-allows the lint at the top of +# the file (most of the crate has a legitimate reason to call these methods +# directly -- deployment staging, host executable-identity checks, artifact +# digesting, and so on are not part of frozen-symlink resolution) and the +# `frozen_symlink_port` submodule inside it re-denies the lint for its own +# scope, which is the only place these calls must not appear outside the +# port. A future edit that adds a bypass inside that module fails +# `cargo clippy` with this list's reason text; a call anywhere else in the +# crate is unaffected, exactly as before this batch. +disallowed-methods = [ + { path = "std::path::Path::canonicalize", reason = "frozen-symlink resolution must go through FrozenSymlinkFs::canonicalize (src/drivers.rs, frozen_symlink_port); this method is blanket-allowed outside that module" }, + { path = "std::fs::canonicalize", reason = "frozen-symlink resolution must go through FrozenSymlinkFs::canonicalize (src/drivers.rs, frozen_symlink_port); this function is blanket-allowed outside that module" }, + { path = "std::fs::symlink_metadata", reason = "frozen-symlink resolution must go through FrozenSymlinkFs::symlink_metadata (src/drivers.rs, frozen_symlink_port); this function is blanket-allowed outside that module" }, + { path = "std::fs::read_link", reason = "frozen-symlink resolution must go through FrozenSymlinkFs::read_link (src/drivers.rs, frozen_symlink_port); this function is blanket-allowed outside that module" }, +] diff --git a/src/drivers.rs b/src/drivers.rs index c000c51..4f4d008 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -1,3 +1,17 @@ +// R-I23 (Self's ruling, Cut 1's eighth fix batch, F1): `clippy.toml`'s +// `disallowed-methods` list has no per-module scope, so it applies to the +// whole crate by construction. This file uses `Path::canonicalize`, +// `fs::canonicalize`, `fs::symlink_metadata` and `fs::read_link` directly in +// many places that have nothing to do with frozen-symlink resolution +// (deployment staging, host executable-identity checks, artifact digesting, +// hardening, and more); the `frozen_symlink_port` submodule below is the one +// place those calls must go through `FrozenSymlinkFs` instead. This blanket +// allow, plus that submodule's own `#![deny(clippy::disallowed_methods)]`, +// is the scoping clippy.toml itself cannot express: Rust's lint-level +// attributes are lexically scoped, so the inner `deny` wins inside the +// submodule regardless of this outer `allow`, and nowhere else changes. +#![allow(clippy::disallowed_methods)] + use std::collections::BTreeMap; use std::ffi::{OsStr, OsString}; use std::fs::{self, OpenOptions}; @@ -30,6 +44,8 @@ use crate::deployment_plan::{ ArtifactReceipt, CompiledDeploymentPlan, ExternalInputMaterializationReceipt, GitlinkTreeFact, SOURCE_SELECTION_FACTS_SCHEMA, SealedRelease, SourceSelection, SourceSelectionFacts, }; +#[cfg(test)] +use crate::deployment_plan::compile_deployment_plan; use cultnet_rs::{ CultNetMessage, CultNetRawPayloadEncoding, CultNetWireContract, GAMECULT_RUNTIME_PRESENCE_HEALTH_SCHEMA, GAMECULT_RUNTIME_PRESENCE_HEALTH_SIGNING_PURPOSE, @@ -66,6 +82,17 @@ struct GitTreeEntry { /// first and only failing once written -- the previous shape let a 64 MiB /// blob with a symlink mode take peak RSS past 200 MB before `ENAMETOOLONG` /// finally closed it. +/// +/// R-I11 (Self's ruling, sixth Cut 1 fix batch, F4): this guard bounds the +/// buffer at `PATH_MAX`, which is the VFS's own ceiling on every target +/// Linux platform and so answers the "is this constant right" question by +/// itself. It does not follow that this guard "refuses exactly what the +/// kernel refuses" -- a filesystem may refuse less. A slow symlink on ext4 +/// (one long enough that the target does not fit inline) is bounded by the +/// filesystem's block size, so on a 1 KiB-block ext4 the kernel refuses +/// targets this guard still admits. This is a doc-accuracy note, not a +/// defect: the guard's job is to bound the in-memory buffer, not to +/// replicate every filesystem's own ceiling. #[cfg(unix)] const FROZEN_SOURCE_SYMLINK_TARGET_LIMIT: usize = libc::PATH_MAX as usize; @@ -1451,9 +1478,16 @@ impl GitSourceDriver { let target_len = fs::symlink_metadata(&target) .with_context(|| format!("inspecting {}", target.display()))? .len() as usize; + // R-I4 (Self's ruling, fifth Cut 1 fix batch): strictly + // less than, not `<=`. The kernel accepts a symlink + // target of exactly PATH_MAX-1 (4095) bytes and refuses + // PATH_MAX (4096) with ENAMETOOLONG, so this guard must + // refuse at 4096 too -- refusing exactly what the kernel + // refuses, with a message that names the reason instead + // of surfacing the OS's own error from the write below. ensure!( - target_len <= FROZEN_SOURCE_SYMLINK_TARGET_LIMIT, - "frozen source symlink target for {} exceeds the platform's path length limit ({target_len} > {FROZEN_SOURCE_SYMLINK_TARGET_LIMIT} bytes)", + target_len < FROZEN_SOURCE_SYMLINK_TARGET_LIMIT, + "frozen source symlink target for {} exceeds the platform's path length limit ({target_len} >= {FROZEN_SOURCE_SYMLINK_TARGET_LIMIT} bytes)", extra.path.display() ); symlink_targets.insert(extra.path.clone(), fs::read(&target)?); @@ -1466,9 +1500,12 @@ impl GitSourceDriver { } } "120000" => { + // R-I4 (Self's ruling, fifth Cut 1 fix batch): see the sibling + // guard above -- strictly less than, matching the kernel's real + // PATH_MAX-1 (4095) byte ceiling on a symlink target. ensure!( - size <= FROZEN_SOURCE_SYMLINK_TARGET_LIMIT, - "frozen source symlink target for {} exceeds the platform's path length limit ({size} > {FROZEN_SOURCE_SYMLINK_TARGET_LIMIT} bytes)", + size < FROZEN_SOURCE_SYMLINK_TARGET_LIMIT, + "frozen source symlink target for {} exceeds the platform's path length limit ({size} >= {FROZEN_SOURCE_SYMLINK_TARGET_LIMIT} bytes)", first.path.display() ); let mut content = vec![0u8; size]; @@ -1878,8 +1915,13 @@ impl SourcePort for GitSourceDriver { "frozen source receipt resolves outside its authority root" ); validate_frozen_source(&root)?; + // R-I25: recomputes under whichever shape (current, or either of + // the two `hash_frozen_source_tree` carried before this cut's sixth + // and seventh batches) actually matches the receipt, so a receipt + // frozen before an Idunn upgrade that changed this algorithm still + // verifies on resume. ensure!( - frozen_source_sha256(&root)? == receipt.snapshot_sha256, + verify_frozen_source_digest(&root, &receipt.snapshot_sha256)?, "frozen source snapshot differs from its receipt" ); let recipe_path = root.join(&plan.source.recipe_path); @@ -5886,6 +5928,18 @@ fn harden_frozen_source_tree(root: &Path, current: &Path) -> Result<()> { Ok(()) } +// R-I23 (Self's ruling, eighth Cut 1 fix batch, F1): everything the +// frozen-symlink resolver can reach lives in this submodule so that +// `#![deny(clippy::disallowed_methods)]` can cover exactly it -- the rest of +// `drivers.rs` blanket-allows the lint (see the file's top attribute) +// because most of the crate has a legitimate, unrelated reason to call +// `Path::canonicalize`/`fs::canonicalize`/`fs::symlink_metadata`/ +// `fs::read_link` directly. Everything this module exports is re-exported +// right below it so every existing caller and test keeps its current path. +mod frozen_symlink_port { + #![deny(clippy::disallowed_methods)] + use super::*; + /// S6 (Self's ruling, third Cut 1 fix batch): resolves a frozen source /// symlink's full chain against the tree's real, final published root. Every /// component the chain crosses that exists on disk is resolved for real @@ -5913,12 +5967,95 @@ fn harden_frozen_source_tree(root: &Path, current: &Path) -> Result<()> { /// symlinks, doubling or not, now costs O(n) real reads no matter how many /// times its target text repeats a name. #[cfg(unix)] -enum FrozenSymlinkStep { +pub(crate) enum FrozenSymlinkStep { CurDir, ParentDir, Normal(std::ffi::OsString), } +// R-I8 (Self's ruling, sixth Cut 1 fix batch): a test-only counter for the +// two syscalls whose per-component repetition is what made the old +// recursion cubic -- `canonicalize()` in `open_frozen_symlink_frame` and +// `symlink_metadata()` in the `Normal` arm below. A call count is +// deterministic under any host load; the wall clock is not (S6/F1: the +// clock-ratio pin this replaces was measured to invert under three +// concurrent Yggdrasil suites). Zero cost outside `cfg(test)`. +#[cfg(test)] +thread_local! { + static FROZEN_SYMLINK_PROBE_CALLS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[cfg(test)] +pub(crate) fn reset_frozen_symlink_probe_calls() { + FROZEN_SYMLINK_PROBE_CALLS.with(|calls| calls.set(0)); +} + +#[cfg(test)] +pub(crate) fn frozen_symlink_probe_calls() -> u64 { + FROZEN_SYMLINK_PROBE_CALLS.with(std::cell::Cell::get) +} + +#[cfg(test)] +fn record_frozen_symlink_probe_call() { + FROZEN_SYMLINK_PROBE_CALLS.with(|calls| calls.set(calls.get() + 1)); +} + +#[cfg(not(test))] +#[inline(always)] +fn record_frozen_symlink_probe_call() {} + +// R-I14 (Self's ruling, seventh Cut 1 fix batch, F1): the counter above was +// an honour system -- it only moved where a call site remembered to invoke +// it by hand, so Soul pass 7 restored the S5-1 bug (a per-component +// `canonicalize()`) without adding that call and the pin measured no +// regression at all. This narrow port is the fix: `canonicalize()` and +// `symlink_metadata()` are reachable from the resolver *only* through +// `FrozenSymlinkFs`, and the one production implementation charges the +// counter inside the same method that makes the syscall, so the two can no +// longer drift apart. `resolve_frozen_source_symlink` and +// `open_frozen_symlink_frame` are generic over this trait instead of +// calling `Path::canonicalize`/`fs::symlink_metadata` directly. +// +// R-I23 (Self's ruling, eighth Cut 1 fix batch, F1): `read_link` joined the +// port for the same reason -- Soul pass 8 found `open_frozen_symlink_frame` +// still calling `fs::read_link` directly, the proof the convention leaks. +// This module (`frozen_symlink_port`, below) is also now the only place in +// the crate where `clippy::disallowed_methods` denies calling +// `Path::canonicalize`, `fs::canonicalize`, `fs::symlink_metadata` or +// `fs::read_link` outside this trait; see `clippy.toml` and the `#![allow]` +// at the top of this file. +#[cfg(unix)] +pub(crate) trait FrozenSymlinkFs { + fn canonicalize(&self, path: &Path) -> std::io::Result; + fn symlink_metadata(&self, path: &Path) -> std::io::Result; + fn read_link(&self, path: &Path) -> std::io::Result; +} + +#[cfg(unix)] +pub(crate) struct StdFrozenSymlinkFs; + +// R-I23: this impl is the port's one production body, so it is the one +// place inside `frozen_symlink_port` allowed to call the real syscalls the +// rest of the module is denied from reaching around the port for. +#[cfg(unix)] +#[allow(clippy::disallowed_methods)] +impl FrozenSymlinkFs for StdFrozenSymlinkFs { + fn canonicalize(&self, path: &Path) -> std::io::Result { + record_frozen_symlink_probe_call(); + path.canonicalize() + } + + fn symlink_metadata(&self, path: &Path) -> std::io::Result { + record_frozen_symlink_probe_call(); + fs::symlink_metadata(path) + } + + fn read_link(&self, path: &Path) -> std::io::Result { + record_frozen_symlink_probe_call(); + fs::read_link(path) + } +} + #[cfg(unix)] fn frozen_symlink_steps(target: &Path) -> Result> { let mut steps = std::collections::VecDeque::new(); @@ -5933,14 +6070,14 @@ fn frozen_symlink_steps(target: &Path) -> Result, + symlink_path: PathBuf, resolved: PathBuf, remaining: std::collections::VecDeque, } @@ -5951,57 +6088,86 @@ struct FrozenSymlinkFrame { /// discovers -- the two call sites the old recursion covered with the same /// function. #[cfg(unix)] -fn open_frozen_symlink_frame( - canonical_root: &Path, +fn open_frozen_symlink_frame( + fs_port: &F, symlink_path: &Path, + ceiling: u32, budget: &mut u32, ) -> Result { + // R-I5 (Self's ruling, fifth Cut 1 fix batch): named, not just counted. + // `ceiling - *budget + 1` is the traversal this open would have been -- + // always `ceiling + 1` at the point this fires, since the ensure! below + // only trips on an exhausted budget, but spelling it this way keeps the + // number tied to the budget's own arithmetic rather than a second + // hand-copied constant. ensure!( *budget > 0, - "frozen source symlink chain exceeds the traversal budget" + "frozen source symlink chain reached {} at {}, past the {ceiling}-traversal ceiling \ + (matching the kernel's own ELOOP limit; see MAX_LINK_TRAVERSALS)", + ceiling - *budget + 1, + symlink_path.display() ); *budget -= 1; - let target = fs::read_link(symlink_path)?; + let target = fs_port.read_link(symlink_path)?; ensure!(target.is_relative(), "frozen source symlink is absolute"); let parent = symlink_path .parent() .context("frozen source symlink has no parent")?; - let resolved = parent.canonicalize().with_context(|| { + let resolved = fs_port.canonicalize(parent).with_context(|| { format!( "resolving frozen source symlink chain at {}", symlink_path.display() ) })?; - ensure!( - resolved.starts_with(canonical_root), - "frozen source symlink escapes its root" - ); + // R-I7 (Self's ruling, fifth Cut 1 fix batch): no containment check + // here -- Soul proved it redundant by deletion (S5-8). The frame this + // returns is always pushed onto the stack and walked through the + // `Normal` arm's own containment check below before `resolved` is read + // by anything outside this function. Ok(FrozenSymlinkFrame { - symlink_path: Some(symlink_path.to_path_buf()), + symlink_path: symlink_path.to_path_buf(), resolved, remaining: frozen_symlink_steps(&target)?, }) } #[cfg(unix)] -fn resolve_frozen_source_symlink(canonical_root: &Path, path: &Path) -> Result { +pub(crate) fn resolve_frozen_source_symlink( + fs_port: &F, + canonical_root: &Path, + path: &Path, +) -> Result { // Total distinct symlinks this one resolution may read, matching the old // `MAX_HOPS` chain-depth bound in spirit: a memoised re-visit is free, so // this is now a bound on real work rather than on nesting depth. + // + // R-I5 (Self's ruling, fifth Cut 1 fix batch), declared: 40 matches the + // kernel's own `ELOOP` symlink-resolution limit (Linux's + // `MAXSYMLINKS`/`ELOOP` ceiling), so a chain the kernel itself would + // refuse to resolve is refused here too. This is a deliberate, narrow + // departure from the third fix batch's "keep S6's behaviour exactly": + // the old recursion charged each *sibling* symlink at the same nesting + // depth as one hop, so a legitimate target reached through more than 39 + // symlinked path components -- accepted before this rewrite, because + // depth, not traversal count, was the old bound -- is now refused. + // Fail-closed at a kernel-matched boundary is judged the right + // behaviour, not a regression to patch around. const MAX_LINK_TRAVERSALS: u32 = 40; let mut budget = MAX_LINK_TRAVERSALS; let mut memo: std::collections::HashMap = std::collections::HashMap::new(); - let mut stack: Vec = - vec![open_frozen_symlink_frame(canonical_root, path, &mut budget)?]; + let mut stack: Vec = vec![open_frozen_symlink_frame( + fs_port, + path, + MAX_LINK_TRAVERSALS, + &mut budget, + )?]; loop { let top = stack.len() - 1; match stack[top].remaining.pop_front() { None => { let frame = stack.pop().expect("resolver stack is never empty"); - if let Some(symlink_path) = frame.symlink_path { - memo.insert(symlink_path, frame.resolved.clone()); - } + memo.insert(frame.symlink_path, frame.resolved.clone()); match stack.last_mut() { None => return Ok(frame.resolved), Some(parent) => parent.resolved = frame.resolved, @@ -6009,37 +6175,53 @@ fn resolve_frozen_source_symlink(canonical_root: &Path, path: &Path) -> Result

{} Some(FrozenSymlinkStep::ParentDir) => { - let frame = &mut stack[top]; - ensure!( - frame.resolved.pop() && frame.resolved.starts_with(canonical_root), - "frozen source symlink escapes its root" - ); + // R-I7 (Self's ruling, fifth Cut 1 fix batch): the `pop()` + // is load-bearing (it walks `resolved` up one component); + // the containment check that used to gate on its result is + // not (S5-8) -- the `Normal` arm's own check, two steps + // below, is what actually stands between this walk and an + // out-of-root path reaching `symlink_metadata`. + stack[top].resolved.pop(); } Some(FrozenSymlinkStep::Normal(part)) => { let frame = &mut stack[top]; frame.resolved.push(&part); + // R-I12 (Self's ruling, sixth Cut 1 fix batch, F5): + // redundant with the loop-tail check below, not load-bearing + // -- Soul pass 6 built 20 hand escape shapes and confirmed + // deleting only this `ensure!` still leaves the suite and + // all 20 green, because the tail check runs after every + // iteration of this loop including this one. Recorded, not + // deleted: two checks costing one cheap `starts_with` is a + // legible redundancy, and the tail check is the one this + // file has already relied on to survive alone (S5-8). ensure!( frame.resolved.starts_with(canonical_root), "frozen source symlink escapes its root" ); let candidate = frame.resolved.clone(); - match fs::symlink_metadata(&candidate) { + match fs_port.symlink_metadata(&candidate) { Ok(metadata) if metadata.file_type().is_symlink() => { if let Some(cached) = memo.get(&candidate) { stack[top].resolved = cached.clone(); } else { stack.push(open_frozen_symlink_frame( - canonical_root, + fs_port, &candidate, + MAX_LINK_TRAVERSALS, &mut budget, )?); } } Ok(_) => { - let real = candidate.canonicalize().with_context(|| { - format!("resolving frozen source symlink chain at {}", candidate.display()) - })?; - stack[top].resolved = real; + // `frame.resolved` is already canonical here: the + // parent was canonicalized when this frame opened, + // and every earlier component in this walk passed + // through this same match. Re-canonicalizing a path + // that is already canonical cannot change the + // answer, and re-running it per component is what + // made a long symlink chain cost roughly O(k^3). + stack[top].resolved = candidate; } Err(ref error) if error.kind() == std::io::ErrorKind::NotFound => { // Nothing exists here yet: the remaining components, @@ -6055,6 +6237,33 @@ fn resolve_frozen_source_symlink(canonical_root: &Path, path: &Path) -> Result

Result

Result<()> { - let canonical_root = root.canonicalize().context("resolving frozen source root")?; - resolve_frozen_source_symlink(&canonical_root, path).map(|_| ()) +pub(crate) fn validate_frozen_source_symlink(root: &Path, path: &Path) -> Result<()> { + let canonical_root = StdFrozenSymlinkFs + .canonicalize(root) + .context("resolving frozen source root")?; + resolve_frozen_source_symlink(&StdFrozenSymlinkFs, &canonical_root, path).map(|_| ()) } +} // mod frozen_symlink_port + +// R-I23: flattened back into `drivers`'s own namespace so every call site +// above and every test below keeps referring to these by their bare names, +// unchanged by the module boundary this batch drew around them. +use frozen_symlink_port::*; + /// S6: run once, against the tree's real, final published path -- after the /// atomic rename off `.partial`, in `freeze_exact`, and again (read-only) /// whenever `observe_frozen` re-checks a tree it did not just write. Separate @@ -6117,9 +6335,15 @@ fn validate_frozen_source_tree(root: &Path, current: &Path) -> Result<()> { "frozen source contains forbidden .git metadata" ); if metadata.is_dir() { + // R-I9 (Self's ruling, sixth Cut 1 fix batch): masking with `0o777` + // (S6/F2) let `0o4555`/`0o2555`/`0o1555` all satisfy `== 0o555`, + // since setuid/setgid/sticky live in the `0o7000` range the old mask + // discarded. Nothing in a frozen source has any business carrying + // those bits, so they are refused outright by widening the mask to + // `0o7777` rather than masked away. ensure!( - metadata.permissions().mode() & 0o777 == 0o555, - "frozen source directory is not 0555" + metadata.permissions().mode() & 0o7777 == 0o555, + "frozen source directory is not 0555 (setuid, setgid and sticky bits are refused)" ); let mut entries = fs::read_dir(current)?.collect::>>()?; entries.sort_by_key(|entry| entry.file_name()); @@ -6127,9 +6351,11 @@ fn validate_frozen_source_tree(root: &Path, current: &Path) -> Result<()> { validate_frozen_source_tree(root, &entry.path())?; } } else if metadata.is_file() { + // R-I9: same widened mask -- `0o4555`/`0o2555`/`0o1555` no longer + // pass as `0o555`. ensure!( - matches!(metadata.permissions().mode() & 0o777, 0o444 | 0o555), - "frozen source file has a noncanonical mode" + matches!(metadata.permissions().mode() & 0o7777, 0o444 | 0o555), + "frozen source file has a noncanonical mode (setuid, setgid and sticky bits are refused)" ); } else if metadata.file_type().is_symlink() { validate_frozen_source_symlink(root, current)?; @@ -6158,7 +6384,7 @@ fn frozen_source_sha256(root: &Path) -> Result { #[cfg(unix)] fn hash_frozen_source_tree(root: &Path, current: &Path, hasher: &mut Sha256) -> Result<()> { - use std::os::unix::fs::PermissionsExt; + use std::os::unix::fs::{MetadataExt, PermissionsExt}; let mut entries = fs::read_dir(current)?.collect::>>()?; entries.sort_by_key(|entry| entry.file_name()); @@ -6170,17 +6396,34 @@ fn hash_frozen_source_tree(root: &Path, current: &Path, hasher: &mut Sha256) -> hasher.update(b"dir\0"); hasher.update(relative.as_bytes()); hasher.update(b"\0"); + // R-I9 (Self's ruling, sixth Cut 1 fix batch): S6/F2 found the + // digest hashed neither a directory's mode nor any entry's gid, + // so a post-write tamper that added setgid/sticky to a + // directory, or rewrote its owning group, re-observed clean + // against its receipt. The full mode (not just an executable-bit + // boolean) and the owning gid are now covered for directories + // too, each pinned by its own test below. + hasher.update((metadata.permissions().mode() & 0o7777).to_le_bytes()); + hasher.update(b"\0"); + hasher.update(metadata.gid().to_le_bytes()); + hasher.update(b"\0"); + hash_frozen_source_xattrs(&path, hasher)?; hash_frozen_source_tree(root, &path, hasher)?; } else if metadata.is_file() { hasher.update(b"file\0"); hasher.update(relative.as_bytes()); hasher.update(b"\0"); - hasher.update( - (metadata.permissions().mode() & 0o111 != 0) - .to_string() - .as_bytes(), - ); + // R-I9: was `mode & 0o111 != 0` (the executable-bit boolean + // alone, from R-I3/S5-4); now the full mode, which is a strict + // superset -- a setuid/setgid/sticky bit changes this term even + // when the executable bit does not, and this widened validator + // never legally admits the extra bits, so the field is decisive + // here even though it can never be observed to vary innocently. + hasher.update((metadata.permissions().mode() & 0o7777).to_le_bytes()); hasher.update(b"\0"); + hasher.update(metadata.gid().to_le_bytes()); + hasher.update(b"\0"); + hash_frozen_source_xattrs(&path, hasher)?; hasher.update(metadata.len().to_le_bytes()); // S9: streamed in fixed-size chunks rather than `fs::read`ing the // whole file, so peak RSS is bounded by the buffer, not by the @@ -6197,6 +6440,275 @@ fn hash_frozen_source_tree(root: &Path, current: &Path, hasher: &mut Sha256) -> } hasher.update(&buffer[..read]); } + } else if metadata.file_type().is_symlink() { + let target = fs::read_link(&path)?; + hasher.update(b"link\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + hasher.update(target.as_os_str().as_encoded_bytes()); + hasher.update(b"\0"); + hash_frozen_source_xattrs(&path, hasher)?; + } else { + bail!("frozen source contains a special filesystem entry") + } + } + Ok(()) +} + +// R-I18 (Self's ruling, seventh Cut 1 fix batch, F3): a real root-owned +// 0555 tree with `security.capability` set to `cap_setuid=ep` -- the +// setuid-equivalent grant that carries no setuid *bit* -- validated +// ACCEPT and re-hashed unchanged under both `validate_frozen_source` and +// `frozen_source_sha256`. Soul pass 7 measured it directly, since none of +// R-I9/R-I13/R-I16's mode-and-gid coverage can see a grant that lives +// entirely outside `mode & 0o7777`. This walks and hashes every extended +// attribute on the entry (name and value, sorted by name so the digest is +// order-independent of `listxattr`'s own ordering, which is unspecified), +// covering `security.capability` and any other xattr the same way. This is +// **detection**, not prevention: it makes a tamper that sets, changes, or +// removes an xattr show up in the digest of a tree already on disk. What +// prevents a file capability from being *exercised* on Idunn's own +// deployment path is `no-new-privileges` on the runner and the systemd +// workload, the same mitigation already relied on for setuid -- this +// digest term exists so `observe_frozen`'s "this is the tree we froze" +// verdict is no longer false for xattrs, per Cut 4. Soul's probe ran as +// root, which is exactly the precondition `setxattr` on `security.capability` +// needs; like the setuid/gid probes before it, this does **not** establish +// that an unprivileged attacker can set one on a tree they do not own. +#[cfg(unix)] +fn hash_frozen_source_xattrs(path: &Path, hasher: &mut Sha256) -> Result<()> { + hash_xattr_list(&read_sorted_xattrs(path)?, hasher); + Ok(()) +} + +// R-I21 (Self's ruling, eighth Cut 1 fix batch, F3): shared with +// `digest_tree_xattrs` below, so the artifact digest's xattr term is framed +// identically to the frozen-source digest's -- same tag, same count prefix, +// same per-attribute name/NUL/length-prefixed-value shape. Soul pass 8's F4 +// pinned this framing on the frozen-source side; sharing the writer means +// the artifact side cannot quietly drift from it. +fn hash_xattr_list(xattrs: &[(Vec, Vec)], hasher: &mut Sha256) { + hasher.update(b"xattrs\0"); + hasher.update((xattrs.len() as u64).to_le_bytes()); + for (name, value) in xattrs { + hasher.update(name); + hasher.update(b"\0"); + hasher.update((value.len() as u64).to_le_bytes()); + hasher.update(value); + } +} + +/// Reads every extended attribute on `path` (without following a symlink), +/// sorted by name, as `(name, value)` pairs. A filesystem or entry that +/// does not support extended attributes at all reports an empty list +/// rather than an error, since that is a property of the storage, not a +/// tamper. +#[cfg(unix)] +fn read_sorted_xattrs(path: &Path) -> Result, Vec)>> { + use std::os::unix::ffi::OsStrExt; + + let cpath = std::ffi::CString::new(path.as_os_str().as_bytes()) + .with_context(|| format!("{} contains a NUL byte", path.display()))?; + + let mut names_buf = Vec::new(); + loop { + let needed = unsafe { libc::llistxattr(cpath.as_ptr(), std::ptr::null_mut(), 0) }; + if needed < 0 { + let error = std::io::Error::last_os_error(); + return match error.raw_os_error() { + Some(libc::ENOTSUP) | Some(libc::EOPNOTSUPP) => Ok(Vec::new()), + _ => Err(error) + .with_context(|| format!("listing extended attributes on {}", path.display())), + }; + } + if needed == 0 { + return Ok(Vec::new()); + } + names_buf = vec![0u8; needed as usize]; + let written = unsafe { + libc::llistxattr( + cpath.as_ptr(), + names_buf.as_mut_ptr() as *mut libc::c_char, + names_buf.len(), + ) + }; + if written < 0 { + // The attribute set changed size between the sizing call and + // this one (another process added/removed an xattr); retry. + continue; + } + names_buf.truncate(written as usize); + break; + } + + let mut names: Vec> = names_buf + .split(|&byte| byte == 0) + .filter(|name| !name.is_empty()) + .map(|name| name.to_vec()) + .collect(); + names.sort(); + + let mut result = Vec::with_capacity(names.len()); + for name in names { + let cname = std::ffi::CString::new(name.clone()) + .with_context(|| format!("extended attribute name on {} contains a NUL", path.display()))?; + let needed = + unsafe { libc::lgetxattr(cpath.as_ptr(), cname.as_ptr(), std::ptr::null_mut(), 0) }; + ensure!( + needed >= 0, + "reading extended attribute {} on {}: {}", + String::from_utf8_lossy(&name), + path.display(), + std::io::Error::last_os_error() + ); + let mut value = vec![0u8; needed as usize]; + let written = unsafe { + libc::lgetxattr( + cpath.as_ptr(), + cname.as_ptr(), + value.as_mut_ptr() as *mut libc::c_void, + value.len(), + ) + }; + ensure!( + written >= 0, + "reading extended attribute {} on {}: {}", + String::from_utf8_lossy(&name), + path.display(), + std::io::Error::last_os_error() + ); + value.truncate(written as usize); + result.push((name, value)); + } + Ok(result) +} + +// R-I25 (Self's finding: check the frozen-source digest for the same +// exposure). `frozen_source_sha256`'s algorithm changed twice before this +// batch touched anything: R-I9 (sixth batch) added the mode and gid terms, +// R-I18 (seventh batch) added the xattr term. Neither landed with a +// versioning scheme, and `observe_frozen` compares a fresh recompute +// against `receipt.snapshot_sha256` -- a `FrozenSourceReceipt` persisted in +// `DeploymentTransaction.frozen_source` (`control_plane.rs`) between the +// `freeze()` step and the step that calls `observe_frozen`, each a separate +// `advance_sealing` call that can cross a restart. So yes, this has the same +// structural exposure R-I20 had on the artifact side: an Idunn upgrade that +// changed this algorithm, landing while a transaction's `freeze()` receipt +// is persisted but not yet observed, would fail `observe_frozen` on resume. +// The window is narrower -- one in-flight Sealing-phase transaction, not +// every already-installed and already-running workload -- but it is real, +// and this cut's own batches 6 through 8 already crossed it twice. Applying +// R-I25's answer here too: recompute in whichever of the three shapes that +// have ever shipped actually matches, current first. +// +// `snapshot_sha256` (`FrozenSourceReceipt`) has no `is_sha256`-style +// external validator the way `expected.artifact_sha256` does -- it is +// validated locally by `require_sha256_id` in this same crate -- but it is +// still a bare `sha256-<64 lower hex>` id with the same "one value, no +// metadata channel" shape, so the same recompute-and-try approach applies +// rather than inventing a tag this one value has never carried. +#[cfg(unix)] +pub(crate) fn verify_frozen_source_digest(root: &Path, stored_sha256_id: &str) -> Result { + if frozen_source_sha256(root)? == stored_sha256_id { + return Ok(true); + } + if frozen_source_sha256_legacy(root, FrozenSourceDigestShape::ModeAndGidOnly)? + == stored_sha256_id + { + return Ok(true); + } + Ok( + frozen_source_sha256_legacy(root, FrozenSourceDigestShape::ExecutableBitOnly)? + == stored_sha256_id, + ) +} + +#[cfg(not(unix))] +pub(crate) fn verify_frozen_source_digest(_root: &Path, _stored_sha256_id: &str) -> Result { + bail!("frozen source observation requires Unix permissions") +} + +/// The two shapes `hash_frozen_source_tree` carried before this batch, kept +/// only so a receipt sealed under either still verifies. Not used to seal +/// anything -- `freeze_exact` always calls `frozen_source_sha256`, the +/// current shape, directly. +#[cfg(unix)] +#[derive(Clone, Copy)] +enum FrozenSourceDigestShape { + /// R-I9 (sixth batch) through the commit before R-I18 (seventh batch): + /// full mode and gid, no xattr term. + ModeAndGidOnly, + /// Before R-I9 (R-I3/S5-4): an executable-bit boolean alone, no gid, no + /// xattr term. + ExecutableBitOnly, +} + +#[cfg(unix)] +fn frozen_source_sha256_legacy(root: &Path, shape: FrozenSourceDigestShape) -> Result { + let mut hasher = Sha256::new(); + hash_frozen_source_tree_legacy(root, root, &mut hasher, shape)?; + Ok(format!("sha256-{:x}", hasher.finalize())) +} + +#[cfg(unix)] +fn hash_frozen_source_tree_legacy( + root: &Path, + current: &Path, + hasher: &mut Sha256, + shape: FrozenSourceDigestShape, +) -> Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + + let mut entries = fs::read_dir(current)?.collect::>>()?; + entries.sort_by_key(|entry| entry.file_name()); + for entry in entries { + let path = entry.path(); + let relative = normalized_relative(path.strip_prefix(root)?)?; + let metadata = fs::symlink_metadata(&path)?; + if metadata.is_dir() { + hasher.update(b"dir\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + if let FrozenSourceDigestShape::ModeAndGidOnly = shape { + hasher.update((metadata.permissions().mode() & 0o7777).to_le_bytes()); + hasher.update(b"\0"); + hasher.update(metadata.gid().to_le_bytes()); + hasher.update(b"\0"); + } + hash_frozen_source_tree_legacy(root, &path, hasher, shape)?; + } else if metadata.is_file() { + hasher.update(b"file\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + match shape { + FrozenSourceDigestShape::ModeAndGidOnly => { + hasher.update((metadata.permissions().mode() & 0o7777).to_le_bytes()); + hasher.update(b"\0"); + hasher.update(metadata.gid().to_le_bytes()); + hasher.update(b"\0"); + } + FrozenSourceDigestShape::ExecutableBitOnly => { + hasher.update( + (metadata.permissions().mode() & 0o111 != 0) + .to_string() + .as_bytes(), + ); + hasher.update(b"\0"); + } + } + hasher.update(metadata.len().to_le_bytes()); + let mut file = fs::File::open(&path) + .with_context(|| format!("opening {} to hash it", path.display()))?; + let mut buffer = [0u8; 1 << 16]; + loop { + let read = file + .read(&mut buffer) + .with_context(|| format!("reading {} to hash it", path.display()))?; + if read == 0 { + break; + } + hasher.update(&buffer[..read]); + } } else if metadata.file_type().is_symlink() { let target = fs::read_link(&path)?; hasher.update(b"link\0"); @@ -7416,11 +7928,25 @@ pub(crate) fn copy_artifact(root: &Path, source: &Path, destination: &Path) -> R } } +// R-I20 (Self's ruling, eighth Cut 1 fix batch, F2): the single-file branch +// used to grow its own copy of the digest -- `raw_sha256(&bytes)` alone, no +// mode, gid or (per R-I21) xattr term -- and it is the branch `host.rs` +// actually takes for an installed executable, immediately before launch and +// while the process is running. It now delegates to `digest_file_entry`, the +// same primitive `digest_tree`'s own file arm uses below, so a bare artifact +// file and a file inside an artifact tree are hashed by one body instead of +// two that can drift apart. pub(crate) fn digest_artifact(path: &Path) -> Result<(String, u64)> { let metadata = fs::symlink_metadata(path)?; if metadata.is_file() { - let bytes = fs::read(path)?; - return Ok((raw_sha256(&bytes), bytes.len().try_into()?)); + let relative = normalized_relative(Path::new( + path.file_name() + .context("artifact file path has no file name")?, + ))?; + let mut hasher = Sha256::new(); + let mut size = 0_u64; + digest_file_entry(path, &relative, &metadata, &mut hasher, &mut size)?; + return Ok((format!("{:x}", hasher.finalize()), size)); } ensure!(metadata.is_dir(), "artifact is not a file or directory"); let mut hasher = Sha256::new(); @@ -7430,6 +7956,47 @@ pub(crate) fn digest_artifact(path: &Path) -> Result<(String, u64)> { Ok((format!("{:x}", hasher.finalize()), size)) } +// R-I25 (Self's ruling: the digest format change is a migration hazard): +// R-I20 changed the bytes `digest_artifact`'s bare-file branch produces, but +// three stored values are compared against a fresh recompute of it forever +// after they are sealed -- `expected.artifact_sha256` +// (`cultnet_rs::IdunnExpectedIncarnationRecord`), `ArtifactReceipt::sha256` +// (`SealedRelease`, in `verify_artifacts` below), and +// `HostWorkloadObservation::executable_sha256` -- so the first Idunn upgrade +// past R-I20 would refuse every already-installed and already-running +// workload sealed under the old shape. +// +// The ruling asked for an explicit version tag recorded with the stored +// value. That is not available here: `expected.artifact_sha256` and +// `ArtifactReceipt::sha256` are both validated to the bare `sha256-<64 lower +// hex>` shape (`is_sha256` in the pinned `cultnet-rs` git dependency; +// `require_sha256` in `deployment_plan.rs`), leaving no channel to carry a +// tag alongside the digest without widening a schema this worktree has no +// authority to change (`cultnet-rs` is a separate repo, pinned by git rev in +// `Cargo.toml`). There has only ever been one prior shape, so verification +// instead tries the current algorithm first and falls back to the exact +// bare-content shape `digest_artifact` produced before R-I20 -- the same +// effect as reading an implicit version, without a tag this worktree cannot +// add. New seals always call `digest_artifact` directly and so always use +// the current shape; nothing here ever rewrites a stored value to match. +pub(crate) fn verify_artifact_digest(path: &Path, stored_sha256_id: &str) -> Result { + let (current_hex, _) = digest_artifact(path)?; + if format!("sha256-{current_hex}") == stored_sha256_id { + return Ok(true); + } + // The pre-R-I20 shape only ever applied to the bare-file branch; the + // directory branch (`digest_tree`) has not changed, so no fallback + // applies to a directory artifact. + let metadata = fs::symlink_metadata(path)?; + if metadata.is_file() { + let legacy_hex = raw_sha256(&fs::read(path)?); + if format!("sha256-{legacy_hex}") == stored_sha256_id { + return Ok(true); + } + } + Ok(false) +} + fn digest_tree(root: &Path, current: &Path, hasher: &mut Sha256, size: &mut u64) -> Result<()> { let mut entries = fs::read_dir(current)?.collect::>>()?; entries.sort_by_key(|entry| entry.file_name()); @@ -7442,21 +8009,55 @@ fn digest_tree(root: &Path, current: &Path, hasher: &mut Sha256, size: &mut u64) hasher.update(b"dir\0"); hasher.update(relative.as_bytes()); hasher.update(b"\0"); + // R-I16 (Self's ruling, seventh Cut 1 fix batch, F4): R-I13 gave + // this digest an executable-bit boolean for files and nothing + // at all for directories. Soul pass 7 measured it blind: setuid, + // setgid, uid, gid and the read bits were all invisible + // (`0o500` hashed the same as `0o555`), and a directory carried + // no mode term whatsoever. This is the *artifact* digest, + // re-derived against the sealed receipt immediately before + // launch, so that blindness meant a post-install tamper adding + // setuid to an installed binary re-verified clean. Widened to + // the full `mode & 0o7777` and the owning gid, for directories + // as well as files, matching `hash_frozen_source_tree`'s own + // R-I9 treatment term for term. Each term is pinned by its own + // test below so narrowing any one of them back down is caught. + hasher.update(digest_tree_mode(&metadata).to_le_bytes()); + hasher.update(b"\0"); + hasher.update(digest_tree_gid(&metadata).to_le_bytes()); + hasher.update(b"\0"); + // R-I21 (Self's ruling, eighth Cut 1 fix batch, F3): the + // artifact digest had no xattr term at all -- the asymmetry ran + // the wrong way, since this is the binary that gets exec'd and + // re-verified before every launch, unlike the frozen source's + // 0555 root-owned tree nothing executes. Covers all three arms + // (dir/file/link), matching `hash_frozen_source_tree`'s R-I18 + // coverage term for term. + digest_tree_xattrs(&path, hasher)?; digest_tree(root, &path, hasher, size)?; } else if metadata.is_file() { - let bytes = fs::read(&path)?; - hasher.update(b"file\0"); - hasher.update(relative.as_bytes()); - hasher.update(b"\0"); - hasher.update((bytes.len() as u64).to_le_bytes()); - hasher.update(&bytes); - *size = size.saturating_add(bytes.len().try_into()?); + digest_file_entry(&path, &relative, &metadata, hasher, size)?; } else if metadata.file_type().is_symlink() { let target = fs::read_link(&path)?; hasher.update(b"link\0"); hasher.update(relative.as_bytes()); hasher.update(b"\0"); hasher.update(target.as_os_str().as_encoded_bytes()); + // R-I13: the second half of R-I3's treatment (S5-3) -- a + // terminator after the target, matching the frozen-source + // digest's own symlink arm and the self-delimiting shape the + // file arm above already has via its length prefix. Not + // independently pinned by a test (R-I15, Self's ruling, seventh + // Cut 1 fix batch, F2): no legitimate fixture can construct a + // colliding pair, because the link name is already + // NUL-terminated ahead of the target above, so two differing + // (name, target) pairs always diverge there regardless of this + // terminator, and no real symlink target or filename can carry + // the NUL byte a boundary collision here would require. See the + // note in `mod tests` where the disproved fixture used to sit. + hasher.update(b"\0"); + // R-I21: the symlink arm's xattr term. + digest_tree_xattrs(&path, hasher)?; *size = size.saturating_add(target.as_os_str().len().try_into()?); } else { bail!("artifact tree contains a special filesystem entry") @@ -7465,6 +8066,78 @@ fn digest_tree(root: &Path, current: &Path, hasher: &mut Sha256, size: &mut u64) Ok(()) } +// R-I20: the one place a file entry is hashed for the artifact digest, +// shared by `digest_tree`'s own file arm and `digest_artifact`'s bare-file +// branch above, so the two can no longer carry different terms. +fn digest_file_entry( + path: &Path, + relative: &str, + metadata: &fs::Metadata, + hasher: &mut Sha256, + size: &mut u64, +) -> Result<()> { + let bytes = fs::read(path)?; + hasher.update(b"file\0"); + hasher.update(relative.as_bytes()); + hasher.update(b"\0"); + // R-I13 (Self's ruling, sixth Cut 1 fix batch, F6)/R-I16 (Self's ruling, + // seventh Cut 1 fix batch, F4): was an executable-bit boolean alone; now + // the full mode plus gid, matching the directory arm's own widening. + hasher.update(digest_tree_mode(metadata).to_le_bytes()); + hasher.update(b"\0"); + hasher.update(digest_tree_gid(metadata).to_le_bytes()); + hasher.update(b"\0"); + // R-I21: the file arm's xattr term. + digest_tree_xattrs(path, hasher)?; + hasher.update((bytes.len() as u64).to_le_bytes()); + hasher.update(&bytes); + *size = size.saturating_add(bytes.len().try_into()?); + Ok(()) +} + +// R-I21: the artifact digest's xattr term, framed identically to the +// frozen-source digest's own (`hash_xattr_list`, shared by both). Unlike the +// frozen source, artifact digesting runs on every target platform this +// daemon ships for, not just Unix, so the reader is platform-conditional +// while the framing stays the same shape everywhere: an empty attribute list +// on a platform without extended-attribute support, not a missing term. +fn digest_tree_xattrs(path: &Path, hasher: &mut Sha256) -> Result<()> { + hash_xattr_list(&digest_tree_read_sorted_xattrs(path)?, hasher); + Ok(()) +} + +#[cfg(unix)] +fn digest_tree_read_sorted_xattrs(path: &Path) -> Result, Vec)>> { + read_sorted_xattrs(path) +} + +#[cfg(not(unix))] +fn digest_tree_read_sorted_xattrs(_path: &Path) -> Result, Vec)>> { + Ok(Vec::new()) +} + +#[cfg(unix)] +fn digest_tree_mode(metadata: &fs::Metadata) -> u32 { + use std::os::unix::fs::PermissionsExt; + metadata.permissions().mode() & 0o7777 +} + +#[cfg(not(unix))] +fn digest_tree_mode(_metadata: &fs::Metadata) -> u32 { + 0 +} + +#[cfg(unix)] +fn digest_tree_gid(metadata: &fs::Metadata) -> u32 { + use std::os::unix::fs::MetadataExt; + metadata.gid() +} + +#[cfg(not(unix))] +fn digest_tree_gid(_metadata: &fs::Metadata) -> u32 { + 0 +} + pub(crate) fn raw_sha256(bytes: &[u8]) -> String { format!("{:x}", Sha256::digest(bytes)) } @@ -8284,6 +8957,126 @@ mod tests { Ok(String::from_utf8(output.stdout)?.trim().to_owned()) } + /// R-I7 (Self's ruling, fifth Cut 1 fix batch): the hostile-tree + /// fixture builders each rebuilt this identically -- hashes `content` + /// into a loose object with real Git, the way a hand-built tree entry + /// needs a real blob to point at. + #[cfg(unix)] + fn hash_object(repo: &Path, content: &[u8]) -> Result { + let mut child = Command::new("git") + .args(["-c", "safe.directory=*", "-C"]) + .arg(repo) + .args(["hash-object", "-w", "--stdin"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + child.stdin.take().unwrap().write_all(content)?; + let output = child.wait_with_output()?; + ensure!(output.status.success(), "git hash-object failed"); + Ok(String::from_utf8(output.stdout)?.trim().to_owned()) + } + + /// R-I7 (Self's ruling, fifth Cut 1 fix batch): writes a literal tree + /// object (`git hash-object -t tree --literally`) from `(mode, name, + /// sha1-hex)` entries, without Git re-sorting or re-validating them -- + /// the only way this suite's hostile fixtures (duplicate names, `.GIT` + /// look-alikes, unsorted entries) get onto disk at all. + #[cfg(unix)] + fn literal_tree(repo: &Path, entries: &[(&str, &str, &str)]) -> Result { + let hex = |s: &str| -> Vec { + (0..20) + .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap()) + .collect() + }; + let mut raw = Vec::new(); + for (mode, name, sha) in entries { + raw.extend_from_slice(format!("{mode} {name}\0").as_bytes()); + raw.extend(hex(sha)); + } + let mut child = Command::new("git") + .args(["-c", "safe.directory=*", "-C"]) + .arg(repo) + .args(["hash-object", "-t", "tree", "--literally", "-w", "--stdin"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + child.stdin.take().unwrap().write_all(&raw)?; + let output = child.wait_with_output()?; + ensure!(output.status.success(), "git hash-object --literally failed"); + Ok(String::from_utf8(output.stdout)?.trim().to_owned()) + } + + /// R-I7 (Self's ruling, fifth Cut 1 fix batch): a `.GIT`-look-alike + /// tree (a `deployment.toml` sibling to a `40000`-mode entry named some + /// case/normalization variant of `.git`, holding a hostile + /// `[core] fsmonitor = ...` config blob) -- the shape six of the + /// eleven `freeze_exact_refuses_already_local_hostile_objects_via_its_ + /// own_fsck` fixtures share, differing only in that one entry's name. + #[cfg(unix)] + fn dotgit_lookalike( + repo: &Path, + recipe: &str, + name: &str, + ) -> Result> { + let cfg = hash_object(repo, b"[core]\n\tfsmonitor = touch /tmp/idunn-test-pwned\n")?; + let inner = literal_tree(repo, &[("100644", "config", &cfg)])?; + let mut entries = vec![ + ("100644".to_owned(), "deployment.toml".to_owned(), recipe.to_owned()), + ("40000".to_owned(), name.to_owned(), inner), + ]; + entries.sort_by(|a, b| a.1.cmp(&b.1)); + Ok(entries) + } + + /// R-I7 (Self's ruling, fifth Cut 1 fix batch): runs a Git command + /// against `repo` as the identity `repo` was already `chown`ed to, + /// exactly the arguments a real unprivileged Git identity would use -- + /// needed once this process (running as root, to exercise Idunn's own + /// root-owned actuator) would otherwise be refused by Git's "dubious + /// ownership" check for touching a directory it does not own. + #[cfg(unix)] + fn git_owned(repo: &Path, args: &[&str]) -> Result { + let output = Command::new("git") + .args([ + "-c", + "safe.directory=*", + "-c", + "user.name=Idunn Test", + "-c", + "user.email=idunn-test@example.invalid", + "-C", + ]) + .arg(repo) + .args(args) + .output()?; + ensure!( + output.status.success(), + "git {:?} failed: {}", + args, + String::from_utf8_lossy(&output.stderr) + ); + Ok(String::from_utf8(output.stdout)?.trim().to_owned()) + } + + /// R-I7 (Self's ruling, fifth Cut 1 fix batch): `chown -R` a fixture + /// origin repository to the unprivileged Git identity (uid/gid 1000) + /// these hostile-tree fixtures build and commit as, so a later `git` + /// invocation running as this process's real identity (root, in every + /// environment that actually exercises `freeze_exact`'s root-only + /// guard) needs `git_owned` rather than being refused outright. + #[cfg(unix)] + fn chown_to_source_identity(repo: &Path) -> Result<()> { + ensure!( + Command::new("/bin/chown") + .args(["-R", "1000:1000"]) + .arg(repo) + .status()? + .success(), + "chowning the fixture origin repository" + ); + Ok(()) + } + fn fixture_docker_runner_binding() -> DockerRunnerBinding { DockerRunnerBinding { image: "eureka-verify-rust".to_owned(), @@ -11376,215 +12169,6 @@ mod tests { Ok(()) } - // --------------------------------------------------------------------- - // S1 (Self's ruling, third Cut 1 fix batch): fsck guards the fetch in - // `resolve()` as well as the one in `freeze_exact`, and `freeze_exact` - // also fscks the selected tree so that objects already local get - // checked too. - // - // Discrepancy: `OperatorBinding::validate()` (`deployment.rs:1027-1040`) - // requires `repository.origin` to start with `https://`, unconditionally - // -- `resolve()` calls it first thing, and `freeze()` calls it through - // `ResolvedSource::validate_against`. No test in this suite stands up a - // real HTTPS Git origin (every existing fixture uses `file://`), and - // adding one is out of this fix batch's scope. This fixture instead - // reproduces `resolve()`'s exact fetch invocation -- literally the same - // arguments `resolve()` passes to `self.git(...)`, this fix included -- - // against the checkout, the same approximation Soul's own probe used - // (`resolve_like_fetch` in `probe4_soul3.rs`), followed by the real, - // unmodified `freeze_exact`. That is "resolve() then freeze()" in every - // way this suite can exercise without a live HTTPS remote: it proves the - // fetch line S1 changed, not a hand-built substitute for it, refuses the - // hostile object, and that `freeze_exact`'s own explicit fsck refuses it - // independently even if the fetch line's guard were ever weakened. - #[cfg(unix)] - #[test] - fn resolve_style_fetch_then_freeze_exact_refuses_hostile_trees() -> Result<()> { - use std::os::unix::fs::PermissionsExt; - - fn hash_object(repo: &Path, content: &[u8]) -> Result { - let mut child = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(repo) - .args(["hash-object", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - child.stdin.take().unwrap().write_all(content)?; - let output = child.wait_with_output()?; - ensure!(output.status.success(), "git hash-object failed"); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - } - fn literal_tree(repo: &Path, entries: &[(&str, &str, &str)]) -> Result { - let hex = |s: &str| -> Vec { - (0..20) - .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap()) - .collect() - }; - let mut raw = Vec::new(); - for (mode, name, sha) in entries { - raw.extend_from_slice(format!("{mode} {name}\0").as_bytes()); - raw.extend(hex(sha)); - } - let mut child = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(repo) - .args(["hash-object", "-t", "tree", "--literally", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - child.stdin.take().unwrap().write_all(&raw)?; - let output = child.wait_with_output()?; - ensure!(output.status.success(), "git hash-object --literally failed"); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - } - fn dotgit_lookalike(repo: &Path, recipe: &str, name: &str) -> Result> { - let cfg = hash_object(repo, b"[core]\n\tfsmonitor = touch /tmp/soul3-pwned\n")?; - let inner = literal_tree(repo, &[("100644", "config", &cfg)])?; - let mut entries = vec![ - ("100644".to_owned(), "deployment.toml".to_owned(), recipe.to_owned()), - ("40000".to_owned(), name.to_owned(), inner), - ]; - entries.sort_by(|a, b| a.1.cmp(&b.1)); - Ok(entries) - } - fn chown_to_source_identity(repo: &Path) -> Result<()> { - ensure!( - Command::new("/bin/chown") - .args(["-R", "1000:1000"]) - .arg(repo) - .status()? - .success(), - "chowning the fixture origin repository" - ); - Ok(()) - } - - #[allow(clippy::type_complexity)] - let fixtures: Vec<(&str, Box Result>>)> = vec![ - ( - "dup-trees", - Box::new(|repo: &Path, recipe: &str| { - let x = hash_object(repo, b"x\n")?; - let y = hash_object(repo, b"y\n")?; - let tx = literal_tree(repo, &[("100644", "x", &x)])?; - let ty = literal_tree(repo, &[("100644", "y", &y)])?; - Ok(vec![ - ("40000".into(), "d".into(), tx), - ("40000".into(), "d".into(), ty), - ("100644".into(), "deployment.toml".into(), recipe.into()), - ]) - }) as Box Result>>, - ), - ("dotGIT", Box::new(|repo: &Path, recipe: &str| dotgit_lookalike(repo, recipe, ".GIT"))), - ("dotGit", Box::new(|repo: &Path, recipe: &str| dotgit_lookalike(repo, recipe, ".Git"))), - ("git-short-name", Box::new(|repo: &Path, recipe: &str| dotgit_lookalike(repo, recipe, "git~1"))), - ("dotgit-trailing-dot", Box::new(|repo: &Path, recipe: &str| dotgit_lookalike(repo, recipe, ".git."))), - ("dotgit-zwnj", Box::new(|repo: &Path, recipe: &str| dotgit_lookalike(repo, recipe, ".git\u{200c}"))), - ("dotgit-trailing-space", Box::new(|repo: &Path, recipe: &str| dotgit_lookalike(repo, recipe, ".git "))), - ]; - - for (label, build) in fixtures { - let temp = tempfile::tempdir()?; - fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o755))?; - let origin_repo = temp.path().join("origin"); - fs::create_dir(&origin_repo)?; - git_at(&origin_repo, &["init", "--initial-branch=main"])?; - git_at(&origin_repo, &["config", "user.name", "Idunn Test"])?; - git_at(&origin_repo, &["config", "user.email", "idunn-test@example.invalid"])?; - fs::write(origin_repo.join("deployment.toml"), b"target = 'test'\n")?; - git_at(&origin_repo, &["add", "--all"])?; - git_at(&origin_repo, &["commit", "-m", "root"])?; - let root_commit = git_at(&origin_repo, &["rev-parse", "HEAD"])?; - let recipe_blob = git_at(&origin_repo, &["rev-parse", "HEAD:deployment.toml"])?; - chown_to_source_identity(&origin_repo)?; - - let source_cache_root = temp.path().join("source-cache"); - let frozen_source_root = temp.path().join("frozen-source"); - fs::create_dir(&frozen_source_root)?; - fs::set_permissions(&frozen_source_root, fs::Permissions::from_mode(0o700))?; - let identity = ProcessIdentity { uid: 1000, gid: 1000 }; - let driver = GitSourceDriver::new( - source_cache_root.clone(), - frozen_source_root.clone(), - Some(identity), - ); - let source = ExactSource { - origin: format!("file://{}", origin_repo.display()), - checkout: source_cache_root.join("checkout"), - gitlinks: BTreeMap::new(), - recipe_path: PathBuf::from("deployment.toml"), - }; - // The checkout is established on the good root commit first, - // exactly as a real deploy target's checkout already exists by - // the time a later push lands: this is what makes the objects - // this fixture cares about arrive only through the *next* - // fetch, not the initial clone. - driver.prepare_source_root(&source)?; - - let entries = build(&origin_repo, &recipe_blob) - .with_context(|| format!("building the {label} fixture"))?; - let entry_refs: Vec<(&str, &str, &str)> = entries - .iter() - .map(|(mode, name, sha)| (mode.as_str(), name.as_str(), sha.as_str())) - .collect(); - let hostile_tree = literal_tree(&origin_repo, &entry_refs)?; - // Not `git_at`: the repository is already chowned to the - // unprivileged identity above, and this process is root, so - // plain `git` here refuses it as "dubious ownership" without - // `safe.directory` (the same reason `freeze_exact_refuses_a_ - // duplicate_named_tree...` above uses its own `git_owned`). - let git_owned = |args: &[&str]| -> Result { - let output = Command::new("git") - .args([ - "-c", "safe.directory=*", - "-c", "user.name=Idunn Test", - "-c", "user.email=idunn-test@example.invalid", - "-C", - ]) - .arg(&origin_repo) - .args(args) - .output()?; - ensure!(output.status.success(), "git {:?} failed: {}", args, String::from_utf8_lossy(&output.stderr)); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - }; - let hostile_commit = git_owned(&["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"])?; - git_owned(&["update-ref", "refs/heads/main", &hostile_commit])?; - - // The exact fetch `resolve()` runs (`drivers.rs`, `fn resolve`), - // S1's fix included: `-c transfer.fsckObjects=true`, `--force`, - // `--no-tags`, fetching `refs/heads/main` into a resolution ref. - let resolve_style_fetch = driver.git([ - OsString::from("-c"), - OsString::from("transfer.fsckObjects=true"), - OsString::from("-C"), - source.checkout.as_os_str().to_owned(), - OsString::from("fetch"), - OsString::from("--force"), - OsString::from("--no-tags"), - OsString::from("origin"), - OsString::from("+refs/heads/main:refs/idunn/resolutions/s1-fixture/r1"), - ]); - match resolve_style_fetch { - Err(_) => { - // Refused at the fetch itself: the strongest outcome. - } - Ok(_) => { - // The fetch admitted the ref; `freeze_exact`'s own - // explicit fsck (defense in depth, for objects a fetch - // never re-checks) must still refuse it. - let result = - driver.freeze_exact(&source, &hostile_commit, "txn-hostile", &frozen_source_root); - assert!( - result.is_err(), - "{label}: a hostile tree that a resolve()-style fetch admitted must still be refused by freeze_exact's own fsck" - ); - } - } - } - Ok(()) - } - // ----------------------------------------------------------------- // S4-1 (Self's ruling, fourth Cut 1 fix batch): the symlink resolver // used to recurse per path component and cost 2^n real filesystem @@ -11631,6 +12215,193 @@ mod tests { Ok(()) } + /// R-I1 (fifth batch)/R-I8 (Self's ruling, sixth Cut 1 fix batch): pins + /// the fix for S5-1, deterministically. `open_frozen_symlink_frame`'s + /// `Normal` step used to call `canonicalize()` on every real + /// (non-symlink) path component it walked, even though `frame.resolved` + /// is already canonical there. The budget only charges distinct + /// symlinks read, never components walked, so one link with k real + /// components cost roughly O(k^3): 14.92 s at k=800 under the unfixed + /// code. + /// + /// Soul pass 6 (F1) found the original wall-clock pin was a coin toss in + /// both directions: `small` measured 2.7-9.4 ms across eight runs, + /// always under the `.max(0.01)` 10 ms floor baked into the assertion, + /// so the test was really just `large < 200 ms` -- and clean code + /// measured 198.9 ms under three concurrent Yggdrasil suites (the + /// current normal load), which fails a clean revision. The other + /// direction is worse: with the cubic `canonicalize()` restored, Soul + /// measured a 1.63x separation, not the 33x Hands reported -- Hands' + /// 11 ms/359 ms pair happened to straddle the clamp boundary, which is + /// what made the ratio look decisive. This test replaces the clock with + /// a call count: `record_frozen_symlink_probe_call` fires once per + /// `canonicalize()`/`read_link()` in `open_frozen_symlink_frame`, once + /// per `canonicalize()` of the root in `validate_frozen_source_symlink`, + /// and once per `symlink_metadata()` per real component in the `Normal` + /// arm, so the count is `components + 4` for one symlink with a + /// k-component target, deterministic under any host load. + /// + /// R-I14 (seventh Cut 1 fix batch, F1) is the reason this doc used to + /// say the count "cannot catch its own regression" -- the port existed, + /// but nothing stopped a call site from reaching past it, and Soul pass + /// 8 proved that by restoring the S5-1 bug as `candidate.canonicalize()?` + /// in the `Normal` arm's `Ok(_)` case: every one of the 193 tests, + /// including this one, stayed green, because a per-component + /// `canonicalize()` that returns its own (already-correct) input adds + /// no call the counter sees. **That claim is now false, and the count is + /// no longer the thing that catches it.** R-I23 (Self's ruling, eighth + /// Cut 1 fix batch, F1) made `Path::canonicalize`, `fs::canonicalize`, + /// `fs::symlink_metadata` and `fs::read_link` a `cargo clippy` compile + /// error anywhere in `frozen_symlink_port` outside the port's own + /// `StdFrozenSymlinkFs` implementation (see `clippy.toml`); Soul's exact + /// mutation is that spelling, so it now fails to build under `cargo + /// clippy -- -D clippy::disallowed_methods`, independent of what this + /// count test asserts. What the count formula still cannot do, and + /// still does not claim to: distinguish a mutation that keeps every + /// call site and every call count the same while doing needless extra + /// work *inside* one of those calls -- that class was never this test's + /// job, and nothing here pretends otherwise. + #[cfg(unix)] + #[test] + fn validate_frozen_source_symlinks_resolves_a_long_component_chain_with_a_linear_call_count( + ) -> Result<()> { + use std::os::unix::fs::symlink; + use std::time::Instant; + + fn build_chain(root: &Path, components: usize) -> Result<()> { + fs::create_dir(root)?; + let mut dir = root.to_path_buf(); + for i in 0..components { + dir.push(format!("d{i}")); + fs::create_dir(&dir)?; + } + fs::write(dir.join("target.txt"), b"leaf\n")?; + let target: PathBuf = (0..components) + .map(|i| format!("d{i}")) + .chain(std::iter::once("target.txt".to_string())) + .collect(); + symlink(target, root.join("link"))?; + Ok(()) + } + + fn resolve_and_count(components: usize) -> Result<(u64, std::time::Duration)> { + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + build_chain(&root, components)?; + reset_frozen_symlink_probe_calls(); + let start = Instant::now(); + validate_frozen_source_symlinks(&root)?; + let elapsed = start.elapsed(); + Ok((frozen_symlink_probe_calls(), elapsed)) + } + + let (small_calls, small_elapsed) = resolve_and_count(50)?; + let (large_calls, large_elapsed) = resolve_and_count(200)?; + + // R-I23 (Self's ruling, eighth Cut 1 fix batch, F1): one root + // canonicalize() in validate_frozen_source_symlink, one symlink open + // (one canonicalize() plus one read_link() in + // open_frozen_symlink_frame), and one symlink_metadata() per real + // component: components + 4, exactly -- not a bound, an exact + // formula, so a cubic term reappearing as *extra* probe calls + // (rather than hidden extra work per call) would still be caught. + assert_eq!( + small_calls, + 54, + "50 real components must cost exactly 50 + 4 = 54 canonicalize()/read_link()/\ + symlink_metadata() calls: {small_calls} (took {small_elapsed:?})" + ); + assert_eq!( + large_calls, + 204, + "200 real components must cost exactly 200 + 4 = 204 canonicalize()/read_link()/\ + symlink_metadata() calls: {large_calls} (took {large_elapsed:?})" + ); + // A 4x longer chain costing ~4x the calls (not ~64x, the cubic + // term's signature) is the deterministic replacement for the old + // wall-clock ratio. + assert!( + large_calls < small_calls * 5, + "resolving a 4x longer component chain must cost far less than the ~64x a \ + cubic per-component canonicalize() would produce: {small_calls} calls at 50 \ + components, {large_calls} calls at 200 components" + ); + Ok(()) + } + + /// R-I2 (Self's ruling, fifth Cut 1 fix batch): pins S5-2. The + /// traversal budget is the resolver's only termination guarantee -- + /// memoisation cannot help a cycle, because an in-progress frame is + /// never inserted into the memo until it finishes, and it never + /// finishes. `cargo-mutants --in-diff` found two mutants that + /// `open_frozen_symlink_frame`'s budget check missed, both making the + /// budget never decrease (`*budget -= 1` mutated away). This fixture, a + /// bound two-link cycle, must refuse under real code and must go red + /// under that mutation (checked by hand below). + /// + /// R-I10 (Self's ruling, sixth Cut 1 fix batch): S6 pass 6 (F3) judged + /// the hang under a broken budget the correct falsification and left it + /// alone -- a wall-clock cap on the verify runner belongs to Cut 4, not + /// here -- but asked this fixture to name *which* error it gets under + /// real code, not merely that it errs. The budget-exhaustion `ensure!` + /// in `open_frozen_symlink_frame` is the only failure this fixture can + /// hit (no absolute target, no missing parent, no `NotFound`), so its + /// message is asserted directly: it must name the 40-traversal ceiling, + /// not some other refusal that happened to also return `Err`. + #[cfg(unix)] + #[test] + fn resolve_frozen_source_symlink_refuses_a_two_link_cycle() -> Result<()> { + use std::os::unix::fs::symlink; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + symlink("b", root.join("a"))?; + symlink("a", root.join("b"))?; + + let result = validate_frozen_source_symlink(&root, &root.join("a")); + let error = result.as_ref().expect_err( + "a two-link cycle can never terminate on its own; only the traversal \ + budget stops it, and it must refuse", + ); + let message = error.to_string(); + assert!( + message.contains("40-traversal ceiling"), + "a two-link cycle must be refused specifically by the traversal budget's own \ + ceiling message, not by some other error: {message:?}" + ); + Ok(()) + } + + /// R-I2 (Self's ruling, fifth Cut 1 fix batch): the second budget + /// fixture -- 41 *distinct* links (not a cycle, so memoisation cannot + /// hide the count) chained end to end, one more than + /// `MAX_LINK_TRAVERSALS`. Each is read exactly once, so this exercises + /// the budget itself rather than the cycle-detection consequence of it. + #[cfg(unix)] + #[test] + fn resolve_frozen_source_symlink_refuses_41_distinct_links() -> Result<()> { + use std::os::unix::fs::symlink; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::write(root.join("leaf.txt"), b"leaf\n")?; + // link40 -> leaf.txt, link39 -> link40, ..., link0 -> link1: 41 + // distinct symlinks (link0..link40) must be read to reach the leaf. + symlink("leaf.txt", root.join("link40"))?; + for i in (0..40usize).rev() { + symlink(format!("link{}", i + 1), root.join(format!("link{i}")))?; + } + + let result = validate_frozen_source_symlink(&root, &root.join("link0")); + assert!( + result.is_err(), + "41 distinct links exceeds the 40-traversal budget and must refuse: {result:?}" + ); + Ok(()) + } + /// S4-5 (Self's ruling, fourth Cut 1 fix batch): every existing S6 test /// naming `..` is a refusal (an escape). `cargo-mutants` found that /// deleting the `Component::ParentDir` match arm entirely -- so it falls @@ -11652,7 +12423,7 @@ mod tests { validate_frozen_source_symlink(&root, &link)?; let canonical_root = root.canonicalize()?; - let resolved = resolve_frozen_source_symlink(&canonical_root, &link)?; + let resolved = resolve_frozen_source_symlink(&StdFrozenSymlinkFs, &canonical_root, &link)?; assert_eq!( resolved, canonical_root.join("deployment.toml"), @@ -11661,6 +12432,89 @@ mod tests { Ok(()) } + /// R-I7 (Self's ruling, fifth Cut 1 fix batch): a symlink whose entire + /// target is `..`, sitting directly at the root, leaves nothing behind + /// to re-enter the root -- no later `Normal` step ever runs to trip + /// that arm's own containment check. This pins that the resolver still + /// refuses it: while auditing R-I7's three "redundant" containment + /// checks for deletion, tracing this exact shape by hand showed the + /// third one (the unconditional check at the bottom of the resolver's + /// loop, after `:6058-6063` in Soul's pass-5 numbering) is the only + /// thing that catches a chain that leaves the root and never returns + /// through another path component -- so unlike the other two, it survives + /// this batch. Recorded here as the discrepancy from the map's "delete + /// all three" reading. + #[cfg(unix)] + #[test] + fn validate_frozen_source_symlink_refuses_a_bare_parent_reference_at_the_root() -> Result<()> { + use std::os::unix::fs::symlink; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::write(root.join("deployment.toml"), b"target = 'test'\n")?; + let link = root.join("escape"); + symlink("..", &link)?; + + let result = validate_frozen_source_symlink(&root, &link); + assert!( + result.is_err(), + "a symlink whose entire target is `..`, with no component after it to trip the \ + `Normal` arm's own check, must still be refused: {result:?}" + ); + Ok(()) + } + + /// R-I25 (Self's finding, applied to the frozen-source side): a receipt + /// frozen under either shape `hash_frozen_source_tree` carried before + /// this cut's sixth and seventh batches must still verify against the + /// current code. Built by calling the legacy hashers directly rather + /// than a real historical commit, since the shapes are pinned here + /// (and, independently, by the `git show` transcript in this batch's + /// commit message). + #[cfg(unix)] + #[test] + fn verify_frozen_source_digest_accepts_both_shapes_that_predate_this_cuts_sixth_batch() + -> Result<()> { + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::write(root.join("f"), b"same content\n")?; + + let current = frozen_source_sha256(&root)?; + let mode_and_gid_only = frozen_source_sha256_legacy( + &root, + FrozenSourceDigestShape::ModeAndGidOnly, + )?; + let executable_bit_only = frozen_source_sha256_legacy( + &root, + FrozenSourceDigestShape::ExecutableBitOnly, + )?; + // Fixture sanity: the three shapes actually disagree here (the gid + // term differs by machine, but the framing -- dir/file tags, + // presence of a gid term at all -- is enough to separate them from + // plain file content). + assert_ne!(current, mode_and_gid_only); + assert_ne!(current, executable_bit_only); + assert_ne!(mode_and_gid_only, executable_bit_only); + + assert!(verify_frozen_source_digest(&root, ¤t)?); + assert!(verify_frozen_source_digest(&root, &mode_and_gid_only)?); + assert!(verify_frozen_source_digest(&root, &executable_bit_only)?); + + // Without the fallback, only the current shape would be accepted -- + // this is what R-I25 replaced. + assert_ne!(current, mode_and_gid_only); + assert_ne!(current, executable_bit_only); + + fs::write(root.join("f"), b"tampered\n")?; + assert!( + !verify_frozen_source_digest(&root, &executable_bit_only)?, + "a content change must still be caught under every shape" + ); + Ok(()) + } + // ----------------------------------------------------------------- // S4-2 (Self's ruling, fourth Cut 1 fix batch): the S9 streamed hash had // no behavioural pin. Deleting the read buffer's size (hashing zero @@ -11699,6 +12553,1343 @@ mod tests { Ok(()) } + /// R-I3 (Self's ruling, fifth Cut 1 fix batch): pins S5-3. The digest + /// is the tamper check `observe_frozen` relies on, so it must not be + /// blind to a repointed symlink target: two trees whose only + /// difference is where the same-named symlink points must hash + /// differently. Before this test existed, deleting the + /// `hasher.update(target...)` call at `hash_frozen_source_tree`'s + /// symlink arm left the suite green. + #[cfg(unix)] + #[test] + fn frozen_source_sha256_differs_when_only_a_symlink_target_differs() -> Result<()> { + use std::os::unix::fs::symlink; + + fn build(temp: &Path, name: &str, target: &str) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + fs::write(root.join("a"), b"a\n")?; + fs::write(root.join("b"), b"b\n")?; + symlink(target, root.join("link"))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let root_a = build(temp.path(), "root-a", "a")?; + let root_b = build(temp.path(), "root-b", "b")?; + let digest_a = frozen_source_sha256(&root_a)?; + let digest_b = frozen_source_sha256(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two trees differing only in a symlink's target must hash differently, \ + since the digest is the only tamper check a repointed in-root symlink faces" + ); + Ok(()) + } + + /// R-I3 (Self's ruling, fifth Cut 1 fix batch): pins S5-4. Deleting the + /// `metadata.permissions().mode() & 0o111 != 0` term left the suite + /// green, and `validate_frozen_source` legally accepts both `0444` and + /// `0555`, so the digest must be the thing that notices a file gaining + /// or losing its executable bit. + #[cfg(unix)] + #[test] + fn frozen_source_sha256_differs_when_only_the_executable_bit_differs() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + fn build(temp: &Path, name: &str, mode: u32) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let file = root.join("f"); + fs::write(&file, b"same content\n")?; + fs::set_permissions(&file, fs::Permissions::from_mode(mode))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let root_a = build(temp.path(), "root-a", 0o444)?; + let root_b = build(temp.path(), "root-b", 0o555)?; + let digest_a = frozen_source_sha256(&root_a)?; + let digest_b = frozen_source_sha256(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two trees differing only in the executable bit must hash differently: \ + validate_frozen_source legally accepts both 0444 and 0555, so the digest \ + is the only thing that can still notice the change" + ); + Ok(()) + } + + /// R-I9 (Self's ruling, sixth Cut 1 fix batch, F2): pins the validator + /// half of the setuid/setgid/sticky privilege finding directly, not just + /// through the digest. Before this batch, `validate_frozen_source_tree` + /// masked with `& 0o777`, so `0o4555` satisfied `== 0o555` and a + /// root-owned setuid file passed validation outright. Widening the mask + /// to `& 0o7777` (S6/F2) must make this fixture -- a file harden would + /// never itself produce, since `harden_frozen_source_tree`'s + /// `set_permissions` always writes an exact `0o444`/`0o555` -- something + /// `validate_frozen_source` refuses on sight, independent of the digest. + #[cfg(unix)] + #[test] + fn validate_frozen_source_refuses_a_setuid_file() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::write(root.join("f"), b"same content\n")?; + // Root already owns both paths (this test's own process is the + // required root euid) -- an explicit `chown`, even to the same + // owner, is not a no-op here: the verify container's `chown` + // silently clears a freshly set setuid bit (confirmed by hand: `chmod + // 4555 f` then `chown 0:0 f` observably drops the file back to + // `0555`), so the setuid bit is set last and nothing touches + // ownership afterward. + fs::set_permissions(root.join("f"), fs::Permissions::from_mode(0o4555))?; + // Root's own mode is set to the one legal value so the fixture's + // only violation is the file's setuid bit -- otherwise this would + // also (correctly, but incidentally) fail the directory-mode check, + // and the assertion below would not isolate what it claims to. + fs::set_permissions(&root, fs::Permissions::from_mode(0o555))?; + let result = validate_frozen_source(&root); + let error = result.as_ref().expect_err( + "a root-owned setuid file must be refused outright, not masked to 0o555", + ); + assert!( + error.to_string().contains("noncanonical mode"), + "must be refused specifically for its mode, not some other reason: {error:?}" + ); + Ok(()) + } + + /// R-I17 (Self's ruling, seventh Cut 1 fix batch, F5): the setgid case + /// of the same file-mask finding. Soul pass 7 measured that narrowing + /// the file mask to `0o5777` (blind to setgid) still leaves all 183 + /// tests passing -- only setuid-on-a-file was pinned by R-I9. + #[cfg(unix)] + #[test] + fn validate_frozen_source_refuses_a_setgid_file() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::write(root.join("f"), b"same content\n")?; + fs::set_permissions(root.join("f"), fs::Permissions::from_mode(0o2555))?; + fs::set_permissions(&root, fs::Permissions::from_mode(0o555))?; + let result = validate_frozen_source(&root); + let error = result.as_ref().expect_err( + "a root-owned setgid file must be refused outright, not masked to 0o555", + ); + assert!( + error.to_string().contains("noncanonical mode"), + "must be refused specifically for its mode, not some other reason: {error:?}" + ); + Ok(()) + } + + /// R-I17 (Self's ruling, seventh Cut 1 fix batch, F5): the sticky case + /// of the same file-mask finding. + #[cfg(unix)] + #[test] + fn validate_frozen_source_refuses_a_sticky_file() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::write(root.join("f"), b"same content\n")?; + fs::set_permissions(root.join("f"), fs::Permissions::from_mode(0o1555))?; + fs::set_permissions(&root, fs::Permissions::from_mode(0o555))?; + let result = validate_frozen_source(&root); + let error = result.as_ref().expect_err( + "a root-owned sticky file must be refused outright, not masked to 0o555", + ); + assert!( + error.to_string().contains("noncanonical mode"), + "must be refused specifically for its mode, not some other reason: {error:?}" + ); + Ok(()) + } + + /// R-I17 (Self's ruling, seventh Cut 1 fix batch, F5): Soul pass 7 + /// measured that narrowing the directory mask to `0o3777` (blind to + /// setuid on a directory) still leaves all 183 tests passing -- no + /// directory-mode test existed at all before this batch. Setuid on a + /// directory carries no kernel semantics of its own, but the widened + /// mask refuses it anyway: a frozen source directory has no legitimate + /// reason to carry it, same as the file case. + #[cfg(unix)] + #[test] + fn validate_frozen_source_refuses_a_setuid_directory() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::set_permissions(&root, fs::Permissions::from_mode(0o4555))?; + let result = validate_frozen_source(&root); + let error = result.as_ref().expect_err( + "a root-owned setuid directory must be refused outright, not masked to 0o555", + ); + assert!( + error.to_string().contains("not 0555"), + "must be refused specifically for its mode, not some other reason: {error:?}" + ); + Ok(()) + } + + /// R-I17 (Self's ruling, seventh Cut 1 fix batch, F5): the setgid case + /// on a directory -- this is, per Soul pass 7, "the one with real + /// semantics" (new entries inherit the directory's owning group), so a + /// mask that admitted it would be the most consequential blind spot in + /// this batch. + #[cfg(unix)] + #[test] + fn validate_frozen_source_refuses_a_setgid_directory() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::set_permissions(&root, fs::Permissions::from_mode(0o2555))?; + let result = validate_frozen_source(&root); + let error = result.as_ref().expect_err( + "a root-owned setgid directory must be refused outright, not masked to 0o555", + ); + assert!( + error.to_string().contains("not 0555"), + "must be refused specifically for its mode, not some other reason: {error:?}" + ); + Ok(()) + } + + /// R-I17 (Self's ruling, seventh Cut 1 fix batch, F5): the sticky case + /// on a directory. + #[cfg(unix)] + #[test] + fn validate_frozen_source_refuses_a_sticky_directory() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + let temp = tempfile::tempdir()?; + let root = temp.path().join("root"); + fs::create_dir(&root)?; + fs::set_permissions(&root, fs::Permissions::from_mode(0o1555))?; + let result = validate_frozen_source(&root); + let error = result.as_ref().expect_err( + "a root-owned sticky directory must be refused outright, not masked to 0o555", + ); + assert!( + error.to_string().contains("not 0555"), + "must be refused specifically for its mode, not some other reason: {error:?}" + ); + Ok(()) + } + + /// R-I9 (Self's ruling, sixth Cut 1 fix batch, F2): pins the digest half + /// of the setuid/setgid/sticky privilege finding. Soul measured a + /// root-owned setuid file (`04555`) accepted by the (pre-fix) validator + /// with the digest unchanged, because the digest hashed only + /// `mode & 0o111 != 0`, a boolean the setuid bit cannot move. The digest + /// now hashes the full mode (`mode & 0o7777`), a strict superset that a + /// setuid bit does move even though the widened R-I9 validator never + /// legally admits it -- the digest is the tamper check for a receipt + /// already on disk, so it must not rely on `validate_frozen_source` + /// having run first. + #[cfg(unix)] + #[test] + fn frozen_source_sha256_differs_when_only_a_files_setuid_bit_differs() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + fn build(temp: &Path, name: &str, mode: u32) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let file = root.join("f"); + fs::write(&file, b"same content\n")?; + fs::set_permissions(&file, fs::Permissions::from_mode(mode))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + // Both admit `mode & 0o111 != 0 == true` -- only the full mode + // distinguishes them. + let root_a = build(temp.path(), "root-a", 0o555)?; + let root_b = build(temp.path(), "root-b", 0o4555)?; + let digest_a = frozen_source_sha256(&root_a)?; + let digest_b = frozen_source_sha256(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two trees differing only in a file's setuid bit must hash differently, since a \ + tampered setuid-root binary is exactly the shape this digest must catch" + ); + Ok(()) + } + + /// R-I9 (Self's ruling, sixth Cut 1 fix batch, F2): the digest hashed no + /// mode term for directories at all before this batch, so a directory + /// gaining setgid or sticky -- both privilege-relevant on a directory, + /// unlike a plain permission bit -- re-observed clean. + #[cfg(unix)] + #[test] + fn frozen_source_sha256_differs_when_only_a_directorys_mode_differs() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + // `hash_frozen_source_tree(root, root, ...)` only ever hashes an + // entry it reads out of some directory's `read_dir` -- the digest + // root itself is walked but never hashed as an entry of its own + // parent, so the differing directory must be a *child* of the + // digest root, not the root path passed to `frozen_source_sha256`. + fn build(temp: &Path, name: &str, mode: u32) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let child = root.join("d"); + fs::create_dir(&child)?; + fs::write(child.join("f"), b"same content\n")?; + fs::set_permissions(child.join("f"), fs::Permissions::from_mode(0o444))?; + fs::set_permissions(&child, fs::Permissions::from_mode(mode))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let root_a = build(temp.path(), "root-a", 0o555)?; + let root_b = build(temp.path(), "root-b", 0o2555)?; + let digest_a = frozen_source_sha256(&root_a)?; + let digest_b = frozen_source_sha256(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two trees differing only in a directory's setgid bit must hash differently" + ); + Ok(()) + } + + /// R-I9 (Self's ruling, sixth Cut 1 fix batch, F2): the digest checked + /// `uid() == 0` (via the separate validator) but never hashed gid at + /// all, on files or directories, so a tampered owning group re-observed + /// clean. This pins the file case. + #[cfg(unix)] + #[test] + fn frozen_source_sha256_differs_when_only_a_files_gid_differs() -> Result<()> { + use std::os::unix::fs::chown; + + fn build(temp: &Path, name: &str, gid: u32) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let file = root.join("f"); + fs::write(&file, b"same content\n")?; + chown(&file, None, Some(gid))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let root_a = build(temp.path(), "root-a", 0)?; + let root_b = build(temp.path(), "root-b", second_gid_for_chown_fixture()?)?; + let digest_a = frozen_source_sha256(&root_a)?; + let digest_b = frozen_source_sha256(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two trees differing only in a file's owning gid must hash differently" + ); + Ok(()) + } + + /// R-I9 (Self's ruling, sixth Cut 1 fix batch, F2): the directory case + /// of the same gid-coverage gap. + #[cfg(unix)] + #[test] + fn frozen_source_sha256_differs_when_only_a_directorys_gid_differs() -> Result<()> { + use std::os::unix::fs::chown; + use std::os::unix::fs::PermissionsExt; + + // Same reason as the mode test above: the digest root's own + // metadata is never hashed, only entries read out of a directory, + // so the differing directory must be a child of the digest root. + fn build(temp: &Path, name: &str, gid: u32) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let child = root.join("d"); + fs::create_dir(&child)?; + fs::write(child.join("f"), b"same content\n")?; + fs::set_permissions(child.join("f"), fs::Permissions::from_mode(0o444))?; + chown(&child, None, Some(gid))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let root_a = build(temp.path(), "root-a", 0)?; + let root_b = build(temp.path(), "root-b", second_gid_for_chown_fixture()?)?; + let digest_a = frozen_source_sha256(&root_a)?; + let digest_b = frozen_source_sha256(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two trees differing only in a directory's owning gid must hash differently" + ); + Ok(()) + } + + /// Sets an extended attribute through the `setxattr(2)` syscall + /// directly, matching Soul's rig rather than a crate dependency this + /// project does not otherwise carry. + #[cfg(unix)] + fn setxattr_or_bail(path: &Path, name: &str, value: &[u8]) -> Result<()> { + use std::os::unix::ffi::OsStrExt; + + let cpath = std::ffi::CString::new(path.as_os_str().as_bytes())?; + let cname = std::ffi::CString::new(name)?; + let rc = unsafe { + libc::setxattr( + cpath.as_ptr(), + cname.as_ptr(), + value.as_ptr() as *const libc::c_void, + value.len(), + 0, + ) + }; + ensure!( + rc == 0, + "setxattr({name}) on {}: {}", + path.display(), + std::io::Error::last_os_error() + ); + Ok(()) + } + + /// R-I18 (Self's ruling, seventh Cut 1 fix batch, F3): the general + /// xattr-coverage pin, independent of any one attribute's namespace or + /// semantics -- before this batch, `frozen_source_sha256` had no xattr + /// term at all, so any tampered extended attribute re-observed clean. + /// Uses the unprivileged `user.` namespace so the pin does not + /// coincidentally depend on the same root/capability precondition the + /// finding itself is about. + #[cfg(unix)] + #[test] + fn frozen_source_sha256_differs_when_only_an_extended_attribute_differs() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + fn build(temp: &Path, name: &str, xattr_value: Option<&[u8]>) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let file = root.join("f"); + fs::write(&file, b"same content\n")?; + fs::set_permissions(&file, fs::Permissions::from_mode(0o444))?; + if let Some(value) = xattr_value { + setxattr_or_bail(&file, "user.idunn.soul_probe", value)?; + } + Ok(root) + } + + let temp = tempfile::tempdir()?; + let root_a = build(temp.path(), "root-a", None)?; + let root_b = build(temp.path(), "root-b", Some(b"tampered"))?; + let digest_a = frozen_source_sha256(&root_a)?; + let digest_b = frozen_source_sha256(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two trees differing only in one file's extended attribute must hash differently" + ); + Ok(()) + } + + /// R-I18 (Self's ruling, seventh Cut 1 fix batch, F3): the exact finding + /// Soul pass 7 measured -- `security.capability` set to `cap_setuid=ep` + /// on a real root-owned 0555 tree, the setuid-equivalent grant that + /// carries no setuid *bit*, so no mode-and-gid term from R-I9/R-I13/R-I16 + /// can see it. Both `validate_frozen_source` and `frozen_source_sha256` + /// are checked directly against the same tamper Soul used. Requires + /// root (this test's own process), the same precondition the finding + /// itself was measured under; it does not show an unprivileged process + /// can reach this path. + #[cfg(unix)] + #[test] + fn frozen_source_privilege_surface_sees_a_file_capability() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + fn build(temp: &Path, name: &str) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let file = root.join("f"); + fs::write(&file, b"same content\n")?; + fs::set_permissions(&file, fs::Permissions::from_mode(0o555))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let clean = build(temp.path(), "clean")?; + fs::set_permissions(&clean, fs::Permissions::from_mode(0o555))?; + let clean_digest = frozen_source_sha256(&clean)?; + + let capped = build(temp.path(), "capped")?; + // `cap_setuid=ep`: the same 20-byte VFS capability value Soul's rig + // used, version 2, permitted+effective, `CAP_SETUID` (bit 7) set. + let value: [u8; 20] = [ + 0x01, 0x00, 0x00, 0x02, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + setxattr_or_bail(&capped.join("f"), "security.capability", &value)?; + fs::set_permissions(&capped, fs::Permissions::from_mode(0o555))?; + let capped_digest = frozen_source_sha256(&capped)?; + + assert_ne!( + clean_digest, capped_digest, + "a root-owned 0555 tree gaining a security.capability xattr must move the digest: \ + this is the setuid-equivalent grant that carries no setuid bit for the mode term \ + to see" + ); + + let validated = validate_frozen_source(&capped); + assert!( + validated.is_ok(), + "the validator does not (and is not asked to) refuse a file capability by itself -- \ + only the digest is this batch's detection surface: {validated:?}" + ); + Ok(()) + } + + /// Picks a second gid to `chown` a fixture to, without hard-coding one + /// that may not exist in every environment this suite runs in. The + /// verify container runs as root (`0:0`), which is exactly the + /// precondition these tests need to `chown` at all -- Soul pass 6 (F2) + /// noted the container's root identity gave its own setuid probe this + /// same precondition "for free" and did not prove a non-root attacker + /// can reach the finding; these digest-coverage tests share that same + /// limit and do not claim otherwise. `1` (commonly `daemon` or `bin`) is + /// used because it exists in essentially every Linux `/etc/group`, + /// including minimal containers; if `chown` to it fails, the test + /// environment cannot exercise this path at all and the fixture itself + /// (not the code under test) is what would be wrong, so this bails + /// loudly rather than silently skip. + /// + /// R-I19 (Self's ruling, seventh Cut 1 fix batch, F6): this used to be + /// named `nix_group_or_skip`, which promised a skip it never performed + /// -- it returned `Ok(1)` unconditionally, and the `/etc/group` + /// reasoning in this comment was already moot, since `chown(2)` never + /// consults `/etc/group` at all (it takes a raw gid, not a name). Soul + /// pass 7 flagged this as informational: not a new class of bug, but a + /// name that told the next reader something the function does not do. + /// Renamed instead of made to skip, since the "bail loudly, don't + /// silently skip" behaviour documented above is the intended one. + #[cfg(unix)] + fn second_gid_for_chown_fixture() -> Result { + Ok(1) + } + + /// R-I25 (Self's ruling: the digest format change is a migration + /// hazard). The whole finding is that nothing exercised the + /// stored-value path -- every prior test recomputes both sides with the + /// same code, which proves nothing about an already-sealed receipt. + /// This constructs a stored value the way the *old* `digest_artifact` + /// (pre-R-I20: `raw_sha256(&bytes)` alone) actually produced it, then + /// verifies it under the current code. + #[test] + fn verify_artifact_digest_accepts_a_receipt_sealed_under_the_pre_r_i20_shape() -> Result<()> { + let temp = tempfile::tempdir()?; + let file = temp.path().join("executable"); + fs::write(&file, b"same content\n")?; + let legacy_stored = format!("sha256-{}", raw_sha256(&fs::read(&file)?)); + + // The current algorithm alone does not accept it: the whole point + // is that `digest_artifact`'s bare-file shape changed under R-I20. + let (current_hex, _) = digest_artifact(&file)?; + assert_ne!( + format!("sha256-{current_hex}"), + legacy_stored, + "fixture is invalid: the legacy and current shapes must actually differ \ + for this test to prove anything" + ); + + assert!( + verify_artifact_digest(&file, &legacy_stored)?, + "a receipt sealed under the pre-R-I20 shape must still verify" + ); + + // Removing the fallback -- i.e. asking only "does the current + // algorithm's output match?", which is what R-I25 replaced -- + // fails against the same stored value and the same file: + assert_ne!( + format!("sha256-{current_hex}"), + legacy_stored, + "without the version fallback this stored value is refused" + ); + Ok(()) + } + + /// A tamper is still caught when the receipt is in the legacy shape: + /// content-only was always what the old shape covered, and it still + /// does. + #[test] + fn verify_artifact_digest_still_refuses_a_content_tamper_under_the_legacy_shape() -> Result<()> + { + let temp = tempfile::tempdir()?; + let file = temp.path().join("executable"); + fs::write(&file, b"same content\n")?; + let legacy_stored = format!("sha256-{}", raw_sha256(&fs::read(&file)?)); + assert!(verify_artifact_digest(&file, &legacy_stored)?); + + fs::write(&file, b"tampered content\n")?; + assert!( + !verify_artifact_digest(&file, &legacy_stored)?, + "a content change must still be caught under the legacy shape" + ); + Ok(()) + } + + /// Never reseals: verifying a legacy-shaped receipt does not mutate it, + /// there is nothing to write back, and the same legacy string keeps + /// verifying on a second call. + #[test] + fn verify_artifact_digest_does_not_reseal_a_legacy_receipt() -> Result<()> { + let temp = tempfile::tempdir()?; + let file = temp.path().join("executable"); + fs::write(&file, b"same content\n")?; + let legacy_stored = format!("sha256-{}", raw_sha256(&fs::read(&file)?)); + assert!(verify_artifact_digest(&file, &legacy_stored)?); + assert!(verify_artifact_digest(&file, &legacy_stored)?); + Ok(()) + } + + /// R-I13 (Self's ruling, sixth Cut 1 fix batch, F6): `digest_tree` is + /// the artifact digest, separate from `frozen_source_sha256`, and had + /// exactly the blindness R-I3 fixed there (S5-4): no executable-bit + /// term. No ruling in the fifth batch covered it, so Soul flagged it + /// explicitly as not fixed by association. + /// + /// R-I20 (Self's ruling, eighth Cut 1 fix batch, F2): this used to wrap + /// the file in a directory so `digest_tree` ran, which is exactly why + /// the single-file branch's own blindness (no mode/gid/xattr term at + /// all) survived three fix batches untested -- `host.rs` hands + /// `digest_artifact` a bare executable file, never a directory + /// containing one. Calls `digest_artifact` on the file itself. + #[cfg(unix)] + #[test] + fn digest_artifact_differs_when_only_the_executable_bit_differs() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + fn build(temp: &Path, name: &str, mode: u32) -> Result { + let file = temp.join(name); + fs::write(&file, b"same content\n")?; + fs::set_permissions(&file, fs::Permissions::from_mode(mode))?; + Ok(file) + } + + let temp = tempfile::tempdir()?; + let file_a = build(temp.path(), "a", 0o644)?; + let file_b = build(temp.path(), "b", 0o755)?; + let (digest_a, _) = digest_artifact(&file_a)?; + let (digest_b, _) = digest_artifact(&file_b)?; + assert_ne!( + digest_a, digest_b, + "two bare artifact files differing only in the executable bit must hash differently" + ); + Ok(()) + } + + /// R-I16 (Self's ruling, seventh Cut 1 fix batch, F4): pins the widened + /// artifact-digest file term beyond the executable-bit boolean above. + /// `0o555` and `0o4555` both report the executable bit set, so this is + /// the term the boolean test just above cannot distinguish -- exactly + /// Soul pass 7's "post-install tamper adding setuid to an installed + /// binary re-verifies clean" scenario. + /// + /// R-I20: bare file, not wrapped in a directory -- see the comment on + /// the executable-bit test above. + #[cfg(unix)] + #[test] + fn digest_artifact_differs_when_only_a_files_setuid_bit_differs() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + fn build(temp: &Path, name: &str, mode: u32) -> Result { + let file = temp.join(name); + fs::write(&file, b"same content\n")?; + fs::set_permissions(&file, fs::Permissions::from_mode(mode))?; + Ok(file) + } + + let temp = tempfile::tempdir()?; + let file_a = build(temp.path(), "a", 0o555)?; + let file_b = build(temp.path(), "b", 0o4555)?; + let (digest_a, _) = digest_artifact(&file_a)?; + let (digest_b, _) = digest_artifact(&file_b)?; + assert_ne!( + digest_a, digest_b, + "two bare artifact files differing only in the setuid bit must hash differently" + ); + Ok(()) + } + + /// R-I16 (Self's ruling, seventh Cut 1 fix batch, F4): the artifact + /// digest hashed no gid term at all before this batch, on files or + /// directories. This pins the file case. + /// + /// R-I20: bare file, not wrapped in a directory -- see the comment on + /// the executable-bit test above. + #[cfg(unix)] + #[test] + fn digest_artifact_differs_when_only_a_files_gid_differs() -> Result<()> { + use std::os::unix::fs::chown; + + fn build(temp: &Path, name: &str, gid: u32) -> Result { + let file = temp.join(name); + fs::write(&file, b"same content\n")?; + chown(&file, None, Some(gid))?; + Ok(file) + } + + let temp = tempfile::tempdir()?; + let file_a = build(temp.path(), "a", 0)?; + let file_b = build(temp.path(), "b", second_gid_for_chown_fixture()?)?; + let (digest_a, _) = digest_artifact(&file_a)?; + let (digest_b, _) = digest_artifact(&file_b)?; + assert_ne!( + digest_a, digest_b, + "two bare artifact files differing only in owning gid must hash differently" + ); + Ok(()) + } + + /// R-I21 (Self's ruling, eighth Cut 1 fix batch, F3): the artifact + /// digest's file-arm xattr term, on the bare-file path `host.rs` takes. + /// Companion to `frozen_source_sha256_differs_when_only_an_extended_attribute_differs`. + #[cfg(unix)] + #[test] + fn digest_artifact_differs_when_only_a_files_extended_attribute_differs() -> Result<()> { + let temp = tempfile::tempdir()?; + let clean = temp.path().join("clean"); + fs::write(&clean, b"same content\n")?; + let clean_digest = digest_artifact(&clean)?.0; + + let capped = temp.path().join("capped"); + fs::write(&capped, b"same content\n")?; + let value: [u8; 20] = [ + 0x01, 0x00, 0x00, 0x02, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + setxattr_or_bail(&capped, "security.capability", &value)?; + let capped_digest = digest_artifact(&capped)?.0; + + assert_ne!( + clean_digest, capped_digest, + "a bare artifact file gaining a security.capability xattr must move the digest" + ); + Ok(()) + } + + /// R-I21: the artifact digest's directory-arm xattr term. + /// + /// `digest_tree(root, root, ...)` only ever hashes an entry it reads out + /// of some directory's `read_dir`, never the digest root itself (see the + /// comment on the directory-mode test above), so the differing + /// directory must be a child of the artifact root -- measured directly: + /// setting the xattr on `capped` itself here first left the two digests + /// equal. + #[cfg(unix)] + #[test] + fn digest_artifact_differs_when_only_a_directorys_extended_attribute_differs() -> Result<()> { + fn build(temp: &Path, name: &str) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let child = root.join("d"); + fs::create_dir(&child)?; + fs::write(child.join("f"), b"same content\n")?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let clean = build(temp.path(), "clean")?; + let clean_digest = digest_artifact(&clean)?.0; + + let capped = build(temp.path(), "capped")?; + setxattr_or_bail(&capped.join("d"), "user.idunn.soul_probe", b"present")?; + let capped_digest = digest_artifact(&capped)?.0; + + assert_ne!( + clean_digest, capped_digest, + "an artifact directory gaining an extended attribute must move the digest" + ); + Ok(()) + } + + /// R-I21: the artifact digest's symlink-arm xattr term. `lsetxattr` is + /// the underlying syscall `read_sorted_xattrs` uses (via `llistxattr`/ + /// `lgetxattr`, which do not follow a final symlink), matching how a + /// symlink's own xattrs, not its target's, are read. + /// + /// Uses the `security` namespace, not `user`: measured directly, + /// `lsetxattr(..., "user.idunn.soul_probe", ...)` on a symlink fails + /// `EPERM` even as root -- the kernel restricts the `user` namespace to + /// regular files and directories, symlinks included in the refusal + /// regardless of privilege. `security.capability` is the same + /// setuid-equivalent value the frozen-source xattr tests already use. + #[cfg(unix)] + #[test] + fn digest_artifact_differs_when_only_a_symlinks_extended_attribute_differs() -> Result<()> { + use std::os::unix::ffi::OsStrExt; + + fn build(temp: &Path, name: &str) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + fs::write(root.join("target"), b"same content\n")?; + std::os::unix::fs::symlink("target", root.join("link"))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let clean = build(temp.path(), "clean")?; + let clean_digest = digest_artifact(&clean)?.0; + + let capped = build(temp.path(), "capped")?; + let cpath = std::ffi::CString::new(capped.join("link").as_os_str().as_bytes())?; + let value: [u8; 20] = [ + 0x01, 0x00, 0x00, 0x02, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + let written = unsafe { + libc::lsetxattr( + cpath.as_ptr(), + c"security.capability".as_ptr(), + value.as_ptr() as *const libc::c_void, + value.len(), + 0, + ) + }; + ensure!( + written == 0, + "lsetxattr on the symlink fixture: {}", + std::io::Error::last_os_error() + ); + let capped_digest = digest_artifact(&capped)?.0; + + assert_ne!( + clean_digest, capped_digest, + "an artifact symlink gaining an extended attribute of its own must move the digest" + ); + Ok(()) + } + + /// R-I22 (Self's ruling, eighth Cut 1 fix batch, F4): Soul pass 8 found + /// `hash_xattr_list`'s framing pinned by nothing -- all 193 tests stayed + /// green while dropping the value length prefix, dropping the count + /// prefix, dropping the name NUL terminator, folding names to + /// lowercase, or hashing names only and dropping values entirely. These + /// five tests call `hash_xattr_list` directly (it is private, reached + /// through `super::*` inside this crate's own test module) with hand + /// constructed attribute lists chosen so a specific piece of framing + /// removed is exactly what makes two different lists hash the same -- + /// each one is a real collision if that framing goes, not just an + /// assertion the digest changed for some reason. + /// + /// Chosen so the byte streams collide once the value's own 8-byte + /// length prefix is dropped: `"a"+NUL+"bc"+"d"+NUL+""` and + /// `"a"+NUL+"b"+"cd"+NUL+""` are the same six bytes + /// (`61 00 62 63 64 00`) without it, because the boundary between the + /// first entry's value and the second entry's name is what the length + /// prefix pins. + #[test] + fn hash_xattr_list_pins_each_values_length_prefix() { + let list_a = vec![ + (b"a".to_vec(), b"bc".to_vec()), + (b"d".to_vec(), b"".to_vec()), + ]; + let list_b = vec![ + (b"a".to_vec(), b"b".to_vec()), + (b"cd".to_vec(), b"".to_vec()), + ]; + + let mut hasher_a = Sha256::new(); + hash_xattr_list(&list_a, &mut hasher_a); + let mut hasher_b = Sha256::new(); + hash_xattr_list(&list_b, &mut hasher_b); + + assert_ne!( + format!("{:x}", hasher_a.finalize()), + format!("{:x}", hasher_b.finalize()), + "[(a,bc),(d,)] and [(a,b),(cd,)] write identical bytes without each value's own \ + length prefix" + ); + } + + /// R-I22: chosen so the byte streams collide once the name/value + /// separator is dropped: name `"a"` with a one-byte value `[0x00]`, and + /// name `"a\x01"` (two bytes) with an empty value, both encode to + /// `61 01 00 00 00 00 00 00 00 00` if nothing marks where the name ends + /// and the value's length prefix begins. + #[test] + fn hash_xattr_list_pins_the_name_terminator() { + let list_a = vec![(b"a".to_vec(), vec![0u8])]; + let list_b = vec![(vec![b'a', 0x01], Vec::new())]; + + let mut hasher_a = Sha256::new(); + hash_xattr_list(&list_a, &mut hasher_a); + let mut hasher_b = Sha256::new(); + hash_xattr_list(&list_b, &mut hasher_b); + + assert_ne!( + format!("{:x}", hasher_a.finalize()), + format!("{:x}", hasher_b.finalize()), + "[(a,[0x00])] and [(a\\x01,[])] write identical bytes without a terminator between \ + each name and its value's length prefix" + ); + } + + /// R-I22: names are hashed as raw bytes, not folded -- `user.A` and + /// `user.a` are a real xattr collision class (distinct attributes on + /// case-sensitive filesystems) that a case-insensitive comparison would + /// merge. + #[test] + fn hash_xattr_list_pins_name_case() { + let list_a = vec![(b"user.A".to_vec(), b"same value".to_vec())]; + let list_b = vec![(b"user.a".to_vec(), b"same value".to_vec())]; + + let mut hasher_a = Sha256::new(); + hash_xattr_list(&list_a, &mut hasher_a); + let mut hasher_b = Sha256::new(); + hash_xattr_list(&list_b, &mut hasher_b); + + assert_ne!( + format!("{:x}", hasher_a.finalize()), + format!("{:x}", hasher_b.finalize()), + "user.A and user.a must not hash the same" + ); + } + + /// R-I22: the material finding -- nothing proved an xattr's *value* was + /// hashed at all, so a capability edited from `cap_setuid` to + /// `cap_sys_admin` (same name, different value) would have been covered + /// by nothing before this batch. + #[test] + fn hash_xattr_list_pins_the_value_not_just_the_name() { + let list_a = vec![(b"security.capability".to_vec(), vec![1, 2, 3])]; + let list_b = vec![(b"security.capability".to_vec(), vec![4, 5, 6])]; + + let mut hasher_a = Sha256::new(); + hash_xattr_list(&list_a, &mut hasher_a); + let mut hasher_b = Sha256::new(); + hash_xattr_list(&list_b, &mut hasher_b); + + assert_ne!( + format!("{:x}", hasher_a.finalize()), + format!("{:x}", hasher_b.finalize()), + "the same attribute name with two different values must not hash the same" + ); + } + + /// R-I22: chosen so the byte streams collide once the leading count + /// prefix is dropped -- zero attributes followed by one attribute's own + /// bytes (as if they were the next field the caller hashes, e.g. a + /// file's size-prefixed content) looks identical to one attribute + /// followed by nothing, because every attribute is otherwise + /// self-delimiting (NUL-terminated name, length-prefixed value). + #[test] + fn hash_xattr_list_pins_the_count_prefix() { + let entry_name = b"n".to_vec(); + let entry_value = b"v".to_vec(); + let mut entry_bytes = Vec::new(); + entry_bytes.extend_from_slice(&entry_name); + entry_bytes.push(0); + entry_bytes.extend_from_slice(&(entry_value.len() as u64).to_le_bytes()); + entry_bytes.extend_from_slice(&entry_value); + + let mut hasher_a = Sha256::new(); + hash_xattr_list(&[], &mut hasher_a); + hasher_a.update(&entry_bytes); + + let mut hasher_b = Sha256::new(); + hash_xattr_list(&[(entry_name, entry_value)], &mut hasher_b); + + assert_ne!( + format!("{:x}", hasher_a.finalize()), + format!("{:x}", hasher_b.finalize()), + "zero xattrs followed by one entry's own bytes must not hash the same as one xattr \ + followed by nothing -- only the count prefix distinguishes them, since each entry \ + is otherwise self-delimiting" + ); + } + + /// R-I16 (Self's ruling, seventh Cut 1 fix batch, F4): before this batch + /// `digest_tree` had no mode term for directories at all, so a directory + /// gaining setgid or sticky -- privilege-relevant, unlike a plain + /// permission bit -- re-verified clean. + #[cfg(unix)] + #[test] + fn digest_artifact_differs_when_only_a_directorys_mode_differs() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + // `digest_tree(root, root, ...)` only ever hashes an entry it reads + // out of some directory's `read_dir`, never the digest root itself, + // so the differing directory must be a child of the artifact root. + fn build(temp: &Path, name: &str, mode: u32) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let child = root.join("d"); + fs::create_dir(&child)?; + fs::write(child.join("f"), b"same content\n")?; + fs::set_permissions(&child, fs::Permissions::from_mode(mode))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let root_a = build(temp.path(), "root-a", 0o755)?; + let root_b = build(temp.path(), "root-b", 0o2755)?; + let (digest_a, _) = digest_artifact(&root_a)?; + let (digest_b, _) = digest_artifact(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two artifact trees differing only in a directory's setgid bit must hash differently" + ); + Ok(()) + } + + /// R-I16 (Self's ruling, seventh Cut 1 fix batch, F4): the directory + /// case of the artifact digest's gid-coverage gap. + #[cfg(unix)] + #[test] + fn digest_artifact_differs_when_only_a_directorys_gid_differs() -> Result<()> { + use std::os::unix::fs::chown; + + fn build(temp: &Path, name: &str, gid: u32) -> Result { + let root = temp.join(name); + fs::create_dir(&root)?; + let child = root.join("d"); + fs::create_dir(&child)?; + fs::write(child.join("f"), b"same content\n")?; + chown(&child, None, Some(gid))?; + Ok(root) + } + + let temp = tempfile::tempdir()?; + let root_a = build(temp.path(), "root-a", 0)?; + let root_b = build(temp.path(), "root-b", second_gid_for_chown_fixture()?)?; + let (digest_a, _) = digest_artifact(&root_a)?; + let (digest_b, _) = digest_artifact(&root_b)?; + assert_ne!( + digest_a, digest_b, + "two artifact trees differing only in a directory's owning gid must hash differently" + ); + Ok(()) + } + + // R-I15 (Self's ruling, seventh Cut 1 fix batch, F2): the boundary-shift + // fixture that used to live here + // (`digest_artifact_differs_across_a_symlink_target_boundary_shift`) is + // deleted, not repaired. Soul pass 7 confirmed it cannot fail: the link + // name is already NUL-terminated *before* the target + // (`hasher.update(relative.as_bytes()); hasher.update(b"\0");` in + // `digest_tree`, ahead of the target bytes), so two spellings whose + // (name, target) pairs differ always diverge at that terminator + // regardless of what the (missing) trailing terminator after the target + // does. The collision class the test's own doc comment described is + // real in principle but unreachable in practice: Soul verified by hand + // that `std::os::unix::fs::symlink("a\0b", ..)` and + // `fs::write(OsString::from("a\0b"), ..)` both fail at `InvalidInput` + // before the kernel ever sees the NUL, and the same closure covers path + // components, so no real symlink target or filename can carry the NUL + // byte a boundary collision here would require. The production comment + // above the missing terminator, which says the term is not + // independently pinned, was the true one; this fixture, which claimed + // otherwise and passed under deletion of what it named, was not. See + // Soul pass 7's F2 and R-I15 in `docs/verify-transaction-cut.md`. + + /// R-I13 (Self's ruling, sixth Cut 1 fix batch, F6): `observe_frozen` is + /// the tamper check -- `validate_frozen_source` plus + /// `frozen_source_sha256` compared against the receipt -- and before this + /// test it had no test of its own at all, exercised only through those + /// two constituents separately (S6 pass 6/F6). This builds a real + /// `CompiledDeploymentPlan` (via `compile_deployment_plan`, the same + /// constructor production code uses, over a minimal but real + /// `TargetDeclaration`/`OperatorBinding` TOML pair that carries no + /// runtime state or dependency graph -- kept minimal deliberately, since + /// `observe_frozen` never reads past `plan.source` and `plan.recipe_blob` + /// once `receipt.validate_against` accepts the plan) and a real + /// `FrozenSourceReceipt` end to end through `GitSourceDriver::freeze_exact`, + /// then calls `observe_frozen` directly and confirms it both accepts the + /// untouched tree and refuses once the tree is tampered with after + /// freezing (a file's content changed post-freeze, caught only because + /// `observe_frozen` re-derives the digest rather than trusting the + /// receipt it was handed). + #[cfg(unix)] + #[test] + fn observe_frozen_accepts_an_intact_receipt_and_refuses_a_tampered_one() -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + const RECIPE: &str = r#" +schema = "gamecult.idunn.target_declaration.v1" +target = "observe-frozen-test" +source_stamp_environment = "OBSERVE_FROZEN_TEST_BUILD_COMMIT" + +[[steps]] +id = "build" +phase = "build" +runner = "rust" +argv = ["cargo", "--version"] + +[[artifacts]] +id = "daemon" +source_kind = "worktree-tree" +source = "deployment.toml" +destination = "daemon" + +[service] +executable_artifact = "daemon" +transport = "http" +required_environment = ["GAMECULT_IDUNN_RUNTIME_BUNDLE"] + +[service.health] +contract = "observe-frozen-test.health" +"#; + + const BINDING: &str = r#" +schema = "gamecult.idunn.operator_binding.v2" +target = "observe-frozen-test" + +[repository] +origin = "https://example.invalid/observe-frozen-test.git" +admitted_ref = "refs/heads/main" +minimum_revision = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +selection = "ref-head" +checkout = "/srv/build/observe-frozen-test" +recipe_path = "deployment.toml" + +[runners.rust] +driver = "docker" +image = "rust@sha256:4c2fd73ef19c5ef9d54bee03b06b2839a392604fbfcd578ed948b71b37c1d7fb" +user = "1000:1000" +affordances = ["source-read"] +allowed_programs = ["cargo"] +memory_mebibytes = 8192 +cpu_quota_percent = 400 +pids_limit = 512 +tmpfs_mebibytes = 1024 + +[workload] +driver = "systemd-transient" +unit_prefix = "idunn-observe-frozen-test" +release_root = "/srv/observe-frozen-test/releases" +runtime_root = "/etc/gamecult/observe-frozen-test/runtime" +network = "host-private" +hardening = "strict" +memory_mebibytes = 2048 +cpu_quota_percent = 200 + +[workload.secret_files] +GAMECULT_RUNTIME_PRESENCE_IDENTITY = "/etc/gamecult/observe-frozen-test/runtime-presence-identity.cc" + +[runtime_identity] +runtime_id = "observe-frozen-test-yggdrasil" +expected_signer_identity_id = "observe-frozen-test-runtime-signer" +trust_anchor_store = "/etc/gamecult/trust/observe-frozen-test.cc" + +[brakes] +deployment_store = "/var/lib/gamecult/idunn-authority/observe-frozen-test-deployment-brake.cc" +lifecycle_store = "/var/lib/gamecult/idunn-authority/observe-frozen-test-lifecycle-brake.cc" + +[rollout] +strategy = "candidate-then-promote" +drain_seconds = 30 +retain_releases = 2 + +[placement] +desired_replicas = 1 +nodes = ["yggdrasil"] +"#; + + let temp = tempfile::tempdir()?; + fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o755))?; + + let origin_repo = temp.path().join("origin"); + fs::create_dir(&origin_repo)?; + git_at(&origin_repo, &["init", "--initial-branch=main"])?; + git_at(&origin_repo, &["config", "user.name", "Idunn Test"])?; + git_at( + &origin_repo, + &["config", "user.email", "idunn-test@example.invalid"], + )?; + fs::write(origin_repo.join("deployment.toml"), RECIPE)?; + // A second file, otherwise unreferenced by the plan: tampering with + // `deployment.toml` itself would also be caught by the separate + // recipe-bytes-equality check further down in `observe_frozen` + // ("observed deployment recipe differs from the persisted plan"), + // which would pass this test even with the digest re-check deleted + // -- confirmed by hand-mutation. Tampering with this file instead is + // caught only by `frozen_source_sha256`'s re-derivation. + fs::write(origin_repo.join("extra.txt"), b"original\n")?; + git_at(&origin_repo, &["add", "--all"])?; + git_at(&origin_repo, &["commit", "-m", "fixture"])?; + let revision = git_at(&origin_repo, &["rev-parse", "HEAD"])?; + // See `freeze_exact_is_byte_exact_and_recipe_checked`: the + // unprivileged identity below clones this fixture as a local path, + // which puts Git's ownership check in play, so hand the fixture to + // that identity the way a real network remote never has to. + ensure!( + Command::new("/bin/chown") + .args(["-R", "1000:1000"]) + .arg(&origin_repo) + .status()? + .success(), + "chowning the fixture origin repository" + ); + + let source_cache_root = temp.path().join("source-cache"); + let frozen_source_root = temp.path().join("frozen-source"); + fs::create_dir(&frozen_source_root)?; + fs::set_permissions(&frozen_source_root, fs::Permissions::from_mode(0o700))?; + + let identity = ProcessIdentity { + uid: 1000, + gid: 1000, + }; + let driver = GitSourceDriver::new( + source_cache_root.clone(), + frozen_source_root.clone(), + Some(identity), + ); + let source = ExactSource { + origin: origin_repo.to_string_lossy().into_owned(), + checkout: source_cache_root.join("checkout"), + gitlinks: BTreeMap::new(), + recipe_path: PathBuf::from("deployment.toml"), + }; + + let transaction_id = "txn-observe-frozen"; + let (tree_root, snapshot_sha256, recipe_bytes, _contains_lfs_pointers) = + driver.freeze_exact(&source, &revision, transaction_id, &frozen_source_root)?; + assert_eq!(recipe_bytes, RECIPE.as_bytes()); + + let facts = SourceSelectionFacts { + schema: SOURCE_SELECTION_FACTS_SCHEMA.into(), + origin: "https://example.invalid/observe-frozen-test.git".into(), + admitted_ref: "refs/heads/main".into(), + admitted_ref_revision: revision.clone(), + revision: revision.clone(), + source_tree: revision.clone(), + recipe_path: PathBuf::from("deployment.toml"), + recipe_blob_sha256: sha256_id(&recipe_bytes), + gitlinks: BTreeMap::new(), + selection: SourceSelection::RefHead, + selected_at_unix_millis: 1, + }; + let plan = compile_deployment_plan( + &recipe_bytes, + BINDING.as_bytes(), + facts, + "observe-frozen-test-incarnation", + None, + 1, + &[], + ) + .context("compiling the minimal observe_frozen fixture plan")?; + + let receipt = FrozenSourceReceipt { + transaction_id: transaction_id.to_owned(), + plan_id: plan.plan_id.clone(), + snapshot_sha256: snapshot_sha256.clone(), + }; + + let observed = driver.observe_frozen(&plan, &receipt)?; + assert_eq!(observed.receipt.snapshot_sha256, snapshot_sha256); + + // Tamper with a file the plan never re-reads on its own, bypassing + // the writer entirely -- exactly the shape `observe_frozen`'s digest + // re-check exists to catch, and nothing else in `observe_frozen` + // happens to catch incidentally. Root (this test's own euid, + // required by `freeze_exact` itself) can write a 0444 root-owned + // file directly via DAC override, without needing to relax its mode + // first. + let tampered_file = tree_root.join("extra.txt"); + fs::write(&tampered_file, b"tampered\n")?; + + let result = driver.observe_frozen(&plan, &receipt); + assert!( + result.is_err(), + "observe_frozen must refuse a frozen tree that was tampered with after freezing: \ + {result:?}" + ); + Ok(()) + } + + + /// R-I4 (Self's ruling, fifth Cut 1 fix batch): pins S4-6's guard and + /// fixes its off-by-one. `materialize_tree_raw`'s symlink-target length + /// `ensure!`s used `<=` against `FROZEN_SOURCE_SYMLINK_TARGET_LIMIT` + /// (`PATH_MAX`, 4096), admitting a 4096-byte target that the kernel's + /// own `symlink()` then refuses with `ENAMETOOLONG` -- confirmed by + /// hand against this container's kernel: a 4095-byte target writes + /// cleanly, a 4096-byte one does not (`errno 36`, "File name too + /// long"). The guard now refuses at 4096, matching this container's + /// kernel and every VFS's own `PATH_MAX` ceiling (see R-I11 above the + /// constant's definition: a filesystem may refuse a target sooner than + /// that, so "matches the kernel" is a `PATH_MAX` claim, not a claim + /// about every filesystem's own limit), so the failure surfaces as this + /// guard's own message instead of an opaque OS error after + /// `fs::read`ing a target this large into memory -- the exact shape + /// S4-6 exists to avoid. + #[cfg(unix)] + #[test] + fn materialize_tree_raw_admits_the_kernels_symlink_target_limit_and_refuses_one_byte_more() + -> Result<()> { + use std::os::unix::fs::PermissionsExt; + + fn build_and_materialize(byte_len: usize) -> Result<()> { + let temp = tempfile::tempdir()?; + let repository = temp.path().join("repository"); + fs::create_dir(&repository)?; + git_at(&repository, &["init", "--initial-branch=main"])?; + git_at(&repository, &["config", "user.name", "Idunn Test"])?; + git_at( + &repository, + &["config", "user.email", "idunn-test@example.invalid"], + )?; + fs::write(repository.join("deployment.toml"), b"target = 'test'\n")?; + git_at(&repository, &["add", "--all"])?; + git_at(&repository, &["commit", "-m", "root"])?; + let recipe_blob = git_at(&repository, &["rev-parse", "HEAD:deployment.toml"])?; + + // A blob this large can never come from a real `symlink()` call + // (the kernel refuses to create one past its own limit), so the + // fixture writes the oversized target straight into a Git blob + // -- exactly how a hostile or merely careless commit could still + // get one past a Git-level check into the tree this guard reads. + let target = vec![b'a'; byte_len]; + let target_blob = hash_object(&repository, &target)?; + let tree = literal_tree( + &repository, + &[ + ("100644", "deployment.toml", &recipe_blob), + ("120000", "link", &target_blob), + ], + )?; + let commit = git_owned(&repository, &["commit-tree", &tree, "-m", "symlink"])?; + + let parent = temp.path().join("transaction"); + fs::create_dir(&parent)?; + fs::set_permissions(&parent, fs::Permissions::from_mode(0o700))?; + let destination = parent.join("source"); + prepare_frozen_source_destination(&destination)?; + let driver = GitSourceDriver::new( + temp.path().join("source-cache"), + temp.path().join("frozen-source"), + None, + ); + let mut created_dirs = std::collections::HashSet::new(); + created_dirs.insert(destination.clone()); + driver.materialize_tree_raw( + &repository, + &commit, + &destination, + &destination, + &mut created_dirs, + )?; + Ok(()) + } + + build_and_materialize(4095).with_context(|| { + "4095 bytes is the kernel's own symlink-target ceiling and must be admitted" + })?; + let error = build_and_materialize(4096) + .expect_err("4096 bytes exceeds the kernel's own symlink-target ceiling and must be refused"); + let rendered = format!("{error:#}"); + assert!( + rendered.contains("exceeds the platform's path length limit"), + "expected the guard's own message, not a bare OS failure after a wasted write \ + attempt, got: {rendered}" + ); + Ok(()) + } + // ----------------------------------------------------------------- // S4-3 (Self's ruling, fourth Cut 1 fix batch): three of the five S5 // tests did not kill their own guard, because another refusal fired @@ -11719,43 +13910,6 @@ mod tests { fn git_tree_entries_refuses_a_tree_that_emits_a_path_twice() -> Result<()> { use std::os::unix::fs::PermissionsExt; - fn hash_object(repo: &Path, content: &[u8]) -> Result { - let mut child = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(repo) - .args(["hash-object", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - child.stdin.take().unwrap().write_all(content)?; - let output = child.wait_with_output()?; - ensure!(output.status.success(), "git hash-object failed"); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - } - fn literal_tree(repo: &Path, entries: &[(&str, &str, &str)]) -> Result { - let hex = |s: &str| -> Vec { - (0..20) - .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap()) - .collect() - }; - let mut raw = Vec::new(); - for (mode, name, sha) in entries { - raw.extend_from_slice(format!("{mode} {name}\0").as_bytes()); - raw.extend(hex(sha)); - } - let mut child = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(repo) - .args(["hash-object", "-t", "tree", "--literally", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - child.stdin.take().unwrap().write_all(&raw)?; - let output = child.wait_with_output()?; - ensure!(output.status.success(), "git hash-object --literally failed"); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - } - let temp = tempfile::tempdir()?; fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o755))?; let origin_repo = temp.path().join("origin"); @@ -11768,14 +13922,7 @@ mod tests { git_at(&origin_repo, &["commit", "-m", "root"])?; let root_commit = git_at(&origin_repo, &["rev-parse", "HEAD"])?; let recipe_blob = git_at(&origin_repo, &["rev-parse", "HEAD:deployment.toml"])?; - ensure!( - Command::new("/bin/chown") - .args(["-R", "1000:1000"]) - .arg(&origin_repo) - .status()? - .success(), - "chowning the fixture origin repository" - ); + chown_to_source_identity(&origin_repo)?; let source_cache_root = temp.path().join("source-cache"); let frozen_source_root = temp.path().join("frozen-source"); @@ -11816,28 +13963,11 @@ mod tests { ("100644", "same.txt", &b), ], )?; - let git_owned = |args: &[&str]| -> Result { - let output = Command::new("git") - .args([ - "-c", "safe.directory=*", - "-c", "user.name=Idunn Test", - "-c", "user.email=idunn-test@example.invalid", - "-C", - ]) - .arg(&origin_repo) - .args(args) - .output()?; - ensure!( - output.status.success(), - "git {:?} failed: {}", - args, - String::from_utf8_lossy(&output.stderr) - ); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - }; - let hostile_commit = - git_owned(&["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"])?; - git_owned(&["update-ref", "refs/heads/main", &hostile_commit])?; + let hostile_commit = git_owned( + &origin_repo, + &["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"], + )?; + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &hostile_commit])?; // The hostile objects land already-local through an UNGUARDED fetch // (no `transfer.fsckObjects=true`), exactly like Soul's probe A: no @@ -11951,53 +14081,6 @@ mod tests { #[allow(clippy::type_complexity)] fn s4_4_hostile_fixtures() -> Vec<(&'static str, Box Result>>)> { - fn hash_object(repo: &Path, content: &[u8]) -> Result { - let mut child = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(repo) - .args(["hash-object", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - child.stdin.take().unwrap().write_all(content)?; - let output = child.wait_with_output()?; - ensure!(output.status.success(), "git hash-object failed"); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - } - fn literal_tree(repo: &Path, entries: &[(&str, &str, &str)]) -> Result { - let hex = |s: &str| -> Vec { - (0..20) - .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap()) - .collect() - }; - let mut raw = Vec::new(); - for (mode, name, sha) in entries { - raw.extend_from_slice(format!("{mode} {name}\0").as_bytes()); - raw.extend(hex(sha)); - } - let mut child = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(repo) - .args(["hash-object", "-t", "tree", "--literally", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - child.stdin.take().unwrap().write_all(&raw)?; - let output = child.wait_with_output()?; - ensure!(output.status.success(), "git hash-object --literally failed"); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - } - fn dotgit_lookalike(repo: &Path, recipe: &str, name: &str) -> Result> { - let cfg = hash_object(repo, b"[core]\n\tfsmonitor = touch /tmp/s4-4-pwned\n")?; - let inner = literal_tree(repo, &[("100644", "config", &cfg)])?; - let mut entries = vec![ - ("100644".to_owned(), "deployment.toml".to_owned(), recipe.to_owned()), - ("40000".to_owned(), name.to_owned(), inner), - ]; - entries.sort_by(|a, b| a.1.cmp(&b.1)); - Ok(entries) - } - vec![ ( "dup-trees", @@ -12097,14 +14180,7 @@ mod tests { git_at(&origin_repo, &["commit", "-m", "root"])?; let root_commit = git_at(&origin_repo, &["rev-parse", "HEAD"])?; let recipe_blob = git_at(&origin_repo, &["rev-parse", "HEAD:deployment.toml"])?; - ensure!( - Command::new("/bin/chown") - .args(["-R", "1000:1000"]) - .arg(&origin_repo) - .status()? - .success(), - "chowning the fixture origin repository" - ); + chown_to_source_identity(&origin_repo)?; let source_cache_root = temp.path().join("source-cache"); let frozen_source_root = temp.path().join("frozen-source"); @@ -12136,50 +14212,13 @@ mod tests { .iter() .map(|(mode, name, sha)| (mode.as_str(), name.as_str(), sha.as_str())) .collect(); - let hex = |s: &str| -> Vec { - (0..20) - .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap()) - .collect() - }; - let mut raw = Vec::new(); - for (mode, name, sha) in &entry_refs { - raw.extend_from_slice(format!("{mode} {name}\0").as_bytes()); - raw.extend(hex(sha)); - } - let mut child = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(&origin_repo) - .args(["hash-object", "-t", "tree", "--literally", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - child.stdin.take().unwrap().write_all(&raw)?; - let tree_output = child.wait_with_output()?; - ensure!(tree_output.status.success(), "git hash-object --literally failed"); - let hostile_tree = String::from_utf8(tree_output.stdout)?.trim().to_owned(); + let hostile_tree = literal_tree(&origin_repo, &entry_refs)?; - let git_owned = |args: &[&str]| -> Result { - let output = Command::new("git") - .args([ - "-c", "safe.directory=*", - "-c", "user.name=Idunn Test", - "-c", "user.email=idunn-test@example.invalid", - "-C", - ]) - .arg(&origin_repo) - .args(args) - .output()?; - ensure!( - output.status.success(), - "git {:?} failed: {}", - args, - String::from_utf8_lossy(&output.stderr) - ); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - }; - let hostile_commit = - git_owned(&["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"])?; - git_owned(&["update-ref", "refs/heads/main", &hostile_commit])?; + let hostile_commit = git_owned( + &origin_repo, + &["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"], + )?; + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &hostile_commit])?; // Unguarded fetch: no `transfer.fsckObjects=true`. Most of // Soul's fixtures land already local this way, exactly like her @@ -12248,48 +14287,8 @@ mod tests { let root_commit = git_at(&origin_repo, &["rev-parse", "HEAD"])?; let recipe_blob = git_at(&origin_repo, &["rev-parse", "HEAD:deployment.toml"])?; - let cfg = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(&origin_repo) - .args(["hash-object", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .and_then(|mut child| { - child - .stdin - .take() - .unwrap() - .write_all(b"[core]\n\tfsmonitor = touch /tmp/s4-4c-pwned\n")?; - child.wait_with_output() - })?; - ensure!(cfg.status.success(), "git hash-object failed"); - let cfg_blob = String::from_utf8(cfg.stdout)?.trim().to_owned(); - - let hex = |s: &str| -> Vec { - (0..20) - .map(|i| u8::from_str_radix(&s[2 * i..2 * i + 2], 16).unwrap()) - .collect() - }; - let write_tree = |entries: &[(&str, &str, &str)]| -> Result { - let mut raw = Vec::new(); - for (mode, name, sha) in entries { - raw.extend_from_slice(format!("{mode} {name}\0").as_bytes()); - raw.extend(hex(sha)); - } - let mut child = Command::new("git") - .args(["-c", "safe.directory=*", "-C"]) - .arg(&origin_repo) - .args(["hash-object", "-t", "tree", "--literally", "-w", "--stdin"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - child.stdin.take().unwrap().write_all(&raw)?; - let output = child.wait_with_output()?; - ensure!(output.status.success(), "git hash-object --literally failed"); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - }; - let inner = write_tree(&[("100644", "config", &cfg_blob)])?; + let cfg_blob = hash_object(&origin_repo, b"[core]\n\tfsmonitor = touch /tmp/idunn-test-pwned\n")?; + let inner = literal_tree(&origin_repo, &[("100644", "config", &cfg_blob)])?; let mut top_entries = vec![ ("100644".to_owned(), "deployment.toml".to_owned(), recipe_blob.clone()), ("40000".to_owned(), ".git".to_owned(), inner), @@ -12299,37 +14298,13 @@ mod tests { .iter() .map(|(m, n, s)| (m.as_str(), n.as_str(), s.as_str())) .collect(); - let hostile_tree = write_tree(&top_refs)?; + let hostile_tree = literal_tree(&origin_repo, &top_refs)?; - ensure!( - Command::new("/bin/chown") - .args(["-R", "1000:1000"]) - .arg(&origin_repo) - .status()? - .success(), - "chowning the fixture origin repository" - ); - let git_owned = |args: &[&str]| -> Result { - let output = Command::new("git") - .args([ - "-c", "safe.directory=*", - "-c", "user.name=Idunn Test", - "-c", "user.email=idunn-test@example.invalid", - "-C", - ]) - .arg(&origin_repo) - .args(args) - .output()?; - ensure!( - output.status.success(), - "git {:?} failed: {}", - args, - String::from_utf8_lossy(&output.stderr) - ); - Ok(String::from_utf8(output.stdout)?.trim().to_owned()) - }; - let hostile_commit = - git_owned(&["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"])?; + chown_to_source_identity(&origin_repo)?; + let hostile_commit = git_owned( + &origin_repo, + &["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"], + )?; let source_cache_root = temp.path().join("source-cache"); let frozen_source_root = temp.path().join("frozen-source"); @@ -12357,7 +14332,7 @@ mod tests { // been fetched by anything, guarded or not, and `freeze_exact`'s own // fetch is the first thing to ever see it. driver.prepare_source_root(&source)?; - git_owned(&["update-ref", "refs/heads/main", &hostile_commit])?; + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &hostile_commit])?; let result = driver.freeze_exact(&source, &hostile_commit, "txn-s4-4c", &frozen_source_root); @@ -12369,4 +14344,462 @@ mod tests { ); Ok(()) } + + // ----------------------------------------------------------------- + // R-I6 (Self's ruling, fifth Cut 1 fix batch): S5-7 -- `SourcePort:: + // resolve` has one production caller and, before this, zero tests. + // Every other fixture in this suite approximates `resolve()` with a + // `file://` origin or a hand-copied fetch invocation; this one stands + // up a real HTTPS Git origin instead. Soul's own pass-5 rig (self- + // signed CA, `git-http-backend` behind TLS, a validated + // `OperatorBinding`, a clean fetch, then `main` moved onto a hostile + // commit) lived in Soul's session scratchpad, which -- as SKILL.md's + // own QUIC scar already documents -- does not survive past the + // session. It was gone by the time this batch landed (checked: no + // matching session, no leftover Yggdrasil work directory, no local + // scratch trace), so this rebuilds the rig from the map's description + // rather than reusing Soul's files, which is the discrepancy this + // comment records. + // ----------------------------------------------------------------- + + /// A minimal CGI-over-HTTPS bridge for `git-http-backend`: no web + /// server in `eureka-verify-rust` speaks smart HTTP out of the box, but + /// `python3` and `openssl` do exist there, and bridging CGI to a + /// `BaseHTTPRequestHandler` wrapped in an `ssl.SSLContext` is the + /// smallest thing that gets a real `https://` clone working. Written to + /// a temp file and spawned fresh per test run; never installed anywhere + /// permanent. + #[cfg(unix)] + const GIT_HTTPS_BRIDGE_PY: &str = r#" +import http.server +import os +import socketserver +import ssl +import subprocess +import sys + +port_file, repo_root, cert, key, git_http_backend = sys.argv[1:6] + + +class CgiTlsHandler(http.server.BaseHTTPRequestHandler): + protocol_version = "HTTP/1.1" + + def _run_backend(self): + length = int(self.headers.get("Content-Length", 0) or 0) + body = self.rfile.read(length) if length else b"" + path, _, query = self.path.partition("?") + env = { + "GATEWAY_INTERFACE": "CGI/1.1", + "SERVER_PROTOCOL": "HTTP/1.1", + "SERVER_SOFTWARE": "idunn-test-git-https-bridge", + "REQUEST_METHOD": self.command, + "SCRIPT_NAME": "", + "PATH_INFO": path, + "QUERY_STRING": query, + "CONTENT_TYPE": self.headers.get("Content-Type", ""), + "CONTENT_LENGTH": str(length), + "GIT_PROJECT_ROOT": repo_root, + "GIT_HTTP_EXPORT_ALL": "1", + "REMOTE_ADDR": self.client_address[0], + "SERVER_NAME": "127.0.0.1", + "SERVER_PORT": str(self.server.server_port), + "REQUEST_URI": self.path, + "PATH": "/usr/bin:/bin", + } + proc = subprocess.run( + [git_http_backend], input=body, capture_output=True, env=env + ) + out = proc.stdout + for sep in (b"\r\n\r\n", b"\n\n"): + if sep in out: + header_blob, _, response_body = out.partition(sep) + break + else: + header_blob, response_body = b"", out + status = 200 + header_lines = [] + for line in header_blob.decode("latin1").splitlines(): + if not line.strip(): + continue + if line.lower().startswith("status:"): + status = int(line.split(":", 1)[1].strip().split(" ")[0]) + else: + header_lines.append(line) + self.send_response(status) + for line in header_lines: + name, _, value = line.partition(":") + self.send_header(name.strip(), value.strip()) + self.send_header("Content-Length", str(len(response_body))) + self.end_headers() + self.wfile.write(response_body) + + def do_GET(self): + self._run_backend() + + def do_POST(self): + self._run_backend() + + def log_message(self, *args): + pass + + +class Server(socketserver.ThreadingMixIn, http.server.HTTPServer): + daemon_threads = True + allow_reuse_address = True + + +def main(): + httpd = Server(("127.0.0.1", 0), CgiTlsHandler) + ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + ctx.load_cert_chain(cert, key) + httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True) + with open(port_file, "w") as f: + f.write(str(httpd.server_address[1])) + f.flush() + os.fsync(f.fileno()) + httpd.serve_forever() + + +if __name__ == "__main__": + main() +"#; + + /// The full `gamecult.idunn.operator_binding.v2` fixture `deployment.rs` + /// uses, minus the fields `resolve()` never reads, with `{origin}`, + /// `{minimum_revision}` and `{checkout}` left for this test to fill in. + /// `OperatorBinding::parse` runs the real, strict `validate()` over + /// whatever this renders, so this is exactly as demanding as a real + /// operator binding. + #[cfg(unix)] + const HTTPS_RIG_BINDING: &str = r#" +schema = "gamecult.idunn.operator_binding.v2" +target = "https-rig" + +[repository] +origin = "{origin}" +admitted_ref = "refs/heads/main" +minimum_revision = "{minimum_revision}" +selection = "ref-head" +checkout = "{checkout}" +recipe_path = "deployment.toml" + +[runners.rust] +driver = "docker" +image = "rust@sha256:4c2fd73ef19c5ef9d54bee03b06b2839a392604fbfcd578ed948b71b37c1d7fb" +user = "1000:1000" +affordances = ["source-read", "artifact-write", "build-cache"] +cache_root = "/srv/ghostlight/build-cache" +allowed_programs = ["cargo"] +network_profile = "build-dependency-egress" +memory_mebibytes = 8192 +cpu_quota_percent = 400 +pids_limit = 512 +tmpfs_mebibytes = 1024 + +[workload] +driver = "systemd-transient" +state_group = "https-rig" +unit_prefix = "idunn-https-rig" +release_root = "/srv/https-rig/releases" +state_root = "/var/lib/gamecult/https-rig" +runtime_root = "/etc/gamecult/https-rig/runtime" +network = "host-private" +hardening = "strict" +memory_mebibytes = 2048 +cpu_quota_percent = 200 + +[workload.argument_bindings] +state_root = "/var/lib/gamecult/https-rig" + +[workload.secret_files] +GAMECULT_RUNTIME_PRESENCE_IDENTITY = "/etc/gamecult/https-rig/runtime-presence-identity.cc" + +[runtime_identity] +runtime_id = "https-rig-yggdrasil" +expected_signer_identity_id = "https-rig-runtime-signer" +trust_anchor_store = "/etc/gamecult/trust/https-rig.cc" + +[route] +driver = "nginx-stream-tcp" +route_id = "https-rig-public" +stable_endpoint = "http://127.0.0.1:18830" +private_host = "127.0.0.1" +private_port_start = 18831 +private_port_end = 18839 +config_path = "/etc/nginx/idunn-stream-routes/https-rig.conf" +reload_unit = "nginx.service" + +[process_write_lease] +record_path = "/etc/gamecult/https-rig/runtime/process-write-lease.cc" + +[brakes] +deployment_store = "/var/lib/gamecult/idunn-authority/https-rig-deployment-brake.cc" +lifecycle_store = "/var/lib/gamecult/idunn-authority/https-rig-lifecycle-brake.cc" + +[rollout] +strategy = "candidate-then-promote" +drain_seconds = 30 +retain_releases = 2 + +[placement] +desired_replicas = 1 +nodes = ["yggdrasil"] +"#; + + /// R-I6 (Self's ruling, fifth Cut 1 fix batch): pins S5-7. Stands up a + /// real TLS `git-http-backend` origin -- a self-signed CA the client + /// trusts through `$HOME/.gitconfig`'s `http.sslCAInfo` (`HOME` is the + /// same directory `prepare_source_root` assigns to the unprivileged Git + /// identity via `ensure_source_directory`), and a validated + /// `OperatorBinding` whose origin is `https://127.0.0.1:/ + /// origin.git`. A clean revision must resolve; moving `refs/heads/main` + /// onto either hostile tree this suite already builds for + /// `freeze_exact` (duplicate-named trees, a `.GIT` look-alike) must be + /// refused by `resolve()`'s own `transfer.fsckObjects=true` fetch -- + /// the exact guard S1 added, and the only thing standing between an + /// admitted ref and every downstream consumer of `ResolvedSource` for + /// every caller `resolve()` has, HTTPS or not. + /// + /// Skips (does not fail) when `openssl` or `git-http-backend` is + /// unavailable -- both exist in `eureka-verify-rust`, but neither is + /// guaranteed on a workstation. + #[cfg(unix)] + #[test] + fn resolve_admits_a_clean_https_origin_and_refuses_hostile_trees_moved_onto_it() -> Result<()> + { + use std::os::unix::fs::PermissionsExt; + + let git_http_backend = Command::new("git") + .arg("--exec-path") + .output() + .ok() + .filter(|output| output.status.success()) + .map(|output| { + PathBuf::from(String::from_utf8_lossy(&output.stdout).trim().to_owned()) + .join("git-http-backend") + }) + .filter(|path| path.is_file()); + let openssl_present = Command::new("openssl") + .arg("version") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .map(|status| status.success()) + .unwrap_or(false); + let Some(git_http_backend) = git_http_backend else { + eprintln!( + "SKIP resolve_admits_a_clean_https_origin_and_refuses_hostile_trees_moved_onto_it: \ + git-http-backend not found" + ); + return Ok(()); + }; + if !openssl_present { + eprintln!( + "SKIP resolve_admits_a_clean_https_origin_and_refuses_hostile_trees_moved_onto_it: \ + openssl not found" + ); + return Ok(()); + } + + let temp = tempfile::tempdir()?; + fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o755))?; + + // A self-signed certificate for 127.0.0.1, since the origin is + // reached at a loopback address, not a real hostname. + let cert = temp.path().join("cert.pem"); + let key = temp.path().join("key.pem"); + let openssl_status = Command::new("openssl") + .args([ + "req", "-x509", "-newkey", "rsa:2048", "-sha256", "-days", "1", "-nodes", + "-keyout", + ]) + .arg(&key) + .arg("-out") + .arg(&cert) + .args(["-subj", "/CN=127.0.0.1", "-addext", "subjectAltName=IP:127.0.0.1"]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status()?; + ensure!(openssl_status.success(), "generating the self-signed test certificate"); + fs::set_permissions(&cert, fs::Permissions::from_mode(0o644))?; + fs::set_permissions(&key, fs::Permissions::from_mode(0o644))?; + + // The origin: a bare repository built directly with Git plumbing + // (no working tree, so an oversized or hostile blob never has to + // round-trip through a real file on disk to become a commit). + // Reached purely over HTTPS by every caller below, so unlike the + // `file://` hostile-tree fixtures elsewhere in this suite, nothing + // here needs `chown_to_source_identity` or `git_owned`'s dubious- + // ownership workaround: the bridge script below runs + // `git-http-backend` as this same process, against a repository + // this same process owns. + let repos = temp.path().join("repos"); + fs::create_dir(&repos)?; + let origin_repo = repos.join("origin.git"); + fs::create_dir(&origin_repo)?; + git_at(&origin_repo, &["init", "--bare", "--initial-branch=main"])?; + let recipe_blob = hash_object(&origin_repo, b"target = 'test'\n")?; + let root_tree = literal_tree(&origin_repo, &[("100644", "deployment.toml", &recipe_blob)])?; + let root_commit = git_owned(&origin_repo, &["commit-tree", &root_tree, "-m", "root"])?; + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &root_commit])?; + + // The bridge: `git-http-backend` has no HTTPS front end of its own, + // so a small CGI-over-TLS shim (see `GIT_HTTPS_BRIDGE_PY`) stands in + // for a real web server. + let bridge_script = temp.path().join("git_https_bridge.py"); + fs::write(&bridge_script, GIT_HTTPS_BRIDGE_PY)?; + let port_file = temp.path().join("port"); + let mut bridge = Command::new("python3") + .arg(&bridge_script) + .arg(&port_file) + .arg(&repos) + .arg(&cert) + .arg(&key) + .arg(&git_http_backend) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .context("starting the test HTTPS bridge")?; + let port: u16 = { + let mut attempts = 0; + loop { + if let Ok(text) = fs::read_to_string(&port_file) { + if let Ok(port) = text.trim().parse() { + break port; + } + } + if let Some(status) = bridge.try_wait()? { + let mut stderr = String::new(); + if let Some(mut pipe) = bridge.stderr.take() { + let _ = pipe.read_to_string(&mut stderr); + } + bail!("the test HTTPS bridge exited early with {status}: {stderr}"); + } + attempts += 1; + ensure!(attempts < 200, "the test HTTPS bridge never reported its port"); + std::thread::sleep(std::time::Duration::from_millis(25)); + } + }; + struct KillOnDrop(std::process::Child); + impl Drop for KillOnDrop { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } + } + let _bridge = KillOnDrop(bridge); + + let source_cache_root = temp.path().join("source-cache"); + let frozen_source_root = temp.path().join("frozen-source"); + fs::create_dir(&frozen_source_root)?; + fs::set_permissions(&frozen_source_root, fs::Permissions::from_mode(0o700))?; + let identity = ProcessIdentity { uid: 1000, gid: 1000 }; + + // Trust for the self-signed CA is scoped to this driver's own Git + // identity, not the container's real trust store: `prepare_source_ + // root` assigns `HOME` (`source_cache_root/.home`) to `identity` + // and `git_command` clears every other environment variable, so + // `$HOME/.gitconfig`'s `http.sslCAInfo` is the only channel left + // for this test to hand the driver's own `git` a CA it will trust. + // Pre-created here, with the same ownership and mode `ensure_ + // source_directory` would assign, so it passes that function's + // ownership check instead of being reassigned or rejected. + use std::os::unix::ffi::OsStrExt; + fs::create_dir(&source_cache_root)?; + unsafe { + ensure!( + libc::chown( + std::ffi::CString::new(source_cache_root.as_os_str().as_bytes())?.as_ptr(), + identity.uid, + identity.gid, + ) == 0, + "chowning the test source cache root" + ); + } + fs::set_permissions(&source_cache_root, fs::Permissions::from_mode(0o750))?; + let home = source_cache_root.join(".home"); + fs::create_dir(&home)?; + fs::write(home.join(".gitconfig"), format!("[http]\n\tsslCAInfo = {}\n", cert.display()))?; + unsafe { + ensure!( + libc::chown( + std::ffi::CString::new(home.as_os_str().as_bytes())?.as_ptr(), + identity.uid, + identity.gid, + ) == 0, + "chowning the test HOME directory" + ); + ensure!( + libc::chown( + std::ffi::CString::new(home.join(".gitconfig").as_os_str().as_bytes())?.as_ptr(), + identity.uid, + identity.gid, + ) == 0, + "chowning the test .gitconfig" + ); + } + fs::set_permissions(&home, fs::Permissions::from_mode(0o750))?; + fs::set_permissions(home.join(".gitconfig"), fs::Permissions::from_mode(0o640))?; + + let driver = GitSourceDriver::new( + source_cache_root.clone(), + frozen_source_root.clone(), + Some(identity), + ); + let origin_url = format!("https://127.0.0.1:{port}/origin.git"); + let checkout = source_cache_root.join("checkout"); + + let render_binding = |resolution_target: &str| -> String { + HTTPS_RIG_BINDING + .replace("{origin}", &origin_url) + .replace("{minimum_revision}", &root_commit) + .replace("{checkout}", &checkout.to_string_lossy()) + .replace("target = \"https-rig\"", &format!("target = \"{resolution_target}\"")) + }; + + // Clean: `main` still points at the root commit. + let binding = OperatorBinding::parse(&render_binding("https-rig-clean")) + .context("parsing the clean HTTPS rig binding")?; + driver + .resolve(&binding, "r1", 1) + .context("a clean HTTPS origin must resolve")?; + + // Hostile: reuse this suite's own dup-trees and `.GIT` fixtures + // (`s4_4_hostile_fixtures`), moving `main` onto each in turn. + for (label, build) in s4_4_hostile_fixtures() + .into_iter() + .filter(|(label, _)| *label == "dup-trees" || *label == ".GIT") + { + let entries = build(&origin_repo, &recipe_blob) + .with_context(|| format!("building the {label} fixture"))?; + let entry_refs: Vec<(&str, &str, &str)> = entries + .iter() + .map(|(mode, name, sha)| (mode.as_str(), name.as_str(), sha.as_str())) + .collect(); + let hostile_tree = literal_tree(&origin_repo, &entry_refs)?; + let hostile_commit = git_owned( + &origin_repo, + &["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"], + )?; + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &hostile_commit])?; + + let binding = OperatorBinding::parse(&render_binding(&format!( + "https-rig-{}", + label.to_lowercase() + ))) + .with_context(|| format!("parsing the {label} HTTPS rig binding"))?; + let result = driver.resolve(&binding, &format!("r-{label}"), 1); + assert!( + result.is_err(), + "{label}: a hostile tree moved onto main over a real HTTPS origin must be \ + refused by resolve()'s own guarded fetch: {result:?}" + ); + + // Restore `main` to the clean commit before the next fixture, + // so each one is judged against its own fresh fetch. + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &root_commit])?; + } + + Ok(()) + } } diff --git a/src/host.rs b/src/host.rs index 5221f12..8b3eed5 100644 --- a/src/host.rs +++ b/src/host.rs @@ -11,6 +11,16 @@ //! Only the process layer is Windows-specific and it is confined to //! `platform`. Everything else is the same code Idunn runs on yggdrasil. +// R-I23 (Self's ruling, Cut 1's eighth fix batch, F1): `clippy.toml`'s +// `disallowed-methods` list (`Path::canonicalize`, `fs::canonicalize`, +// `fs::symlink_metadata`, `fs::read_link`) exists to keep the frozen-symlink +// resolver in `drivers.rs` from bypassing its `FrozenSymlinkFs` port; it has +// no per-module scope of its own, so it applies to this file too even +// though this file's own executable-identity comparisons have nothing to do +// with that resolver. See `drivers.rs`'s matching attribute for the full +// rationale. +#![allow(clippy::disallowed_methods)] + use std::collections::{BTreeMap, BTreeSet}; use std::fs::{self, File}; use std::io::Write; @@ -36,7 +46,8 @@ use crate::deployment::{ use crate::deployment_plan::{ArtifactReceipt, CompiledDeploymentPlan, SealedRelease}; use crate::drivers::{ HostWorkloadObservation, InstalledReleaseObservation, copy_artifact, copy_tree, - digest_artifact, release_artifact, remove_tree_inside, sha256_id, write_runtime_bundle_records, + digest_artifact, release_artifact, remove_tree_inside, sha256_id, verify_artifact_digest, + write_runtime_bundle_records, }; use crate::host_actuator::{ HOST_ACTUATOR_CHANNEL, HOST_ACTUATOR_CONNECTION_ID, HOST_ACTUATOR_PING_MS, @@ -679,12 +690,17 @@ impl Actuator { ); let (executable, arguments) = Self::launch_command(&declaration, workload, &installed.root)?; - let (executable_sha256, _) = digest_artifact(&executable)?; - let executable_sha256 = format!("sha256-{executable_sha256}"); + // R-I25: recomputes under whichever shape (current or the one + // R-I20 replaced) actually matches `expected.artifact_sha256`, so a + // receipt sealed before that change still verifies. The value + // carried forward is `expected.artifact_sha256` itself, not a fresh + // recompute, so a legacy-shaped receipt is echoed rather than + // silently upgraded to a value it was never sealed against. ensure!( - executable_sha256 == expected.artifact_sha256, + verify_artifact_digest(&executable, &expected.artifact_sha256)?, "installed executable differs from Expected" ); + let executable_sha256 = expected.artifact_sha256.clone(); let environment = self.launch_environment(workload, &bundle)?; if let Some(mut previous) = self.spawned.remove(&activation.runtime_instance_id) { if previous.try_wait()?.is_none() { @@ -800,9 +816,10 @@ impl Actuator { facts.image_path, prior.executable ); - let (executable_sha256, _) = digest_artifact(Path::new(&prior.executable))?; + // R-I25: `prior.executable_sha256` carries whichever shape was + // recorded at `start()`, current or legacy; verify against that. ensure!( - format!("sha256-{executable_sha256}") == prior.executable_sha256, + verify_artifact_digest(Path::new(&prior.executable), &prior.executable_sha256)?, "the installed executable changed under the running process" ); } @@ -897,9 +914,12 @@ fn verify_artifacts(root: &Path, release: &SealedRelease) -> Result<()> { for artifact in &release.artifacts { let path = root.join(&artifact.destination); ensure!(path.exists(), "artifact {} is absent", artifact.artifact_id); - let (sha256, size_bytes) = digest_artifact(&path)?; + // R-I25: the byte length an artifact seals is independent of which + // `digest_artifact` shape computed its digest, so it is checked + // directly rather than through the version fallback below. + let size_bytes = fs::metadata(&path)?.len(); ensure!( - format!("sha256-{sha256}") == artifact.sha256 && size_bytes == artifact.size_bytes, + verify_artifact_digest(&path, &artifact.sha256)? && size_bytes == artifact.size_bytes, "artifact {} differs from its sealed digest", artifact.artifact_id ); @@ -1302,6 +1322,42 @@ mod platform { #[cfg(test)] mod tests { use super::*; + use crate::deployment_plan::ExternalInputMaterializationReceipt; + use crate::drivers::raw_sha256; + + /// R-I25 (Self's ruling: the digest format change is a migration + /// hazard). `verify_artifacts` is exactly `host.rs`'s own stored-value + /// path R-I20 exposed: a receipt sealed by the pre-R-I20 + /// `digest_artifact` (bare content, no mode/gid/xattr term) must still + /// verify under the current code, with no re-seal. + #[test] + fn verify_artifacts_accepts_a_receipt_sealed_under_the_pre_r_i20_shape() -> Result<()> { + let temp = tempfile::tempdir()?; + let root = temp.path().to_path_buf(); + let destination = PathBuf::from("bin/service"); + let content = b"same content\n"; + fs::create_dir_all(root.join("bin"))?; + fs::write(root.join(&destination), content)?; + let legacy_sha256 = format!("sha256-{}", raw_sha256(content)); + + let release = SealedRelease { + schema: "gamecult.idunn.sealed_release.v1".into(), + sealed_release_id: format!("sha256-{}", "0".repeat(64)), + plan_id: "plan-1".into(), + artifacts: vec![ArtifactReceipt { + artifact_id: "service".into(), + destination: destination.clone(), + sha256: legacy_sha256, + size_bytes: content.len() as u64, + executable: true, + }], + external_inputs: Vec::::new(), + sealed_at_unix_millis: 1, + }; + + verify_artifacts(&root, &release)?; + Ok(()) + } #[test] fn config_is_strict_and_absolute() { diff --git a/src/host_actuator.rs b/src/host_actuator.rs index 90592be..2226ae4 100644 --- a/src/host_actuator.rs +++ b/src/host_actuator.rs @@ -1108,7 +1108,11 @@ mod tests { let answerer = thread::spawn(move || -> Result<()> { for _ in 0..500 { client.poll_resends()?; - while let Some(frame) = client.receive_once()? { + // A plain `if let`, not `while`: every path through this body + // returns, so a loop here would never run a second iteration + // (`clippy::never_loop`) -- the outer `for` is what retries + // across polls. + if let Some(frame) = client.receive_once()? { let envelope = HostActuatorEnvelope::decode(&frame.payload)?; let request = envelope.open_request("raven", &idunn_anchor)?; assert_eq!(envelope.session_nonce, nonce); diff --git a/src/provisioning.rs b/src/provisioning.rs index 1c74a83..1d719d7 100644 --- a/src/provisioning.rs +++ b/src/provisioning.rs @@ -1,3 +1,9 @@ +// R-I23 (Self's ruling, Cut 1's eighth fix batch, F1): see the matching +// attribute in `drivers.rs` -- `clippy.toml`'s `disallowed-methods` list has +// no per-module scope, so it reaches this file's unrelated `.canonicalize()` +// call too unless blanket-allowed here. +#![allow(clippy::disallowed_methods)] + use anyhow::{Context, Result, anyhow, bail}; use cultcache_rs::{ CultCacheEnvelope, CultCacheExpectedEnvelope, DatabaseEntry, SingleFileMessagePackBackingStore,