diff --git a/Documentation/ABI/testing/sysfs-driver-apple-sep b/Documentation/ABI/testing/sysfs-driver-apple-sep index d575f242da296..bb0f3d9ac8f55 100644 --- a/Documentation/ABI/testing/sysfs-driver-apple-sep +++ b/Documentation/ABI/testing/sysfs-driver-apple-sep @@ -580,6 +580,31 @@ Description: no handler for, as an unsigned decimal. The first few are logged. Normally 0. +What: /sys/bus/platform/devices/.sep/diag/sensor_bringup_failures +Date: November 2026 +KernelVersion: 7.3 +Contact: Ryan Murray +Description: + (RO) How many Touch ID sensor bring-up attempts have failed + since probe, as an unsigned decimal: attach-time, late, + verify-time and restore attempts alike, whether or not a + later attempt brought the sensor up. Each failure is logged + with the step that failed. sensor_attach says whether the + sensor came up in the end; this says how often it took more + than one attempt. Normally 0. + +What: /sys/bus/platform/devices/.sep/diag/capture_frame_errors +Date: November 2026 +KernelVersion: 7.3 +Contact: Ryan Murray +Description: + (RO) How many Touch ID captures since probe returned a frame + the driver refused, for a checksum that did not match or a + length the sensor advertised that cannot be a frame, as an + unsigned decimal. Each such capture ends with a request to + touch again; nothing from the frame reaches the enclave. + Normally 0. + What: /sys/bus/platform/devices/.sep/diag/sleep What: /sys/bus/platform/devices/.sep/diag/sleeps Date: November 2026 diff --git a/drivers/soc/apple/diag.rs b/drivers/soc/apple/diag.rs index 079ffba1a9b8c..442018915900a 100644 --- a/drivers/soc/apple/diag.rs +++ b/drivers/soc/apple/diag.rs @@ -72,6 +72,8 @@ extern "C" { fn sep_diag_sse_event() -> c_uint; fn sep_diag_xarm_failed() -> c_uint; fn sep_diag_unhandled() -> c_uint; + fn sep_diag_sensor_bringup_failed() -> c_uint; + fn sep_diag_capture_frame_error() -> c_uint; fn sep_diag_log_summary(dev: *mut c_void); } @@ -481,6 +483,21 @@ pub(crate) fn unhandled() -> u32 { unsafe { sep_diag_unhandled() } } +/// One sensor bring-up attempt failed, whether or not another follows. +/// Returns how many have since probe, this one included. +pub(crate) fn sensor_bringup_failed() -> u32 { + // SAFETY: no preconditions; updates one word. + unsafe { sep_diag_sensor_bringup_failed() } +} + +/// A capture's frame was refused for a bad checksum or an unusable length, +/// and the user was asked to touch again. Returns how many have since +/// probe, this one included. +pub(crate) fn capture_frame_error() -> u32 { + // SAFETY: no preconditions; updates one word. + unsafe { sep_diag_capture_frame_error() } +} + /// Logs the one-line summary of the boot-time bring-up. Call it after /// [`bringup_ended`]. pub(crate) fn log_summary(dev: &device::Device) { diff --git a/drivers/soc/apple/diag_shim.c b/drivers/soc/apple/diag_shim.c index b073720d9abd6..0fe57d0f4e33b 100644 --- a/drivers/soc/apple/diag_shim.c +++ b/drivers/soc/apple/diag_shim.c @@ -118,6 +118,11 @@ static struct { atomic_t skdl; atomic_t xarm_failures; atomic_t unhandled; + /* Failure counts that cover one bind: they start at 0 when it registers. */ + struct sep_diag_bind_counts { + atomic_t sensor_bringup_failures; + atomic_t capture_frame_errors; + } bind; atomic_t sleeps; atomic_t sleeping; /* SEP_DIAG_TRUSTED_*, with the refusal status above it. */ @@ -127,6 +132,17 @@ static struct { struct kernfs_node *touchid_kn; } sep_diag; +static void sep_diag_bind_counts_reset(struct sep_diag_bind_counts *counts) +{ + atomic_set(&counts->sensor_bringup_failures, 0); + atomic_set(&counts->capture_frame_errors, 0); +} + +static ssize_t sep_diag_count_emit(char *buf, const atomic_t *count) +{ + return sysfs_emit(buf, "%u\n", (unsigned int)atomic_read(count)); +} + static DEFINE_SPINLOCK(sep_diag_kn_lock); static void sep_diag_put_kns(void *data) @@ -704,6 +720,22 @@ static ssize_t unhandled_messages_show(struct device *dev, } static DEVICE_ATTR_RO(unhandled_messages); +static ssize_t sensor_bringup_failures_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return sep_diag_count_emit(buf, &sep_diag.bind.sensor_bringup_failures); +} +static DEVICE_ATTR_RO(sensor_bringup_failures); + +static ssize_t capture_frame_errors_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return sep_diag_count_emit(buf, &sep_diag.bind.capture_frame_errors); +} +static DEVICE_ATTR_RO(capture_frame_errors); + static ssize_t sleeps_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -770,6 +802,8 @@ static struct attribute *sep_diag_attrs[] = { &dev_attr_skdl.attr, &dev_attr_xarm_failures.attr, &dev_attr_unhandled_messages.attr, + &dev_attr_sensor_bringup_failures.attr, + &dev_attr_capture_frame_errors.attr, &dev_attr_sleeps.attr, &dev_attr_sleep.attr, &dev_attr_trusted.attr, @@ -801,6 +835,8 @@ int sep_diag_register(struct device *dev, const char *profile, sep_diag.probe_ns = ktime_get_boottime_ns(); /* A refusal belongs to the bind that saw it. */ atomic_set(&sep_diag.trusted, SEP_DIAG_TRUSTED_UNKNOWN); + /* So do the failure counts: they are documented as counts since probe. */ + sep_diag_bind_counts_reset(&sep_diag.bind); ret = devm_device_add_group(dev, &sep_diag_group); if (ret) @@ -903,6 +939,16 @@ unsigned int sep_diag_unhandled(void) return (unsigned int)atomic_inc_return(&sep_diag.unhandled); } +unsigned int sep_diag_sensor_bringup_failed(void) +{ + return (unsigned int)atomic_inc_return(&sep_diag.bind.sensor_bringup_failures); +} + +unsigned int sep_diag_capture_frame_error(void) +{ + return (unsigned int)atomic_inc_return(&sep_diag.bind.capture_frame_errors); +} + /* From the PM notifier (pm_shim.c), which runs its calls one at a time. */ void sep_diag_sleep(bool entering) { @@ -1107,3 +1153,8 @@ void sep_diag_bringup_ended(void) SEP_DIAG_TOUCHID_PENDING) sep_diag_notify_kn(&sep_diag.touchid_kn); } + +/* CONFIG_KUNIT=y and the (bool) SEP test option; plain defined() so host tests can build the file. */ +#if defined(CONFIG_KUNIT) && defined(CONFIG_APPLE_SEP_KUNIT_TEST) +#include "diag_shim_test.c" +#endif diff --git a/drivers/soc/apple/diag_shim_test.c b/drivers/soc/apple/diag_shim_test.c new file mode 100644 index 0000000000000..3b0f6bac36d46 --- /dev/null +++ b/drivers/soc/apple/diag_shim_test.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * KUnit tests for the diag shim's failure counts: they count since the probe + * that registered the group, so a registration starts them at 0. Included from + * diag_shim.c. Works on fixture-owned counts only: the production sep_diag + * state, its sysfs group and kernfs references are never touched. + */ +#include + +static unsigned int diag_test_read(struct kunit *test, const atomic_t *count) +{ + char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL); + unsigned int value; + + KUNIT_ASSERT_NOT_NULL(test, buf); + /* The same formatting the sysfs attributes use. */ + KUNIT_ASSERT_GT(test, sep_diag_count_emit(buf, count), 0); + KUNIT_ASSERT_EQ(test, kstrtouint(strim(buf), 10, &value), 0); + return value; +} + +static void diag_failure_counts_start_at_each_probe(struct kunit *test) +{ + struct sep_diag_bind_counts *counts = kunit_kzalloc(test, sizeof(*counts), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, counts); + /* A bind that registered and then failed its attach. */ + sep_diag_bind_counts_reset(counts); + atomic_inc(&counts->sensor_bringup_failures); + atomic_inc(&counts->sensor_bringup_failures); + atomic_inc(&counts->capture_frame_errors); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->sensor_bringup_failures), 2U); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->capture_frame_errors), 1U); + + /* The next probe's registration starts both at 0; they count on from there. */ + sep_diag_bind_counts_reset(counts); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->sensor_bringup_failures), 0U); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->capture_frame_errors), 0U); + atomic_inc(&counts->capture_frame_errors); + KUNIT_EXPECT_EQ(test, diag_test_read(test, &counts->capture_frame_errors), 1U); +} + +static struct kunit_case diag_shim_test_cases[] = { + KUNIT_CASE(diag_failure_counts_start_at_each_probe), + {} +}; + +static struct kunit_suite diag_shim_test_suite = { + .name = "apple-sep-diag-shim", + .test_cases = diag_shim_test_cases, +}; +kunit_test_suite(diag_shim_test_suite); diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index e1a935bd04a12..034a55e84b945 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -928,6 +928,13 @@ pub(crate) enum TouchOp { /// operation with. Only an outright refusal by the enclave (`EKEYREJECTED`: /// it performed nothing) hands the grant back, once, for the one retry /// the verify makes after it completes the evaluation. +/// +/// The retry is not one the enclave protocol asks for: the operation runs +/// first where macOS runs it (after MATCH_CANCEL, before the evaluation is +/// completed), and again after the completion only because which of the two +/// the enclave accepts has not been measured. Either way at most one +/// operation runs per match. Tracked in +/// . pub(crate) struct KeyGrant { op: TouchOp, /// The user bag the record lives under (`HANDLE_USER_BAG`), if any. diff --git a/drivers/soc/apple/registration.rs b/drivers/soc/apple/registration.rs index a49b75e42488f..08438937ac906 100644 --- a/drivers/soc/apple/registration.rs +++ b/drivers/soc/apple/registration.rs @@ -75,6 +75,11 @@ pub(crate) fn patch_loaded(nonce_session: bool, accepted: bool, needs_patch: boo /// a reboot does and as a verify's second try does: after a failed sequence /// counter init a J314s refused the CLEAR_STATE re-registration for the /// rest of the boot, while a fresh one recovered. +/// +/// The alternation is not a step the enclave protocol defines. It covers +/// two enclave behaviours each seen on one machine, and neither recovery +/// works for both. Why the attempt before it failed is still open: +/// . pub(crate) fn retry_stage(stage: u32, nonce_session: bool, retry: u32) -> u32 { if nonce_session || retry % 2 == 1 { stage @@ -187,10 +192,18 @@ pub(crate) struct Retry { budget_ms: u64, } -/// At attach, on a profile with a nonce session. Across 29 J700 boots, 7 -/// needed a second attempt and one failed three attempts made back to back. -/// Pauses of 0.5, 1, 2 and 4 s give a sensor or an enclave that is still -/// settling time to do so, and five attempts end within about 10 s. +/// At attach, on every profile. Across 29 J700 boots, 7 needed a second +/// attempt and one failed three attempts made back to back; an M1 Pro lost +/// the sensor on 2 boots in 4 with a single attempt. Pauses of 0.5, 1, 2 and +/// 4 s give a sensor or an enclave that is still settling time to do so, +/// and five attempts end within about 10 s. +/// +/// What the failed attempts wait for is not known: the sensor answers its +/// first status reads and then falls silent part-way through the sequence, +/// after the SBIO endpoint is up and INIT_SBIO_COMMUNICATION was accepted. +/// The sensor has no power-good line to wait on. Every failed attempt is +/// counted in `diag/sensor_bringup_failures`; the cause is tracked in +/// . pub(crate) const ATTACH: Retry = Retry { attempts: 5, first_pause_ms: 500, @@ -205,6 +218,17 @@ pub(crate) const VERIFY: Retry = Retry { budget_ms: u64::MAX, }; +/// Before a verify on a boot whose attach-time bring-up left the restored +/// enrolment unproven: the attempt the attach did not finish, then one from +/// a fresh registration on a profile without a nonce session (see +/// [`retry_stage`]; the round counts on from the failed attach), back to +/// back. +pub(crate) const RESTORE: Retry = Retry { + attempts: 2, + first_pause_ms: 0, + budget_ms: u64::MAX, +}; + impl Retry { /// The pause before attempt `next`, counted from 1, when the first /// attempt began `elapsed_ms` ago; `None` if there is no such attempt. @@ -656,6 +680,26 @@ mod tests { assert_eq!(m.failed, 2); } + #[test] + fn a_restore_makes_two_attempts_the_second_from_a_fresh_registration() { + assert_eq!(RESTORE.pause_before(2, 0), Some(0)); + assert_eq!(RESTORE.pause_before(3, 0), None); + + let mut m = Model::new(); + m.nonce = [false; 8]; + assert_eq!(bring_up(&RESTORE, &mut m), Outcome::GaveUp(2)); + assert_eq!(m.paused, 0); + assert_eq!(m.failed, 1); + + // The round counts on from the failed attach: its first attempt + // keeps the stage the attach left, its second starts fresh on a + // profile without a nonce session, and a nonce-session profile + // keeps clearing. + assert_eq!(retry_stage(ESTABLISHED, false, 1), ESTABLISHED); + assert_eq!(retry_stage(ESTABLISHED, false, 2), FRESH); + assert_eq!(retry_stage(ESTABLISHED, true, 2), ESTABLISHED); + } + #[test] fn a_retry_clears_first_then_tries_a_fresh_registration() { // Without a nonce session: the first retry keeps the stage, so a diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 1ce3134a42653..4576196e02509 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -2057,8 +2057,8 @@ impl SepData { wanted, began_ns: shim::boottime_ns(), attach: kind == Bringup::Attach, - // The late round follows the failed attach-time one. - retry: u32::from(kind == Bringup::Late), + // The late and the restore rounds follow a failed attach-time one. + retry: u32::from(matches!(kind, Bringup::Late | Bringup::Restore)), }; registration::bring_up(retry, &mut attempts) } @@ -2523,6 +2523,12 @@ impl SepData { // undone and every verify refused until reboot. The restore itself is // still a valid candidate, so bring the sensor up once more and let // complete_bringup() finish the proof before refusing. + // + // After a failed first patch, the enclave can refuse the serial + // re-registration that follows CLEAR_STATE, while a reboot's fresh + // registration works: registration::RESTORE makes the second attempt + // from a fresh registration, as registration::retry_stage decides for + // every other round. fn retry_restore_proof(&self) { if self.templates_restored.load(Relaxed) || !self.cold_restore_candidate.load(Relaxed) @@ -2534,18 +2540,14 @@ impl SepData { self.dev, "verify: attach-time bring-up left the device-view proof undone; retrying the sensor bring-up\n" ); - if !self.bring_sensor_online() { - // After a failed first patch, the enclave can refuse the serial - // re-registration that follows CLEAR_STATE, while a reboot's - // fresh registration works. Retry once from the fresh stage. - dev_warn_ratelimited!( - self.dev, - "verify: retrying the sensor bring-up from a fresh registration\n" - ); - self.bringup.store(BRINGUP_FRESH, Relaxed); - if !self.bring_sensor_online() { - dev_err_ratelimited!(self.dev, "verify: the retried sensor bring-up failed\n"); - } + let outcome = self.bring_sensor_online_retrying( + c"restore", + Bringup::Restore, + ®istration::RESTORE, + || self.verify_wanted(), + ); + if !outcome.online() { + dev_err_ratelimited!(self.dev, "verify: the retried sensor bring-up failed\n"); } let _ = sensor::idle(); self.refresh_match_credential(); @@ -3089,6 +3091,7 @@ impl SepData { advertised: sent, computed, }) => { + diag::capture_frame_error(); dev_warn_ratelimited!( self.dev, "verify: frame checksum mismatch (sensor sent {:#06x}, computed {:#06x})\n", @@ -3099,6 +3102,7 @@ impl SepData { return bio::VerifyOutcome::Failed(ENROL_STATUS_RETRY); } Err(sensor::CaptureError::Length(n)) => { + diag::capture_frame_error(); dev_warn_ratelimited!( self.dev, "verify: the sensor advertised an unusable frame length {}\n", @@ -4197,9 +4201,10 @@ impl SepData { advertised: sent, computed, }) => { + diag::capture_frame_error(); dev_warn_ratelimited!( self.dev, - "enrol: capture {} frame checksum mismatch (sensor sent {:#06x}, computed {:#06x}); retrying\n", + "enrol: capture {} frame checksum mismatch (sensor sent {:#06x}, computed {:#06x}); asking for another touch\n", counter, sent, computed @@ -4207,9 +4212,10 @@ impl SepData { return ImageOutcome::Retry(bio::Guidance::LiftAndMove); } Err(sensor::CaptureError::Length(n)) => { + diag::capture_frame_error(); dev_warn_ratelimited!( self.dev, - "enrol: capture {} advertised an unusable frame length {}; retrying\n", + "enrol: capture {} advertised an unusable frame length {}; asking for another touch\n", counter, n ); @@ -6402,6 +6408,9 @@ enum Bringup { Late, /// Before a verify. Verify, + /// Before a verify, when the attach left the restored enrolment + /// unproven. + Restore, } /// A sensor bring-up with retries, as [`registration::bring_up`] drives it. @@ -6428,11 +6437,17 @@ impl bool> registration::Attempts for BringupAttempts<'_, W> { self.sep.bringup.store(stage, Relaxed); } self.retry += 1; - if self.attach { + let online = if self.attach { self.sep.attach_bring_sensor_online() } else { self.sep.bring_sensor_online() + }; + if !online { + // Each failure is logged at the step that failed; the count + // shows how often the sensor needs another attempt (#2322). + diag::sensor_bringup_failed(); } + online } fn wanted(&self) -> bool { diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 2e45b04dd527b..9b076c62714cc 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -144,9 +144,16 @@ const LATE_ATTACH_DEFERS: u32 = 3; // result is still wanted. const BRINGUP_PAUSE_STEP_MS: u32 = 100; -// J700 traces show the key-store endpoint arriving around 43 s after boot, -// well after the first infrastructure endpoints. The wait exits promptly on -// other machines when their key store becomes ready. +// How long the bring-up waits for the key store to be advertised, and a +// trusted-key caller for the bring-up to end. Both waits end on the event +// itself (the EP_SKS advertisement, `trusted_ready`), so a healthy boot never +// waits this long; on expiry the key store counts as absent and callers get +// an error. The bound covers a J700 that advertised the key store about 43 s +// after boot (commit fdcf9537f07b), about twice that. Regular-root boots now +// see it within 2 s of attach on every Mac; why the J700 was that slow, and +// whether an early (initrd) load still is, is open: +// https://github.com/aurora-silicon/linux/issues/2323. A trusted-key caller +// waits inside key_construction_mutex: see issue 2313. const EXCHANGE_TIMEOUT_MS: time::Msecs = 90000; const PHASE_ATTACH: u32 = 0; diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index bd17030f3b7f1..992f51bd19e09 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -260,6 +260,8 @@ void sep_diag_set_offer(u8 endpoint, int state); unsigned int sep_diag_sse_event(void); unsigned int sep_diag_xarm_failed(void); unsigned int sep_diag_unhandled(void); +unsigned int sep_diag_sensor_bringup_failed(void); +unsigned int sep_diag_capture_frame_error(void); void sep_diag_sleep(bool entering); void sep_diag_log_summary(struct device *dev); diff --git a/drivers/soc/apple/xart_guard.rs b/drivers/soc/apple/xart_guard.rs index f5e9b9490b44e..1712fe17880b6 100644 --- a/drivers/soc/apple/xart_guard.rs +++ b/drivers/soc/apple/xart_guard.rs @@ -386,6 +386,8 @@ fn take_allowance() -> Option { "xART guard: this boot may rewrite the first existing kind-{} record the enclave asks for, once, for a boot policy change\n", kind ), + // Fail-closed: a damaged file grants nothing, and it is emptied + // above, so it cannot grant anything at a later boot either. None => pr_warn_ratelimited!("xART guard: ignored a damaged one-time allowance\n"), } allowance @@ -548,6 +550,10 @@ impl Guard { None => torn += 1, } } + // Fail-closed: an entry that does not decode lists nothing as + // Linux's, so a record it may have named stays protected if it + // existed at open. It still counts towards MAX_ENTRIES and + // `loaded_entries`, so may_retry_create sees the list as it is. if torn != 0 { pr_warn_ratelimited!("xART guard: ignored {} unreadable list entries\n", torn); }