Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 29 additions & 1 deletion drivers/soc/apple/sks.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
// Copyright 2026 Dj
//! SEP key store (SKS, endpoint `0x12`): key-bag and key-management request/reply
Expand Down Expand Up @@ -669,12 +669,40 @@
return false;
}
};
// A cold-boot Mac's create rewrites a pre-Linux record for its own
// reasons (XART-SAFETY.md), so only warm attach gets the window.
let first_setup = self.profile.bootstrap == profile::Bootstrap::WarmRegister
&& self
.with_store(|store| store.open_first_setup())
.unwrap_or(false);
let refusals = || self.with_store(|store| store.guard_refusals()).unwrap_or(0);
let refused_before = refusals();
intent.sending();
let Some(out) = self.sks_exchange(request.name, request.msg, &request.img) else {
let exchanged = self.sks_exchange(request.name, request.msg, &request.img);
if first_setup {
self.with_store(|store| store.close_first_setup());
}
let Some(out) = exchanged else {
dev_err!(
self.dev,
"sks: CREATE_KEYBAG exchange did not complete (buffers unregistered, wedged, or OOL write failed)\n"
);
// The enclave could not save the state the create needed, so it
// kept no keybag: let the next boot try again rather than refuse
// every attempt as a keybag that may exist unnamed.
if refusals() > refused_before {
dev_warn!(
self.dev,
"sks: the xART guard refused the enclave's write during CREATE_KEYBAG; the next boot may try again\n"
);
if let Err(e) = keybag::mark_refused(slot) {
dev_err!(
self.dev,
"sks: cannot mark the refused keybag create retryable: {:?}\n",
e
);
}
}
return false;
};
let Some(body) = self.sks_report_response(crate::sks::SKS_CREATE_NAME, &out) else {
Expand Down
113 changes: 113 additions & 0 deletions drivers/soc/apple/xart_guard.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
// Copyright 2026 Ryan Murray

Expand Down Expand Up @@ -33,6 +33,15 @@
//! record that was never written, which is harmless, and a failed adoption is
//! retried in the same slot rather than skipped. It can never leave a Linux
//! record that the next boot treats as another OS's.
//!
//! One exception lets a fresh install set up Touch ID on a warm-attach Mac.
//! Blessing a newly installed OS in macOS leaves a SEP-xART locker (kind 4)
//! that every OS's enclave reads at attach in a pending state, and the next
//! enclave to save its state commits it. macOS's enclave does that at boot;
//! under Linux the first save is the first `CREATE_KEYBAG`. While that create
//! runs on an install whose list is loaded and empty ([`FirstSetup`]), the
//! guard lets the enclave rewrite one existing kind-4 locker. It does not list
//! the locker as Linux's, so the next rewrite is refused again.

use crate::shim::StoreFile;
use crate::xart_store::{crc32_ieee, Key};
Expand Down Expand Up @@ -79,6 +88,24 @@
}
}

/// Whether the first keybag creation of a fresh install is in progress.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FirstSetup {
/// No first keybag creation is in progress.
Closed,
/// The enclave may rewrite one existing SEP-xART locker.
Open,
/// It has; further refusals stand.
Used,
}

/// Whether a request the guard would refuse passes as the enclave committing a
/// pending SEP-xART locker during the first keybag creation: a rewrite (not a
/// delete) of a kind-4 record at level 1, while the window is open.
pub(crate) fn first_setup_allows(level: u8, window: FirstSetup, key: &Key, write: bool) -> bool {
level == 1 && window == FirstSetup::Open && write && key.kind == 4
}

/// The guard level for `xart_guard=param` on a platform whose profile
/// defaults to `platform`, or `None` for no guard. A negative value takes the
/// platform's level; a level above 2 is treated as 2, the strictest.
Expand Down Expand Up @@ -206,6 +233,7 @@
owned: KVec<Key>,
existed: KVec<Key>,
refused: usize,
first_setup: FirstSetup,
}

impl Guard {
Expand All @@ -231,6 +259,7 @@
owned: KVec::new(),
existed: existing,
refused: 0,
first_setup: FirstSetup::Closed,
};
// A list created this boot (or by a crashed one) is only safe to adopt
// into once its name is durable: fsync() of the file alone does not
Expand Down Expand Up @@ -309,6 +338,27 @@
self.file.is_some()
}

/// The writes and deletes refused since probe.
pub(crate) fn refusals(&self) -> usize {
self.refused
}

/// Opens the first-setup window for one keybag creation, only on a fresh
/// install: the list loaded and holding no entry, torn ones included.
/// Returns whether it opened.
pub(crate) fn open_first_setup(&mut self) -> bool {
if self.file.is_none() || self.end != 0 {
Comment thread
maralcbr marked this conversation as resolved.
Resolved
return false;
}
self.first_setup = FirstSetup::Open;
true
}

/// Closes the first-setup window, used or not.
pub(crate) fn close_first_setup(&mut self) {
self.first_setup = FirstSetup::Closed;
}

fn adopt(&mut self, key: &Key) -> Result<()> {
let file = self.file.as_ref().ok_or(ENOENT)?;
// The file's length, not the number of distinct keys: a torn or
Expand Down Expand Up @@ -347,6 +397,14 @@
e
)
}),
Verdict::Refuse if first_setup_allows(self.level, self.first_setup, key, write) => {
self.first_setup = FirstSetup::Used;
pr_info!(
"xART guard: allowed the enclave to commit an existing kind-{} locker during this install's first keybag creation\n",
key.kind
);
Ok(())
}
Verdict::Refuse => {
crate::diag::xart_guard_refused();
self.refused += 1;
Expand Down Expand Up @@ -567,6 +625,61 @@
Ok(())
}

#[test]
fn first_setup_allows_only_a_kind_4_rewrite_at_level_1() {
let open = FirstSetup::Open;
assert!(first_setup_allows(1, open, &session(1), true));
assert!(!first_setup_allows(1, open, &session(1), false));
assert!(!first_setup_allows(1, open, &Key::new(3, [1; 16]), true));
assert!(!first_setup_allows(2, open, &session(1), true));
assert!(!first_setup_allows(1, FirstSetup::Closed, &session(1), true));
assert!(!first_setup_allows(1, FirstSetup::Used, &session(1), true));
}

#[test]
fn a_fresh_install_commits_one_locker_during_its_first_create() -> Result<()> {
let bytes = RefCell::new(KVec::new());
let pending = session(1);
let other = session(2);
let user = Key::new(3, [3; 16]);
let mut guard = Guard::with_file(1, keys(&[pending, other, user])?, Ok(mem(&bytes)));
// Outside the window the locker is protected as before.
assert_eq!(guard.check(&pending, true), Err(EPERM));
assert!(guard.open_first_setup());
// A delete, or a USER-xART locker, does not use the window.
assert_eq!(guard.check(&other, false), Err(EPERM));
assert_eq!(guard.check(&user, true), Err(EPERM));
assert_eq!(guard.check(&pending, true), Ok(()));
// One rewrite only, and the locker does not become Linux's.
assert_eq!(guard.check(&other, true), Err(EPERM));
assert_eq!(bytes.borrow().len(), 0);
guard.close_first_setup();
assert_eq!(guard.check(&pending, true), Err(EPERM));
assert_eq!(guard.refusals(), 5);
Ok(())
}

#[test]
fn first_setup_needs_an_empty_loaded_list() -> Result<()> {
let pending = session(1);
let own = session(2);
let bytes = RefCell::new(list_of(&[own])?);
let mut guard = Guard::with_file(1, keys(&[pending, own])?, Ok(mem(&bytes)));
assert!(!guard.open_first_setup());
assert_eq!(guard.check(&pending, true), Err(EPERM));

// A torn entry is still an earlier setup.
let torn = RefCell::new(KVec::new());
torn.borrow_mut().extend_from_slice(&encode(&own)[..10], GFP_KERNEL)?;
let mut guard = Guard::with_file(1, keys(&[pending])?, Ok(mem(&torn)));
assert!(!guard.open_first_setup());

let mut guard = Guard::<MemFile<'_>>::with_file(1, keys(&[pending])?, Err(ENOENT));
assert!(!guard.open_first_setup());
assert_eq!(guard.check(&pending, true), Err(EPERM));
Ok(())
}

#[test]
fn a_guard_allows_a_listed_record() -> Result<()> {
let own = session(2);
Expand Down
19 changes: 19 additions & 0 deletions drivers/soc/apple/xart_store.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
// Copyright 2026 Dj

Expand Down Expand Up @@ -442,6 +442,25 @@
.map(|guard| (guard.effective_level(), guard.list_loaded()))
}

/// The guard's refusals since probe; 0 without a guard.
pub(crate) fn guard_refusals(&self) -> usize {
self.guard.as_ref().map_or(0, |guard| guard.refusals())
}

/// Opens the guard's first-setup window (see [`crate::xart_guard`]).
/// `false` without a guard or once this install has listed a record.
pub(crate) fn open_first_setup(&mut self) -> bool {
self.guard
.as_mut()
.is_some_and(|guard| guard.open_first_setup())
}

pub(crate) fn close_first_setup(&mut self) {
if let Some(guard) = self.guard.as_mut() {
guard.close_first_setup();
}
}

/// Byte offset of the APFS-resolved gigalocker extent in the container.
pub(crate) fn base(&self) -> u64 {
self.base
Expand Down
48 changes: 47 additions & 1 deletion tools/aurora-sep/XART-SAFETY.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ another finger into an existing keybag does not create one.
| --- | --- | --- |
| M1 (T8103), M1 Pro/Max (T6000/T6001) | cold boot | Refused: it rewrites a pre-Linux session record (seen on T6000; T8103 and T6001 untested). Needs `xart_guard=0`. |
| M2 (T8112) | warm attach | No xART writes (J413), so the guard does not affect it. |
| M2 Pro/Max (T6020/T6021), M3 (T8122) | warm attach | Untested. |
| M2 Max (T6021, J416c) | warm attach | On a fresh install, commits one pending SEP-xART locker; allowed once (see "Fresh installs"). Otherwise no xART writes. |
| M2 Pro (T6020), M3 (T8122) | warm attach | Untested. |
| MacBook Neo (T8140) | warm attach | Guard off by default. |

Whether the enclave writes xART records while enrolling a finger has not
Expand All @@ -97,6 +98,9 @@ the kernel log names the refused kind. What to do depends on the case:
until it has been checked there.
- A warm-attach Mac set up under the guard whose verify is refused: report
the log line. `xart_guard=0` restores use at the cost of the protection.
- A keybag creation with a refused write: the enclave may stop answering
until a restart, and the driver marks the create retryable, so the next
boot tries again.
- A list that could not be read: every record that existed at boot is
protected and nothing can be adopted, so new records cannot be written
either.
Expand Down Expand Up @@ -188,6 +192,48 @@ unverified until macOS Touch ID unlock and keychain access have been checked
there. The other cold-boot SoCs (T8103, T6001, T6002) probably behave the
same way; that is untested.

## Fresh installs: the pending SEP-xART locker (2026-10-25)

Measured on an M2 Max (T6021, J416c, system firmware 26.6.2) installed with
the Omarchy Mac installer:

- The installer blesses the new stub from macOS. While it does, the
enclave saves SEP-xART root/locker/root triples, which leave one SEP-xART
locker (kind 4) larger than its usual 1137 bytes (2321 bytes right after
the bless on one run). By Linux's first boot, after macOS shut down and
recoveryOS finished the install, it held 2715 bytes on both runs.
- Every OS's enclave reads that locker at attach, alongside three others:
macOS's own xART log shows the same four, under its own OS identity.
- The next enclave to save its state commits the locker back to 1137 bytes.
macOS's enclave does that at boot. Under Linux the first save comes inside
the first `CREATE_KEYBAG`: a SEP-xART root write, then the locker rewrite.
The guard refused the rewrite, `CREATE_KEYBAG` got no reply, and the
enclave stopped answering until a restart.
- After one macOS boot, keybag creation under the guard made no xART writes.
Enrolment, match, restore after a restart and a match after it wrote only
the USER-xART root. macOS Touch ID unlock and the login keychain were
unaffected.
- Changing the startup disk between macOS and Linux in System Settings did
not leave the locker pending.
- With the window below, a fresh install (no macOS boot after it) created
its keybag on Linux's first boot: the enclave committed the locker to
1137 bytes, as macOS's does, and the list stayed empty. Enrolment, match,
`keyctl` seal and load, restore and match after a restart, macOS Touch ID
unlock and keychain, then restore, `keyctl` load and match back in Linux
all passed, with no refusal.

So during the first `CREATE_KEYBAG` of an install whose list is loaded and
empty, on a warm-attach Mac at level 1, the guard lets the enclave rewrite
one existing kind-4 locker (`FirstSetup` in `xart_guard.rs`). It does not
list the locker as Linux's, so no later rewrite passes. Deletes, kind-3
lockers and a second rewrite are refused as before, and cold-boot Macs get
no window: their create rewrites a pre-Linux record for a different reason
(above).

If a create still has a write refused, the enclave cannot have kept a
keybag, so the driver marks the attempt retryable rather than leaving an
intent record that refuses every later create with `EEXIST`.

## Earlier deployment policy

The iBoot APFS container and its XART `.gl` file are shared with macOS. The
Expand Down
Loading