Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion drivers/soc/apple/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,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
57 changes: 57 additions & 0 deletions drivers/soc/apple/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,34 @@ 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,
/// The enclave never answers a user-bag `DESIGNATE_KEYBAG`, and that
/// unanswered request condemns the SKS endpoint for the rest of the boot
/// (`sks_wedged`), taking the whole key store down. Measured on T8112
/// (J415, OS firmware 26.6.2): CREATE_KEYBAG returns a handle, then
/// DESIGNATE_KEYBAG gets no reply. Such an enclave refuses per-user key
/// bags with `EOPNOTSUPP` before any request is sent (see
/// [`PlatformProfile::per_user_keybags`]).
pub(crate) designate_unanswered: bool,
/// The `xart_guard` level when the parameter is left at its default (see
/// [`crate::xart_guard`]).
pub(crate) xart_guard: u8,
}

impl PlatformProfile {
/// Whether the enclave can create and designate a NEW per-user key bag
/// (`userbag_ioctl`, linux#921): a rule of the key store, not of the SoC.
/// Both key stores have the user-bag requests, the variant-5 store's and
/// the 13.5 store's own shapes (`userbag_sepos13`, run on T8103). An
/// enclave with the measured unanswered-DESIGNATE defect is the only
/// exception, and a key store added later is refused until its requests
/// are written.
pub(crate) const fn per_user_keybags(&self) -> bool {
match self.key_store {
KeyStore::Variant5 | KeyStore::Sepos13 { .. } => !self.designate_unanswered,
}
}
}

pub(crate) const T8103: PlatformProfile = PlatformProfile {
name: "T8103/J313",
shmem_capacity: 0x3_0000,
Expand Down Expand Up @@ -222,6 +245,7 @@ pub(crate) const T8103: PlatformProfile = PlatformProfile {
cpx_encryption_mode: 2,
},
persistent_enrol: true,
designate_unanswered: 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.
Expand Down Expand Up @@ -263,6 +287,7 @@ pub(crate) const T6000: PlatformProfile = PlatformProfile {
cpx_encryption_mode: 2,
},
persistent_enrol: true,
designate_unanswered: 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 +322,8 @@ pub(crate) const T6020: PlatformProfile = PlatformProfile {
},
key_store: KeyStore::Variant5,
persistent_enrol: true,
// Not measured: no M2 Pro/Max has run a user-bag DESIGNATE.
designate_unanswered: false,
nonce_session: false,
// A warm-attach enclave shared with macOS, as on T8112.
xart_guard: 1,
Expand Down Expand Up @@ -326,6 +353,8 @@ pub(crate) const T6020_SEPOS13: PlatformProfile = PlatformProfile {
pub(crate) const T8140: PlatformProfile = PlatformProfile {
key_store: KeyStore::Variant5,
persistent_enrol: true,
// J700 answers the user-bag CREATE and DESIGNATE (variant-5 capture).
designate_unanswered: false,
name: "T8140/J700",
shmem_capacity: 0x4_0000,
shmem_first_item: b"CINP",
Expand Down Expand Up @@ -398,6 +427,8 @@ pub(crate) const T8112: PlatformProfile = PlatformProfile {
},
key_store: KeyStore::Variant5,
persistent_enrol: true,
// J415 never answers a user-bag DESIGNATE_KEYBAG (see the field).
designate_unanswered: true,
nonce_session: false,
// Guarded from its first Linux boot (2026-10-16): no record refused.
xart_guard: 1,
Expand Down Expand Up @@ -427,6 +458,10 @@ pub(crate) const T8122: PlatformProfile = PlatformProfile {
dart_window: &DART_HIGH_ALIAS,
// A warm-attach enclave shared with macOS, as on T8112.
xart_guard: 1,
// The unanswered DESIGNATE is a J415 measurement, not a property of the
// T8112 profile this one extends; no M3 (nor the M4/M5 Airs extending
// this) has run a user-bag DESIGNATE.
designate_unanswered: false,
..T8112
};

Expand Down Expand Up @@ -799,6 +834,28 @@ mod tests {
assert!(T6040.xart_guard == 1);
}

#[test]
fn per_user_keybags_follow_the_key_store_and_the_designate_defect() {
// The 13.5 key store's own user-bag requests: run on T8103 (J293);
// T6000 and T6020 on the 13.5 store use the same requests.
for p in [&T8103, &T6000, &T6020_SEPOS13] {
assert!(matches!(p.key_store, KeyStore::Sepos13 { .. }));
assert!(!p.designate_unanswered);
assert!(p.per_user_keybags());
}
// The measured unanswered DESIGNATE.
assert!(T8112.designate_unanswered);
assert!(!T8112.per_user_keybags());
// Every other variant-5 enclave.
for p in [
&T6020, &T8140, &T8122, &T6040, &T6041, &T6050, &T8132, &T8142,
] {
assert!(p.key_store == KeyStore::Variant5);
assert!(!p.designate_unanswered);
assert!(p.per_user_keybags());
}
}

/// The name of the profile `select` picks for a root `compatible` list.
fn picked(compatibles: &[&[u8]]) -> Option<&'static str> {
let has = |c: &[u8]| compatibles.iter().any(|x| *x == c);
Expand Down
74 changes: 57 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,25 @@ 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);
}
self.userbag_key_admitted(user)?;
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 All @@ -554,6 +568,7 @@ impl SepData {
acm: Option<&[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>,
) -> Result<KVec<u8>> {
Self::refkey_pub(blob).ok_or(EINVAL)?;
self.userbag_key_admitted(user)?;
let eph = refkey_seal::sealed_ephemeral(sealed)?;
let handle = crate::sks::KeyBagHandle::from_load_reply(user.special_handle().value());
let shared = crate::Secret(self.sks_refkey_key_exchange(handle, blob, eph, acm)?);
Expand All @@ -568,6 +583,7 @@ impl SepData {
digest: &[u8; 32],
acm: Option<&[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>,
) -> Result<KVec<u8>> {
self.userbag_key_admitted(user)?;
let handle = crate::sks::KeyBagHandle::from_load_reply(user.special_handle().value());
self.sks_refkey_sign(handle, blob, digest, acm)
}
Expand Down Expand Up @@ -601,7 +617,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 +667,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 +821,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
Loading