From f1366ff2a1cc702b88106a2681ded69af03464cf Mon Sep 17 00:00:00 2001 From: Audrey Tang Date: Tue, 27 Oct 2026 23:14:20 -0700 Subject: [PATCH] soc: apple: sep: Report xART guard refusals only from the live guard A refusal sets diag/xart_guard_refusals and turns diag/matching to "blocked". The KUnit suites build guards over in-memory files and make them refuse on purpose, so on a kernel built with the tests every Mac read "blocked" although its guard had refused nothing, and a readiness check that trusts diag/matching would stop it. Reported on omacom/linux-aurora#200 by MuBai-He (J713). Only the guard that Guard::open() builds reports to the diagnostics now; guards from with_file() do not. A new case fails if a guard over a test file reports a refusal. Co-authored-by: sisyphus-dev-ai --- drivers/soc/apple/diag.rs | 6 ++++++ drivers/soc/apple/xart_guard.rs | 37 +++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/drivers/soc/apple/diag.rs b/drivers/soc/apple/diag.rs index 288c92a3cd94c3..049aa62dfbce22 100644 --- a/drivers/soc/apple/diag.rs +++ b/drivers/soc/apple/diag.rs @@ -200,6 +200,12 @@ fn matching() -> crate::xart_guard::Matching { .matching() } +/// How many refusals have been reported since registration (for the KUnit suites). +#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] +pub(crate) fn refusals() -> u32 { + REFUSALS.load(Relaxed) +} + /// The xART guard refused one of the enclave's writes or deletes. pub(crate) fn xart_guard_refused() { REFUSALS.fetch_add(1, Relaxed); diff --git a/drivers/soc/apple/xart_guard.rs b/drivers/soc/apple/xart_guard.rs index 6efb2a8609330f..fb08885f86b65e 100644 --- a/drivers/soc/apple/xart_guard.rs +++ b/drivers/soc/apple/xart_guard.rs @@ -246,6 +246,10 @@ pub(crate) struct Guard { existed: KVec, refused: usize, first_setup: FirstSetup, + /// Whether a refusal is reported to the diagnostics. Only the live guard does: + /// guards that tests build over in-memory files must not leave refusals in + /// diag/xart_guard_refusals and diag/matching. + report: bool, } impl Guard { @@ -253,11 +257,13 @@ impl Guard { /// holds now. Never fails: a list that cannot be read protects every /// existing record. pub(crate) fn open(level: u8, existing: KVec) -> Guard { - Guard::with_file( + let mut guard = Guard::with_file( level, existing, StoreFile::open_host(OWNED_PATH).map(|(file, _created)| file), - ) + ); + guard.report = true; + guard } } @@ -272,6 +278,7 @@ impl Guard { existed: existing, refused: 0, first_setup: FirstSetup::Closed, + report: false, }; // A list created this boot (or by a crashed one) is only safe to adopt // into once its name is durable: fsync() of the file alone does not @@ -406,6 +413,13 @@ impl Guard { Ok(()) } + /// Tells the diagnostics about a refusal, if this guard reports. + fn report_refusal(&self) { + if self.report { + crate::diag::xart_guard_refused(); + } + } + /// Checks one enclave write (`write`) or delete before it reaches the /// gigalocker. An error refuses the request. pub(crate) fn check(&mut self, key: &Key, write: bool) -> Result<()> { @@ -415,7 +429,7 @@ impl Guard { Verdict::Allow => Ok(()), Verdict::Adopt if !write => Ok(()), Verdict::Adopt => self.adopt(key).inspect_err(|e| { - crate::diag::xart_guard_refused(); + self.report_refusal(); pr_warn!( "xART guard: could not list a new kind-{} record as Linux's ({:?}); refusing to write it\n", key.kind, @@ -431,7 +445,7 @@ impl Guard { Ok(()) } Verdict::Refuse => { - crate::diag::xart_guard_refused(); + self.report_refusal(); self.refused += 1; if self.refused <= LOGGED_REFUSALS { let action = if write { "rewrite" } else { "delete" }; @@ -650,6 +664,21 @@ mod tests { Ok(()) } + #[test] + fn a_guard_over_a_test_file_does_not_report_to_the_diagnostics() -> Result<()> { + // A refusal by the live guard blocks matching in diag/matching. A guard the + // suites build over an in-memory file must not leave one there: on a kernel + // built with the tests it would block a Mac whose guard refused nothing. + let bytes = RefCell::new(KVec::new()); + let foreign = session(1); + let mut guard = Guard::with_file(1, keys(&[foreign])?, Ok(mem(&bytes))); + let before = crate::diag::refusals(); + assert_eq!(guard.check(&foreign, true), Err(EPERM)); + assert_eq!(guard.check(&foreign, false), Err(EPERM)); + assert_eq!(crate::diag::refusals(), before); + Ok(()) + } + #[test] fn first_setup_allows_only_a_kind_4_rewrite_at_level_1() { let open = FirstSetup::Open;