Skip to content
Merged
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
29 changes: 29 additions & 0 deletions Documentation/ABI/testing/sysfs-driver-apple-sep
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,32 @@ Description:
power domains stay on across sleep, so every other attribute
keeps its value over a suspend; a reader checking that the
enclave still answers after a resume reads the hwrng.

What: /sys/bus/platform/devices/<unit>.sep/diag/trusted
Date: November 2026
KernelVersion: 7.3
Contact: Erik Bourget <erik@ebourget.net>
Description:
(RO) What the next trusted-key seal or unseal can expect of
the enclave, from the latest one this bind sent.

============== ==============================================
unknown No trusted-key unseal has worked and the
enclave has refused no identity unlock yet.
ready The latest trusted-key unseal worked. A seal
unseals its new blob before returning it, so
a working seal also reads "ready".
refused <n> The enclave answered the identity key-bag
unlock, which every seal and unseal on the
13.5 key store needs first, with status <n>
(a signed decimal). Trusted keys fail until
an unlock works.
============== ==============================================

A refusal is the enclave's answer for this key bag, not a
lost reply, and a test or loader stops on it rather than
asking again. An xART guard refusal (xart_guard_refusals)
can cause one: check that first. A later identity unlock that
works clears a stale refusal back to "unknown", and a later
unseal that works changes the value to "ready". The value
resets to "unknown" when the driver binds.
63 changes: 63 additions & 0 deletions drivers/soc/apple/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ extern "C" {
fn sep_diag_set_restore(state: c_int);
fn sep_diag_set_xart_guard(level: c_uint, list_loaded: bool, matching: c_int);
fn sep_diag_xart_guard_refused(matching: c_int);
fn sep_diag_set_trusted(state: c_int, status: c_int);
#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)]
fn sep_diag_set_trusted_atomic(
word: *mut kernel::bindings::atomic_t,
state: c_int,
status: c_int,
);
fn sep_diag_clear_trusted_refusal();
#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)]
fn sep_diag_clear_trusted_refusal_atomic(word: *mut kernel::bindings::atomic_t);
fn sep_diag_bringup_ended();
fn sep_diag_set_endpoint(id: u8, name: u32);
fn sep_diag_set_os_identity(source: c_int);
Expand Down Expand Up @@ -287,6 +297,59 @@ pub(crate) fn xart_guard_refused() {
unsafe { sep_diag_xart_guard_refused(matching() as c_int) };
}

/// What the next trusted-key seal or unseal can expect, as
/// `SEP_DIAG_TRUSTED_*` in `shim.h` spells it.
#[derive(Clone, Copy)]
pub(crate) enum Trusted {
/// A trusted-key unseal, on its own or as a seal's round trip, worked.
Ready,
/// The enclave answered the identity key-bag unlock that every seal and
/// unseal needs with this status.
Refused(i8),
}

impl Trusted {
fn raw(self) -> (c_int, c_int) {
match self {
Trusted::Ready => (1, 0),
Trusted::Refused(status) => (2, c_int::from(status)),
}
}
}

/// The latest trusted-key outcome; it replaces the one before.
pub(crate) fn trusted(state: Trusted) {
let (state, status) = state.raw();
// SAFETY: no preconditions; stores one word.
unsafe { sep_diag_set_trusted(state, status) };
}

/// Clears a stale refusal after a validated identity unlock, leaving `Ready`
/// tied to a working trusted-key unseal.
pub(crate) fn clear_trusted_refusal() {
// SAFETY: no preconditions; updates one atomic word.
unsafe { sep_diag_clear_trusted_refusal() };
}

#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)]
pub(crate) fn set_trusted_for_test(word: &Atomic<i32>, state: Trusted) {
kernel::static_assert!(size_of::<Atomic<i32>>() == size_of::<kernel::bindings::atomic_t>());
kernel::static_assert!(align_of::<Atomic<i32>>() == align_of::<kernel::bindings::atomic_t>());
let (state, status) = state.raw();
// SAFETY: `Atomic<i32>` matches `atomic_t` in size and alignment (asserted
// above) and `word` is valid for atomic writes.
unsafe { sep_diag_set_trusted_atomic(word.as_ptr().cast(), state, status) };
}

#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)]
pub(crate) fn clear_trusted_refusal_for_test(word: &Atomic<i32>) {
kernel::static_assert!(size_of::<Atomic<i32>>() == size_of::<kernel::bindings::atomic_t>());
kernel::static_assert!(align_of::<Atomic<i32>>() == align_of::<kernel::bindings::atomic_t>());
// SAFETY: `Atomic<i32>` matches `atomic_t` in size and alignment (asserted
// above) and `word` is valid for atomic read-modify-write.
unsafe { sep_diag_clear_trusted_refusal_atomic(word.as_ptr().cast()) };
}

/// The boot-time bring-up is over, however far it got.
pub(crate) fn bringup_ended() {
// SAFETY: no preconditions; updates two words.
Expand Down
56 changes: 56 additions & 0 deletions drivers/soc/apple/diag_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ static struct {
atomic_t unhandled;
atomic_t sleeps;
atomic_t sleeping;
/* SEP_DIAG_TRUSTED_*, with the refusal status above it. */
atomic_t trusted;

struct kernfs_node *keystore_kn;
struct kernfs_node *touchid_kn;
Expand Down Expand Up @@ -717,6 +719,23 @@ static ssize_t sleep_show(struct device *dev, struct device_attribute *attr,
}
static DEVICE_ATTR_RO(sleep);

static ssize_t trusted_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
int word = atomic_read(&sep_diag.trusted);

switch (word & 0xff) {
case SEP_DIAG_TRUSTED_READY:
return sysfs_emit(buf, "ready\n");
case SEP_DIAG_TRUSTED_REFUSED:
/* One word, so the status is the one this refusal stored. */
return sysfs_emit(buf, "refused %d\n", (s8)(word >> 8));
default:
return sysfs_emit(buf, "unknown\n");
}
}
static DEVICE_ATTR_RO(trusted);

static struct attribute *sep_diag_attrs[] = {
&dev_attr_abi.attr,
&dev_attr_profile.attr,
Expand Down Expand Up @@ -753,6 +772,7 @@ static struct attribute *sep_diag_attrs[] = {
&dev_attr_unhandled_messages.attr,
&dev_attr_sleeps.attr,
&dev_attr_sleep.attr,
&dev_attr_trusted.attr,
NULL
};

Expand All @@ -779,6 +799,8 @@ int sep_diag_register(struct device *dev, const char *profile,
sep_diag.sepos13 = sepos13;
sep_diag.xart = xart;
sep_diag.probe_ns = ktime_get_boottime_ns();
/* A refusal belongs to the bind that saw it. */
atomic_set(&sep_diag.trusted, SEP_DIAG_TRUSTED_UNKNOWN);

ret = devm_device_add_group(dev, &sep_diag_group);
if (ret)
Expand Down Expand Up @@ -1017,6 +1039,40 @@ void sep_diag_set_xart_guard(unsigned int level, bool list_loaded,
atomic_set(&sep_diag.matching, matching);
}

void sep_diag_set_trusted_atomic(atomic_t *word, int state, int status)
{
atomic_set(word, (state & 0xff) | ((u8)status << 8));
}

/*
* The latest answer wins: a success after a refusal (or the reverse) is what
* the next seal or unseal can expect.
*/
void sep_diag_set_trusted(int state, int status)
{
sep_diag_set_trusted_atomic(&sep_diag.trusted, state, status);
}

/*
* A validated identity unlock (for example through /dev/sep-key) clears any
* stale refusal so a loader re-run probes again, while leaving "ready" tied
* to a working trusted-key unseal.
*/
void sep_diag_clear_trusted_refusal_atomic(atomic_t *word)
{
int old = atomic_read(word);

while ((old & 0xff) == SEP_DIAG_TRUSTED_REFUSED) {
if (atomic_try_cmpxchg(word, &old, SEP_DIAG_TRUSTED_UNKNOWN))
break;
}
}

void sep_diag_clear_trusted_refusal(void)
{
sep_diag_clear_trusted_refusal_atomic(&sep_diag.trusted);
}

/* With matching as the refusal leaves it. */
void sep_diag_xart_guard_refused(int matching)
{
Expand Down
69 changes: 68 additions & 1 deletion drivers/soc/apple/refkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ use kernel::sync::atomic::Relaxed;

pub(crate) const REFKEY_ENVELOPE_VERSION: u32 = 2;

/// Validates a `DEVICE_STATE_TRANSITION` response body and clears any stale
/// refusal via `clear_refusal` so a subsequent loader run probes again. `Ready`
/// stays tied to a successful trusted-key unseal.
fn record_identity_unlock_with(body: &[u8], clear_refusal: impl FnOnce()) -> bool {
if body.len() == 20 && body[..4] == [0; 4] {
clear_refusal();
true
} else {
false
}
}

fn record_identity_unlock(body: &[u8]) -> bool {
record_identity_unlock_with(body, crate::diag::clear_trusted_refusal)
}

impl SepData {
/// Keep only decoded bytes across switch-root, never an open initrd file.
fn with_identity_keybag<T>(
Expand Down Expand Up @@ -102,12 +118,15 @@ impl SepData {
"sks: ref-key identity unlock refused (status {})\n",
out.reply.status
);
// An answer, not a lost reply: the enclave decided on this bag.
// diag/trusted lets readers stop on it instead of asking again.
crate::diag::trusted(crate::diag::Trusted::Refused(out.reply.status));
return false;
}
let Some(body) = self.sks_report_response(c"DEVICE_STATE_TRANSITION", &out) else {
return false;
};
body.len() == 20 && body[..4] == [0; 4]
record_identity_unlock(body)
}

fn sks_create_refkey(
Expand Down Expand Up @@ -342,6 +361,8 @@ impl SepData {
drop(crate::Secret(recovered));
return Err(EIO); // trusted.rs maps every unseal rejection to EBADMSG.
}
// A seal's round trip ends here too.
crate::diag::trusted(crate::diag::Trusted::Ready);
Ok(recovered)
}

Expand Down Expand Up @@ -805,3 +826,49 @@ impl SepData {
}
}
}

#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)]
#[::kernel::macros::kunit_tests(apple_sep_refkey_trusted)]
mod tests {
use super::*;
use kernel::sync::atomic::Atomic;

#[test]
fn validated_unlock_clears_refusal_without_setting_ready() {
let state = Atomic::new(0i32);
crate::diag::set_trusted_for_test(&state, crate::diag::Trusted::Refused(-8));
assert_eq!(state.load(Relaxed) & 0xff, 2);
assert_eq!((state.load(Relaxed) >> 8) as i8, -8);

assert!(!record_identity_unlock_with(&[0; 19], || {
crate::diag::clear_trusted_refusal_for_test(&state)
}));
assert_eq!(state.load(Relaxed) & 0xff, 2);
assert_eq!((state.load(Relaxed) >> 8) as i8, -8);

let mut bad_status = [0u8; 20];
bad_status[0] = 1;
assert!(!record_identity_unlock_with(&bad_status, || {
crate::diag::clear_trusted_refusal_for_test(&state)
}));
assert_eq!(state.load(Relaxed) & 0xff, 2);

assert!(record_identity_unlock_with(&[0; 20], || {
crate::diag::clear_trusted_refusal_for_test(&state)
}));
assert_eq!(state.load(Relaxed), 0);

crate::diag::set_trusted_for_test(&state, crate::diag::Trusted::Ready);
assert_eq!(state.load(Relaxed), 1);
}

#[test]
fn validated_unlock_preserves_ready() {
let state = Atomic::new(0i32);
crate::diag::set_trusted_for_test(&state, crate::diag::Trusted::Ready);
assert!(record_identity_unlock_with(&[0; 20], || {
crate::diag::clear_trusted_refusal_for_test(&state)
}));
assert_eq!(state.load(Relaxed), 1);
}
}
12 changes: 12 additions & 0 deletions drivers/soc/apple/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ int sep_sensor_irq_wait(unsigned int timeout_ms);
#define SEP_DIAG_PNON_FAILED 3
#define SEP_DIAG_PNON_DIGEST_LEN 48

/*
* Mirrored by diag.rs (Trusted). The word keeps the state in its low byte and,
* for SEP_DIAG_TRUSTED_REFUSED, the enclave's status in the next one.
*/
#define SEP_DIAG_TRUSTED_UNKNOWN 0
#define SEP_DIAG_TRUSTED_READY 1
#define SEP_DIAG_TRUSTED_REFUSED 2

/* Mirrored by diag.rs (Offer): endpoints checked at bring-up, sent nothing. */
#define SEP_DIAG_OFFER_PENDING 0
#define SEP_DIAG_OFFER_IDLE 1
Expand Down Expand Up @@ -238,6 +246,10 @@ void sep_diag_set_restore(int state);
void sep_diag_set_xart_guard(unsigned int level, bool list_loaded,
int matching);
void sep_diag_xart_guard_refused(int matching);
void sep_diag_set_trusted(int state, int status);
void sep_diag_set_trusted_atomic(atomic_t *word, int state, int status);
void sep_diag_clear_trusted_refusal(void);
void sep_diag_clear_trusted_refusal_atomic(atomic_t *word);
void sep_diag_bringup_ended(void);
void sep_diag_set_endpoint(u8 id, u32 name);
void sep_diag_set_os_identity(int source);
Expand Down
5 changes: 5 additions & 0 deletions tools/aurora-sep/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ for example with `systemctl edit aurora-sep.service`:
Environment=AURORA_SEP_READY_TIMEOUT=180
```

The loader does not wait when the enclave refuses the identity key-bag unlock
that trusted keys need: the driver's `diag/trusted` then reads
`refused <status>`, and the loader logs it and exits at once, because asking
again gets the same answer.

SEP writes are always on: xART writes and identity-keybag provisioning have
no switch. The driver's `xart_writes` and `provision_keybag` parameters were
removed (aurora-silicon/linux#2591), and a stale value on the kernel command
Expand Down
41 changes: 39 additions & 2 deletions tools/aurora-sep/load-driver
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ guard_refused() {
[ -n "$refusals" ] && [ "$refusals" != 0 ]
}

guard_exit() {
log "the xART guard refused a record the enclave needs to unlock Linux's keybag. After a boot policy change (a new Stage 1 or boot object) the driver lets that one update through by itself; this one it could not. Touch ID and its fingerprints are intact. To allow it on the next boot, run aurora-sep-policy-change and reboot; see XART-SAFETY.md"
exit 1
}

# The driver's diag/trusted reads "refused <status>" once the enclave has
# answered the identity key-bag unlock that every seal and unseal needs with a
# refusal. That is the enclave's decision on this bag, not a slow bring-up:
# asking again at 10 Hz until the deadline only repeats it (about 1200 refused
# seals and kernel warnings per run). Kernels without the attribute read empty
# here and keep the old behaviour.
trusted_refused() {
[ -n "$diag" ] || return 1
trusted=$(cat "$diag/trusted" 2>/dev/null || true)
case "$trusted" in
refused*) return 0 ;;
esac
return 1
}

refused_exit() {
log "the enclave refused to unlock the identity key bag that trusted keys need (diag/trusted: $trusted). Asking again gets the same answer, so this run stops here; trusted keys are unavailable this boot. The key bag and ref-key are left as they are: do not replace them to get past this"
exit 1
}

start=$(uptime_s)
deadline=$((start + ready_timeout))

Expand All @@ -103,6 +128,16 @@ for keystore in /sys/bus/platform/drivers/apple_sep/*/diag/keystore; do
done
done

# A run started again this boot (fprintd's dependency starts this unit on
# every activation) needs no probe to learn the answer. When the xART guard
# refused too, its message names the way out, so it comes first, as below.
if trusted_refused; then
if guard_refused; then
guard_exit
fi
refused_exit
fi

verified=false
while :; do
key_id=$(printf 'new 32' | keyctl padd trusted aurora-sep-check @s 2>/dev/null || true)
Expand All @@ -119,8 +154,10 @@ while :; do
fi
[ "$verified" = true ] && break
if guard_refused; then
log "the xART guard refused a record the enclave needs to unlock Linux's keybag. After a boot policy change (a new Stage 1 or boot object) the driver lets that one update through by itself; this one it could not. Touch ID and its fingerprints are intact. To allow it on the next boot, run aurora-sep-policy-change and reboot; see XART-SAFETY.md"
exit 1
guard_exit
fi
if trusted_refused; then
refused_exit
fi
[ "$(uptime_s)" -lt "$deadline" ] || break
sleep 0.1
Expand Down
Loading
Loading