Skip to content

soc: apple: sep: one Touch ID match = one key operation; SEP reply-wait, SCRD and ioctl hardening - #1541

Merged
RyanTheTide merged 6 commits into
integration/aurorafrom
secchain/sep-one-op
Nov 11, 2026
Merged

RyanTheTide merged 6 commits into
integration/aurorafrom
secchain/sep-one-op

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

SEP driver hardening for /dev/sep-key and the shared SEP paths.

One Touch ID match runs exactly one key operation. The enclave lets a matched credential context authorise any number of operations until it is deleted (self-test 20/21 measured a second signature, and a signature followed by an unseal). The kernel now enforces the bound in its types:

  • KeyGrant holds the one operation of a touch-gated SIGN/UNSEAL. run() takes it by value, so once an operation has run, or has failed in a way that may have let the enclave act, nothing is left to run another. Only an outright enclave refusal (EKEYREJECTED) returns it, once, for the existing retry after the evaluation completes. A transport error no longer triggers a second attempt.
  • OwnedContext deletes the credential context on drop, so every way out of the key-mode verify and the key self-tests deletes it.
  • A signal to the caller ends a touch wait with EINTR. That includes the caller dying.
  • Self-tests 20 and 21 (second operation in an open matched context) are only built with the new CONFIG_APPLE_SEP_KEY_RESEARCH (default n). Without it they return EOPNOTSUPP.
  • read_request() copies every request straight into kernel memory and checks it before anything acts. SEAL/UNSEAL/self-test buffers are wiped on drop. A malformed record or sealed blob is refused before the caller is asked to touch the sensor, not after the match.

Shared SEP paths

  • Reply waits (SKS, SCRD, SBIO, control) used interruptible sleeps that, with a signal pending, returned at once without consuming time. They spun until the reply came, or forever. They are uninterruptible now; they are bounded at 2-30 s.
  • SCRD exchanges are serialised. The endpoint has one buffer pair and matches replies by request type only, so concurrent callers could swap payloads or replies, including a new context's handle. SKS sequence numbers are now allocated atomically.
  • Trusted-key unseal, machine ref-key setup and sep-bio attestation load the identity bag under key_bag_lock, like /dev/sep-key. Before, an unload on one path could end the handle another was using.
  • refkey_seal wipes the shared secret, KDF output and plaintext on every error path and never reallocates them.

Tests

  • KUnit apple_sep_key_one_op, apple_sep_key_fuzz, and the C suite apple_sep_ioctl. The C suite drives every /dev/sep-key request, the /dev/sep-bio session ioctls and REARM from real user memory (kunit_vm_mmap): unaligned, truncated, faulting, out-of-range and random input.
  • tools/testing/selftests/drivers/apple/sep-ioctl-fuzz.py: root, no finger. It covers malformed, faulting and concurrent requests, cancel, SIGINT/SIGKILL during a touch wait, and checks the kernel log stays clean.

The SKS, SCRD, SBIO and control reply waits slept with
wait_interruptible_timeout() and, on a signal, carried on with the time
that was left. With a signal pending an interruptible sleep returns at
once without consuming any of that time, so the loop spun on the CPU
until the reply came, and forever if it never did.

Since /dev/sep-key runs its enclave requests in the caller's own thread,
a Ctrl-C or a kill during a SIGN or UNSEAL reached these loops: the
caller could neither be stopped nor complete while the SEP was slow.
The same held for /dev/hwrng readers and the sep-bio delete path.

Reply waits are bounded (2-30 s) and must not abandon a request the SEP
is still processing, so sleep uninterruptibly for them. Waits that end
on a signal by design (trusted-key readiness, the RNG hold, worker exit)
are unchanged.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The SCRD endpoint has one pair of shared buffers, and scrd_transact()
tells replies apart only by request type. It took no lock, so two
callers in flight together (a touch-gated /dev/sep-key call, the
password re-arm on /dev/sep-bio-rearm, a key self-test) could overwrite
each other's payload or take each other's reply, including the handle
of a newly created credential context. Serialise SCRD exchanges as SKS
exchanges already are.

sks_next_seq() also read and stored the counter in two steps; callers
build requests outside the exchange lock, so two requests could carry
the same sequence and defeat the late-reply bookkeeping of an abandoned
one. Use one fetch_add.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
/dev/sep-key calls run concurrently and serialise their identity-bag
load, operation and unload on key_bag_lock (with_identity_bag). The
trusted-key unseal, the machine ref-key setup and the sep-bio
attestation loaded and unloaded the same bag without it, so one of them
could unload the bag while a /dev/sep-key signature or unseal was using
its handle. Take the lock on those paths too.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The ECIES helpers wiped the shared secret and the KDF output only on
their success paths. A failed allocation or a failed AES-GCM call left
the ECDH shared X, KDF blocks and digests, or the plaintext being
sealed, in freed memory, and growing vectors left copies behind when
they reallocated. Size the buffers up front and wipe them on every
return.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The enclave does not bound what a matched credential context authorises:
one match lets any number of key operations through until the context
is deleted (self-test 20 measured a second signature; self-test 21 a
signature and an unseal). One touch per operation is therefore kernel
discipline, and this makes it structural:

 - KeyGrant: the one operation a touch-gated SIGN or UNSEAL may run. It
   is made once per call and KeyGrant::run() takes it by value; any
   result that may have let the enclave act consumes it. Only an
   outright refusal (EKEYREJECTED: nothing performed) returns it, once,
   for the retry the verify makes after completing the evaluation. A
   transport error no longer leads to a second attempt.
 - OwnedContext: the credential context a /dev/sep-key verify opens is
   deleted when it drops, on every path out of run_verify(); the key
   self-tests use it too, so an early return no longer leaks a context
   (self-test 10 could leak one holding a user password credential).
 - A signal to the caller, or its death, ends a touch wait with EINTR at
   once (the capture checks it); before, the wait ran on for up to a
   minute and could still sign for a caller that was gone.
 - Self-test modes 20 and 21, which run a second operation in an open
   matched context, are built only with the new research option
   CONFIG_APPLE_SEP_KEY_RESEARCH (default n) and return EOPNOTSUPP
   otherwise; production kernels contain no code that can do it.

Requests are now copied in and checked by read_request() before
anything acts: straight into kernel memory (no stack copy of a secret),
with SEAL/UNSEAL/self-test buffers wiped on drop, and a malformed record
or sealed blob is refused before a touch is asked for instead of after
the match. The REARM password is read the same way.

KUnit: apple_sep_key_one_op (one success leaves nothing to run, one
retry after a refusal only, other failures consume the grant, the
context is deleted exactly once on each exit path, research modes
absent), apple_sep_key_fuzz (lengths at and past their bounds, every
flag value, mutated and random records and requests), and the C suite
apple_sep_ioctl, which drives every /dev/sep-key request, the
/dev/sep-bio session ioctls and REARM from real user memory
(kunit_vm_mmap): unaligned, truncated, faulting and random buffers.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
sep-ioctl-fuzz.py runs on a Mac as root and needs no finger. It sends
malformed, truncated, unaligned and faulting requests, random bytes and
unknown commands to /dev/sep-key, hammers it from eight threads, cancels
and signals touch-gated calls, and sends /dev/sep-bio only read-only
calls and starts that are always refused (never a valid enrol, verify or
delete). It passes when every refusal is a clean errno, nothing hangs,
and the kernel log gains no oops, warning, KASAN or kmemleak report.

Assisted-by: LLM
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant