Skip to main content

capsule_runtime/
containment.rs

1//! Containment classes: what an operator *declares* versus what a host actually *achieves*.
2//!
3//! The two directions are deliberately kept apart:
4//!
5//! * The **declared floor** ([`murmur_artifact::ContainmentClass`]) is a requirement, combined
6//!   from the manifest, the workspace config and `--containment` by taking the strongest (see
7//!   [`murmur_artifact::effective_containment_floor`]).
8//! * The **achieved class** starts from [`EnforcementTier`], which is host-probed at launch time.
9//!   [`achieved_class_for_tier`] takes nothing else — not the manifest, not the grant set — so no
10//!   declaration can ever talk a host into reporting a class it cannot back. Exactly one manifest
11//!   property may then talk a *capsule* down from that ceiling, and only downwards:
12//!   `capabilities.filesystem.workdir_exec: true` caps the result at
13//!   [`ContainmentClass::Advisory`] (see [`achieved_containment_class`]), because a capsule whose
14//!   own workdir is executable has given up the claim `scoped` makes. The cap is applied in a
15//!   separate wrapper rather than inside [`achieved_class_for_tier`] precisely so the "no
16//!   declaration raises a class" property stays a property of one small, pure function.
17//!
18//! [`ContainmentClass::Sealed`] is reachable exactly when [`EnforcementTier::KernelSealed`] is:
19//! a Linux host with a usable Landlock ABI, AppArmor out of the way of unprivileged user
20//! namespaces, and a namespace probe that really created one. See [`crate::sealed`] for the
21//! mechanism and for the [`SealedBlocker`] taxonomy that turns "not sealed here" into a specific
22//! command an operator can run.
23//!
24//! The shortfall reason for `sealed` is therefore *mechanism-specific*, not a fixed string: a
25//! host missing the AppArmor profile and a container missing `CAP_SYS_ADMIN` both fail to reach
26//! `sealed`, but they are fixed in completely different places, and a refusal that cannot tell
27//! them apart is a refusal an operator cannot act on.
28
29use murmur_artifact::{ContainmentClass, ExportMode, Exports, FileExport, PeerFilesExport};
30use serde::Serialize;
31
32use crate::{
33    cgroup::{IoMaxReport, IoMaxStatus},
34    errors::RuntimeError,
35    sandbox::{detect_enforcement_tier, EnforcementTier},
36    sealed::{SealedBlocker, UsernsGrant},
37    types::CapabilityPolicy,
38};
39
40pub use crate::sandbox::{detect_sealed_blocker, detect_userns_grant};
41
42/// The class a host in `tier` can actually back, and nothing stronger.
43///
44/// Pure — the host probe lives in [`detect_enforcement_tier`], mirroring the
45/// `tier_from_probe`/`detect_enforcement_tier` split in `sandbox.rs` so this mapping stays
46/// unit-testable on any OS.
47pub(crate) fn achieved_class_for_tier(tier: EnforcementTier) -> ContainmentClass {
48    match tier {
49        // A private mount namespace pivoted onto a composed root: paths outside it are absent,
50        // not merely denied — exactly the mechanism `sealed` names. Landlock and seccomp still
51        // install inside it, so this arm is strictly stronger than `KernelFull`'s, never an
52        // alternative to it.
53        EnforcementTier::KernelSealed => ContainmentClass::Sealed,
54        // Landlock mediates the filesystem and seccomp mediates exec/network: exactly the
55        // mechanism `scoped` names.
56        EnforcementTier::KernelFull => ContainmentClass::Scoped,
57        // Seccomp alone leaves the filesystem scope convention-only, and `EnvironmentOnly` has
58        // no kernel primitive at all. Neither can back a filesystem claim, so neither clears
59        // `scoped`.
60        EnforcementTier::KernelSeccompOnly | EnforcementTier::EnvironmentOnly => {
61            ContainmentClass::Advisory
62        }
63    }
64}
65
66/// The class a session actually achieves: the host's ceiling from [`achieved_class_for_tier`],
67/// capped by what the capsule's own `capabilities.filesystem.workdir_exec` leaves standing.
68///
69/// This is the one place a *manifest* property lowers an achieved class, and it does so by capping
70/// rather than by deciding: [`achieved_class_for_tier`] above stays pure and tier-only, so no
71/// declaration can talk a host up, and this wrapper can only ever talk a capsule *down*.
72///
73/// Why `workdir_exec` caps at [`ContainmentClass::Advisory`]: `scoped` (and `sealed` above it) both
74/// claim that what runs inside the capsule is bounded by what the operator declared. With the
75/// workdir's `Execute` right granted, a binary the agent compiles, downloads or renames inside its
76/// own workdir runs whatever `capabilities.shell.allow` says — the allowlist stops being an
77/// enforceable property. A host that could back `scoped` still cannot back a claim the capsule
78/// itself has given up, so the honest report is `advisory`, on every tier.
79pub(crate) fn achieved_containment_class(
80    tier: EnforcementTier,
81    workdir_exec: bool,
82) -> ContainmentClass {
83    let host_ceiling = achieved_class_for_tier(tier);
84    if workdir_exec {
85        host_ceiling.min(ContainmentClass::Advisory)
86    } else {
87        host_ceiling
88    }
89}
90
91/// Probes this host and reports the strongest class it can actually provide.
92///
93/// Host-only, and deliberately still takes no capsule input: it answers "what can this machine
94/// back?", which is what `mur doctor` and the escape-conformance harness ask. A *session* asks the
95/// narrower question and must use [`achieved_containment_class`] with its own probed tier instead
96/// — see `runtime::stage_session`, which probes the tier once and passes it to both that call and
97/// [`scope_report_for_tier`].
98pub fn detect_achieved_containment() -> ContainmentClass {
99    achieved_class_for_tier(detect_enforcement_tier())
100}
101
102/// Why `achieved` falls short of `declared`, naming the missing *mechanism* rather than the
103/// missing class. `None` when the floor is met.
104///
105/// Pure and host-independent: the caller supplies both classes *and* — for the `sealed` arm — the
106/// already-probed [`SealedBlocker`], so this function still probes nothing and stays testable
107/// without a kernel. `sealed_blocker` is `None` when the caller has not probed (or cannot: a
108/// non-Linux host), in which case the arm falls back to naming the mechanism generically.
109///
110/// `workdir_exec` is the capsule's own `capabilities.filesystem.workdir_exec`. It is checked before
111/// every host arm below, because it is the one shortfall an operator cannot fix by changing hosts:
112/// no kernel backs `scoped` for a capsule that has declared its workdir executable. Naming a
113/// missing Landlock ABI to someone whose real problem is a line in their own manifest sends them to
114/// the wrong machine.
115pub fn containment_shortfall_reason(
116    declared: ContainmentClass,
117    achieved: ContainmentClass,
118    sealed_blocker: Option<SealedBlocker>,
119    workdir_exec: bool,
120) -> Option<String> {
121    if achieved >= declared {
122        return None;
123    }
124
125    if workdir_exec && declared > ContainmentClass::Advisory {
126        return Some(WORKDIR_EXEC_SHORTFALL.to_string());
127    }
128
129    let reason = match declared {
130        // Unreachable in practice — `Advisory` is the weakest class, so nothing is below it —
131        // but stated rather than `unreachable!()` so a future variant reordering cannot panic
132        // a launch path.
133        ContainmentClass::Advisory => {
134            "advisory is the weakest containment class and is satisfied by every host".to_string()
135        }
136        ContainmentClass::Scoped => {
137            "scoped requires Landlock filesystem mediation (Linux 5.13+ with a usable Landlock \
138             ABI); this host provides no kernel filesystem mediation, so paths outside the \
139             workdir are constrained by convention only"
140                .to_string()
141        }
142        // The mechanism exists now, so the reason names *which part of it* this host is missing
143        // and how to fix that part — never the old blanket "no host can provide it today".
144        ContainmentClass::Sealed => match sealed_blocker {
145            Some(blocker) => blocker.reason(),
146            None => "sealed requires a private mount namespace pivoted onto a composed root \
147                     (unshare(CLONE_NEWUSER|CLONE_NEWNS) + pivot_root), which this host did not \
148                     provide"
149                .to_string(),
150        },
151    };
152
153    Some(reason)
154}
155
156/// The reason a capsule that declared `capabilities.filesystem.workdir_exec: true` falls short of
157/// any floor above `advisory`. Fixed text rather than a `match` arm per declared class: the
158/// mechanism, the consequence and the remedy are identical for `scoped` and `sealed`, because both
159/// rest on the claim this declaration gives up.
160const WORKDIR_EXEC_SHORTFALL: &str =
161    "capabilities.filesystem.workdir_exec: true keeps the Landlock Execute right on the session \
162     workdir, so a binary the capsule compiles, downloads or renames inside it runs regardless of \
163     capabilities.shell.allow — the allowlist stops being an enforceable property of this capsule. \
164     No host can back a class above advisory for it. Either remove workdir_exec (the allowlist is \
165     then enforced by the kernel on the resolved path) or lower the declared containment floor to \
166     advisory";
167
168/// The refusal gate: `Ok(())` when the host's `achieved` class meets or exceeds the `declared`
169/// floor, [`RuntimeError::ContainmentFloorUnmet`] otherwise.
170///
171/// Pure — takes every input as an argument and probes nothing, so the whole decision is
172/// testable without a kernel.
173pub fn check_containment_floor(
174    declared: ContainmentClass,
175    achieved: ContainmentClass,
176    sealed_blocker: Option<SealedBlocker>,
177    workdir_exec: bool,
178) -> Result<(), RuntimeError> {
179    match containment_shortfall_reason(declared, achieved, sealed_blocker, workdir_exec) {
180        None => Ok(()),
181        Some(reason) => Err(RuntimeError::ContainmentFloorUnmet {
182            declared,
183            achieved,
184            reason,
185        }),
186    }
187}
188
189/// The declared `exports.files` block as it appears in a [`ScopeReport`] — and therefore in
190/// `mur run --explain-scope --json` and in `trace.jsonl`'s `session_start.effective_grants`.
191///
192/// A flattened copy rather than [`FileExport`] itself: this is a wire shape with a stable field
193/// order and a `mode` that serializes as `"read-only"`, and it must not move whenever the
194/// manifest type gains a field the report has no business publishing.
195#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
196pub struct ExportsFilesReport {
197    /// `exports.files.root` verbatim, as declared.
198    pub root: String,
199    pub mode: ExportMode,
200    /// The effective per-file read ceiling, with the default already applied.
201    pub max_bytes: u64,
202}
203
204impl From<&FileExport> for ExportsFilesReport {
205    fn from(export: &FileExport) -> Self {
206        Self {
207            root: export.root.clone(),
208            mode: export.mode,
209            max_bytes: export.max_bytes,
210        }
211    }
212}
213
214/// The declared `exports.peer_files` block as it appears in a [`ScopeReport`].
215///
216/// A flattened copy rather than [`PeerFilesExport`] itself, on the same terms as
217/// [`ExportsFilesReport`]: a wire shape with a stable field order, which must not move whenever
218/// the manifest type gains a field the report has no business publishing.
219#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
220pub struct PeerFilesReport {
221    /// `exports.peer_files.root` verbatim, as declared.
222    pub root: String,
223    /// The effective handle-lifetime ceiling in seconds, with the ephemeral default already
224    /// applied — so an operator reads the value the runtime will actually clamp to, not the
225    /// absence of a declaration.
226    pub max_ttl_secs: u64,
227    /// The effective per-file ceiling on a redeemed read, with the default already applied.
228    pub max_bytes: u64,
229}
230
231impl From<&PeerFilesExport> for PeerFilesReport {
232    fn from(export: &PeerFilesExport) -> Self {
233        Self {
234            root: export.root.clone(),
235            max_ttl_secs: export.effective_max_ttl_secs(),
236            max_bytes: export.max_bytes,
237        }
238    }
239}
240
241/// One artifact's durable state grant as it appears in a [`ScopeReport`].
242///
243/// A grant rather than a disclosure, unlike [`ExportsFilesReport`] beside it — but reported on
244/// exactly the same terms, and for the same reason: it opens a host path outside the workdir that
245/// an operator reading `filesystem scope` alone would never see.
246#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
247pub struct StateStoreReport {
248    /// The artifact whose entry declared `capabilities.state`.
249    pub artifact: String,
250    /// The store directory name, with the capsule-name default already applied — so an operator
251    /// reads the directory that will actually be opened, not the absence of a declaration.
252    pub store: String,
253    /// Absolute host path of the store, `~/.murmur/state/<store>` with the home already resolved.
254    /// Reported as a string rather than a path so the JSON shape does not vary by platform.
255    pub host_path: String,
256}
257
258/// The filesystem surface one artifact's guest is preopened into, as a stable wire name.
259///
260/// The three values are the whole space: a WASI guest either holds a directory descriptor for the
261/// accessible workdir, holds one for a subtree of it, or holds none at all. Which one an entry
262/// resolves to is decided by its role and its `capabilities.filesystem.scope` in
263/// [`crate::network_policy::preopen_reports`]; the baselines the two roles start from are
264/// [`crate::network_policy::ToolCapabilityGrant`] and
265/// [`crate::network_policy::HookCapabilityGrant`].
266#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
267#[serde(rename_all = "kebab-case")]
268pub enum PreopenSurface {
269    /// The entire accessible workdir, preopened as `"."` with `DirPerms::all()` and
270    /// `FilePerms::all()`. The wide default a `runtime: tool` or `runtime: driver` entry with no
271    /// `capabilities.filesystem.scope` resolves to — see
272    /// [`crate::network_policy::ToolCapabilityGrant`] for the threat that default is chosen
273    /// against and the threat it is not.
274    WholeWorkdir,
275    /// `<accessible workdir>/<scope>` and nothing above it, preopened as `"."` with the same
276    /// rights. What a declared `capabilities.filesystem.scope` resolves to, whatever the role.
277    ScopedSubtree,
278    /// No preopened directory of any kind, so the guest holds no `wasi:filesystem` descriptor. The
279    /// default-deny baseline a `runtime: hook` entry with no `capabilities.filesystem.scope`
280    /// resolves to — see [`crate::network_policy::HookCapabilityGrant`].
281    Nothing,
282}
283
284impl PreopenSurface {
285    /// The phrase [`PreopenReport::render`] names this surface with. Distinct per variant rather
286    /// than a formatted path, because the point of the line is that an operator can tell the three
287    /// apart without knowing the resolved workdir.
288    fn summary(self) -> &'static str {
289        match self {
290            PreopenSurface::WholeWorkdir => "the whole accessible workdir",
291            PreopenSurface::ScopedSubtree => "one subtree of the accessible workdir",
292            PreopenSurface::Nothing => "nothing preopened",
293        }
294    }
295}
296
297/// One artifact's resolved WASI preopen as it appears in a [`ScopeReport`] — and therefore in
298/// `mur run --explain-scope`, in `mur doctor`, and in `trace.jsonl`'s
299/// `session_start.effective_grants`.
300///
301/// Per-artifact, like [`StateStoreReport`] beside it, and reported for the same reason: the
302/// capsule-wide [`ScopeReport::filesystem_scope`] is joined into no guest preopen root, so an
303/// operator reading that field alone learns nothing about what any tool, driver or hook can
304/// actually see.
305#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
306pub struct PreopenReport {
307    /// The artifact whose entry in the capsule operator's own manifest this describes.
308    pub artifact: String,
309    /// [`murmur_artifact::ArtifactRuntime::as_str`] verbatim, so the report and the manifest name
310    /// a role with one vocabulary.
311    pub role: String,
312    /// The entry's declared `capabilities.filesystem.scope`, verbatim. `None` = undeclared, which
313    /// is what [`Self::surface`] has already resolved against the role's own baseline.
314    pub scope: Option<String>,
315    /// What the guest is preopened into once the role's baseline and [`Self::scope`] are both
316    /// applied.
317    pub surface: PreopenSurface,
318}
319
320impl PreopenReport {
321    /// The single line this preopen renders as, shared by [`ScopeReport::render`] and by
322    /// `mur doctor` so the two cannot word the same grant differently.
323    pub fn render(&self) -> String {
324        let artifact = &self.artifact;
325        let role = &self.role;
326        let surface = self.surface.summary();
327        match &self.scope {
328            Some(scope) => {
329                format!("{artifact} ({role}): {surface} — capabilities.filesystem.scope: {scope}")
330            }
331            None => format!(
332                "{artifact} ({role}): {surface} — no capabilities.filesystem.scope declared"
333            ),
334        }
335    }
336}
337
338/// What the filesystem mechanism this session installs restricts, as a stable wire name.
339///
340/// Keyed on [`crate::sandbox::applied_tier`] — the host tier capped by the declared containment
341/// floor, which is the same value [`crate::sandbox::ShellEnforcement::resolve`] installs the
342/// session's mechanisms from. The achieved containment class answers a different question ("what
343/// can this host back"), and a capsule declaring less than `sealed` on a sealed-capable host is
344/// where the two part company: it is reported `achieved: sealed` and runs Landlock-scoped.
345#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
346#[serde(rename_all = "kebab-case")]
347pub enum FilesystemRestriction {
348    /// No kernel mechanism mediates the filesystem, so a path outside the workdir is reachable.
349    Advisory,
350    /// Landlock refuses the open, and the path outside the granted set still exists.
351    Enforced,
352    /// A composed root, where the path outside the capsule is not there to open.
353    Absent,
354}
355
356/// What the filesystem mechanism this session installs does *not* protect, in words an operator
357/// can act on.
358///
359/// The counterpart to every grant beside it in a [`ScopeReport`]: those say what the capsule may
360/// reach, and this says which of the restrictions an operator would read into them are not there.
361///
362/// [`Self::not_protected`] is empty at [`FilesystemRestriction::Absent`] and non-empty below it. It
363/// is serialized either way — an absent key identifies a runtime that predates the field, never a
364/// capsule with nothing to disclaim.
365#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
366pub struct FilesystemBoundaryReport {
367    /// What the session's filesystem mechanism restricts.
368    pub restriction: FilesystemRestriction,
369    /// One statement per protection an operator would otherwise assume, empty at
370    /// [`FilesystemRestriction::Absent`]. Rendered verbatim under `Not protected here`, so the JSON
371    /// and the human report cannot word the same disclaimer differently.
372    pub not_protected: Vec<String>,
373}
374
375/// What a session with no kernel filesystem mechanism leaves unrestricted.
376///
377/// Names the mechanism rather than the class, because "advisory" is the word an operator has
378/// already read on the `achieved:` line and taken for a protection.
379const ADVISORY_FILESYSTEM_NOT_PROTECTED: &str =
380    "Filesystem restriction is advisory on this host: no kernel mechanism denies a read or a write \
381     outside the session workdir, so capabilities.filesystem.scope bounds what the runtime hands a \
382     subprocess rather than what that subprocess can reach. A Linux host with a usable Landlock \
383     ABI installs a Landlock domain instead, where the kernel refuses the open.";
384
385/// What a Landlock-scoped session leaves unrestricted.
386///
387/// Not the `advisory` sentence: Landlock really does mediate the filesystem here, and an operator
388/// told otherwise goes and hardens something the kernel already holds. What Landlock does not do is
389/// make a path absent — it mediates the operations that touch a file, not path resolution.
390const ENFORCED_FILESYSTEM_NOT_PROTECTED: &str =
391    "A path outside the granted set is denied rather than absent: Landlock refuses the open, and \
392     stat, access and readlink on that path still succeed and still report the real file's \
393     metadata, because Landlock mediates the operations that touch a file and not path resolution. \
394     A probe that only asks whether a path exists therefore learns nothing about what was denied — \
395     read the file to see the refusal. Declaring capabilities.containment: sealed on a host that \
396     reaches the sealed tier composes a root the path is not in at all.";
397
398/// What no mechanism protects, and the reason `--explain-scope` carries this block at all.
399///
400/// Identical wherever it prints, because the rewrite it describes is identical: `HOME` is replaced
401/// with the session's synthetic home on every tier and every platform (see `crate::shell`), so the
402/// obvious `~`-based containment probe answers *no such file* on a host that denies nothing.
403const PATH_REWRITING_NOT_PROTECTED: &str =
404    "Path rewriting is a convenience, not a boundary: HOME is set to <workdir>/.capsule-home on \
405     every tier, so `cat ~/.ssh/id_rsa` answers \"No such file or directory\" whether or not \
406     anything was denied — the tilde resolved into the workdir and the host file was never looked \
407     for. A ~-based probe is therefore never evidence of containment; test with an absolute path \
408     to a real file outside the workdir instead.";
409
410/// The filesystem boundary the tier a session applies actually provides, and the protections it
411/// does not.
412///
413/// Pure and derived from the applied tier alone, so `--explain-scope` and
414/// `session_start.effective_grants` cannot word the same disclaimer differently: both go through
415/// [`scope_report_for_tier`], which calls this once with the tier it has already resolved.
416///
417/// The argument is the *applied* tier, never the host tier and never the achieved class. A composed
418/// root is what makes a host path absent, and a session composes one only when the host reaches
419/// `KernelSealed` **and** the capsule declared a `sealed` floor; a Landlock domain is installed
420/// whenever the applied tier is `KernelFull` or `KernelSealed`, including for a capsule whose
421/// `capabilities.filesystem.workdir_exec` caps its achieved class at `advisory`.
422pub(crate) fn filesystem_boundary(applied: EnforcementTier) -> FilesystemBoundaryReport {
423    // The path-rewriting statement rides along with the mechanism-specific one rather than standing
424    // on its own, so a composed root — where the paths outside it are absent, and the `~` rewrite
425    // lands inside the only writable path in that root — discloses nothing at all.
426    let (restriction, filesystem) = match applied {
427        EnforcementTier::KernelSealed => (FilesystemRestriction::Absent, None),
428        EnforcementTier::KernelFull => (
429            FilesystemRestriction::Enforced,
430            Some(ENFORCED_FILESYSTEM_NOT_PROTECTED),
431        ),
432        EnforcementTier::KernelSeccompOnly | EnforcementTier::EnvironmentOnly => (
433            FilesystemRestriction::Advisory,
434            Some(ADVISORY_FILESYSTEM_NOT_PROTECTED),
435        ),
436    };
437
438    let not_protected = match filesystem {
439        Some(filesystem) => vec![
440            filesystem.to_string(),
441            PATH_REWRITING_NOT_PROTECTED.to_string(),
442        ],
443        None => Vec::new(),
444    };
445
446    FilesystemBoundaryReport {
447        restriction,
448        not_protected,
449    }
450}
451
452/// Read-only answer to "what would this capsule actually be allowed to do on this host?".
453///
454/// Built from the already-parsed [`CapabilityPolicy`] plus one host tier probe. Nothing here
455/// stages artifacts, touches a registry, creates a workdir or resolves DNS — it is a diagnostic,
456/// so it reports even when [`Self::floor_met`] is `false`.
457#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
458pub struct ScopeReport {
459    /// Strongest class any source asked for (manifest / workspace config / `--containment`).
460    pub declared_containment: ContainmentClass,
461    /// Strongest class this host can actually back, derived from the tier probe alone.
462    pub achieved_containment: ContainmentClass,
463    /// `achieved_containment >= declared_containment`. `false` means `mur run` would refuse.
464    pub floor_met: bool,
465    /// The missing mechanism when `floor_met` is `false`; absent otherwise.
466    #[serde(skip_serializing_if = "Option::is_none")]
467    pub shortfall_reason: Option<String>,
468    /// The probed host tier, as a stable wire name.
469    pub enforcement_tier: &'static str,
470    /// Where this host's permission to create an unprivileged user namespace comes from, as
471    /// [`UsernsGrant::wire_name`], or `null` off Linux, where AppArmor does not exist and the
472    /// question has no answer.
473    ///
474    /// Always serialized, on the same terms as [`Self::workdir_exec`]: the key's absence
475    /// identifies a runtime that predates it, not a host that was not asked. Reported because
476    /// three very different hosts — no AppArmor, hardening switched off host-wide, and the shipped
477    /// profile confining `mur` — otherwise produce byte-identical reports while differing enormously
478    /// in what else on the machine can create a user namespace.
479    pub userns_grant: Option<UsernsGrant>,
480    /// `capabilities.filesystem.scope`, verbatim.
481    pub filesystem_scope: Option<String>,
482    /// `capabilities.filesystem.workdir_exec`. Always present (never skipped when `false`), so a
483    /// consumer can tell "declared false" from "this runtime predates the key" by the field's
484    /// presence rather than by its value.
485    ///
486    /// `true` is the only manifest declaration that lowers [`Self::achieved_containment`], which is
487    /// why it is reported next to the grants rather than buried: reading `achieved: advisory` on a
488    /// Landlock-capable host makes sense only alongside it.
489    pub workdir_exec: bool,
490    /// `capabilities.filesystem.read_only`, verbatim — the workdir subtrees the capsule may read
491    /// but must not write. Empty when the capsule declares none, which is the whole workdir
492    /// writable. Always serialized as an array (never skipped), on the same terms as
493    /// [`Self::state_stores`]: an absent key identifies a runtime that predates the field, not a
494    /// capsule that declared nothing.
495    ///
496    /// The one field here that subtracts rather than grants, which is why it is reported beside
497    /// [`Self::read_only_advisory_for`] rather than alone: a protection stated without its
498    /// qualification reads as a boundary the capsule does not have.
499    pub read_only_paths: Vec<String>,
500    /// The entries of [`Self::shell_allow`] that [`Self::read_only_paths`] is only advisory
501    /// against — the same set `W-SEC-017` fires for, resolved by the same
502    /// [`read_only_advisory_for`] both this report and `mur doctor` call.
503    ///
504    /// Empty means the declaration is enforced for every readable call. Non-empty names the
505    /// binaries whose own file I/O the dispatch-time write-intent analyser cannot read, so the
506    /// report cannot claim more than `W-SEC-017` already qualifies. Always serialized as an array
507    /// (never skipped), on the same terms as [`Self::read_only_paths`], and always empty when
508    /// that list is: nothing is advisory against a protection that was never declared.
509    pub read_only_advisory_for: Vec<String>,
510    /// The filesystem surface every guest-bearing artifact in this capsule is preopened into, one
511    /// entry per `runtime: tool`, `runtime: driver` and `runtime: hook` entry, in manifest order.
512    /// Empty when the capsule declares only skills. Always serialized as an array (never skipped),
513    /// on the same terms as [`Self::state_stores`]: an absent key identifies a runtime that
514    /// predates the field, not a capsule with no guest artifacts.
515    ///
516    /// This is the field that answers "what can this tool read and write?".
517    /// [`Self::filesystem_scope`] above does not: it is the capsule-wide declaration, and it is
518    /// joined into no preopen root.
519    ///
520    /// Copied through untouched, exactly as the reports beside it are — resolving a preopen
521    /// changes no class, no floor and no tier.
522    pub preopens: Vec<PreopenReport>,
523    /// `capabilities.network.allow`, verbatim — declared destinations, not resolved IPs.
524    pub network_allow: Vec<String>,
525    /// `capabilities.network.unix_sockets`.
526    pub unix_sockets: bool,
527    /// `capabilities.shell.allow`, verbatim.
528    pub shell_allow: Vec<String>,
529    /// `capabilities.spawn.allow`, verbatim.
530    pub spawn_allow: Vec<String>,
531    /// `capabilities.env.allow`, verbatim.
532    pub env_allow: Vec<String>,
533    /// `capabilities.plan.submit` — whether the model is offered the `submit-plan` tool. Always
534    /// serialized (never skipped when `false`), on the same terms as [`Self::workdir_exec`]: an
535    /// absent key identifies a runtime that predates the grant, not a capsule that declined it.
536    ///
537    /// It opens no host resource of its own. It is reported beside the grants because it changes
538    /// *how much* of every other grant one model turn can reach: a plan runs many steps against
539    /// this capsule's own tools, shell allowlist and spawn grant without a turn in between.
540    pub plan_submit: bool,
541    /// Every host directory a `capabilities.shell.interpreter_runtime` grant opens, rendered as
542    /// `<binary>: <dir>[ (list_dir)]`. These are the paths outside the workdir that stay
543    /// reachable even at `scoped`.
544    pub interpreter_runtime_grants: Vec<String>,
545    /// The read-only file surface `exports.files` declares, or `null` when the capsule declares
546    /// none. Always serialized (never skipped), so a consumer can tell "this runtime predates
547    /// exports" from "this capsule declined to export anything" by the field's presence rather
548    /// than by its value — the same terms [`Self::workdir_exec`] is reported on.
549    ///
550    /// An export is a *disclosure*, not a grant: it never appears in
551    /// [`Self::achieved_containment`], and declaring one cannot change any other field of this
552    /// report. It is printed beside the grants because it is the other direction of the same
553    /// question — what crosses the capsule boundary, and which way.
554    pub exports_files: Option<ExportsFilesReport>,
555    /// The peer-handoff surface `exports.peer_files` declares, or `null` when the capsule
556    /// declares none. Always serialized, on the same terms as [`Self::exports_files`].
557    ///
558    /// A separate authoriser from `exports.files`, over a separate subtree: a capsule may declare
559    /// either, both or neither, and this field says nothing about the other. Like every export it
560    /// is a disclosure rather than a grant, and declaring it cannot change
561    /// [`Self::achieved_containment`], [`Self::enforcement_tier`] or [`Self::floor_met`].
562    pub peer_files: Option<PeerFilesReport>,
563    /// `capabilities.peer_fetch.allow`, verbatim — the peers this capsule may redeem a handle
564    /// against. Empty when the block is absent, which is deny.
565    ///
566    /// A separate list from [`Self::network_allow`] and never an alias for it: a destination
567    /// reachable by `send` is not redeemable unless it also appears here, because ingesting a
568    /// peer's bytes into this workdir is a different question from being allowed to talk to it.
569    pub peer_fetch_allow: Vec<String>,
570    /// Every durable state store this session's artifacts declare, one entry per declaring
571    /// artifact, empty when none does. Always serialized as an array (never skipped), on the same
572    /// terms as [`Self::exports_files`]: an absent key identifies a runtime that predates state
573    /// stores, not a capsule that declined one.
574    ///
575    /// Declaring a store cannot change [`Self::achieved_containment`], [`Self::floor_met`] or
576    /// [`Self::enforcement_tier`] — it is copied through untouched, exactly as the export reports
577    /// beside it are. It is reported because it is the one grant that opens a path *outside* every
578    /// workdir, which nothing else in this block would reveal.
579    pub state_stores: Vec<StateStoreReport>,
580    /// Every artifact whose manifest entry declares a `config:` block, in manifest order, empty
581    /// when none does. Always serialized as an array (never skipped), on the same terms as
582    /// [`Self::state_stores`]: an absent key identifies a runtime that predates artifact config,
583    /// not a capsule that declined it.
584    ///
585    /// Names only. What an artifact is configured *with* is operator-authored plaintext that
586    /// belongs in the manifest and in nothing this report — or `trace.jsonl`, which carries this
587    /// report verbatim — writes out.
588    ///
589    /// Declaring config cannot change [`Self::achieved_containment`], [`Self::floor_met`] or
590    /// [`Self::enforcement_tier`]: it adds one environment variable to one guest and reaches no
591    /// grant. Reported because a variable an artifact silently depends on is otherwise invisible
592    /// to an operator reading the capsule's scope.
593    pub configured_artifacts: Vec<String>,
594    /// Every `capabilities.shell.staged_runtime` grant, rendered as
595    /// `<binary>: <source_path> (pin: <pin>)`. These are host runtime trees a `sealed` capsule
596    /// asks to have bind-mounted read-only into its composed root.
597    ///
598    /// Reported whatever the floor and whatever this host can back — a capsule that declares a
599    /// staged runtime and cannot run here is exactly the case an operator is inspecting when they
600    /// reach for `--explain-scope`, so hiding the grant behind a met floor would blank out the
601    /// diagnostic in the one case it is for.
602    pub staged_runtime_grants: Vec<String>,
603    /// Every path the runtime itself writes inside the capsule's workdir, so a consumer for whom
604    /// that workdir is the deliverable can subtract them and be left with what the *capsule*
605    /// changed. Always serialized as an array (never skipped), on the same terms as
606    /// [`Self::state_stores`]: an absent key identifies a runtime that predates the declaration,
607    /// not a runtime that writes nothing.
608    ///
609    /// The one field here that describes what the runtime does rather than what the capsule may
610    /// do. It is reported beside the grants because it answers the other half of "what is this
611    /// workdir", and because a consumer that has to maintain its own list of runtime entries
612    /// silently falls behind every time the set changes — enabling `sealed` being exactly that
613    /// case.
614    ///
615    /// Keyed on the tier this session would install: [`Self::enforcement_tier`] capped by
616    /// [`Self::declared_containment`], which is what decides whether a composed root is built at
617    /// all. Reading `enforcement_tier: mountns+pivot_root+landlock+seccomp` next to a report with
618    /// no `sealed` rows is that cap, not a disagreement — the host could seal and the capsule did
619    /// not ask. See [`crate::workdir_writes`].
620    pub runtime_writes: Vec<crate::workdir_writes::RuntimeWriteReport>,
621    /// The declared `capabilities.resources.cgroup_io_bytes_per_sec` ceiling, and whether it is
622    /// actually on this session's cgroup scope.
623    ///
624    /// Always serialized (never skipped), on the same terms as [`Self::workdir_exec`] and
625    /// [`Self::state_stores`]: an absent key identifies a runtime that predates the field, not a
626    /// host that was not asked. [`IoMaxStatus::NotProbed`] is what "not asked" looks like, and it
627    /// is a value in the report rather than a missing key.
628    ///
629    /// The one field here filled from a *performed* operation rather than from the policy: at
630    /// launch it carries the outcome of the real `io.max` write, established before `session_start`
631    /// is written so `trace.jsonl` cannot claim a ceiling the kernel refused; under
632    /// `--explain-scope` it carries the outcome of the same write against a throwaway cgroup that
633    /// is created and removed. Declaring a ceiling that does not apply changes no class and refuses
634    /// no launch — `io.max` is the one non-fatal cgroup limit — so this field is the only place
635    /// the shortfall appears.
636    pub io_max: IoMaxReport,
637    /// What this session's filesystem mechanism does not restrict — the block `--explain-scope`
638    /// prints as `Not protected here`, directly under `Containment`.
639    ///
640    /// Always serialized (never skipped), on the same terms as [`Self::state_stores`]: an absent
641    /// key identifies a runtime that predates the field, and an empty
642    /// [`FilesystemBoundaryReport::not_protected`] identifies a session with nothing to disclaim.
643    ///
644    /// Derived in [`filesystem_boundary`] from [`crate::sandbox::applied_tier`] — the same value
645    /// [`Self::runtime_writes`] is keyed on — and from no [`CapabilityPolicy`] field. Not from
646    /// [`Self::achieved_containment`], which reports the class this *host* can back: a capsule
647    /// declaring less than `sealed` on a sealed-capable host reads `achieved: sealed` and composes
648    /// no root, so a boundary keyed on the class would report an absence the capsule does not have.
649    /// It stands beside the grants rather than among them because it qualifies every one at once.
650    pub filesystem_boundary: FilesystemBoundaryReport,
651}
652
653impl ScopeReport {
654    /// Human-readable rendering used by `mur run --explain-scope` without `--json`.
655    pub fn render(&self) -> String {
656        let mut out = String::new();
657        out.push_str("Containment\n");
658        out.push_str(&format!("  declared:  {}\n", self.declared_containment));
659        out.push_str(&format!("  achieved:  {}\n", self.achieved_containment));
660        out.push_str(&format!(
661            "  floor met: {}\n",
662            if self.floor_met { "yes" } else { "no" }
663        ));
664        if let Some(reason) = &self.shortfall_reason {
665            out.push_str(&format!("  reason:    {reason}\n"));
666        }
667        out.push_str(&format!("  mechanism: {}\n", self.enforcement_tier));
668        out.push_str(&format!(
669            "  userns grant: {}\n",
670            match self.userns_grant {
671                Some(grant) => grant.wire_name(),
672                None => "n/a",
673            }
674        ));
675
676        out.push_str(&render_not_protected(&self.filesystem_boundary));
677
678        out.push_str("\nEffective grants\n");
679        out.push_str(&format!(
680            "  filesystem scope: {}\n",
681            self.filesystem_scope.as_deref().unwrap_or("<none>")
682        ));
683        out.push_str(&format!("  workdir exec:     {}\n", self.workdir_exec));
684        out.push_str(&render_read_only(
685            &self.read_only_paths,
686            &self.read_only_advisory_for,
687        ));
688        push_list(
689            &mut out,
690            "preopens",
691            &self
692                .preopens
693                .iter()
694                .map(PreopenReport::render)
695                .collect::<Vec<_>>(),
696        );
697        push_list(&mut out, "network allow", &self.network_allow);
698        out.push_str(&format!("  unix sockets:     {}\n", self.unix_sockets));
699        push_list(&mut out, "shell allow", &self.shell_allow);
700        push_list(&mut out, "spawn allow", &self.spawn_allow);
701        push_list(&mut out, "env allow", &self.env_allow);
702        out.push_str(&format!("  plan submit:      {}\n", self.plan_submit));
703        push_list(
704            &mut out,
705            "interpreter runtime",
706            &self.interpreter_runtime_grants,
707        );
708        push_list(&mut out, "staged runtime", &self.staged_runtime_grants);
709        push_list(
710            &mut out,
711            "state stores",
712            &self
713                .state_stores
714                .iter()
715                .map(|store| format!("{}: {} -> {}", store.artifact, store.store, store.host_path))
716                .collect::<Vec<_>>(),
717        );
718        push_list(&mut out, "artifact config", &self.configured_artifacts);
719
720        out.push_str("\nResource plane\n");
721        match &self.exports_files {
722            None => out.push_str("  exports.files: <none>\n"),
723            Some(export) => {
724                out.push_str(&format!("  exports.files root: {}\n", export.root));
725                out.push_str(&format!("  mode:               {}\n", export.mode));
726                out.push_str(&format!(
727                    "  max bytes:          {} (per file)\n",
728                    export.max_bytes
729                ));
730            }
731        }
732
733        out.push_str(&format!(
734            "  io.max:        {} ({} bytes/s declared)\n",
735            match self.io_max.status {
736                IoMaxStatus::Enforced => "enforced",
737                IoMaxStatus::Unavailable => "unavailable",
738                IoMaxStatus::NotRequired => "not-required",
739                IoMaxStatus::NotProbed => "not-probed",
740            },
741            self.io_max.declared_bytes_per_sec
742        ));
743        if let Some(reason) = &self.io_max.reason {
744            out.push_str(&format!("    {reason}\n"));
745        }
746
747        out.push_str("\nPeer handoff\n");
748        match &self.peer_files {
749            None => out.push_str("  exports.peer_files: <none>\n"),
750            Some(peer) => {
751                out.push_str(&format!("  exports.peer_files root: {}\n", peer.root));
752                out.push_str(&format!(
753                    "  max handle ttl:          {}s\n",
754                    peer.max_ttl_secs
755                ));
756                out.push_str(&format!(
757                    "  max bytes:               {} (per file)\n",
758                    peer.max_bytes
759                ));
760            }
761        }
762        push_list(&mut out, "peer_fetch allow", &self.peer_fetch_allow);
763
764        out.push_str("\nRuntime writes\n");
765        out.push_str(&crate::workdir_writes::render_runtime_writes(
766            &self.runtime_writes,
767        ));
768
769        if !self.floor_met {
770            out.push_str(
771                "\nThis is a report only — `mur run` without --explain-scope would refuse to \
772                 launch here.\n",
773            );
774        }
775        out
776    }
777}
778
779/// The allowlisted binaries a declared `capabilities.filesystem.read_only` is only advisory
780/// against: those whose own file I/O the dispatch-time write-intent analyser cannot read, in
781/// `shell_allow` order.
782///
783/// Exactly the set `W-SEC-017` fires one line for, resolved here so `--explain-scope`, `trace.jsonl`
784/// and `mur doctor` cannot disagree with the warning printed beside them. Empty when nothing is
785/// declared read-only: an interpreter is not advisory against a protection that does not exist.
786#[must_use]
787pub fn read_only_advisory_for(read_only_paths: &[String], shell_allow: &[String]) -> Vec<String> {
788    if read_only_paths.is_empty() {
789        return Vec::new();
790    }
791    shell_allow
792        .iter()
793        .filter(|binary| crate::protected_paths::is_advisory_interpreter(binary))
794        .cloned()
795        .collect()
796}
797
798/// The `read_only` block of the scope report: the declared paths, then one line saying whether the
799/// protection is enforced for every readable call or advisory against a named binary.
800///
801/// Shared by [`ScopeReport::render`] and by `mur doctor` so the two cannot word the same grant
802/// differently — the precedent [`PreopenReport::render`] sets. Ends with a newline; emits nothing
803/// beyond the `<none>` line when no path is declared, because there is no enforcement question to
804/// answer about a protection nobody asked for.
805#[must_use]
806pub fn render_read_only(read_only_paths: &[String], advisory_for: &[String]) -> String {
807    let mut out = String::new();
808    push_list(&mut out, "read_only", read_only_paths);
809    if read_only_paths.is_empty() {
810        return out;
811    }
812    if advisory_for.is_empty() {
813        out.push_str(
814            "  read_only enforcement: enforced for every tool call and every shell command the \
815             dispatch check can read\n",
816        );
817    } else {
818        out.push_str(&format!(
819            "  read_only enforcement: advisory against {} — an interpreter's own file I/O is not \
820             something the dispatch check can read; enforced for every tool call and for every \
821             shell command it can read\n",
822            advisory_for.join(", ")
823        ));
824    }
825    out
826}
827
828/// The `Not protected here` block: one line per statement in
829/// [`FilesystemBoundaryReport::not_protected`], and nothing at all when that list is empty.
830///
831/// A heading with no entries under it would read as a disclaimer the reader had to interpret, so a
832/// composed root prints no heading rather than an empty one. Every line is a statement from the report
833/// verbatim, so the human rendering and `--explain-scope --json` say the same thing in the same
834/// words.
835fn render_not_protected(boundary: &FilesystemBoundaryReport) -> String {
836    if boundary.not_protected.is_empty() {
837        return String::new();
838    }
839    let mut out = String::from("\nNot protected here\n");
840    for statement in &boundary.not_protected {
841        out.push_str(&format!("  - {statement}\n"));
842    }
843    out
844}
845
846fn push_list(out: &mut String, label: &str, values: &[String]) {
847    if values.is_empty() {
848        out.push_str(&format!("  {label}: <none>\n"));
849        return;
850    }
851    out.push_str(&format!("  {label}:\n"));
852    for value in values {
853        out.push_str(&format!("    - {value}\n"));
854    }
855}
856
857/// Builds a [`ScopeReport`] for `policy` against this host, with `declared` as the already-
858/// combined floor. Takes one [`crate::sandbox::HostProbe`], so the tier, the blocker and the
859/// grant it reports all describe one host at one moment.
860///
861/// `state_stores`, `configured_artifacts` and `preopens` are resolved by the caller (from the
862/// manifest's artifact entries, via [`crate::state_store::state_store_reports`],
863/// [`crate::artifact_config::configured_artifact_names`] and
864/// [`crate::network_policy::preopen_reports`]) rather than derived here, because all three are
865/// per-artifact and `policy` is capsule-wide. No resolution creates anything, so this stays a
866/// read-only diagnostic.
867pub fn explain_scope(
868    policy: &CapabilityPolicy,
869    declared: ContainmentClass,
870    exports: Option<&Exports>,
871    state_stores: Vec<StateStoreReport>,
872    configured_artifacts: Vec<String>,
873    preopens: Vec<PreopenReport>,
874    io_max: IoMaxReport,
875) -> ScopeReport {
876    let probe = crate::sandbox::HostProbe::probe();
877    scope_report_for_tier(
878        policy,
879        declared,
880        probe.tier(),
881        probe.sealed_blocker(),
882        probe.userns_grant(),
883        exports,
884        state_stores,
885        configured_artifacts,
886        preopens,
887        io_max,
888    )
889}
890
891/// [`explain_scope`] with the tier and the sealed blocker injected — the seam every test uses so
892/// no test depends on the host it happens to run on.
893#[allow(clippy::too_many_arguments)]
894pub(crate) fn scope_report_for_tier(
895    policy: &CapabilityPolicy,
896    declared: ContainmentClass,
897    tier: EnforcementTier,
898    sealed_blocker: Option<SealedBlocker>,
899    userns_grant: Option<UsernsGrant>,
900    exports: Option<&Exports>,
901    state_stores: Vec<StateStoreReport>,
902    configured_artifacts: Vec<String>,
903    preopens: Vec<PreopenReport>,
904    io_max: IoMaxReport,
905) -> ScopeReport {
906    let achieved = achieved_containment_class(tier, policy.workdir_exec_allowed);
907    // The tier this session would actually install: the host reading `enforcement_tier`
908    // reports, capped by the declared floor, which is the same `sandbox::applied_tier`
909    // `ShellEnforcement::resolve` runs. Two fields below are keyed on it rather than on
910    // `achieved`, because both describe mechanisms rather than the class this host can back.
911    let applied = crate::sandbox::applied_tier(tier, declared);
912    let shortfall_reason = containment_shortfall_reason(
913        declared,
914        achieved,
915        sealed_blocker,
916        policy.workdir_exec_allowed,
917    );
918
919    ScopeReport {
920        declared_containment: declared,
921        achieved_containment: achieved,
922        floor_met: shortfall_reason.is_none(),
923        shortfall_reason,
924        enforcement_tier: enforcement_tier_name(tier),
925        userns_grant,
926        // Copied straight through and never consulted above: `achieved` is already computed, and
927        // no export or peer grant may reach it.
928        exports_files: exports
929            .and_then(|exports| exports.files.as_ref())
930            .map(ExportsFilesReport::from),
931        peer_files: exports
932            .and_then(|exports| exports.peer_files.as_ref())
933            .map(PeerFilesReport::from),
934        peer_fetch_allow: policy.peer_fetch_allow.clone(),
935        // Resolved by `state_store::state_store_reports` before this call and copied through on
936        // the same terms as the export reports above: a grant that opens a path outside the
937        // workdir still gets no say in what class this host is reported to back.
938        state_stores,
939        // Resolved by `artifact_config::configured_artifact_names` before this call and copied
940        // through on the same terms: one environment variable delivered to one guest gets no say
941        // in what class this host is reported to back.
942        configured_artifacts,
943        filesystem_scope: policy.filesystem_scope.clone(),
944        workdir_exec: policy.workdir_exec_allowed,
945        read_only_paths: policy.read_only_paths.clone(),
946        read_only_advisory_for: read_only_advisory_for(
947            &policy.read_only_paths,
948            &policy.shell_allow,
949        ),
950        // Resolved by `network_policy::preopen_reports` before this call and copied through on the
951        // same terms as the reports above: what one guest is preopened into gets no say in what
952        // class this host is reported to back.
953        preopens,
954        network_allow: policy.network_allow.clone(),
955        unix_sockets: policy.unix_sockets_allowed,
956        shell_allow: policy.shell_allow.clone(),
957        spawn_allow: policy.spawn_allow.clone(),
958        env_allow: policy.env_allow.clone(),
959        plan_submit: policy.plan_submit,
960        interpreter_runtime_grants: policy
961            .shell_interpreter_runtime
962            .iter()
963            .flat_map(|grant| {
964                grant.dirs.iter().map(move |dir| {
965                    let listable = if dir.list_dir { " (list_dir)" } else { "" };
966                    format!("{}: {}{}", grant.binary, dir.path, listable)
967                })
968            })
969            .collect(),
970        staged_runtime_grants: policy
971            .shell_staged_runtime
972            .iter()
973            .map(|grant| {
974                format!(
975                    "{}: {} (pin: {})",
976                    grant.binary, grant.source_path, grant.pin
977                )
978            })
979            .collect(),
980        // A capsule declaring `scoped` on a sealed-capable host composes no root and writes no
981        // `/tmp` store, so a declaration keyed on the bare host tier would name two directories
982        // that never appear. Nothing here touches the filesystem: `--explain-scope` returns before
983        // a workdir exists, and a diagnostic that created the directory it describes would be
984        // answering a question it had changed.
985        runtime_writes: crate::workdir_writes::runtime_writes(applied),
986        // Established by performing the `io.max` write, never inferred: `prepare_scope` fills it
987        // at launch and `cgroup::probe_io_max` fills it under `--explain-scope`. Like every field
988        // above it, it reaches no class and no floor.
989        io_max,
990        // Derived here, from the `applied` tier already resolved above, rather than filled by
991        // either caller: `--explain-scope --json` and `session_start.effective_grants` are the
992        // same object, and a field either of them worded for itself would make them differ.
993        filesystem_boundary: filesystem_boundary(applied),
994    }
995}
996
997/// Stable wire name for a tier, so `--explain-scope --json` consumers see the mechanism rather
998/// than a `Debug` rendering that is free to change.
999fn enforcement_tier_name(tier: EnforcementTier) -> &'static str {
1000    match tier {
1001        EnforcementTier::KernelSealed => "mountns+pivot_root+landlock+seccomp",
1002        EnforcementTier::KernelFull => "landlock+seccomp",
1003        EnforcementTier::KernelSeccompOnly => "seccomp-only",
1004        EnforcementTier::EnvironmentOnly => "none",
1005    }
1006}
1007
1008#[cfg(test)]
1009#[allow(clippy::print_stdout, clippy::print_stderr)]
1010mod tests {
1011    use super::*;
1012    use murmur_artifact::{InterpreterRuntimeDir, InterpreterRuntimeGrant, StagedRuntimeGrant};
1013
1014    /// Every tier, so a new variant cannot quietly escape a test that iterates "all of them".
1015    const ALL_TIERS: &[EnforcementTier] = &[
1016        EnforcementTier::KernelSealed,
1017        EnforcementTier::KernelFull,
1018        EnforcementTier::KernelSeccompOnly,
1019        EnforcementTier::EnvironmentOnly,
1020    ];
1021
1022    #[test]
1023    fn kernel_full_achieves_scoped() {
1024        assert_eq!(
1025            achieved_class_for_tier(EnforcementTier::KernelFull),
1026            ContainmentClass::Scoped
1027        );
1028    }
1029
1030    /// The sealed tier, and only the sealed tier, reports `sealed`.
1031    #[test]
1032    fn only_the_sealed_tier_achieves_sealed() {
1033        assert_eq!(
1034            achieved_class_for_tier(EnforcementTier::KernelSealed),
1035            ContainmentClass::Sealed
1036        );
1037        for tier in ALL_TIERS
1038            .iter()
1039            .filter(|t| **t != EnforcementTier::KernelSealed)
1040        {
1041            assert_ne!(achieved_class_for_tier(*tier), ContainmentClass::Sealed);
1042        }
1043    }
1044
1045    #[test]
1046    fn tiers_without_landlock_achieve_only_advisory() {
1047        assert_eq!(
1048            achieved_class_for_tier(EnforcementTier::KernelSeccompOnly),
1049            ContainmentClass::Advisory
1050        );
1051        assert_eq!(
1052            achieved_class_for_tier(EnforcementTier::EnvironmentOnly),
1053            ContainmentClass::Advisory
1054        );
1055    }
1056
1057    /// The whole tier × `workdir_exec` truth table, with both inputs supplied by the test and no
1058    /// kernel consulted — the seam `achieved_class_for_tier`/`achieved_containment_class` exists
1059    /// for. This is emphatically *not* a test of the security property (that a workdir binary
1060    /// cannot execute); it tests only the class arithmetic, which is all that can honestly be
1061    /// tested without a Landlock-capable host.
1062    #[test]
1063    fn workdir_exec_caps_the_achieved_class_at_advisory_on_every_tier() {
1064        // Without the declaration: the host ceiling, unchanged.
1065        assert_eq!(
1066            achieved_containment_class(EnforcementTier::KernelSealed, false),
1067            ContainmentClass::Sealed
1068        );
1069        assert_eq!(
1070            achieved_containment_class(EnforcementTier::KernelFull, false),
1071            ContainmentClass::Scoped
1072        );
1073        assert_eq!(
1074            achieved_containment_class(EnforcementTier::KernelSeccompOnly, false),
1075            ContainmentClass::Advisory
1076        );
1077        assert_eq!(
1078            achieved_containment_class(EnforcementTier::EnvironmentOnly, false),
1079            ContainmentClass::Advisory
1080        );
1081
1082        // With it: advisory, whatever the host could otherwise back.
1083        for tier in ALL_TIERS {
1084            assert_eq!(
1085                achieved_containment_class(*tier, true),
1086                ContainmentClass::Advisory,
1087                "workdir_exec must cap tier {tier:?} at advisory"
1088            );
1089        }
1090    }
1091
1092    /// The cap goes in the wrapper, never in the tier-only function — a regression here would mean
1093    /// a manifest key had leaked into the one mapping that must stay host-only.
1094    #[test]
1095    fn the_tier_only_mapping_is_unaffected_by_the_cap() {
1096        for tier in ALL_TIERS {
1097            assert_eq!(
1098                achieved_class_for_tier(*tier),
1099                achieved_containment_class(*tier, false),
1100                "the tier-only mapping and the uncapped wrapper must agree on tier {tier:?}"
1101            );
1102        }
1103    }
1104
1105    /// The refusal an operator actually hits: `workdir_exec: true` plus
1106    /// `capabilities.containment: scoped`, on a host that could otherwise back `scoped`. It must
1107    /// refuse, and the reason must send them to their manifest rather than to another machine.
1108    #[test]
1109    fn workdir_exec_refuses_a_scoped_floor_even_on_a_landlock_capable_host() {
1110        let achieved = achieved_containment_class(EnforcementTier::KernelFull, true);
1111        assert_eq!(achieved, ContainmentClass::Advisory);
1112
1113        let error = check_containment_floor(ContainmentClass::Scoped, achieved, None, true)
1114            .expect_err("workdir_exec + scoped must refuse");
1115        match error {
1116            RuntimeError::ContainmentFloorUnmet {
1117                declared,
1118                achieved,
1119                reason,
1120            } => {
1121                assert_eq!(declared, ContainmentClass::Scoped);
1122                assert_eq!(achieved, ContainmentClass::Advisory);
1123                assert!(
1124                    reason.contains("workdir_exec"),
1125                    "the reason must name the manifest key, got: {reason}"
1126                );
1127                assert!(
1128                    !reason.contains("this host provides no kernel filesystem mediation"),
1129                    "the reason must not blame the host for a manifest declaration: {reason}"
1130                );
1131            }
1132            other => panic!("expected ContainmentFloorUnmet, got {other:?}"),
1133        }
1134    }
1135
1136    /// `sealed` gets the same manifest-side reason rather than a `SealedBlocker` one: a probed
1137    /// blocker is real but is not what is stopping *this* capsule, and reporting it would send the
1138    /// operator to install an AppArmor profile that changes nothing.
1139    #[test]
1140    fn workdir_exec_outranks_a_probed_sealed_blocker_in_the_reason() {
1141        let reason = containment_shortfall_reason(
1142            ContainmentClass::Sealed,
1143            ContainmentClass::Advisory,
1144            Some(SealedBlocker::AppArmorProfileMissing),
1145            true,
1146        )
1147        .expect("refusal");
1148        assert!(reason.contains("workdir_exec"));
1149        assert!(!reason.contains("apparmor_parser"));
1150    }
1151
1152    /// The declaration is not itself a refusal: a capsule that declares `workdir_exec` and asks
1153    /// for nothing above `advisory` launches normally on every host.
1154    #[test]
1155    fn workdir_exec_alone_refuses_nothing() {
1156        for tier in ALL_TIERS {
1157            let achieved = achieved_containment_class(*tier, true);
1158            assert!(
1159                check_containment_floor(ContainmentClass::Advisory, achieved, None, true).is_ok(),
1160                "workdir_exec at an advisory floor must launch on tier {tier:?}"
1161            );
1162        }
1163    }
1164
1165    #[test]
1166    fn advisory_floor_is_met_on_every_tier() {
1167        for tier in ALL_TIERS {
1168            let achieved = achieved_class_for_tier(*tier);
1169            assert!(
1170                check_containment_floor(ContainmentClass::Advisory, achieved, None, false).is_ok()
1171            );
1172        }
1173    }
1174
1175    #[test]
1176    fn scoped_floor_is_met_on_every_landlock_capable_tier() {
1177        for tier in [EnforcementTier::KernelFull, EnforcementTier::KernelSealed] {
1178            let achieved = achieved_class_for_tier(tier);
1179            assert!(
1180                check_containment_floor(ContainmentClass::Scoped, achieved, None, false).is_ok()
1181            );
1182        }
1183
1184        for tier in [
1185            EnforcementTier::KernelSeccompOnly,
1186            EnforcementTier::EnvironmentOnly,
1187        ] {
1188            let achieved = achieved_class_for_tier(tier);
1189            let error = check_containment_floor(ContainmentClass::Scoped, achieved, None, false)
1190                .expect_err("scoped must refuse without Landlock");
1191            match error {
1192                RuntimeError::ContainmentFloorUnmet {
1193                    declared,
1194                    achieved,
1195                    reason,
1196                } => {
1197                    assert_eq!(declared, ContainmentClass::Scoped);
1198                    assert_eq!(achieved, ContainmentClass::Advisory);
1199                    assert!(
1200                        reason.contains("Landlock"),
1201                        "reason must name the missing mechanism, got: {reason}"
1202                    );
1203                }
1204                other => panic!("expected ContainmentFloorUnmet, got {other:?}"),
1205            }
1206        }
1207    }
1208
1209    /// The edge case the new tier makes possible: a capsule declaring the *weaker* class on a
1210    /// sealed-capable host. The floor is met (a stronger host satisfies a weaker requirement),
1211    /// and the applied mechanism is the declared one — asserted next door in
1212    /// `sandbox::applied_tier`'s tests, since a careless `match` here could force sealed-only
1213    /// behaviour onto a `scoped` declaration.
1214    #[test]
1215    fn a_weaker_declaration_is_satisfied_by_a_sealed_host_without_being_upgraded() {
1216        let achieved = achieved_class_for_tier(EnforcementTier::KernelSealed);
1217        assert_eq!(achieved, ContainmentClass::Sealed);
1218        assert!(check_containment_floor(ContainmentClass::Advisory, achieved, None, false).is_ok());
1219        assert!(check_containment_floor(ContainmentClass::Scoped, achieved, None, false).is_ok());
1220        assert_eq!(
1221            containment_shortfall_reason(ContainmentClass::Scoped, achieved, None, false),
1222            None
1223        );
1224    }
1225
1226    #[test]
1227    fn sealed_floor_is_met_only_on_the_sealed_tier() {
1228        assert!(check_containment_floor(
1229            ContainmentClass::Sealed,
1230            achieved_class_for_tier(EnforcementTier::KernelSealed),
1231            None,
1232            false
1233        )
1234        .is_ok());
1235
1236        for tier in ALL_TIERS
1237            .iter()
1238            .filter(|t| **t != EnforcementTier::KernelSealed)
1239        {
1240            let achieved = achieved_class_for_tier(*tier);
1241            let error = check_containment_floor(
1242                ContainmentClass::Sealed,
1243                achieved,
1244                Some(SealedBlocker::NamespaceCreationDenied),
1245                false,
1246            )
1247            .expect_err("sealed must refuse on a host that cannot back it");
1248            match error {
1249                RuntimeError::ContainmentFloorUnmet {
1250                    declared, reason, ..
1251                } => {
1252                    assert_eq!(declared, ContainmentClass::Sealed);
1253                    assert!(
1254                        reason.contains("--cap-add SYS_ADMIN"),
1255                        "reason must name the blocking mechanism's remediation, got: {reason}"
1256                    );
1257                }
1258                other => panic!("expected ContainmentFloorUnmet, got {other:?}"),
1259            }
1260        }
1261    }
1262
1263    /// The refusal text is mechanism-specific, not one fixed sentence: the same
1264    /// declared/achieved pair produces different, individually actionable reasons.
1265    #[test]
1266    fn the_sealed_refusal_names_the_specific_blocker() {
1267        let achieved = ContainmentClass::Scoped;
1268
1269        let apparmor = containment_shortfall_reason(
1270            ContainmentClass::Sealed,
1271            achieved,
1272            Some(SealedBlocker::AppArmorProfileMissing),
1273            false,
1274        )
1275        .expect("refusal");
1276        assert!(apparmor.contains("mur-sealed"));
1277        assert!(apparmor.contains("apparmor_parser -r"));
1278        assert!(!apparmor.contains("--cap-add"));
1279
1280        let container = containment_shortfall_reason(
1281            ContainmentClass::Sealed,
1282            achieved,
1283            Some(SealedBlocker::NamespaceCreationDenied),
1284            false,
1285        )
1286        .expect("refusal");
1287        assert!(container.contains("--cap-add SYS_ADMIN"));
1288        assert!(container.contains("outside the container"));
1289        assert!(!container.contains("apparmor_parser"));
1290
1291        // A host that mounts and will not pivot is refused at launch with its own text, rather
1292        // than at the first subprocess with `E-RUN-014`.
1293        let pivot = containment_shortfall_reason(
1294            ContainmentClass::Sealed,
1295            ContainmentClass::Advisory,
1296            Some(SealedBlocker::PivotRootDenied),
1297            false,
1298        )
1299        .expect("refusal");
1300        assert!(pivot.contains("pivot_root(2)"), "reason was: {pivot}");
1301        assert_ne!(
1302            pivot,
1303            containment_shortfall_reason(
1304                ContainmentClass::Sealed,
1305                ContainmentClass::Advisory,
1306                Some(SealedBlocker::MountDenied),
1307                false,
1308            )
1309            .expect("refusal")
1310        );
1311
1312        // Never the pre-mechanism blanket text again, under any blocker.
1313        for blocker in [
1314            SealedBlocker::NotLinux,
1315            SealedBlocker::AppArmorProfileMissing,
1316            SealedBlocker::NamespaceCreationDenied,
1317            SealedBlocker::MountDenied,
1318            SealedBlocker::PivotRootDenied,
1319            SealedBlocker::KernelUnsupported,
1320            SealedBlocker::LandlockUnavailable,
1321        ] {
1322            let reason = containment_shortfall_reason(
1323                ContainmentClass::Sealed,
1324                achieved,
1325                Some(blocker),
1326                false,
1327            )
1328            .expect("refusal");
1329            assert!(
1330                !reason.contains("no host can provide it today"),
1331                "got: {reason}"
1332            );
1333        }
1334    }
1335
1336    /// A caller with no probe result still gets a reason naming the mechanism rather than an
1337    /// empty or misleading one.
1338    #[test]
1339    fn the_sealed_refusal_falls_back_to_naming_the_mechanism_without_a_probe() {
1340        let reason = containment_shortfall_reason(
1341            ContainmentClass::Sealed,
1342            ContainmentClass::Advisory,
1343            None,
1344            false,
1345        )
1346        .expect("refusal");
1347        assert!(reason.contains("pivot_root"));
1348        assert!(reason.contains("CLONE_NEWNS"));
1349    }
1350
1351    #[test]
1352    fn shortfall_reason_is_absent_when_the_floor_is_met() {
1353        assert_eq!(
1354            containment_shortfall_reason(
1355                ContainmentClass::Advisory,
1356                ContainmentClass::Scoped,
1357                None,
1358                false
1359            ),
1360            None
1361        );
1362        assert_eq!(
1363            containment_shortfall_reason(
1364                ContainmentClass::Scoped,
1365                ContainmentClass::Scoped,
1366                None,
1367                false
1368            ),
1369            None
1370        );
1371        assert_eq!(
1372            containment_shortfall_reason(
1373                ContainmentClass::Sealed,
1374                ContainmentClass::Sealed,
1375                Some(SealedBlocker::NamespaceCreationDenied),
1376                false
1377            ),
1378            None,
1379            "a met floor must ignore a stale blocker rather than manufacture a refusal"
1380        );
1381    }
1382
1383    fn sample_policy() -> CapabilityPolicy {
1384        CapabilityPolicy {
1385            network_allow: vec!["https://api.example.com".to_string()],
1386            unix_sockets_allowed: false,
1387            filesystem_scope: Some("workdir".to_string()),
1388            shell_allow: vec!["python3".to_string()],
1389            spawn_allow: vec!["helper".to_string()],
1390            env_allow: vec!["TZ".to_string()],
1391            shell_interpreter_runtime: vec![InterpreterRuntimeGrant {
1392                binary: "python3".to_string(),
1393                dirs: vec![InterpreterRuntimeDir {
1394                    path: "/usr/lib/python3.11".to_string(),
1395                    list_dir: true,
1396                }],
1397            }],
1398            ..CapabilityPolicy::default()
1399        }
1400    }
1401
1402    #[test]
1403    fn scope_report_mirrors_the_policy_and_the_probed_tier() {
1404        let report = scope_report_for_tier(
1405            &sample_policy(),
1406            ContainmentClass::Scoped,
1407            EnforcementTier::KernelFull,
1408            None,
1409            None,
1410            None,
1411            Vec::new(),
1412            Vec::new(),
1413            Vec::new(),
1414            IoMaxReport::default(),
1415        );
1416
1417        assert_eq!(report.declared_containment, ContainmentClass::Scoped);
1418        assert_eq!(report.achieved_containment, ContainmentClass::Scoped);
1419        assert!(report.floor_met);
1420        assert_eq!(report.shortfall_reason, None);
1421        assert_eq!(report.enforcement_tier, "landlock+seccomp");
1422        assert_eq!(report.filesystem_scope.as_deref(), Some("workdir"));
1423        assert_eq!(report.network_allow, vec!["https://api.example.com"]);
1424        assert!(!report.unix_sockets);
1425        assert_eq!(report.shell_allow, vec!["python3"]);
1426        assert_eq!(report.spawn_allow, vec!["helper"]);
1427        assert_eq!(report.env_allow, vec!["TZ"]);
1428        assert!(!report.workdir_exec);
1429        assert_eq!(
1430            report.interpreter_runtime_grants,
1431            vec!["python3: /usr/lib/python3.11 (list_dir)"]
1432        );
1433    }
1434
1435    /// `--explain-scope` is where an operator finds out *why* a Landlock-capable host is reporting
1436    /// `advisory`, so the declaration and the class it produced have to appear together — in the
1437    /// struct, in the JSON, and in the rendered text.
1438    #[test]
1439    fn scope_report_surfaces_workdir_exec_and_the_advisory_it_forces() {
1440        let policy = CapabilityPolicy {
1441            shell_allow: vec!["gcc".to_string()],
1442            workdir_exec_allowed: true,
1443            ..CapabilityPolicy::default()
1444        };
1445
1446        let report = scope_report_for_tier(
1447            &policy,
1448            ContainmentClass::Scoped,
1449            EnforcementTier::KernelFull,
1450            None,
1451            None,
1452            None,
1453            Vec::new(),
1454            Vec::new(),
1455            Vec::new(),
1456            IoMaxReport::default(),
1457        );
1458
1459        assert!(report.workdir_exec);
1460        assert_eq!(report.achieved_containment, ContainmentClass::Advisory);
1461        assert!(!report.floor_met);
1462        // The host is fully capable; the mechanism line must keep saying so, so the report does
1463        // not read as a broken host.
1464        assert_eq!(report.enforcement_tier, "landlock+seccomp");
1465        assert!(report
1466            .shortfall_reason
1467            .as_deref()
1468            .expect("refusal")
1469            .contains("workdir_exec"));
1470
1471        let value: serde_json::Value = serde_json::to_value(&report).unwrap();
1472        assert_eq!(value["workdir_exec"], true);
1473
1474        let rendered = report.render();
1475        assert!(rendered.contains("workdir exec:     true"));
1476        assert!(rendered.contains("achieved:  advisory"));
1477    }
1478
1479    /// The key is always written, on the same terms `workdir_exec` and `state_stores` are: an
1480    /// absent `io_max` identifies a runtime that predates the field, not a host that was not
1481    /// asked. "Not asked" is a value — `not-probed` — inside the object.
1482    #[test]
1483    fn scope_report_json_always_carries_io_max() {
1484        let unprobed: serde_json::Value = serde_json::to_value(scope_report_for_tier(
1485            &sample_policy(),
1486            ContainmentClass::Scoped,
1487            EnforcementTier::KernelFull,
1488            None,
1489            None,
1490            None,
1491            Vec::new(),
1492            Vec::new(),
1493            Vec::new(),
1494            IoMaxReport::default(),
1495        ))
1496        .unwrap();
1497        assert_eq!(unprobed["io_max"]["status"], "not-probed");
1498        assert_eq!(unprobed["io_max"]["declared_bytes_per_sec"], 0);
1499        assert!(unprobed["io_max"].get("reason").is_none());
1500
1501        let enforced: serde_json::Value = serde_json::to_value(scope_report_for_tier(
1502            &sample_policy(),
1503            ContainmentClass::Scoped,
1504            EnforcementTier::KernelFull,
1505            None,
1506            None,
1507            None,
1508            Vec::new(),
1509            Vec::new(),
1510            Vec::new(),
1511            IoMaxReport {
1512                declared_bytes_per_sec: 52_428_800,
1513                status: IoMaxStatus::Enforced,
1514                reason: None,
1515            },
1516        ))
1517        .unwrap();
1518        assert_eq!(enforced["io_max"]["status"], "enforced");
1519        assert_eq!(enforced["io_max"]["declared_bytes_per_sec"], 52_428_800);
1520    }
1521
1522    /// A ceiling that did not apply has to be visible in the rendering an operator reads, not
1523    /// only in the JSON: this report is where the claim of an I/O bound would otherwise stand
1524    /// uncontradicted.
1525    #[test]
1526    fn the_rendered_resource_plane_states_what_became_of_the_io_ceiling() {
1527        let rendered = scope_report_for_tier(
1528            &sample_policy(),
1529            ContainmentClass::Scoped,
1530            EnforcementTier::KernelFull,
1531            None,
1532            None,
1533            None,
1534            Vec::new(),
1535            Vec::new(),
1536            Vec::new(),
1537            IoMaxReport {
1538                declared_bytes_per_sec: 52_428_800,
1539                status: IoMaxStatus::Unavailable,
1540                reason: Some("the io controller is not delegated".to_string()),
1541            },
1542        )
1543        .render();
1544
1545        assert!(rendered.contains("Resource plane"), "{rendered}");
1546        assert!(rendered.contains("io.max:"), "{rendered}");
1547        assert!(rendered.contains("unavailable"), "{rendered}");
1548        assert!(rendered.contains("52428800 bytes/s declared"), "{rendered}");
1549        assert!(
1550            rendered.contains("the io controller is not delegated"),
1551            "{rendered}"
1552        );
1553    }
1554
1555    /// The field is always written, including `false`, so a consumer can distinguish "this capsule
1556    /// declared nothing" from "this runtime does not know the key".
1557    #[test]
1558    fn scope_report_json_always_carries_workdir_exec() {
1559        let value: serde_json::Value = serde_json::to_value(scope_report_for_tier(
1560            &sample_policy(),
1561            ContainmentClass::Scoped,
1562            EnforcementTier::KernelFull,
1563            None,
1564            None,
1565            None,
1566            Vec::new(),
1567            Vec::new(),
1568            Vec::new(),
1569            IoMaxReport::default(),
1570        ))
1571        .unwrap();
1572        assert_eq!(value["workdir_exec"], false);
1573    }
1574
1575    /// `--explain-scope` is a diagnostic, not a launch: a capsule declaring `staged_runtime` on a
1576    /// host that cannot back `sealed` is exactly the case an operator inspects with it, so the
1577    /// grant must be reported on every tier — including the ones where `mur run` would refuse.
1578    #[test]
1579    fn staged_runtime_grants_are_reported_on_every_tier() {
1580        let policy = CapabilityPolicy {
1581            shell_allow: vec!["python3".to_string()],
1582            shell_staged_runtime: vec![StagedRuntimeGrant {
1583                binary: "python3".to_string(),
1584                source_path: "/opt/testbed/conda/envs/django__django".to_string(),
1585                pin: "conda-4.10.3/python-3.9.19".to_string(),
1586            }],
1587            ..CapabilityPolicy::default()
1588        };
1589
1590        for tier in ALL_TIERS {
1591            let report = scope_report_for_tier(
1592                &policy,
1593                ContainmentClass::Sealed,
1594                *tier,
1595                Some(SealedBlocker::NamespaceCreationDenied),
1596                None,
1597                None,
1598                Vec::new(),
1599                Vec::new(),
1600                Vec::new(),
1601                IoMaxReport::default(),
1602            );
1603            assert_eq!(
1604                report.staged_runtime_grants,
1605                vec![
1606                    "python3: /opt/testbed/conda/envs/django__django \
1607                     (pin: conda-4.10.3/python-3.9.19)"
1608                ],
1609                "tier {tier:?} must still report the declared staged runtime"
1610            );
1611            assert!(
1612                report.render().contains("staged runtime"),
1613                "the rendered report must carry a staged runtime section on tier {tier:?}"
1614            );
1615        }
1616    }
1617
1618    /// The absent case: no `staged_runtime` in the manifest leaves the section empty rather than
1619    /// inventing one, and `render` still names it as `<none>` like every other empty list.
1620    #[test]
1621    fn absent_staged_runtime_reports_nothing() {
1622        let report = scope_report_for_tier(
1623            &sample_policy(),
1624            ContainmentClass::Scoped,
1625            EnforcementTier::KernelFull,
1626            None,
1627            None,
1628            None,
1629            Vec::new(),
1630            Vec::new(),
1631            Vec::new(),
1632            IoMaxReport::default(),
1633        );
1634        assert!(report.staged_runtime_grants.is_empty());
1635        assert!(report.render().contains("staged runtime: <none>"));
1636    }
1637
1638    /// A capsule that declares `scoped` *and* opens host paths through `interpreter_runtime`
1639    /// still achieves only `scoped` — the grant set is reported, never consulted to upgrade
1640    /// the achieved class.
1641    #[test]
1642    fn grant_set_never_raises_the_achieved_class() {
1643        let report = scope_report_for_tier(
1644            &sample_policy(),
1645            ContainmentClass::Sealed,
1646            EnforcementTier::KernelFull,
1647            Some(SealedBlocker::NamespaceCreationDenied),
1648            None,
1649            None,
1650            Vec::new(),
1651            Vec::new(),
1652            Vec::new(),
1653            IoMaxReport::default(),
1654        );
1655        assert_eq!(report.achieved_containment, ContainmentClass::Scoped);
1656        assert!(!report.floor_met);
1657    }
1658
1659    #[test]
1660    fn scope_report_still_reports_when_the_floor_is_unmet() {
1661        let report = scope_report_for_tier(
1662            &sample_policy(),
1663            ContainmentClass::Sealed,
1664            EnforcementTier::EnvironmentOnly,
1665            Some(SealedBlocker::NotLinux),
1666            None,
1667            None,
1668            Vec::new(),
1669            Vec::new(),
1670            Vec::new(),
1671            IoMaxReport::default(),
1672        );
1673
1674        assert_eq!(report.declared_containment, ContainmentClass::Sealed);
1675        assert_eq!(report.achieved_containment, ContainmentClass::Advisory);
1676        assert!(!report.floor_met);
1677        assert!(report.shortfall_reason.unwrap().contains("non-Linux"));
1678        assert_eq!(report.enforcement_tier, "none");
1679    }
1680
1681    #[test]
1682    fn scope_report_names_the_sealed_mechanism_on_a_sealed_host() {
1683        let report = scope_report_for_tier(
1684            &sample_policy(),
1685            ContainmentClass::Sealed,
1686            EnforcementTier::KernelSealed,
1687            None,
1688            None,
1689            None,
1690            Vec::new(),
1691            Vec::new(),
1692            Vec::new(),
1693            IoMaxReport::default(),
1694        );
1695        assert_eq!(report.achieved_containment, ContainmentClass::Sealed);
1696        assert!(report.floor_met);
1697        assert_eq!(
1698            report.enforcement_tier,
1699            "mountns+pivot_root+landlock+seccomp"
1700        );
1701    }
1702
1703    #[test]
1704    fn scope_report_json_uses_wire_names() {
1705        let report = scope_report_for_tier(
1706            &CapabilityPolicy::default(),
1707            ContainmentClass::Advisory,
1708            EnforcementTier::KernelSeccompOnly,
1709            None,
1710            None,
1711            None,
1712            Vec::new(),
1713            Vec::new(),
1714            Vec::new(),
1715            IoMaxReport::default(),
1716        );
1717        let value: serde_json::Value = serde_json::to_value(&report).unwrap();
1718
1719        assert_eq!(value["declared_containment"], "advisory");
1720        assert_eq!(value["achieved_containment"], "advisory");
1721        assert_eq!(value["floor_met"], true);
1722        assert_eq!(value["enforcement_tier"], "seccomp-only");
1723        // Absent rather than null when the floor is met.
1724        assert!(value.get("shortfall_reason").is_none());
1725    }
1726
1727    /// The grant is reported, and it never moves a class, a floor or a mechanism: the same tier
1728    /// with four different grants produces four reports that differ in exactly one field.
1729    #[test]
1730    fn the_userns_grant_is_reported_without_changing_any_verdict() {
1731        let baseline = scope_report_for_tier(
1732            &sample_policy(),
1733            ContainmentClass::Advisory,
1734            EnforcementTier::KernelSealed,
1735            None,
1736            None,
1737            None,
1738            Vec::new(),
1739            Vec::new(),
1740            Vec::new(),
1741            IoMaxReport::default(),
1742        );
1743
1744        for grant in UsernsGrant::ALL {
1745            let report = scope_report_for_tier(
1746                &sample_policy(),
1747                ContainmentClass::Advisory,
1748                EnforcementTier::KernelSealed,
1749                None,
1750                Some(*grant),
1751                None,
1752                Vec::new(),
1753                Vec::new(),
1754                Vec::new(),
1755                IoMaxReport::default(),
1756            );
1757            assert_eq!(report.achieved_containment, baseline.achieved_containment);
1758            assert_eq!(report.floor_met, baseline.floor_met);
1759            assert_eq!(report.enforcement_tier, baseline.enforcement_tier);
1760            assert_eq!(report.userns_grant, Some(*grant));
1761
1762            let value: serde_json::Value = serde_json::to_value(&report).unwrap();
1763            assert_eq!(value["userns_grant"], grant.wire_name());
1764            assert!(report
1765                .render()
1766                .contains(&format!("userns grant: {}", grant.wire_name())));
1767        }
1768
1769        // Off Linux the key is present and null — AppArmor does not exist there, which is not the
1770        // same statement as "the grant was withheld".
1771        let value: serde_json::Value = serde_json::to_value(&baseline).unwrap();
1772        assert!(value["userns_grant"].is_null());
1773        assert!(baseline.render().contains("userns grant: n/a"));
1774    }
1775
1776    #[test]
1777    fn rendered_report_names_both_classes_and_the_refusal() {
1778        let rendered = scope_report_for_tier(
1779            &sample_policy(),
1780            ContainmentClass::Sealed,
1781            EnforcementTier::KernelFull,
1782            Some(SealedBlocker::AppArmorProfileMissing),
1783            None,
1784            None,
1785            Vec::new(),
1786            Vec::new(),
1787            Vec::new(),
1788            IoMaxReport::default(),
1789        )
1790        .render();
1791
1792        assert!(rendered.contains("declared:  sealed"));
1793        assert!(rendered.contains("achieved:  scoped"));
1794        assert!(rendered.contains("floor met: no"));
1795        assert!(rendered.contains("mur-sealed"));
1796        assert!(rendered.contains("would refuse to launch"));
1797    }
1798
1799    /// The invariant the whole design rests on: an export is a disclosure, not a grant. Declaring
1800    /// one must leave the achieved class — and everything else the report says about what the
1801    /// guest can reach — byte-identical.
1802    #[test]
1803    fn an_export_never_changes_what_the_report_says_about_containment() {
1804        let export = FileExport {
1805            root: "out/".to_string(),
1806            mode: ExportMode::ReadOnly,
1807            max_bytes: 10 * 1024 * 1024,
1808        };
1809        for tier in ALL_TIERS {
1810            for workdir_exec in [false, true] {
1811                let policy = CapabilityPolicy {
1812                    workdir_exec_allowed: workdir_exec,
1813                    ..sample_policy()
1814                };
1815                let without = scope_report_for_tier(
1816                    &policy,
1817                    ContainmentClass::Scoped,
1818                    *tier,
1819                    None,
1820                    None,
1821                    None,
1822                    Vec::new(),
1823                    Vec::new(),
1824                    Vec::new(),
1825                    IoMaxReport::default(),
1826                );
1827                let with = scope_report_for_tier(
1828                    &policy,
1829                    ContainmentClass::Scoped,
1830                    *tier,
1831                    None,
1832                    None,
1833                    Some(&Exports {
1834                        files: Some(export.clone()),
1835                        peer_files: None,
1836                    }),
1837                    Vec::new(),
1838                    Vec::new(),
1839                    Vec::new(),
1840                    IoMaxReport::default(),
1841                );
1842                assert_eq!(
1843                    with.achieved_containment, without.achieved_containment,
1844                    "tier {tier:?}, workdir_exec {workdir_exec}"
1845                );
1846                assert_eq!(with.enforcement_tier, without.enforcement_tier);
1847                assert_eq!(with.floor_met, without.floor_met);
1848                assert_eq!(with.shortfall_reason, without.shortfall_reason);
1849                // Everything but the one new field is identical, which is the strongest form of
1850                // "an export changes nothing else" this report can state.
1851                assert_eq!(
1852                    ScopeReport {
1853                        exports_files: None,
1854                        ..with.clone()
1855                    },
1856                    without
1857                );
1858                assert_eq!(
1859                    with.exports_files,
1860                    Some(ExportsFilesReport {
1861                        root: "out/".to_string(),
1862                        mode: ExportMode::ReadOnly,
1863                        max_bytes: 10 * 1024 * 1024,
1864                    })
1865                );
1866            }
1867        }
1868    }
1869
1870    /// The same invariant for the peer-handoff grants, and stated separately because they are a
1871    /// separate authoriser: declaring either half must leave the achieved class, the tier and the
1872    /// floor verdict byte-identical, and must change no field of the report but its own.
1873    #[test]
1874    fn peer_handoff_never_changes_what_the_report_says_about_containment() {
1875        let peer_files = PeerFilesExport {
1876            root: "out/".to_string(),
1877            max_ttl_secs: Some(900),
1878            max_bytes: 10 * 1024 * 1024,
1879        };
1880        for tier in ALL_TIERS {
1881            for workdir_exec in [false, true] {
1882                let without_policy = CapabilityPolicy {
1883                    workdir_exec_allowed: workdir_exec,
1884                    ..sample_policy()
1885                };
1886                let with_policy = CapabilityPolicy {
1887                    peer_fetch_allow: vec!["localhost:41234".to_string()],
1888                    ..without_policy.clone()
1889                };
1890                let without = scope_report_for_tier(
1891                    &without_policy,
1892                    ContainmentClass::Scoped,
1893                    *tier,
1894                    None,
1895                    None,
1896                    None,
1897                    Vec::new(),
1898                    Vec::new(),
1899                    Vec::new(),
1900                    IoMaxReport::default(),
1901                );
1902                let with = scope_report_for_tier(
1903                    &with_policy,
1904                    ContainmentClass::Scoped,
1905                    *tier,
1906                    None,
1907                    None,
1908                    Some(&Exports {
1909                        files: None,
1910                        peer_files: Some(peer_files.clone()),
1911                    }),
1912                    Vec::new(),
1913                    Vec::new(),
1914                    Vec::new(),
1915                    IoMaxReport::default(),
1916                );
1917                assert_eq!(
1918                    with.achieved_containment, without.achieved_containment,
1919                    "tier {tier:?}, workdir_exec {workdir_exec}"
1920                );
1921                assert_eq!(with.enforcement_tier, without.enforcement_tier);
1922                assert_eq!(with.floor_met, without.floor_met);
1923                assert_eq!(with.shortfall_reason, without.shortfall_reason);
1924                assert_eq!(
1925                    ScopeReport {
1926                        peer_files: None,
1927                        peer_fetch_allow: Vec::new(),
1928                        ..with.clone()
1929                    },
1930                    without
1931                );
1932                assert_eq!(
1933                    with.peer_files,
1934                    Some(PeerFilesReport {
1935                        root: "out/".to_string(),
1936                        max_ttl_secs: 900,
1937                        max_bytes: 10 * 1024 * 1024,
1938                    })
1939                );
1940                assert_eq!(with.peer_fetch_allow, vec!["localhost:41234".to_string()]);
1941                // Declaring `peer_fetch` does not widen `network.allow`, on either side of the
1942                // report: the two lists are carried separately and neither is derived from the
1943                // other.
1944                assert_eq!(with.network_allow, without.network_allow);
1945            }
1946        }
1947    }
1948
1949    /// Both peer fields are written whether or not they were declared, on the same terms as
1950    /// `exports_files`: an absent key identifies an older runtime, a `null`/`[]` identifies a
1951    /// capsule that declined.
1952    #[test]
1953    fn the_peer_fields_are_always_serialized() {
1954        let undeclared: serde_json::Value = serde_json::to_value(scope_report_for_tier(
1955            &sample_policy(),
1956            ContainmentClass::Advisory,
1957            EnforcementTier::KernelSealed,
1958            None,
1959            None,
1960            None,
1961            Vec::new(),
1962            Vec::new(),
1963            Vec::new(),
1964            IoMaxReport::default(),
1965        ))
1966        .unwrap();
1967        assert_eq!(undeclared["peer_files"], serde_json::Value::Null);
1968        assert_eq!(undeclared["peer_fetch_allow"], serde_json::json!([]));
1969
1970        let declared: serde_json::Value = serde_json::to_value(scope_report_for_tier(
1971            &CapabilityPolicy {
1972                peer_fetch_allow: vec!["localhost:41234".to_string()],
1973                ..sample_policy()
1974            },
1975            ContainmentClass::Advisory,
1976            EnforcementTier::KernelSealed,
1977            None,
1978            None,
1979            Some(&Exports {
1980                files: None,
1981                peer_files: Some(PeerFilesExport {
1982                    root: "out/".to_string(),
1983                    max_ttl_secs: None,
1984                    max_bytes: 10 * 1024 * 1024,
1985                }),
1986            }),
1987            Vec::new(),
1988            Vec::new(),
1989            Vec::new(),
1990            IoMaxReport::default(),
1991        ))
1992        .unwrap();
1993        assert_eq!(
1994            declared["peer_files"],
1995            serde_json::json!({"root": "out/", "max_ttl_secs": 3600, "max_bytes": 10_485_760})
1996        );
1997        assert_eq!(
1998            declared["peer_fetch_allow"],
1999            serde_json::json!(["localhost:41234"])
2000        );
2001    }
2002
2003    #[test]
2004    fn render_names_peer_handoff_in_both_directions() {
2005        let undeclared = scope_report_for_tier(
2006            &sample_policy(),
2007            ContainmentClass::Advisory,
2008            EnforcementTier::KernelSealed,
2009            None,
2010            None,
2011            None,
2012            Vec::new(),
2013            Vec::new(),
2014            Vec::new(),
2015            IoMaxReport::default(),
2016        )
2017        .render();
2018        assert!(undeclared.contains("Peer handoff"));
2019        assert!(undeclared.contains("exports.peer_files: <none>"));
2020        assert!(undeclared.contains("peer_fetch allow: <none>"));
2021
2022        let declared = scope_report_for_tier(
2023            &CapabilityPolicy {
2024                peer_fetch_allow: vec!["localhost:41234".to_string()],
2025                ..sample_policy()
2026            },
2027            ContainmentClass::Advisory,
2028            EnforcementTier::KernelSealed,
2029            None,
2030            None,
2031            Some(&Exports {
2032                files: None,
2033                peer_files: Some(PeerFilesExport {
2034                    root: "out/".to_string(),
2035                    max_ttl_secs: Some(900),
2036                    max_bytes: 4096,
2037                }),
2038            }),
2039            Vec::new(),
2040            Vec::new(),
2041            Vec::new(),
2042            IoMaxReport::default(),
2043        )
2044        .render();
2045        assert!(declared.contains("exports.peer_files root: out/"));
2046        assert!(declared.contains("max handle ttl:          900s"));
2047        assert!(declared.contains("max bytes:               4096 (per file)"));
2048        assert!(declared.contains("- localhost:41234"));
2049    }
2050
2051    /// A declared state store is reported, and moves nothing else. Same discipline the export
2052    /// reports are held to: it is copied through, never consulted, so a durable directory grant
2053    /// cannot talk a host into reporting a class it does not back.
2054    #[test]
2055    fn a_state_store_is_reported_and_moves_no_other_field() {
2056        let policy = sample_policy();
2057        let stores = vec![StateStoreReport {
2058            artifact: "murmur-tool-corpus".to_string(),
2059            store: "shey".to_string(),
2060            host_path: "/home/dev/.murmur/state/shey".to_string(),
2061        }];
2062
2063        let without = scope_report_for_tier(
2064            &policy,
2065            ContainmentClass::Scoped,
2066            EnforcementTier::KernelFull,
2067            None,
2068            None,
2069            None,
2070            Vec::new(),
2071            Vec::new(),
2072            Vec::new(),
2073            IoMaxReport::default(),
2074        );
2075        let with = scope_report_for_tier(
2076            &policy,
2077            ContainmentClass::Scoped,
2078            EnforcementTier::KernelFull,
2079            None,
2080            None,
2081            None,
2082            stores.clone(),
2083            Vec::new(),
2084            Vec::new(),
2085            IoMaxReport::default(),
2086        );
2087
2088        assert_eq!(with.state_stores, stores);
2089        assert!(without.state_stores.is_empty());
2090        assert_eq!(with.achieved_containment, without.achieved_containment);
2091        assert_eq!(with.floor_met, without.floor_met);
2092        assert_eq!(with.enforcement_tier, without.enforcement_tier);
2093        assert_eq!(with.shortfall_reason, without.shortfall_reason);
2094        // The only field that moved is its own.
2095        assert_eq!(
2096            ScopeReport {
2097                state_stores: Vec::new(),
2098                ..with.clone()
2099            },
2100            without
2101        );
2102
2103        // Rendered under the grants, one line per declaring artifact; `<none>` when empty.
2104        assert!(with
2105            .render()
2106            .contains("- murmur-tool-corpus: shey -> /home/dev/.murmur/state/shey"));
2107        assert!(without.render().contains("state stores: <none>"));
2108
2109        // Always an array in JSON, never skipped: an absent key means an older runtime, not a
2110        // capsule that declined a store.
2111        let json = serde_json::to_value(&without).unwrap();
2112        assert_eq!(json["state_stores"], serde_json::json!([]));
2113        assert_eq!(
2114            serde_json::to_value(&with).unwrap()["state_stores"],
2115            serde_json::json!([{
2116                "artifact": "murmur-tool-corpus",
2117                "store": "shey",
2118                "host_path": "/home/dev/.murmur/state/shey",
2119            }])
2120        );
2121    }
2122
2123    /// The preopen set is reported and moves nothing else, on the same terms the state-store
2124    /// report beside it is held to. It is also the field that answers what `filesystem_scope`
2125    /// does not: a capsule-wide scope is joined into no guest preopen root, so both can be
2126    /// present and disagree without either being wrong.
2127    #[test]
2128    fn preopens_are_reported_and_move_no_other_field() {
2129        let policy = sample_policy();
2130        let preopens = vec![
2131            PreopenReport {
2132                artifact: "notes-tool".to_string(),
2133                role: "tool".to_string(),
2134                scope: None,
2135                surface: PreopenSurface::WholeWorkdir,
2136            },
2137            PreopenReport {
2138                artifact: "cache-tool".to_string(),
2139                role: "tool".to_string(),
2140                scope: Some("cache".to_string()),
2141                surface: PreopenSurface::ScopedSubtree,
2142            },
2143            PreopenReport {
2144                artifact: "telemetry-hook".to_string(),
2145                role: "hook".to_string(),
2146                scope: None,
2147                surface: PreopenSurface::Nothing,
2148            },
2149        ];
2150
2151        let without = scope_report_for_tier(
2152            &policy,
2153            ContainmentClass::Scoped,
2154            EnforcementTier::KernelFull,
2155            None,
2156            None,
2157            None,
2158            Vec::new(),
2159            Vec::new(),
2160            Vec::new(),
2161            IoMaxReport::default(),
2162        );
2163        let with = scope_report_for_tier(
2164            &policy,
2165            ContainmentClass::Scoped,
2166            EnforcementTier::KernelFull,
2167            None,
2168            None,
2169            None,
2170            Vec::new(),
2171            Vec::new(),
2172            preopens.clone(),
2173            IoMaxReport::default(),
2174        );
2175
2176        assert_eq!(with.preopens, preopens);
2177        assert!(without.preopens.is_empty());
2178        // The only field that moved is its own.
2179        assert_eq!(
2180            ScopeReport {
2181                preopens: Vec::new(),
2182                ..with.clone()
2183            },
2184            without
2185        );
2186
2187        // Three visibly different lines under `Effective grants`; `<none>` when empty.
2188        let rendered = with.render();
2189        assert!(rendered.contains(
2190            "- notes-tool (tool): the whole accessible workdir — no capabilities.filesystem.scope \
2191             declared"
2192        ));
2193        assert!(rendered.contains(
2194            "- cache-tool (tool): one subtree of the accessible workdir — \
2195             capabilities.filesystem.scope: cache"
2196        ));
2197        assert!(rendered.contains(
2198            "- telemetry-hook (hook): nothing preopened — no capabilities.filesystem.scope declared"
2199        ));
2200        assert!(without.render().contains("preopens: <none>"));
2201
2202        // Always an array in JSON, never skipped, with an undeclared scope as `null`.
2203        assert_eq!(
2204            serde_json::to_value(&without).unwrap()["preopens"],
2205            serde_json::json!([])
2206        );
2207        assert_eq!(
2208            serde_json::to_value(&with).unwrap()["preopens"],
2209            serde_json::json!([
2210                {
2211                    "artifact": "notes-tool",
2212                    "role": "tool",
2213                    "scope": null,
2214                    "surface": "whole-workdir",
2215                },
2216                {
2217                    "artifact": "cache-tool",
2218                    "role": "tool",
2219                    "scope": "cache",
2220                    "surface": "scoped-subtree",
2221                },
2222                {
2223                    "artifact": "telemetry-hook",
2224                    "role": "hook",
2225                    "scope": null,
2226                    "surface": "nothing",
2227                },
2228            ])
2229        );
2230    }
2231
2232    /// `exports_files` is written whether or not it was declared, on the same terms as
2233    /// `workdir_exec`: an absent key identifies an older runtime, a `null` a capsule that
2234    /// exported nothing.
2235    #[test]
2236    fn exports_files_is_always_serialized() {
2237        let undeclared = serde_json::to_value(scope_report_for_tier(
2238            &sample_policy(),
2239            ContainmentClass::Advisory,
2240            EnforcementTier::KernelFull,
2241            None,
2242            None,
2243            None,
2244            Vec::new(),
2245            Vec::new(),
2246            Vec::new(),
2247            IoMaxReport::default(),
2248        ))
2249        .unwrap();
2250        assert_eq!(undeclared["exports_files"], serde_json::Value::Null);
2251
2252        let declared = serde_json::to_value(scope_report_for_tier(
2253            &sample_policy(),
2254            ContainmentClass::Advisory,
2255            EnforcementTier::KernelFull,
2256            None,
2257            None,
2258            Some(&Exports {
2259                files: Some(FileExport {
2260                    root: "out/".to_string(),
2261                    mode: ExportMode::ReadOnly,
2262                    max_bytes: 10_485_760,
2263                }),
2264                peer_files: None,
2265            }),
2266            Vec::new(),
2267            Vec::new(),
2268            Vec::new(),
2269            IoMaxReport::default(),
2270        ))
2271        .unwrap();
2272        assert_eq!(
2273            declared["exports_files"],
2274            serde_json::json!({"root": "out/", "mode": "read-only", "max_bytes": 10_485_760})
2275        );
2276    }
2277
2278    #[test]
2279    fn render_names_the_resource_plane_in_both_directions() {
2280        let undeclared = scope_report_for_tier(
2281            &sample_policy(),
2282            ContainmentClass::Advisory,
2283            EnforcementTier::KernelFull,
2284            None,
2285            None,
2286            None,
2287            Vec::new(),
2288            Vec::new(),
2289            Vec::new(),
2290            IoMaxReport::default(),
2291        )
2292        .render();
2293        assert!(
2294            undeclared.contains("Resource plane") && undeclared.contains("exports.files: <none>"),
2295            "{undeclared}"
2296        );
2297
2298        let declared = scope_report_for_tier(
2299            &sample_policy(),
2300            ContainmentClass::Advisory,
2301            EnforcementTier::KernelFull,
2302            None,
2303            None,
2304            Some(&Exports {
2305                files: Some(FileExport {
2306                    root: "out/".to_string(),
2307                    mode: ExportMode::ReadOnly,
2308                    max_bytes: 10_485_760,
2309                }),
2310                peer_files: None,
2311            }),
2312            Vec::new(),
2313            Vec::new(),
2314            Vec::new(),
2315            IoMaxReport::default(),
2316        )
2317        .render();
2318        assert!(declared.contains("exports.files root: out/"), "{declared}");
2319        assert!(
2320            declared.contains("mode:               read-only"),
2321            "{declared}"
2322        );
2323        assert!(
2324            declared.contains("max bytes:          10485760 (per file)"),
2325            "{declared}"
2326        );
2327    }
2328
2329    // ── read_only reporting ───────────────────────────────────────────────────
2330
2331    fn report_for(read_only: &[&str], shell_allow: &[&str]) -> ScopeReport {
2332        let policy = CapabilityPolicy {
2333            read_only_paths: read_only.iter().map(|p| (*p).to_string()).collect(),
2334            shell_allow: shell_allow.iter().map(|b| (*b).to_string()).collect(),
2335            ..CapabilityPolicy::default()
2336        };
2337        scope_report_for_tier(
2338            &policy,
2339            ContainmentClass::Scoped,
2340            EnforcementTier::KernelFull,
2341            None,
2342            None,
2343            None,
2344            Vec::new(),
2345            Vec::new(),
2346            Vec::new(),
2347            IoMaxReport::default(),
2348        )
2349    }
2350
2351    /// With no allowlisted interpreter the declaration holds everywhere the check can reach, and
2352    /// the report says exactly that and nothing stronger.
2353    #[test]
2354    fn declared_read_only_with_no_interpreter_reports_as_enforced() {
2355        let report = report_for(&["tests", "bench/fixtures"], &["git"]);
2356        assert_eq!(
2357            report.read_only_paths,
2358            vec!["tests".to_string(), "bench/fixtures".to_string()]
2359        );
2360        assert!(report.read_only_advisory_for.is_empty());
2361
2362        let rendered = report.render();
2363        assert!(rendered.contains("  read_only:\n    - tests\n    - bench/fixtures\n"));
2364        assert!(rendered.contains(
2365            "read_only enforcement: enforced for every tool call and every shell command the \
2366             dispatch check can read"
2367        ));
2368        assert!(!rendered.contains("advisory against"));
2369    }
2370
2371    /// An allowlisted interpreter is named, and the enforcement line is qualified — the report
2372    /// must never claim more than `W-SEC-017` already withdraws.
2373    #[test]
2374    fn an_allowlisted_interpreter_is_named_as_advisory() {
2375        let report = report_for(&["tests"], &["git", "python3"]);
2376        assert_eq!(report.read_only_advisory_for, vec!["python3".to_string()]);
2377
2378        let rendered = report.render();
2379        assert!(rendered.contains("read_only enforcement: advisory against python3"));
2380        assert!(rendered
2381            .contains("enforced for every tool call and for every shell command it can read"));
2382    }
2383
2384    /// The report's advisory set is the set `W-SEC-017` fires for, because both call one
2385    /// resolver. Asserted through the resolver rather than by re-listing interpreters, so adding
2386    /// one to `protected_paths::ADVISORY_INTERPRETERS` cannot make the two disagree.
2387    #[test]
2388    fn the_reported_advisory_set_is_the_set_the_warning_fires_for() {
2389        let shell_allow: Vec<String> = ["git", "python3", "node", "make", "bash"]
2390            .iter()
2391            .map(|b| (*b).to_string())
2392            .collect();
2393        let declared = vec!["tests".to_string()];
2394        assert_eq!(
2395            report_for(&["tests"], &["git", "python3", "node", "make", "bash"])
2396                .read_only_advisory_for,
2397            read_only_advisory_for(&declared, &shell_allow)
2398        );
2399        // Nothing is advisory against a protection that was never declared, whatever is allowed.
2400        assert!(read_only_advisory_for(&[], &shell_allow).is_empty());
2401    }
2402
2403    /// Both keys are always arrays, including for a capsule with no `capabilities.filesystem`
2404    /// block at all: an absent key must identify a runtime that predates the field, never a
2405    /// capsule that declared nothing.
2406    #[test]
2407    fn both_read_only_keys_are_always_serialized_arrays() {
2408        let json = serde_json::to_value(report_for(&[], &["python3"])).unwrap();
2409        assert_eq!(json["read_only_paths"], serde_json::json!([]));
2410        assert_eq!(json["read_only_advisory_for"], serde_json::json!([]));
2411
2412        let json = serde_json::to_value(report_for(&["tests"], &["python3"])).unwrap();
2413        assert_eq!(json["read_only_paths"], serde_json::json!(["tests"]));
2414        assert_eq!(
2415            json["read_only_advisory_for"],
2416            serde_json::json!(["python3"])
2417        );
2418    }
2419
2420    /// An undeclared `read_only` renders one `<none>` line and asks no enforcement question,
2421    /// because there is no protection to qualify.
2422    #[test]
2423    fn an_undeclared_read_only_renders_none_and_no_enforcement_line() {
2424        let rendered = report_for(&[], &["python3"]).render();
2425        assert!(rendered.contains("  read_only: <none>\n"));
2426        assert!(!rendered.contains("read_only enforcement:"));
2427    }
2428
2429    // ── Grant coverage ────────────────────────────────────────────────────────
2430
2431    /// Every `pub` field of [`CapabilityPolicy`] is either reported by [`ScopeReport`] or
2432    /// exempted here with a written reason.
2433    ///
2434    /// The field list is derived from `types.rs`'s own source text rather than written out, so a
2435    /// grant added later fails this test until someone decides which it is.
2436    ///
2437    /// The mapping is checked in both directions — every policy field is accounted for, *and*
2438    /// every report key the mapping names is really present in a serialized report — so the
2439    /// table cannot claim coverage it does not have.
2440    #[test]
2441    fn every_capability_policy_field_is_reported_or_exempted() {
2442        /// Policy fields deliberately absent from the scope report, each with the reason.
2443        const EXEMPT: &[(&str, &str)] = &[
2444            (
2445                "shell_strip_env",
2446                "Names host variables removed from the shell child's environment on top of the \
2447                 credential patterns the runtime always strips. It subtracts from what the \
2448                 capsule sees and can open nothing, so reporting it beside the grants would pad \
2449                 the list an operator audits for reach.",
2450            ),
2451            (
2452                "shell_baseline_env",
2453                "The fixed variables the shell child starts with (PATH, HOME, …). It shapes the \
2454                 environment of a subprocess the capsule was already granted, and names no host \
2455                 resource `shell allow` and `env allow` do not already account for.",
2456            ),
2457            (
2458                "limits",
2459                "Execution ceilings for a WASM guest — memory, table elements, instance count, \
2460                 deadline. A bound on how much the capsule may consume, never a reach it gains.",
2461            ),
2462            (
2463                "resources",
2464                "The same, for native subprocesses: rlimits, the Linux cgroup v2 scope and the \
2465                 workdir size ceiling. A ceiling, not a grant, on exactly `limits`' terms.",
2466            ),
2467            (
2468                "declared_deadline_seconds",
2469                "The undefaulted form of `limits.deadline_seconds`, retained only so hook calls \
2470                 can apply a lower default. Reporting it would report one ceiling twice.",
2471            ),
2472            (
2473                "state_declared",
2474                "Records that a capsule-wide `capabilities.state` block was declared where \
2475                 nothing reads it, which `W-SEC-014` says in words. The stores that actually \
2476                 exist are reported per artifact in `state_stores`.",
2477            ),
2478            (
2479                "conversation_declared",
2480                "Records a capsule-wide `capabilities.conversation` block that reaches no \
2481                 artifact, on `state_declared`'s terms; `W-SEC-016` is what an operator needs, \
2482                 and the grant itself is per artifact.",
2483            ),
2484        ];
2485
2486        /// Policy field → the key it is reported under in a serialized [`ScopeReport`].
2487        const REPORTED: &[(&str, &str)] = &[
2488            ("network_allow", "network_allow"),
2489            ("peer_fetch_allow", "peer_fetch_allow"),
2490            ("unix_sockets_allowed", "unix_sockets"),
2491            ("filesystem_scope", "filesystem_scope"),
2492            ("workdir_exec_allowed", "workdir_exec"),
2493            ("read_only_paths", "read_only_paths"),
2494            ("shell_allow", "shell_allow"),
2495            ("spawn_allow", "spawn_allow"),
2496            ("shell_interpreter_runtime", "interpreter_runtime_grants"),
2497            ("shell_staged_runtime", "staged_runtime_grants"),
2498            ("env_allow", "env_allow"),
2499            ("plan_submit", "plan_submit"),
2500            ("containment_floor", "declared_containment"),
2501        ];
2502
2503        let declared = capability_policy_field_names(include_str!("types.rs"));
2504        assert!(
2505            declared.len() > 10,
2506            "failed to read CapabilityPolicy's fields out of types.rs (found {declared:?})"
2507        );
2508
2509        for field in &declared {
2510            let reported = REPORTED.iter().any(|(policy, _)| policy == field);
2511            let exempt = EXEMPT.iter().any(|(policy, _)| policy == field);
2512            assert!(
2513                reported || exempt,
2514                "CapabilityPolicy::{field} is a grant no --explain-scope report mentions. Either \
2515                 add it to ScopeReport and to this test's REPORTED table, or add it to EXEMPT \
2516                 with a written reason for why an operator does not need to see it."
2517            );
2518            assert!(
2519                !(reported && exempt),
2520                "CapabilityPolicy::{field} is listed as both reported and exempt"
2521            );
2522        }
2523
2524        for (policy_field, _) in REPORTED.iter().chain(EXEMPT.iter()) {
2525            assert!(
2526                declared.iter().any(|field| field == policy_field),
2527                "the table names CapabilityPolicy::{policy_field}, which types.rs no longer \
2528                 declares — remove the stale row"
2529            );
2530        }
2531
2532        // The other direction: a report key the table claims must really be in the JSON, so no
2533        // row can cover a field with a key that does not exist.
2534        let json = serde_json::to_value(scope_report_for_tier(
2535            &sample_policy(),
2536            ContainmentClass::Scoped,
2537            EnforcementTier::KernelFull,
2538            None,
2539            None,
2540            None,
2541            Vec::new(),
2542            Vec::new(),
2543            Vec::new(),
2544            IoMaxReport::default(),
2545        ))
2546        .unwrap();
2547        let object = json.as_object().expect("a scope report is a JSON object");
2548        for (policy_field, report_key) in REPORTED {
2549            assert!(
2550                object.contains_key(*report_key),
2551                "the table maps CapabilityPolicy::{policy_field} to a report key \
2552                 '{report_key}' that a serialized ScopeReport does not carry"
2553            );
2554        }
2555    }
2556
2557    /// A report off [`sample_policy`] at a chosen host tier and declared floor, so the mechanism
2558    /// this session would install is the only thing that varies between the cases below.
2559    fn boundary_report(declared: ContainmentClass, tier: EnforcementTier) -> ScopeReport {
2560        scope_report_for_tier(
2561            &sample_policy(),
2562            declared,
2563            tier,
2564            None,
2565            None,
2566            None,
2567            Vec::new(),
2568            Vec::new(),
2569            Vec::new(),
2570            IoMaxReport::default(),
2571        )
2572    }
2573
2574    /// A host that mediates nothing says so, in the block an operator reads before the grants.
2575    #[test]
2576    fn a_session_with_no_kernel_mechanism_reports_what_it_does_not_protect() {
2577        let report = boundary_report(ContainmentClass::Advisory, EnforcementTier::EnvironmentOnly);
2578
2579        assert_eq!(report.achieved_containment, ContainmentClass::Advisory);
2580        assert_eq!(
2581            report.filesystem_boundary.restriction,
2582            FilesystemRestriction::Advisory
2583        );
2584        assert_eq!(report.filesystem_boundary.not_protected.len(), 2);
2585
2586        let filesystem = &report.filesystem_boundary.not_protected[0];
2587        assert!(filesystem.contains("advisory"), "{filesystem}");
2588        assert!(
2589            filesystem.contains("denies a read or a write outside the session workdir"),
2590            "{filesystem}"
2591        );
2592
2593        let rewriting = &report.filesystem_boundary.not_protected[1];
2594        assert!(rewriting.contains("not a boundary"), "{rewriting}");
2595        assert!(rewriting.contains("HOME"), "{rewriting}");
2596
2597        let rendered = report.render();
2598        let heading = rendered
2599            .find("\nNot protected here\n")
2600            .expect("a session that composes no root prints the block");
2601        assert!(
2602            heading > rendered.find("Containment\n").unwrap(),
2603            "the block belongs under Containment:\n{rendered}"
2604        );
2605        assert!(
2606            heading < rendered.find("\nEffective grants\n").unwrap(),
2607            "the block belongs ahead of the grants an operator reads it against:\n{rendered}"
2608        );
2609        for statement in &report.filesystem_boundary.not_protected {
2610            assert!(rendered.contains(statement.as_str()), "{rendered}");
2611        }
2612    }
2613
2614    /// A Landlock domain really does mediate the filesystem, so its statement says what Landlock
2615    /// leaves open rather than repeating the advisory one.
2616    #[test]
2617    fn a_landlock_session_reports_denial_rather_than_absence() {
2618        let report = boundary_report(ContainmentClass::Advisory, EnforcementTier::KernelFull);
2619
2620        assert_eq!(report.achieved_containment, ContainmentClass::Scoped);
2621        assert_eq!(
2622            report.filesystem_boundary.restriction,
2623            FilesystemRestriction::Enforced
2624        );
2625        assert_eq!(report.filesystem_boundary.not_protected.len(), 2);
2626
2627        let filesystem = &report.filesystem_boundary.not_protected[0];
2628        assert!(
2629            !filesystem.contains("advisory"),
2630            "Landlock mediates the filesystem here: {filesystem}"
2631        );
2632        assert!(
2633            filesystem.contains("denied rather than absent"),
2634            "{filesystem}"
2635        );
2636        for probe in ["stat", "access", "readlink"] {
2637            assert!(filesystem.contains(probe), "{filesystem}");
2638        }
2639    }
2640
2641    /// A capsule that declares less than `sealed` composes no root on a sealed-capable host: it
2642    /// runs Landlock-scoped, and the block must say so rather than claim the paths outside its
2643    /// grants are absent. This is the configuration `achieved_containment` cannot distinguish —
2644    /// it reads `sealed` on both sides of this test.
2645    #[test]
2646    fn a_sealed_capable_host_still_discloses_landlock_for_a_capsule_that_declared_less() {
2647        let report = boundary_report(ContainmentClass::Advisory, EnforcementTier::KernelSealed);
2648
2649        assert_eq!(
2650            report.achieved_containment,
2651            ContainmentClass::Sealed,
2652            "the host can back sealed, which is what `achieved` answers"
2653        );
2654        assert_eq!(
2655            report.filesystem_boundary.restriction,
2656            FilesystemRestriction::Enforced,
2657            "no composed root is installed for a capsule that did not declare one"
2658        );
2659        assert_eq!(
2660            report.filesystem_boundary.not_protected,
2661            boundary_report(ContainmentClass::Advisory, EnforcementTier::KernelFull)
2662                .filesystem_boundary
2663                .not_protected,
2664            "the same mechanism gets the same statements on either host"
2665        );
2666        assert!(report.render().contains("Not protected here"));
2667    }
2668
2669    /// The half of the block that is genuinely mechanism-independent: `HOME` is rewritten on every
2670    /// tier, so the statement about it is one string, not one per mechanism.
2671    #[test]
2672    fn the_path_rewriting_statement_is_identical_wherever_it_is_carried() {
2673        let advisory =
2674            boundary_report(ContainmentClass::Advisory, EnforcementTier::EnvironmentOnly);
2675        let enforced = boundary_report(ContainmentClass::Advisory, EnforcementTier::KernelFull);
2676
2677        assert_eq!(
2678            advisory.filesystem_boundary.not_protected[1],
2679            enforced.filesystem_boundary.not_protected[1]
2680        );
2681        assert!(
2682            advisory.filesystem_boundary.not_protected[1]
2683                .contains(crate::shell::SYNTHETIC_HOME_DIR_NAME),
2684            "the statement must name the directory `shell::build_shell_env` really sets HOME to"
2685        );
2686    }
2687
2688    /// A composed root prints no heading — not a heading with an empty list under it — and still
2689    /// carries the key, because an absent key must identify a runtime that predates the field.
2690    #[test]
2691    fn a_composed_root_carries_the_key_with_nothing_to_disclaim() {
2692        let report = boundary_report(ContainmentClass::Sealed, EnforcementTier::KernelSealed);
2693
2694        assert_eq!(report.achieved_containment, ContainmentClass::Sealed);
2695        assert_eq!(
2696            report.filesystem_boundary.restriction,
2697            FilesystemRestriction::Absent
2698        );
2699        assert!(report.filesystem_boundary.not_protected.is_empty());
2700        assert!(
2701            !report.render().contains("Not protected here"),
2702            "{}",
2703            report.render()
2704        );
2705
2706        let json = serde_json::to_value(&report).unwrap();
2707        assert_eq!(json["filesystem_boundary"]["restriction"], "absent");
2708        assert_eq!(
2709            json["filesystem_boundary"]["not_protected"],
2710            serde_json::json!([]),
2711            "the key is present and empty at absent, never missing"
2712        );
2713    }
2714
2715    /// `capabilities.filesystem.workdir_exec` caps the achieved *class* at `advisory` and leaves
2716    /// the *mechanism* alone: Landlock is still installed, so the block prints the Landlock
2717    /// statement rather than claiming the host denies nothing.
2718    #[test]
2719    fn workdir_exec_caps_the_class_without_changing_the_mechanism() {
2720        let policy = CapabilityPolicy {
2721            workdir_exec_allowed: true,
2722            ..CapabilityPolicy::default()
2723        };
2724
2725        let report = scope_report_for_tier(
2726            &policy,
2727            ContainmentClass::Advisory,
2728            EnforcementTier::KernelSealed,
2729            None,
2730            None,
2731            None,
2732            Vec::new(),
2733            Vec::new(),
2734            Vec::new(),
2735            IoMaxReport::default(),
2736        );
2737
2738        assert_eq!(
2739            report.enforcement_tier,
2740            "mountns+pivot_root+landlock+seccomp"
2741        );
2742        assert_eq!(report.achieved_containment, ContainmentClass::Advisory);
2743        assert_eq!(
2744            report.filesystem_boundary.restriction,
2745            FilesystemRestriction::Enforced
2746        );
2747        assert_eq!(report.filesystem_boundary.not_protected.len(), 2);
2748        assert!(report.render().contains("Not protected here"));
2749    }
2750
2751    /// The restriction follows the tier the session applies, at every combination of host tier and
2752    /// declared floor — which is what `runtime_writes` beside it is already keyed on.
2753    #[test]
2754    fn the_restriction_follows_the_tier_the_session_would_install() {
2755        for tier in ALL_TIERS {
2756            for declared in [
2757                ContainmentClass::Advisory,
2758                ContainmentClass::Scoped,
2759                ContainmentClass::Sealed,
2760            ] {
2761                let report = boundary_report(declared, *tier);
2762                let expected = match crate::sandbox::applied_tier(*tier, declared) {
2763                    EnforcementTier::KernelSealed => FilesystemRestriction::Absent,
2764                    EnforcementTier::KernelFull => FilesystemRestriction::Enforced,
2765                    EnforcementTier::KernelSeccompOnly | EnforcementTier::EnvironmentOnly => {
2766                        FilesystemRestriction::Advisory
2767                    }
2768                };
2769                assert_eq!(
2770                    report.filesystem_boundary.restriction, expected,
2771                    "{tier:?} / {declared:?}"
2772                );
2773                assert_eq!(
2774                    report.filesystem_boundary.not_protected.is_empty(),
2775                    expected == FilesystemRestriction::Absent,
2776                    "{tier:?} / {declared:?}"
2777                );
2778                let wire = match expected {
2779                    FilesystemRestriction::Advisory => "advisory",
2780                    FilesystemRestriction::Enforced => "enforced",
2781                    FilesystemRestriction::Absent => "absent",
2782                };
2783                assert_eq!(
2784                    serde_json::to_value(report.filesystem_boundary.restriction).unwrap(),
2785                    serde_json::Value::String(wire.to_string())
2786                );
2787            }
2788        }
2789    }
2790
2791    /// The one assertion the report cannot satisfy by agreeing with itself: what a subprocess in a
2792    /// real session actually gets back when it probes a host file outside its workdir, compared
2793    /// against the `restriction` the report prints for that same session.
2794    ///
2795    /// Runs on whatever tier this host reaches, and asserts against the restriction it observes
2796    /// rather than against a tier it assumes. `bash` reads the file with a redirect and tests its
2797    /// existence with `[[ -e ]]` — both are syscalls the shell makes itself, so nothing here needs
2798    /// a second binary on the exec allowlist.
2799    ///
2800    /// Driving `execute_shell` needs an egress network namespace, so it stands down where the host
2801    /// withholds one rather than failing: a host that restricts unprivileged user namespaces
2802    /// refuses the uid_map write every spawn goes through, whatever the filesystem tier.
2803    #[test]
2804    fn the_reported_restriction_matches_what_a_subprocess_actually_reaches() {
2805        if crate::network_namespace::skip_without_egress_namespace(
2806            "the_reported_restriction_matches_what_a_subprocess_actually_reaches",
2807        ) {
2808            return;
2809        }
2810        if crate::sandbox::find_on_path("bash").is_none() {
2811            eprintln!(
2812                "[SKIP-HOST] the_reported_restriction_matches_what_a_subprocess_actually_reaches: \
2813                 no bash on PATH"
2814            );
2815            return;
2816        }
2817        let outside = tempfile::tempdir().expect("a temp directory outside the workdir");
2818        let secret = outside.path().join("host-file");
2819        std::fs::write(&secret, "PROBE-CONTENTS\n").unwrap();
2820        let secret = secret.to_str().unwrap().to_string();
2821
2822        for declared in [ContainmentClass::Advisory, ContainmentClass::Sealed] {
2823            let workdir = tempfile::tempdir().unwrap();
2824            let policy = CapabilityPolicy {
2825                shell_allow: vec!["bash".to_string()],
2826                ..CapabilityPolicy::default()
2827            };
2828            let probe = crate::sandbox::HostProbe::probe();
2829            let enforcement =
2830                crate::sandbox::ShellEnforcement::resolve(&policy, declared, probe).unwrap();
2831            let report = scope_report_for_tier(
2832                &policy,
2833                declared,
2834                probe.tier(),
2835                None,
2836                None,
2837                None,
2838                Vec::new(),
2839                Vec::new(),
2840                Vec::new(),
2841                IoMaxReport::default(),
2842            );
2843
2844            let command = format!(
2845                "if read -r line < '{secret}' 2>/dev/null; then echo \"read=$line\"; \
2846                 else echo read=refused; fi; \
2847                 if [[ -e '{secret}' ]]; then echo exists=yes; else echo exists=no; fi"
2848            );
2849            let result = crate::shell::execute_shell(
2850                "bash",
2851                &["-c", &command],
2852                &[],
2853                workdir.path(),
2854                workdir.path(),
2855                &policy,
2856                &enforcement,
2857            )
2858            .unwrap_or_else(|error| panic!("declared {declared:?}: {error}"));
2859            let seen = result.stdout;
2860
2861            match report.filesystem_boundary.restriction {
2862                FilesystemRestriction::Advisory => assert!(
2863                    seen.contains("read=PROBE-CONTENTS"),
2864                    "reported advisory, so nothing denies the read: {seen}"
2865                ),
2866                FilesystemRestriction::Enforced => assert!(
2867                    seen.contains("read=refused") && seen.contains("exists=yes"),
2868                    "reported enforced, so the read is denied and the path still exists: {seen}"
2869                ),
2870                FilesystemRestriction::Absent => assert!(
2871                    seen.contains("read=refused") && seen.contains("exists=no"),
2872                    "reported absent, so the path is not in the composed root: {seen}"
2873                ),
2874            }
2875        }
2876    }
2877
2878    /// The `pub` field names of `CapabilityPolicy`, read out of `types.rs`'s own text.
2879    fn capability_policy_field_names(source: &str) -> Vec<String> {
2880        source
2881            .split_once("pub struct CapabilityPolicy {")
2882            .expect("types.rs declares CapabilityPolicy")
2883            .1
2884            .split_once("\n}")
2885            .expect("CapabilityPolicy's declaration is closed at column 0")
2886            .0
2887            .lines()
2888            .filter_map(|line| {
2889                line.strip_prefix("    pub ")
2890                    .and_then(|rest| rest.split_once(':'))
2891                    .map(|(name, _)| name.to_string())
2892            })
2893            .collect()
2894    }
2895}