diff --git a/drivers/soc/apple/profile.rs b/drivers/soc/apple/profile.rs index 6803c1464652e0..46e61fa0a62fc9 100644 --- a/drivers/soc/apple/profile.rs +++ b/drivers/soc/apple/profile.rs @@ -154,6 +154,8 @@ const DART_T8112: DartWindow = DartWindow { pub(crate) struct PlatformProfile { pub(crate) name: &'static str, + /// Retry sensor registration before opening an enrolment on the enclave. + pub(crate) retry_enrol_registration: bool, /// Capacity of the boot shared-memory window. The layout is still checked /// against the manifests; this only bounds the allocation. pub(crate) shmem_capacity: usize, @@ -187,6 +189,7 @@ pub(crate) struct PlatformProfile { pub(crate) const T8103: PlatformProfile = PlatformProfile { name: "T8103/J313", + retry_enrol_registration: false, shmem_capacity: 0x3_0000, shmem_first_item: b"CNIP", bootstrap: Bootstrap::Boot, @@ -229,6 +232,7 @@ pub(crate) const T8103: PlatformProfile = PlatformProfile { /// identically. Enrolment persistence is reboot-tested on J314s. pub(crate) const T6000: PlatformProfile = PlatformProfile { name: "T6000/J316s", + retry_enrol_registration: true, shmem_capacity: 0x3_0000, shmem_first_item: b"CNIP", bootstrap: Bootstrap::Boot, @@ -266,6 +270,7 @@ pub(crate) const T6000: PlatformProfile = PlatformProfile { pub(crate) const T6020: PlatformProfile = PlatformProfile { name: "T6020/J414s", + retry_enrol_registration: false, shmem_capacity: 0x4_0000, shmem_first_item: b"CINP", bootstrap: Bootstrap::WarmRegister, @@ -322,6 +327,7 @@ pub(crate) const T8140: PlatformProfile = PlatformProfile { key_store: KeyStore::Variant5, persistent_enrol: true, name: "T8140/J700", + retry_enrol_registration: false, shmem_capacity: 0x4_0000, shmem_first_item: b"CINP", bootstrap: Bootstrap::WarmRegister, @@ -365,6 +371,7 @@ pub(crate) const T8140: PlatformProfile = PlatformProfile { /// warm-registration platforms (T6020 on current firmware, T8140). pub(crate) const T8112: PlatformProfile = PlatformProfile { name: "T8112/J415", + retry_enrol_registration: false, shmem_capacity: 0x4_0000, shmem_first_item: b"CINP", bootstrap: Bootstrap::WarmRegister, diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 9c69ca184c6912..1821c360c69535 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -1796,13 +1796,50 @@ impl SepData { bio::VerifyOutcome::Matched(bio::MatchEvidence::from_enclave_reply(identity)) } - pub(crate) fn run_enrolment(&self) { - self.enrol_frames_accepted.store(0, Relaxed); + fn power_down_enrol_sensor(&self) { + let _ = sensor::idle(); + sensor::power(false); + self.note_capture_end(); + } - if !self.bring_sensor_online() { + /// A T6000/T6001 can time out reading the sensor serial before the + /// first scan. Retry only bring-up, before opening an enclave enrolment. + fn bring_sensor_online_for_enrol(&self, epoch: u64) -> bool { + let wanted = || { + !self.shutting_down.load(Relaxed) + && !self.suspending.load(Relaxed) + && bio::enrol_is_live(&self.bio_session.lock(), epoch) + }; + if !wanted() { + return false; + } + let online = if self.profile.retry_enrol_registration { + self.bring_sensor_online_retrying( + c"enrol", + Bringup::Enrol, + ®istration::VERIFY, + wanted, + ) + .online() + } else { + self.bring_sensor_online() + }; + if !wanted() { + self.power_down_enrol_sensor(); + return false; + } + if !online { dev_err!(self.dev, "enrol: the sensor did not come back online\n"); self.finish_enrolment(Err(ENROL_STATUS_SENSOR)); let _ = sensor::idle(); + } + online + } + + pub(crate) fn run_enrolment(&self) { + self.enrol_frames_accepted.store(0, Relaxed); + + if !self.bring_sensor_online_for_enrol(self.capture_epoch.load(Relaxed)) { return; } @@ -1921,9 +1958,7 @@ impl SepData { self.reconcile_identities(); } - let _ = sensor::idle(); - sensor::power(false); - self.note_capture_end(); + self.power_down_enrol_sensor(); if let Some(result) = outcome { let filed = match (result, identity, enrolment_persisted) { @@ -3899,6 +3934,8 @@ impl SepData { /// Which bring-up a round of attempts makes. #[derive(Clone, Copy, PartialEq, Eq)] enum Bringup { + /// Before an enrolment. + Enrol, /// As Touch ID is activated. Attach, /// The late round after an attach that fell short. diff --git a/tools/aurora-sep/tests/test_enrol_bringup.py b/tools/aurora-sep/tests/test_enrol_bringup.py new file mode 100644 index 00000000000000..ee48011ad8cd2d --- /dev/null +++ b/tools/aurora-sep/tests/test_enrol_bringup.py @@ -0,0 +1,91 @@ +# SPDX-License-Identifier: MIT +"""Run the driver's pre-enrolment bring-up against a scripted sensor.""" +from pathlib import Path +import subprocess +import tempfile +import unittest + +from test_verify_bringup import APPLE, method, registration_module + + +class EnrolBringupTest(unittest.TestCase): + def test_registration_before_opening_an_enrolment(self): + source = (APPLE / 'sbio.rs').read_text() + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + (root / 'test.rs').write_text( + registration_module() + HARNESS + method(source, 'power_down_enrol_sensor') + + method(source, 'bring_sensor_online_for_enrol') + CASES) + subprocess.run(['rustc', '--edition=2021', str(root / 'test.rs'), '-o', str(root / 'test')], check=True) + subprocess.run([str(root / 'test')], check=True) + + +HARNESS = r""" +use std::cell::Cell; +use std::ffi::CStr; +use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; +macro_rules! dev_err { ($($args:tt)*) => {} } +const ENROL_STATUS_SENSOR: u32 = 1; +thread_local! { static EVENTS: std::cell::RefCell> = Default::default(); } +fn event(name: &'static str) { EVENTS.with(|events| events.borrow_mut().push(name)); } +mod sensor { + pub fn idle() -> bool { super::event("idle"); true } + pub fn power(on: bool) { assert!(!on); super::event("off"); } +} +struct Profile { retry_enrol_registration: bool } +enum Bringup { Enrol } +struct Session { epoch: u64, attempts: Cell, cancel_after: u32 } +impl Session { fn lock(&self) -> &Self { self } } +mod bio { + pub fn enrol_is_live(s: &super::Session, epoch: u64) -> bool { + s.epoch == epoch && s.attempts.get() < s.cancel_after + } +} +struct Sensor { + profile: Profile, bio_session: Session, shutting_down: AtomicBool, + suspending: AtomicBool, ready_after: u32, +} +struct Round<'a, W>(&'a Sensor, W); +impl bool> registration::Attempts for Round<'_, W> { + fn attempt(&mut self) -> bool { self.0.bring_sensor_online() } + fn wanted(&self) -> bool { (self.1)() } + fn elapsed_ms(&self) -> u64 { 0 } + fn failed(&mut self, _: u32, _: u32, _: u32) {} + fn pause(&mut self, _: u32) -> bool { true } +} +impl Sensor { + fn note_capture_end(&self) { event("settle"); } + fn finish_enrolment(&self, result: Result<(), u32>) { + assert_eq!(result, Err(ENROL_STATUS_SENSOR)); event("fault"); + } + fn bring_sensor_online(&self) -> bool { + let count = &self.bio_session.attempts; + count.set(count.get() + 1); + count.get() == self.ready_after + } + fn bring_sensor_online_retrying(&self, _: &CStr, _: Bringup, policy: ®istration::Retry, + wanted: impl Fn() -> bool) -> registration::Outcome { + registration::bring_up(policy, &mut Round(self, wanted)) + } +""" + +CASES = r""" +} +fn run(retry: bool, ready_after: u32, cancel_after: u32, epoch: u64) -> (bool, u32, Vec<&'static str>) { + let sensor = Sensor { + profile: Profile { retry_enrol_registration: retry }, + bio_session: Session { epoch: 10, attempts: Cell::new(0), cancel_after }, + suspending: false.into(), shutting_down: false.into(), ready_after, + }; + (sensor.bring_sensor_online_for_enrol(epoch), sensor.bio_session.attempts.get(), EVENTS.with(|e| e.take())) +} +fn main() { + assert_eq!(run(true, 1, 99, 10), (true, 1, vec![])); + assert_eq!(run(true, 3, 99, 10), (true, 3, vec![])); + assert_eq!(run(true, 4, 99, 10), (false, 3, vec!["fault", "idle"])); + assert_eq!(run(false, 2, 99, 10), (false, 1, vec!["fault", "idle"])); + assert_eq!(run(true, 2, 1, 10), (false, 1, vec!["idle", "off", "settle"])); + assert_eq!(run(true, 1, 1, 10), (false, 1, vec!["idle", "off", "settle"])); // cancelled during a successful attempt + assert_eq!(run(true, 1, 99, 9), (false, 0, vec![])); // obsolete worker +} +"""