From 670ac6f1f57e21e8b9ab069e6e10c509321dd575 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 13 Nov 2026 23:39:03 +1100 Subject: [PATCH 01/24] soc: apple: sep: gate per-user key bags on a proven capability, not the create variant userbag_ioctl refused per-user key bags unless profile.key_store was the variant-5 create encoding. That encoding describes how the identity key bag is created, not whether the enclave can create a NEW per-user bag. T8112 (M2) and T8122 (M3) are warm-register platforms whose SEP is handed over running and shared with macOS; their identity bag uses the variant-5 form, so they passed the Variant5 check, yet their enclave never replies to a user-bag DESIGNATE_KEYBAG. A single unanswered DESIGNATE condemns the SKS endpoint for the rest of the boot (sks_wedged), taking the whole key store down: the log shows "DESIGNATE_KEYBAG got no reply" followed by every later SKS request reporting "endpoint gone for this boot". Per-user bag create and designate are proven only on T8140 (J700) and the T6040 (J616s / M4 Pro) enclave family. Add an explicit PlatformProfile capability, per_user_keybags, true only on those, and gate userbag_ioctl and the per-user-bag ref-key / FDE paths on it instead of on the create-encoding variant. The unsupported platforms now return EOPNOTSUPP before any enclave request, so the SKS endpoint is never wedged. Touch ID and the identity bag are unaffected: neither uses userbag_ioctl. The sep KUnit suite passes (598 run, 596 passed, 2 skipped, 0 failed). Assisted-by: LLM Signed-off-by: Ryan Murray --- drivers/soc/apple/profile.rs | 20 ++++++++++++++++++++ drivers/soc/apple/refkey.rs | 6 +++--- drivers/soc/apple/userbag.rs | 14 +++++++++----- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/drivers/soc/apple/profile.rs b/drivers/soc/apple/profile.rs index c87d46f44eb9f..d3431d7a089d9 100644 --- a/drivers/soc/apple/profile.rs +++ b/drivers/soc/apple/profile.rs @@ -185,6 +185,16 @@ pub(crate) struct PlatformProfile { pub(crate) key_store: KeyStore, /// Platforms with reboot-tested owner export and user-before-master saves. pub(crate) persistent_enrol: bool, + /// Whether creating a NEW per-user key bag (`userbag_ioctl`, linux#921) is + /// supported on this enclave. This is NOT the same as the create-encoding + /// [`KeyStore`] variant: the warm, macOS-shared enclaves (T8112/M2, + /// T8122/M3) encode identity bags with the variant-5 form yet never reply + /// to a user-bag `DESIGNATE_KEYBAG`, which would condemn the SKS endpoint + /// for the boot. Set true only where user-bag create/designate is proven on + /// hardware (T8140/Neo and the J616s/M4-Pro enclave family); false + /// elsewhere so the ioctl refuses cleanly with `EOPNOTSUPP`. Touch ID and + /// the identity bag are unaffected (they do not use `userbag_ioctl`). + pub(crate) per_user_keybags: bool, /// The `xart_guard` level when the parameter is left at its default (see /// [`crate::xart_guard`]). pub(crate) xart_guard: u8, @@ -222,6 +232,7 @@ pub(crate) const T8103: PlatformProfile = PlatformProfile { cpx_encryption_mode: 2, }, persistent_enrol: true, + per_user_keybags: false, nonce_session: false, // Off: Touch ID setup on a cold-boot SEP rewrites a session record that // predates Linux, so the guard would only block it. @@ -263,6 +274,7 @@ pub(crate) const T6000: PlatformProfile = PlatformProfile { cpx_encryption_mode: 2, }, persistent_enrol: true, + per_user_keybags: false, nonce_session: false, // As T8103. On J316s, creating the identity keybag also rewrites a // session record that predates Linux. @@ -297,6 +309,8 @@ pub(crate) const T6020: PlatformProfile = PlatformProfile { }, key_store: KeyStore::Variant5, persistent_enrol: true, + // M2 Pro's warm macOS-shared enclave: user-bag create not proven (as M2). + per_user_keybags: false, nonce_session: false, // A warm-attach enclave shared with macOS, as on T8112. xart_guard: 1, @@ -326,6 +340,9 @@ pub(crate) const T6020_SEPOS13: PlatformProfile = PlatformProfile { pub(crate) const T8140: PlatformProfile = PlatformProfile { key_store: KeyStore::Variant5, persistent_enrol: true, + // User-bag create/designate proven on J700 hardware (variant-5 capture); + // the J616s/M4-Pro family (T6040 and its inheritors) shares this. + per_user_keybags: true, name: "T8140/J700", shmem_capacity: 0x4_0000, shmem_first_item: b"CINP", @@ -398,6 +415,9 @@ pub(crate) const T8112: PlatformProfile = PlatformProfile { }, key_store: KeyStore::Variant5, persistent_enrol: true, + // M2's warm macOS-shared enclave encodes the identity bag variant-5 but + // never replies to a user-bag DESIGNATE_KEYBAG (wedges SKS): refuse cleanly. + per_user_keybags: false, nonce_session: false, // Guarded from its first Linux boot (2026-10-16): no record refused. xart_guard: 1, diff --git a/drivers/soc/apple/refkey.rs b/drivers/soc/apple/refkey.rs index f0fde4827754a..c0ffcb1d98074 100644 --- a/drivers/soc/apple/refkey.rs +++ b/drivers/soc/apple/refkey.rs @@ -537,7 +537,7 @@ impl SepData { user: crate::sks::DesignateUser, acl: Option<&[u8]>, ) -> Result> { - if self.profile.key_store != profile::KeyStore::Variant5 { + if !self.profile.per_user_keybags { return Err(EOPNOTSUPP); } crate::xart_guard::close_policy_window(); @@ -601,7 +601,7 @@ impl SepData { /// caller has unlocked the bag with the user's password. Variant-5 key /// store only. pub(crate) fn fde_key_create(&self, user: crate::sks::DesignateUser) -> Result> { - if self.profile.key_store != profile::KeyStore::Variant5 { + if !self.profile.per_user_keybags { return Err(EOPNOTSUPP); } crate::xart_guard::close_policy_window(); @@ -651,7 +651,7 @@ impl SepData { record: &[u8], sealed: &[u8], ) -> Result> { - if self.profile.key_store != profile::KeyStore::Variant5 { + if !self.profile.per_user_keybags { return Err(EOPNOTSUPP); } Self::refkey_pub(record).ok_or(EINVAL)?; diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index 5b6bdeff833fb..f2db368e5d85e 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -1304,13 +1304,17 @@ impl crate::SepData { pub(crate) fn userbag_ioctl(&self, cmd: u32, arg: usize) -> Result { crate::bio::require_admin()?; - if self.profile.key_store != crate::profile::KeyStore::Variant5 { - // The user-bag layouts were captured on the variant-5 key store - // (macOS 26 on the Neo). The 13.x key store of M1/M2-generation - // Macs has not been captured, so nothing is sent there. + if !self.profile.per_user_keybags { + // Gate on the proven-hardware capability, NOT the create-encoding + // variant: the warm macOS-shared enclaves (T8112/M2, T8122/M3) use + // the variant-5 identity encoding yet never reply to a user-bag + // DESIGNATE_KEYBAG, which condemns the SKS endpoint for the boot. + // User-bag create/designate is proven only on the Neo (T8140) and + // the J616s/M4-Pro family; everything else refuses cleanly here so + // the enclave is never wedged. Touch ID / identity are untouched. dev_info_ratelimited!( self.dev, - "userbag: per-user key bags are not supported on this key store yet (only variant 5 has been captured)\n" + "userbag: per-user key bags are not supported on this Mac's Secure Enclave key store\n" ); return Err(EOPNOTSUPP); } From 60d354ba6648b7168ad11fe4e129f7f186a8e090 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Wed, 11 Nov 2026 08:38:03 -0600 Subject: [PATCH 02/24] soc: apple: sep: keep explicit user bags in 13.5 ref-key requests On the 13.5 key store, sks_req_refkey always substituted the configured Linux user's designation for the target handle. That is right for the identity bag's source handles, but it also redirected an explicitly designated user bag, so a key meant for a per-user bag was made and used under the identity bag instead. Factor the choice into refkey_target(): source handles keep the identity mapping, and an explicit user designation keeps its own bag. Variant 5 is unchanged. The KUnit cases cover the identity fallback, the user-designation boundaries and variant 5. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/refkey.rs | 53 +++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/drivers/soc/apple/refkey.rs b/drivers/soc/apple/refkey.rs index c0ffcb1d98074..b41ac297693e3 100644 --- a/drivers/soc/apple/refkey.rs +++ b/drivers/soc/apple/refkey.rs @@ -13,6 +13,23 @@ use kernel::sync::atomic::Relaxed; pub(crate) const REFKEY_ENVELOPE_VERSION: u32 = 2; +fn refkey_target(key_store: profile::KeyStore, keybag: i32) -> Result<(i32, u32)> { + match key_store { + // Explicit user designations retain their own bag on the 13.5 protocol. + profile::KeyStore::Sepos13 { .. } if keybag <= -crate::sks::SKS_DESIGNATE_USER_MIN => { + Ok((keybag, 3)) + } + profile::KeyStore::Sepos13 { .. } => { + // Identity calls use the designated session. A loaded source + // handle and class F with -1 did not persist correctly on J313. + let user = crate::sks::DesignateUser::new(crate::SBIO_PROBE_USER_ID) + .ok_or(EINVAL)?; + Ok((user.special_handle().value(), 3)) + } + profile::KeyStore::Variant5 => Ok((keybag, REFKEY_ENVELOPE_VERSION)), + } +} + impl SepData { /// Keep only decoded bytes across switch-root, never an open initrd file. fn with_identity_keybag( @@ -37,17 +54,7 @@ impl SepData { } fn sks_req_refkey(&self, keybag: i32, der_set: &[u8]) -> Result { - let (keybag, envelope) = match self.profile.key_store { - // The 13.5 operation envelope is 3. Use the designated identity - // session rather than the source handle; class F with -1 expired - // at the next boot on J313. - profile::KeyStore::Sepos13 { .. } => { - let user = crate::sks::DesignateUser::new(crate::SBIO_PROBE_USER_ID) - .ok_or(EINVAL)?; - (user.special_handle().value(), 3) - } - profile::KeyStore::Variant5 => (keybag, REFKEY_ENVELOPE_VERSION), - }; + let (keybag, envelope) = refkey_target(self.profile.key_store, keybag)?; let mut body = image::Body::new(); body.put_u32(0)?; body.put_u64(crate::sks::SKS_CLIENT_ID)?; @@ -805,3 +812,27 @@ impl SepData { } } } + +#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(apple_sep_refkey_target)] +mod target_tests { + use super::*; + + #[test] + fn user_designations_keep_their_bag_on_each_protocol() -> Result<()> { + let old = profile::KeyStore::Sepos13 { cpx_encryption_mode: 2 }; + let identity = crate::sks::DesignateUser::new(crate::SBIO_PROBE_USER_ID) + .ok_or(EINVAL)?.special_handle().value(); + for handle in [0, 7, -1, -(crate::sks::SKS_DESIGNATE_USER_MIN - 1)] { + assert_eq!(refkey_target(old, handle)?, (identity, 3)); + } + for handle in [-crate::sks::SKS_DESIGNATE_USER_MIN, -0x0100_03e8] { + assert_eq!(refkey_target(old, handle)?, (handle, 3)); + } + for handle in [0, 7, -1, -0x0100_03e8] { + assert_eq!(refkey_target(profile::KeyStore::Variant5, handle)?, + (handle, REFKEY_ENVELOPE_VERSION)); + } + Ok(()) + } +} From 54f58a3ac62a4a59a5c9a291b7e59e278145159c Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Wed, 11 Nov 2026 12:37:23 -0600 Subject: [PATCH 03/24] soc: apple: sep: userbag: designate with the key store's own variant The per-user bag path hard-coded designate variant 1 and its flags trailer, the variant-5 form. The 13.5 key store takes variant 0 with the body ending after the empty blob, as the identity-bag path already sends it. Move the variant choice into sks::designate_variant(), shared by both paths, and encode and decode the user-bag designation with it. The variant-5 layout is unchanged; the user-bag ioctls stay gated by per_user_keybags. The KUnit golden test gains a 13.5 case for the shorter body and its reply, and checks that each store rejects the other's form. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/sks.rs | 15 +++++++--- drivers/soc/apple/userbag.rs | 55 +++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/drivers/soc/apple/sks.rs b/drivers/soc/apple/sks.rs index 547780e3483dc..c9e0be1819d19 100644 --- a/drivers/soc/apple/sks.rs +++ b/drivers/soc/apple/sks.rs @@ -407,10 +407,7 @@ impl SepData { /// designate uses struct version 0; the other profile uses its own /// variant. fn sks_designate_variant(&self) -> u32 { - match self.profile.key_store { - profile::KeyStore::Sepos13 { .. } => crate::sks::SKS_DESIGNATE_VERSION_13, - profile::KeyStore::Variant5 => crate::sks::SKS_DESIGNATE_VARIANT, - } + designate_variant(self.profile.key_store) } /// `0x0d` designate, which makes the system key bag. On 13.5 the body is: @@ -1468,6 +1465,16 @@ pub(crate) const SKS_DESIGNATE_VARIANT: u32 = 1; static_assert!(SKS_DESIGNATE_VARIANT == 1); pub(crate) const SKS_DESIGNATE_VERSION_13: u32 = 0; +/// The designate request's leading variant on `key_store`: `0` on the 13.5 +/// key store, which ends the body after the empty blob, and `1` on variant 5, +/// which adds the flags word. +pub(crate) fn designate_variant(key_store: profile::KeyStore) -> u32 { + match key_store { + profile::KeyStore::Sepos13 { .. } => SKS_DESIGNATE_VERSION_13, + profile::KeyStore::Variant5 => SKS_DESIGNATE_VARIANT, + } +} + pub(crate) const SKS_DESIGNATE_USER_MIN: i32 = 10; static_assert!(SKS_DESIGNATE_USER_MIN > 0); diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index f2db368e5d85e..517a2fbc16738 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -30,7 +30,6 @@ const CLIENT: u64 = crate::sks::SKS_CLIENT_ID; const CREATE_VARIANT: u32 = 4; const CREATE_BAG_TYPE: u32 = 0; const CREATE_ARG: i32 = -1; -const DESIGNATE_VARIANT: u32 = 1; const CHANGE_VARIANT: u32 = 3; const LOCK_STATE_VARIANT: u32 = 1; const DEVICE_STATE_VARIANT: u32 = 0; @@ -200,28 +199,32 @@ fn decode_create_reply(body: &[u8]) -> Option { Some(crate::sks::KeyBagHandle::from_create_reply(handle)) } -fn encode_designate(source_handle: i32, user_handle: i32) -> Result { +fn encode_designate(key_store: crate::profile::KeyStore, source_handle: i32, user_handle: i32) -> Result { if source_handle < 0 || user_handle >= 0 { return Err(EINVAL); } let mut body = crate::image::Body::new(); - body.put_u32(DESIGNATE_VARIANT)?; + body.put_u32(crate::sks::designate_variant(key_store))?; body.put_u64(CLIENT)?; body.put_i32(source_handle)?; body.put_i32(user_handle)?; body.put_blob(&[])?; - body.put_u64(0)?; + // The 13.5 structure ends after the empty blob, as in sks_req_designate. + if key_store == crate::profile::KeyStore::Variant5 { + body.put_u64(0)?; + } Ok(body) } -fn decode_designate(body: &[u8]) -> Option { +fn decode_designate(key_store: crate::profile::KeyStore, body: &[u8]) -> Option { let mut c = Cursor::new(body); - if c.u32()? != DESIGNATE_VARIANT || c.u64()? != CLIENT { + if c.u32()? != crate::sks::designate_variant(key_store) || c.u64()? != CLIENT { return None; } let source_handle = c.i32()?; let user_handle = c.i32()?; - if source_handle < 0 || user_handle >= 0 || !c.blob()?.is_empty() || c.u64()? != 0 || !c.done() + if source_handle < 0 || user_handle >= 0 || !c.blob()?.is_empty() + || (key_store == crate::profile::KeyStore::Variant5 && c.u64()? != 0) || !c.done() { return None; } @@ -231,8 +234,8 @@ fn decode_designate(body: &[u8]) -> Option { }) } -fn decode_designate_reply(body: &[u8]) -> bool { - body == DESIGNATE_VARIANT.to_le_bytes() +fn decode_designate_reply(key_store: crate::profile::KeyStore, body: &[u8]) -> bool { + body == crate::sks::designate_variant(key_store).to_le_bytes() } fn encode_transition( @@ -1168,13 +1171,13 @@ impl crate::SepData { let designated = self.sks_step(crate::sks::SKS_DESIGNATE_NAME, |_healthy| { self.userbag_request( crate::sks::SKS_DESIGNATE_NAME, - encode_designate(handle.value(), user.special_handle().value())?, + encode_designate(self.profile.key_store, handle.value(), user.special_handle().value())?, |seq, len| crate::sks::encode_sks_designate(seq, len).ok_or(EINVAL), ) }); let valid = designated.as_ref().is_some_and(|out| { self.sks_report_response(crate::sks::SKS_DESIGNATE_NAME, out) - .is_some_and(decode_designate_reply) + .is_some_and(|body| decode_designate_reply(self.profile.key_store, body)) }); let _ = self.sks_send(self.sks_req_unload_keybag(handle)); if !valid { @@ -1208,13 +1211,13 @@ impl crate::SepData { let designated = self.sks_step(crate::sks::SKS_DESIGNATE_NAME, |_healthy| { self.userbag_request( crate::sks::SKS_DESIGNATE_NAME, - encode_designate(handle.value(), user.special_handle().value())?, + encode_designate(self.profile.key_store, handle.value(), user.special_handle().value())?, |seq, len| crate::sks::encode_sks_designate(seq, len).ok_or(EINVAL), ) }); let valid = designated.as_ref().is_some_and(|out| { self.sks_report_response(crate::sks::SKS_DESIGNATE_NAME, out) - .is_some_and(decode_designate_reply) + .is_some_and(|body| decode_designate_reply(self.profile.key_store, body)) }); if !valid { let _ = self.sks_send(self.sks_req_unload_keybag(handle)); @@ -1544,7 +1547,8 @@ mod tests { #[test] fn designate_layout_golden_and_round_trip() { - let body = encode_designate(1, USER_HANDLE).expect("designate"); + let key_store = crate::profile::KeyStore::Variant5; + let body = encode_designate(key_store, 1, USER_HANDLE).expect("designate"); let mut golden = [0u8; 32]; golden[0..4].copy_from_slice(&1u32.to_le_bytes()); golden[4..12].copy_from_slice(&CLIENT_LE); @@ -1552,13 +1556,32 @@ mod tests { golden[16..20].copy_from_slice(&USER_HANDLE.to_le_bytes()); assert_eq!(body.as_slice(), &golden); assert_eq!( - decode_designate(body.as_slice()), + decode_designate(key_store, body.as_slice()), Some(Designate { source_handle: 1, user_handle: USER_HANDLE }) ); - assert!(decode_designate_reply(&1u32.to_le_bytes())); + assert!(decode_designate_reply(key_store, &1u32.to_le_bytes())); + } + + #[test] + fn sepos13_designation_has_no_flags_trailer() { + let key_store = crate::profile::KeyStore::Sepos13 { cpx_encryption_mode: 0 }; + let body = encode_designate(key_store, 1, USER_HANDLE).expect("designate"); + let mut golden = [0u8; 24]; + golden[4..12].copy_from_slice(&CLIENT_LE); + golden[12..16].copy_from_slice(&1i32.to_le_bytes()); + golden[16..20].copy_from_slice(&USER_HANDLE.to_le_bytes()); + assert_eq!(body.as_slice(), &golden); + assert_eq!(decode_designate(key_store, body.as_slice()), + Some(Designate { source_handle: 1, user_handle: USER_HANDLE })); + assert!(decode_designate_reply(key_store, &0u32.to_le_bytes())); + assert!(!decode_designate_reply(key_store, &1u32.to_le_bytes())); + assert!(decode_designate(crate::profile::KeyStore::Variant5, body.as_slice()).is_none()); + let mut extra = [0u8; 32]; + extra[..24].copy_from_slice(body.as_slice()); + assert!(decode_designate(key_store, &extra).is_none()); } #[test] From 1859110e125a55f20f2af18f12b3976675b7b4f9 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sat, 14 Nov 2026 10:57:12 -0600 Subject: [PATCH 04/24] soc: apple: sep: userbag: per-user bags on the 13.5 key store The user-bag ioctls refused every request on the 13.5 key store of M1/M2-generation Macs. Add that store's backend for create, import, export, unlock and lock: - create uses the generic DeviceBag constructor (version 1); - unlock first unlocks the loaded bag with the version-0 CHANGE_LOCK_STATE, which has no flags trailer, then runs the designated-session device-state transition. Neither alone lets a key under the bag run; - lock runs the transition with state Locked and the loaded-bag lock, then sends grace-timer fires, as the Mac's kernel does, until the store names the bag locked. The store locks only on a fire, about 10 s after an immediate lock on M1. The bag lock is not held while it waits. A screen lock with a grace period is not captured on this store and is refused with EOPNOTSUPP; - import of a bag the user's designation already names (a repeated login) keeps that bag rather than designating it twice. Variant 5 keeps a designation after its temporary handle is unloaded. The 13.5 loaded-bag unlock needs the handle, so the bag stays loaded from import or create until logout, which locks and unloads it. Password change, state query and FDE sealing stay EOPNOTSUPP on the 13.5 store. KUnit golden layouts cover the 13.5 create and its reply, the loaded-bag unlock and lock, the timer fire, and the loaded-bag registry. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/sep.rs | 6 + drivers/soc/apple/userbag.rs | 293 ++++++++++++++++++++++++++++++++++- 2 files changed, 296 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 02fcc78b25564..e42378fb5a6f5 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -738,6 +738,11 @@ struct SepData { #[pin] cold_match_contexts: Mutex>, + // Per-user bags the 13.5 key store keeps loaded between import and + // logout (see `userbag::LoadedBags`). + #[pin] + user_bags: Mutex, + // The sensor stayed powered and registered after the last verify, so the // next verify captures without a new bring-up, as macOS does. Cleared by // anything that powers the sensor off or resets it. @@ -1215,6 +1220,7 @@ impl SepData { match_context <- new_mutex!(None), match_context_generation: Atomic::new(0), cold_match_contexts <- new_mutex!(KVec::new()), + user_bags <- new_mutex!(userbag::LoadedBags::new()), sensor_kept: Atomic::new(false), sensor_reset_in_capture: Atomic::new(false), bg_ops_ready: Atomic::new(false), diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index 517a2fbc16738..edee5071c3030 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -199,6 +199,84 @@ fn decode_create_reply(body: &[u8]) -> Option { Some(crate::sks::KeyBagHandle::from_create_reply(handle)) } +// The 13.5 key store (M1/M2 generation, `KeyStore::Sepos13`) takes older +// forms of create and of the loaded-bag lock-state change. Both are the +// hardware-tested J293 encodings: the generic DeviceBag constructor and the +// version-0 CHANGE_LOCK_STATE with no flags trailer. The device-state +// transition and designation are shared with variant 5 (see +// `encode_transition`, `crate::sks::designate_variant`). +const CREATE_VARIANT_13: u32 = 1; +const LOCK_STATE_VARIANT_13: u32 = 0; + +fn encode_create_13(secret: &[u8]) -> Result { + let mut body = crate::image::Body::new(); + body.put_u32(CREATE_VARIANT_13)?; + body.put_u64(CLIENT)?; + body.put_u32(0)?; + body.put_u32(0)?; + body.put_blob(secret)?; + body.put_blob(&[])?; + Ok(body) +} + +fn decode_create_reply_13(body: &[u8]) -> Option { + let mut c = Cursor::new(body); + if c.u32()? != CREATE_VARIANT_13 { + return None; + } + let handle = c.i32()?; + if handle < 0 || !c.done() { + return None; + } + Some(crate::sks::KeyBagHandle::from_create_reply(handle)) +} + +/// Changes a loaded bag's lock state by its handle: unlocked (0) with the +/// secret, or locked (1) with none. On the 13.5 store the unlock precedes the +/// designated-session transition; neither alone lets a key under the bag +/// run, and the session transition alone does not lock it either. +fn encode_loaded_lock_state_13(bag: i32, state: TransitionState, secret: &[u8]) -> Result { + if bag < 0 || (state == TransitionState::Unlocked) == secret.is_empty() { + return Err(EINVAL); + } + let mut body = crate::image::Body::new(); + body.put_u32(LOCK_STATE_VARIANT_13)?; + body.put_u64(CLIENT)?; + body.put_i32(bag)?; + body.put_i32(state.wire() as i32)?; + body.put_blob(secret)?; + Ok(body) +} + +/// Bags the 13.5 store holds loaded for designated users. Variant 5 keeps a +/// designation after its temporary handle is unloaded; on the 13.5 store the +/// loaded-bag unlock needs the handle, so a user's bag stays loaded from +/// import or create until logout. Keyed by the designation's special handle. +pub(crate) struct LoadedBags(KVec<(i32, crate::sks::KeyBagHandle)>); + +impl LoadedBags { + pub(crate) const fn new() -> Self { + LoadedBags(KVec::new()) + } + + fn get(&self, user: i32) -> Option { + self.0.iter().find(|(u, _)| *u == user).map(|(_, h)| *h) + } + + fn insert(&mut self, user: i32, handle: crate::sks::KeyBagHandle) -> Result<()> { + if self.get(user).is_some() { + return Err(EBUSY); + } + self.0.push((user, handle), GFP_KERNEL)?; + Ok(()) + } + + fn remove(&mut self, user: i32) -> Option { + let at = self.0.iter().position(|(u, _)| *u == user)?; + Some(self.0.remove(at).ok()?.1) + } +} + fn encode_designate(key_store: crate::profile::KeyStore, source_handle: i32, user_handle: i32) -> Result { if source_handle < 0 || user_handle >= 0 { return Err(EINVAL); @@ -1093,21 +1171,132 @@ impl crate::SepData { .ok_or(EACCES) } - /// Opens a user's bag through the captured device-state transition. + /// Whether `user`'s designation slot already names the loaded `bag`. + fn userbag_designation_names(&self, user: crate::sks::DesignateUser, bag: crate::sks::KeyBagHandle) -> bool { + let Some(loaded) = self.sks_read_uuid(bag) else { + return false; + }; + let Some(out) = self.sks_send(self.sks_req_copy_uuid_special(user.special_handle())) else { + return false; + }; + out.reply.status == 0 && self.sks_uuid_from_reply(&out) == Some(loaded) + } + + fn userbag_sepos13(&self) -> bool { + matches!(self.profile.key_store, crate::profile::KeyStore::Sepos13 { .. }) + } + + /// Opens a user's bag through the captured device-state transition. The + /// 13.5 store first unlocks the loaded bag itself. pub(crate) fn userbag_unlock( &self, user: crate::sks::DesignateUser, secret: &[u8], ) -> Result<()> { + if !self.userbag_sepos13() { + return self.userbag_transition(user, TransitionState::Unlocked, 0, secret); + } + let bags = self.user_bags.lock(); + let bag = bags.get(user.special_handle().value()).ok_or(ENOENT)?; + self.userbag_loaded_lock_state_13(bag, TransitionState::Unlocked, secret)?; self.userbag_transition(user, TransitionState::Unlocked, 0, secret) } + fn userbag_loaded_lock_state_13( + &self, + bag: crate::sks::KeyBagHandle, + state: TransitionState, + secret: &[u8], + ) -> Result<()> { + let out = self + .sks_step(crate::sks::SKS_LOCK_STATE_NAME, |_healthy| { + self.userbag_request( + crate::sks::SKS_LOCK_STATE_NAME, + encode_loaded_lock_state_13(bag.value(), state, secret)?, + |seq, len| crate::sks::encode_sks_change_lock_state(seq, len).ok_or(EINVAL), + ) + }) + .ok_or(EACCES)?; + let body = self.userbag_body(crate::sks::SKS_LOCK_STATE_NAME, &out)?; + if body.len() != 16 || crate::image::operation_status(body) != Some(0) { + return Err(self.userbag_refused("loaded-bag lock state", body)); + } + Ok(()) + } + + /// Locks the session and the loaded bag itself (13.5 store), then sends + /// grace-timer fires, as the Mac's kernel does, until the store reports + /// the bag locked. Both lock requests are accepted with the bag still + /// usable: the store locks only when its timer fires, about 10 s later + /// even for an immediate lock on M1. + fn userbag_lock_13(&self, user: crate::sks::DesignateUser, flags: u64) -> Result<()> { + const FIRES: u32 = 15; + // Not held across the requests and the waits between fires: every + // other bag and Touch ID path takes this lock. + let bag = self.user_bags.lock().get(user.special_handle().value()).ok_or(ENOENT)?; + self.userbag_transition(user, TransitionState::Locked, flags, &[])?; + self.userbag_loaded_lock_state_13(bag, TransitionState::Locked, &[])?; + for fire in 0..FIRES { + let out = self + .sks_step(crate::sks::SKS_HANDLE_TIMER_FIRE_NAME, |_healthy| { + self.userbag_request( + crate::sks::SKS_HANDLE_TIMER_FIRE_NAME, + encode_timer_fire()?, + |seq, len| Ok(crate::sks::encode_sks_handle_timer_fire(seq, len)), + ) + }) + .ok_or(EACCES)?; + let body = self.userbag_body(crate::sks::SKS_HANDLE_TIMER_FIRE_NAME, &out)?; + match decode_timer_fire_reply(body) { + Some(h) if h == user.special_handle().value() => { + dev_dbg!(self.dev, "userbag: {}'s bag locked after {} timer fires\n", h, fire + 1); + return Ok(()); + } + Some(_) => {} + None => return Err(self.userbag_refused("13.5 timer fire", body)), + } + kernel::time::delay::fsleep(kernel::time::Delta::from_millis(1000)); + } + dev_info_ratelimited!(self.dev, "userbag: the store did not lock {}'s bag within {} s\n", user.special_handle().value(), FIRES); + Err(ETIMEDOUT) + } + + /// Logout on the 13.5 store: lock the session, then unload the bag. The + /// caller exports it first; the next login imports it again. + fn userbag_logout_13(&self, user: crate::sks::DesignateUser) -> Result<()> { + let locked = self.userbag_lock_13(user, 0); + let removed = self.user_bags.lock().remove(user.special_handle().value()); + if let Some(bag) = removed { + let unloaded = self.sks_send(self.sks_req_unload_keybag(bag)); + if !matches!(unloaded, Some(ref out) if out.reply.status == 0) { + dev_info_ratelimited!(self.dev, "userbag: unloading {}'s bag at logout failed\n", user.special_handle().value()); + return Err(EIO); + } + } + locked + } + pub(crate) fn userbag_lock( &self, user: crate::sks::DesignateUser, screen_lock: bool, immediate: bool, ) -> Result<()> { + // The 13.5 store locks as it unlocks: the device-state transition + // (the variant-5 layout; flags 1 for a screen lock, 0 at once), then + // the loaded bag. The transition alone leaves the bag usable. + if self.userbag_sepos13() { + // A screen lock that keeps the class keys for a grace period has + // not been captured on this store: refuse it rather than wait. + if screen_lock && !immediate { + dev_info_ratelimited!( + self.dev, + "userbag: a screen lock with a grace period is not supported on the 13.5 key store yet\n" + ); + return Err(EOPNOTSUPP); + } + return self.userbag_lock_13(user, 0); + } if immediate { let handle = user.special_handle().value(); return self @@ -1168,6 +1357,16 @@ impl crate::SepData { /// handle, in macOS's order. pub(crate) fn userbag_import(&self, user: crate::sks::DesignateUser, wrapped: &[u8]) -> Result<()> { let handle = self.sks_load_wrapped(wrapped).ok_or(EACCES)?; + // The 13.5 store keeps a designation after its bag is unloaded and + // refuses to designate the slot again: a second login in one boot + // finds it already naming this bag. + if self.userbag_sepos13() && self.userbag_designation_names(user, handle) { + if let Err(e) = self.user_bags.lock().insert(user.special_handle().value(), handle) { + let _ = self.sks_send(self.sks_req_unload_keybag(handle)); + return Err(e); + } + return Ok(()); + } let designated = self.sks_step(crate::sks::SKS_DESIGNATE_NAME, |_healthy| { self.userbag_request( crate::sks::SKS_DESIGNATE_NAME, @@ -1179,6 +1378,14 @@ impl crate::SepData { self.sks_report_response(crate::sks::SKS_DESIGNATE_NAME, out) .is_some_and(|body| decode_designate_reply(self.profile.key_store, body)) }); + if valid && self.userbag_sepos13() { + // Kept loaded for the session (see `LoadedBags`). + if let Err(e) = self.user_bags.lock().insert(user.special_handle().value(), handle) { + let _ = self.sks_send(self.sks_req_unload_keybag(handle)); + return Err(e); + } + return Ok(()); + } let _ = self.sks_send(self.sks_req_unload_keybag(handle)); if !valid { dev_info_ratelimited!(self.dev, "userbag: designating the imported bag as {} failed\n", user.special_handle().value()); @@ -1195,9 +1402,13 @@ impl crate::SepData { let _healthy = self .sks_health_check(crate::sks::SKS_CREATE_NAME) .ok_or(EACCES)?; + let sepos13 = self.userbag_sepos13(); + if sepos13 && self.user_bags.lock().get(user.special_handle().value()).is_some() { + return Err(EBUSY); + } let request = self.userbag_request( crate::sks::SKS_CREATE_NAME, - encode_create(secret)?, + if sepos13 { encode_create_13(secret)? } else { encode_create(secret)? }, |seq, len| crate::sks::encode_sks_create(seq, len).ok_or(EINVAL), )?; let created = crate::xart_guard::keybag_create_window(|| self.sks_send(Ok(request))) @@ -1206,7 +1417,8 @@ impl crate::SepData { return Err(EACCES); } let body = self.userbag_body(crate::sks::SKS_CREATE_NAME, &created)?; - let handle = decode_create_reply(body).ok_or(EPROTO)?; + let handle = if sepos13 { decode_create_reply_13(body) } else { decode_create_reply(body) } + .ok_or_else(|| self.userbag_refused("create", body))?; let designated = self.sks_step(crate::sks::SKS_DESIGNATE_NAME, |_healthy| { self.userbag_request( @@ -1223,6 +1435,12 @@ impl crate::SepData { let _ = self.sks_send(self.sks_req_unload_keybag(handle)); return Err(EACCES); } + if sepos13 { + if let Err(e) = self.user_bags.lock().insert(user.special_handle().value(), handle) { + let _ = self.sks_send(self.sks_req_unload_keybag(handle)); + return Err(e); + } + } // Copying the wrapped bag into a per-user durable store is phase 2. // Until then this ioctl establishes only the live designated bag. Ok(()) @@ -1321,6 +1539,22 @@ impl crate::SepData { ); return Err(EOPNOTSUPP); } + // Variant 5 (macOS 26 on the Neo) has every layout. The 13.5 store of + // M1/M2-generation Macs has create, import/export, unlock and lock; + // its password change, state query and FDE sealing are not captured. + let supported = match self.profile.key_store { + crate::profile::KeyStore::Variant5 => true, + crate::profile::KeyStore::Sepos13 { .. } => { + matches!(cmd, IOC_CREATE | IOC_IMPORT | IOC_EXPORT | IOC_UNLOCK | IOC_LOCK) + } + }; + if !supported { + dev_info_ratelimited!( + self.dev, + "userbag: this request is not supported on this key store yet\n" + ); + return Err(EOPNOTSUPP); + } let user_ptr = UserPtr::from_addr(arg); if cmd != IOC_STATE && !crate::bio::idle_for_mutation(&self.bio_session.lock()) { return Err(EBUSY); @@ -1448,6 +1682,10 @@ impl crate::SepData { let (uid, reason, grace_s) = parse_lock(&request)?; let user = bag_designation(uid)?; let screen = reason == LOCK_REASON_SCREEN; + if reason == LOCK_REASON_LOGOUT && self.userbag_sepos13() { + self.userbag_logout_13(user)?; + return crate::bio::ok(); + } self.userbag_lock(user, screen, lock_is_immediate(reason, grace_s))?; crate::bio::ok() } @@ -1545,6 +1783,55 @@ mod tests { ); } + #[test] + fn sepos13_create_layout_golden() { + let body = encode_create_13(b"pw").expect("create"); + let mut golden = [0u8; 32]; + golden[0..4].copy_from_slice(&1u32.to_le_bytes()); + golden[4..12].copy_from_slice(&CLIENT_LE); + golden[20..24].copy_from_slice(&2u32.to_le_bytes()); + golden[24..26].copy_from_slice(b"pw"); + assert_eq!(body.as_slice(), &golden); + assert_eq!(decode_create_reply_13(&[1, 0, 0, 0, 7, 0, 0, 0]).unwrap().value(), 7); + // The variant-5 reply, a negative handle and a trailer are refused. + assert!(decode_create_reply_13(&[4, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0]).is_none()); + assert!(decode_create_reply_13(&[1, 0, 0, 0, 0xff, 0xff, 0xff, 0xff]).is_none()); + assert!(decode_create_reply_13(&[1, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0]).is_none()); + assert!(decode_create_reply(&[1, 0, 0, 0, 7, 0, 0, 0]).is_none()); + } + + #[test] + fn sepos13_loaded_unlock_layout_golden() { + let body = encode_loaded_lock_state_13(5, TransitionState::Unlocked, b"pw").expect("unlock"); + let mut golden = [0u8; 28]; + golden[4..12].copy_from_slice(&CLIENT_LE); + golden[12..16].copy_from_slice(&5i32.to_le_bytes()); + golden[20..24].copy_from_slice(&2u32.to_le_bytes()); + golden[24..26].copy_from_slice(b"pw"); + assert_eq!(body.as_slice(), &golden); + assert_eq!(encode_loaded_lock_state_13(-5, TransitionState::Unlocked, b"pw").err(), Some(EINVAL)); + assert_eq!(encode_loaded_lock_state_13(5, TransitionState::Unlocked, b"").err(), Some(EINVAL)); + let body = encode_loaded_lock_state_13(5, TransitionState::Locked, b"").expect("lock"); + let mut golden = [0u8; 24]; + golden[4..12].copy_from_slice(&CLIENT_LE); + golden[12..16].copy_from_slice(&5i32.to_le_bytes()); + golden[16..20].copy_from_slice(&1i32.to_le_bytes()); + assert_eq!(body.as_slice(), &golden); + assert_eq!(encode_loaded_lock_state_13(5, TransitionState::Locked, b"pw").err(), Some(EINVAL)); + } + + #[test] + fn loaded_bags_hold_one_bag_per_user() { + let mut bags = LoadedBags::new(); + let h = crate::sks::KeyBagHandle::from_load_reply(3); + bags.insert(USER_HANDLE, h).expect("insert"); + assert_eq!(bags.insert(USER_HANDLE, h).unwrap_err(), EBUSY); + assert_eq!(bags.get(USER_HANDLE).unwrap().value(), 3); + assert!(bags.get(USER_HANDLE - 1).is_none()); + assert_eq!(bags.remove(USER_HANDLE).unwrap().value(), 3); + assert!(bags.remove(USER_HANDLE).is_none()); + } + #[test] fn designate_layout_golden_and_round_trip() { let key_store = crate::profile::KeyStore::Variant5; From 51802dd395f53e754458a24277b0bf4a4dbcd801 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sat, 14 Nov 2026 15:21:49 -0600 Subject: [PATCH 05/24] soc: apple: sep: userbag: password change on the 13.5 key store The 13.5 key store refused CHANGE with EOPNOTSUPP, so a user's bag could not follow a password change. Its change-secret request is version 1 of the variant-5 one, as macOS 13 sends it: client, handle, the old and new secrets, two further blobs and two words (a u8 flag and the generation option), without variant 5's last word. Send it with the two blobs empty, the flag 0xff and the generation option 0, as the variant-5 path does, and accept the version-1 reply. KUnit adds the golden layout. Not yet run on hardware. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/userbag.rs | 69 ++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index edee5071c3030..99054c5e8caa3 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -434,6 +434,37 @@ fn decode_change_secret(body: &[u8]) -> Option> { }) } +/// The 13.5 store's change-secret request (opcode 7): version 1, then the +/// variant-5 fields without its last word, as macOS 13 sends it: client, +/// handle, the old and new secrets, two further blobs (empty, as in the +/// variant-5 capture), a u8 flag widened to a word (0xff, as captured) and +/// the generation option (0). +const CHANGE_VARIANT_13: u32 = 1; + +fn encode_change_secret_13(user_handle: i32, old: &[u8], new: &[u8]) -> Result { + if user_handle >= 0 || old.is_empty() || new.is_empty() { + return Err(EINVAL); + } + let mut body = crate::image::Body::new(); + body.put_u32(CHANGE_VARIANT_13)?; + body.put_u64(CLIENT)?; + body.put_i32(user_handle)?; + body.put_blob(old)?; + body.put_blob(new)?; + body.put_blob(&[])?; + body.put_blob(&[])?; + body.put_u32(CHANGE_TRAILER)?; + body.put_u32(0)?; + Ok(body) +} + +/// The 13.5 reply: version 1, then zero words (the generation out-value). +fn decode_change_secret_reply_13(body: &[u8]) -> bool { + body.len() >= 4 + && body[..4] == CHANGE_VARIANT_13.to_le_bytes() + && body[4..].iter().all(|b| *b == 0) +} + fn decode_change_secret_reply(body: &[u8]) -> bool { body == [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] } @@ -1324,18 +1355,24 @@ impl crate::SepData { new: &[u8], ) -> Result<()> { let handle = user.special_handle().value(); + let sepos13 = self.userbag_sepos13(); let out = self .sks_step(crate::sks::SKS_CHANGE_SECRET_NAME, |_healthy| { self.userbag_request( crate::sks::SKS_CHANGE_SECRET_NAME, - encode_change_secret(handle, old, new)?, + if sepos13 { + encode_change_secret_13(handle, old, new)? + } else { + encode_change_secret(handle, old, new)? + }, |seq, len| Ok(crate::sks::encode_sks_change_secret(seq, len)), ) }) .ok_or(EACCES)?; let body = self.userbag_body(crate::sks::SKS_CHANGE_SECRET_NAME, &out)?; - if !decode_change_secret_reply(body) { - return Err(EPROTO); + let ok = if sepos13 { decode_change_secret_reply_13(body) } else { decode_change_secret_reply(body) }; + if !ok { + return Err(self.userbag_refused("change secret", body)); } Ok(()) } @@ -1540,12 +1577,12 @@ impl crate::SepData { return Err(EOPNOTSUPP); } // Variant 5 (macOS 26 on the Neo) has every layout. The 13.5 store of - // M1/M2-generation Macs has create, import/export, unlock and lock; - // its password change, state query and FDE sealing are not captured. + // M1/M2-generation Macs has create, import/export, unlock, lock and + // password change; its state query and FDE sealing are not captured. let supported = match self.profile.key_store { crate::profile::KeyStore::Variant5 => true, crate::profile::KeyStore::Sepos13 { .. } => { - matches!(cmd, IOC_CREATE | IOC_IMPORT | IOC_EXPORT | IOC_UNLOCK | IOC_LOCK) + matches!(cmd, IOC_CREATE | IOC_IMPORT | IOC_EXPORT | IOC_UNLOCK | IOC_LOCK | IOC_CHANGE) } }; if !supported { @@ -1800,6 +1837,26 @@ mod tests { assert!(decode_create_reply(&[1, 0, 0, 0, 7, 0, 0, 0]).is_none()); } + #[test] + fn sepos13_change_secret_layout_golden() { + let body = encode_change_secret_13(USER_HANDLE, b"ol", b"nw").expect("change"); + let mut golden = [0u8; 52]; + golden[0..4].copy_from_slice(&1u32.to_le_bytes()); + golden[4..12].copy_from_slice(&CLIENT_LE); + golden[12..16].copy_from_slice(&USER_HANDLE.to_le_bytes()); + golden[16..20].copy_from_slice(&2u32.to_le_bytes()); + golden[20..22].copy_from_slice(b"ol"); + golden[24..28].copy_from_slice(&2u32.to_le_bytes()); + golden[28..30].copy_from_slice(b"nw"); + // two empty blobs at 32 and 36 + golden[40..44].copy_from_slice(&0xffu32.to_le_bytes()); + assert_eq!(&body.as_slice()[..44], &golden[..44]); + assert_eq!(body.as_slice().len(), 48); + assert!(decode_change_secret_reply_13(&[1, 0, 0, 0, 0, 0, 0, 0])); + assert!(!decode_change_secret_reply_13(&[3, 0, 0, 0, 0, 0, 0, 0])); + assert!(!decode_change_secret_reply_13(&[1, 0, 0, 0, 5, 0, 0, 0])); + } + #[test] fn sepos13_loaded_unlock_layout_golden() { let body = encode_loaded_lock_state_13(5, TransitionState::Unlocked, b"pw").expect("unlock"); From 9770961cdd0c2611228f687e6b00d1676479ec9f Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sat, 14 Nov 2026 10:58:57 -0600 Subject: [PATCH 06/24] soc: apple: sep: sep-key: user-bag keys on the 13.5 key store CREATE_USER refused on the 13.5 key store because user_key_create_in_bag made only variant 5's EC key type 5. The 13.5 store makes EC type 4 ref-keys, as it already does under the identity bag. Choose the key type from the key store, and advertise SEP_KEY_FEATURE_PASSWORD wherever per_user_keybags allows user bags. Sign and unseal under a user bag already address the designation's special handle on either store. On J293, keys created under a user's bag signed with an enclave-checked password. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/key.rs | 3 ++- drivers/soc/apple/refkey.rs | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index 9a3047d0d1157..ad4e2c48a3dc4 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -2247,7 +2247,8 @@ impl SepData { } let user = UserPtr::from_addr(arg); match read_request(cmd, arg)? { - KeyRequest::Info => write_user(user, &info_for(self.profile.key_store == crate::profile::KeyStore::Variant5)), + // Password keys live in per-user bags (see `userbag`). + KeyRequest::Info => write_user(user, &info_for(self.profile.per_user_keybags)), KeyRequest::Create(mut req, policy) => { may_create_machine_key(crate::shim::capable_admin(), crate::shim::current_euid())?; let (acl, flags) = match policy { diff --git a/drivers/soc/apple/refkey.rs b/drivers/soc/apple/refkey.rs index b41ac297693e3..088c2fc648333 100644 --- a/drivers/soc/apple/refkey.rs +++ b/drivers/soc/apple/refkey.rs @@ -537,8 +537,10 @@ impl SepData { } /// Test only (`key_selftest`): a ref-key under the designated user bag - /// `user` (outer handle `-uid`), which the caller has unlocked. Variant-5 - /// key store only. + /// `user` (outer handle `-uid`), which the caller has unlocked. The key + /// type follows the key store, as for identity-bag keys: the 13.5 store + /// makes EC type 4 under a designated user bag as it does under the + /// identity bag. pub(crate) fn user_key_create_in_bag( &self, user: crate::sks::DesignateUser, @@ -547,8 +549,12 @@ impl SepData { if !self.profile.per_user_keybags { return Err(EOPNOTSUPP); } + let key_type = match self.profile.key_store { + profile::KeyStore::Sepos13 { .. } => 4, + profile::KeyStore::Variant5 => 5, + }; crate::xart_guard::close_policy_window(); - self.sks_create_refkey_at(user.special_handle().value(), 9, 5, acl).ok_or(EIO) + self.sks_create_refkey_at(user.special_handle().value(), 9, key_type, acl).ok_or(EIO) } /// Unseal (enclave `ock` + kernel decrypt, as `user_key_unseal`) with a From 03bf52b4e9aef1ed35cc40c335aaa7b875b5ab5a Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sun, 15 Nov 2026 22:07:03 -0600 Subject: [PATCH 07/24] soc: apple: sep: allow per-user key bags on T8103 The 13.5 key store paths for per-user bags (create, designate, import/export, unlock, lock, password change) and for user-bag keys are in place, so turn the per_user_keybags capability on for T8103. On J293 (MacBook Pro 13-inch, M1, cold-boot SEP) the userbag ioctls created, exported, imported, unlocked and locked (logout and suspend) a user's bag, and user-bag keys signed with an enclave-checked password. Password change is implemented but not yet run on hardware; a screen lock with a grace period stays refused on this store. T6000 and the 13.5 J414s profile are unchanged: nothing has run there. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/profile.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/profile.rs b/drivers/soc/apple/profile.rs index d3431d7a089d9..5d2261f0ff7df 100644 --- a/drivers/soc/apple/profile.rs +++ b/drivers/soc/apple/profile.rs @@ -232,7 +232,9 @@ pub(crate) const T8103: PlatformProfile = PlatformProfile { cpx_encryption_mode: 2, }, persistent_enrol: true, - per_user_keybags: false, + // User-bag create, designate, import/export, unlock, lock and password + // change on the 13.5 key store, proven on J293 hardware (cold-boot SEP). + per_user_keybags: true, nonce_session: false, // Off: Touch ID setup on a cold-boot SEP rewrites a session record that // predates Linux, so the guard would only block it. From 65d1db9919bb8df31450a6d17d345ef97013d8d8 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Wed, 11 Nov 2026 08:31:17 -0600 Subject: [PATCH 08/24] soc: apple: sep: fail key operations when credential disposal fails A password-authorized sign or unseal creates an unlock evaluation in the enclave and deletes it afterwards. The deletion's answer was ignored, so a result could be returned while the credential context it was authorized by might still be live. Confirm the deletion before a result leaves the driver, and discard the result if it fails. An unconfirmed deletion latches key operations off until the driver restarts (a reboot or a module reload), so nothing later runs against a context that may still hold the credential; capability queries and cancellation stay available meanwhile. The KUnit cases and the host test run the production guard for success, early exits and a failed deletion, checking that the result is destroyed and that deletion is attempted exactly once. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- .github/workflows/sep-source.yml | 1 + drivers/soc/apple/key.rs | 86 ++++++++++++++++--- drivers/soc/apple/sbio.rs | 2 +- drivers/soc/apple/sep.rs | 23 +++-- .../tests/test_key_context_cleanup.py | 46 ++++++++++ 5 files changed, 139 insertions(+), 19 deletions(-) create mode 100644 tools/aurora-sep/tests/test_key_context_cleanup.py diff --git a/.github/workflows/sep-source.yml b/.github/workflows/sep-source.yml index 342886382a065..5c1c99444b301 100644 --- a/.github/workflows/sep-source.yml +++ b/.github/workflows/sep-source.yml @@ -71,6 +71,7 @@ jobs: python3 -m unittest tools/aurora-sep/tests/test_owner_preparation.py -v python3 -m unittest tools/aurora-sep/tests/test_policy_window.py -v python3 -m unittest tools/aurora-sep/tests/test_log_ratelimit.py -v + python3 -m unittest tools/aurora-sep/tests/test_key_context_cleanup.py -v python3 -m unittest discover -s tools/aurora-sep/tests -p test_packaging.py -v rust-build: diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index ad4e2c48a3dc4..25d72ea73e1d1 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -957,12 +957,12 @@ impl KeyGrant { /// Deletes an enclave credential context. pub(crate) trait ContextDelete { - fn delete_context(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]); + fn delete_context(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) -> Result<()>; } impl ContextDelete for SepData { - fn delete_context(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) { - self.close_unlock_evaluation(handle); + fn delete_context(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) -> Result<()> { + self.delete_unlock_evaluation(handle) } } @@ -972,22 +972,38 @@ impl ContextDelete for SepData { pub(crate) struct OwnedContext<'a, D: ContextDelete + ?Sized> { owner: &'a D, handle: [u8; crate::scrd::SCRD_ACM_HANDLE_LEN], + disposed: bool, } impl<'a, D: ContextDelete + ?Sized> OwnedContext<'a, D> { pub(crate) fn new(owner: &'a D, handle: [u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) -> Self { - OwnedContext { owner, handle } + OwnedContext { owner, handle, disposed: false } } pub(crate) fn handle(&self) -> &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN] { &self.handle } + + fn dispose(&mut self) -> Result<()> { + if self.disposed { + return Ok(()); + } + self.disposed = true; + let result = self.owner.delete_context(&self.handle); + crate::image::wipe(&mut self.handle); + result + } + + /// Dispose before publishing the result. Failure drops any owned output. + pub(crate) fn finish(mut self, result: Result) -> Result { + self.dispose()?; + result + } } impl Drop for OwnedContext<'_, D> { fn drop(&mut self) { - self.owner.delete_context(&self.handle); - crate::image::wipe(&mut self.handle); + let _ = self.dispose(); } } @@ -1645,6 +1661,9 @@ impl SepData { errno_of(&job.result) ); ended?; + if self.key_context_failed.load(Relaxed) { + return Err(EIO); + } Ok(job) } @@ -2097,7 +2116,7 @@ impl SepData { digest: &[u8; DIGEST_LEN], sealed: &[u8], pw: &[u8], - ) -> Result> { + ) -> Result { let bag = bag_user(uid)?; // Not under `catacomb_writer`: a touch wait holds it for up to a // minute, and this path needs no capture. @@ -2107,9 +2126,9 @@ impl SepData { dev_info_refusal!(self.dev, "sep-key: uid {}'s password refused by the enclave (status {})\n", uid, verify); Err(EACCES) } else { - self.key_op_in(Some(uid), op, blob, digest, sealed, Some(ctx.handle())) + self.key_op_in(Some(uid), op, blob, digest, sealed, Some(ctx.handle())).map(crate::Secret) }; - drop(ctx); + let r = ctx.finish(r); dev_dbg!( self.dev, "sep-key: password-gated {:?} under uid {}'s bag: {}\n", @@ -2245,6 +2264,9 @@ impl SepData { if self.shutting_down.load(Relaxed) { return Err(ENODEV); } + if cmd != IOC_INFO && cmd != IOC_CANCEL && self.key_context_failed.load(Relaxed) { + return Err(EIO); + } let user = UserPtr::from_addr(arg); match read_request(cmd, arg)? { // Password keys live in per-user bags (see `userbag`). @@ -2349,7 +2371,7 @@ impl SepData { require_admin_or(uid)?; let pw = check_password(&req.password)?; let digest = req.sign.digest; - crate::Secret(self.key_op_with_password(uid, h.blob, TouchOp::Sign, &digest, &[], pw)?) + self.key_op_with_password(uid, h.blob, TouchOp::Sign, &digest, &[], pw)? }; put_signature(&sig, &mut req.sign)?; // `req` wipes the password when it drops. @@ -2361,7 +2383,7 @@ impl SepData { let uid = check_password_handle(&h)?; require_admin_or(uid)?; let pw = check_password(&req.password)?; - crate::Secret(self.key_op_with_password(uid, h.blob, TouchOp::Unseal, &[0; DIGEST_LEN], sealed, pw)?) + self.key_op_with_password(uid, h.blob, TouchOp::Unseal, &[0; DIGEST_LEN], sealed, pw)? }; put_secret(&secret, &mut req.seal)?; // `secret` and `req` wipe the plaintext and password when they drop. @@ -2511,9 +2533,10 @@ mod one_op_tests { } impl ContextDelete for Deleter { - fn delete_context(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) { + fn delete_context(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) -> Result<()> { self.deleted.set(self.deleted.get() + 1); self.last.set(*handle); + Ok(()) } } @@ -2554,6 +2577,45 @@ mod one_op_tests { assert_eq!(d.deleted.get(), 1); } + struct RefusingDeleter(Cell); + + impl ContextDelete for RefusingDeleter { + fn delete_context(&self, _handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) -> Result<()> { + self.0.set(self.0.get() + 1); + Err(EIO) + } + } + + struct PendingOutput<'a>(&'a Cell); + + impl Drop for PendingOutput<'_> { + fn drop(&mut self) { + self.0.set(true); + } + } + + #[test] + fn cleanup_failure_discards_success_without_retrying_disposal() { + let d = RefusingDeleter(Cell::new(0)); + let discarded = Cell::new(false); + let ctx = OwnedContext::new(&d, [0x51; crate::scrd::SCRD_ACM_HANDLE_LEN]); + let result = ctx.finish(Ok(PendingOutput(&discarded))); + assert!(matches!(result, Err(e) if e == EIO)); + assert!(discarded.get()); + assert_eq!(d.0.get(), 1); + } + + #[test] + fn confirmed_disposal_preserves_the_operation_result() { + let d = deleter(); + let ctx = OwnedContext::new(&d, [0x52; crate::scrd::SCRD_ACM_HANDLE_LEN]); + assert_eq!(ctx.finish(Ok(37)), Ok(37)); + assert_eq!(d.deleted.get(), 1); + let ctx = OwnedContext::new(&d, [0x53; crate::scrd::SCRD_ACM_HANDLE_LEN]); + assert_eq!(ctx.finish::<()>(Err(ECANCELED)), Err(ECANCELED)); + assert_eq!(d.deleted.get(), 2); + } + // Production kernels do not build the reuse measurements. #[test] fn reuse_measurements_are_research_only() { diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 0c3b733941936..e353c938bc8a5 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -2871,7 +2871,7 @@ impl SepData { Some(context) => drop(context), None => self.close_unlock_evaluation(&evaluation), } - if key_mode && matched { + if key_mode && matched && !self.key_context_failed.load(Relaxed) { self.key_touch_after_close(&evaluation); } } diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index e42378fb5a6f5..6f7cd31265569 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -885,6 +885,8 @@ struct SepData { key_touch_active: Atomic, /// `SEP_KEY_CANCEL`: ends the touch-gated operation that is waiting. key_touch_cancel: Atomic, + /// An unconfirmed credential disposal disables private key operations. + key_context_failed: Atomic, // The enclave status of the last /dev/sep-key signature (self-test). last_key_status: Atomic, @@ -1274,6 +1276,7 @@ impl SepData { key_touch <- new_mutex!(None), key_touch_active: Atomic::new(false), key_touch_cancel: Atomic::new(false), + key_context_failed: Atomic::new(false), last_key_status: Atomic::new(0), bio_lockout: Atomic::new(false), bio_after_boot: Atomic::new(false), @@ -2777,13 +2780,21 @@ impl SepData { } pub(crate) fn close_unlock_evaluation(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) { + let _ = self.delete_unlock_evaluation(handle); + } + + pub(crate) fn delete_unlock_evaluation(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) -> Result<()> { match self.scrd_command(&crate::scrd::scrd_context_delete(handle)) { - Some((0, _)) => {} - other => dev_warn_ratelimited!( - self.dev, - "scrd: deleting the unlock evaluation answered {:?}\n", - other.map(|(status, _)| status) - ), + Some((0, _)) => Ok(()), + other => { + self.key_context_failed.store(true, Relaxed); + dev_warn_ratelimited!( + self.dev, + "scrd: deleting the unlock evaluation answered {:?}; private key operations disabled until reboot\n", + other.map(|(status, _)| status) + ); + Err(EIO) + } } } diff --git a/tools/aurora-sep/tests/test_key_context_cleanup.py b/tools/aurora-sep/tests/test_key_context_cleanup.py new file mode 100644 index 0000000000000..a127590b89ab0 --- /dev/null +++ b/tools/aurora-sep/tests/test_key_context_cleanup.py @@ -0,0 +1,46 @@ +"""Run the production credential guard and its focused cleanup tests on the host.""" +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[3] + + +@unittest.skipUnless(shutil.which("rustc"), "rustc required") +class ContextCleanupTests(unittest.TestCase): + def test_production_context_disposal(self): + source = (ROOT / "drivers/soc/apple/key.rs").read_text() + trait = source[source.index("pub(crate) trait ContextDelete"): + source.index("impl ContextDelete for SepData")] + guard = source[source.index("/// A credential context"): + source.index("/// Self-test copies")] + tests = source[source.index(" struct Deleter {"): + source.index(" // Production kernels do not build")] + harness = r''' +use std::cell::Cell; +type Result = std::result::Result; +const EIO: i32 = -5; +const EINTR: i32 = -4; +const ECANCELED: i32 = -125; +const ETIMEDOUT: i32 = -110; +const ENODEV: i32 = -19; +mod scrd { pub const SCRD_ACM_HANDLE_LEN: usize = 16; } +mod image { pub fn wipe(bytes: &mut [u8]) { bytes.fill(0); } } +TRAIT +GUARD +TESTS +''' + harness = harness.replace("TRAIT", trait).replace("GUARD", guard).replace("TESTS", tests) + with tempfile.TemporaryDirectory() as directory: + source_path = Path(directory) / "context_cleanup.rs" + binary_path = Path(directory) / "context_cleanup" + source_path.write_text(harness) + subprocess.run(["rustc", "--edition=2021", "--test", str(source_path), + "-o", str(binary_path)], check=True, capture_output=True, text=True) + subprocess.run([str(binary_path)], check=True, capture_output=True, text=True) + + +if __name__ == "__main__": + unittest.main() From 36ae236b98ebc9ea29fc9eb7fa0c3cec5db977e2 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sat, 14 Nov 2026 11:49:06 -0600 Subject: [PATCH 09/24] soc: apple: sep: Touch ID for user-bag keys in the user's own domain A touch-gated SIGN or UNSEAL on a key under a user's bag matched the configured user's templates. The enclave refuses such a key unless the match is against the bag user's own catacomb, as macOS matches the logged-in user, so the call could never succeed on the 13.5 store. Add a biometric subject: while a touch-gated call on a user-bag key waits, the verify path addresses the bag's designation instead of the configured user. That covers the unlock evaluation, the match, the lockout and template-update persistence, and the check that a matched identity is known. Each bag user's catacomb lives in its own file, /var/lib/apple-sep-catacomb-bag-.bin, through a new catacomb Slot. Fixed machine components keep their existing files. On the 13.5 store, a password unlock now also loads that user's catacomb and makes the Touch ID preparation: an SCRD context verified with the bag's password and found TouchIdEnrollment-satisfied. Only its handle is kept, never the password, and it is deleted when the bag locks or logs out. A touch-gated call on that bag's key without one is refused with EACCES before the sensor is armed, so Touch ID needs the password once after every lock, as on macOS. Unlock never takes user_bags while it prepares, keeping the catacomb_writer-before-user_bags order of a touch. On J293: after a password unlock, one touch signed a user-presence key under the bag (verified), and the template update was saved to the user's file. After a lock the same call was refused at once. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/catacomb.rs | 73 +++++++--- drivers/soc/apple/key.rs | 28 +++- drivers/soc/apple/sbio.rs | 76 +++++++--- drivers/soc/apple/sep.rs | 17 +++ drivers/soc/apple/userbag.rs | 133 ++++++++++++++++-- .../tests/test_identity_preservation.py | 20 ++- 6 files changed, 292 insertions(+), 55 deletions(-) diff --git a/drivers/soc/apple/catacomb.rs b/drivers/soc/apple/catacomb.rs index bae0396a5b9c5..717d89c9add4f 100644 --- a/drivers/soc/apple/catacomb.rs +++ b/drivers/soc/apple/catacomb.rs @@ -9,13 +9,43 @@ use crate::shim; use crate::store::crc16_ccitt_false; use kernel::prelude::*; +use kernel::str::CString; const MAGIC: [u8; 8] = *b"SEPCTMB1"; /// magic(8) + len(4, LE) + crc(2, LE) + pad(2) const HEADER: usize = 16; const MAX_CATACOMB: usize = 1 << 20; -fn path_for(kind: u8) -> Option<&'static CStr> { +/// Where a catacomb lives: one of the fixed machine components, or the +/// biometric user of a Unix user's own key bag (`userbag`), one file per uid. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Slot { + Kind(u8), + BagUser(u32), +} + +impl From for Slot { + fn from(kind: u8) -> Self { + Slot::Kind(kind) + } +} + +enum Path { + Fixed(&'static CStr), + Owned(CString), +} + +impl core::ops::Deref for Path { + type Target = CStr; + fn deref(&self) -> &CStr { + match self { + Path::Fixed(p) => p, + Path::Owned(p) => p, + } + } +} + +fn fixed_path(kind: u8) -> Option<&'static CStr> { Some(match kind { crate::PRIVATE_TYPE_CATACOMB_MASTER => c"/var/lib/apple-sep-catacomb-master.bin", crate::PRIVATE_TYPE_CATACOMB_OWNER => c"/var/lib/apple-sep-catacomb-owner.bin", @@ -24,13 +54,22 @@ fn path_for(kind: u8) -> Option<&'static CStr> { }) } -pub(crate) fn is_kind(kind: u8) -> bool { - path_for(kind).is_some() +fn path_for(slot: impl Into) -> Option { + match slot.into() { + Slot::Kind(kind) => fixed_path(kind).map(Path::Fixed), + Slot::BagUser(uid) => CString::try_from_fmt(fmt!("/var/lib/apple-sep-catacomb-bag-{}.bin", uid)) + .ok() + .map(Path::Owned), + } +} + +pub(crate) fn is_kind(slot: impl Into) -> bool { + path_for(slot).is_some() } -pub(crate) fn writable(kind: u8) -> bool { - path_for(kind) - .and_then(|path| shim::StoreFile::open_writable_existing(path).ok()) +pub(crate) fn writable(slot: impl Into) -> bool { + path_for(slot) + .and_then(|path| shim::StoreFile::open_writable_existing(&path).ok()) .is_some() } @@ -38,7 +77,7 @@ pub(crate) fn writable(kind: u8) -> bool { /// A corrupt existing file is never truncated here: it is failure evidence. pub(crate) fn prepare_missing_owner() -> Result<()> { let path = path_for(crate::PRIVATE_TYPE_CATACOMB_OWNER).ok_or(EINVAL)?; - let (file, created) = shim::StoreFile::open_host(path)?; + let (file, created) = shim::StoreFile::open_host(&path)?; if !created { return Err(ENODATA); } @@ -46,8 +85,8 @@ pub(crate) fn prepare_missing_owner() -> Result<()> { file.sync_parent() } -pub(crate) fn write(kind: u8, blob: &[u8]) -> Result<()> { - let path = path_for(kind).ok_or(EINVAL)?; +pub(crate) fn write(slot: impl Into, blob: &[u8]) -> Result<()> { + let path = path_for(slot).ok_or(EINVAL)?; if blob.len() > MAX_CATACOMB { return Err(ENOSPC); } @@ -56,7 +95,7 @@ pub(crate) fn write(kind: u8, blob: &[u8]) -> Result<()> { head[8..12].copy_from_slice(&(blob.len() as u32).to_le_bytes()); head[12..14].copy_from_slice(&crc16_ccitt_false(blob).to_le_bytes()); - let file = shim::StoreFile::open_trunc(path)?; + let file = shim::StoreFile::open_trunc(&path)?; file.write_all(0, &head)?; if !blob.is_empty() { file.write_all(HEADER as u64, blob)?; @@ -67,9 +106,9 @@ pub(crate) fn write(kind: u8, blob: &[u8]) -> Result<()> { /// `Ok(None)` only when the file is missing. A file that exists but cannot be /// read or fails validation is an error: "cannot tell" must never read as /// "nothing saved", or a caller may select a fresh context over a saved one. -pub(crate) fn read(kind: u8) -> Result>> { - let path = path_for(kind).ok_or(EINVAL)?; - let file = match shim::StoreFile::open_readonly(path) { +pub(crate) fn read(slot: impl Into) -> Result>> { + let path = path_for(slot).ok_or(EINVAL)?; + let file = match shim::StoreFile::open_readonly(&path) { Ok(file) => file, Err(e) if e == ENOENT => return Ok(None), Err(e) => return Err(e), @@ -98,11 +137,11 @@ pub(crate) fn read(kind: u8) -> Result>> { } /// True only for a present, valid file. -pub(crate) fn is_saved(kind: u8) -> bool { - matches!(read(kind), Ok(Some(_))) +pub(crate) fn is_saved(slot: impl Into) -> bool { + matches!(read(slot), Ok(Some(_))) } /// True only when the file is provably missing. -pub(crate) fn is_absent(kind: u8) -> bool { - matches!(read(kind), Ok(None)) +pub(crate) fn is_absent(slot: impl Into) -> bool { + matches!(read(slot), Ok(None)) } diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index 25d72ea73e1d1..9b2bba527856e 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -911,6 +911,23 @@ 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. +/// Routes Touch ID to a bag user while a touch-gated call waits; the +/// configured user again when it ends, on every path. +struct SubjectScope<'a>(&'a SepData); + +impl<'a> SubjectScope<'a> { + fn new(sep: &'a SepData, subject: crate::BioSubject) -> Self { + *sep.bio_subject.lock() = Some(subject); + SubjectScope(sep) + } +} + +impl Drop for SubjectScope<'_> { + fn drop(&mut self) { + *self.0.bio_subject.lock() = None; + } +} + pub(crate) struct KeyGrant { op: TouchOp, /// The user bag the record lives under (`HANDLE_USER_BAG`), if any. @@ -1605,6 +1622,15 @@ impl SepData { // with each other, and with every other touch-gated call: one job, // one grant, one context at a time. let _writer = self.catacomb_writer.lock(); + // A key under a user's bag is matched in that user's domain, and + // only after its password unlocked the bag (13.5 store). + let _subject = match job.grant.as_ref().and_then(|g| g.bag()) { + Some(uid) => match self.userbag_touch_subject(uid)? { + Some(subject) => Some(SubjectScope::new(self, subject)), + None => None, + }, + None => None, + }; *self.key_touch.lock() = Some(job); self.key_touch_cancel.store(false, Relaxed); self.key_touch_active.store(true, Relaxed); @@ -1703,7 +1729,7 @@ impl SepData { /// A fresh tracked context for `uid`; with `secret`, a passcode /// credential from `bag`'s verify-secret is added. Returns the context /// and the verify status (0 when none was asked). - fn key_context_for( + pub(crate) fn key_context_for( &self, uid: i32, cred: Option<(crate::sks::SpecialHandle, &[u8])>, diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index e353c938bc8a5..795216f63f730 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -1021,7 +1021,37 @@ impl SepData { new_identity } + /// The biometric user the current match or enrolment addresses. + pub(crate) fn bio_user(&self) -> i32 { + self.bio_subject.lock().map_or(SBIO_PROBE_USER_ID, |s| s.user) + } + + /// Where that user's catacomb is kept. + fn bio_user_slot(&self) -> crate::catacomb::Slot { + match *self.bio_subject.lock() { + Some(s) => crate::catacomb::Slot::BagUser(s.uid), + None => crate::catacomb::Slot::Kind(PRIVATE_TYPE_CATACOMB_USER), + } + } + + /// A matched identity belongs to the user the match addressed: one of + /// the configured user's indexed templates, or one the enclave lists for + /// the bag user. + fn bio_identity_known(&self, identity: &[u8; bio::UUID_LEN]) -> bool { + let subject = *self.bio_subject.lock(); + match subject { + None => self.bio_index.lock().contains_uuid(identity), + Some(s) => self + .enclave_identities_for(s.user) + .is_some_and(|ids| ids.iter().any(|id| id == identity)), + } + } + fn reconcile_identities(&self) { + // The configured user's index; a bag user's identities are not in it. + if self.bio_subject.lock().is_some() { + return; + } let Some(listed) = self.enclave_identities_for(SBIO_PROBE_USER_ID) else { return; }; @@ -1060,7 +1090,7 @@ impl SepData { } } - fn enclave_identities_for(&self, user_id: i32) -> Option> { + pub(crate) fn enclave_identities_for(&self, user_id: i32) -> Option> { let op = crate::sbio::sbio_list_identities(); let SbioOutcome::Ok(reply) = self.sbio_call(&op) else { return None; @@ -1503,7 +1533,7 @@ impl SepData { user_ok && lockout } - fn read_component_states(&self) -> Option> { + pub(crate) fn read_component_states(&self) -> Option> { let op = crate::sbio::sbio_context_state(); let SbioOutcome::Ok(reply) = self.sbio_call(&op) else { return None; @@ -1516,11 +1546,11 @@ impl SepData { Some(reply) } - fn restore_catacomb( + pub(crate) fn restore_catacomb( &self, state: Option, id: i32, - kind: u8, + kind: impl Into, what: &CStr, ) -> RestoreOutcome { let Some(state) = state else { @@ -1723,7 +1753,16 @@ impl SepData { /// `Ok(None)` means provably not stored; any read failure is an error so /// it cannot be mistaken for a missing file. - fn read_stored(&self, kind: u8, what: &CStr) -> Result>> { + fn read_stored(&self, kind: impl Into, what: &CStr) -> Result>> { + let kind = match kind.into() { + crate::catacomb::Slot::Kind(kind) => kind, + // A bag user's catacomb lives only in its own file. + slot @ crate::catacomb::Slot::BagUser(_) => { + return crate::catacomb::read(slot).inspect_err(|e| { + dev_err_ratelimited!(self.dev, "restore: cannot read {}: {:?}\n", what, e); + }); + } + }; if crate::catacomb::is_kind(kind) { match crate::catacomb::read(kind) { Ok(Some(blob)) => return Ok(Some(blob)), @@ -1875,12 +1914,12 @@ impl SepData { for (who, kind, what) in [ ( crate::sbio::CatacombUser::enrolling(user), - PRIVATE_TYPE_CATACOMB_USER, + self.bio_user_slot(), c"post-match user catacomb", ), ( crate::sbio::CatacombUser::MASTER, - PRIVATE_TYPE_CATACOMB_MASTER, + crate::catacomb::Slot::Kind(PRIVATE_TYPE_CATACOMB_MASTER), c"post-match master catacomb", ), ] { @@ -1920,7 +1959,8 @@ impl SepData { true } - fn save_catacomb(&self, who: crate::sbio::CatacombUser, kind: u8, what: &CStr) -> bool { + fn save_catacomb(&self, who: crate::sbio::CatacombUser, kind: impl Into, what: &CStr) -> bool { + let kind = kind.into(); let selector = crate::sbio::SaveSelector::new(who); let save = match self.sbio_transfer(&crate::sbio::sbio_save_catacomb(&selector)) { @@ -2622,7 +2662,7 @@ impl SepData { dev_err_ratelimited!( self.dev, "verify: the enclave reports the Touch ID catacomb of user {} damaged; refusing before the capture. This is not a non-matching finger: the enrolment needs repair\n", - SBIO_PROBE_USER_ID + self.bio_user() ); self.finish_verify( bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE), @@ -2644,7 +2684,7 @@ impl SepData { } if self.match_scope_gen.load(Relaxed) != self.bio_open_gen.load(Relaxed) { - if let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) { + if let Some(user) = crate::sbio::UserId::new(self.bio_user()) { self.settle_owed_enclave_failures(user); } } @@ -2702,7 +2742,7 @@ impl SepData { // Each verify matches under its own unlock evaluation: a context whose // preflight leaves a biometric match outstanding, carried in the image // init and completed after an enrolled match. - let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + let Some(user) = crate::sbio::UserId::new(self.bio_user()) else { self.finish_verify(bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE), token_bytes); return; }; @@ -2965,7 +3005,7 @@ impl SepData { bio::VerifyOutcome::Matched(_) | bio::VerifyOutcome::NoMatch ); if definitive { - let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + let Some(user) = crate::sbio::UserId::new(self.bio_user()) else { if !reported_early { let failed = bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE); self.finish_verify(failed, token_bytes); @@ -3000,7 +3040,7 @@ impl SepData { // "unspecified error", which cannot tell a sensor fault from a refused // match step. fn verify_one_image(&self) -> bio::VerifyOutcome { - let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + let Some(user) = crate::sbio::UserId::new(self.bio_user()) else { dev_err_ratelimited!(self.dev, "verify: invalid probe user id\n"); return bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE); }; @@ -3194,7 +3234,7 @@ impl SepData { // match result: user id groups templates, UUID at offset 0x04 identifies one let identity = *parsed.identity_uuid(); - let known = self.bio_index.lock().contains_uuid(&identity); + let known = self.bio_identity_known(&identity); dev_info_ratelimited!( self.dev, @@ -4650,7 +4690,7 @@ impl SepData { /// and matching stays off. fn persist_template_update(&self) -> bool { self.persistence_owed.store(true, Relaxed); - let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + let Some(user) = crate::sbio::UserId::new(self.bio_user()) else { return false; }; let user_c = crate::sbio::CatacombUser::enrolling(user); @@ -4658,7 +4698,7 @@ impl SepData { dev_err_ratelimited!(self.dev, "sbio: template update: the user catacomb never became save-pending\n"); return false; } - if !self.save_catacomb(user_c, PRIVATE_TYPE_CATACOMB_USER, c"user catacomb after a template update") { + if !self.save_catacomb(user_c, self.bio_user_slot(), c"user catacomb after a template update") { return false; } self.finish_owed_persistence() @@ -4670,13 +4710,13 @@ impl SepData { if !self.persistence_owed.load(Relaxed) { return true; } - let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + let Some(user) = crate::sbio::UserId::new(self.bio_user()) else { return false; }; let user_c = crate::sbio::CatacombUser::enrolling(user); // A user save left pending by an earlier failure goes first. if self.component_state(user_c).is_some_and(|s| s & crate::sbio::COMPONENT_STATE_SAVE_PENDING != 0) - && !self.save_catacomb(user_c, PRIVATE_TYPE_CATACOMB_USER, c"user catacomb after a template update") + && !self.save_catacomb(user_c, self.bio_user_slot(), c"user catacomb after a template update") { return false; } diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 6f7cd31265569..c2218b0a1f41a 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -294,6 +294,17 @@ enum LockState { Unlocked, } +/// A Unix user's own biometric user: the designation of its key bag +/// (`userbag::bag_designation`) and the uid whose catacomb file it uses. +/// A Touch ID match for one of that bag's keys runs in this user's domain, +/// as macOS matches against the logged-in user's catacomb: a match against +/// another user's templates does not satisfy the key. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct BioSubject { + pub(crate) user: i32, + pub(crate) uid: u32, +} + impl LockState { const fn wire(self) -> i32 { match self { @@ -743,6 +754,11 @@ struct SepData { #[pin] user_bags: Mutex, + // The biometric user a match addresses while a touch-gated call on a + // user-bag key waits (see `BioSubject`); none means the configured user. + #[pin] + bio_subject: Mutex>, + // The sensor stayed powered and registered after the last verify, so the // next verify captures without a new bring-up, as macOS does. Cleared by // anything that powers the sensor off or resets it. @@ -1223,6 +1239,7 @@ impl SepData { match_context_generation: Atomic::new(0), cold_match_contexts <- new_mutex!(KVec::new()), user_bags <- new_mutex!(userbag::LoadedBags::new()), + bio_subject <- new_mutex!(None), sensor_kept: Atomic::new(false), sensor_reset_in_capture: Atomic::new(false), bg_ops_ready: Atomic::new(false), diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index 99054c5e8caa3..ddaa944e7660c 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -252,28 +252,51 @@ fn encode_loaded_lock_state_13(bag: i32, state: TransitionState, secret: &[u8]) /// designation after its temporary handle is unloaded; on the 13.5 store the /// loaded-bag unlock needs the handle, so a user's bag stays loaded from /// import or create until logout. Keyed by the designation's special handle. -pub(crate) struct LoadedBags(KVec<(i32, crate::sks::KeyBagHandle)>); +/// +/// An entry also holds the user's Touch ID preparation once a password +/// unlock has made it: an SCRD context the enclave verified with the bag's +/// password and found TouchIdEnrollment-satisfied. It stays live until the +/// bag locks, and lets a match in that user's domain satisfy the bag's keys +/// without the password, as on macOS after the first unlock. The password +/// itself is never kept. +pub(crate) struct LoadedBags(KVec); + +struct LoadedBag { + user: i32, + handle: crate::sks::KeyBagHandle, + touch: Option<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>, +} impl LoadedBags { pub(crate) const fn new() -> Self { LoadedBags(KVec::new()) } + fn entry(&mut self, user: i32) -> Option<&mut LoadedBag> { + self.0.iter_mut().find(|b| b.user == user) + } + fn get(&self, user: i32) -> Option { - self.0.iter().find(|(u, _)| *u == user).map(|(_, h)| *h) + self.0.iter().find(|b| b.user == user).map(|b| b.handle) + } + + fn touch(&self, user: i32) -> Option<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]> { + self.0.iter().find(|b| b.user == user).and_then(|b| b.touch) } fn insert(&mut self, user: i32, handle: crate::sks::KeyBagHandle) -> Result<()> { if self.get(user).is_some() { return Err(EBUSY); } - self.0.push((user, handle), GFP_KERNEL)?; + self.0.push(LoadedBag { user, handle, touch: None }, GFP_KERNEL)?; Ok(()) } - fn remove(&mut self, user: i32) -> Option { - let at = self.0.iter().position(|(u, _)| *u == user)?; - Some(self.0.remove(at).ok()?.1) + /// Removes the entry; the caller disposes of any touch preparation. + fn remove(&mut self, user: i32) -> Option<(crate::sks::KeyBagHandle, Option<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>)> { + let at = self.0.iter().position(|b| b.user == user)?; + let b = self.0.remove(at).ok()?; + Some((b.handle, b.touch)) } } @@ -1213,7 +1236,7 @@ impl crate::SepData { out.reply.status == 0 && self.sks_uuid_from_reply(&out) == Some(loaded) } - fn userbag_sepos13(&self) -> bool { + pub(crate) fn userbag_sepos13(&self) -> bool { matches!(self.profile.key_store, crate::profile::KeyStore::Sepos13 { .. }) } @@ -1227,10 +1250,81 @@ impl crate::SepData { if !self.userbag_sepos13() { return self.userbag_transition(user, TransitionState::Unlocked, 0, secret); } - let bags = self.user_bags.lock(); - let bag = bags.get(user.special_handle().value()).ok_or(ENOENT)?; - self.userbag_loaded_lock_state_13(bag, TransitionState::Unlocked, secret)?; - self.userbag_transition(user, TransitionState::Unlocked, 0, secret) + { + let bags = self.user_bags.lock(); + let bag = bags.get(user.special_handle().value()).ok_or(ENOENT)?; + self.userbag_loaded_lock_state_13(bag, TransitionState::Unlocked, secret)?; + self.userbag_transition(user, TransitionState::Unlocked, 0, secret)?; + } + // The password is accepted; Touch ID for this user is a bonus that + // never fails the unlock. Prepared without `user_bags` held: a touch + // takes `catacomb_writer` before `user_bags`, so this must not take + // them the other way round. + let touch = self.userbag_prepare_touch_13(user, secret); + let mut bags = self.user_bags.lock(); + if let Some(entry) = bags.entry(user.special_handle().value()) { + if let Some(old) = core::mem::replace(&mut entry.touch, touch) { + let _ = self.delete_unlock_evaluation(&old); + } + } else if let Some(t) = touch { + let _ = self.delete_unlock_evaluation(&t); + } + Ok(()) + } + + /// After a password unlock on the 13.5 store: loads the user's catacomb + /// from its file and makes the Touch ID preparation (see `LoadedBags`). + /// None when the user has no templates or the enclave refuses. + fn userbag_prepare_touch_13( + &self, + user: crate::sks::DesignateUser, + secret: &[u8], + ) -> Option<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]> { + let id = -user.special_handle().value(); + let uid = u32::try_from(id).ok()?.checked_sub(USER_BAG_BASE)?; + { + let _writer = self.catacomb_writer.lock(); + let raw = self.read_component_states()?; + let state = crate::sbio::ComponentStates::new(&raw).state_for(id); + let outcome = self.restore_catacomb(state, id, crate::catacomb::Slot::BagUser(uid), c"bag user catacomb"); + if !matches!(outcome, crate::RestoreOutcome::AlreadyActive | crate::RestoreOutcome::Restored) { + dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: its catacomb did not load ({:?})\n", uid, outcome); + return None; + } + } + if self.enclave_identities_for(id).is_none_or(|ids| ids.is_empty()) { + return None; + } + let (handle, status) = self.key_context_for(id, Some((user.special_handle(), secret))).ok()?; + let ready = status == 0 + && matches!(self.scrd_command(&crate::scrd::scrd_verify_touchid_enrollment(&handle)), + Some((0, ref reply)) if reply.get(..4) == Some(&1u32.to_le_bytes())); + if !ready { + let _ = self.delete_unlock_evaluation(&handle); + dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: the preparation was refused\n", uid); + return None; + } + dev_info_ratelimited!(self.dev, "userbag: Touch ID ready for uid {}\n", uid); + Some(handle) + } + + /// Touch ID for a key under `uid`'s bag (13.5 store): the preparation + /// must exist, i.e. the password has unlocked the bag since it last + /// locked, and the enclave must still find it TouchIdEnrollment-satisfied. + pub(crate) fn userbag_touch_subject(&self, uid: u32) -> Result> { + if !self.userbag_sepos13() { + return Ok(None); + } + let user = bag_designation(uid)?; + let Some(handle) = self.user_bags.lock().touch(user.special_handle().value()) else { + return Err(EACCES); + }; + let ready = matches!(self.scrd_command(&crate::scrd::scrd_verify_touchid_enrollment(&handle)), + Some((0, ref reply)) if reply.get(..4) == Some(&1u32.to_le_bytes())); + if !ready { + return Err(EACCES); + } + Ok(Some(crate::BioSubject { user: -user.special_handle().value(), uid })) } fn userbag_loaded_lock_state_13( @@ -1264,7 +1358,15 @@ impl crate::SepData { const FIRES: u32 = 15; // Not held across the requests and the waits between fires: every // other bag and Touch ID path takes this lock. - let bag = self.user_bags.lock().get(user.special_handle().value()).ok_or(ENOENT)?; + let (bag, touch) = { + let mut bags = self.user_bags.lock(); + let bag = bags.get(user.special_handle().value()).ok_or(ENOENT)?; + (bag, bags.entry(user.special_handle().value()).and_then(|e| e.touch.take())) + }; + // Touch ID ends with the lock, whatever the store answers. + if let Some(t) = touch { + let _ = self.delete_unlock_evaluation(&t); + } self.userbag_transition(user, TransitionState::Locked, flags, &[])?; self.userbag_loaded_lock_state_13(bag, TransitionState::Locked, &[])?; for fire in 0..FIRES { @@ -1297,7 +1399,10 @@ impl crate::SepData { fn userbag_logout_13(&self, user: crate::sks::DesignateUser) -> Result<()> { let locked = self.userbag_lock_13(user, 0); let removed = self.user_bags.lock().remove(user.special_handle().value()); - if let Some(bag) = removed { + if let Some((bag, touch)) = removed { + if let Some(t) = touch { + let _ = self.delete_unlock_evaluation(&t); + } let unloaded = self.sks_send(self.sks_req_unload_keybag(bag)); if !matches!(unloaded, Some(ref out) if out.reply.status == 0) { dev_info_ratelimited!(self.dev, "userbag: unloading {}'s bag at logout failed\n", user.special_handle().value()); @@ -1885,7 +1990,7 @@ mod tests { assert_eq!(bags.insert(USER_HANDLE, h).unwrap_err(), EBUSY); assert_eq!(bags.get(USER_HANDLE).unwrap().value(), 3); assert!(bags.get(USER_HANDLE - 1).is_none()); - assert_eq!(bags.remove(USER_HANDLE).unwrap().value(), 3); + assert_eq!(bags.remove(USER_HANDLE).unwrap().0.value(), 3); assert!(bags.remove(USER_HANDLE).is_none()); } diff --git a/tools/aurora-sep/tests/test_identity_preservation.py b/tools/aurora-sep/tests/test_identity_preservation.py index ef2e5b04f6a02..b468d61274c39 100644 --- a/tools/aurora-sep/tests/test_identity_preservation.py +++ b/tools/aurora-sep/tests/test_identity_preservation.py @@ -183,6 +183,10 @@ def body(source, signature): CATACOMB = r''' const MAGIC:[u8;8]=*b"SEPCTMB1";const HEADER:usize=16;const MAX_CATACOMB:usize=1<<20; +struct CString(std::ffi::CString); +impl CString{fn try_from_fmt(s:String)->Result{Ok(Self(std::ffi::CString::new(s).unwrap()))}} +impl Deref for CString{type Target=CStr;fn deref(&self)->&CStr{&self.0}} +macro_rules! fmt {($($x:tt)*)=>{format!($($x)*)};} const PRIVATE_TYPE_CATACOMB_MASTER:u8=1;const PRIVATE_TYPE_CATACOMB_OWNER:u8=2;const PRIVATE_TYPE_CATACOMB_USER:u8=3; thread_local!{static OPEN:Cell=const{Cell::new(0)};static IMAGE:RefCell>=const{RefCell::new(Vec::new())};} mod shim{ @@ -233,8 +237,11 @@ def body(source, signature): } mod catacomb{ use super::*; + pub enum Slot{Kind(u8),BagUser(u32)} + impl From for Slot{fn from(kind:u8)->Self{Slot::Kind(kind)}} pub fn is_kind(_:u8)->bool{true} - pub fn read(kind:u8)->Result>>{ + pub fn read(slot:impl Into)->Result>>{ + let Slot::Kind(kind)=slot.into() else {return Ok(None)}; match FILE.with(|f|f.borrow()[(kind-PRIVATE_TYPE_CATACOMB_MASTER) as usize]){ 0=>Ok(Some(KVec(vec![1,0,0,0]))),ENOENT=>Ok(None),e=>Err(e)} } @@ -341,8 +348,11 @@ def test_refused_keybag_requires_canonical_writer_record(self): def test_catacomb_read_error_is_not_absence(self): text=(ROOT/'catacomb.rs').read_text() - method=body(text,'pub(crate) fn read(kind: u8)') - path=body(text,'fn path_for(kind: u8)').replace('crate::','') + method=body(text,'pub(crate) fn read(slot: impl Into)') + path='\n'.join(body(text,item) for item in ( + 'pub(crate) enum Slot {','impl From for Slot {','enum Path {', + 'impl core::ops::Deref for Path {','fn fixed_path(kind: u8)', + 'fn path_for(slot: impl Into)')).replace('crate::','') crc=body((ROOT/'store.rs').read_text(),'pub(crate) const fn crc16_ccitt_false(') make=lambda m:CATACOMB.replace('METHOD',m).replace('PATH',path).replace('@@CRC@@',crc) self.run_rust(make(method)) @@ -353,7 +363,7 @@ def test_catacomb_read_error_is_not_absence(self): def test_callers_refuse_unreadable_catacombs(self): cat=(ROOT/'catacomb.rs').read_text();sb=(ROOT/'sbio.rs').read_text() parts={'IS_SAVED':body(cat,'pub(crate) fn is_saved('),'IS_ABSENT':body(cat,'pub(crate) fn is_absent('), - 'RESTORE':body(sb,' fn restore_catacomb('),'READ_STORED':body(sb,' fn read_stored('), + 'RESTORE':body(sb,'fn restore_catacomb('),'READ_STORED':body(sb,' fn read_stored('), 'PROBE':body(sb,' fn probe_owner_export(')} def make(**over): text=CALLERS @@ -362,7 +372,7 @@ def make(**over): return text self.run_rust(make()) restore=parts['RESTORE'].replace('Err(_) => return RestoreOutcome::Failed,','Err(_) => return RestoreOutcome::NoStoredFile,',1) - absent=parts['IS_ABSENT'].replace('matches!(read(kind), Ok(None))','!matches!(read(kind), Ok(Some(_)))',1) + absent=parts['IS_ABSENT'].replace('matches!(read(slot), Ok(None))','!matches!(read(slot), Ok(Some(_)))',1) for name,old in (('RESTORE',restore),('IS_ABSENT',absent)): self.assertNotEqual(old,parts[name]) self.run_rust(make(**{name:old}),should_pass=False) From 7704abe5259d335200c151b928e7faf7652760de Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sat, 14 Nov 2026 12:33:56 -0600 Subject: [PATCH 10/24] soc: apple: sep: enrol into a Unix user's own domain A v5 enrolment carries the resolved Unix account, but it always went into the configured user's domain, whose templates cannot satisfy keys under that account's own bag. When the account's bag is loaded on the 13.5 store, run the enrolment with that bag user as the biometric subject: - its first enrolment selects a context scoped to that user alone, proved by zero identities, and never SYSTEM, which would also reselect the shared master; later ones activate the component the unlock restored; - BEGIN_ENROL is authorized by the credential the user's own password unlock prepared, not the identity bag's material; - the user's catacomb is saved first, then any other dirty user, the shared master and the lockout record. Success is reported only once nothing is save-pending; - the identity is not filed in the shared index, which is reconciled against the configured user. Matches check it against the enclave's list for that user. A bag that is loaded but locked refuses the enrolment, as macOS asks for the password to add a finger. An account with no loaded bag keeps the configured user, as before. A password unlock now prepares the credential for a user with no templates yet, so their first enrolment can run. On J293, an ENROL_START for the account completed into the bag user's domain (two identities), saved its catacomb file, and one touch of the new finger then signed a user-presence key under the bag. The configured user's enrolments were unchanged. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/bio.rs | 12 ++++ drivers/soc/apple/key.rs | 19 +----- drivers/soc/apple/sbio.rs | 119 ++++++++++++++++++++++++++++++++--- drivers/soc/apple/sep.rs | 18 ++++++ drivers/soc/apple/userbag.rs | 46 +++++++++++--- 5 files changed, 180 insertions(+), 34 deletions(-) diff --git a/drivers/soc/apple/bio.rs b/drivers/soc/apple/bio.rs index d2567ab59650f..fb09ffa515ac6 100644 --- a/drivers/soc/apple/bio.rs +++ b/drivers/soc/apple/bio.rs @@ -520,6 +520,8 @@ enum Op { Idle, Enrol { label: [u8; LABEL_LEN], + /// The resolved Unix account the enrolment is for. + account_uid: u32, stage: u32, terminal: Option, guidance: Guidance, @@ -757,6 +759,7 @@ mod enrol_request_tests { session.start_worker().unwrap(); session.op = Op::Enrol { label: [0; LABEL_LEN], + account_uid: 1000, stage: 0, terminal: None, guidance: Guidance::Place, @@ -835,6 +838,7 @@ fn enrol_start(ctx: &mut Context<'_>, user: UserPtr) -> Result { ctx.session.start_worker()?; ctx.session.op = Op::Enrol { label: request.label, + account_uid: request.account_uid, stage: 0, terminal: None, guidance: Guidance::Place, @@ -853,6 +857,14 @@ fn enrol_start(ctx: &mut Context<'_>, user: UserPtr) -> Result { }) } +/// The account of the enrolment the worker runs, if it runs one. +pub(crate) fn enrol_account(session: &Session) -> Option { + match session.op { + Op::Enrol { account_uid, terminal: None, .. } => Some(account_uid), + _ => None, + } +} + pub(crate) fn enrol_advance( session: &mut Session, epoch: u64, diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index 9b2bba527856e..36174b55994d7 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -911,23 +911,6 @@ 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. -/// Routes Touch ID to a bag user while a touch-gated call waits; the -/// configured user again when it ends, on every path. -struct SubjectScope<'a>(&'a SepData); - -impl<'a> SubjectScope<'a> { - fn new(sep: &'a SepData, subject: crate::BioSubject) -> Self { - *sep.bio_subject.lock() = Some(subject); - SubjectScope(sep) - } -} - -impl Drop for SubjectScope<'_> { - fn drop(&mut self) { - *self.0.bio_subject.lock() = None; - } -} - pub(crate) struct KeyGrant { op: TouchOp, /// The user bag the record lives under (`HANDLE_USER_BAG`), if any. @@ -1626,7 +1609,7 @@ impl SepData { // only after its password unlocked the bag (13.5 store). let _subject = match job.grant.as_ref().and_then(|g| g.bag()) { Some(uid) => match self.userbag_touch_subject(uid)? { - Some(subject) => Some(SubjectScope::new(self, subject)), + Some(subject) => Some(crate::SubjectScope::new(self, subject)), None => None, }, None => None, diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 795216f63f730..906b08b9b0f07 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -984,7 +984,7 @@ impl SepData { fn stash_enrol_identity(&self, record: &[u8]) { let mut found: KVec<[u8; bio::UUID_LEN]> = KVec::new(); - crate::sbio::enrol_identity_candidates(record, SBIO_PROBE_USER_ID, |_at, uuid| { + crate::sbio::enrol_identity_candidates(record, self.bio_user(), |_at, uuid| { let _ = found.push(uuid, GFP_KERNEL); }); *self.enrol_identity_candidates.lock() = found; @@ -992,7 +992,7 @@ impl SepData { fn identity_just_enrolled(&self) -> Option<[u8; bio::UUID_LEN]> { let candidates = core::mem::take(&mut *self.enrol_identity_candidates.lock()); - let listed = self.enclave_identities_for(SBIO_PROBE_USER_ID)?; + let listed = self.enclave_identities_for(self.bio_user())?; let mut confirmed: KVec<[u8; bio::UUID_LEN]> = KVec::new(); for uuid in candidates.iter() { @@ -1112,6 +1112,19 @@ impl SepData { } fn finish_enrolment(&self, outcome: core::result::Result<[u8; bio::UUID_LEN], u32>) { + // A bag user's identities are not the configured user's: the shared + // index (reconciled against the configured user) never holds them. + // Matches check them against the enclave's own list for that user. + if self.bio_subject.lock().is_some() { + let mut scratch = bio::IdentityIndex::new(); + let filed = bio::enrol_finish( + &mut self.bio_session.lock(), self.capture_epoch.load(Relaxed), &mut scratch, outcome, + ); + if filed { + self.bio_wake(); + } + return; + } let filed = { let mut session = self.bio_session.lock(); let mut index = self.bio_index.lock(); @@ -1188,6 +1201,9 @@ impl SepData { } fn open_enrolment_context(&self, user: crate::sbio::UserId) -> bool { + if self.bio_subject.lock().is_some() { + return self.open_bag_user_context(user); + } let listed = match self.enclave_identities_for(SBIO_PROBE_USER_ID) { Some(list) => list.len(), None => { @@ -1209,6 +1225,24 @@ impl SepData { self.open_fresh_context(user, &proof) } + /// A bag user's enrolment context. Their first enrolment selects a + /// context scoped to that user alone, proved by zero identities; never + /// SYSTEM, which would also reselect the shared master. Later ones + /// activate the component their unlock restored. + fn open_bag_user_context(&self, user: crate::sbio::UserId) -> bool { + let Some(ids) = self.enclave_identities_for(user.value()) else { + return false; + }; + if let Some(proof) = crate::sbio::NoExistingCatacomb::from_zero_identities(ids.len()) { + let select = crate::sbio::sbio_select_context(crate::sbio::ContextScope::user(user), &proof); + if self.sbio_enrol_step(&select).is_none() { + dev_err_ratelimited!(self.dev, "enrol: selecting user {}'s first context was refused\n", user.value()); + return false; + } + } + self.activate_protected_config(user) + } + fn enrol_into_existing_context(&self, user: crate::sbio::UserId) -> bool { if !self.activate_protected_config(user) { return false; @@ -1366,18 +1400,21 @@ impl SepData { let _ = self.sbio_call(&crate::sbio::sbio_cancel_operation()); } - let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + let bag_user = self.bio_subject.lock().is_some(); + let Some(user) = crate::sbio::UserId::new(self.bio_user()) else { dev_err_ratelimited!(self.dev, "enrol: invalid probe user id\n"); return None; }; // 0x03 answers -3 until a user key bag is designated - if !self.keybag_designated.load(Relaxed) { + // A bag user's enrolment is authorized by their own password unlock, + // not by the identity bag's material. + if !bag_user && !self.keybag_designated.load(Relaxed) { dev_err_ratelimited!(self.dev, "enrol: no user keybag was designated\n"); return None; } - if self.enrol_material.lock().is_none() { + if !bag_user && self.enrol_material.lock().is_none() { dev_err_ratelimited!(self.dev, "enrol: no enrolment material is held\n"); return None; } @@ -1388,7 +1425,12 @@ impl SepData { return None; } - let Some(acm_handle) = self.establish_passcode_validated_context(user) else { + let credential = if bag_user { + self.userbag_subject_credential() + } else { + self.establish_passcode_validated_context(user) + }; + let Some(acm_handle) = credential else { dev_err_ratelimited!( self.dev, "enrol: could not establish the PasscodeValidated SCRD credential; not falling back to a transient SKS token (a catacomb sealed under one does not survive a reboot)\n" @@ -1753,6 +1795,51 @@ impl SepData { /// `Ok(None)` means provably not stored; any read failure is an error so /// it cannot be mistaken for a missing file. + /// After a bag user's enrolment: their own catacomb first, then any + /// other dirty user, the shared master, and the lockout record. Nothing + /// is reported saved while any component is still save-pending. + fn save_bag_user_components(&self, user: crate::sbio::UserId) -> bool { + let Some(configured) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + return false; + }; + for (who, slot, required, what) in [ + (crate::sbio::CatacombUser::enrolling(user), self.bio_user_slot(), true, c"bag user catacomb"), + (crate::sbio::CatacombUser::enrolling(configured), crate::catacomb::Slot::Kind(PRIVATE_TYPE_CATACOMB_USER), false, c"user catacomb"), + (crate::sbio::CatacombUser::OWNER, crate::catacomb::Slot::Kind(PRIVATE_TYPE_CATACOMB_OWNER), false, c"owner catacomb"), + (crate::sbio::CatacombUser::MASTER, crate::catacomb::Slot::Kind(PRIVATE_TYPE_CATACOMB_MASTER), true, c"master catacomb"), + ] { + let Some(raw) = self.read_component_states() else { + return false; + }; + let state = crate::sbio::ComponentStates::new(&raw).state_for(who.value()); + let Some(state) = state else { + if required { + dev_err_ratelimited!(self.dev, "enrol: the {} is absent after the enrolment\n", what); + return false; + } + continue; + }; + if (required || state & crate::sbio::COMPONENT_STATE_SAVE_PENDING != 0) + && !self.save_catacomb(who, slot, what) + { + dev_err_ratelimited!(self.dev, "enrol: saving the {} failed\n", what); + return false; + } + if required && !crate::catacomb::is_saved(slot) { + return false; + } + } + let Some(raw) = self.read_component_states() else { + return false; + }; + let states = crate::sbio::ComponentStates::new(&raw); + if (0..states.count()).any(|i| states.pair(i).is_some_and(|(_, st)| st & crate::sbio::COMPONENT_STATE_SAVE_PENDING != 0)) { + dev_err_ratelimited!(self.dev, "enrol: a component is still save-pending after the bag user's enrolment\n"); + return false; + } + self.save_lockout() + } + fn read_stored(&self, kind: impl Into, what: &CStr) -> Result>> { let kind = match kind.into() { crate::catacomb::Slot::Kind(kind) => kind, @@ -1784,6 +1871,9 @@ impl SepData { } fn save_all_components(&self, user: crate::sbio::UserId) -> bool { + if self.bio_subject.lock().is_some() { + return self.save_bag_user_components(user); + } if self.profile.persistent_enrol || module_parameters::j414s_persistent_enrol.value() != 0 { return self.save_all_components_user_first(user); } @@ -3255,6 +3345,19 @@ impl SepData { } pub(crate) fn run_enrolment(&self) { + // An enrolment for a Unix account whose bag is loaded goes into that + // user's own domain (13.5 store); one whose bag is locked waits for + // the password. + let account = bio::enrol_account(&self.bio_session.lock()); + let _subject = match account.map(|uid| self.userbag_enrol_subject(uid)) { + Some(Err(_)) => { + dev_info_ratelimited!(self.dev, "enrol: the account's key bag is locked; its password is needed to add a finger\n"); + self.finish_enrolment(Err(ENROL_STATUS_ENCLAVE)); + return; + } + Some(Ok(Some(subject))) => Some(crate::SubjectScope::new(self, subject)), + _ => None, + }; self.end_match_scope(c"an enrolment"); self.enrol_frames_accepted.store(0, Relaxed); self.sensor_kept.store(false, Relaxed); @@ -3367,7 +3470,7 @@ impl SepData { if enrolment_completed { open.completed(); - if let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) { + if let Some(user) = crate::sbio::UserId::new(self.bio_user()) { if !self.save_all_components(user) { dev_err_ratelimited!( self.dev, @@ -4251,7 +4354,7 @@ impl SepData { // as it has it, while the enclave processes the frame. self.pause_after_touch(c"enrol"); - let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + let Some(user) = crate::sbio::UserId::new(self.bio_user()) else { dev_err_ratelimited!(self.dev, "enrol: invalid user id before image processing\n"); return ImageOutcome::Failed(ENROL_STATUS_ENCLAVE); }; diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index c2218b0a1f41a..7fb33812652d0 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -305,6 +305,24 @@ pub(crate) struct BioSubject { pub(crate) uid: u32, } +/// Routes Touch ID to a bag user while a touch-gated call or an enrolment +/// runs, under `catacomb_writer`; the configured user again when it ends, on +/// every path. +pub(crate) struct SubjectScope<'a>(&'a SepData); + +impl<'a> SubjectScope<'a> { + pub(crate) fn new(sep: &'a SepData, subject: BioSubject) -> Self { + *sep.bio_subject.lock() = Some(subject); + SubjectScope(sep) + } +} + +impl Drop for SubjectScope<'_> { + fn drop(&mut self) { + *self.0.bio_subject.lock() = None; + } +} + impl LockState { const fn wire(self) -> i32 { match self { diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index ddaa944e7660c..2a87939a7e2f5 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -1286,15 +1286,19 @@ impl crate::SepData { let _writer = self.catacomb_writer.lock(); let raw = self.read_component_states()?; let state = crate::sbio::ComponentStates::new(&raw).state_for(id); - let outcome = self.restore_catacomb(state, id, crate::catacomb::Slot::BagUser(uid), c"bag user catacomb"); - if !matches!(outcome, crate::RestoreOutcome::AlreadyActive | crate::RestoreOutcome::Restored) { - dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: its catacomb did not load ({:?})\n", uid, outcome); - return None; + let slot = crate::catacomb::Slot::BagUser(uid); + // A user who never enrolled has neither a component nor a file: + // nothing to load, and the preparation still authorizes the first + // enrolment. Either one without the other is not that. + let fresh = state.is_none() && crate::catacomb::is_absent(slot); + if !fresh { + let outcome = self.restore_catacomb(state, id, slot, c"bag user catacomb"); + if !matches!(outcome, crate::RestoreOutcome::AlreadyActive | crate::RestoreOutcome::Restored) { + dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: its catacomb did not load ({:?})\n", uid, outcome); + return None; + } } } - if self.enclave_identities_for(id).is_none_or(|ids| ids.is_empty()) { - return None; - } let (handle, status) = self.key_context_for(id, Some((user.special_handle(), secret))).ok()?; let ready = status == 0 && matches!(self.scrd_command(&crate::scrd::scrd_verify_touchid_enrollment(&handle)), @@ -1304,10 +1308,36 @@ impl crate::SepData { dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: the preparation was refused\n", uid); return None; } - dev_info_ratelimited!(self.dev, "userbag: Touch ID ready for uid {}\n", uid); + dev_info_ratelimited!(self.dev, "userbag: Touch ID and enrolment ready for uid {}\n", uid); Some(handle) } + /// The subject of an enrolment for the Unix account `uid` (13.5 store): + /// that user's own domain when their bag is loaded. Refused while it is + /// loaded but not unlocked by the password since its last lock, as macOS + /// asks for the password to add a finger. None (no bag) keeps the + /// configured user. + pub(crate) fn userbag_enrol_subject(&self, uid: u32) -> Result> { + if !self.userbag_sepos13() || uid < MIN_UID { + return Ok(None); + } + let user = bag_designation(uid)?; + let bags = self.user_bags.lock(); + if bags.get(user.special_handle().value()).is_none() { + return Ok(None); + } + if bags.touch(user.special_handle().value()).is_none() { + return Err(EACCES); + } + Ok(Some(crate::BioSubject { user: -user.special_handle().value(), uid })) + } + + /// The current subject's password-verified credential, for BEGIN_ENROL. + pub(crate) fn userbag_subject_credential(&self) -> Option<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]> { + let subject = (*self.bio_subject.lock())?; + self.user_bags.lock().touch(-subject.user) + } + /// Touch ID for a key under `uid`'s bag (13.5 store): the preparation /// must exist, i.e. the password has unlocked the bag since it last /// locked, and the enclave must still find it TouchIdEnrollment-satisfied. From 72a9b18d418acf740b7cff0736d809729545270e Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sat, 14 Nov 2026 13:14:38 -0600 Subject: [PATCH 11/24] soc: apple: sep: verify for a named Unix account SEP_BIO_VERIFY_START carried no account, so every verify matched the built-in Linux user, even for an account whose fingers live in its own key bag's domain. Add SEP_BIO_VERIFY_ACCOUNT. With it, the request's account_uid (the word that was reserved) is the resolved Unix account: - while that account's bag is loaded and unlocked by its password, the match runs in that user's own domain, and the configured user's after-boot and lockout gates do not apply; - while it is loaded but locked, the verify fails without a capture with the lockout status, so pam_fprintd asks for the password; - with no loaded bag, the built-in user matches as before. The after-boot and lockout gates track the configured user: an account's own domain is used only once that account's bag has been unlocked by its password since boot, which the after-boot gate asks for, and the lockout record kept here is the configured user's. A verify that names an account is not short-circuited by the configured user's empty index. Where per_user_keybags allows user bags, GET_INFO advertises the flag in reserved[0] (SEP_BIO_INFO_VERIFY_ACCOUNT); elsewhere the flag is refused with EINVAL. The interface version stays 5, so existing libfprint keeps working, and older kernels refuse the flag with EINVAL. On J293, an account verify with a finger enrolled only in that user's domain matched; after the bag locked it was refused at once with status 8; a plain verify with a configured finger still matched. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/bio.rs | 42 +++++++++++++++++++++++++----- drivers/soc/apple/sbio.rs | 35 ++++++++++++++++++++++--- drivers/soc/apple/sep_ioctl_test.c | 13 +++++++-- drivers/soc/apple/userbag.rs | 10 +++---- include/uapi/linux/apple-sep.h | 14 +++++++++- 5 files changed, 96 insertions(+), 18 deletions(-) diff --git a/drivers/soc/apple/bio.rs b/drivers/soc/apple/bio.rs index fb09ffa515ac6..1cc392c08ef89 100644 --- a/drivers/soc/apple/bio.rs +++ b/drivers/soc/apple/bio.rs @@ -161,10 +161,15 @@ struct EnrolPoll { #[repr(C)] struct VerifyStart { flags: u32, - reserved: u32, + account_uid: u32, nonce: [u8; NONCE_LEN], } +/// `VerifyStart.flags`: `account_uid` is the Unix account to match for. +const VERIFY_ACCOUNT: u32 = 1; +/// `Info.reserved[0]`: this kernel takes `VERIFY_ACCOUNT`. +const INFO_VERIFY_ACCOUNT: u32 = 2; + #[repr(C)] #[derive(Default)] struct VerifyPoll { @@ -529,6 +534,8 @@ enum Op { }, Verify { nonce: [u8; NONCE_LEN], + /// The Unix account to match for (`VERIFY_ACCOUNT`), if named. + account: Option, terminal: Option, guidance: Guidance, }, @@ -608,6 +615,9 @@ pub(crate) struct Context<'a> { pub(crate) index: &'a IdentityIndex, pub(crate) sensor_present: bool, pub(crate) live_identity_count: Option, + /// Whether a verify may name a Unix account: only where per-user key + /// bags exist (`PlatformProfile::per_user_keybags`). + pub(crate) verify_account: bool, } pub(crate) struct Handled { @@ -680,7 +690,8 @@ fn get_info(ctx: &mut Context<'_>, user: UserPtr) -> Result { // when SEP itself successfully answered LIST_IDENTITIES; older // kernels and unavailable SEP contexts leave this flag clear. reserved: [ - u32::from(ctx.live_identity_count.is_some()), + u32::from(ctx.live_identity_count.is_some()) + | if ctx.verify_account { INFO_VERIFY_ACCOUNT } else { 0 }, ctx.live_identity_count.unwrap_or(0) as u32, 0, ], @@ -858,6 +869,14 @@ fn enrol_start(ctx: &mut Context<'_>, user: UserPtr) -> Result { } /// The account of the enrolment the worker runs, if it runs one. +/// The account a running verify names (`VERIFY_ACCOUNT`), if any. +pub(crate) fn verify_account(session: &Session) -> Option { + match session.op { + Op::Verify { account, terminal: None, .. } => account, + _ => None, + } +} + pub(crate) fn enrol_account(session: &Session) -> Option { match session.op { Op::Enrol { account_uid, terminal: None, .. } => Some(account_uid), @@ -1047,19 +1066,26 @@ fn verify_start(ctx: &mut Context<'_>, user: UserPtr) -> Result { let request: VerifyStart = UserSlice::new(user, core::mem::size_of::()) .reader() .read()?; - if request.flags != 0 || request.reserved != 0 { - return Err(EINVAL); - } + // VERIFY_ACCOUNT names the Unix account in `account_uid`: the match runs + // in that user's own domain when their key bag is loaded. + let account = match request.flags { + 0 if request.account_uid == 0 => None, + VERIFY_ACCOUNT if ctx.verify_account && request.account_uid != u32::MAX => Some(request.account_uid), + _ => return Err(EINVAL), + }; require_sensor(ctx.sensor_present)?; if ctx.session.worker_epoch.is_some() || !ctx.session.op.may_start() { return Err(EBUSY); } - if ctx.index.total() == 0 { + // The index holds the configured user's templates only; an account's + // own templates are in the enclave, so it is never empty for them here. + if account.is_none() && ctx.index.total() == 0 { ctx.session.token = None; ctx.session.op = Op::Verify { nonce: request.nonce, + account: None, terminal: Some(VerifyOutcome::NoMatch), guidance: Guidance::None, }; @@ -1078,6 +1104,7 @@ fn verify_start(ctx: &mut Context<'_>, user: UserPtr) -> Result { ctx.session.token = None; ctx.session.op = Op::Verify { nonce: request.nonce, + account, terminal: None, guidance: Guidance::Place, }; @@ -1155,7 +1182,7 @@ fn verify_poll(ctx: &mut Context<'_>, user: UserPtr) -> Result { match &ctx.session.op { Op::Idle => out.state = State::Idle, Op::Enrol { .. } => return Err(EBUSY), - Op::Verify { nonce, terminal, guidance } => { + Op::Verify { nonce, terminal, guidance, .. } => { out.guidance = *guidance; match terminal { None => { @@ -1361,6 +1388,7 @@ pub unsafe extern "C" fn sep_kunit_bio_ioctl(bio: *mut c_void, cmd: c_uint, arg: index: &bio.index, sensor_present: true, live_identity_count: Some(0), + verify_account: true, }; match ioctl(&mut ctx, cmd, arg as usize) { Ok(h) => h.ret as c_long, diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 906b08b9b0f07..c3cca1bc38221 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -2712,10 +2712,38 @@ impl SepData { } pub(crate) fn run_verify(&self) { + // A verify for an account whose bag is loaded matches in that user's + // own domain; one whose bag is locked needs the password first. A + // key touch already runs under its key's subject. + let account = if self.key_touch_active.load(Relaxed) { + None + } else { + bio::verify_account(&self.bio_session.lock()) + }; + let subject = match account.map(|uid| self.userbag_bio_subject(uid)) { + Some(Err(_)) => { + dev_info_refusal!(self.dev, "verify: the account's key bag is locked; refusing without a capture until its password unlocks it\n"); + self.finish_verify(bio::VerifyOutcome::Failed(ENROL_STATUS_LOCKOUT), [0u8; bio::TOKEN_LEN]); + return; + } + Some(Ok(Some(subject))) => Some(crate::SubjectScope::new(self, subject)), + _ => None, + }; + // The gates below are the configured user's. A bag user's own lock + // and the enclave's per-user count stand in for them. + let bag_user = subject.is_some(); + // A bag user with nothing enrolled cannot match, and the enclave + // refuses a match against them: answer no match without a capture, + // as an empty index is answered. fprintd's duplicate check before a + // first enrolment asks exactly this. + if bag_user && self.enclave_identities_for(self.bio_user()).is_some_and(|ids| ids.is_empty()) { + self.finish_verify(bio::VerifyOutcome::NoMatch, [0u8; bio::TOKEN_LEN]); + return; + } // Asked to wait for the first password since boot, as macOS does // after a restart: nothing is wrong with the finger or the enclave, // so refuse at once, before any capture. - if self.bio_after_boot.load(Relaxed) { + if !bag_user && self.bio_after_boot.load(Relaxed) { if !self.bio_lockout_logged.xchg(true, Relaxed) { dev_info_refusal!( self.dev, @@ -2730,7 +2758,7 @@ impl SepData { } // A capture now would only be refused at MATCH_RESULT again, so do // not ask the user to touch the sensor for it. - if self.bio_lockout.load(Relaxed) { + if !bag_user && self.bio_lockout.load(Relaxed) { if !self.bio_lockout_logged.xchg(true, Relaxed) { dev_info_refusal!( self.dev, @@ -3349,7 +3377,7 @@ impl SepData { // user's own domain (13.5 store); one whose bag is locked waits for // the password. let account = bio::enrol_account(&self.bio_session.lock()); - let _subject = match account.map(|uid| self.userbag_enrol_subject(uid)) { + let _subject = match account.map(|uid| self.userbag_bio_subject(uid)) { Some(Err(_)) => { dev_info_ratelimited!(self.dev, "enrol: the account's key bag is locked; its password is needed to add a finger\n"); self.finish_enrolment(Err(ENROL_STATUS_ENCLAVE)); @@ -6021,6 +6049,7 @@ impl SepData { index: &index, sensor_present, live_identity_count, + verify_account: self.profile.per_user_keybags, }; bio::ioctl(&mut ctx, cmd, arg)? }; diff --git a/drivers/soc/apple/sep_ioctl_test.c b/drivers/soc/apple/sep_ioctl_test.c index 1df385e658290..ad41efebfc3ac 100644 --- a/drivers/soc/apple/sep_ioctl_test.c +++ b/drivers/soc/apple/sep_ioctl_test.c @@ -400,14 +400,23 @@ static void sep_bio_session_rules(struct kunit *test) KUNIT_ASSERT_EQ(test, sep_ioctl_map(test, &m), 0); bio = sep_kunit_bio_new(); KUNIT_ASSERT_NOT_NULL(test, bio); - /* VERIFY_START: flags and reserved must be zero. */ - req[0] = 1; + /* + * VERIFY_START: no flag but VERIFY_ACCOUNT, and an account only with + * it. (This session offers VERIFY_ACCOUNT.) + */ + req[0] = 2; put(test, &m, 1, req, 40); KUNIT_EXPECT_EQ(test, sep_kunit_bio_ioctl(bio, SB_VERIFY_START, m.base + 1), -EINVAL); memset(req, 0, sizeof(req)); req[4] = 1; put(test, &m, 1, req, 40); KUNIT_EXPECT_EQ(test, sep_kunit_bio_ioctl(bio, SB_VERIFY_START, m.base + 1), -EINVAL); + /* VERIFY_ACCOUNT with the reserved "no account" uid. */ + memset(req, 0, sizeof(req)); + req[0] = 1; + memset(req + 4, 0xff, 4); + put(test, &m, 1, req, 40); + KUNIT_EXPECT_EQ(test, sep_kunit_bio_ioctl(bio, SB_VERIFY_START, m.base + 1), -EINVAL); /* ENROL_START: finger 0, 11, a label without the FP1- form. */ memset(req, 0, sizeof(req)); put_unaligned_le32(1000, req + 4); diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index 2a87939a7e2f5..e5c19ac10e927 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -1312,12 +1312,12 @@ impl crate::SepData { Some(handle) } - /// The subject of an enrolment for the Unix account `uid` (13.5 store): - /// that user's own domain when their bag is loaded. Refused while it is - /// loaded but not unlocked by the password since its last lock, as macOS - /// asks for the password to add a finger. None (no bag) keeps the + /// The subject of an enrolment or a verify for the Unix account `uid` + /// (13.5 store): that user's own domain when their bag is loaded. Refused + /// while it is loaded but not unlocked by the password since its last + /// lock, as macOS asks for the password then. None (no bag) keeps the /// configured user. - pub(crate) fn userbag_enrol_subject(&self, uid: u32) -> Result> { + pub(crate) fn userbag_bio_subject(&self, uid: u32) -> Result> { if !self.userbag_sepos13() || uid < MIN_UID { return Ok(None); } diff --git a/include/uapi/linux/apple-sep.h b/include/uapi/linux/apple-sep.h index 2b949a8c89597..582662e24b059 100644 --- a/include/uapi/linux/apple-sep.h +++ b/include/uapi/linux/apple-sep.h @@ -55,6 +55,9 @@ struct sep_bio_info { /* reserved[0] flags; reserved[1] is a live SEP identity count only when valid. */ #define SEP_BIO_INFO_LIVE_COUNT_VALID 1u +/* SEP_BIO_VERIFY_START takes SEP_BIO_VERIFY_ACCOUNT (only where per-user + * key bags are supported). */ +#define SEP_BIO_INFO_VERIFY_ACCOUNT 2u struct sep_bio_list { __u32 count; @@ -111,9 +114,18 @@ struct sep_bio_enrol_poll { __u32 progress_percent; }; +/* + * flags SEP_BIO_VERIFY_ACCOUNT: 'account_uid' is the resolved Unix account the + * match is for. While that account's own key bag is loaded and unlocked by + * its password, the match runs against that user's own templates; while it + * is loaded but locked, the verify fails and the password is needed. With + * no loaded bag, or without the flag, the built-in Linux user matches. + */ +#define SEP_BIO_VERIFY_ACCOUNT 1u + struct sep_bio_verify_start { __u32 flags; - __u32 reserved; + __u32 account_uid; /* with SEP_BIO_VERIFY_ACCOUNT, else 0 */ __u8 nonce[SEP_BIO_NONCE_LEN]; }; From 9a6f4c3c9b91445862cacd9d94aa174cccc5f783 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sat, 14 Nov 2026 14:46:42 -0600 Subject: [PATCH 12/24] soc: apple: sep: userbag: open a bag while a fingerprint prompt waits A lock screen runs its password and fingerprint prompts at once. While fprintd waited for a finger, the user-bag ioctls refused everything but STATE with EBUSY. A password unlock would also have waited on the waiting verify's catacomb_writer to load the user's catacomb. So a password at the lock screen created a bag that could not then be exported or unlocked. Create, import, export, unlock and lock do not touch the Touch ID session, so they no longer require it to be idle; password change and FDE sealing still do. The user's catacomb is loaded once per load of their bag. Loading it resets the enclave's biometry lockout for that user, which invalidates a Touch ID credential made before it, so the first password after the bag loads loads the catacomb before making the credential. If a fingerprint wait holds catacomb_writer then, that capture is ended as a cancel ends it (fprintd sees a failure it may retry), and the lock is awaited for at most WORKER_EXIT_WAIT_MS. A key's touch is not ended. Without this, the first touch at a lock screen after boot found the credential stale and needed the password again. Every accepted password renews the credential. One that is replaced or ends is deleted in the enclave and its handle wiped. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/sbio.rs | 10 ++ drivers/soc/apple/userbag.rs | 178 ++++++++++++++++++++++++++++------- 2 files changed, 155 insertions(+), 33 deletions(-) diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index c3cca1bc38221..9d03467e7da1d 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -1158,6 +1158,16 @@ impl SepData { } } + /// Ends an enrolment or verify in progress as a CANCEL does: its caller + /// sees a failure it can retry. Returns whether one was ended. + pub(crate) fn bio_cancel_capture(&self) -> bool { + let woke = bio::abort_capture(&mut self.bio_session.lock()); + if woke { + self.bio_wake(); + } + woke + } + pub(crate) fn attach_sensor(&self) { if let Err(e) = sensor::register_driver() { dev_err_ratelimited!( diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index e5c19ac10e927..85d44274df29b 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -265,6 +265,9 @@ struct LoadedBag { user: i32, handle: crate::sks::KeyBagHandle, touch: Option<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>, + /// The user's catacomb was loaded since this bag was (see + /// `userbag_restore_13`). + restored: bool, } impl LoadedBags { @@ -288,7 +291,7 @@ impl LoadedBags { if self.get(user).is_some() { return Err(EBUSY); } - self.0.push(LoadedBag { user, handle, touch: None }, GFP_KERNEL)?; + self.0.push(LoadedBag { user, handle, touch: None, restored: false }, GFP_KERNEL)?; Ok(()) } @@ -1236,6 +1239,13 @@ impl crate::SepData { out.reply.status == 0 && self.sks_uuid_from_reply(&out) == Some(loaded) } + /// Ends a bag user's Touch ID preparation: deletes its unlock evaluation + /// in the enclave and wipes the handle. + fn userbag_dispose_touch(&self, mut handle: [u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) { + let _ = self.delete_unlock_evaluation(&handle); + crate::image::wipe(&mut handle); + } + pub(crate) fn userbag_sepos13(&self) -> bool { matches!(self.profile.key_store, crate::profile::KeyStore::Sepos13 { .. }) } @@ -1257,19 +1267,81 @@ impl crate::SepData { self.userbag_transition(user, TransitionState::Unlocked, 0, secret)?; } // The password is accepted; Touch ID for this user is a bonus that - // never fails the unlock. Prepared without `user_bags` held: a touch - // takes `catacomb_writer` before `user_bags`, so this must not take - // them the other way round. + // never fails the unlock. + self.userbag_refresh_touch_13(user, secret); + Ok(()) + } + + /// Makes a fresh Touch ID preparation for `user` after its password was + /// accepted (a bag unlock, or a bag context's password authentication). + /// + /// Loading the user's catacomb resets the enclave's biometry lockout for + /// that user, which invalidates a preparation made before it. So the + /// catacomb is loaded first (`userbag_writer_for_restore`); only if that + /// fails does the first touch find the preparation stale (see + /// `userbag_touch_subject`). + pub(crate) fn userbag_refresh_touch_13(&self, user: crate::sks::DesignateUser, secret: &[u8]) { + if !self.userbag_sepos13() { + return; + } + let uid = u32::try_from(-user.special_handle().value()) + .ok() + .and_then(|id| id.checked_sub(USER_BAG_BASE)); + if let Some(uid) = uid { + if let Some(_writer) = self.userbag_writer_for_restore(user, uid) { + let mut bags = self.user_bags.lock(); + let _ = self.userbag_restore_13(&mut bags, user, uid); + } + } + // Prepared without `user_bags` held: a touch takes `catacomb_writer` + // before `user_bags`, so this must not take them the other way round. let touch = self.userbag_prepare_touch_13(user, secret); let mut bags = self.user_bags.lock(); if let Some(entry) = bags.entry(user.special_handle().value()) { if let Some(old) = core::mem::replace(&mut entry.touch, touch) { - let _ = self.delete_unlock_evaluation(&old); + self.userbag_dispose_touch(old); } } else if let Some(t) = touch { - let _ = self.delete_unlock_evaluation(&t); + self.userbag_dispose_touch(t); } - Ok(()) + } + + /// `catacomb_writer`, to load `uid`'s catacomb before a password makes + /// their Touch ID preparation. A fingerprint wait holds the lock for as + /// long as its prompt lasts, and a lock screen runs that prompt beside + /// the password one; loading the catacomb later, at the first touch, + /// makes the preparation stale, and that touch would need the password + /// once more. So on the first password since the bag was loaded, a + /// waiting capture is ended as a cancel ends it (fprintd may retry) and + /// the lock is awaited for at most `WORKER_EXIT_WAIT_MS`. A key's touch + /// is not ended. None when nothing is left to load or the lock stayed + /// held. + fn userbag_writer_for_restore(&self, user: crate::sks::DesignateUser, uid: u32) -> Option> { + let pending = self.user_bags.lock().entry(user.special_handle().value()).is_some_and(|e| !e.restored); + if !pending { + return None; + } + if let Some(writer) = self.catacomb_writer.try_lock() { + return Some(writer); + } + if crate::catacomb::is_absent(crate::catacomb::Slot::BagUser(uid)) || self.key_touch_active.load(kernel::sync::atomic::Relaxed) { + return None; + } + if self.bio_cancel_capture() { + dev_dbg!(self.dev, "userbag: ended a waiting fingerprint prompt to load uid {}'s templates\n", uid); + } + let mut waited: u32 = 0; + while waited < crate::WORKER_EXIT_WAIT_MS { + kernel::time::delay::fsleep(kernel::time::Delta::from_millis(20)); + waited += 20; + if let Some(writer) = self.catacomb_writer.try_lock() { + return Some(writer); + } + } + dev_info_ratelimited!(self.dev, + "userbag: a capture still held Touch ID after {} ms; uid {} needs the password once more\n", + crate::WORKER_EXIT_WAIT_MS, uid); + None } /// After a password unlock on the 13.5 store: loads the user's catacomb @@ -1280,38 +1352,62 @@ impl crate::SepData { user: crate::sks::DesignateUser, secret: &[u8], ) -> Option<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]> { + // The catacomb was loaded before this (`userbag_refresh_touch_13`), + // or is loaded at the first Touch ID use under `catacomb_writer`. let id = -user.special_handle().value(); let uid = u32::try_from(id).ok()?.checked_sub(USER_BAG_BASE)?; - { - let _writer = self.catacomb_writer.lock(); - let raw = self.read_component_states()?; - let state = crate::sbio::ComponentStates::new(&raw).state_for(id); - let slot = crate::catacomb::Slot::BagUser(uid); - // A user who never enrolled has neither a component nor a file: - // nothing to load, and the preparation still authorizes the first - // enrolment. Either one without the other is not that. - let fresh = state.is_none() && crate::catacomb::is_absent(slot); - if !fresh { - let outcome = self.restore_catacomb(state, id, slot, c"bag user catacomb"); - if !matches!(outcome, crate::RestoreOutcome::AlreadyActive | crate::RestoreOutcome::Restored) { - dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: its catacomb did not load ({:?})\n", uid, outcome); - return None; - } - } - } let (handle, status) = self.key_context_for(id, Some((user.special_handle(), secret))).ok()?; let ready = status == 0 && matches!(self.scrd_command(&crate::scrd::scrd_verify_touchid_enrollment(&handle)), Some((0, ref reply)) if reply.get(..4) == Some(&1u32.to_le_bytes())); if !ready { - let _ = self.delete_unlock_evaluation(&handle); + self.userbag_dispose_touch(handle); dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: the preparation was refused\n", uid); return None; } - dev_info_ratelimited!(self.dev, "userbag: Touch ID and enrolment ready for uid {}\n", uid); + dev_dbg!(self.dev, "userbag: Touch ID and enrolment ready for uid {}\n", uid); Some(handle) } + /// Loads a bag user's catacomb from its file once per load of their bag. + /// Called with `catacomb_writer` held, before their first match or + /// enrolment. A user who never enrolled has neither a component nor a + /// file: nothing to load. Either one without the other is not that. + fn userbag_restore_13(&self, bags: &mut LoadedBags, user: crate::sks::DesignateUser, uid: u32) -> bool { + let Some(entry) = bags.entry(user.special_handle().value()) else { + return false; + }; + if entry.restored { + return true; + } + let id = -user.special_handle().value(); + let Some(raw) = self.read_component_states() else { + return false; + }; + let state = crate::sbio::ComponentStates::new(&raw).state_for(id); + let slot = crate::catacomb::Slot::BagUser(uid); + if !(state.is_none() && crate::catacomb::is_absent(slot)) { + let outcome = self.restore_catacomb(state, id, slot, c"bag user catacomb"); + if !matches!(outcome, crate::RestoreOutcome::AlreadyActive | crate::RestoreOutcome::Restored) { + dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: its catacomb did not load ({:?})\n", uid, outcome); + return false; + } + // Loading it resets the enclave's biometry lockout for the user, + // which invalidates a Touch ID preparation made before it: that + // one goes, and the next password makes a valid one + // (`userbag_refresh_touch_13`). + if matches!(outcome, crate::RestoreOutcome::Restored) { + if let Some(t) = entry.touch.take() { + self.userbag_dispose_touch(t); + dev_info_ratelimited!(self.dev, + "userbag: Touch ID for uid {} needs the password once more: its templates loaded after the unlock\n", uid); + } + } + } + entry.restored = true; + true + } + /// The subject of an enrolment or a verify for the Unix account `uid` /// (13.5 store): that user's own domain when their bag is loaded. Refused /// while it is loaded but not unlocked by the password since its last @@ -1322,11 +1418,11 @@ impl crate::SepData { return Ok(None); } let user = bag_designation(uid)?; - let bags = self.user_bags.lock(); + let mut bags = self.user_bags.lock(); if bags.get(user.special_handle().value()).is_none() { return Ok(None); } - if bags.touch(user.special_handle().value()).is_none() { + if bags.touch(user.special_handle().value()).is_none() || !self.userbag_restore_13(&mut bags, user, uid) { return Err(EACCES); } Ok(Some(crate::BioSubject { user: -user.special_handle().value(), uid })) @@ -1346,8 +1442,20 @@ impl crate::SepData { return Ok(None); } let user = bag_designation(uid)?; - let Some(handle) = self.user_bags.lock().touch(user.special_handle().value()) else { - return Err(EACCES); + let handle = { + let mut bags = self.user_bags.lock(); + let Some(handle) = bags.touch(user.special_handle().value()) else { + return Err(EACCES); + }; + if !self.userbag_restore_13(&mut bags, user, uid) { + return Err(EACCES); + } + // Loading the catacomb just now dropped a preparation it made + // stale (`userbag_restore_13`). + if bags.touch(user.special_handle().value()).is_none() { + return Err(EACCES); + } + handle }; let ready = matches!(self.scrd_command(&crate::scrd::scrd_verify_touchid_enrollment(&handle)), Some((0, ref reply)) if reply.get(..4) == Some(&1u32.to_le_bytes())); @@ -1395,7 +1503,7 @@ impl crate::SepData { }; // Touch ID ends with the lock, whatever the store answers. if let Some(t) = touch { - let _ = self.delete_unlock_evaluation(&t); + self.userbag_dispose_touch(t); } self.userbag_transition(user, TransitionState::Locked, flags, &[])?; self.userbag_loaded_lock_state_13(bag, TransitionState::Locked, &[])?; @@ -1431,7 +1539,7 @@ impl crate::SepData { let removed = self.user_bags.lock().remove(user.special_handle().value()); if let Some((bag, touch)) = removed { if let Some(t) = touch { - let _ = self.delete_unlock_evaluation(&t); + self.userbag_dispose_touch(t); } let unloaded = self.sks_send(self.sks_req_unload_keybag(bag)); if !matches!(unloaded, Some(ref out) if out.reply.status == 0) { @@ -1728,7 +1836,11 @@ impl crate::SepData { return Err(EOPNOTSUPP); } let user_ptr = UserPtr::from_addr(arg); - if cmd != IOC_STATE && !crate::bio::idle_for_mutation(&self.bio_session.lock()) { + // Create, import, export, unlock and lock leave the Touch ID session + // alone. A lock screen runs its password and fingerprint prompts at + // once, so a login must not wait for a fingerprint wait to end. + let reads_or_opens = matches!(cmd, IOC_STATE | IOC_CREATE | IOC_EXPORT | IOC_IMPORT | IOC_UNLOCK | IOC_LOCK); + if !reads_or_opens && !crate::bio::idle_for_mutation(&self.bio_session.lock()) { return Err(EBUSY); } match cmd { From fd8c3c5529cf3c7300d3422f852d600832f56708 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Sun, 15 Nov 2026 20:18:47 -0600 Subject: [PATCH 13/24] soc: apple: sep: list the identities an account's own match can answer A verify naming a Unix account matches in that account's own key bag, but SEP_BIO_LIST returned only the shared Linux index. fprintd treats a storage device's list as complete: after the first failed match it deletes every stored print the list lacks, so a single miss deleted the prints of fingers enrolled in an account's domain, which the next match still answered and fprintd could no longer place. List the identities of every account bag the enclave holds after the index's, once each and without a label (fprintd compares identities). The enclave is queried outside the session and index locks, as the live-count query is. The list now names every loaded account's identities to whoever opens /dev/sep-bio, which is root-only. KUnit covers the ordering and deduplication. On J293, the lock screen kept matching a finger enrolled in the user's domain after misses. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/bio.rs | 55 +++++++++++++++++++++++++++++++++++++-- drivers/soc/apple/sbio.rs | 39 +++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/drivers/soc/apple/bio.rs b/drivers/soc/apple/bio.rs index 1cc392c08ef89..37615e2145cce 100644 --- a/drivers/soc/apple/bio.rs +++ b/drivers/soc/apple/bio.rs @@ -618,6 +618,15 @@ pub(crate) struct Context<'a> { /// Whether a verify may name a Unix account: only where per-user key /// bags exist (`PlatformProfile::per_user_keybags`). pub(crate) verify_account: bool, + /// Identities enrolled in a Unix account's own key bag, which a verify + /// naming that account matches. Listed with the index's, so a list + /// holds every identity a match can answer. + pub(crate) account_identities: &'a [[u8; UUID_LEN]], +} + +/// Whether `cmd` lists identities, which then needs the accounts' own. +pub(crate) fn lists_identities(cmd: u32) -> bool { + cmd == IOC_LIST } pub(crate) struct Handled { @@ -702,19 +711,38 @@ fn get_info(ctx: &mut Context<'_>, user: UserPtr) -> Result { ok() } -fn list(ctx: &mut Context<'_>, user: UserPtr) -> Result { +/// The index's identities, then each account's own not already among them +/// (without a label: the account's records are fprintd's, by identity). +/// fprintd deletes any of its prints a list lacks, so a list without the +/// accounts' identities would delete prints a match still answers. +fn listed(index: &IdentityIndex, accounts: &[[u8; UUID_LEN]]) -> List { let mut out = List { count: 0, reserved: 0, id: [Identity::ZERO; MAX_IDENTITIES], }; - for entry in ctx.index.entries.iter() { + for entry in index.entries.iter() { if out.count as usize >= MAX_IDENTITIES { break; } out.id[out.count as usize] = *entry; out.count += 1; } + for uuid in accounts { + if out.count as usize >= MAX_IDENTITIES { + break; + } + if *uuid == [0; UUID_LEN] || out.id[..out.count as usize].iter().any(|id| id.uuid == *uuid) { + continue; + } + out.id[out.count as usize] = Identity { uuid: *uuid, label: [0; LABEL_LEN] }; + out.count += 1; + } + out +} + +fn list(ctx: &mut Context<'_>, user: UserPtr) -> Result { + let out = listed(ctx.index, ctx.account_identities); UserSlice::new(user, core::mem::size_of::()) .writer() @@ -745,6 +773,28 @@ pub(crate) fn validate_enrol_start_arg(arg: usize) -> Result<()> { validate_enrol_request(&request) } +#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(apple_sep_list_accounts)] +mod list_accounts_tests { + use super::*; + + #[test] + fn a_list_holds_the_accounts_identities_once_after_the_index() { + let mut index = IdentityIndex::new(); + let shared = Identity { uuid: [1; UUID_LEN], label: [b'x'; LABEL_LEN] }; + assert!(index.insert(shared).is_ok()); + let accounts = [[2; UUID_LEN], [1; UUID_LEN], [0; UUID_LEN], [2; UUID_LEN], [3; UUID_LEN]]; + let out = listed(&index, &accounts); + assert_eq!(out.count, 3); + assert_eq!(out.id[0].uuid, [1; UUID_LEN]); + assert_eq!(out.id[0].label, [b'x'; LABEL_LEN]); + assert_eq!(out.id[1].uuid, [2; UUID_LEN]); + assert_eq!(out.id[1].label, [0; LABEL_LEN]); + assert_eq!(out.id[2].uuid, [3; UUID_LEN]); + assert_eq!(listed(&index, &[]).count, 1); + } +} + #[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] #[::kernel::macros::kunit_tests(apple_sep_enrol_request)] mod enrol_request_tests { @@ -1389,6 +1439,7 @@ pub unsafe extern "C" fn sep_kunit_bio_ioctl(bio: *mut c_void, cmd: c_uint, arg: sensor_present: true, live_identity_count: Some(0), verify_account: true, + account_identities: &[], }; match ioctl(&mut ctx, cmd, arg as usize) { Ok(h) => h.ret as c_long, diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 9d03467e7da1d..cad163b2946cf 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -1090,6 +1090,26 @@ impl SepData { } } + /// The identities enrolled in Unix accounts' own key bags (designated at + /// `USER_BAG_BASE + uid`), whichever accounts' bags the enclave holds. + pub(crate) fn enclave_account_identities(&self) -> Option> { + let SbioOutcome::Ok(reply) = self.sbio_call(&crate::sbio::sbio_list_identities()) else { + return None; + }; + let records = crate::sbio::IdentityRecords::new(&reply)?; + let mut out = KVec::new(); + let mut failed = false; + records.each_built_in_account(|identity| { + if out.push(*identity.uuid(), GFP_KERNEL).is_err() { + failed = true; + } + }); + if failed { + return None; + } + Some(out) + } + pub(crate) fn enclave_identities_for(&self, user_id: i32) -> Option> { let op = crate::sbio::sbio_list_identities(); let SbioOutcome::Ok(reply) = self.sbio_call(&op) else { @@ -6046,6 +6066,12 @@ impl SepData { // The SPI probe may have deferred until its SMC power supplier bound. // A saved attach-time snapshot would incorrectly reject later ioctls. let sensor_present = sensor::is_bound(); + // Also outside the locks: the accounts' own identities, for a list. + let account_identities = if bio::lists_identities(cmd) && self.touchid_started.load(Relaxed) { + self.enclave_account_identities().unwrap_or_default() + } else { + KVec::new() + }; let handled = if cmd == bio::IOC_DELETE_ORPHAN { self.bio_delete_orphan(arg)? } else { @@ -6060,6 +6086,7 @@ impl SepData { sensor_present, live_identity_count, verify_account: self.profile.per_user_keybags, + account_identities: &account_identities, }; bio::ioctl(&mut ctx, cmd, arg)? }; @@ -7434,6 +7461,18 @@ impl<'a> IdentityRecords<'a> { .any(|(identity, _)| identity.uuid == *uuid) } + /// Each built-in identity of a Unix account's own key bag. + pub(crate) fn each_built_in_account(&self, mut visit: impl FnMut(IdentityV1)) { + let base = crate::userbag::USER_BAG_BASE as i32; + for i in 0..self.count() { + if let Some((identity, built_in)) = self.record(i) { + if built_in && identity.user_id >= base { + visit(identity); + } + } + } + } + pub(crate) fn each_built_in_for(&self, user_id: i32, mut visit: impl FnMut(IdentityV1)) { for i in 0..self.count() { if let Some((identity, built_in)) = self.record(i) { From eec5f12494db0ad4edca2285165c30b4f5f9bd5f Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Tue, 17 Nov 2026 14:51:04 -0600 Subject: [PATCH 14/24] soc: apple: sep: Touch ID in the bag user's domain on the variant-5 store The variant-5 key store (T8140, T6040 and its inheritors) has per-user bags too, but Touch ID followed them only on the 13.5 store. Variant 5 unloads a bag once it is designated, so the bag never entered `user_bags`: a touch for one of its user-presence keys matched in the configured owner domain, and the enclave refused that credential for the key (status -8). Password authorization was unaffected. Track a designated variant-5 bag under its designation at create and import, prepare its Touch ID after each password unlock, end the preparation at every lock (with or without a grace period) and drop the entry at logout. Enrolments, verifies and key touches for the bag's user then take that user's own domain, as on 13.5; the gates follow the profile's per-user bags instead of the 13.5 store. KUnit covers tracking a designation once. On J716c (T6050), a fingerprint enrolled in the bag user's domain authorized the bag's presence keys after a password unlock. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/key.rs | 2 +- drivers/soc/apple/sbio.rs | 3 +- drivers/soc/apple/userbag.rs | 82 ++++++++++++++++++++++++++++++------ 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index 36174b55994d7..3f56276543e35 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -1606,7 +1606,7 @@ impl SepData { // one grant, one context at a time. let _writer = self.catacomb_writer.lock(); // A key under a user's bag is matched in that user's domain, and - // only after its password unlocked the bag (13.5 store). + // only after its password unlocked the bag. let _subject = match job.grant.as_ref().and_then(|g| g.bag()) { Some(uid) => match self.userbag_touch_subject(uid)? { Some(subject) => Some(crate::SubjectScope::new(self, subject)), diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index cad163b2946cf..5c4ac72d344f7 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -3404,8 +3404,7 @@ impl SepData { pub(crate) fn run_enrolment(&self) { // An enrolment for a Unix account whose bag is loaded goes into that - // user's own domain (13.5 store); one whose bag is locked waits for - // the password. + // user's own domain; one whose bag is locked waits for the password. let account = bio::enrol_account(&self.bio_session.lock()); let _subject = match account.map(|uid| self.userbag_bio_subject(uid)) { Some(Err(_)) => { diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index 85d44274df29b..22d092d78d1ed 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -295,6 +295,16 @@ impl LoadedBags { Ok(()) } + /// Tracks a variant-5 bag, which the store addresses by its designation + /// `user` once designated. Already tracked is not an error: a designation + /// is kept until logout. + fn track_designated(&mut self, user: i32) -> Result<()> { + if self.get(user).is_some() { + return Ok(()); + } + self.insert(user, crate::sks::KeyBagHandle::from_load_reply(user)) + } + /// Removes the entry; the caller disposes of any touch preparation. fn remove(&mut self, user: i32) -> Option<(crate::sks::KeyBagHandle, Option<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>)> { let at = self.0.iter().position(|b| b.user == user)?; @@ -1250,6 +1260,22 @@ impl crate::SepData { matches!(self.profile.key_store, crate::profile::KeyStore::Sepos13 { .. }) } + /// Variant 5 unloads a bag once it is designated and addresses it by its + /// designation from then on. Its entry in `user_bags` holds only the + /// user's Touch ID preparation and catacomb state; nothing unloads it. + fn userbag_track_designated(&self, user: crate::sks::DesignateUser) -> Result<()> { + self.user_bags.lock().track_designated(user.special_handle().value()) + } + + /// A lock of `user`'s bag (variant 5): its Touch ID preparation ends + /// before the store is asked, whatever it answers. + fn userbag_end_touch(&self, user: crate::sks::DesignateUser) { + let touch = self.user_bags.lock().entry(user.special_handle().value()).and_then(|e| e.touch.take()); + if let Some(t) = touch { + self.userbag_dispose_touch(t); + } + } + /// Opens a user's bag through the captured device-state transition. The /// 13.5 store first unlocks the loaded bag itself. pub(crate) fn userbag_unlock( @@ -1258,7 +1284,9 @@ impl crate::SepData { secret: &[u8], ) -> Result<()> { if !self.userbag_sepos13() { - return self.userbag_transition(user, TransitionState::Unlocked, 0, secret); + self.userbag_transition(user, TransitionState::Unlocked, 0, secret)?; + self.userbag_refresh_touch_13(user, secret); + return Ok(()); } { let bags = self.user_bags.lock(); @@ -1281,7 +1309,7 @@ impl crate::SepData { /// fails does the first touch find the preparation stale (see /// `userbag_touch_subject`). pub(crate) fn userbag_refresh_touch_13(&self, user: crate::sks::DesignateUser, secret: &[u8]) { - if !self.userbag_sepos13() { + if !self.profile.per_user_keybags { return; } let uid = u32::try_from(-user.special_handle().value()) @@ -1409,12 +1437,12 @@ impl crate::SepData { } /// The subject of an enrolment or a verify for the Unix account `uid` - /// (13.5 store): that user's own domain when their bag is loaded. Refused - /// while it is loaded but not unlocked by the password since its last - /// lock, as macOS asks for the password then. None (no bag) keeps the - /// configured user. + /// (a store with per-user bags): that user's own domain when their bag + /// is loaded. Refused while it is loaded but not unlocked by the password + /// since its last lock, as macOS asks for the password then. None (no + /// bag) keeps the configured user. pub(crate) fn userbag_bio_subject(&self, uid: u32) -> Result> { - if !self.userbag_sepos13() || uid < MIN_UID { + if !self.profile.per_user_keybags || uid < MIN_UID { return Ok(None); } let user = bag_designation(uid)?; @@ -1434,11 +1462,12 @@ impl crate::SepData { self.user_bags.lock().touch(-subject.user) } - /// Touch ID for a key under `uid`'s bag (13.5 store): the preparation - /// must exist, i.e. the password has unlocked the bag since it last - /// locked, and the enclave must still find it TouchIdEnrollment-satisfied. + /// Touch ID for a key under `uid`'s bag (a store with per-user bags): the + /// preparation must exist, i.e. the password has unlocked the bag since + /// it last locked, and the enclave must still find it + /// TouchIdEnrollment-satisfied. pub(crate) fn userbag_touch_subject(&self, uid: u32) -> Result> { - if !self.userbag_sepos13() { + if !self.profile.per_user_keybags { return Ok(None); } let user = bag_designation(uid)?; @@ -1571,6 +1600,9 @@ impl crate::SepData { } return self.userbag_lock_13(user, 0); } + // Variant 5: this bag's Touch ID ends with any lock, including one + // with a grace period. + self.userbag_end_touch(user); if immediate { let handle = user.special_handle().value(); return self @@ -1671,7 +1703,7 @@ impl crate::SepData { dev_info_ratelimited!(self.dev, "userbag: designating the imported bag as {} failed\n", user.special_handle().value()); return Err(EACCES); } - Ok(()) + self.userbag_track_designated(user) } pub(crate) fn userbag_create( @@ -1720,6 +1752,8 @@ impl crate::SepData { let _ = self.sks_send(self.sks_req_unload_keybag(handle)); return Err(e); } + } else { + self.userbag_track_designated(user)?; } // Copying the wrapped bag into a per-user durable store is phase 2. // Until then this ioctl establishes only the live designated bag. @@ -1970,7 +2004,15 @@ impl crate::SepData { self.userbag_logout_13(user)?; return crate::bio::ok(); } - self.userbag_lock(user, screen, lock_is_immediate(reason, grace_s))?; + let locked = self.userbag_lock(user, screen, lock_is_immediate(reason, grace_s)); + if reason == LOCK_REASON_LOGOUT { + // Variant 5: the next login imports the bag again. + let removed = self.user_bags.lock().remove(user.special_handle().value()); + if let Some((_, Some(t))) = removed { + self.userbag_dispose_touch(t); + } + } + locked?; crate::bio::ok() } IOC_CHANGE => { @@ -2136,6 +2178,20 @@ mod tests { assert!(bags.remove(USER_HANDLE).is_none()); } + #[test] + fn a_designated_bag_is_tracked_once_under_its_designation() { + let mut bags = LoadedBags::new(); + bags.track_designated(USER_HANDLE).expect("track"); + assert_eq!(bags.get(USER_HANDLE).unwrap().value(), USER_HANDLE); + // Tracking again (a second import before logout) keeps the entry and + // its Touch ID preparation. + bags.entry(USER_HANDLE).unwrap().touch = Some([7; crate::scrd::SCRD_ACM_HANDLE_LEN]); + bags.track_designated(USER_HANDLE).expect("again"); + assert_eq!(bags.touch(USER_HANDLE), Some([7; crate::scrd::SCRD_ACM_HANDLE_LEN])); + assert_eq!(bags.remove(USER_HANDLE).unwrap().1, Some([7; crate::scrd::SCRD_ACM_HANDLE_LEN])); + assert!(bags.get(USER_HANDLE).is_none()); + } + #[test] fn designate_layout_golden_and_round_trip() { let key_store = crate::profile::KeyStore::Variant5; From c1eb84eb42ce4e4cc36b2765d47dfe2cf2370f8a Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Wed, 18 Nov 2026 10:37:03 -0600 Subject: [PATCH 15/24] soc: apple: sep: delete a print from the key bag that holds it A print enrolled into a Unix account's own key bag is filed in that user's domain and never in the shared index. SEP_BIO_DELETE looked only at the index, so deleting one reported success ("nothing to delete") while the enclave kept the template, and the finger went on unlocking the bag's keys and the lock screen. When the identity is not in the index but the enclave lists it under an account's bag, delete it there: the bag must be unlocked by its password (its catacomb loaded), as for an enrolment into it; 0x57 names that user's identity as the enclave lists it, and that user's catacomb, any other dirty one, the master and the lockout record are saved before success is reported. A locked bag refuses with EACCES; a busy session with EBUSY; writes off with EROFS, as for any deletion. KUnit covers finding an account's identity by its own user. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/bio.rs | 2 +- drivers/soc/apple/sbio.rs | 133 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/bio.rs b/drivers/soc/apple/bio.rs index 37615e2145cce..e0c72b267f7d4 100644 --- a/drivers/soc/apple/bio.rs +++ b/drivers/soc/apple/bio.rs @@ -324,7 +324,7 @@ const IOC_ENROL_POLL: u32 = _IOR::(MAGIC, 0x04); const IOC_VERIFY_START: u32 = _IOW::(MAGIC, 0x05); const IOC_VERIFY_POLL: u32 = _IOR::(MAGIC, 0x06); const IOC_CANCEL: u32 = _IO(MAGIC, 0x07); -const IOC_DELETE: u32 = _IOW::(MAGIC, 0x08); +pub(crate) const IOC_DELETE: u32 = _IOW::(MAGIC, 0x08); const IOC_DELETE_ALL: u32 = _IO(MAGIC, 0x09); pub(crate) const IOC_ATTEST: u32 = _IOWR::(MAGIC, 0x0a); pub(crate) const IOC_LIST_ENCLAVE: u32 = _IOR::(MAGIC, 0x0b); diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 5c4ac72d344f7..089575e9ce0cc 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -6071,6 +6071,22 @@ impl SepData { } else { KVec::new() }; + // A print enrolled into a Unix account's own key bag is not in the + // shared index: the enclave holds it under that user. Delete it + // there; without this the request reported success and the print + // kept matching. + if cmd == bio::IOC_DELETE && self.touchid_started.load(Relaxed) { + bio::require_admin()?; + let request: bio::Delete = + kernel::uaccess::UserSlice::new(kernel::uaccess::UserPtr::from_addr(arg), core::mem::size_of::()) + .reader() + .read()?; + if !self.bio_index.lock().contains_uuid(&request.uuid) { + if let Some(identity) = self.enclave_account_identity(&request.uuid) { + return self.delete_account_identity(identity); + } + } + } let handled = if cmd == bio::IOC_DELETE_ORPHAN { self.bio_delete_orphan(arg)? } else { @@ -6125,6 +6141,80 @@ impl SepData { Ok(handled) } + fn enclave_account_identity(&self, uuid: &[u8; bio::UUID_LEN]) -> Option { + let SbioOutcome::Ok(reply) = self.sbio_call(&crate::sbio::sbio_list_identities()) else { + return None; + }; + crate::sbio::IdentityRecords::new(&reply)?.account_identity(uuid) + } + + /// Deletes a print from the Unix account's own key bag that holds it. + /// Its bag must be unlocked by the password (its catacomb loaded), as for + /// an enrolment into it; then 0x57 in that user's domain, and that user's + /// catacomb, any other dirty one, the master and the lockout record are + /// saved before success is reported. + fn delete_account_identity(&self, identity: crate::sbio::IdentityV1) -> Result { + if !self.xart_writable() { + dev_warn_ratelimited!( + self.dev, + "Touch ID: deleting a fingerprint needs xart_writes=1; with writes off the deletion would not survive a reboot\n" + ); + return Err(EROFS); + } + let uid = u32::try_from(identity.user_id()) + .ok() + .and_then(|id| id.checked_sub(crate::userbag::USER_BAG_BASE)) + .ok_or(EINVAL)?; + let deleted = loop { + self.wait_until_awake()?; + let _writer = self.catacomb_writer.lock(); + if !bio::idle_for_mutation(&self.bio_session.lock()) { + return Err(EBUSY); + } + // A suspend began while this waited for the lock: wait it out. + if !self.capture_begin() { + continue; + } + let deleted = self.delete_in_account_domain(&identity, uid); + self.capture_end(); + break deleted; + }; + deleted?; + dev_info_ratelimited!(self.dev, "Touch ID: deleted a fingerprint from uid {}'s own key bag\n", uid); + bio::ok() + } + + /// Called with `catacomb_writer` held. + fn delete_in_account_domain(&self, identity: &crate::sbio::IdentityV1, uid: u32) -> Result<()> { + let subject = match self.userbag_bio_subject(uid) { + Ok(Some(subject)) if subject.user == identity.user_id() => subject, + _ => { + dev_info_ratelimited!( + self.dev, + "Touch ID: uid {}'s key bag is not unlocked; unlock it with the password to delete a fingerprint from it\n", + uid + ); + return Err(EACCES); + } + }; + let _scope = crate::SubjectScope::new(self, subject); + let Some(user) = crate::sbio::UserId::new(subject.user) else { + return Err(EIO); + }; + if self.sbio_expect_ok(&crate::sbio::sbio_delete_identity(identity)).is_none() { + dev_err_ratelimited!(self.dev, "bio: 0x57 in a key bag's domain failed, status above; nothing was removed\n"); + return Err(EIO); + } + if !self.save_bag_user_components(user) { + dev_err_ratelimited!( + self.dev, + "bio: removed from the key bag's domain but not saved; the fingerprint returns at the next boot, and deleting it again retries\n" + ); + return Err(EIO); + } + Ok(()) + } + fn bio_attest(&self, arg: usize) -> Result { let user = kernel::uaccess::UserPtr::from_addr(arg); let req: bio::Attest = @@ -7472,6 +7562,19 @@ impl<'a> IdentityRecords<'a> { } } + /// The built-in identity `uuid` when a Unix account's own key bag holds + /// it, as the enclave lists it (that account's user id). The shared index + /// never holds these, so a deletion takes the user id from here. + pub(crate) fn account_identity(&self, uuid: &[u8; IDENTITY_UUID_LEN]) -> Option { + let mut found = None; + self.each_built_in_account(|identity| { + if identity.uuid == *uuid && found.is_none() { + found = Some(identity); + } + }); + found + } + pub(crate) fn each_built_in_for(&self, user_id: i32, mut visit: impl FnMut(IdentityV1)) { for i in 0..self.count() { if let Some((identity, built_in)) = self.record(i) { @@ -7511,6 +7614,31 @@ mod identity_scope_tests { assert_eq!(seen, 1); } + #[test] + fn an_account_identity_keeps_its_own_user() { + let account = crate::userbag::USER_BAG_BASE as i32 + 1000; + let mut reply = [0u8; 3 * IDENTITY_RECORD_LEN]; + for (i, (user, selector, byte)) in [ + (SBIO_PROBE_USER_ID, IR_SELECTOR_BUILTIN, 1u8), + (account, IR_SELECTOR_BUILTIN, 2u8), + (account, 2, 3u8), + ].iter().enumerate() { + let at = i * IDENTITY_RECORD_LEN; + reply[at..at + 4].copy_from_slice(&user.to_le_bytes()); + reply[at + 4..at + IDENTITY_V1_LEN].fill(*byte); + reply[at + IR_SELECTOR..at + IR_SELECTOR + 4] + .copy_from_slice(&selector.to_le_bytes()); + } + let records = IdentityRecords::new(&reply).unwrap(); + let found = records.account_identity(&[2u8; IDENTITY_UUID_LEN]).unwrap(); + assert_eq!(found.user_id(), account); + // The configured user's identity is not an account's, and neither is + // one under another selector. + assert!(records.account_identity(&[1u8; IDENTITY_UUID_LEN]).is_none()); + assert!(records.account_identity(&[3u8; IDENTITY_UUID_LEN]).is_none()); + assert!(records.account_identity(&[9u8; IDENTITY_UUID_LEN]).is_none()); + } + #[test] fn match_result_carries_the_exact_enclave_identity() { let mut reply = [0u8; SBIO_MATCH_RESULT_LEN]; @@ -7591,6 +7719,11 @@ impl IdentityV1 { &self.uuid } + /// The enclave user the identity belongs to. + pub(crate) fn user_id(&self) -> i32 { + self.user_id + } + pub(crate) fn wire(&self) -> [u8; IDENTITY_V1_LEN] { let mut out = [0u8; IDENTITY_V1_LEN]; out[0..4].copy_from_slice(&self.user_id.to_le_bytes()); From 69c54da5745bb75e534b3d280662d53e5b7d631f Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Wed, 18 Nov 2026 11:16:17 -0600 Subject: [PATCH 16/24] soc: apple: sep: userbag: start a bag's Touch ID afresh when the enclave forgot it A bag user's catacomb is saved in its own file, and the shared master records its component. A kernel without per-user bags restores and saves the master without that component, so after one boot of it the loaded master lists nothing for the bag user while the file remains. The bag's restore treated that as a failure, and Touch ID for the bag stayed off: verifies and keys were refused and no finger could be enrolled again. Decide from what the enclave lists: a listed component is loaded as before (a missing file is refused, with what to do); neither component nor file is a bag that never enrolled; a file the loaded master no longer lists is stale, so the bag starts with no fingerprints and the next enrolment replaces the file. Without a loaded master nothing proves that, and the restore refuses as before. The restore runs only after the bag's password, so starting afresh admits no finger; it costs the owner their enrolled ones. KUnit covers the decision. On J716c (T6050), after a boot of a kernel without per-user bags left the master without the bag's component, enrolment into the bag had been refused. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/userbag.rs | 72 ++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index 22d092d78d1ed..fd4d3c84803c1 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -313,6 +313,29 @@ impl LoadedBags { } } +/// What a bag's restore does, from whether the enclave lists a catacomb +/// component for its user, whether a saved file exists, and whether the +/// shared master is loaded (so a missing component means something). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum BagRestore { + /// Never enrolled: nothing to load. + Nothing, + Load, + /// The loaded master forgot this user: the file is stale. + StartFresh, + /// A file but no loaded master to judge it by. + Refuse, +} + +fn bag_restore_plan(component: bool, file: bool, master_active: bool) -> BagRestore { + match (component, file) { + (true, _) => BagRestore::Load, + (false, false) => BagRestore::Nothing, + (false, true) if master_active => BagRestore::StartFresh, + (false, true) => BagRestore::Refuse, + } +} + fn encode_designate(key_store: crate::profile::KeyStore, source_handle: i32, user_handle: i32) -> Result { if source_handle < 0 || user_handle >= 0 { return Err(EINVAL); @@ -1402,6 +1425,9 @@ impl crate::SepData { /// enrolment. A user who never enrolled has neither a component nor a /// file: nothing to load. Either one without the other is not that. fn userbag_restore_13(&self, bags: &mut LoadedBags, user: crate::sks::DesignateUser, uid: u32) -> bool { + // Only reached after the bag's password (a Touch ID preparation + // exists), so starting a bag afresh never lets a finger in: it only + // costs the owner their enrolled fingers. let Some(entry) = bags.entry(user.special_handle().value()) else { return false; }; @@ -1412,12 +1438,38 @@ impl crate::SepData { let Some(raw) = self.read_component_states() else { return false; }; - let state = crate::sbio::ComponentStates::new(&raw).state_for(id); + let states = crate::sbio::ComponentStates::new(&raw); + let state = states.state_for(id); let slot = crate::catacomb::Slot::BagUser(uid); - if !(state.is_none() && crate::catacomb::is_absent(slot)) { + let master_active = states + .state_for(crate::sbio::CatacombUser::MASTER.value()) + .is_some_and(|s| s & crate::sbio::COMPONENT_STATE_ACTIVE != 0); + let plan = bag_restore_plan(state.is_some(), !crate::catacomb::is_absent(slot), master_active); + if plan == BagRestore::StartFresh { + // The loaded master lists nothing for this user, so its saved + // file predates the enclave's current templates: a kernel without + // per-user bags saved the master since, or the enclave was reset. + // Nothing in it can load; the next enrolment replaces it. + dev_info_ratelimited!( + self.dev, + "userbag: uid {}'s saved fingerprints are not in the enclave's templates any more (another kernel saved them since); its Touch ID starts afresh: enrol again\n", + uid + ); + } else if plan == BagRestore::Refuse { + dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: a saved catacomb but no loaded master to check it against\n", uid); + return false; + } else if plan == BagRestore::Load { let outcome = self.restore_catacomb(state, id, slot, c"bag user catacomb"); if !matches!(outcome, crate::RestoreOutcome::AlreadyActive | crate::RestoreOutcome::Restored) { - dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: its catacomb did not load ({:?})\n", uid, outcome); + if matches!(outcome, crate::RestoreOutcome::NoStoredFile) { + dev_info_ratelimited!( + self.dev, + "userbag: no Touch ID for uid {}: the enclave expects its saved catacomb and the file is missing; restore the file (it is not recreated)\n", + uid + ); + } else { + dev_info_ratelimited!(self.dev, "userbag: no Touch ID for uid {}: its catacomb did not load ({:?})\n", uid, outcome); + } return false; } // Loading it resets the enclave's biometry lockout for the user, @@ -2178,6 +2230,20 @@ mod tests { assert!(bags.remove(USER_HANDLE).is_none()); } + #[test] + fn a_stale_bag_file_starts_afresh_only_against_a_loaded_master() { + // A component the enclave lists is loaded, file or not (a missing + // file is then refused by the load). + assert_eq!(bag_restore_plan(true, true, true), BagRestore::Load); + assert_eq!(bag_restore_plan(true, false, false), BagRestore::Load); + assert_eq!(bag_restore_plan(false, false, true), BagRestore::Nothing); + assert_eq!(bag_restore_plan(false, false, false), BagRestore::Nothing); + // A file the loaded master no longer knows is stale. + assert_eq!(bag_restore_plan(false, true, true), BagRestore::StartFresh); + // Without a loaded master, nothing proves that. + assert_eq!(bag_restore_plan(false, true, false), BagRestore::Refuse); + } + #[test] fn a_designated_bag_is_tracked_once_under_its_designation() { let mut bags = LoadedBags::new(); From 604c9d34b15aec0c508e44741436acaa4b2fb1cc Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Wed, 18 Nov 2026 11:28:54 -0600 Subject: [PATCH 17/24] soc: apple: sep: let a bag user's match through the configured user's identity proof Matching waits at each boot for proof that the cold restore worked: the configured Linux user's templates must list at least one identity, because zero after a restore cannot be told from a restore that silently failed. Once every fingerprint is enrolled into a Unix account's own key bag, the configured user has none, the proof never completes, and every verify and key touch was refused before the capture, the bag's own fingers included. A match in a bag user's own domain does not use the configured user's templates: the bag's catacomb is loaded and checked after its password, and a bag with nothing loaded answers no match. Let a match with a bag subject through once every attach step of the restore succeeded (the cold-match preparation, the SCRD credential and the component restore); a match as the configured user still needs the full proof. On J716c (T6050), with both fingers enrolled into the bag and none for the configured user, the lock screen refused every touch after a reboot. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/sbio.rs | 11 ++++++++++- drivers/soc/apple/sep.rs | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 089575e9ce0cc..b2051c48c4ccf 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -2819,7 +2819,15 @@ impl SepData { return; } self.retry_restore_proof(); - if !self.templates_restored.load(Relaxed) { + // The identity proof is the configured user's: zero identities after + // a restore cannot be told from a restore that silently failed. A + // match in a bag user's own domain does not use those templates; the + // bag's catacomb was loaded after its password, and a bag with none + // answers no match above. Every attach step must still have passed. + let bag_subject = self.bio_subject.lock().is_some(); + if !self.templates_restored.load(Relaxed) + && !(bag_subject && self.components_restored.load(Relaxed)) + { dev_err_ratelimited!( self.dev, "verify: refusing before touching the sensor; cold-match prep, restore, or device-view identity proof did not complete. This is not a non-matching finger\n" @@ -6297,6 +6305,7 @@ impl SepData { }; let identities = self.log_identity_count(c"after Catacomb restore with sensor registered"); let identity_ready = matches!(identities, Some(n) if n > 0); + self.components_restored.store(prepared && match_context && restored, Relaxed); let candidate = prepared && match_context && restored && identity_ready; self.cold_restore_candidate.store(candidate, Relaxed); // COMPLETE_INIT has not synchronized the device view yet. Never make diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 7fb33812652d0..0e73bc7f4208e 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -736,6 +736,10 @@ struct SepData { templates_restored: Atomic, // Provisional cold restore; COMPLETE_INIT must still prove the device view. cold_restore_candidate: Atomic, + /// Every attach step of the cold restore succeeded, whatever the + /// configured user's identity count: enough for a match in a bag user's + /// own domain, whose catacomb is loaded and checked on its own. + components_restored: Atomic, sensor_calibrated: Atomic, sensor_key_loaded: Atomic, @@ -1252,6 +1256,7 @@ impl SepData { sensor_key_loaded: Atomic::new(false), templates_restored: Atomic::new(false), cold_restore_candidate: Atomic::new(false), + components_restored: Atomic::new(false), enrol_material <- new_mutex!(None), match_context <- new_mutex!(None), match_context_generation: Atomic::new(0), From 5551d55bb01c6eb898776cad499e7a501dcc72a8 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Mon, 16 Nov 2026 13:13:07 -0600 Subject: [PATCH 18/24] soc: apple: sep: one owner for the sensor at a time Touch ID enrolment and verify, a touch-gated /dev/sep-key operation and the late sensor attach each drive the sensor and the enclave's match operations, and serialize on catacomb_writer. Other BIO calls reach the enclave without it (GET_INFO, ATTEST, LIST_ENCLAVE, PREPARE_ENROL), so one of those could run between a key touch's steps. Add a capture owner. A synchronous user (one of those calls, a deletion, a bio open, the late attach, a key touch) takes a lease for its duration; an enrol or verify start hands its lease to the queued worker, which gives it up only after its cleanup. A second owner gets EBUSY. Starting a capture still waits, as before, for the last worker to stop, now before the lease is taken. KUnit covers the owner's transitions. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/bio.rs | 6 ++ drivers/soc/apple/capture.rs | 141 +++++++++++++++++++++++++++++++++++ drivers/soc/apple/key.rs | 1 + drivers/soc/apple/sbio.rs | 19 ++++- drivers/soc/apple/sep.rs | 4 + 5 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 drivers/soc/apple/capture.rs diff --git a/drivers/soc/apple/bio.rs b/drivers/soc/apple/bio.rs index e0c72b267f7d4..9bf622a0d2707 100644 --- a/drivers/soc/apple/bio.rs +++ b/drivers/soc/apple/bio.rs @@ -1323,6 +1323,12 @@ pub(crate) fn abort_capture(session: &mut Session) -> bool { true } +/// Calls that must not interleave with an admitted sensor operation. +pub(crate) fn uses_enclave(cmd: u32) -> bool { + starts_capture(cmd) || matches!(cmd, IOC_GET_INFO | IOC_DELETE | IOC_DELETE_ALL + | IOC_DELETE_ORPHAN | IOC_ATTEST | IOC_LIST_ENCLAVE | IOC_PREPARE_ENROL) +} + /// The ioctls that start a sensor capture. pub(crate) fn starts_capture(cmd: u32) -> bool { cmd == IOC_ENROL_START || cmd == IOC_VERIFY_START diff --git a/drivers/soc/apple/capture.rs b/drivers/soc/apple/capture.rs new file mode 100644 index 0000000000000..ca25dc06c5983 --- /dev/null +++ b/drivers/soc/apple/capture.rs @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Shared capture ownership; an idle biometric file does not own the sensor. + +use crate::SepData; +use kernel::prelude::*; +use kernel::sync::atomic::Relaxed; + +enum Owner { + Idle, + Sync(u64), + Bio(u64), +} + +pub(crate) struct State { + generation: u64, + owner: Owner, +} + +impl State { + pub(crate) fn new() -> Self { + Self { + generation: 0, + owner: Owner::Idle, + } + } + + fn acquire(&mut self) -> Result { + if !matches!(self.owner, Owner::Idle) { + return Err(EBUSY); + } + self.generation = self.generation.checked_add(1).ok_or(EOVERFLOW)?; + self.owner = Owner::Sync(self.generation); + Ok(self.generation) + } + + /// Hands a synchronous owner's capture to the bio worker `epoch`. + fn transfer_to_bio(&mut self, generation: u64, epoch: u64) { + if matches!(self.owner, Owner::Sync(active) if active == generation) { + self.owner = Owner::Bio(epoch); + } + } + + /// Ends a synchronous owner, unless it was handed on or replaced. + fn release(&mut self, generation: u64) { + if matches!(self.owner, Owner::Sync(active) if active == generation) { + self.owner = Owner::Idle; + } + } + + pub(crate) fn finish_bio(&mut self, epoch: u64) { + if matches!(self.owner, Owner::Bio(active) if active == epoch) { + self.owner = Owner::Idle; + } + } +} + +/// A synchronous firmware operation joins the suspend drain. Ownership can be +/// transferred to a queued bio worker, which releases it only after cleanup. +pub(crate) struct Lease<'a> { + sep: &'a SepData, + generation: u64, + running: bool, +} + +impl<'a> Lease<'a> { + pub(crate) fn acquire(sep: &'a SepData) -> Result { + let generation = { + let mut state = sep.capture.lock(); + if sep.shutting_down.load(Relaxed) { + return Err(ENODEV); + } + if sep.suspending.load(Relaxed) { + return Err(EBUSY); + } + state.acquire()? + }; + let mut lease = Self { + sep, + generation, + running: false, + }; + if !sep.capture_begin() { + return Err(EBUSY); + } + lease.running = true; + Ok(lease) + } + + pub(crate) fn into_bio(mut self, epoch: u64) { + self.sep.capture.lock().transfer_to_bio(self.generation, epoch); + // The queued worker will join the drain when it starts; a suspend + // before then aborts its session, and capture_begin refuses hardware. + self.sep.capture_end(); + self.running = false; + } +} + +impl Drop for Lease<'_> { + fn drop(&mut self) { + if self.running { + self.sep.capture_end(); + } + self.sep.capture.lock().release(self.generation); + } +} + +#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(apple_sep_capture_owner)] +mod tests { + use super::*; + + #[test] + fn one_owner_at_a_time() { + let mut state = State::new(); + let first = state.acquire().unwrap(); + assert_eq!(state.acquire().err(), Some(EBUSY)); + state.release(first); + let second = state.acquire().unwrap(); + assert!(second != first); + // A stale owner's release does not end the current one. + state.release(first); + assert_eq!(state.acquire().err(), Some(EBUSY)); + state.release(second); + assert!(state.acquire().is_ok()); + } + + #[test] + fn a_bio_worker_keeps_the_capture_until_it_finishes() { + let mut state = State::new(); + let lease = state.acquire().unwrap(); + state.transfer_to_bio(lease, 7); + // The lease that handed it on no longer owns it. + state.release(lease); + assert_eq!(state.acquire().err(), Some(EBUSY)); + state.finish_bio(6); + assert_eq!(state.acquire().err(), Some(EBUSY)); + state.finish_bio(7); + assert!(state.acquire().is_ok()); + } +} + diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index 3f56276543e35..e5bd6c1a0324e 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -1601,6 +1601,7 @@ impl SepData { }; let mut job = KeyTouch::new_in(op, blob, digest, sealed, constraint, bag)?; job.probe = probe; + let _capture = crate::capture::Lease::acquire(self)?; // Serialises with /dev/sep-bio's enrol and verify work, as they do // with each other, and with every other touch-gated call: one job, // one grant, one context at a time. diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index b2051c48c4ccf..3af73fe79c4dc 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -2325,6 +2325,10 @@ impl SepData { Self::defer_late_attach(this); return; } + let Ok(_capture) = crate::capture::Lease::acquire(this) else { + Self::defer_late_attach(this); + return; + }; // Enrol, verify and deletion drive the sensor and the enclave under // the same lock, and `capture_begin` lets a suspend wait for us. let _writer = this.catacomb_writer.lock(); @@ -5167,7 +5171,7 @@ impl SepData { self.wait_for_worker_exit(&mut session, true)?; bio::open(&mut session)?; drop(session); - if let Err(e) = self.prepare_bio_open() { + if let Err(e) = crate::capture::Lease::acquire(self).and_then(|_lease| self.prepare_bio_open()) { bio::release(&mut self.bio_session.lock()); return Err(e); } @@ -5208,6 +5212,7 @@ impl SepData { /// The enrol or verify work item `epoch` has stopped. pub(crate) fn capture_worker_stopped(&self, epoch: u64) { bio::worker_stopped(&mut self.bio_session.lock(), epoch); + self.capture.lock().finish_bio(epoch); self.worker_gone.notify_all(); } @@ -6022,6 +6027,11 @@ impl SepData { if self.shutting_down.load(Relaxed) { return Err(ENODEV); } + if bio::starts_capture(cmd) { + self.wait_until_awake()?; + self.wait_for_worker_exit(&mut self.bio_session.lock(), false)?; + } + let lease = if bio::uses_enclave(cmd) { Some(crate::capture::Lease::acquire(self)?) } else { None }; if cmd == bio::IOC_ATTEST { return self.bio_attest(arg); } @@ -6099,9 +6109,6 @@ impl SepData { self.bio_delete_orphan(arg)? } else { let mut session = self.bio_session.lock(); - if bio::starts_capture(cmd) { - self.wait_for_worker_exit(&mut session, false)?; - } let index = self.bio_index.lock(); let mut ctx = bio::Context { session: &mut session, @@ -6143,6 +6150,10 @@ impl SepData { deleted?; } + if handled.start_verify || handled.start_enrol { + let epoch = bio::queued_epoch(&self.bio_session.lock()).ok_or(EIO)?; + lease.ok_or(EIO)?.into_bio(epoch); + } if handled.wake { self.bio_wake(); } diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 0e73bc7f4208e..3b33922788b31 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -13,6 +13,7 @@ compile_error!("apple_sep requires CONFIG_OF_DYNAMIC: the SEP and DART nodes shi mod ratelimit; mod attach; mod bio; +mod capture; mod catacomb; mod control; mod der; @@ -907,6 +908,8 @@ struct SepData { // /dev/sep-key: P-256 keys created inside the SEP (see `key`). #[pin] key_dev: Mutex>, + #[pin] + capture: Mutex, // /dev/sep-key: one identity-bag load, operation and unload at a time. // The device's ioctls run concurrently (a touch wait must not block @@ -1312,6 +1315,7 @@ impl SepData { bio_dev <- new_mutex!(None), rearm_dev <- new_mutex!(None), key_dev <- new_mutex!(None), + capture <- new_mutex!(capture::State::new()), key_bag_lock <- new_mutex!(()), key_touch <- new_mutex!(None), key_touch_active: Atomic::new(false), From ccd73c1402bda2afb1927626f3ebcc77369079a7 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Mon, 16 Nov 2026 13:15:57 -0600 Subject: [PATCH 19/24] soc: apple: sep: sep-key: make /dev/sep-key a per-open device /dev/sep-key was registered with sep_bio_register_concurrent: one shared context for every opener, no per-file state, and an unregister that waited for running ioctls. Requests that belong to the file that made them (an id to cancel, an authorization a file holds) need state per open file. Register it through a small misc-device shim instead. Each open makes a file that holds its own reference to the driver, so it may outlive the registration; its calls then fail with ENODEV. Because unregistering no longer waits for running calls, the driver counts them, and remove waits for them to end before it takes the hardware down, as the old unregister did; no call starts once remove has begun. Every existing /dev/sep-key request goes to key_ioctl unchanged. The unused register_concurrent Rust wrapper goes; the C function stays for its KUnit test. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/Makefile | 2 +- drivers/soc/apple/key.rs | 64 ++------------ drivers/soc/apple/key_device.rs | 150 ++++++++++++++++++++++++++++++++ drivers/soc/apple/key_shim.c | 115 ++++++++++++++++++++++++ drivers/soc/apple/sep.rs | 9 +- drivers/soc/apple/shim.h | 8 ++ drivers/soc/apple/shim.rs | 43 --------- 7 files changed, 290 insertions(+), 101 deletions(-) create mode 100644 drivers/soc/apple/key_device.rs create mode 100644 drivers/soc/apple/key_shim.c diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index 25871d19a196c..eb3595e214a95 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -53,7 +53,7 @@ obj-$(CONFIG_APPLE_SEP_FV_INTERFACE) += fv_shim.o obj-$(CONFIG_APPLE_SEP) += apple-sep.o apple-sep-y := sep.o hwrng_shim.o store_shim.o bio_shim.o sha_shim.o \ crypto_shim.o sensor_shim.o p256_shim.o trusted_shim.o work_shim.o \ - pm_shim.o diag_shim.o attach_shim.o + pm_shim.o diag_shim.o key_shim.o attach_shim.o obj-$(CONFIG_APPLE_PMP) += apple-pmp.o apple-pmp-y := pmp.o pmp-export.o pmp-display.o pmp-telemetry.o diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index e5bd6c1a0324e..bc39ae1d2620c 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -1349,26 +1349,13 @@ mod tests { impl SepData { pub(crate) fn register_key(&self) -> Result<()> { - let ctx = core::ptr::from_ref(self).cast_mut().cast::(); - self.publish_chardev(&self.key_dev, || { - // SAFETY: as in `register_bio`: `remove()` drops the registration - // before the `Arc` that keeps `ctx` alive, and the shim's unregister - // waits for running ioctls and clears the stored context before - // any later callback. Ioctls run concurrently: a touch-gated SIGN or - // UNSEAL sleeps up to a minute, and CANCEL must reach it meanwhile. - // They serialise on `catacomb_writer`; CANCEL only sets a flag. - unsafe { - crate::shim::BioChardev::register_concurrent( - DEVICE_NAME, - DEVICE_MODE, - ctx, - key_open_trampoline, - key_release_trampoline, - key_ioctl_trampoline, - key_ready_trampoline, - ) - } - })?; + let mut device = self.key_dev.lock(); + if self.shutting_down.load(Relaxed) { return Err(ENODEV); } + if device.is_none() { + // SAFETY: remove drops the registration before the driver Arc. + // Every successful open acquires its own reference. + *device = Some(unsafe { crate::key_device::Registration::new(self)? }); + } Ok(()) } @@ -2270,7 +2257,7 @@ impl SepData { Ok(()) } - fn key_ioctl(&self, cmd: u32, arg: usize) -> Result<()> { + pub(crate) fn key_ioctl(&self, cmd: u32, arg: usize) -> Result<()> { if self.shutting_down.load(Relaxed) { return Err(ENODEV); } @@ -2403,41 +2390,6 @@ impl SepData { } } -// /dev/sep-key has no session: any number of openers, no poll. - -/// # Safety -/// `ctx` is the `SepData` pointer given to `register_key`, valid while the -/// registration lives (see `register_key`). -unsafe extern "C" fn key_open_trampoline(ctx: *mut c_void) -> c_int { - // SAFETY: per the contract above. - let this = unsafe { &*ctx.cast::() }; - if this.shutting_down.load(Relaxed) { - return ENODEV.to_errno(); - } - 0 -} - -/// # Safety -/// As for `key_open_trampoline`. -unsafe extern "C" fn key_release_trampoline(_ctx: *mut c_void) {} - -/// # Safety -/// As for `key_open_trampoline`. -unsafe extern "C" fn key_ioctl_trampoline(ctx: *mut c_void, cmd: c_uint, arg: c_ulong) -> c_long { - // SAFETY: per the contract above. - let this = unsafe { &*ctx.cast::() }; - match this.key_ioctl(cmd, arg) { - Ok(()) => 0, - Err(e) => e.to_errno() as c_long, - } -} - -/// # Safety -/// As for `key_open_trampoline`. -unsafe extern "C" fn key_ready_trampoline(_ctx: *mut c_void) -> c_int { - 0 -} - /// KUnit hook for `sep_ioctl_test.c`: [`read_request`] on user memory the /// test maps, so the copy-in of every request is exercised from real user /// addresses (unaligned, faulting, truncated) with no hardware. diff --git a/drivers/soc/apple/key_device.rs b/drivers/soc/apple/key_device.rs new file mode 100644 index 0000000000000..7c9381370a54d --- /dev/null +++ b/drivers/soc/apple/key_device.rs @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! `/dev/sep-key` as a per-open device: each file owns its state and a +//! reference to the driver; the key commands keep their existing ABI. + +use crate::{key as keys, SepData}; +use kernel::prelude::*; +use kernel::sync::atomic::{Acquire, Full, Relaxed, Release}; +use kernel::sync::{Arc, ArcBorrow}; +use kernel::types::ForeignOwnable; + +struct File { + sep: Arc, +} + +impl File { + fn ioctl(&self, cmd: u32, arg: usize) -> Result { + let _call = Call::enter(&self.sep)?; + self.sep.key_ioctl(cmd, arg) + } +} + +/// A `/dev/sep-key` call in progress. A file outlives the device's +/// registration, so `remove` waits for every call to end +/// ([`wait_for_calls`]) before it takes the hardware down, and no call +/// starts once it has begun. +pub(crate) struct Call<'a>(&'a SepData); + +impl<'a> Call<'a> { + pub(crate) fn enter(sep: &'a SepData) -> Result { + // Full: pairs with the barrier in `wait_for_calls`, so either this + // sees `shutting_down` or that sees this call. + sep.key_calls.fetch_add(1, Full); + let call = Self(sep); + if sep.shutting_down.load(Relaxed) { + return Err(ENODEV); + } + Ok(call) + } +} + +impl Drop for Call<'_> { + fn drop(&mut self) { + self.0.key_calls.fetch_sub(1, Release); + } +} + +/// Waits for the calls in progress once `shutting_down` is set. Each sees +/// it and ends: a touch wait checks it, and `remove` wakes the enclave's +/// waits. +pub(crate) fn wait_for_calls(sep: &SepData) { + kernel::sync::barrier::smp_mb(Full); + while sep.key_calls.load(Acquire) != 0 { + kernel::time::delay::fsleep(kernel::time::Delta::from_millis(10)); + } +} + +extern "C" { + fn sep_key_register( + name: *const c_char, + mode: u16, + ctx: *mut c_void, + open: unsafe extern "C" fn(*mut c_void, *mut *mut c_void) -> c_int, + release: unsafe extern "C" fn(*mut c_void), + ioctl: unsafe extern "C" fn(*mut c_void, c_uint, c_ulong) -> c_long, + ) -> *mut c_void; + fn sep_key_unregister(device: *mut c_void); +} + +pub(crate) struct Registration(*mut c_void); +// SAFETY: registration has no thread affinity; the C shim serializes open/detach. +unsafe impl Send for Registration {} + +impl Registration { + /// # Safety + /// The driver's original Arc must keep sep alive until this is dropped. + pub(crate) unsafe fn new(sep: &SepData) -> Result { + // SAFETY: the caller supplies the registered-window guarantee above. + let device = unsafe { + sep_key_register( + keys::DEVICE_NAME.as_char_ptr(), + keys::DEVICE_MODE, + core::ptr::from_ref(sep).cast_mut().cast(), + open, + release, + ioctl, + ) + }; + if device.is_null() { + return Err(ENODEV); + } + Ok(Self(device)) + } +} + +impl Drop for Registration { + fn drop(&mut self) { + // SAFETY: one live registration, never used after this call. + unsafe { sep_key_unregister(self.0) }; + } +} + +/// `/dev/sep-key` open. +/// +/// # Safety +/// The C shim holds its registration lock throughout. `ctx` is the pointer +/// of the driver's original `SepData` Arc, and `out` points to the file's +/// private-data slot. +unsafe extern "C" fn open(ctx: *mut c_void, out: *mut *mut c_void) -> c_int { + // SAFETY: `ctx` came from that Arc, which outlives the registration. + let sep = unsafe { ArcBorrow::::from_raw(ctx.cast()) }; + if sep.shutting_down.load(Relaxed) { + return ENODEV.to_errno(); + } + let file = Arc::new( + File { sep: sep.into() }, + kernel::alloc::flags::GFP_KERNEL_ACCOUNT, + ); + match file { + Ok(file) => { + // SAFETY: out is writable; ownership moves to the matching release. + unsafe { out.write(file.into_foreign()) }; + 0 + } + Err(_) => ENOMEM.to_errno(), + } +} + +/// `/dev/sep-key` release. +/// +/// # Safety +/// C passes the pointer `open` stored, once, after every file callback has +/// returned. +unsafe extern "C" fn release(ctx: *mut c_void) { + // SAFETY: the foreign Arc from `open`, given back exactly once. + drop(unsafe { as ForeignOwnable>::from_foreign(ctx) }); +} + +/// `/dev/sep-key` ioctl. +/// +/// # Safety +/// `ctx` is the pointer `open` stored; the file reference the VFS holds for +/// this call keeps it from being released meanwhile. +unsafe extern "C" fn ioctl(ctx: *mut c_void, cmd: c_uint, arg: c_ulong) -> c_long { + // SAFETY: live until `release`, which cannot run during this call. + let file = unsafe { as ForeignOwnable>::borrow(ctx) }; + match file.ioctl(cmd, arg) { + Ok(()) => 0, + Err(error) => error.to_errno() as c_long, + } +} diff --git a/drivers/soc/apple/key_shim.c b/drivers/soc/apple/key_shim.c new file mode 100644 index 0000000000000..78d84fc41707d --- /dev/null +++ b/drivers/soc/apple/key_shim.c @@ -0,0 +1,115 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* Application-key files own their Rust context, independently of registration. */ +#include +#include +#include +#include + +#include "shim.h" + +struct sep_key_device { + struct miscdevice misc; + struct mutex lock; + void *ctx; + int (*open)(void *ctx, void **file); + void (*release)(void *file); + long (*ioctl)(void *file, unsigned int cmd, unsigned long arg); +}; + +struct sep_key_file { + void *ctx; + void (*release)(void *file); + long (*ioctl)(void *file, unsigned int cmd, unsigned long arg); +}; + +static int sep_key_open(struct inode *inode, struct file *file) +{ + struct sep_key_device *d = container_of(file->private_data, + struct sep_key_device, misc); + struct sep_key_file *f; + int ret; + + f = kzalloc(sizeof(*f), GFP_KERNEL); + if (!f) + return -ENOMEM; + /* + * misc_open serializes registration lifetime. Rust clones the device Arc + * here; subsequent ioctls and release never borrow the registration. + */ + mutex_lock(&d->lock); + ret = d->ctx ? d->open(d->ctx, &f->ctx) : -ENODEV; + f->release = d->release; + f->ioctl = d->ioctl; + mutex_unlock(&d->lock); + if (ret) { + kfree(f); + return ret; + } + file->private_data = f; + return 0; +} + +static int sep_key_release(struct inode *inode, struct file *file) +{ + struct sep_key_file *f = file->private_data; + + f->release(f->ctx); + kfree(f); + return 0; +} + +static long sep_key_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct sep_key_file *f = file->private_data; + + /* Rust serializes request transitions, never a complete blocking ioctl. */ + return f->ioctl(f->ctx, cmd, arg); +} + +static const struct file_operations sep_key_fops = { + .owner = THIS_MODULE, + .open = sep_key_open, + .release = sep_key_release, + .unlocked_ioctl = sep_key_ioctl, + .compat_ioctl = compat_ptr_ioctl, +}; + +void *sep_key_register(const char *name, u16 mode, void *ctx, + int (*open)(void *, void **), + void (*release)(void *), + long (*ioctl)(void *, unsigned int, unsigned long)) +{ + struct sep_key_device *d; + + d = kzalloc(sizeof(*d), GFP_KERNEL); + if (!d) + return NULL; + mutex_init(&d->lock); + d->ctx = ctx; + d->open = open; + d->release = release; + d->ioctl = ioctl; + d->misc.minor = MISC_DYNAMIC_MINOR; + d->misc.name = name; + d->misc.mode = mode; + d->misc.fops = &sep_key_fops; + if (misc_register(&d->misc)) { + mutex_destroy(&d->lock); + kfree(d); + return NULL; + } + return d; +} + +void sep_key_unregister(void *device) +{ + struct sep_key_device *d = device; + + misc_deregister(&d->misc); + mutex_lock(&d->lock); + d->ctx = NULL; + mutex_unlock(&d->lock); + /* Each successful open owns its context and copies of the callbacks. */ + mutex_destroy(&d->lock); + kfree(d); +} diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 3b33922788b31..e1e2247119a7c 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -24,6 +24,7 @@ mod hibe; mod hwrng; mod image; mod key; +mod key_device; mod keybag; mod keybag_identity; mod os_identity; @@ -907,7 +908,9 @@ struct SepData { // /dev/sep-key: P-256 keys created inside the SEP (see `key`). #[pin] - key_dev: Mutex>, + key_dev: Mutex>, + /// `/dev/sep-key` calls in progress (`key_device::Call`). + key_calls: Atomic, #[pin] capture: Mutex, @@ -1315,6 +1318,7 @@ impl SepData { bio_dev <- new_mutex!(None), rearm_dev <- new_mutex!(None), key_dev <- new_mutex!(None), + key_calls: Atomic::new(0), capture <- new_mutex!(capture::State::new()), key_bag_lock <- new_mutex!(()), key_touch <- new_mutex!(None), @@ -3299,6 +3303,9 @@ impl SepData { drop(dev); let dev = self.key_dev.lock().take(); drop(dev); + // Files outlive the registration; their calls end before the + // hardware goes. + key_device::wait_for_calls(self); self.rng_shutdown.store(true, Relaxed); self.control_wq.notify_all(); diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 938e9b9a529a7..bb00077532239 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -18,6 +18,14 @@ #include +/* -- application-key files ---------------------------------------------- */ + +void *sep_key_register(const char *name, u16 mode, void *ctx, + int (*open)(void *, void **), + void (*release)(void *), + long (*ioctl)(void *, unsigned int, unsigned long)); +void sep_key_unregister(void *device); + struct apple_sep_fv_new_file_key; struct dev_pm_ops; diff --git a/drivers/soc/apple/shim.rs b/drivers/soc/apple/shim.rs index e370a91b4c4e8..28ed7c8965d00 100644 --- a/drivers/soc/apple/shim.rs +++ b/drivers/soc/apple/shim.rs @@ -216,15 +216,6 @@ extern "C" { f_ioctl: Option c_long>, f_ready: Option c_int>, ) -> *mut c_void; - fn sep_bio_register_concurrent( - name: *const c_char, - mode: u16, - ctx: *mut c_void, - f_open: Option c_int>, - f_release: Option, - f_ioctl: Option c_long>, - f_ready: Option c_int>, - ) -> *mut c_void; fn sep_bio_unregister(dev: *mut c_void); fn sep_bio_wake(dev: *mut c_void); fn sep_bio_capable_admin() -> c_int; @@ -301,40 +292,6 @@ impl BioChardev { Ok(BioChardev { dev }) } - /// As [`BioChardev::register`], but ioctls run concurrently, without the - /// shim's device lock, so one that sleeps does not hold up `open()` or - /// another caller's ioctl. `f_ioctl` must do its own locking. Dropping - /// the registration waits for the ioctls that are running. - /// - /// # Safety - /// As for [`BioChardev::register`]. - pub(crate) unsafe fn register_concurrent( - name: &'static CStr, - mode: u16, - ctx: *mut c_void, - f_open: unsafe extern "C" fn(*mut c_void) -> c_int, - f_release: unsafe extern "C" fn(*mut c_void), - f_ioctl: unsafe extern "C" fn(*mut c_void, c_uint, c_ulong) -> c_long, - f_ready: unsafe extern "C" fn(*mut c_void) -> c_int, - ) -> Result { - // SAFETY: as in `register`. - let dev = unsafe { - sep_bio_register_concurrent( - name.as_char_ptr(), - mode, - ctx, - Some(f_open), - Some(f_release), - Some(f_ioctl), - Some(f_ready), - ) - }; - if dev.is_null() { - return Err(ENODEV); - } - Ok(BioChardev { dev }) - } - pub(crate) fn wake(&self) { // SAFETY: `dev` is live per the type invariant. unsafe { sep_bio_wake(self.dev) }; From 4b2e5ffe79e2c96e09fd413e4f16808a937860d3 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Mon, 16 Nov 2026 13:19:24 -0600 Subject: [PATCH 20/24] soc: apple: sep: sep-key: authorization contexts for user-bag keys A password manager whose items are sealed to Secure Enclave keys needs one authentication to cover a run of operations: macOS lets a Touch ID or password authentication be reused for a short time for exactly this (a reuse duration on the authentication context), so that revealing several items after one unlock does not ask for a finger each time. /dev/sep-key has only one-shot authorization: one match, one operation. Without more, such an app either asks for a finger for every item or unseals its vault key once and keeps it outside the enclave. Add authorization contexts (SEP_KEY_CTX_*, SEP_KEY_FEATURE_CONTEXTS), opt-in with key_context_api=1 on the 13.5 key store with per-user bags, and bounded: - a context belongs to one Unix account's bag (NEW: root or that user) and is a file descriptor: closing it ends it; - one authentication (AUTH: a Touch ID match in the user's own domain) lasts lifetime_s from the match, at most 12 hours, and ends at once when the bag locks, on suspend, on REVOKE and when the driver stops (EKEYEXPIRED after); - each SIGN or DECRYPT is still one enclave operation under that key's own access control, run with the context's credential. Each request carries an id; SEP_KEY_CANCEL_ID on the file it was made on ends it (an AUTH waiting for a finger). A request's result is delivered once, after its worker has given up the capture; a cancel or the deadline wins over an undelivered result. An AUTH straight after a cancelled one waits, bounded, for the old request's cleanup rather than fail with EBUSY, and an ended request's capture is not reported as a sensor fault. The one-match-one-operation rule stays for every other path; a context is an explicit, scoped and expiring exception requested by its owner. KUnit covers request ids, the result and context state machines; the host test runs the delivery transitions with a drop observer. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/Makefile | 2 +- drivers/soc/apple/capture.rs | 26 +- drivers/soc/apple/key.rs | 120 ++++- drivers/soc/apple/key_command.rs | 288 ++++++++++++ drivers/soc/apple/key_context.rs | 433 ++++++++++++++++++ drivers/soc/apple/key_context_device.rs | 425 +++++++++++++++++ drivers/soc/apple/key_context_shim.c | 102 +++++ drivers/soc/apple/key_context_state.rs | 177 +++++++ drivers/soc/apple/key_device.rs | 115 ++++- drivers/soc/apple/key_request.rs | 243 ++++++++++ drivers/soc/apple/key_result.rs | 126 +++++ drivers/soc/apple/key_wait.rs | 63 +++ drivers/soc/apple/key_wait_shim.c | 64 +++ drivers/soc/apple/sbio.rs | 32 +- drivers/soc/apple/sep.rs | 30 +- drivers/soc/apple/shim.h | 13 +- drivers/soc/apple/userbag.rs | 4 + include/uapi/linux/apple-sep.h | 100 ++++ .../keys/tests/check_context_lifecycle.py | 124 +++++ .../tests/test_key_context_cleanup.py | 2 +- 20 files changed, 2456 insertions(+), 33 deletions(-) create mode 100644 drivers/soc/apple/key_command.rs create mode 100644 drivers/soc/apple/key_context.rs create mode 100644 drivers/soc/apple/key_context_device.rs create mode 100644 drivers/soc/apple/key_context_shim.c create mode 100644 drivers/soc/apple/key_context_state.rs create mode 100644 drivers/soc/apple/key_request.rs create mode 100644 drivers/soc/apple/key_result.rs create mode 100644 drivers/soc/apple/key_wait.rs create mode 100644 drivers/soc/apple/key_wait_shim.c create mode 100644 tools/aurora-sep/keys/tests/check_context_lifecycle.py diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index eb3595e214a95..28ee76a4a5903 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -53,7 +53,7 @@ obj-$(CONFIG_APPLE_SEP_FV_INTERFACE) += fv_shim.o obj-$(CONFIG_APPLE_SEP) += apple-sep.o apple-sep-y := sep.o hwrng_shim.o store_shim.o bio_shim.o sha_shim.o \ crypto_shim.o sensor_shim.o p256_shim.o trusted_shim.o work_shim.o \ - pm_shim.o diag_shim.o key_shim.o attach_shim.o + pm_shim.o diag_shim.o key_shim.o key_wait_shim.o key_context_shim.o attach_shim.o obj-$(CONFIG_APPLE_PMP) += apple-pmp.o apple-pmp-y := pmp.o pmp-export.o pmp-display.o pmp-telemetry.o diff --git a/drivers/soc/apple/capture.rs b/drivers/soc/apple/capture.rs index ca25dc06c5983..21ad60263c395 100644 --- a/drivers/soc/apple/capture.rs +++ b/drivers/soc/apple/capture.rs @@ -1,14 +1,15 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT //! Shared capture ownership; an idle biometric file does not own the sensor. -use crate::SepData; +use crate::{key_request::Request, SepData}; use kernel::prelude::*; -use kernel::sync::atomic::Relaxed; +use kernel::sync::{atomic::Relaxed, Arc}; enum Owner { Idle, Sync(u64), Bio(u64), + Key(Arc), } pub(crate) struct State { @@ -33,6 +34,27 @@ impl State { Ok(self.generation) } + pub(crate) fn start_key(&mut self, request: Arc) -> Result { + if !matches!(self.owner, Owner::Idle) { + return Err(EBUSY); + } + self.owner = Owner::Key(request); + Ok(()) + } + + pub(crate) fn key(&self) -> Option> { + match &self.owner { + Owner::Key(request) => Some(request.clone()), + _ => None, + } + } + + pub(crate) fn finish_key(&mut self, request: &Arc) { + if matches!(&self.owner, Owner::Key(active) if Arc::ptr_eq(active, request)) { + self.owner = Owner::Idle; + } + } + /// Hands a synchronous owner's capture to the bio worker `epoch`. fn transfer_to_bio(&mut self, generation: u64, epoch: u64) { if matches!(self.owner, Owner::Sync(active) if active == generation) { diff --git a/drivers/soc/apple/key.rs b/drivers/soc/apple/key.rs index bc39ae1d2620c..d61b1d290ecb1 100644 --- a/drivers/soc/apple/key.rs +++ b/drivers/soc/apple/key.rs @@ -83,6 +83,9 @@ pub(crate) const FEATURE_PASSWORD: u32 = 4; /// enclave keeps no anti-replay record for it), so a FIDO PIN retry budget /// built on it would not be root-proof. aurora-fido offers no PIN without it. pub(crate) const FEATURE_PERSISTENT_ATTEMPTS: u32 = 8; +/// `INFO.features`: the authorization-context requests (`SEP_KEY_CTX_*`) +/// are accepted, as `key_device` gates them. +pub(crate) const FEATURE_CONTEXTS: u32 = 0x20; /// The longest password `SIGN_PASSWORD` / `UNSEAL_PASSWORD` take, as the /// key-bag ioctls. pub(crate) const PASSWORD_MAX: usize = crate::userbag::SECRET_MAX; @@ -139,7 +142,7 @@ pub(crate) fn create_policy(flags: u32) -> Result { /// none until this long has passed; one capture itself waits up to a minute. const TOUCH_WAIT_MS: u64 = 60_000; -const MAGIC: u32 = 0xB1; +pub(crate) const MAGIC: u32 = 0xB1; /// Handle header: `"SEPK"`, version, flags, reserved u16, blob length (LE u32). const HANDLE_MAGIC: &[u8; 4] = b"SEPK"; @@ -706,11 +709,23 @@ pub(crate) struct Handle<'a> { /// Either handle version. Version 2 is only `HANDLE_USER_BAG`. pub(crate) fn decode_any_handle(handle: &[u8; HANDLE_MAX], len: u32) -> Result> { + let len = len as usize; + if len > HANDLE_MAX { + return Err(EINVAL); + } + decode_any_handle_bytes(&handle[..len]) +} + +/// As `decode_any_handle`, for a handle copied to exactly its length. +pub(crate) fn decode_any_handle_bytes(handle: &[u8]) -> Result> { + if handle.len() < 5 { + return Err(EINVAL); + } if handle[4] != HANDLE_VERSION_USER { - let (blob, flags) = decode_handle(handle, len)?; + let (blob, flags) = decode_handle_bytes(handle)?; return Ok(Handle { blob, flags, uid: None }); } - let len = len as usize; + let len = handle.len(); if !(HANDLE_HEADER_USER + 1..=HANDLE_MAX).contains(&len) || &handle[..4] != HANDLE_MAGIC || (handle[5] != HANDLE_USER_BAG && handle[5] != HANDLE_USER_BAG | HANDLE_USER_PLAIN) @@ -768,6 +783,12 @@ pub(crate) fn require_admin_or(uid: u32) -> Result<()> { /// authenticated; the enclave enforces the record's own list either way. pub(crate) fn decode_handle(handle: &[u8; HANDLE_MAX], len: u32) -> Result<(&[u8], u8)> { let len = len as usize; + if len > HANDLE_MAX { return Err(EINVAL); } + decode_handle_bytes(&handle[..len]) +} + +pub(crate) fn decode_handle_bytes(handle: &[u8]) -> Result<(&[u8], u8)> { + let len = handle.len(); if !(HANDLE_HEADER + 1..=HANDLE_MAX).contains(&len) { return Err(EINVAL); } @@ -966,9 +987,8 @@ impl ContextDelete for SepData { } } -/// A credential context the kernel opened for one operation: deleted when -/// this is dropped, on every path out of the code that owns it (success, -/// error, cancel, timeout, signal). Its handle never leaves the kernel. +/// An owned credential, disposed on every exit unless ownership transfers +/// to a retained context. Its handle never leaves the kernel. pub(crate) struct OwnedContext<'a, D: ContextDelete + ?Sized> { owner: &'a D, handle: [u8; crate::scrd::SCRD_ACM_HANDLE_LEN], @@ -984,6 +1004,13 @@ impl<'a, D: ContextDelete + ?Sized> OwnedContext<'a, D> { &self.handle } + pub(crate) fn into_handle(mut self) -> [u8; crate::scrd::SCRD_ACM_HANDLE_LEN] { + self.disposed = true; + let handle = self.handle; + crate::image::wipe(&mut self.handle); + handle + } + fn dispose(&mut self) -> Result<()> { if self.disposed { return Ok(()); @@ -1036,6 +1063,8 @@ pub(crate) struct KeyTouch { probe: Option, again: Option>, again_status: i32, + authorization: Option>, + retained: Option, } impl KeyTouch { @@ -1066,6 +1095,8 @@ impl KeyTouch { probe: None, again: None, again_status: 0, + authorization: None, + retained: None, }) } @@ -1364,11 +1395,50 @@ impl SepData { /// A touch-gated call runs its capture in the caller's own thread, so a /// signal to the caller (or its death) ends the capture at once. pub(crate) fn capture_live(&self) -> bool { - (self.key_touch_active.load(Relaxed) - && !self.key_touch_cancel.load(Relaxed) - && !self.shutting_down.load(Relaxed) - && !kernel::current!().signal_pending()) - || crate::bio::capture_is_live(&self.bio_session.lock(), self.capture_epoch.load(Relaxed)) + if self.shutting_down.load(Relaxed) || self.suspending.load(Relaxed) { return false; } + if self.key_touch_active.load(Relaxed) { + return !self.key_touch_cancel.load(Relaxed) + && !kernel::current!().signal_pending() + && self.key_authorization_request().map_or(true, |request| request.is_live()); + } + crate::bio::capture_is_live(&self.bio_session.lock(), self.capture_epoch.load(Relaxed)) + } + + pub(crate) fn key_authorization_request(&self) -> Option> { + self.key_touch.lock().as_ref().and_then(|job| job.authorization.clone()) + } + + pub(crate) fn retain_key_authorization(&self, credential: OwnedContext<'_, Self>) -> Result<()> { + let request = self.key_authorization_request().ok_or(EIO)?; + let crate::key_request::Input::Authenticate(context) = &request.data.input else { return Err(EIO); }; + let retained = crate::key_context::Credential::new(context.sep.clone(), credential.into_handle()); + let mut guard = self.key_touch.lock(); + let job = (&mut *guard).as_mut().ok_or(EIO)?; + job.retained = Some(retained); + Ok(()) + } + + pub(crate) fn key_authorize(&self, request: kernel::sync::Arc) + -> Result + { + let bag = match &request.data.input { + crate::key_request::Input::Authenticate(context) => context.bag, + _ => None, + }; + // A bag context authorizes that bag's user-presence keys. + let constraint = match bag { + Some(_) => presence_constraint()?, + None => self.key_bio_acl(true)?.constraint, + }; + let mut job = KeyTouch::new(TouchOp::Sign, &[], &[0; DIGEST_LEN], &[], constraint)?; + job.grant = None; + job.authorization = Some(request.clone()); + let result = self.key_touch_wait(job); + // An abandoned frame wait is not a sensor failure when its owner + // has cancelled or expired. Check this even if capture returned Err. + request.check_live()?; + let mut job = result?; + job.retained.take().ok_or(EIO) } fn key_bio_acl(&self, any: bool) -> Result { @@ -1589,13 +1659,24 @@ impl SepData { let mut job = KeyTouch::new_in(op, blob, digest, sealed, constraint, bag)?; job.probe = probe; let _capture = crate::capture::Lease::acquire(self)?; + self.key_touch_wait(job) + } + + fn key_touch_wait(&self, job: KeyTouch) -> Result { + let op = job.op; // Serialises with /dev/sep-bio's enrol and verify work, as they do // with each other, and with every other touch-gated call: one job, // one grant, one context at a time. let _writer = self.catacomb_writer.lock(); // A key under a user's bag is matched in that user's domain, and // only after its password unlocked the bag. - let _subject = match job.grant.as_ref().and_then(|g| g.bag()) { + let bag = job.grant.as_ref().and_then(|g| g.bag()).or_else(|| { + job.authorization.as_ref().and_then(|request| match &request.data.input { + crate::key_request::Input::Authenticate(context) => context.bag, + _ => None, + }) + }); + let _subject = match bag { Some(uid) => match self.userbag_touch_subject(uid)? { Some(subject) => Some(crate::SubjectScope::new(self, subject)), None => None, @@ -1608,9 +1689,12 @@ impl SepData { dev_dbg!(self.dev, "sep-key: waiting for a Touch ID match ({:?})\n", op); let deadline = crate::shim::boottime_ns().saturating_add(TOUCH_WAIT_MS * 1_000_000); let ended = loop { - if self.shutting_down.load(Relaxed) { + if self.shutting_down.load(Relaxed) || self.suspending.load(Relaxed) { break Err(ENODEV); } + if let Some(request) = self.key_authorization_request() { + if let Err(error) = request.check_live() { break Err(error); } + } if self.key_touch_cancel.load(Relaxed) { break Err(ECANCELED); } @@ -2267,7 +2351,13 @@ impl SepData { let user = UserPtr::from_addr(arg); match read_request(cmd, arg)? { // Password keys live in per-user bags (see `userbag`). - KeyRequest::Info => write_user(user, &info_for(self.profile.per_user_keybags)), + KeyRequest::Info => { + let mut info = info_for(self.profile.per_user_keybags); + if crate::key_device::contexts_supported(self) { + info.features |= FEATURE_CONTEXTS; + } + write_user(user, &info) + } KeyRequest::Create(mut req, policy) => { may_create_machine_key(crate::shim::capable_admin(), crate::shim::current_euid())?; let (acl, flags) = match policy { @@ -2306,7 +2396,7 @@ impl SepData { KeyRequest::Cancel => { // Ends the touch-gated call that is waiting, whoever made it: // like pulling a security key, it can only make a request fail. - if self.key_touch_active.load(Relaxed) { + if self.key_touch_active.load(Relaxed) && self.key_authorization_request().is_none() { self.key_touch_cancel.store(true, Relaxed); dev_dbg!(self.dev, "sep-key: waiting operation cancelled\n"); } diff --git a/drivers/soc/apple/key_command.rs b/drivers/soc/apple/key_command.rs new file mode 100644 index 0000000000000..d94f929d5b2d5 --- /dev/null +++ b/drivers/soc/apple/key_command.rs @@ -0,0 +1,288 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Bounded FIFO for context operations that need no sensor lease. + +use crate::{key_request::Input, key_request::Request, SepData}; +use kernel::alloc::flags::GFP_KERNEL_ACCOUNT; +use kernel::prelude::*; +use kernel::sync::{ + atomic::{Acquire, Relaxed, Release}, + Arc, +}; + +pub(crate) const MAX_COMMANDS: usize = 32; + +pub(crate) struct State { + requests: KVec>, +} + +impl State { + pub(crate) fn new() -> Self { + Self { + requests: KVec::new(), + } + } + + fn admit(&mut self, request: Arc) -> Result { + if self.requests.len() >= MAX_COMMANDS { + return Err(EBUSY); + } + self.requests.push(request, GFP_KERNEL_ACCOUNT)?; + Ok(()) + } + + fn finish(&mut self, request: &Arc) { + self.requests.retain(|active| !Arc::ptr_eq(active, request)); + } + + pub(crate) fn cancel(&self, reason: Error) { + for request in &self.requests { + let _ = request.cancel(reason); + } + } +} + +/// Marks the real frame wait, not merely an accepted authentication request. +/// The exclusive capture owner makes this a single sensor-wide interval. +pub(crate) struct SensorWait<'a>(&'a SepData); +impl<'a> SensorWait<'a> { + pub(crate) fn new(sep: &'a SepData) -> Self { + sep.key_sensor_waiting.store(true, Release); + Self(sep) + } +} +impl Drop for SensorWait<'_> { + fn drop(&mut self) { + self.0.key_sensor_waiting.store(false, Release); + } +} + +impl SepData { + pub(crate) fn queue_context_command(this: Arc, request: Arc) -> Result { + if !matches!( + request.data.input, + Input::ContextSign(_, _) | Input::ContextDecrypt(_, _) + ) { + return Err(EINVAL); + } + // Remove/PM set their admission flag, then take this same lock to join + // every enqueue. FIFO dispatch and cleanup share one ordered queue. + let mut commands = this.key_commands.lock(); + if this.shutting_down.load(Relaxed) { + return Err(ENODEV); + } + if this.suspending.load(Relaxed) { + return Err(EBUSY); + } + commands.admit(request.clone())?; + let sep = this.clone(); + let work = request.clone(); + if let Err(error) = this + .key_context_queue + .try_spawn(GFP_KERNEL_ACCOUNT, move || { + sep.run_context_command(&work); + }) + { + commands.finish(&request); + return Err(error.into()); + } + Ok(()) + } + + fn run_context_command(&self, work: &Arc) { + let running = !self.shutting_down.load(Relaxed) && work.is_live() && self.capture_begin(); + if running { + // Identity operations already serialize inside the upstream + // key-bag helpers; they do not wait on the sensor owner. + if work.is_live() { + let result = (|| { + work.check_live()?; + let (context, decrypt) = match &work.data.input { + Input::ContextSign(context, _) => (context, false), + Input::ContextDecrypt(context, _) => (context, true), + _ => return Err(EINVAL), + }; + let lease = context.acquire()?; + let blob = &work.data.blob; + let acm = Some(lease.handle()); + let result = match (&work.data.input, context.bag) { + (Input::ContextSign(_, digest), None) => { + self.user_key_sign(blob, digest, acm) + } + (Input::ContextDecrypt(_, sealed), None) => { + self.user_key_unseal(blob, sealed, acm) + } + (Input::ContextSign(_, digest), Some(uid)) => crate::key::bag_user(uid) + .and_then(|u| self.user_key_sign_in_bag(u, blob, digest, acm)), + (Input::ContextDecrypt(_, sealed), Some(uid)) => crate::key::bag_user(uid) + .and_then(|u| self.user_key_unseal_in_bag(u, blob, sealed, acm)), + _ => Err(EINVAL), + } + .map(crate::Secret); + drop(lease); + if self.key_context_failed.load(Relaxed) { + return Err(EIO); + } + result.map(|value| { + if decrypt { + crate::key_request::Output::Plaintext(value) + } else { + crate::key_request::Output::Signature(value) + } + }) + })(); + if matches!(result, Err(error) if error == EIO) { + if let Input::ContextSign(context, _) | Input::ContextDecrypt(context, _) = + &work.data.input + { + context.revoke(EIO); + } + } + work.complete(result); + } else { + work.complete(Err( + if crate::shim::monotonic_ns() >= work.data.deadline_ns { + ETIMEDOUT + } else { + ECANCELED + }, + )); + } + self.capture_end(); + } else { + let error = if self.shutting_down.load(Relaxed) { + ENODEV + } else if crate::shim::monotonic_ns() >= work.data.deadline_ns { + ETIMEDOUT + } else { + ECANCELED + }; + work.complete(Err(error)); + } + self.key_commands.lock().finish(work); + work.retire(); + } + + pub(crate) fn cancel_context_commands(&self, reason: Error) { + self.key_commands.lock().cancel(reason); + } + + pub(crate) fn context_sensor_waiting( + &self, + context: &Arc, + ) -> bool { + if !self.key_sensor_waiting.load(Acquire) { + return false; + } + let Some(request) = self.capture.lock().key() else { + return false; + }; + request.is_live() + && matches!(&request.data.input, + Input::Authenticate(active) if Arc::ptr_eq(active, context)) + } +} + +impl SepData { + /// Starts `request`'s authentication. A request that has ended (cancel, + /// deadline) keeps the capture until its worker has cleaned up; a new + /// one waits for that, for at most `WORKER_EXIT_WAIT_MS`, rather than + /// fail with EBUSY, which the caller cannot tell from a sensor in use. + pub(crate) fn queue_key(this: Arc, request: Arc) -> Result { + let mut waited: u32 = 0; + loop { + match Self::try_queue_key(this.clone(), request.clone()) { + Err(error) + if error == EBUSY + && waited < crate::WORKER_EXIT_WAIT_MS + && this.key_capture_ending() => + { + kernel::time::delay::fsleep(kernel::time::Delta::from_millis(10)); + waited += 10; + } + result => return result, + } + } + } + + /// The capture is held by a key request that has already ended. + fn key_capture_ending(&self) -> bool { + self.capture + .lock() + .key() + .is_some_and(|active| !active.is_live()) + } + + /// Whether a key touch's capture is still wanted: false once the context + /// request that owns it has ended. Other key touches end only by fault. + pub(crate) fn key_capture_wanted(&self) -> bool { + self.capture + .lock() + .key() + .map_or(true, |active| active.is_live()) + } + + fn try_queue_key(this: Arc, request: Arc) -> Result { + let Input::Authenticate(context) = &request.data.input else { + return Err(EINVAL); + }; + let mut capture = this.capture.lock(); + if this.shutting_down.load(Relaxed) { + return Err(ENODEV); + } + if this.suspending.load(Relaxed) { + return Err(EBUSY); + } + if this.key_context_failed.load(Relaxed) { + return Err(EIO); + } + capture.start_key(request.clone())?; + if let Err(error) = context.authenticate(request.id, &request.completion) { + capture.finish_key(&request); + return Err(error); + } + let sep = this.clone(); + let work = request.clone(); + if let Err(_error) = this.key_auth_queue.try_spawn(GFP_KERNEL_ACCOUNT, move || { + sep.run_key_authentication(&work); + }) { + capture.finish_key(&request); + drop(capture); + context.finish_auth(&request.completion, Err(EIO)); + context.forget(&request.completion); + request.retire(); + return match request.completion.claim() { + Err(error) => Err(error), + Ok(_) => Err(EIO), + }; + } + Ok(()) + } + + fn run_key_authentication(&self, request: &Arc) { + let Input::Authenticate(context) = &request.data.input else { + return; + }; + let result = (|| { + request.check_live()?; + context.check_domain()?; + if !self.capture_begin() { + return Err(ENODEV); + } + let result = self.key_authorize(request.clone()); + self.capture_end(); + result + })(); + context.finish_auth( + &request.completion, + result.map(|credential| (credential, Ok(()))), + ); + self.capture.lock().finish_key(request); + request.retire(); + } + + pub(crate) fn cancel_key(&self, reason: Error) { + if let Some(request) = self.capture.lock().key() { + let _ = request.cancel(reason); + } + } +} diff --git a/drivers/soc/apple/key_context.rs b/drivers/soc/apple/key_context.rs new file mode 100644 index 0000000000000..5d67fff4ad18e --- /dev/null +++ b/drivers/soc/apple/key_context.rs @@ -0,0 +1,433 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Kernel-owned authorization, hardware leases and asynchronous disposal. + +use crate::{ + key_context_state::{Cleanup, Phase, State}, + key_request as keys, + key_request::Completion, + key_wait::Event, + scrd, SepData, +}; +use kernel::alloc::flags::GFP_KERNEL_ACCOUNT; +use kernel::prelude::*; +use kernel::sync::{atomic::Relaxed, Arc, Mutex}; +use kernel::workqueue::{impl_has_work, new_work, Work, WorkItem}; + +pub(crate) const MAX_CONTEXTS: usize = 16; +pub(crate) const MAX_REQUESTS: usize = 32; + +/// Owned provisional credential while the capture worker finishes retiring. +/// If publication loses to cancellation, this owner still disposes it once. +pub(crate) struct Credential { + sep: Arc, + handle: [u8; scrd::SCRD_ACM_HANDLE_LEN], + transferred: bool, +} + +impl Credential { + pub(crate) fn new(sep: Arc, handle: [u8; scrd::SCRD_ACM_HANDLE_LEN]) -> Self { + Self { + sep, + handle, + transferred: false, + } + } + + fn into_handle(mut self) -> [u8; scrd::SCRD_ACM_HANDLE_LEN] { + self.transferred = true; + let handle = self.handle; + crate::image::wipe(&mut self.handle); + handle + } +} + +impl Drop for Credential { + fn drop(&mut self) { + if !self.transferred { + let _ = self.sep.delete_unlock_evaluation(&self.handle); + } + crate::image::wipe(&mut self.handle); + } +} + +struct Grant([u8; scrd::SCRD_ACM_HANDLE_LEN]); +impl Drop for Grant { + fn drop(&mut self) { + crate::image::wipe(&mut self.0); + } +} + +struct Inner { + life: State, + grant: Option, + deliveries: KVec>, + ids: keys::RequestIds, + closing: bool, + /// When the matched grant stops authorizing (monotonic ns); 0 never. + expires_ns: u64, +} + +#[pin_data] +pub(crate) struct Context { + pub(crate) sep: Arc, + generation: u64, + /// The user whose bag this context authorizes keys under: its match runs + /// in that user's domain, and the bag's lock revokes it. None for the + /// identity bag's biometric keys. + pub(crate) bag: Option, + /// How long one match authorizes, from the match; 0 until revoked. + lifetime_ns: u64, + #[pin] + inner: Mutex, + changed: Event, + #[pin] + work: Work, +} + +impl_has_work! { impl HasWork for Context { self.work } } + +/// Only a running hardware use retains this lease; user copying never does. +pub(crate) struct Lease<'a> { + context: &'a Arc, + grant: Grant, +} +impl Lease<'_> { + pub(crate) fn handle(&self) -> &[u8; scrd::SCRD_ACM_HANDLE_LEN] { + &self.grant.0 + } +} +impl Drop for Lease<'_> { + fn drop(&mut self) { + self.context.inner.lock().life.release(); + self.context.changed.wake(); + self.context.schedule(); + } +} + +impl Context { + pub(crate) fn new( + sep: Arc, + expected_generation: u64, + bag: Option, + lifetime_s: u32, + ) -> Result> { + let mut contexts = sep.key_contexts.lock(); + if sep.shutting_down.load(Relaxed) || sep.suspending.load(Relaxed) { + return Err(ENODEV); + } + if expected_generation != sep.key_context_generation.load(Relaxed) { + return Err(ENODEV); + } + if contexts.len() >= MAX_CONTEXTS { + return Err(ENOSPC); + } + let owner = sep.clone(); + let generation = sep.key_context_generation.load(Relaxed); + let context = Arc::pin_init( + try_pin_init!(Self { + sep: owner, + generation, + bag, + lifetime_ns: u64::from(lifetime_s) * 1_000_000_000, + inner <- kernel::new_mutex!(Inner { + life: State::new(), + grant: None, + deliveries: KVec::new(), + ids: keys::RequestIds::new(), + closing: false, + expires_ns: 0, + }), + changed: Event::new()?, + work <- new_work!("sep_context_cleanup"), + }), + GFP_KERNEL_ACCOUNT, + )?; + contexts.push(context.clone(), GFP_KERNEL_ACCOUNT)?; + Ok(context) + } + + pub(crate) fn check_domain(&self) -> Result { + if self.sep.shutting_down.load(Relaxed) + || self.sep.suspending.load(Relaxed) + || self.generation != self.sep.key_context_generation.load(Relaxed) + { + return Err(ENODEV); + } + Ok(()) + } + + fn schedule(self: &Arc) { + // Shutdown takes this same registry lock before draining the queue. + let _contexts = self.sep.key_contexts.lock(); + if self.sep.shutting_down.load(Relaxed) { + return; + } + { + let inner = self.inner.lock(); + if inner.life.phase != Phase::Revoking && !inner.closing { + return; + } + } + let _ = self.sep.key_context_queue.enqueue(self.clone()); + } + + pub(crate) fn authenticate(&self, id: u64, completion: &Arc) -> Result { + let mut inner = self.inner.lock(); + inner.ids.check(id)?; + inner + .deliveries + .push(completion.clone(), GFP_KERNEL_ACCOUNT)?; + if let Err(error) = inner.life.authenticate() { + inner.deliveries.pop(); + return Err(error); + } + inner.ids.admit(id) + } + + pub(crate) fn finish_auth( + self: &Arc, + completion: &Completion, + result: Result<(Credential, Result)>, + ) { + let mut inner = self.inner.lock(); + let status = match result { + Ok((credential, status)) => { + inner.grant = Some(Grant(credential.into_handle())); + if self.lifetime_ns != 0 { + inner.expires_ns = crate::shim::monotonic_ns().saturating_add(self.lifetime_ns); + } + if self.sep.key_context_failed.load(Relaxed) { + Err(EIO) + } else if completion.is_live() { + status + } else { + Err(ECANCELED) + } + } + Err(error) => Err(error), + }; + if inner.life.authenticated(status) { + if !completion.complete(Ok(keys::Output::Authenticated)) { + inner.life.revoke(ECANCELED); + } + } else { + completion.complete(Err(status.err().unwrap_or(EKEYREVOKED))); + } + drop(inner); + self.changed.wake(); + self.schedule(); + } + + pub(crate) fn register(&self, completion: &Arc) -> Result { + let mut inner = self.inner.lock(); + if !inner.life.claim_allowed() { + return Err(EKEYREVOKED); + } + if inner.deliveries.len() >= MAX_REQUESTS { + return Err(EBUSY); + } + inner + .deliveries + .push(completion.clone(), GFP_KERNEL_ACCOUNT)?; + Ok(()) + } + + pub(crate) fn forget(&self, completion: &Arc) { + self.inner + .lock() + .deliveries + .retain(|active| !Arc::ptr_eq(active, completion)); + } + + pub(crate) fn acquire(self: &Arc) -> Result> { + self.check_domain()?; + if self.sep.key_context_failed.load(Relaxed) { + return Err(EIO); + } + let mut inner = self.inner.lock(); + if inner.expires_ns != 0 && crate::shim::monotonic_ns() >= inner.expires_ns { + drop(inner); + self.revoke(EKEYEXPIRED); + return Err(EKEYEXPIRED); + } + let grant = Grant(inner.grant.as_ref().ok_or(EKEYREVOKED)?.0); + inner.life.acquire()?; + Ok(Lease { + context: self, + grant, + }) + } + + pub(crate) fn claim(&self, completion: &Arc) -> Result { + let mut inner = self.inner.lock(); + // Lock order is context then delivery. Nothing holds either at copyout. + if self.check_domain().is_err() { + let _ = completion.cancel(ENODEV); + } + if !inner.life.claim_allowed() { + let _ = completion.cancel(EKEYREVOKED); + } + if self.sep.key_context_failed.load(Relaxed) { + let _ = completion.cancel(EIO); + } + let result = completion.claim(); + inner + .deliveries + .retain(|active| !Arc::ptr_eq(active, completion)); + result + } + + pub(crate) fn mark_revoke(&self, reason: Error) { + let mut inner = self.inner.lock(); + inner.life.revoke(reason); + for delivery in &inner.deliveries { + let _ = delivery.cancel(reason); + } + self.changed.wake(); + } + + pub(crate) fn revoke(self: &Arc, reason: Error) { + self.mark_revoke(reason); + self.schedule(); + } + + pub(crate) fn wait_revoke(&self, deadline_ns: u64) -> Result { + loop { + let sequence = self.changed.sequence(); + if let Some(result) = self.inner.lock().life.revocation_result() { + return result; + } + if crate::shim::monotonic_ns() >= deadline_ns { + return Err(ETIMEDOUT); + } + self.changed.wait(sequence, deadline_ns)?; + } + } + + pub(crate) fn cancel(self: &Arc, id: u64) -> Result { + let mut inner = self.inner.lock(); + if inner.ids.cancel_ahead(id)? { + return Ok(()); + } + if id != inner.ids.last() { + return Err(ENOENT); + } + if inner.life.phase == Phase::Authorized { + return Err(EALREADY); + } + if inner.life.phase != Phase::Authenticating { + return Err(ENOENT); + } + // Publish authorization and cancel under the same context -> delivery + // lock order. Cancel cannot report success after a grant was published. + inner.deliveries.first().ok_or(ENOENT)?.cancel(ECANCELED)?; + inner.life.revoke(ECANCELED); + for delivery in &inner.deliveries { + let _ = delivery.cancel(ECANCELED); + } + drop(inner); + self.changed.wake(); + self.schedule(); + Ok(()) + } + + pub(crate) fn query(&self) -> (u32, u32, i32) { + let inner = self.inner.lock(); + ( + inner.life.phase as u32, + inner.life.cleanup as u32, + inner.life.reason.map_or(0, Error::to_errno), + ) + } + + pub(crate) fn close(self: &Arc) { + self.inner.lock().closing = true; + self.revoke(EKEYREVOKED); + } + + fn cleanup(&self) { + let grant = { + let mut inner = self.inner.lock(); + if !inner.life.claim_cleanup() { + return; + } + inner.grant.take() + }; + let result = if let Some(grant) = grant { + // Cleanup shares the ordered command lane; SCRD serializes packets. + self.sep.delete_unlock_evaluation(&grant.0) + } else if self.sep.key_context_failed.load(Relaxed) { + Err(EIO) + } else { + Ok(()) + }; + self.inner.lock().life.disposed(result); + self.changed.wake(); + } +} + +impl WorkItem for Context { + type Pointer = Arc; + fn run(this: Arc) { + this.cleanup(); + // Lock order: registry then context. Remove only after disposal; NEW + // and closing contexts both count against the per-provider bound. + let mut contexts = this.sep.key_contexts.lock(); + let inner = this.inner.lock(); + if inner.closing && inner.life.phase == Phase::Dead && inner.life.cleanup != Cleanup::Failed + { + contexts.retain(|context| !Arc::ptr_eq(context, &this)); + } + } +} + +impl SepData { + fn context_snapshot(&self) -> [Option>; MAX_CONTEXTS] { + let contexts = self.key_contexts.lock(); + let mut snapshot = core::array::from_fn(|_| None); + for (slot, context) in snapshot.iter_mut().zip(contexts.iter()) { + *slot = Some(context.clone()); + } + snapshot + } + + /// The bag of `uid` locked: its contexts stop authorizing at once. + pub(crate) fn revoke_bag_contexts(&self, uid: u32) { + for context in self.context_snapshot().into_iter().flatten() { + if context.bag == Some(uid) { + context.revoke(EKEYREVOKED); + } + } + } + + pub(crate) fn revoke_key_contexts(&self, reason: Error) { + self.key_context_generation.fetch_add(1, Relaxed); + for context in self.context_snapshot().into_iter().flatten() { + context.revoke(reason); + } + } + + pub(crate) fn drain_key_contexts(&self, deadline_ns: u64) -> Result { + for context in self.context_snapshot().into_iter().flatten() { + // Disposal failure poisons a physically idle provider. It does not + // justify preventing sleep once every command/user has drained. + match context.wait_revoke(deadline_ns) { + Err(error) if error == EIO => {} + result => result?, + } + } + Ok(()) + } + + pub(crate) fn remove_key_contexts(&self) { + // Shutdown has stopped every producer and the key worker was flushed. + // Queue the final disposals before draining this dedicated queue. + for context in self.context_snapshot().into_iter().flatten() { + context.mark_revoke(ENODEV); + let _ = self.key_context_queue.enqueue(context); + } + self.key_context_queue.drain(); + self.key_contexts.lock().clear(); + } +} + diff --git a/drivers/soc/apple/key_context_device.rs b/drivers/soc/apple/key_context_device.rs new file mode 100644 index 0000000000000..8cc2857eaccae --- /dev/null +++ b/drivers/soc/apple/key_context_device.rs @@ -0,0 +1,425 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Authorization contexts on `/dev/sep-key` (`SEP_KEY_CTX_*` in +//! `include/uapi/linux/apple-sep.h`, a design draft). + +use super::*; +use crate::key_context::Context; + +#[derive(Clone, Copy)] +#[repr(C)] +pub(super) struct Buffer { + pub(super) len: u32, + pub(super) reserved: u32, + pub(super) ptr: u64, +} +/// `flags` NEW_BAG: the context authorizes keys under `uid`'s bag (root or +/// that user). `lifetime_s`: how long one match authorizes, from the match; +/// 0 until revoked or closed. +#[repr(C)] +struct NewContext { + flags: u32, + fd: i32, + uid: u32, + lifetime_s: u32, +} +const NEW_BAG: u32 = 1; +/// ECIES to a P-256 key: the uncompressed ephemeral point, then the 16-byte +/// tag; the IV is derived, so this is all that is not ciphertext. +const SEALED_OVERHEAD: usize = crate::refkey_seal::POINT_LEN + 16; +const UNCOMPRESSED_POINT: u8 = 0x04; +const METHOD_TOUCH: u32 = 1; +const OUTCOME_OK: u32 = 0; +const OUTCOME_NOT_MATCHED: u32 = 1; +const OUTCOME_FAILED: u32 = 5; +const LIFETIME_MAX_S: u32 = 12 * 3600; +#[repr(C)] +struct Authenticate { + id: u64, + timeout_ms: u32, + method: u32, + outcome: u32, + reserved: u32, +} +#[repr(C)] +struct Query { + phase: u32, + cleanup: u32, + reason: i32, + activity_flags: u32, +} +#[repr(C)] +struct Revoke { + timeout_ms: u32, + reserved: u32, +} +#[repr(C)] +struct ContextSign { + id: u64, + timeout_ms: u32, + context_fd: i32, + blob_len: u32, + reserved: u32, + blob_ptr: u64, + digest: [u8; 32], + sig_len: u32, + reserved2: u32, + signature: [u8; 72], +} +#[repr(C)] +struct ContextDecrypt { + id: u64, + timeout_ms: u32, + context_fd: i32, + blob: Buffer, + sealed: Buffer, + plain: Buffer, +} + +static_assert!(core::mem::size_of::() == 16); +static_assert!(core::mem::size_of::() == 24); +static_assert!(core::mem::size_of::() == 16); +static_assert!(core::mem::size_of::() == 8); +static_assert!(core::mem::size_of::() == 144); +static_assert!(core::mem::size_of::() == 64); +// SAFETY: fixed-width repr(C) layouts, no padding, all bit patterns valid. +unsafe impl kernel::transmute::FromBytes for NewContext {} +unsafe impl kernel::transmute::AsBytes for NewContext {} +unsafe impl kernel::transmute::FromBytes for Authenticate {} +unsafe impl kernel::transmute::AsBytes for Authenticate {} +unsafe impl kernel::transmute::AsBytes for Query {} +unsafe impl kernel::transmute::FromBytes for Revoke {} +unsafe impl kernel::transmute::FromBytes for ContextSign {} +unsafe impl kernel::transmute::AsBytes for ContextSign {} +unsafe impl kernel::transmute::FromBytes for ContextDecrypt {} +unsafe impl kernel::transmute::AsBytes for ContextDecrypt {} + +const NEW: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x40); +const AUTH: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x41); +const QUERY: u32 = kernel::ioctl::_IOR::(crate::key::MAGIC, 0x42); +const REVOKE: u32 = kernel::ioctl::_IOW::(crate::key::MAGIC, 0x43); +const SIGN: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x44); +const DECRYPT: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x45); + +pub(super) fn handles(cmd: u32) -> bool { + matches!(cmd, NEW | SIGN | DECRYPT) +} + +extern "C" { + fn sep_context_file_new( + context: *mut c_void, + release: unsafe extern "C" fn(*mut c_void), + ioctl: unsafe extern "C" fn(*mut c_void, c_uint, c_ulong) -> c_long, + file: *mut *mut c_void, + fd: *mut c_int, + ) -> c_int; + fn sep_context_file_finish(file: *mut c_void, fd: c_int, install: bool); + fn sep_context_file_get(fd: c_int, context: *mut *mut c_void) -> *mut c_void; + fn sep_context_file_put(file: *mut c_void); +} + +pub(super) fn deadline(timeout_ms: u32) -> Result { + if !(1..=120_000).contains(&timeout_ms) { + return Err(EINVAL); + } + Ok(crate::shim::monotonic_ns().saturating_add(u64::from(timeout_ms) * 1_000_000)) +} + +fn read(buffer: &Buffer, min: usize) -> Result> { + if buffer.reserved != 0 || !(min..=keys::HANDLE_MAX).contains(&(buffer.len as usize)) { + return Err(EINVAL); + } + let mut data = KVec::new(); + data.resize( + buffer.len as usize, + 0, + kernel::alloc::flags::GFP_KERNEL_ACCOUNT, + )?; + UserSlice::new(UserPtr::from_addr(buffer.ptr as usize), data.len()) + .reader() + .read_slice(&mut data)?; + Ok(data) +} + +/// The record behind a handle the context may authorize: an identity-bag +/// biometric key, or a user-presence key under the context's own bag. +fn decode_record(handle: &[u8], bag: Option) -> Result> { + let h = keys::decode_any_handle_bytes(handle)?; + let allowed = match bag { + None => h.uid.is_none() && h.flags & keys::HANDLE_BIOMETRY != 0, + Some(uid) => h.uid == Some(uid) && h.flags == keys::HANDLE_USER_BAG, + }; + if !allowed { + return Err(EOPNOTSUPP); + } + let mut record = KVec::new(); + record.extend_from_slice(h.blob, kernel::alloc::flags::GFP_KERNEL_ACCOUNT)?; + Ok(record) +} + +fn get_context(fd: i32) -> Result> { + let mut pointer = core::ptr::null_mut(); + // SAFETY: the shim validates the file operations and holds an fget reference. + let file = unsafe { sep_context_file_get(fd, &mut pointer) }; + if file.is_null() { + return Err(EBADF); + } + // SAFETY: the file owns this foreign Arc until the matching put below. + let context: Arc = unsafe { as ForeignOwnable>::borrow(pointer) }.into(); + // SAFETY: release exactly the reference returned by file_get. + unsafe { sep_context_file_put(file) }; + Ok(context) +} + +impl File { + fn context(&self, fd: i32) -> Result> { + let context = get_context(fd)?; + if !Arc::ptr_eq(&context.sep, &self.sep) { + return Err(EXDEV); + } + context.check_domain()?; + Ok(context) + } + + fn context_execute( + &self, + context: &Arc, + request: Arc, + ) -> Result { + context.register(&request.completion)?; + let result = (|| { + SepData::queue_context_command(self.sep.clone(), request.clone())?; + request.completion.wait_ready()?; + context.claim(&request.completion) + })(); + context.forget(&request.completion); + result + } + + pub(super) fn context_ioctl(&self, cmd: u32, user: UserPtr) -> Result { + self.check_api()?; + if self.context_generation != self.sep.key_context_generation.load(Relaxed) { + return Err(ENODEV); + } + if cmd == NEW { + let mut out: NewContext = UserSlice::new(user, 16).reader().read()?; + if out.flags & !NEW_BAG != 0 || out.fd != -1 || out.lifetime_s > LIFETIME_MAX_S { + return Err(EINVAL); + } + let bag = if out.flags & NEW_BAG != 0 { + keys::bag_user(out.uid)?; + keys::require_admin_or(out.uid)?; + Some(out.uid) + } else { + if out.uid != 0 { + return Err(EINVAL); + } + None + }; + let context = Context::new( + self.sep.clone(), + self.context_generation, + bag, + out.lifetime_s, + )?; + let pointer = context.clone().into_foreign(); + let mut file = core::ptr::null_mut(); + let mut fd = -1; + // SAFETY: ownership transfers to C only on success; callbacks match. + let status = unsafe { + sep_context_file_new(pointer, release_context, context_ioctl, &mut file, &mut fd) + }; + if status != 0 { + // SAFETY: failed creation did not consume this foreign Arc. + drop(unsafe { as ForeignOwnable>::from_foreign(pointer) }); + context.close(); + return Err(Error::from_errno(status)); + } + out.fd = fd; + let result = UserSlice::new(user, 16).writer().write(&out); + // SAFETY: reserved fd and file belong to this task. Install only + // after copyout; failure releases both and asynchronously closes. + unsafe { sep_context_file_finish(file, fd, result.is_ok()) }; + return result; + } + if cmd == SIGN { + let mut out: ContextSign = UserSlice::new(user, 144).reader().read()?; + if out.reserved != 0 + || out.reserved2 != 0 + || out.sig_len != 0 + || out.signature != [0; 72] + { + return Err(EINVAL); + } + let deadline_ns = deadline(out.timeout_ms)?; + let blob = read( + &Buffer { + len: out.blob_len, + reserved: 0, + ptr: out.blob_ptr, + }, + 1, + )?; + let context = self.context(out.context_fd)?; + let blob = decode_record(&blob, context.bag)?; + let request = Request::new( + out.id, + crate::key_request::KeyRequest { + blob, + input: crate::key_request::Input::ContextSign(context.clone(), out.digest), + deadline_ns, + }, + )?; + let _active = self.begin(request.clone())?; + let crate::key_request::Output::Signature(signature) = + self.context_execute(&context, request)? + else { + return Err(EIO); + }; + if signature.is_empty() || signature.len() > keys::SIG_MAX { + return Err(EIO); + } + out.sig_len = signature.len() as u32; + out.signature[..signature.len()].copy_from_slice(&signature); + return UserSlice::new(user, 144).writer().write(&out); + } + let mut out: ContextDecrypt = UserSlice::new(user, 64).reader().read()?; + let deadline_ns = deadline(out.timeout_ms)?; + let blob = read(&out.blob, 1)?; + let sealed = read(&out.sealed, SEALED_OVERHEAD)?; + if sealed.len() > keys::SEALED_MAX + || sealed[0] != UNCOMPRESSED_POINT + || out.plain.reserved != 0 + || out.plain.len as usize > keys::HANDLE_MAX + { + return Err(EINVAL); + } + if (out.plain.len as usize) < sealed.len() - SEALED_OVERHEAD { + return Err(ENOSPC); + } + let context = self.context(out.context_fd)?; + let blob = decode_record(&blob, context.bag)?; + let request = Request::new( + out.id, + crate::key_request::KeyRequest { + blob, + input: crate::key_request::Input::ContextDecrypt(context.clone(), sealed), + deadline_ns, + }, + )?; + let _active = self.begin(request.clone())?; + let crate::key_request::Output::Plaintext(plain) = + self.context_execute(&context, request)? + else { + return Err(EIO); + }; + if plain.len() > out.plain.len as usize || plain.len() > keys::SECRET_MAX { + return Err(EIO); + } + UserSlice::new( + UserPtr::from_addr(out.plain.ptr as usize), + out.plain.len as usize, + ) + .writer() + .write_slice(&plain)?; + out.plain.len = plain.len() as u32; + UserSlice::new(user, 64).writer().write(&out) + } +} + +/// A context file's ioctl. +/// +/// # Safety +/// `pointer` is the foreign `Context` Arc the anonymous file holds; the file +/// reference the VFS holds for this call keeps it from being released. +unsafe extern "C" fn context_ioctl(pointer: *mut c_void, cmd: c_uint, arg: c_ulong) -> c_long { + // SAFETY: live until `release_context`, which cannot run during this call. + let borrowed = unsafe { as ForeignOwnable>::borrow(pointer) }; + let context: Arc = borrowed.into(); + let result = (|| { + let _call = Call::enter(&context.sep)?; + let user = UserPtr::from_addr(arg as usize); + if cmd == QUERY { + let (phase, cleanup, reason) = context.query(); + return UserSlice::new(user, 16).writer().write(&Query { + phase, + cleanup, + reason, + activity_flags: context.sep.context_sensor_waiting(&context) as u32, + }); + } + if cmd == REVOKE { + let request: Revoke = UserSlice::new(user, 8).reader().read()?; + if request.reserved != 0 { + return Err(EINVAL); + } + let until = deadline(request.timeout_ms)?; + context.revoke(EKEYREVOKED); + return context.wait_revoke(until); + } + if cmd == IOC_CANCEL { + let request: Cancel = UserSlice::new(user, 8).reader().read()?; + return context.cancel(request.request_id); + } + if cmd != AUTH { + return Err(ENOTTY); + } + context.check_domain()?; + if context.sep.key_context_failed.load(Relaxed) { + return Err(EIO); + } + let mut out: Authenticate = UserSlice::new(user, 24).reader().read()?; + if out.id == 0 || out.reserved != 0 || out.outcome != 0 { + return Err(EINVAL); + } + if out.method != METHOD_TOUCH { + return Err(EOPNOTSUPP); + } + let request = Request::new( + out.id, + crate::key_request::KeyRequest { + blob: KVec::new(), + input: crate::key_request::Input::Authenticate(context.clone()), + deadline_ns: deadline(out.timeout_ms)?, + }, + )?; + SepData::queue_key(context.sep.clone(), request.clone())?; + let result = request + .completion + .wait_ready() + .and_then(|()| context.claim(&request.completion)); + context.forget(&request.completion); + let status = match result { + Ok(crate::key_request::Output::Authenticated) => Ok(()), + Ok(_) => Err(EIO), + Err(error) => Err(error), + }; + if let Err(error) = status { + context.revoke(error); + } + out.outcome = match status { + Ok(()) => OUTCOME_OK, + Err(error) if error == EACCES => OUTCOME_NOT_MATCHED, + _ => OUTCOME_FAILED, + }; + if let Err(error) = UserSlice::new(user, 24).writer().write(&out) { + context.revoke(error); + return Err(error); + } + status + })(); + match result { + Ok(()) => 0, + Err(error) => error.to_errno() as c_long, + } +} + +/// A context file's release. +/// +/// # Safety +/// C passes the file's foreign `Context` Arc once, after every file call has +/// returned. +unsafe extern "C" fn release_context(pointer: *mut c_void) { + // SAFETY: the foreign Arc the file held, given back exactly once. + let context = unsafe { as ForeignOwnable>::from_foreign(pointer) }; + context.close(); +} diff --git a/drivers/soc/apple/key_context_shim.c b/drivers/soc/apple/key_context_shim.c new file mode 100644 index 0000000000000..8212654e072ac --- /dev/null +++ b/drivers/soc/apple/key_context_shim.c @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* Anonymous authorization files. Protocol and lifetime policy remain in Rust. */ +#include +#include +#include +#include + +#include "shim.h" + +struct sep_context_file { + void *ctx; + void (*release)(void *ctx); + long (*ioctl)(void *ctx, unsigned int cmd, unsigned long arg); +}; + +static int sep_context_release(struct inode *inode, struct file *file) +{ + struct sep_context_file *context = file->private_data; + + context->release(context->ctx); + kfree(context); + return 0; +} + +static long sep_context_ioctl(struct file *file, unsigned int cmd, + unsigned long arg) +{ + struct sep_context_file *context = file->private_data; + + return context->ioctl(context->ctx, cmd, arg); +} + +static const struct file_operations sep_context_fops = { + .owner = THIS_MODULE, + .release = sep_context_release, + .unlocked_ioctl = sep_context_ioctl, + .compat_ioctl = compat_ptr_ioctl, +}; + +int sep_context_file_new(void *ctx, void (*release)(void *), + long (*ioctl)(void *, unsigned int, unsigned long), + void **out, int *out_fd) +{ + struct sep_context_file *context; + struct file *file; + int fd; + + fd = get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) + return fd; + context = kzalloc(sizeof(*context), GFP_KERNEL_ACCOUNT); + if (!context) { + put_unused_fd(fd); + return -ENOMEM; + } + context->ctx = ctx; + context->release = release; + context->ioctl = ioctl; + file = anon_inode_getfile("apple-sep-context", &sep_context_fops, + context, O_RDWR | O_CLOEXEC); + if (IS_ERR(file)) { + kfree(context); + put_unused_fd(fd); + return PTR_ERR(file); + } + *out = file; + *out_fd = fd; + return 0; +} + +void sep_context_file_finish(void *ptr, int fd, bool install) +{ + struct file *file = ptr; + + if (install) { + fd_install(fd, file); + } else { + put_unused_fd(fd); + fput(file); + } +} + +void *sep_context_file_get(int fd, void **ctx) +{ + struct file *file = fget(fd); + struct sep_context_file *context; + + if (!file) + return NULL; + if (file->f_op != &sep_context_fops) { + fput(file); + return NULL; + } + context = file->private_data; + *ctx = context->ctx; + return file; +} + +void sep_context_file_put(void *file) +{ + fput(file); +} diff --git a/drivers/soc/apple/key_context_state.rs b/drivers/soc/apple/key_context_state.rs new file mode 100644 index 0000000000000..c29f2d4d30694 --- /dev/null +++ b/drivers/soc/apple/key_context_state.rs @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! One authorization epoch. State locks never cover firmware or user copies. + +use kernel::prelude::*; + +#[derive(Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub(crate) enum Phase { + New, + Authenticating, + Authorized, + Revoking, + Dead, +} + +#[derive(Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub(crate) enum Cleanup { + None, + Pending, + Running, + Confirmed, + Failed, +} + +pub(crate) struct State { + pub(crate) phase: Phase, + pub(crate) cleanup: Cleanup, + pub(crate) reason: Option, + users: u32, +} + +impl State { + pub(crate) fn new() -> Self { + Self { + phase: Phase::New, + cleanup: Cleanup::None, + reason: None, + users: 0, + } + } + + pub(crate) fn authenticate(&mut self) -> Result { + if self.phase != Phase::New { + return Err(EKEYREVOKED); + } + self.phase = Phase::Authenticating; + self.users = 1; + Ok(()) + } + + pub(crate) fn authenticated(&mut self, result: Result) -> bool { + self.users -= 1; + if self.phase == Phase::Authenticating && result.is_ok() { + self.phase = Phase::Authorized; + return true; + } + self.revoke(result.err().unwrap_or(EKEYREVOKED)); + false + } + + pub(crate) fn acquire(&mut self) -> Result { + if self.phase != Phase::Authorized { + return Err(EKEYREVOKED); + } + self.users = self.users.checked_add(1).ok_or(EOVERFLOW)?; + Ok(()) + } + + pub(crate) fn release(&mut self) { + self.users -= 1; + } + + pub(crate) fn revoke(&mut self, reason: Error) { + if matches!(self.phase, Phase::Revoking | Phase::Dead) { + return; + } + self.phase = Phase::Revoking; + self.cleanup = Cleanup::Pending; + self.reason = Some(reason); + } + + pub(crate) fn claim_cleanup(&mut self) -> bool { + if self.phase != Phase::Revoking || self.users != 0 || self.cleanup != Cleanup::Pending { + return false; + } + self.cleanup = Cleanup::Running; + true + } + + pub(crate) fn disposed(&mut self, result: Result) { + self.cleanup = if result.is_ok() { + Cleanup::Confirmed + } else { + Cleanup::Failed + }; + self.phase = Phase::Dead; + } + + pub(crate) fn revocation_result(&self) -> Option { + match self.cleanup { + Cleanup::Confirmed => Some(Ok(())), + Cleanup::Failed => Some(Err(EIO)), + _ => None, + } + } + + pub(crate) fn claim_allowed(&self) -> bool { + self.phase == Phase::Authorized + } +} + +#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(apple_sep_key_context_state)] +mod tests { + use super::*; + + #[test] + fn one_authentication_then_operations_while_authorized() { + let mut state = State::new(); + assert_eq!(state.acquire(), Err(EKEYREVOKED)); + assert!(!state.claim_allowed()); + assert_eq!(state.authenticate(), Ok(())); + assert_eq!(state.authenticate(), Err(EKEYREVOKED)); + assert!(state.authenticated(Ok(()))); + assert!(state.phase == Phase::Authorized && state.claim_allowed()); + assert_eq!(state.acquire(), Ok(())); + assert_eq!(state.acquire(), Ok(())); + state.release(); + state.release(); + // A context authenticates once. + assert_eq!(state.authenticate(), Err(EKEYREVOKED)); + } + + #[test] + fn a_failed_authentication_revokes_with_its_reason() { + let mut state = State::new(); + assert_eq!(state.authenticate(), Ok(())); + assert!(!state.authenticated(Err(ECANCELED))); + assert!(state.phase == Phase::Revoking && state.reason == Some(ECANCELED)); + assert_eq!(state.acquire(), Err(EKEYREVOKED)); + } + + #[test] + fn cleanup_waits_for_users_and_runs_once() { + let mut state = State::new(); + assert_eq!(state.authenticate(), Ok(())); + assert!(state.authenticated(Ok(()))); + assert_eq!(state.acquire(), Ok(())); + state.revoke(EKEYEXPIRED); + // A later reason does not replace the first. + state.revoke(EIO); + assert_eq!(state.reason, Some(EKEYEXPIRED)); + assert!(!state.claim_allowed()); + assert!(!state.claim_cleanup()); + assert!(state.revocation_result().is_none()); + state.release(); + assert!(state.claim_cleanup()); + assert!(!state.claim_cleanup()); + state.disposed(Ok(())); + assert!(state.phase == Phase::Dead); + assert_eq!(state.revocation_result(), Some(Ok(()))); + // Dead stays dead. + state.revoke(EIO); + assert!(state.phase == Phase::Dead); + } + + #[test] + fn a_failed_disposal_is_reported() { + let mut state = State::new(); + state.revoke(EKEYREVOKED); + assert!(state.claim_cleanup()); + state.disposed(Err(EIO)); + assert_eq!(state.revocation_result(), Some(Err(EIO))); + } +} + diff --git a/drivers/soc/apple/key_device.rs b/drivers/soc/apple/key_device.rs index 7c9381370a54d..59c1d87ce03c8 100644 --- a/drivers/soc/apple/key_device.rs +++ b/drivers/soc/apple/key_device.rs @@ -2,19 +2,117 @@ //! `/dev/sep-key` as a per-open device: each file owns its state and a //! reference to the driver; the key commands keep their existing ABI. -use crate::{key as keys, SepData}; +use crate::{key as keys, key_request::{Request, RequestIds}, SepData}; use kernel::prelude::*; use kernel::sync::atomic::{Acquire, Full, Relaxed, Release}; -use kernel::sync::{Arc, ArcBorrow}; +use kernel::sync::{Arc, ArcBorrow, Mutex}; use kernel::types::ForeignOwnable; +use kernel::uaccess::{UserPtr, UserSlice}; +#[path = "key_context_device.rs"] +mod contexts; + +#[repr(C)] +struct Cancel { + request_id: u64, +} +// SAFETY: one initialized fixed-width integer, all bit patterns valid. +unsafe impl kernel::transmute::FromBytes for Cancel {} +/// `SEP_KEY_CANCEL_ID`: ends this file's request `request_id`. +const IOC_CANCEL: u32 = kernel::ioctl::_IOW::(crate::key::MAGIC, 0x48); + +/// Whether the authorization-context requests are offered: opted in by +/// `key_context_api`, on the 13.5 key store with per-user bags. +pub(crate) fn contexts_supported(sep: &SepData) -> bool { + crate::module_parameters::key_context_api.value() != 0 + && sep.profile.per_user_keybags + && matches!( + sep.profile.key_store, + crate::profile::KeyStore::Sepos13 { .. } + ) +} + +enum Operation { + Idle, + Private(Arc), +} +struct State { + ids: RequestIds, + operation: Operation, +} + +#[pin_data] struct File { sep: Arc, + context_generation: u64, + #[pin] + state: Mutex, +} + +struct Active<'a>(&'a File); +impl Drop for Active<'_> { + fn drop(&mut self) { + self.0.state.lock().operation = Operation::Idle; + } } impl File { + fn begin(&self, request: Arc) -> Result> { + let mut state = self.state.lock(); + if !matches!(state.operation, Operation::Idle) { + return Err(EBUSY); + } + state.ids.admit(request.id)?; + state.operation = Operation::Private(request); + Ok(Active(self)) + } + + fn cancel(&self, id: u64) -> Result { + if id == 0 { + return Err(EINVAL); + } + let request = { + let mut state = self.state.lock(); + match &state.operation { + Operation::Private(request) if request.id == id => request.clone(), + _ => { + return if state.ids.cancel_ahead(id)? { + Ok(()) + } else { + Err(ENOENT) + } + } + } + }; + request.cancel(ECANCELED) + } + + fn check_api(&self) -> Result { + if self.sep.shutting_down.load(Relaxed) { + return Err(ENODEV); + } + if !contexts_supported(&self.sep) { + return Err(EOPNOTSUPP); + } + if self.sep.key_context_failed.load(Relaxed) { + return Err(EIO); + } + if !self.sep.xart_writable() { + return Err(EROFS); + } + Ok(()) + } + fn ioctl(&self, cmd: u32, arg: usize) -> Result { let _call = Call::enter(&self.sep)?; + let user = UserPtr::from_addr(arg); + if cmd == IOC_CANCEL { + let request: Cancel = UserSlice::new(user, 8).reader().read()?; + return self.cancel(request.request_id); + } + if contexts::handles(cmd) { + return self.context_ioctl(cmd, user); + } self.sep.key_ioctl(cmd, arg) } } @@ -111,8 +209,15 @@ unsafe extern "C" fn open(ctx: *mut c_void, out: *mut *mut c_void) -> c_int { if sep.shutting_down.load(Relaxed) { return ENODEV.to_errno(); } - let file = Arc::new( - File { sep: sep.into() }, + let file = Arc::pin_init( + try_pin_init!(File { + context_generation: sep.key_context_generation.load(Relaxed), + sep: sep.into(), + state <- kernel::new_mutex!(State { + ids: RequestIds::new(), + operation: Operation::Idle, + }), + }), kernel::alloc::flags::GFP_KERNEL_ACCOUNT, ); match file { @@ -121,7 +226,7 @@ unsafe extern "C" fn open(ctx: *mut c_void, out: *mut *mut c_void) -> c_int { unsafe { out.write(file.into_foreign()) }; 0 } - Err(_) => ENOMEM.to_errno(), + Err(error) => error.to_errno(), } } diff --git a/drivers/soc/apple/key_request.rs b/drivers/soc/apple/key_request.rs new file mode 100644 index 0000000000000..be72ff76115ab --- /dev/null +++ b/drivers/soc/apple/key_request.rs @@ -0,0 +1,243 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! One owned request; contexts retain delivery records, never request inputs. + +use crate::{key_result::State, key_wait::Event}; +use kernel::alloc::flags::GFP_KERNEL_ACCOUNT; +use kernel::prelude::*; +use kernel::sync::{ + atomic::{Acquire, Atomic, Release}, + Arc, Mutex, +}; + +pub(crate) enum Input { + Authenticate(Arc), + ContextSign( + Arc, + [u8; crate::key::DIGEST_LEN], + ), + ContextDecrypt(Arc, KVec), +} + +pub(crate) enum Output { + Authenticated, + Signature(crate::Secret), + Plaintext(crate::Secret), +} + +pub(crate) struct KeyRequest { + pub(crate) blob: KVec, + pub(crate) input: Input, + pub(crate) deadline_ns: u64, +} + +#[pin_data] +pub(crate) struct Completion { + #[pin] + phase: Mutex, + changed: Event, + deadline_ns: u64, + retired: Atomic, +} + +impl Completion { + fn new(deadline_ns: u64) -> Result> { + Arc::pin_init( + try_pin_init!(Self { + phase <- kernel::new_mutex!(State::Pending), + changed: Event::new()?, + deadline_ns, + retired: Atomic::new(false), + }), + GFP_KERNEL_ACCOUNT, + ) + } + + pub(crate) fn is_live(&self) -> bool { + self.phase.lock().pending() && crate::shim::monotonic_ns() < self.deadline_ns + } + + pub(crate) fn complete(&self, result: Result) -> bool { + let mut phase = self.phase.lock(); + let expired = crate::shim::monotonic_ns() >= self.deadline_ns; + let accepted = phase.complete(result, expired); + drop(phase); + self.changed.wake(); + accepted && !expired + } + + /// Success is deliverable only after the worker releases capture ownership. + pub(crate) fn retire(&self) { + self.retired.store(true, Release); + self.changed.wake(); + } + + pub(crate) fn cancel(&self, reason: Error) -> Result { + let result = self.phase.lock().cancel(reason); + self.changed.wake(); + result + } + + /// Caller may hold the context lock, never a userspace mapping. + pub(crate) fn claim(&self) -> Result { + let mut phase = self.phase.lock(); + if crate::shim::monotonic_ns() >= self.deadline_ns { + phase.expire(); + } + if matches!(*phase, State::Complete(Ok(_))) && !self.retired.load(Acquire) { + return Err(EAGAIN); + } + phase.claim() + } + + pub(crate) fn wait_ready(&self) -> Result { + loop { + let sequence = self.changed.sequence(); + { + let mut phase = self.phase.lock(); + if crate::shim::monotonic_ns() >= self.deadline_ns { + phase.expire(); + } + if phase.ready() + && (!matches!(*phase, State::Complete(Ok(_))) || self.retired.load(Acquire)) + { + return Ok(()); + } + } + if let Err(error) = self.changed.wait(sequence, self.deadline_ns) { + let _ = self.cancel(error); + return Ok(()); + } + } + } +} + +/// The ids of one file's or one context's requests. Each new request takes +/// a larger id than the last; a cancel may name an id not used yet, and that +/// request is then refused with ECANCELED when it comes. +pub(crate) struct RequestIds { + last: u64, + cancelled_through: u64, +} + +impl RequestIds { + pub(crate) const fn new() -> Self { + Self { + last: 0, + cancelled_through: 0, + } + } + + /// Whether `id` may start a new request. + pub(crate) fn check(&self, id: u64) -> Result { + if id == 0 { + return Err(EINVAL); + } + if id <= self.cancelled_through { + return Err(ECANCELED); + } + if id <= self.last { + return Err(EINVAL); + } + Ok(()) + } + + /// Checks `id` and records it as the latest request. + pub(crate) fn admit(&mut self, id: u64) -> Result { + self.check(id)?; + self.last = id; + Ok(()) + } + + /// A cancel of `id`: true when it names an id not used yet, which is + /// then recorded; false for one already used, which the caller ends. + pub(crate) fn cancel_ahead(&mut self, id: u64) -> Result { + if id == 0 { + return Err(EINVAL); + } + if id > self.last { + self.cancelled_through = self.cancelled_through.max(id); + return Ok(true); + } + Ok(false) + } + + pub(crate) fn last(&self) -> u64 { + self.last + } +} + +pub(crate) struct Request { + pub(crate) id: u64, + pub(crate) data: KeyRequest, + pub(crate) completion: Arc, +} + +impl Request { + pub(crate) fn new(id: u64, data: KeyRequest) -> Result> { + let completion = Completion::new(data.deadline_ns)?; + Ok(Arc::new( + Self { + id, + data, + completion, + }, + GFP_KERNEL_ACCOUNT, + )?) + } + + pub(crate) fn is_live(&self) -> bool { + self.completion.is_live() + } + pub(crate) fn check_live(&self) -> Result { + if self.is_live() { + return Ok(()); + } + Err(if crate::shim::monotonic_ns() >= self.data.deadline_ns { + ETIMEDOUT + } else { + ECANCELED + }) + } + pub(crate) fn complete(&self, result: Result) { + self.completion.complete(result); + } + pub(crate) fn cancel(&self, reason: Error) -> Result { + self.completion.cancel(reason) + } + pub(crate) fn retire(&self) { + self.completion.retire(); + } +} + +#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(apple_sep_key_request_ids)] +mod request_ids_tests { + use super::*; + + #[test] + fn ids_must_rise_and_zero_is_never_one() { + let mut ids = RequestIds::new(); + assert_eq!(ids.admit(0), Err(EINVAL)); + assert_eq!(ids.admit(5), Ok(())); + assert_eq!(ids.admit(5), Err(EINVAL)); + assert_eq!(ids.admit(3), Err(EINVAL)); + assert_eq!(ids.admit(6), Ok(())); + assert_eq!(ids.last(), 6); + } + + #[test] + fn a_cancel_ahead_refuses_that_request_when_it_comes() { + let mut ids = RequestIds::new(); + assert_eq!(ids.admit(1), Ok(())); + assert_eq!(ids.cancel_ahead(0), Err(EINVAL)); + assert_eq!(ids.cancel_ahead(1), Ok(false)); + assert_eq!(ids.cancel_ahead(4), Ok(true)); + assert_eq!(ids.check(2), Err(ECANCELED)); + assert_eq!(ids.admit(4), Err(ECANCELED)); + assert_eq!(ids.last(), 1); + assert_eq!(ids.admit(5), Ok(())); + // An earlier, smaller cancel does not lower the mark. + assert_eq!(ids.cancel_ahead(3), Ok(false)); + assert_eq!(ids.admit(6), Ok(())); + } +} diff --git a/drivers/soc/apple/key_result.rs b/drivers/soc/apple/key_result.rs new file mode 100644 index 0000000000000..fd2f97d7ceb8e --- /dev/null +++ b/drivers/soc/apple/key_result.rs @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Single-owner delivery transitions, independent of user copies and firmware. + +use crate::key_request::Output; +use kernel::prelude::*; + +pub(crate) enum State { + Pending, + Complete(Result), + Abandoned(Error), + Claimed, +} + +impl State { + pub(crate) fn pending(&self) -> bool { + matches!(self, Self::Pending) + } + + pub(crate) fn complete(&mut self, result: Result, expired: bool) -> bool { + if !self.pending() { + return false; + } + let result = result.map_err(|error| match error { + ERESTARTSYS | ERESTARTNOINTR | ERESTARTNOHAND | ERESTART_RESTARTBLOCK => EINTR, + EBUSY => EIO, + _ => error, + }); + // A late failure retains its diagnosis; only work without a terminal + // failure, or successful undelivered output, can expire. + *self = Self::Complete(match result { + Err(error) => Err(error), + Ok(_) if expired => Err(ETIMEDOUT), + result => result, + }); + true + } + + pub(crate) fn cancel(&mut self, reason: Error) -> Result { + match self { + Self::Claimed => return Err(EALREADY), + Self::Complete(Err(_)) | Self::Abandoned(_) => return Err(ENOENT), + _ => *self = Self::Abandoned(reason), + } + Ok(()) + } + + pub(crate) fn expire(&mut self) { + if matches!(self, Self::Pending | Self::Complete(Ok(_))) { + *self = Self::Abandoned(ETIMEDOUT); + } + } + + pub(crate) fn ready(&self) -> bool { + !self.pending() + } + + pub(crate) fn claim(&mut self) -> Result { + match self { + Self::Pending => Err(EAGAIN), + Self::Abandoned(error) | Self::Complete(Err(error)) => Err(*error), + Self::Claimed => Err(ENODATA), + Self::Complete(_) => { + let Self::Complete(result) = core::mem::replace(self, Self::Claimed) else { + return Err(EIO); + }; + result + } + } + } +} + +#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(apple_sep_key_result)] +mod tests { + use super::*; + + fn done() -> Result { + Ok(Output::Authenticated) + } + + #[test] + fn a_result_is_claimed_once() { + let mut state = State::Pending; + assert_eq!(state.claim().err(), Some(EAGAIN)); + assert!(state.complete(done(), false)); + assert!(!state.complete(Err(EIO), false)); + assert!(matches!(state.claim(), Ok(Output::Authenticated))); + assert_eq!(state.claim().err(), Some(ENODATA)); + assert_eq!(state.cancel(ECANCELED), Err(EALREADY)); + } + + #[test] + fn a_cancel_before_the_claim_wins() { + let mut state = State::Pending; + assert!(state.complete(done(), false)); + assert_eq!(state.cancel(ECANCELED), Ok(())); + assert_eq!(state.claim().err(), Some(ECANCELED)); + assert_eq!(state.cancel(ECANCELED), Err(ENOENT)); + } + + #[test] + fn expiry_ends_success_but_keeps_a_failure() { + let mut state = State::Pending; + assert!(state.complete(done(), true)); + assert_eq!(state.claim().err(), Some(ETIMEDOUT)); + let mut state = State::Pending; + assert!(state.complete(done(), false)); + state.expire(); + assert_eq!(state.claim().err(), Some(ETIMEDOUT)); + let mut state = State::Pending; + assert!(state.complete(Err(EACCES), true)); + state.expire(); + assert_eq!(state.claim().err(), Some(EACCES)); + } + + #[test] + fn restarts_and_busy_are_not_reported_as_such() { + let mut state = State::Pending; + assert!(state.complete(Err(ERESTARTSYS), false)); + assert_eq!(state.claim().err(), Some(EINTR)); + let mut state = State::Pending; + assert!(state.complete(Err(EBUSY), false)); + assert_eq!(state.claim().err(), Some(EIO)); + } +} + diff --git a/drivers/soc/apple/key_wait.rs b/drivers/soc/apple/key_wait.rs new file mode 100644 index 0000000000000..060c14dc0488b --- /dev/null +++ b/drivers/soc/apple/key_wait.rs @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Killable/freezable wait bridge. No Rust state lock crosses a sleep. + +use kernel::prelude::*; + +extern "C" { + fn sep_key_event_new() -> *mut c_void; + fn sep_key_event_free(event: *mut c_void); + fn sep_key_event_sequence(event: *mut c_void) -> u64; + fn sep_key_event_wake(event: *mut c_void); + fn sep_key_event_wait(event: *mut c_void, sequence: u64, timeout_ms: u32) -> c_int; +} + +pub(crate) struct Event(*mut c_void); +// SAFETY: the C object contains only a waitqueue and an atomic sequence. +unsafe impl Send for Event {} +// SAFETY: every operation is synchronized by that queue/atomic. +unsafe impl Sync for Event {} + +impl Event { + pub(crate) fn new() -> Result { + // SAFETY: allocation has no preconditions. + let event = unsafe { sep_key_event_new() }; + if event.is_null() { + Err(ENOMEM) + } else { + Ok(Self(event)) + } + } + + pub(crate) fn sequence(&self) -> u64 { + // SAFETY: self owns a live event. + unsafe { sep_key_event_sequence(self.0) } + } + + pub(crate) fn wake(&self) { + // SAFETY: self owns a live event. + unsafe { sep_key_event_wake(self.0) }; + } + + pub(crate) fn wait(&self, sequence: u64, deadline_ns: u64) -> Result { + let ms = deadline_ns + .saturating_sub(crate::shim::monotonic_ns()) + .div_ceil(1_000_000); + if ms == 0 { + return Ok(()); + } + // SAFETY: the event lives throughout the call. Callers hold no locks. + let status = unsafe { sep_key_event_wait(self.0, sequence, ms.min(120_000) as u32) }; + if status == 0 { + Ok(()) + } else { + Err(Error::from_errno(status)) + } + } +} + +impl Drop for Event { + fn drop(&mut self) { + // SAFETY: all waiters own a reference to the containing object. + unsafe { sep_key_event_free(self.0) }; + } +} diff --git a/drivers/soc/apple/key_wait_shim.c b/drivers/soc/apple/key_wait_shim.c new file mode 100644 index 0000000000000..5c55f96bdd22a --- /dev/null +++ b/drivers/soc/apple/key_wait_shim.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +#include +#include +#include +#include + +#include "shim.h" + +/* Sequence sampling before the Rust state check closes the wake-before-wait race. */ +struct sep_key_event { + wait_queue_head_t wait; + atomic64_t sequence; +}; + +void *sep_key_event_new(void) +{ + struct sep_key_event *event = kzalloc(sizeof(*event), GFP_KERNEL_ACCOUNT); + + if (event) { + init_waitqueue_head(&event->wait); + atomic64_set(&event->sequence, 0); + } + return event; +} + +void sep_key_event_free(void *ptr) +{ + kfree(ptr); +} + +u64 sep_key_event_sequence(void *ptr) +{ + struct sep_key_event *event = ptr; + + return atomic64_read(&event->sequence); +} + +void sep_key_event_wake(void *ptr) +{ + struct sep_key_event *event = ptr; + + atomic64_inc(&event->sequence); + wake_up_all(&event->wait); +} + +int sep_key_event_wait(void *ptr, u64 sequence, u32 timeout_ms) +{ + struct sep_key_event *event = ptr; + long left = msecs_to_jiffies(timeout_ms); + DEFINE_WAIT(wait); + int ret = 0; + + for (;;) { + ret = prepare_to_wait_event(&event->wait, &wait, + TASK_KILLABLE | TASK_FREEZABLE); + if (atomic64_read(&event->sequence) != sequence || !left) + break; + if (ret) + break; + left = schedule_timeout(left); + } + finish_wait(&event->wait, &wait); + return ret ? -EINTR : 0; +} diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 3af73fe79c4dc..6f59cebbde419 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -2921,6 +2921,7 @@ impl SepData { // Touch ID device: fprintd claims it for one PAM authentication. let open_gen = self.bio_open_gen.load(Relaxed); let key_mode = self.key_touch_active.load(Relaxed); + let retaining = self.key_authorization_request().is_some(); // A /dev/sep-key signature is its own prompt: a fresh evaluation that // never outlives this run, whatever prompt /dev/sep-bio has open. let current = if !key_mode && self.match_scope_gen.load(Relaxed) == open_gen { @@ -2976,7 +2977,7 @@ impl SepData { // A /dev/sep-key call owns its evaluation outright: it is deleted // when this goes, on every path out of this run, never kept open // for a later touch, and never shared with /dev/sep-bio. - let key_context = key_mode.then(|| crate::key::OwnedContext::new(self, evaluation)); + let mut key_context = key_mode.then(|| crate::key::OwnedContext::new(self, evaluation)); let mut outcome = self.verify_one_image(); // A retry straight from the capture (no finger in time, partial @@ -3067,11 +3068,13 @@ impl SepData { } *self.match_context.lock() = None; self.match_scope_gen.store(0, Relaxed); - match key_context { - Some(context) => drop(context), - None => self.close_unlock_evaluation(&evaluation), + if !retaining { + match key_context.take() { + Some(context) => drop(context), + None => self.close_unlock_evaluation(&evaluation), + } } - if key_mode && matched && !self.key_context_failed.load(Relaxed) { + if key_mode && !retaining && matched && !self.key_context_failed.load(Relaxed) { self.key_touch_after_close(&evaluation); } } @@ -3191,6 +3194,11 @@ impl SepData { "verify: post-result steps done {} ms after the touch was finished\n", shim::boottime_ns().saturating_sub(touched_at) / 1_000_000 ); + if retaining && matches!(outcome, bio::VerifyOutcome::Matched(_)) { + if key_context.take().ok_or(EIO).and_then(|credential| self.retain_key_authorization(credential)).is_err() { + outcome = bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE); + } + } if !reported_early { self.finish_verify(outcome, token_bytes); } @@ -3253,8 +3261,9 @@ impl SepData { return bio::VerifyOutcome::Failed(ENROL_STATUS_SENSOR); } CaptureWait::Abandon => { - // A cancelled verify also abandons; only a live one is a fault. - if self.key_touch_active.load(Relaxed) + // A cancelled verify also abandons, as does a context's touch + // whose request ended; only a live one is a fault. + if (self.key_touch_active.load(Relaxed) && self.key_capture_wanted()) || bio::verify_is_live(&self.bio_session.lock(), self.capture_epoch.load(Relaxed)) { dev_err_ratelimited!( @@ -4528,6 +4537,7 @@ impl SepData { // to the same duration as the poll path. let deadline_ns = crate::shim::monotonic_ns().saturating_add(wait_ms.saturating_mul(1_000_000)); let mut attempt: u32 = 0; + let _waiting = crate::key_command::SensorWait::new(self); loop { if !self.capture_live() { return CaptureWait::Abandon; @@ -5262,6 +5272,9 @@ impl SepData { self.suspending.store(true, Relaxed); bio::abort_capture(&mut session) }; + self.cancel_key(ENODEV); + self.cancel_context_commands(ENODEV); + self.revoke_key_contexts(ENODEV); if woke { dev_info_ratelimited!( self.dev, @@ -5282,11 +5295,16 @@ impl SepData { "Touch ID: a capture was still running {} ms after the system started to sleep; continuing\n", SLEEP_DRAIN_MS ); + self.key_context_failed.store(true, Relaxed); return; } kernel::time::delay::fsleep(kernel::time::Delta::from_millis(10)); waited += 10; } + let deadline = shim::monotonic_ns().saturating_add(u64::from(SLEEP_DRAIN_MS) * 1_000_000); + if self.drain_key_contexts(deadline).is_err() { + self.key_context_failed.store(true, Relaxed); + } } /// The system has resumed, or the suspend was aborted. diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index e1e2247119a7c..89ad170782e92 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -24,7 +24,13 @@ mod hibe; mod hwrng; mod image; mod key; +mod key_command; +mod key_context; +mod key_context_state; mod key_device; +mod key_request; +mod key_result; +mod key_wait; mod keybag; mod keybag_identity; mod os_identity; @@ -909,10 +915,17 @@ struct SepData { // /dev/sep-key: P-256 keys created inside the SEP (see `key`). #[pin] key_dev: Mutex>, - /// `/dev/sep-key` calls in progress (`key_device::Call`). key_calls: Atomic, + key_context_generation: Atomic, + #[pin] + key_contexts: Mutex>>, + key_context_queue: workqueue::OwnedQueue, + key_auth_queue: workqueue::OwnedQueue, + #[pin] + key_commands: Mutex, #[pin] capture: Mutex, + key_sensor_waiting: Atomic, // /dev/sep-key: one identity-bag load, operation and unload at a time. // The device's ioctls run concurrently (a touch wait must not block @@ -1319,7 +1332,13 @@ impl SepData { rearm_dev <- new_mutex!(None), key_dev <- new_mutex!(None), key_calls: Atomic::new(0), + key_context_generation: Atomic::new(0), + key_contexts <- new_mutex!(KVec::new()), + key_context_queue: workqueue::OwnedQueue::new_ordered(c"sep_context")?, + key_auth_queue: workqueue::OwnedQueue::new_ordered(c"sep_auth")?, + key_commands <- new_mutex!(key_command::State::new()), capture <- new_mutex!(capture::State::new()), + key_sensor_waiting: Atomic::new(false), key_bag_lock <- new_mutex!(()), key_touch <- new_mutex!(None), key_touch_active: Atomic::new(false), @@ -3291,6 +3310,11 @@ impl SepData { self.sks_wq.notify_all(); self.scrd_wq.notify_all(); pm::unregister(); + self.cancel_key(ENODEV); + self.cancel_context_commands(ENODEV); + self.revoke_key_contexts(ENODEV); + self.key_auth_queue.drain(); + self.remove_key_contexts(); // Release any capture start still held for a resume that will not be // reported now. self.sleep_finished(); @@ -3801,6 +3825,10 @@ module! { default: 0, description: "Opt-in J414s diagnostic: with zero live identities, select the system context and try saving the missing owner Catacomb", }, + key_context_api: u8 { + default: 0, + description: "Enable the authorization-context requests on /dev/sep-key (SEP_KEY_CTX_*, a design draft; 13.5 key store with per-user bags)", + }, key_selftest: u8 { default: 0, description: "Test only: let root run SEP_KEY_SELFTEST, which signs with an access-controlled /dev/sep-key key under deliberately unsuitable credential contexts to show the enclave refuses", diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index bb00077532239..dbb099b64edc5 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -18,13 +18,24 @@ #include -/* -- application-key files ---------------------------------------------- */ +/* -- application-key files and waits ------------------------------------ */ void *sep_key_register(const char *name, u16 mode, void *ctx, int (*open)(void *, void **), void (*release)(void *), long (*ioctl)(void *, unsigned int, unsigned long)); void sep_key_unregister(void *device); +void *sep_key_event_new(void); +void sep_key_event_free(void *event); +u64 sep_key_event_sequence(void *event); +void sep_key_event_wake(void *event); +int sep_key_event_wait(void *event, u64 sequence, u32 timeout_ms); +int sep_context_file_new(void *ctx, void (*release)(void *), + long (*ioctl)(void *, unsigned int, unsigned long), + void **file, int *fd); +void sep_context_file_finish(void *file, int fd, bool install); +void *sep_context_file_get(int fd, void **ctx); +void sep_context_file_put(void *file); struct apple_sep_fv_new_file_key; struct dev_pm_ops; diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index fd4d3c84803c1..87467fbbd0f93 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -1575,6 +1575,10 @@ impl crate::SepData { /// even for an immediate lock on M1. fn userbag_lock_13(&self, user: crate::sks::DesignateUser, flags: u64) -> Result<()> { const FIRES: u32 = 15; + // Retained matches for this bag's keys end with the lock. + if let Some(uid) = u32::try_from(-user.special_handle().value()).ok().and_then(|id| id.checked_sub(USER_BAG_BASE)) { + self.revoke_bag_contexts(uid); + } // Not held across the requests and the waits between fires: every // other bag and Touch ID path takes this lock. let (bag, touch) = { diff --git a/include/uapi/linux/apple-sep.h b/include/uapi/linux/apple-sep.h index 582662e24b059..f92148dbf9d4f 100644 --- a/include/uapi/linux/apple-sep.h +++ b/include/uapi/linux/apple-sep.h @@ -321,6 +321,7 @@ struct sep_bio_rearm { #define SEP_KEY_FEATURE_SEAL 0x2 #define SEP_KEY_FEATURE_PASSWORD 0x4 #define SEP_KEY_FEATURE_PERSISTENT_ATTEMPTS 0x8 +#define SEP_KEY_FEATURE_CONTEXTS 0x20 /* authorization contexts, below */ #define SEP_KEY_PASSWORD_MAX 256 #define SEP_KEY_SECRET_MAX 64 #define SEP_KEY_SEALED_MAX 256 @@ -415,6 +416,105 @@ struct sep_key_unseal_password { #define SEP_KEY_SIGN_PASSWORD _IOWR(SEP_BIO_IOC_MAGIC, 0x29, struct sep_key_sign_password) #define SEP_KEY_UNSEAL_PASSWORD _IOWR(SEP_BIO_IOC_MAGIC, 0x2a, struct sep_key_unseal_password) +/* + * Authorization contexts (SEP_KEY_FEATURE_CONTEXTS; module parameter + * key_context_api=1, 13.5 key store). DESIGN DRAFT, not a stable ABI. + * + * One authentication (a Touch ID match in the bag user's own domain) + * authorizes a bounded run of operations on that bag's keys, as macOS lets + * an authentication be reused for a short time: a password manager reveals + * many items after one unlock. The bounds: + * + * - a context belongs to one Unix account's key bag (NEW: root or that + * user) and is a file descriptor: closing it ends it; + * - it lasts lifetime_s from the authentication (at most 12 hours, 0 = + * until revoked), and ends at once when the bag locks, on suspend, on + * REVOKE and when the driver stops; + * - each SIGN or DECRYPT still runs one enclave operation under the + * enclave's own access-control check of that key. + * + * Every request carries a caller-chosen nonzero id; SEP_KEY_CANCEL_ID on + * the file it was made on (the /dev/sep-key file, or the context for its + * AUTH) ends that request, such as an AUTH waiting for a finger. + * Outcome: 0 authenticated, 1 not matched, 5 failed. + * Errors: EOPNOTSUPP without the feature, EKEYEXPIRED once the + * context has ended, EBUSY while another request runs, ECANCELED when + * cancelled, ETIMEDOUT at timeout_ms. + */ +#define SEP_KEY_CTX_NEW_BAG 0x1 +#define SEP_KEY_CTX_LIFETIME_MAX (12 * 3600) +#define SEP_KEY_CTX_METHOD_TOUCH 1 +#define SEP_KEY_CTX_OUTCOME_OK 0 +#define SEP_KEY_CTX_OUTCOME_NOT_MATCHED 1 +#define SEP_KEY_CTX_OUTCOME_FAILED 5 + +struct sep_key_buffer { + __u32 len; + __u32 reserved; + __u64 ptr; +}; + +struct sep_key_ctx_new { + __u32 flags; /* SEP_KEY_CTX_NEW_BAG */ + __s32 fd; /* out: the context */ + __u32 uid; /* the bag's Unix account */ + __u32 lifetime_s; /* 0..SEP_KEY_CTX_LIFETIME_MAX */ +}; + +struct sep_key_ctx_auth { + __u64 id; + __u32 timeout_ms; + __u32 method; /* SEP_KEY_CTX_METHOD_TOUCH */ + __u32 outcome; /* out: SEP_KEY_CTX_OUTCOME_* */ + __u32 reserved; +}; + +struct sep_key_ctx_query { + __u32 phase; + __u32 cleanup; + __s32 reason; + __u32 activity_flags; +}; + +struct sep_key_ctx_revoke { + __u32 timeout_ms; + __u32 reserved; +}; + +struct sep_key_ctx_sign { + __u64 id; + __u32 timeout_ms; + __s32 context_fd; + __u32 blob_len; + __u32 reserved; + __u64 blob_ptr; + __u8 digest[SEP_KEY_DIGEST_LEN]; + __u32 sig_len; /* out */ + __u32 reserved2; + __u8 signature[SEP_KEY_SIG_MAX]; +}; + +struct sep_key_ctx_decrypt { + __u64 id; + __u32 timeout_ms; + __s32 context_fd; + struct sep_key_buffer blob; + struct sep_key_buffer sealed; + struct sep_key_buffer plain; /* in: capacity, out: length */ +}; + +struct sep_key_cancel_id { + __u64 request_id; +}; + +#define SEP_KEY_CTX_NEW _IOWR(SEP_BIO_IOC_MAGIC, 0x40, struct sep_key_ctx_new) +#define SEP_KEY_CTX_AUTH _IOWR(SEP_BIO_IOC_MAGIC, 0x41, struct sep_key_ctx_auth) +#define SEP_KEY_CTX_QUERY _IOR(SEP_BIO_IOC_MAGIC, 0x42, struct sep_key_ctx_query) +#define SEP_KEY_CTX_REVOKE _IOW(SEP_BIO_IOC_MAGIC, 0x43, struct sep_key_ctx_revoke) +#define SEP_KEY_CTX_SIGN _IOWR(SEP_BIO_IOC_MAGIC, 0x44, struct sep_key_ctx_sign) +#define SEP_KEY_CTX_DECRYPT _IOWR(SEP_BIO_IOC_MAGIC, 0x45, struct sep_key_ctx_decrypt) +#define SEP_KEY_CANCEL_ID _IOW(SEP_BIO_IOC_MAGIC, 0x48, struct sep_key_cancel_id) + /* * Test only, refused unless apple_sep.key_selftest=1, root: sign with a * biometric key under a deliberately unsuitable credential context, to show diff --git a/tools/aurora-sep/keys/tests/check_context_lifecycle.py b/tools/aurora-sep/keys/tests/check_context_lifecycle.py new file mode 100644 index 0000000000000..d789c18f7c444 --- /dev/null +++ b/tools/aurora-sep/keys/tests/check_context_lifecycle.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Run the actual ownership transitions with a host-side secret drop observer.""" +from pathlib import Path +import subprocess +import tempfile + +root = Path(__file__).resolve().parents[4] +state = (root / 'drivers/soc/apple/key_result.rs').read_text() +source = r''' +#![allow(dead_code)] +extern crate self as kernel; +pub mod prelude {pub(crate) use crate::*;} +#[derive(Clone,Copy,Debug,PartialEq,Eq)] struct Error(i32); +type Result = std::result::Result; +const ERESTARTSYS:Error=Error(-512); const ERESTARTNOINTR:Error=Error(-513); +const ERESTARTNOHAND:Error=Error(-514); const ERESTART_RESTARTBLOCK:Error=Error(-516); +const EINTR:Error=Error(-4); const EBUSY:Error=Error(-16); const EIO:Error=Error(-5); +const ETIMEDOUT:Error=Error(-110); const EALREADY:Error=Error(-114); +const ENOENT:Error=Error(-2); const EAGAIN:Error=Error(-11); const ENODATA:Error=Error(-61); +const ECANCELED:Error=Error(-125); const EACCES:Error=Error(-13); +use std::sync::{Arc,Mutex,atomic::{AtomicUsize,Ordering::Relaxed}}; +#[derive(Debug)] struct Secret(Arc); +impl Drop for Secret {fn drop(&mut self){self.0.fetch_add(1,Relaxed);}} +mod key_request {pub(crate) type Output=super::Secret;} +mod delivery { +'''+state+r''' +} +#[test]fn terminal_errors_survive_expiry_and_late_completion(){ + for error in [ECANCELED,EACCES,EIO] { + let mut state=delivery::State::Pending; + if error==ECANCELED {state.cancel(error).unwrap();} else {assert!(state.complete(Err(error),true));} + state.expire(); assert_eq!(state.claim().unwrap_err(),error); + assert!(!state.complete(Err(ETIMEDOUT),false)); assert_eq!(state.claim().unwrap_err(),error); + } +} +#[test]fn cancellation_and_expiry_destroy_unclaimed_plaintext(){ + for expire in [false,true] { + let drops=Arc::new(AtomicUsize::new(0));let mut state=delivery::State::Pending; + assert!(state.complete(Ok(Secret(drops.clone())),false)); + if expire {state.expire();} else {state.cancel(ECANCELED).unwrap();} + assert_eq!(drops.load(Relaxed),1); + assert_eq!(state.claim().unwrap_err(),if expire{ETIMEDOUT}else{ECANCELED}); + } +} +#[test]fn cancel_versus_claim_has_one_linearization_point(){ + for _ in 0..100 { + let drops=Arc::new(AtomicUsize::new(0));let mut initial=delivery::State::Pending; + initial.complete(Ok(Secret(drops.clone())),false); + let state=Arc::new(Mutex::new(initial));let other=state.clone(); + let cancel=std::thread::spawn(move||other.lock().unwrap().cancel(ECANCELED)); + let claim=state.lock().unwrap().claim();let canceled=cancel.join().unwrap(); + match claim {Ok(secret)=>{assert_eq!(canceled,Err(EALREADY));drop(secret);}, + Err(error)=>{assert_eq!(error,ECANCELED);assert_eq!(canceled,Ok(()));}} + assert_eq!(drops.load(Relaxed),1); + } +} +#[test]fn claimed_output_does_not_hold_a_delivery_lock(){ + let drops=Arc::new(AtomicUsize::new(0));let mut state=delivery::State::Pending; + state.complete(Ok(Secret(drops.clone())),false);let output=state.claim().unwrap(); + assert_eq!(state.cancel(ECANCELED),Err(EALREADY));state.expire(); + assert_eq!(state.claim().unwrap_err(),ENODATA);drop(output);assert_eq!(drops.load(Relaxed),1); +} +''' + +# Exercise the actual completion methods at the worker-retirement boundary too. +request = (root / 'drivers/soc/apple/key_request.rs').read_text() +def method(name): + start = request.rfind('\n', 0, request.index('fn ' + name + '(')) + 1 + brace = request.index('{', start) + depth, end = 1, brace + 1 + while depth: + depth += (request[end] == '{') - (request[end] == '}') + end += 1 + return request[start:end] + +source += r""" +mod shim {pub fn monotonic_ns()->u64{0}} +mod completion { +use super::*; +use crate::delivery::State; +use crate::key_request::Output; +use std::sync::{atomic::{AtomicBool,AtomicU64,Ordering::{Acquire,Release}},Condvar}; +struct Mutex(std::sync::Mutex); +implMutex{fn lock(&self)->std::sync::MutexGuard<'_,T>{self.0.lock().unwrap()}} +struct Event {sequence:AtomicU64,lock:std::sync::Mutex<()>,changed:Condvar} +impl Event { + fn new()->Self{Self{sequence:AtomicU64::new(0),lock:std::sync::Mutex::new(()),changed:Condvar::new()}} + fn sequence(&self)->u64{self.sequence.load(Acquire)} + fn wake(&self){let _lock=self.lock.lock().unwrap();self.sequence.fetch_add(1,Release);self.changed.notify_all();} + fn wait(&self,sequence:u64,_:u64)->Result{ + let mut lock=self.lock.lock().unwrap(); + while self.sequence.load(Acquire)==sequence{lock=self.changed.wait(lock).unwrap();}Ok(())} +} +struct Completion {phase:Mutex,changed:Event,deadline_ns:u64,retired:AtomicBool} +impl Completion { + fn new()->Arc{Arc::new(Self{phase:Mutex(std::sync::Mutex::new(State::Pending)), + changed:Event::new(),deadline_ns:u64::MAX,retired:AtomicBool::new(false)})} +""" + '\n'.join(method(name) for name in ('complete','retire','cancel','claim','wait_ready')) + r""" +} +#[test]fn success_waits_for_capture_release_without_consuming_output(){ + let done=Completion::new();let drops=Arc::new(AtomicUsize::new(0)); + done.complete(Ok(Secret(drops.clone())));assert!(matches!(done.claim(),Err(EAGAIN))); + let other=done.clone();let (tx,rx)=std::sync::mpsc::channel(); + let waiter=std::thread::spawn(move||{other.wait_ready().unwrap();tx.send(other.claim()).unwrap();}); + assert!(rx.recv_timeout(std::time::Duration::from_millis(20)).is_err()); + done.retire();drop(rx.recv_timeout(std::time::Duration::from_secs(1)).unwrap().unwrap()); + waiter.join().unwrap();assert_eq!(drops.load(Relaxed),1);assert!(matches!(done.claim(),Err(ENODATA))); +} +#[test]fn cancellation_does_not_wait_for_worker_retirement_or_revive_output(){ + let done=Completion::new();let drops=Arc::new(AtomicUsize::new(0)); + done.complete(Ok(Secret(drops.clone())));done.cancel(ECANCELED).unwrap();done.wait_ready().unwrap(); + assert!(matches!(done.claim(),Err(ECANCELED)));assert_eq!(drops.load(Relaxed),1); + done.retire();assert!(matches!(done.claim(),Err(ECANCELED))); +} +} +""" + +with tempfile.TemporaryDirectory(prefix='sep-context-lifecycle-') as name: + directory = Path(name) + (directory / 'test.rs').write_text(source) + subprocess.run(['rustc', '--edition=2021', '--test', str(directory / 'test.rs'), + '-o', str(directory / 'test')], check=True) + subprocess.run([str(directory / 'test'), '--test-threads=1'], check=True) diff --git a/tools/aurora-sep/tests/test_key_context_cleanup.py b/tools/aurora-sep/tests/test_key_context_cleanup.py index a127590b89ab0..741af467836bd 100644 --- a/tools/aurora-sep/tests/test_key_context_cleanup.py +++ b/tools/aurora-sep/tests/test_key_context_cleanup.py @@ -14,7 +14,7 @@ def test_production_context_disposal(self): source = (ROOT / "drivers/soc/apple/key.rs").read_text() trait = source[source.index("pub(crate) trait ContextDelete"): source.index("impl ContextDelete for SepData")] - guard = source[source.index("/// A credential context"): + guard = source[source.index("pub(crate) struct OwnedContext"): source.index("/// Self-test copies")] tests = source[source.index(" struct Deleter {"): source.index(" // Production kernels do not build")] From 2213645fbf83906064231f3c8beeda05f72be500 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Mon, 16 Nov 2026 13:20:30 -0600 Subject: [PATCH 21/24] soc: apple: sep: sep-key: authenticate a context with the bag's password Touch ID is not always there to authorize a context: no finger may be enrolled, the bag may have just been unlocked after a lock or a restart, or the user may prefer the password, as macOS offers it in its prompt. Add SEP_KEY_CTX_AUTH_PASSWORD. The enclave checks the password against that context's bag, and counts a wrong one, into a fresh credential that becomes the context's grant; a wrong password ends the context (outcome NOT_MATCHED), so another attempt opens a new one. The kernel wipes its copy of the password, in the reply too. An accepted password also renews the bag's Touch ID preparation, as a bag unlock does. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/key_context_device.rs | 91 +++++++++++++++++++++++++ include/uapi/linux/apple-sep.h | 19 ++++-- 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/drivers/soc/apple/key_context_device.rs b/drivers/soc/apple/key_context_device.rs index 8cc2857eaccae..943e911864cc7 100644 --- a/drivers/soc/apple/key_context_device.rs +++ b/drivers/soc/apple/key_context_device.rs @@ -40,6 +40,16 @@ struct Authenticate { outcome: u32, reserved: u32, } +/// Password authentication of a bag context: the enclave checks the +/// password against that bag (and counts a wrong one) into a fresh credential +/// that becomes the context's grant. The kernel wipes its copy. +#[repr(C)] +struct AuthPassword { + id: u64, + timeout_ms: u32, + outcome: u32, + password: keys::Password, +} #[repr(C)] struct Query { phase: u32, @@ -77,6 +87,7 @@ struct ContextDecrypt { static_assert!(core::mem::size_of::() == 16); static_assert!(core::mem::size_of::() == 24); +static_assert!(core::mem::size_of::() == 16 + 8 + keys::PASSWORD_MAX); static_assert!(core::mem::size_of::() == 16); static_assert!(core::mem::size_of::() == 8); static_assert!(core::mem::size_of::() == 144); @@ -86,6 +97,8 @@ unsafe impl kernel::transmute::FromBytes for NewContext {} unsafe impl kernel::transmute::AsBytes for NewContext {} unsafe impl kernel::transmute::FromBytes for Authenticate {} unsafe impl kernel::transmute::AsBytes for Authenticate {} +unsafe impl kernel::transmute::FromBytes for AuthPassword {} +unsafe impl kernel::transmute::AsBytes for AuthPassword {} unsafe impl kernel::transmute::AsBytes for Query {} unsafe impl kernel::transmute::FromBytes for Revoke {} unsafe impl kernel::transmute::FromBytes for ContextSign {} @@ -95,6 +108,7 @@ unsafe impl kernel::transmute::AsBytes for ContextDecrypt {} const NEW: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x40); const AUTH: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x41); +const AUTH_PASSWORD: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x46); const QUERY: u32 = kernel::ioctl::_IOR::(crate::key::MAGIC, 0x42); const REVOKE: u32 = kernel::ioctl::_IOW::(crate::key::MAGIC, 0x43); const SIGN: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x44); @@ -360,6 +374,9 @@ unsafe extern "C" fn context_ioctl(pointer: *mut c_void, cmd: c_uint, arg: c_ulo let request: Cancel = UserSlice::new(user, 8).reader().read()?; return context.cancel(request.request_id); } + if cmd == AUTH_PASSWORD { + return auth_password(&context, user); + } if cmd != AUTH { return Err(ENOTTY); } @@ -413,6 +430,80 @@ unsafe extern "C" fn context_ioctl(pointer: *mut c_void, cmd: c_uint, arg: c_ulo } } +fn auth_password(context: &Arc, user: UserPtr) -> Result { + context.check_domain()?; + if context.sep.key_context_failed.load(Relaxed) { + return Err(EIO); + } + let size = core::mem::size_of::(); + // `req` wipes the password when it drops, on every path. + let mut req: AuthPassword = UserSlice::new(user, size).reader().read()?; + if req.id == 0 || req.outcome != 0 { + return Err(EINVAL); + } + let uid = context.bag.ok_or(EOPNOTSUPP)?; + let deadline_ns = deadline(req.timeout_ms)?; + let status = (|| { + let pw = keys::check_password(&req.password)?; + let request = Request::new( + req.id, + crate::key_request::KeyRequest { + blob: KVec::new(), + input: crate::key_request::Input::Authenticate(context.clone()), + deadline_ns, + }, + )?; + context.authenticate(request.id, &request.completion)?; + let made = (|| { + let bag = keys::bag_user(uid)?; + let (handle, verify) = context.sep.key_context_for( + -bag.special_handle().value(), + Some((bag.special_handle(), pw)), + )?; + // Dropped on refusal: the credential deletes its context. + let credential = crate::key_context::Credential::new(context.sep.clone(), handle); + if verify != 0 { + return Err(EACCES); + } + // The password also renews this user's Touch ID, as a bag + // unlock does. + context.sep.userbag_refresh_touch_13(bag, pw); + Ok(credential) + })(); + context.finish_auth( + &request.completion, + made.map(|credential| (credential, Ok(()))), + ); + // No capture is held, so the result is deliverable at once. + request.retire(); + let result = request + .completion + .wait_ready() + .and_then(|()| context.claim(&request.completion)); + context.forget(&request.completion); + match result { + Ok(crate::key_request::Output::Authenticated) => Ok(()), + Ok(_) => Err(EIO), + Err(error) => Err(error), + } + })(); + if let Err(error) = status { + context.revoke(error); + } + req.outcome = match status { + Ok(()) => OUTCOME_OK, + Err(error) if error == EACCES => OUTCOME_NOT_MATCHED, + _ => OUTCOME_FAILED, + }; + crate::image::wipe(&mut req.password.bytes); + req.password.len = 0; + if let Err(error) = UserSlice::new(user, size).writer().write(&req) { + context.revoke(error); + return Err(error); + } + status +} + /// A context file's release. /// /// # Safety diff --git a/include/uapi/linux/apple-sep.h b/include/uapi/linux/apple-sep.h index f92148dbf9d4f..5fe499a37ae5e 100644 --- a/include/uapi/linux/apple-sep.h +++ b/include/uapi/linux/apple-sep.h @@ -420,10 +420,11 @@ struct sep_key_unseal_password { * Authorization contexts (SEP_KEY_FEATURE_CONTEXTS; module parameter * key_context_api=1, 13.5 key store). DESIGN DRAFT, not a stable ABI. * - * One authentication (a Touch ID match in the bag user's own domain) - * authorizes a bounded run of operations on that bag's keys, as macOS lets - * an authentication be reused for a short time: a password manager reveals - * many items after one unlock. The bounds: + * One authentication (a Touch ID match in the bag user's own domain, or the + * bag's password checked by the enclave) authorizes a bounded run of + * operations on that bag's keys, as macOS lets an authentication be reused + * for a short time: a password manager reveals many items after one unlock. + * The bounds: * * - a context belongs to one Unix account's key bag (NEW: root or that * user) and is a file descriptor: closing it ends it; @@ -436,7 +437,7 @@ struct sep_key_unseal_password { * Every request carries a caller-chosen nonzero id; SEP_KEY_CANCEL_ID on * the file it was made on (the /dev/sep-key file, or the context for its * AUTH) ends that request, such as an AUTH waiting for a finger. - * Outcome: 0 authenticated, 1 not matched, 5 failed. + * Outcome: 0 authenticated, 1 not matched or wrong password, 5 failed. * Errors: EOPNOTSUPP without the feature, EKEYEXPIRED once the * context has ended, EBUSY while another request runs, ECANCELED when * cancelled, ETIMEDOUT at timeout_ms. @@ -469,6 +470,13 @@ struct sep_key_ctx_auth { __u32 reserved; }; +struct sep_key_ctx_auth_password { + __u64 id; + __u32 timeout_ms; + __u32 outcome; /* out */ + struct sep_key_password password; +}; + struct sep_key_ctx_query { __u32 phase; __u32 cleanup; @@ -513,6 +521,7 @@ struct sep_key_cancel_id { #define SEP_KEY_CTX_REVOKE _IOW(SEP_BIO_IOC_MAGIC, 0x43, struct sep_key_ctx_revoke) #define SEP_KEY_CTX_SIGN _IOWR(SEP_BIO_IOC_MAGIC, 0x44, struct sep_key_ctx_sign) #define SEP_KEY_CTX_DECRYPT _IOWR(SEP_BIO_IOC_MAGIC, 0x45, struct sep_key_ctx_decrypt) +#define SEP_KEY_CTX_AUTH_PASSWORD _IOWR(SEP_BIO_IOC_MAGIC, 0x46, struct sep_key_ctx_auth_password) #define SEP_KEY_CANCEL_ID _IOW(SEP_BIO_IOC_MAGIC, 0x48, struct sep_key_cancel_id) /* From f9d69dec2c1e6e6b89ec33684fd9c0c0dbec07b6 Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Mon, 16 Nov 2026 13:21:45 -0600 Subject: [PATCH 22/24] soc: apple: sep: sep-key: bind a context to one key A context authorizes every key under its bag for as long as it lives. A password manager's vault session needs only its item key, and only to decrypt; if anything else in that process misused the context, it could not then decrypt a passkey's secret or sign with another key. Add SEP_KEY_CTX_BIND. Before AUTH, and only once, it names one key (by its handle, which must be under the context's bag) and the operations (SEP_KEY_CTX_SCOPE_SIGN, _DECRYPT) the context may run on it; SIGN and DECRYPT then refuse any other key or operation with EPERM. A BIND after authentication is refused with EBUSY: a scope cannot widen what an existing authorization covers. An unbound context behaves as before. KUnit covers the scope's checks. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/key_context.rs | 80 +++++++++++++++++++++++++ drivers/soc/apple/key_context_device.rs | 22 +++++++ include/uapi/linux/apple-sep.h | 21 +++++-- 3 files changed, 118 insertions(+), 5 deletions(-) diff --git a/drivers/soc/apple/key_context.rs b/drivers/soc/apple/key_context.rs index 5d67fff4ad18e..a7a4d527e0315 100644 --- a/drivers/soc/apple/key_context.rs +++ b/drivers/soc/apple/key_context.rs @@ -50,6 +50,32 @@ impl Drop for Credential { } } +/// `Context::bind` operations: what a bound context may run on its key. +pub(crate) const SCOPE_SIGN: u32 = 1; +pub(crate) const SCOPE_DECRYPT: u32 = 2; + +/// The one key a bound context may use, and how. A vault session bound to +/// its item key for decryption cannot then decrypt a passkey's secret or sign +/// with another key, however long it lives. +struct Scope { + record: KVec, + ops: u32, +} + +impl Scope { + fn new(record: KVec, ops: u32) -> Result { + if ops == 0 || ops & !(SCOPE_SIGN | SCOPE_DECRYPT) != 0 { + return Err(EINVAL); + } + Ok(Self { record, ops }) + } + + /// Whether `op` on the key `record` is within this scope. + fn allows(&self, record: &[u8], op: u32) -> bool { + self.record.as_slice() == record && self.ops & op != 0 + } +} + struct Grant([u8; scrd::SCRD_ACM_HANDLE_LEN]); impl Drop for Grant { fn drop(&mut self) { @@ -65,6 +91,7 @@ struct Inner { closing: bool, /// When the matched grant stops authorizing (monotonic ns); 0 never. expires_ns: u64, + scope: Option, } #[pin_data] @@ -136,6 +163,7 @@ impl Context { ids: keys::RequestIds::new(), closing: false, expires_ns: 0, + scope: None, }), changed: Event::new()?, work <- new_work!("sep_context_cleanup"), @@ -171,6 +199,27 @@ impl Context { let _ = self.sep.key_context_queue.enqueue(self.clone()); } + /// Limits the context to one key record and the `SCOPE_*` operations in + /// `ops`. Only once, and only before it is authenticated: a scope cannot + /// widen what an existing authorization covers. + pub(crate) fn bind(&self, record: KVec, ops: u32) -> Result { + let scope = Scope::new(record, ops)?; + let mut inner = self.inner.lock(); + if inner.life.phase != Phase::New || inner.scope.is_some() { + return Err(EBUSY); + } + inner.scope = Some(scope); + Ok(()) + } + + /// Refuses a key or an operation outside a bound context's scope. + pub(crate) fn in_scope(&self, record: &[u8], op: u32) -> Result { + match &self.inner.lock().scope { + Some(scope) if !scope.allows(record, op) => Err(EPERM), + _ => Ok(()), + } + } + pub(crate) fn authenticate(&self, id: u64, completion: &Arc) -> Result { let mut inner = self.inner.lock(); inner.ids.check(id)?; @@ -431,3 +480,34 @@ impl SepData { } } +#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)] +#[::kernel::macros::kunit_tests(apple_sep_key_context_scope)] +mod tests { + use super::*; + + fn record(bytes: &[u8]) -> KVec { + let mut out = KVec::new(); + out.extend_from_slice(bytes, GFP_KERNEL).unwrap(); + out + } + + #[test] + fn a_scope_names_known_operations() { + assert!(Scope::new(record(b"key"), 0).is_err()); + assert!(Scope::new(record(b"key"), 4).is_err()); + assert!(Scope::new(record(b"key"), SCOPE_SIGN | 4).is_err()); + assert!(Scope::new(record(b"key"), SCOPE_SIGN | SCOPE_DECRYPT).is_ok()); + } + + #[test] + fn a_scope_allows_only_its_key_and_operations() { + let scope = Scope::new(record(b"vault key"), SCOPE_DECRYPT).unwrap(); + assert!(scope.allows(b"vault key", SCOPE_DECRYPT)); + assert!(!scope.allows(b"vault key", SCOPE_SIGN)); + assert!(!scope.allows(b"passkey key", SCOPE_DECRYPT)); + assert!(!scope.allows(b"vault ke", SCOPE_DECRYPT)); + let both = Scope::new(record(b"k"), SCOPE_SIGN | SCOPE_DECRYPT).unwrap(); + assert!(both.allows(b"k", SCOPE_SIGN) && both.allows(b"k", SCOPE_DECRYPT)); + } +} + diff --git a/drivers/soc/apple/key_context_device.rs b/drivers/soc/apple/key_context_device.rs index 943e911864cc7..c42e7224e1a13 100644 --- a/drivers/soc/apple/key_context_device.rs +++ b/drivers/soc/apple/key_context_device.rs @@ -50,6 +50,14 @@ struct AuthPassword { outcome: u32, password: keys::Password, } +/// Binds a context, before AUTH, to the key in `handle` for the +/// `key_context::SCOPE_*` operations in `ops`. +#[repr(C)] +struct ContextBind { + ops: u32, + reserved: u32, + handle: Buffer, +} #[repr(C)] struct Query { phase: u32, @@ -87,6 +95,7 @@ struct ContextDecrypt { static_assert!(core::mem::size_of::() == 16); static_assert!(core::mem::size_of::() == 24); +static_assert!(core::mem::size_of::() == 24); static_assert!(core::mem::size_of::() == 16 + 8 + keys::PASSWORD_MAX); static_assert!(core::mem::size_of::() == 16); static_assert!(core::mem::size_of::() == 8); @@ -98,6 +107,7 @@ unsafe impl kernel::transmute::AsBytes for NewContext {} unsafe impl kernel::transmute::FromBytes for Authenticate {} unsafe impl kernel::transmute::AsBytes for Authenticate {} unsafe impl kernel::transmute::FromBytes for AuthPassword {} +unsafe impl kernel::transmute::FromBytes for ContextBind {} unsafe impl kernel::transmute::AsBytes for AuthPassword {} unsafe impl kernel::transmute::AsBytes for Query {} unsafe impl kernel::transmute::FromBytes for Revoke {} @@ -109,6 +119,7 @@ unsafe impl kernel::transmute::AsBytes for ContextDecrypt {} const NEW: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x40); const AUTH: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x41); const AUTH_PASSWORD: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x46); +const BIND: u32 = kernel::ioctl::_IOW::(crate::key::MAGIC, 0x47); const QUERY: u32 = kernel::ioctl::_IOR::(crate::key::MAGIC, 0x42); const REVOKE: u32 = kernel::ioctl::_IOW::(crate::key::MAGIC, 0x43); const SIGN: u32 = kernel::ioctl::_IOWR::(crate::key::MAGIC, 0x44); @@ -275,6 +286,7 @@ impl File { )?; let context = self.context(out.context_fd)?; let blob = decode_record(&blob, context.bag)?; + context.in_scope(&blob, crate::key_context::SCOPE_SIGN)?; let request = Request::new( out.id, crate::key_request::KeyRequest { @@ -312,6 +324,7 @@ impl File { } let context = self.context(out.context_fd)?; let blob = decode_record(&blob, context.bag)?; + context.in_scope(&blob, crate::key_context::SCOPE_DECRYPT)?; let request = Request::new( out.id, crate::key_request::KeyRequest { @@ -377,6 +390,15 @@ unsafe extern "C" fn context_ioctl(pointer: *mut c_void, cmd: c_uint, arg: c_ulo if cmd == AUTH_PASSWORD { return auth_password(&context, user); } + if cmd == BIND { + context.check_domain()?; + let request: ContextBind = UserSlice::new(user, 24).reader().read()?; + if request.reserved != 0 { + return Err(EINVAL); + } + let record = decode_record(&read(&request.handle, 1)?, context.bag)?; + return context.bind(record, request.ops); + } if cmd != AUTH { return Err(ENOTTY); } diff --git a/include/uapi/linux/apple-sep.h b/include/uapi/linux/apple-sep.h index 5fe499a37ae5e..241ba252aec8d 100644 --- a/include/uapi/linux/apple-sep.h +++ b/include/uapi/linux/apple-sep.h @@ -422,12 +422,14 @@ struct sep_key_unseal_password { * * One authentication (a Touch ID match in the bag user's own domain, or the * bag's password checked by the enclave) authorizes a bounded run of - * operations on that bag's keys, as macOS lets an authentication be reused - * for a short time: a password manager reveals many items after one unlock. - * The bounds: + * operations on that bag's keys, or with BIND on one of them, as macOS lets + * an authentication be reused for a short time: a password manager reveals + * many items after one unlock. The bounds: * * - a context belongs to one Unix account's key bag (NEW: root or that * user) and is a file descriptor: closing it ends it; + * - BIND, before AUTH, may name the one key and the operations + * (SEP_KEY_CTX_SCOPE_*) it authorizes; nothing else is then accepted; * - it lasts lifetime_s from the authentication (at most 12 hours, 0 = * until revoked), and ends at once when the bag locks, on suspend, on * REVOKE and when the driver stops; @@ -439,11 +441,13 @@ struct sep_key_unseal_password { * AUTH) ends that request, such as an AUTH waiting for a finger. * Outcome: 0 authenticated, 1 not matched or wrong password, 5 failed. * Errors: EOPNOTSUPP without the feature, EKEYEXPIRED once the - * context has ended, EBUSY while another request runs, ECANCELED when - * cancelled, ETIMEDOUT at timeout_ms. + * context has ended, EPERM outside its scope, EBUSY while another request + * runs, ECANCELED when cancelled, ETIMEDOUT at timeout_ms. */ #define SEP_KEY_CTX_NEW_BAG 0x1 #define SEP_KEY_CTX_LIFETIME_MAX (12 * 3600) +#define SEP_KEY_CTX_SCOPE_SIGN 0x1 +#define SEP_KEY_CTX_SCOPE_DECRYPT 0x2 #define SEP_KEY_CTX_METHOD_TOUCH 1 #define SEP_KEY_CTX_OUTCOME_OK 0 #define SEP_KEY_CTX_OUTCOME_NOT_MATCHED 1 @@ -477,6 +481,12 @@ struct sep_key_ctx_auth_password { struct sep_key_password password; }; +struct sep_key_ctx_bind { + __u32 ops; /* SEP_KEY_CTX_SCOPE_* */ + __u32 reserved; + struct sep_key_buffer handle; +}; + struct sep_key_ctx_query { __u32 phase; __u32 cleanup; @@ -522,6 +532,7 @@ struct sep_key_cancel_id { #define SEP_KEY_CTX_SIGN _IOWR(SEP_BIO_IOC_MAGIC, 0x44, struct sep_key_ctx_sign) #define SEP_KEY_CTX_DECRYPT _IOWR(SEP_BIO_IOC_MAGIC, 0x45, struct sep_key_ctx_decrypt) #define SEP_KEY_CTX_AUTH_PASSWORD _IOWR(SEP_BIO_IOC_MAGIC, 0x46, struct sep_key_ctx_auth_password) +#define SEP_KEY_CTX_BIND _IOW(SEP_BIO_IOC_MAGIC, 0x47, struct sep_key_ctx_bind) #define SEP_KEY_CANCEL_ID _IOW(SEP_BIO_IOC_MAGIC, 0x48, struct sep_key_cancel_id) /* From 15abbac99a82af3949879ac71d483668a61dbd5f Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Tue, 17 Nov 2026 14:54:22 -0600 Subject: [PATCH 23/24] soc: apple: sep: sep-key: contexts on the variant-5 store Authorization contexts were offered only on the 13.5 key store, but they need only per-user bags and a match in the bag user's own domain, which the variant-5 store (T8140, T6040 and its inheritors) now has too. Offer them on any key store with per-user bags. A variant-5 bag's lock, with or without a grace period and at logout, now revokes that bag's contexts before the store is asked, as the 13.5 lock does, so no retained match outlives it. On J716c (T6050) a bag context authorized with Touch ID or the bag's password signed and decrypted with the bag's presence keys, kept each key and operation to its binding, and ended when released. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/key_device.rs | 9 ++------- drivers/soc/apple/sep.rs | 2 +- drivers/soc/apple/userbag.rs | 12 ++++++++---- include/uapi/linux/apple-sep.h | 3 ++- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/drivers/soc/apple/key_device.rs b/drivers/soc/apple/key_device.rs index 59c1d87ce03c8..f591dd29b25b6 100644 --- a/drivers/soc/apple/key_device.rs +++ b/drivers/soc/apple/key_device.rs @@ -22,14 +22,9 @@ unsafe impl kernel::transmute::FromBytes for Cancel {} const IOC_CANCEL: u32 = kernel::ioctl::_IOW::(crate::key::MAGIC, 0x48); /// Whether the authorization-context requests are offered: opted in by -/// `key_context_api`, on the 13.5 key store with per-user bags. +/// `key_context_api`, on a key store with per-user bags (13.5 or variant 5). pub(crate) fn contexts_supported(sep: &SepData) -> bool { - crate::module_parameters::key_context_api.value() != 0 - && sep.profile.per_user_keybags - && matches!( - sep.profile.key_store, - crate::profile::KeyStore::Sepos13 { .. } - ) + crate::module_parameters::key_context_api.value() != 0 && sep.profile.per_user_keybags } enum Operation { diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 89ad170782e92..181f0560f082a 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -3827,7 +3827,7 @@ module! { }, key_context_api: u8 { default: 0, - description: "Enable the authorization-context requests on /dev/sep-key (SEP_KEY_CTX_*, a design draft; 13.5 key store with per-user bags)", + description: "Enable the authorization-context requests on /dev/sep-key (SEP_KEY_CTX_*, a design draft; key stores with per-user bags)", }, key_selftest: u8 { default: 0, diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index 87467fbbd0f93..d95ebe9222366 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -1290,9 +1290,13 @@ impl crate::SepData { self.user_bags.lock().track_designated(user.special_handle().value()) } - /// A lock of `user`'s bag (variant 5): its Touch ID preparation ends - /// before the store is asked, whatever it answers. + /// A lock of `user`'s bag (variant 5): its contexts and its Touch ID + /// preparation end before the store is asked, whatever it answers. fn userbag_end_touch(&self, user: crate::sks::DesignateUser) { + // Retained matches for this bag's keys end with the lock. + if let Some(uid) = u32::try_from(-user.special_handle().value()).ok().and_then(|id| id.checked_sub(USER_BAG_BASE)) { + self.revoke_bag_contexts(uid); + } let touch = self.user_bags.lock().entry(user.special_handle().value()).and_then(|e| e.touch.take()); if let Some(t) = touch { self.userbag_dispose_touch(t); @@ -1656,8 +1660,8 @@ impl crate::SepData { } return self.userbag_lock_13(user, 0); } - // Variant 5: this bag's Touch ID ends with any lock, including one - // with a grace period. + // Variant 5: this bag's contexts and Touch ID end with any lock, + // including one with a grace period. self.userbag_end_touch(user); if immediate { let handle = user.special_handle().value(); diff --git a/include/uapi/linux/apple-sep.h b/include/uapi/linux/apple-sep.h index 241ba252aec8d..50b6477fd6802 100644 --- a/include/uapi/linux/apple-sep.h +++ b/include/uapi/linux/apple-sep.h @@ -418,7 +418,8 @@ struct sep_key_unseal_password { /* * Authorization contexts (SEP_KEY_FEATURE_CONTEXTS; module parameter - * key_context_api=1, 13.5 key store). DESIGN DRAFT, not a stable ABI. + * key_context_api=1, a key store with per-user bags). DESIGN DRAFT, not a + * stable ABI. * * One authentication (a Touch ID match in the bag user's own domain, or the * bag's password checked by the enclave) authorizes a bounded run of From fb42dbfd6cf2fa0bb53c47eedafa75d66f1a073b Mon Sep 17 00:00:00 2001 From: Cameron Cooper Date: Tue, 17 Nov 2026 17:16:13 -0600 Subject: [PATCH 24/24] soc: apple: sep: wait for the last touch before any enclave request One owner for the sensor at a time made every /dev/sep-bio request that takes the enclave hold the capture, but only enrol and verify starts waited for the last enrol or verify worker to stop. A no-match is reported before that worker's bookkeeping ends (the lockout record and the background result), so the request right after one often found the sensor still held and was refused with EBUSY: fprintd's enrolment after its duplicate check (PREPARE_ENROL) failed, and libfprint's GET_INFO before a retry failed, which made that retry match as the built-in user instead of the account. Every request that takes the enclave now waits for the worker, bounded as a capture start is. The worker also frees the sensor before it marks itself stopped, so a request woken by its exit finds the sensor free. On J716c (T6050), fprintd's enrolment of a new finger had failed with EBUSY right after the duplicate check. Assisted-by: LLM [Claude Code, claude-opus-5-5] Signed-off-by: Cameron Cooper --- drivers/soc/apple/sbio.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 6f59cebbde419..bbc263dd89b94 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -5221,8 +5221,9 @@ impl SepData { /// The enrol or verify work item `epoch` has stopped. pub(crate) fn capture_worker_stopped(&self, epoch: u64) { - bio::worker_stopped(&mut self.bio_session.lock(), epoch); + // The sensor first: a request woken by the worker's exit finds it free. self.capture.lock().finish_bio(epoch); + bio::worker_stopped(&mut self.bio_session.lock(), epoch); self.worker_gone.notify_all(); } @@ -6047,6 +6048,12 @@ impl SepData { } if bio::starts_capture(cmd) { self.wait_until_awake()?; + } + // The last enrol or verify worker keeps the sensor until its + // bookkeeping ends, after a no-match is already reported. Every + // request that takes the enclave waits for it, or fprintd's next call + // (a duplicate check's enrolment, a retry's GET_INFO) is refused. + if bio::uses_enclave(cmd) { self.wait_for_worker_exit(&mut self.bio_session.lock(), false)?; } let lease = if bio::uses_enclave(cmd) { Some(crate::capture::Lease::acquire(self)?) } else { None };