Skip to content
3 changes: 2 additions & 1 deletion drivers/soc/apple/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions drivers/soc/apple/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -222,6 +232,9 @@ pub(crate) const T8103: PlatformProfile = PlatformProfile {
cpx_encryption_mode: 2,
},
persistent_enrol: true,
// 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.
Expand Down Expand Up @@ -263,6 +276,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.
Expand Down Expand Up @@ -297,6 +311,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,
Expand Down Expand Up @@ -326,6 +342,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",
Expand Down Expand Up @@ -398,6 +417,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,
Expand Down
71 changes: 54 additions & 17 deletions drivers/soc/apple/refkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
Expand All @@ -37,17 +54,7 @@ impl SepData {
}

fn sks_req_refkey(&self, keybag: i32, der_set: &[u8]) -> Result<SksRequest> {
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)?;
Expand Down Expand Up @@ -530,18 +537,24 @@ 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,
acl: Option<&[u8]>,
) -> Result<KVec<u8>> {
if self.profile.key_store != profile::KeyStore::Variant5 {
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
Expand Down Expand Up @@ -601,7 +614,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<KVec<u8>> {
if self.profile.key_store != profile::KeyStore::Variant5 {
if !self.profile.per_user_keybags {
return Err(EOPNOTSUPP);
}
crate::xart_guard::close_policy_window();
Expand Down Expand Up @@ -651,7 +664,7 @@ impl SepData {
record: &[u8],
sealed: &[u8],
) -> Result<KVec<u8>> {
if self.profile.key_store != profile::KeyStore::Variant5 {
if !self.profile.per_user_keybags {
return Err(EOPNOTSUPP);
}
Self::refkey_pub(record).ok_or(EINVAL)?;
Expand Down Expand Up @@ -805,3 +818,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(())
}
}
6 changes: 6 additions & 0 deletions drivers/soc/apple/sep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,11 @@ struct SepData {
#[pin]
cold_match_contexts: Mutex<KVec<[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>>,

// Per-user bags the 13.5 key store keeps loaded between import and
// logout (see `userbag::LoadedBags`).
#[pin]
user_bags: Mutex<userbag::LoadedBags>,

// 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.
Expand Down Expand Up @@ -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),
Expand Down
15 changes: 11 additions & 4 deletions drivers/soc/apple/sks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);

Expand Down
Loading