Skip to content

soc: apple: sep: return -EAGAIN from trusted-key callbacks before ready - #3237

Merged
RyanTheTide merged 1 commit into
aurora-silicon:mainfrom
AxxzyWasTaken:pr/sep-trusted-eagain
Nov 29, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:mainfrom
AxxzyWasTaken:pr/sep-trusted-eagain

Conversation

@AxxzyWasTaken

@AxxzyWasTaken AxxzyWasTaken commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: Trusted-key callbacks (tk_get_random, tk_seal, tk_unseal) run inside trusted_instantiate(), which is called from __key_instantiate_and_link() while holding the global key_construction_mutex. Previously, those paths called SepData::wait_for_trusted_ready(), sleeping up to 90 s (EXCHANGE_TIMEOUT_MS) under that mutex and stalling every other key instantiation on the system (including systemd service startup) whenever an early trusted-key operation raced SEP bring-up. (PR #3168 implemented the diag/keystore kernfs_notify() half and auto-closed #2313; this completes the non-blocking trusted-key half.)

  • Add non-blocking SepData::check_trusted_ready() backed by trusted::ready_state() and switch tk_get_random(), tk_unseal(), refkey_seal_trusted(), and refkey_unseal_cached() to it:
    • Return -EAGAIN immediately (zero wait) while SEP bring-up is still running (phase != PHASE_READY) or while the enclave is transiently gated for system sleep (SleepGate::Draining / SleepGate::Told).
    • Return terminal errors (-ENODEV on driver shutdown or missing SKS endpoint; -EIO on failed SKS initialization or SleepGate::Lost) so callers do not spin indefinitely on -EAGAIN.
    • Check readiness at the start of tk_unseal() before inspecting the blob so readiness errors return -EAGAIN/-ENODEV/-EIO without creating a blob oracle, while preserving opaque -EBADMSG for post-readiness unseal rejections.
    • Skip the one-shot seal failure warning in tk_seal() for expected -EAGAIN retries.
  • Keep wait_for_trusted_ready() for callers outside key_construction_mutex (complete_attach sysfs completion and /dev/sep-key ioctls).
  • Publish PHASE_READY (Release/Acquire, under endpoints.lock()) inside finish_bringup() before diag::keystore_open(), diag::keystore_ended(), and diag::bringup_ended() transition diag/keystore out of "unknown" and emit kernfs_notify(), preventing a race where userspace waiting on diag/keystore immediately calls add_key() and gets a spurious -EAGAIN.
  • Add apple_sep_trusted KUnit tests in drivers/soc/apple/trusted.rs, add host tests in tools/aurora-sep/tests/test_trusted_readiness.py, update tools/aurora-sep/tests/test_refkey_seal_admission.py, and wire both into .github/workflows/sep-source.yml.

Relates to #2313.

Tested: Cross-compiled ARCH=arm64 LLVM=1 (defconfig + asahi.config) drivers/soc/apple/apple-sep.o with both CONFIG_APPLE_SEP_KUNIT_TEST=y (all 7 apple_sep_trusted KUnit wrappers verified via llvm-nm) and CONFIG_APPLE_SEP_KUNIT_TEST=n, plus security/keys/trusted-keys/tests/trusted_source_kunit.o (0 new warnings).

  • tools/aurora-sep/tests/test_trusted_readiness.py, test_refkey_seal_admission.py, and all 18 CI sep-source.yml Python unit tests pass under both python3 and python3 -O; tools/asahi/run-host-tests.py --only sep passes 23/23.
  • git diff --check clean; scripts/checkpatch.pl --strict --git HEAD~1..HEAD reports 0 errors / 0 code warnings.

Not yet: Live boot and early-probe hardware rows on Apple Silicon Macs (including aurora-sep-load and LUKS initrd keystore_only attach) and arm64 KUnit execution (covered by CI sep-source.yml).

Risk: two-way · all Macs with Apple SEP (apple-sep)

Trusted-key instantiation in security/keys/trusted-keys/trusted_core.c runs
inside __key_instantiate_and_link(), which holds the global
key_construction_mutex. Calling SepData::wait_for_trusted_ready() from
tk_get_random(), refkey_seal_trusted(), and refkey_unseal_cached() slept
for up to EXCHANGE_TIMEOUT_MS (90 s) while holding that mutex, stalling
every other key instantiation on the system (including systemd service
startup) whenever an early trusted-key operation raced SEP bring-up.

Replace those blocking waits in the trusted-key callbacks with a
non-blocking SepData::check_trusted_ready() check backed by a pure
trusted::ready_state() decision helper:

- Return -EAGAIN immediately (zero wait) while SEP bring-up is still in
  progress (phase != PHASE_READY) or while the enclave is transiently
  gated for system sleep (SleepGate::Draining / SleepGate::Told).
- Return terminal errors instead of spinning on -EAGAIN when the driver
  is shutting down (-ENODEV), when bring-up completed without an SKS
  endpoint (-ENODEV) or failed SKS initialisation (-EIO), or when the
  enclave failed to return from sleep (SleepGate::Lost -> -EIO).
- Check readiness at the start of tk_unseal() before inspecting the blob
  so not-ready and terminal readiness states return -EAGAIN/-ENODEV/-EIO
  without creating a blob oracle, while preserving opaque -EBADMSG for
  every post-readiness unseal rejection.
- Suppress the one-shot seal failure warning in tk_seal() for expected
  -EAGAIN retries so early retries do not spam or exhaust the warning.
- Keep wait_for_trusted_ready() for callers outside
  key_construction_mutex (sysfs complete_attach and /dev/sep-key ioctls).
- Publish PHASE_READY (with Release/Acquire ordering) inside
  finish_bringup() before diag::keystore_open(), diag::keystore_ended(),
  and diag::bringup_ended() transition diag/keystore out of "unknown"
  and emit kernfs_notify(), so userspace waiting on diag/keystore never
  races into a spurious -EAGAIN.

Add KUnit tests (apple_sep_trusted) and host tests
(tools/aurora-sep/tests/test_trusted_readiness.py, plus updated
test_refkey_seal_admission.py) covering readiness transitions, terminal
errors, sleep-gate handling, error masking, and call-site invariants.

Relates to: aurora-silicon#2313
Signed-off-by: AxxzyWasTaken <[email protected]>
@github-actions github-actions Bot added area:platform Area from CODEOWNERS (bot) area:release Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at fffee85c: not ready yet

Check Detail
CI at head ok 25 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok comment at fffee85c
Conflicts ok none
PR text ok Risk: two-way · all Macs with Apple SEP (apple-sep)

Areas: area:platform, area:release

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Nov 26, 2026
@sithglan

Copy link
Copy Markdown
Contributor

Hardware verification row on J813 (M5 MacBook Air 13-inch, T8142, apple_sep xart_guard=1 trusted_keys=1 filevault_api=1) at exact PR head fffee85c5489: PASS.

  • Kernel: 7.3.0-aurora-j813-pr3237+ built at exact head fffee85c5489 (booted via one-shot aura-try; note: btcentauri preloaded before rom_boot=3 in n1-up to avoid the pre-#3373 fn2 BAR0 SError on main).
  • Early-probe bring-up race (add_key("trusted", ...) polling every 15 ms across apple_sep probe and 1442 ms bring-up):
    • Before apple_sep registered "trusted": -ENODEV (0.003–0.012 ms).
    • Across attach=pending and attach=attached, keystore=unknown ([3.961s..5.404s]): 86/86 tk_unseal (load dummy hex) calls returned -EAGAIN (-11) immediately (max 2.823 ms, 0.064 ms initial) and 86/86 tk_get_random (new 32) calls returned -EAGAIN immediately (max 0.036 ms, 0.005 ms initial) — zero stall under key_construction_mutex, zero seal-failure warning spam, and zero blob oracle before readiness.
    • On the very first call after diag/keystore transitioned to open (ready in 1442 ms: 12 endpoints ..., key store open): zero spurious -EAGAIN race; tk_unseal passed readiness immediately and returned opaque -EBADMSG (-74, 48.502 ms).
  • Deep sleep/wake + post-sleep readiness (t8142_wake_s=8, echo mem > /sys/power/state):
    • SUSPEND_RC=0, boot object resumes 0 -> 1, apple_sep: the enclave is back from its sleep request (sleep=awake, sleeps=1, keystore=open, hwrng=registered).
    • Post-sleep tk_unseal on dummy blob returned opaque -EBADMSG (-74).
  • Health: 0 DART/IOMMU faults, 0 xART guard refusals, 0 failed systemd units.

@github-actions github-actions Bot removed the needs-hw-row No hardware row at the PR head (bot) label Nov 27, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at fffee85c

Blockers: none in code (note before merge: cancelled CI jobs apple-build, rust-build, and kunit must rerun green; second-family regression smoke is needed for blast:all; .github/workflows/sep-source.yml requires Owner approval).

Checked: no Apple material, no AI attribution, no chip/board/firmware gates, no cmdline hacks, human Signed-off-by, non-blocking trusted-key callbacks under key_construction_mutex, Release/Acquire publication ordering in finish_bringup() before diag::keystore_open()/bringup_ended()/keystore_ended(), all callers of run_bringup()/run_keystore_bringup() verified, opaque -EBADMSG preservation post-readiness with transient -EAGAIN passthrough, KUnit and host tests added, exact-head J813 hardware row PASS.

Evidence
  • Code inspection across all 8 touched files:
    • Verified every caller of run_bringup() and run_keystore_bringup() in drivers/soc/apple/ is inside sep.rs and flows directly into finish_bringup(keystore_open, keystore_only), so moving diag::keystore_open() into finish_bringup() after self.phase.store(PHASE_READY, Release) under self.endpoints.lock() cannot drop keystore-open notifications on any path.
    • tk_get_random(), tk_seal() (via refkey_seal_trusted()), and tk_unseal() (directly and via refkey_unseal_cached()) all call check_trusted_ready() and never sleep on wait_for_trusted_ready() under key_construction_mutex. complete_attach and /dev/sep-key ioctls (with_identity_bag) retain wait_for_trusted_ready().
    • tk_unseal() checks readiness before reading p->blob (avoiding a blob oracle) and unseal_failure_errno() preserves -EAGAIN while collapsing all other post-readiness failures to -EBADMSG.
    • seal_should_warn() prevents expected -EAGAIN retries from consuming the one-shot warning in tk_seal().
  • Hardware row at exact head fffee85c5489: J813 (sithglan, 86/86 early add_key("trusted", ...) calls returned -EAGAIN immediately, median 0.064 ms, no stall, succeeded once keystore opened).
  • CI at fffee85c: host-tests, source, pr-text, readiness PASS; apple-build, rust-build, and KUnit jobs were cancelled and must rerun green before merge.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Nov 29, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Row (J616s, M4 Pro) at exact head fffee85 in combined candidate cedaeff9ee2fe5396733351bb571d0a6049bd7fc on main aa76613: 3x keyctl trusted new 32 -> pipe -> load (SEP ref-key seal/unseal) + tampered-blob control -> PASS (3/3 seal+unseal, 3/3 tampered refused EBADMSG, diag trusted=ready, aurora-sep 22 pass/0 fail before and after 2 s2idle; 0 failed units, Enforcing, 0 WARN/BUG). Evidence: traces/kernel/20261129-closeout-j616s-combo/. Saved boot restored.

@RyanTheTide

Copy link
Copy Markdown
Contributor

code-owner ping @DjDeveloperr: closeout merge at fffee85c. The App approved at this head. Rows: J813 at head + J616s exact head (cedaeff9): trusted-key seal/unseal 3/3, tampered refused 3/3. Merging under the 6-point rule 1 h from now, once hosted CI at this head is green, unless you object.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging under the 6-point rule (Ryan 09:29, permanent), at fffee85c. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: J813-at-head-+-J616s-exact-head-(cedaeff9):-trusted-key-seal/unseal-3/. (5) There's no blocking review. (6) Dj was pinged here 63 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit 7b741d2 into aurora-silicon:main Nov 29, 2026
34 of 122 checks passed
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merged. Thanks @AxxzyWasTaken: trusted keys now return -EAGAIN before the SEP is ready, and we verified it on the J616s as well as your J813.

RyanTheTide added a commit that referenced this pull request Nov 29, 2026
#3237 made finish_bringup() take the key-store outcome and end the
keystore/bringup diagnostics after the PHASE_READY store, so trusted-key
callbacks see -EAGAIN until bring-up has finished on every exit. This
branch moved finish_bringup() behind ready::Bringup so ready::publish()
can recheck the attach-only endpoints after the store.

Keep both: Bringup::finish_bringup() and ready::publish() carry
keystore_open/keystore_only through to #3237's finish_bringup(), the
early-failure exits pass false/false as main does, and the summary line
follows publish() because it reads the key-store state set there. A new
apple_sep_ready case checks the outcome reaches finish_bringup().

Signed-off-by: Ryan Murray <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform Area from CODEOWNERS (bot) area:release Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apple_sep: trusted-key callbacks wait up to 90 s under key_construction_mutex, and diag/keystore cannot be waited on

3 participants