Skip to content

soc: apple: sep: authorization contexts for user-bag keys - #2051

Draft
cameroncooper wants to merge 37 commits into
aurora-silicon:mainfrom
cameroncooper:review/sep-retained-authorization
Draft

cameroncooper wants to merge 37 commits into
aurora-silicon:mainfrom
cameroncooper:review/sep-retained-authorization

Conversation

@cameroncooper

@cameroncooper cameroncooper commented Nov 16, 2026 •

Copy link
Copy Markdown

Stacked on #2050 (and its stack, on today's integration/aurora). Review starts at "one owner for the sensor at a time"; 14 commits, each builds on its own.

The case

A password manager that keeps its items sealed to Secure Enclave keys is an obvious thing to build on this driver, and I have one running (Keychain, an iCloud Keychain-compatible password manager for Linux, on J293). With /dev/sep-key as it is, every reveal or autofill needs a fresh Touch ID match or password: one match, one operation (#1541). That leaves such an app two choices: prompt for every item (ten items, ten touches), or unseal its vault key once and keep it in process memory, outside the enclave. macOS does neither: an authentication context can be reused for a short time (touchIDAuthenticationAllowableReuseDuration), which is how Keychain and Passwords reveal several items after one unlock.

So this adds authorization contexts: an explicit, scoped and expiring exception that a key's owner asks for, while the one-match-one-operation rule stays for every other path. The key never leaves the enclave, every operation still goes through it, and the kernel enforces the ceiling and the scope; the app picks the lifetime within them.

  • A context belongs to one Unix account's bag (SEP_KEY_CTX_NEW: root or that user) and is a file descriptor; closing it ends it.
  • It belongs to the effective uid that made it: every request on it, or naming it, from another euid gets EPERM, root included. Within that uid the descriptor is a capability, and apple-sep.h says so.
  • One authentication (SEP_KEY_CTX_AUTH: a Touch ID match in the user's own domain from soc: apple: sep: Touch ID in each user's own domain #2050; SEP_KEY_CTX_AUTH_PASSWORD: the bag's password, checked and counted by the enclave) lasts lifetime_s, 1 s to 12 hours (0 is refused with EINVAL).
  • After the lifetime, requests get EKEYEXPIRED. A context ended early (REVOKE, the bag locking, a suspend, a failed AUTH) gets EKEYREVOKED; after the driver stops, ENODEV.
  • SEP_KEY_CTX_BIND, once and only before authentication (EBUSY otherwise), can limit it to one key and the operations (sign, decrypt) it may run; anything else is then refused with EPERM.
  • Each SEP_KEY_CTX_SIGN/DECRYPT is still one enclave operation under that key's own access control.
  • Advertised as SEP_KEY_FEATURE_CONTEXTS (0x20; 0x10 is soc: apple: sep: Touch ID and password keys under a user's bag #1767's) wherever per_user_keybags() holds. There is no module parameter.

The ABI is in apple-sep.h (0x40–0x48 on the usual magic). NEW and QUERY have reserved words (24 bytes each), so NEW is now 0xc018b140. It is still marked DESIGN DRAFT, and the PR stays a draft until you're happy with the ABI.

Commits

  1. one owner for the sensor at a time: a capture owner (lease) so the BIO calls that reach the enclave without catacomb_writer (GET_INFO, ATTEST, LIST_ENCLAVE, PREPARE_ENROL) cannot run between a key touch's steps.
  2. make /dev/sep-key a per-open device: each open gets its own file and driver reference, so requests can belong to the file that made them. Every existing request is unchanged. Since unregistering no longer waits for running calls, remove counts them and waits for them before taking the hardware down.
  3. authorization contexts: NEW, Touch ID AUTH, SIGN, DECRYPT, QUERY, REVOKE, SEP_KEY_CANCEL_ID; request ids, single delivery of each result, revocation on bag lock and suspend.
  4. authenticate a context with the bag's password: AUTH_PASSWORD.
  5. bind a context to one key: BIND.
  6. contexts on the variant-5 store: offered on any key store with per-user bags; a variant-5 bag's lock (and logout) revokes its contexts first, as the 13.5 lock does.
  7. wait for the last touch before any enclave request: fixes a regression from (1), where the call right after a no-match (PREPARE_ENROL after fprintd's duplicate check) got EBUSY. It can be folded into (1) if you prefer.
  8. offer contexts wherever per-user bags are: the key_context_api parameter is gone.
  9. refuse a context with no lifetime: lifetime_s must be 1..=SEP_KEY_CTX_LIFETIME_MAX; the expiry is always set.
  10. a context answers only the uid that made it: the creator's euid is recorded at NEW and checked on every use.
  11. test that a context binds once, before its AUTH: KUnit for the existing rule.
  12. room to grow in the context NEW and QUERY requests: reserved[2] in both; NEW refuses nonzero, QUERY writes 0.
  13. a suspend ends contexts, not the files: found on hardware below. A suspend ended contexts through the driver-stop path, which also staled every open /dev/sep-key file, so a long-running client lost contexts after each resume until it reopened the device. A suspend now revokes with EKEYREVOKED and leaves files usable; driver removal is unchanged.
  14. say which error ends a context: the header text for the errors above, and the brief ENOSPC while a closed context's credential is still being deleted.

Tests

  • KUnit (in-module, arm64 QEMU): apple_sep_capture_owner, apple_sep_key_request_ids, apple_sep_key_result, apple_sep_key_context_state, apple_sep_key_context_scope, plus a_lifetime_is_bounded_and_never_zero, only_the_creator_uses_a_context, bind_comes_once_and_before_auth; all 342 SEP tests pass at the tip.
  • Host test tools/aurora-sep/keys/tests/check_context_lifecycle.py (6 cases).
  • Every commit builds (arm64 module), with no new warnings in the SEP files.

Hardware

J293 (M1, 13.5 store), 2026-11-23, this stack on today's integration/aurora, throwaway .userPresence keys A and B under one account's bag:

  • NEW for another user's bag, as that user: EPERM. lifetime_s 0: EINVAL; 60: ok.
  • NEW (root), BIND to A for sign, one Touch ID AUTH: ok. BIND again: EBUSY. Sign with A twice, no further touch: ok. Sign with B: EPERM.
  • The bag's own user, given root's context descriptor: SIGN and QUERY get EPERM; root's next sign with it: ok.
  • REVOKE, then sign: EKEYREVOKED. Sign naming the closed descriptor: EBADF.
  • 16 contexts open at once, the 17th ENOSPC, all 16 back after closing. 20 NEW+close in turn: all 20, with a brief ENOSPC wait when a NEW came right after a close.
  • An authorized context, then a bag lock (aurora-sep-userbag --lock-all): the next sign gets EKEYREVOKED, and a direct key sign is refused at once (EACCES).
  • An authorized context, then an s2idle suspend and resume: the next sign gets EKEYREVOKED, and a new NEW on the same /dev/sep-key file works (before (13): ENODEV for both).
  • Keychain, with contexts on by default: after a resume and the password, its Touch ID unlock works.

J716c (M5 Max, T6050, variant-5 store), 2026-11-23, this stack with #3001 and #2162 (the sensor's DT), throwaway keys A and B under the one account's bag:

  • NEW for another uid's bag, as the user: EPERM. lifetime_s 0: EINVAL; 60: ok.
  • NEW (root), BIND to A, one Touch ID AUTH: ok. BIND again: EBUSY. Sign with A twice, no further touch: ok. Sign with B: EPERM.
  • The user, given root's context: SIGN and QUERY get EPERM; root's next sign: ok.
  • REVOKE, then sign: EKEYREVOKED. The closed descriptor: EBADF. 20 NEW+close in turn: all 20.
  • An authorized context, then a bag lock: EKEYREVOKED, and NEW on the same file works.
  • An authorized context, then an s2idle suspend and resume: EKEYREVOKED, and NEW on the same file works.

That run found a variant-5 bug outside this PR: the password after a bag lock didn't reopen the bag, because the import designated a second copy and the store refused. It's fixed in #2049 ("an import of a bag still held is EBUSY"), and the rows after the bag lock ran with that fix.

Assisted-by: LLM (Claude:Opus 5.5)

@cameroncooper
cameroncooper force-pushed the review/sep-retained-authorization branch from 7d8fbe0 to c18d442 Compare November 16, 2026 18:45
@cameroncooper
cameroncooper force-pushed the review/sep-retained-authorization branch from c18d442 to f9afd69 Compare November 16, 2026 19:49
@cameroncooper cameroncooper changed the title RFC: soc: apple: sep: authorization contexts for user-bag keys soc: apple: sep: authorization contexts for user-bag keys Nov 16, 2026
@cameroncooper
cameroncooper force-pushed the review/sep-retained-authorization branch 2 times, most recently from e9afbc6 to fb42dbf Compare November 18, 2026 18:20
@RyanTheTide

Copy link
Copy Markdown

Thanks for the clear write-up. Three points:

  1. key_device.rs:27: contexts are enabled by the key_context_api module parameter. We avoid module-parameter opt-ins for features; please advertise SEP_KEY_FEATURE_CONTEXTS wherever per-user bags are supported, and keep it in draft until the ABI can be on by default.

  2. lifetime_s = 0 means no expiry (key_context.rs:245), while the description promises at most 12 hours. Please reject 0 or treat it as SEP_KEY_CTX_LIFETIME_MAX, so the kernel's ceiling always holds.

  3. The owner check happens only at SEP_KEY_CTX_NEW. After that, anyone holding the context fd (passed over a socket or inherited by fork) can sign and decrypt under it. Either document the fd as a capability in apple-sep.h, or record the creator's credentials at NEW and refuse other users at use. Please also confirm BIND is refused after AUTH.

Nit: sep_key_ctx_new and sep_key_ctx_query have no reserved field.

Row we would like on J293 and one newer Mac: NEW for another user gives EPERM; signing after REVOKE, a bag lock or a suspend gives EKEYEXPIRED; BIND to key A then signing with key B gives EPERM; closing the fd ends the context.

@RyanTheTide

Copy link
Copy Markdown

contexts_supported() in key_device.rs offers the SEP_KEY_CTX_* requests only when the key_context_api module parameter is set, and it defaults to 0 (the description calls the API a design draft). As written, the feature this PR adds is not there on a default boot. Either settle the UAPI and gate it only on sep.profile.per_user_keybags, or keep the context API out of this PR. Until then this should stay a draft.

@RyanTheTide

Copy link
Copy Markdown

@cameroncooper, a tidy-up pass over the open PRs here. These of yours need something:

If any of these are no longer needed, closing them helps; otherwise a merge or rebase onto integration/aurora is usually all they need. Thanks!

@cameroncooper

Copy link
Copy Markdown
Author

Thanks. All points are in, and the stack is rebased on today's integration/aurora (#2048 merged), so the conflict is gone.

  1. No module parameter. key_context_api is removed; SEP_KEY_FEATURE_CONTEXTS is offered wherever per_user_keybags() holds, so a default boot has the feature. It stays a draft until you're happy with the ABI.
  2. Lifetime. lifetime_s 0 is refused with EINVAL, as is anything over SEP_KEY_CTX_LIFETIME_MAX, so the expiry is always set and the 12-hour ceiling holds. KUnit: a_lifetime_is_bounded_and_never_zero.
  3. Owner. The creator's effective uid is recorded at NEW, and every request on the context or naming it from another euid gets EPERM, root included (root can make a context for any user but uses only its own). Within that uid the descriptor is a capability, and apple-sep.h says so. KUnit: only_the_creator_uses_a_context. BIND is refused (EBUSY) unless the context is new and nothing is bound yet; KUnit bind_comes_once_and_before_auth, and on hardware below.

Nit: NEW and QUERY have reserved[2] now (24 bytes each; NEW refuses nonzero, QUERY writes 0), so their numbers change (NEW 0xc018b140).

Rows on J293 (2026-11-23):

  • NEW for another user's bag: EPERM.
  • BIND to key A, then sign with key B: EPERM.
  • Signing after REVOKE: EKEYREVOKED. After a bag lock: EKEYREVOKED. After a suspend: EKEYREVOKED.
  • Signing through the closed descriptor: EBADF.

Two corrections came out of running them:

  • The error. The driver has always answered EKEYREVOKED for a context ended early and EKEYEXPIRED only once its lifetime has passed, but the header (and my description) said EKEYEXPIRED for both. The header now says which is which; Keychain already tells them apart.
  • Suspend. A suspend went through the driver-stop path, so a live context answered ENODEV and every /dev/sep-key file opened before the suspend refused all context requests for good, NEW included. A suspend now revokes with EKEYREVOKED and leaves the files usable; driver removal is unchanged. Verified on hardware: after a resume, the old context gets EKEYREVOKED and a NEW on the same file works.

Overlap with #2903: your #2903 and this PR's first commit ("one owner for the sensor at a time") both change how a /dev/sep-key touch wait and a /dev/sep-bio capture share the sensor, and they conflict in key.rs and sbio.rs. If #2903 lands first, I'll rebase this onto it and keep only what it doesn't already cover; if you'd rather have one of the two approaches, tell me which.

The newer Mac row: not yet run with this revision. The earlier J716c (M5 Max) results are in the description; I'll rerun the rows there.

Assisted-by: LLM (Claude:Opus 5.5)

RyanTheTide and others added 13 commits November 23, 2026 15:13
…he create variant

userbag_ioctl refused per-user key bags unless profile.key_store was the
variant-5 create encoding. That encoding describes how the identity key bag
is created, not whether the enclave can create a NEW per-user bag. T8112 (M2)
and T8122 (M3) are warm-register platforms whose SEP is handed over running
and shared with macOS; their identity bag uses the variant-5 form, so they
passed the Variant5 check, yet their enclave never replies to a user-bag
DESIGNATE_KEYBAG. A single unanswered DESIGNATE condemns the SKS endpoint for
the rest of the boot (sks_wedged), taking the whole key store down: the log
shows "DESIGNATE_KEYBAG got no reply" followed by every later SKS request
reporting "endpoint gone for this boot".

Per-user bag create and designate are proven only on T8140 (J700) and the
T6040 (J616s / M4 Pro) enclave family. Add an explicit PlatformProfile
capability, per_user_keybags, true only on those, and gate userbag_ioctl and
the per-user-bag ref-key / FDE paths on it instead of on the create-encoding
variant. The unsupported platforms now return EOPNOTSUPP before any enclave
request, so the SKS endpoint is never wedged. Touch ID and the identity bag
are unaffected: neither uses userbag_ioctl.

The sep KUnit suite passes (598 run, 596 passed, 2 skipped, 0 failed).

Assisted-by: LLM
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
Signed-off-by: Cameron Cooper <c@meron.cc>
per_user_keybags was a list of the enclaves where a user-bag create and
designate had been run: true on T8140 and the T6040 family, false
everywhere else. That makes every enclave nobody has tried refuse, and
says nothing about why the one measured failure fails.

The user-bag requests are part of the variant-5 key store; the 13.5 key
store has no such requests. The one measured defect is the T8112 (J415)
enclave, which returns a handle for CREATE_KEYBAG and then never answers
DESIGNATE_KEYBAG, condemning the SKS endpoint for the boot. Name that
defect, designate_unanswered, set it on T8112 only, and derive
per_user_keybags() from the key store variant and that flag.

T8122 no longer inherits the flag from the T8112 profile it extends:
the unanswered DESIGNATE was measured on J415, not on an M3. T6020 on
current firmware, T8122, T8132 and T8142 therefore offer per-user key
bags; none of them has run a user-bag DESIGNATE yet. T8103, T6000 and
T6020 on the 13.5 key store keep refusing, as does T8112.

Add a KUnit case covering per_user_keybags() for every profile.

Signed-off-by: Ryan <ryan@aurorasilicon.org>
Signed-off-by: Cameron Cooper <c@meron.cc>
On the 13.5 key store, sks_req_refkey always substituted the configured
Linux user's designation for the target handle. That is right for the
identity bag's source handles, but it also redirected an explicitly
designated user bag, so a key meant for a per-user bag was made and
used under the identity bag instead.

Factor the choice into refkey_target(): source handles keep the
identity mapping, and an explicit user designation keeps its own bag.
Variant 5 is unchanged.

The KUnit cases cover the identity fallback, the user-designation
boundaries and variant 5.

Signed-off-by: Cameron Cooper <c@meron.cc>
The per-user bag path hard-coded designate variant 1 and its flags
trailer, the variant-5 form. The 13.5 key store takes variant 0 with
the body ending after the empty blob, as the identity-bag path already
sends it.

Move the variant choice into sks::designate_variant(), shared by both
paths, and encode and decode the user-bag designation with it. The
variant-5 layout is unchanged; the user-bag ioctls stay gated by
per_user_keybags.

The KUnit golden test gains a 13.5 case for the shorter body and its
reply, and checks that each store rejects the other's form.

Signed-off-by: Cameron Cooper <c@meron.cc>
The user-bag ioctls refused every request on the 13.5 key store of
M1/M2-generation Macs. Add that store's backend for create, import,
export, unlock and lock:

- create uses the generic DeviceBag constructor (version 1);
- unlock first unlocks the loaded bag with the version-0
  CHANGE_LOCK_STATE, which has no flags trailer, then runs the
  designated-session device-state transition. Neither alone lets a key
  under the bag run;
- lock runs the transition with state Locked and the loaded-bag lock,
  then sends grace-timer fires, as the Mac's kernel does, until the
  store names the bag locked. The store locks only on a fire, about
  10 s after an immediate lock on M1. The bag lock is not held while
  it waits. A screen lock with a grace period is not captured on this
  store and is refused with EOPNOTSUPP;
- import of a bag the user's designation already names (a repeated
  login) keeps that bag rather than designating it twice.

Variant 5 keeps a designation after its temporary handle is unloaded.
The 13.5 loaded-bag unlock needs the handle, so the bag stays loaded
from import or create until logout, which locks and unloads it.

Password change, state query and FDE sealing stay EOPNOTSUPP on the
13.5 store.

KUnit golden layouts cover the 13.5 create and its reply, the
loaded-bag unlock and lock, the timer fire, and the loaded-bag registry.

Signed-off-by: Cameron Cooper <c@meron.cc>
The 13.5 key store refused CHANGE with EOPNOTSUPP, so a user's bag
could not follow a password change.

Its change-secret request is version 1 of the variant-5 one, as macOS
13 sends it: client, handle, the old and new secrets, two further blobs
and two words (a u8 flag and the generation option), without variant
5's last word. Send it with the two blobs empty, the flag 0xff and the
generation option 0, as the variant-5 path does, and accept the
version-1 reply.

KUnit adds the golden layout. Not yet run on hardware.

Signed-off-by: Cameron Cooper <c@meron.cc>
CREATE_USER refused on the 13.5 key store because user_key_create_in_bag
made only variant 5's EC key type 5. The 13.5 store makes EC type 4
ref-keys, as it already does under the identity bag.

Choose the key type from the key store, and advertise
SEP_KEY_FEATURE_PASSWORD wherever per_user_keybags allows user bags.
Sign and unseal under a user bag already address the designation's
special handle on either store.

On J293, keys created under a user's bag signed with an enclave-checked
password.

Signed-off-by: Cameron Cooper <c@meron.cc>
per_user_keybags() refuses the 13.5 key store because that store had no
user-bag requests. It has them now: create, designate, import/export,
unlock, lock and password change in its own shapes (userbag_sepos13),
and user-bag keys under them. Derive the capability from the measured
DESIGNATE defect alone, so T8112 stays the only enclave that refuses.

On J293 (MacBook Pro 13-inch, M1, cold-boot SEP) the userbag ioctls
created, exported, imported, unlocked and locked (logout and suspend)
a user's bag, and user-bag keys signed with an enclave-checked
password. T6000 and T6020 on the 13.5 store send the same requests and
now offer per-user bags too; no M1 Pro/Max or 13.5-store M2 Pro/Max has
run them yet.

Signed-off-by: Cameron Cooper <c@meron.cc>
…onfirm its lock

On the 13.5 store a lock is two requests the store accepts with the bag
still usable, then grace-timer fires until the store reports the bag
locked. When that failed (a refused request, an unreadable reply, or
no lock after 15 fires), userbag_lock_13() returned an error with the
bag still unlocked in the store, and the suspend and logout locks rely
on it.

On any such failure the driver now holds the bag locked: key creation,
signing and unsealing under it are refused with EACCES until an unlock
of it succeeds. The hold outlives an unload, so a bag the logout could
not lock stays refused if it is loaded again before its password.

Also say why the host sends the timer fires: the store locks the bag
only on a fire it is sent, and on macOS the kernel sends them; nothing
else on Linux does.

Signed-off-by: Cameron Cooper <c@meron.cc>
userbag_refused() printed up to 96 bytes of a refused reply at info
level. The 13.5 store's create and change-secret replies now reach it,
and those can carry key-bag material. Print the length and the
operation status instead.

Signed-off-by: Cameron Cooper <c@meron.cc>
The login helper imports the saved bag at every password login and
takes EBUSY as "already loaded: unlock it". A bag locked for a suspend
(or open for another session) stays in user_bags, so a password unlock
after the lock imports it again.

The 13.5 store got EBUSY from LoadedBags::insert() once the second
copy was loaded. Variant 5 never got there: it designated the second
copy, the store refused (the bag was still designated), and the import
answered EACCES. On J716c (M5 Max) the password at the lock screen
then left the bag locked, and the user's Touch ID keys refused until a
logout.

Answer EBUSY before loading a copy when the driver still holds the
user's bag, on both stores. The helper then unlocks it as before.

Signed-off-by: Cameron Cooper <c@meron.cc>
A touch-gated SIGN or UNSEAL on a key under a user's bag matched the
configured user's templates. The enclave refuses such a key unless the
match is against the bag user's own catacomb, as macOS matches the
logged-in user, so the call could never succeed on the 13.5 store.

Add a biometric subject: while a touch-gated call on a user-bag key
waits, the verify path addresses the bag's designation instead of the
configured user. That covers the unlock evaluation, the match, the
lockout and template-update persistence, and the check that a matched
identity is known. Each bag user's catacomb lives in its own file,
/var/lib/apple-sep-catacomb-bag-<uid>.bin, through a new catacomb Slot.
Fixed machine components keep their existing files.

On the 13.5 store, a password unlock now also loads that user's
catacomb and makes the Touch ID preparation: an SCRD context verified
with the bag's password and found TouchIdEnrollment-satisfied. Only its
handle is kept, never the password, and it is deleted when the bag locks
or logs out. A touch-gated call on that bag's key without one is refused
with EACCES before the sensor is armed, so Touch ID needs the password
once after every lock, as on macOS. Unlock never takes user_bags while
it prepares, keeping the catacomb_writer-before-user_bags order of a
touch.

On J293: after a password unlock, one touch signed a user-presence key
under the bag (verified), and the template update was saved to the
user's file. After a lock the same call was refused at once.

Signed-off-by: Cameron Cooper <c@meron.cc>
A v5 enrolment carries the resolved Unix account, but it always went
into the configured user's domain, whose templates cannot satisfy keys
under that account's own bag.

When the account's bag is loaded on the 13.5 store, run the enrolment
with that bag user as the biometric subject:
- its first enrolment selects a context scoped to that user alone,
  proved by zero identities, and never SYSTEM, which would also
  reselect the shared master; later ones activate the component the
  unlock restored;
- BEGIN_ENROL is authorized by the credential the user's own password
  unlock prepared, not the identity bag's material;
- the user's catacomb is saved first, then any other dirty user, the
  shared master and the lockout record. Success is reported only once
  nothing is save-pending;
- the identity is not filed in the shared index, which is reconciled
  against the configured user. Matches check it against the enclave's
  list for that user.

A bag that is loaded but locked refuses the enrolment, as macOS asks
for the password to add a finger. An account with no loaded bag keeps
the configured user, as before. A password unlock now prepares the
credential for a user with no templates yet, so their first enrolment
can run.

On J293, an ENROL_START for the account completed into the bag user's
domain (two identities), saved its catacomb file, and one touch of the
new finger then signed a user-presence key under the bag. The
configured user's enrolments were unchanged.

Signed-off-by: Cameron Cooper <c@meron.cc>
SEP_BIO_VERIFY_START carried no account, so every verify matched the
built-in Linux user, even for an account whose fingers live in its own
key bag's domain.

Add SEP_BIO_VERIFY_ACCOUNT. With it, the request's account_uid (the
word that was reserved) is the resolved Unix account:
- while that account's bag is loaded and unlocked by its password, the
  match runs in that user's own domain, and the configured user's
  after-boot and lockout gates do not apply;
- while it is loaded but locked, the verify fails without a capture
  with the lockout status, so pam_fprintd asks for the password;
- with no loaded bag, the built-in user matches as before.

The after-boot and lockout gates track the configured user: an
account's own domain is used only once that account's bag has been
unlocked by its password since boot, which the after-boot gate asks
for, and the lockout record kept here is the configured user's.

A verify that names an account is not short-circuited by the configured
user's empty index. Where per_user_keybags allows user bags, GET_INFO
advertises the flag in reserved[0] (SEP_BIO_INFO_VERIFY_ACCOUNT);
elsewhere the flag is refused with EINVAL. The interface version stays 5, so
existing libfprint keeps working, and older kernels refuse the flag with
EINVAL.

On J293, an account verify with a finger enrolled only in that user's
domain matched; after the bag locked it was refused at once with status
8; a plain verify with a configured finger still matched.

Signed-off-by: Cameron Cooper <c@meron.cc>
A lock screen runs its password and fingerprint prompts at once. While
fprintd waited for a finger, the user-bag ioctls refused everything but
STATE with EBUSY. A password unlock would also have waited on the
waiting verify's catacomb_writer to load the user's catacomb. So a
password at the lock screen created a bag that could not then be
exported or unlocked.

Create, import, export, unlock and lock do not touch the Touch ID
session, so they no longer require it to be idle; password change and
FDE sealing still do.

The user's catacomb is loaded once per load of their bag. Loading it
resets the enclave's biometry lockout for that user, which invalidates
a Touch ID credential made before it, so the first password after the
bag loads loads the catacomb before making the credential. If a
fingerprint wait holds catacomb_writer then, that capture is ended as a
cancel ends it (fprintd sees a failure it may retry), and the lock is
awaited for at most WORKER_EXIT_WAIT_MS. A key's touch is not ended.
Without this, the first touch at a lock screen after boot found the
credential stale and needed the password again.

Every accepted password renews the credential. One that is replaced or
ends is deleted in the enclave and its handle wiped.

Signed-off-by: Cameron Cooper <c@meron.cc>
A verify naming a Unix account matches in that account's own key bag,
but SEP_BIO_LIST returned only the shared Linux index. fprintd treats a
storage device's list as complete: after the first failed match it
deletes every stored print the list lacks, so a single miss deleted the
prints of fingers enrolled in an account's domain, which the next match
still answered and fprintd could no longer place.

List the identities of every account bag the enclave holds after the
index's, once each and without a label (fprintd compares identities).
The enclave is queried outside the session and index locks, as the
live-count query is. The list now names every loaded account's
identities to whoever opens /dev/sep-bio, which is root-only.

KUnit covers the ordering and deduplication. On J293, the lock screen
kept matching a finger enrolled in the user's domain after misses.

Signed-off-by: Cameron Cooper <c@meron.cc>
…tore

The variant-5 key store (T8140, T6040 and its inheritors) has per-user
bags too, but Touch ID followed them only on the 13.5 store. Variant 5
unloads a bag once it is designated, so the bag never entered
`user_bags`: a touch for one of its user-presence keys matched in the
configured owner domain, and the enclave refused that credential for
the key (status -8). Password authorization was unaffected.

Track a designated variant-5 bag under its designation at create and
import, prepare its Touch ID after each password unlock, end the
preparation at every lock (with or without a grace period) and drop the
entry at logout. Enrolments, verifies and key touches for the bag's
user then take that user's own domain, as on 13.5; the gates follow
the profile's per-user bags instead of the 13.5 store.

KUnit covers tracking a designation once. On J716c (T6050), a
fingerprint enrolled in the bag user's domain authorized the bag's
presence keys after a password unlock.

Signed-off-by: Cameron Cooper <c@meron.cc>
A print enrolled into a Unix account's own key bag is filed in that
user's domain and never in the shared index. SEP_BIO_DELETE looked only
at the index, so deleting one reported success ("nothing to delete")
while the enclave kept the template, and the finger went on unlocking
the bag's keys and the lock screen.

When the identity is not in the index but the enclave lists it under an
account's bag, delete it there: the bag must be unlocked by its password
(its catacomb loaded), as for an enrolment into it; 0x57 names that
user's identity as the enclave lists it, and that user's catacomb, any
other dirty one, the master and the lockout record are saved before
success is reported. A locked bag refuses with EACCES; a busy session
with EBUSY; writes off with EROFS, as for any deletion.

KUnit covers finding an account's identity by its own user.

Signed-off-by: Cameron Cooper <c@meron.cc>
…ave forgot it

A bag user's catacomb is saved in its own file, and the shared master
records its component. A kernel without per-user bags restores and
saves the master without that component, so after one boot of it the
loaded master lists nothing for the bag user while the file remains.
The bag's restore treated that as a failure, and Touch ID for the bag
stayed off: verifies and keys were refused and no finger could be
enrolled again.

Decide from what the enclave lists: a listed component is loaded as
before (a missing file is refused, with what to do); neither component
nor file is a bag that never enrolled; a file the loaded master no
longer lists is stale, so the bag starts with no fingerprints and the
next enrolment replaces the file. Without a loaded master nothing
proves that, and the restore refuses as before. The restore runs only
after the bag's password, so starting afresh admits no finger; it costs
the owner their enrolled ones.

KUnit covers the decision. On J716c (T6050), after a boot of a kernel
without per-user bags left the master without the bag's component,
enrolment into the bag had been refused.

Signed-off-by: Cameron Cooper <c@meron.cc>
Matching waits at each boot for proof that the cold restore worked: the
configured Linux user's templates must list at least one identity,
because zero after a restore cannot be told from a restore that
silently failed. Once every fingerprint is enrolled into a Unix
account's own key bag, the configured user has none, the proof never
completes, and every verify and key touch was refused before the
capture, the bag's own fingers included.

A match in a bag user's own domain does not use the configured user's
templates, so give it the equivalent proof on the bag side instead
(bag_identity_proof): every attach step of the restore succeeded (the
cold-match preparation, the SCRD credential and the component restore),
and the enclave lists the bag user's identities, read after the bag's
catacomb loaded. A bag listing none answers no match without a
capture; a list the enclave does not give is refused as unproven, as
the configured user's is. A match as the configured user still needs
the full proof.

On J716c (T6050), with both fingers enrolled into the bag and none for
the configured user, the lock screen refused every touch after a
reboot.

Signed-off-by: Cameron Cooper <c@meron.cc>
… loaded

A verify or an enrolment naming a Unix account whose bag is not loaded
(logged out, or not yet unlocked this boot) ran as the configured
user: a verify matched against the shared templates, and an enrolment
went into the shared domain. That is right for an account that never
enrolled a finger of its own. For one that did, the shared templates
are not its fingers, and a match there would let another user's finger
answer for it, or a finger the account deleted from its bag.

Refuse such a request while the bag is not loaded once the account has
an enrolment of its own: its saved bag catacomb exists, or cannot be
shown to be absent. An enrolment into a bag succeeds only once that file
is saved, so the file is the evidence. The master's component list is
not: it names a bag that was designated but never enrolled as well. An
account without the file keeps the configured user, as before per-user
bags.

Signed-off-by: Cameron Cooper <c@meron.cc>
A bag user's catacomb load that answered the cold-transition status
(0x8002) was taken as restored. On J293 one such load came back active
with none of the bag's identities: the bag looked loaded, every match
in it answered no match, and fprintd deleted the account's host records
as unknown to the device. The configured user's 0x8002 load already
needs a live identity count; a bag user's did not.

After a bag user's 0x8002 load, ask the enclave for the bag's
identities. None listed is an empty load: say so, with "enrol again",
and keep the bag usable as for a stale file, so the owner can enrol
again in it. A list that cannot be read is a failed load.

Signed-off-by: Cameron Cooper <c@meron.cc>
An account verify skipped the configured user's after-boot and lockout
gates, but its result still went to the configured user's count: a miss
in an account's own domain counted towards the machine-wide lockout,
and a match there reset it. A password re-armed only the machine-wide
state.

Keep the failed-attempt count, the lockout after VERIFY_RETRY_LIMIT
misses and the wait for the first password since boot per account,
for every match that runs in an account's own domain (a verify naming
it, or a key touch under its bag):

- the account's gates are checked before the capture, and a refusal is
  SEP_BIO_STATUS_LOCKOUT, as for the configured user;
- a definitive result counts for that account alone. The enclave
  answers a finger enrolled only for another user as another user, and
  that is a no match, so it is a miss for the named account and never
  resets its count;
- the enclave's own refusal of MATCH_RESULT locks that account;
- once SEP_BIO_REQUIRE_PASSWORD was called this boot, each account
  also waits for its own password;
- the account's bag password, once the enclave accepts it, clears its
  state and no other. SEP_BIO_REARM and SEP_BIO_LOCKOUT_STATE stay the
  configured user's.

Nothing is recorded across a restart: a match runs in an account's
domain only after its bag's password this boot, which clears its state
anyway.

Signed-off-by: Cameron Cooper <c@meron.cc>
Touch ID enrolment and verify, a touch-gated /dev/sep-key operation and
the late sensor attach each drive the sensor and the enclave's match
operations, and serialize on catacomb_writer. Other BIO calls reach the
enclave without it (GET_INFO, ATTEST, LIST_ENCLAVE, PREPARE_ENROL), so
one of those could run between a key touch's steps.

Add a capture owner. A synchronous user (one of those calls, a
deletion, a bio open, the late attach, a key touch) takes a lease for
its duration; an enrol or verify start hands its lease to the queued
worker, which gives it up only after its cleanup. A second owner gets
EBUSY. Starting a capture still waits, as before, for the last worker
to stop, now before the lease is taken.

KUnit covers the owner's transitions.

Signed-off-by: Cameron Cooper <c@meron.cc>
/dev/sep-key was registered with sep_bio_register_concurrent: one shared
context for every opener, no per-file state, and an unregister that
waited for running ioctls. Requests that belong to the file that made
them (an id to cancel, an authorization a file holds) need state per
open file.

Register it through a small misc-device shim instead. Each open makes a
file that holds its own reference to the driver, so it may outlive the
registration; its calls then fail with ENODEV. Because unregistering no
longer waits for running calls, the driver counts them, and remove
waits for them to end before it takes the hardware down, as the old
unregister did; no call starts once remove has begun.

Every existing /dev/sep-key request goes to key_ioctl unchanged. The
unused register_concurrent Rust wrapper goes; the C function stays for
its KUnit test.

Signed-off-by: Cameron Cooper <c@meron.cc>
A password manager whose items are sealed to Secure Enclave keys needs
one authentication to cover a run of operations: macOS lets a Touch ID
or password authentication be reused for a short time for exactly this
(a reuse duration on the authentication context), so that revealing
several items after one unlock does not ask for a finger each time.
/dev/sep-key has only one-shot authorization: one match, one operation.
Without more, such an app either asks for a finger for every item or
unseals its vault key once and keeps it outside the enclave.

Add authorization contexts (SEP_KEY_CTX_*, SEP_KEY_FEATURE_CONTEXTS),
opt-in with key_context_api=1 on the 13.5 key store with per-user
bags, and bounded:

- a context belongs to one Unix account's bag (NEW: root or that user)
  and is a file descriptor: closing it ends it;
- one authentication (AUTH: a Touch ID match in the user's own domain)
  lasts lifetime_s from the match, at most 12 hours, and ends at once
  when the bag locks, on suspend, on REVOKE and when the driver stops
  (EKEYEXPIRED after);
- each SIGN or DECRYPT is still one enclave operation under that key's
  own access control, run with the context's credential.

Each request carries an id; SEP_KEY_CANCEL_ID on the file it was made on
ends it (an AUTH waiting for a finger). A request's result is delivered
once, after its worker has given up the capture; a cancel or the
deadline wins over an undelivered result. An AUTH straight after a
cancelled one waits, bounded, for the old request's cleanup rather than
fail with EBUSY, and an ended request's capture is not reported as a
sensor fault.

The one-match-one-operation rule stays for every other path; a context
is an explicit, scoped and expiring exception requested by its owner.

KUnit covers request ids, the result and context state machines; the
host test runs the delivery transitions with a drop observer.

Signed-off-by: Cameron Cooper <c@meron.cc>
Touch ID is not always there to authorize a context: no finger may be
enrolled, the bag may have just been unlocked after a lock or a restart,
or the user may prefer the password, as macOS offers it in its prompt.

Add SEP_KEY_CTX_AUTH_PASSWORD. The enclave checks the password against
that context's bag, and counts a wrong one, into a fresh credential that
becomes the context's grant; a wrong password ends the context (outcome
NOT_MATCHED), so another attempt opens a new one. The kernel wipes its
copy of the password, in the reply too. An accepted password also renews
the bag's Touch ID preparation, as a bag unlock does.

Signed-off-by: Cameron Cooper <c@meron.cc>
A context authorizes every key under its bag for as long as it lives. A
password manager's vault session needs only its item key, and only to
decrypt; if anything else in that process misused the context, it could
not then decrypt a passkey's secret or sign with another key.

Add SEP_KEY_CTX_BIND. Before AUTH, and only once, it names one key (by
its handle, which must be under the context's bag) and the operations
(SEP_KEY_CTX_SCOPE_SIGN, _DECRYPT) the context may run on it; SIGN and
DECRYPT then refuse any other key or operation with EPERM. A BIND after
authentication is refused with EBUSY: a scope cannot widen what an
existing authorization covers. An unbound context behaves as before.

KUnit covers the scope's checks.

Signed-off-by: Cameron Cooper <c@meron.cc>
Authorization contexts were offered only on the 13.5 key store, but they
need only per-user bags and a match in the bag user's own domain, which
the variant-5 store (T8140, T6040 and its inheritors) now has too. Offer
them on any key store with per-user bags.

A variant-5 bag's lock, with or without a grace period and at logout,
now revokes that bag's contexts before the store is asked, as the 13.5
lock does, so no retained match outlives it.

On J716c (T6050) a bag context authorized with Touch ID or the bag's
password signed and decrypted with the bag's presence keys, kept each
key and operation to its binding, and ended when released.

Signed-off-by: Cameron Cooper <c@meron.cc>
One owner for the sensor at a time made every /dev/sep-bio request that
takes the enclave hold the capture, but only enrol and verify starts
waited for the last enrol or verify worker to stop. A no-match is
reported before that worker's bookkeeping ends (the lockout record and
the background result), so the request right after one often found the
sensor still held and was refused with EBUSY: fprintd's enrolment after
its duplicate check (PREPARE_ENROL) failed, and libfprint's GET_INFO
before a retry failed, which made that retry match as the built-in user
instead of the account.

Every request that takes the enclave now waits for the worker, bounded
as a capture start is. The worker also frees the sensor before it marks
itself stopped, so a request woken by its exit finds the sensor free.

On J716c (T6050), fprintd's enrolment of a new finger had failed with
EBUSY right after the duplicate check.

Signed-off-by: Cameron Cooper <c@meron.cc>
The authorization-context requests were opt-in through the
key_context_api module parameter. Features here are not enabled by
module parameter, so drop it: SEP_KEY_FEATURE_CONTEXTS is advertised,
and the requests accepted, on every key store with per-user bags. A
context authorizes keys under one user's bag, so a store without them
has nothing for one to authorize.

The ABI stays marked DESIGN DRAFT in apple-sep.h.

Signed-off-by: Cameron Cooper <c@meron.cc>
SEP_KEY_CTX_NEW read lifetime_s = 0 as "no expiry", so a context could
outlast the 12-hour ceiling the ABI promises. Refuse 0 with EINVAL:
a lifetime is now 1 s up to SEP_KEY_CTX_LIFETIME_MAX, checked in one
place (key_context::lifetime_ns), and every authentication sets its
expiry.

Signed-off-by: Cameron Cooper <c@meron.cc>
The owner check ran only at SEP_KEY_CTX_NEW. After that, whoever held
the context descriptor, passed over a socket or inherited across a
setuid exec, could authenticate it and sign or decrypt under it.

Record the creator's effective uid at NEW. Every request on the context
(AUTH, AUTH_PASSWORD, BIND, QUERY, REVOKE, CANCEL_ID) and every SIGN or
DECRYPT naming it from another effective uid now fails with EPERM, root
included: root may make a context for any user, but uses only its own.
Within the creating uid the descriptor stays a capability, which
apple-sep.h now says.

Signed-off-by: Cameron Cooper <c@meron.cc>
…AUTH

SEP_KEY_CTX_BIND is refused with EBUSY once the context has left its
new phase or already has a scope. Move that rule into bind_admitted()
and cover it in KUnit for every phase, so a BIND during or after AUTH
can't narrow, or appear to narrow, a grant asked for without it. No
behaviour change.

Signed-off-by: Cameron Cooper <c@meron.cc>
…equests

sep_key_ctx_new and sep_key_ctx_query had no reserved field. Add two
words to each: NEW refuses a nonzero one with EINVAL, and QUERY returns
them as 0. Both requests grow to 24 bytes, so their ioctl numbers
change; the contexts ABI is still a design draft.

Signed-off-by: Cameron Cooper <c@meron.cc>
A suspend ended every context through revoke_key_contexts(ENODEV), the
driver-stop path, which also advances the context generation. So a live
context answered ENODEV after a resume, and every /dev/sep-key file
opened before the suspend refused all context requests from then on,
NEW included. A long-running client, such as a password manager's
agent, lost contexts until it reopened the device after each resume.

End the contexts on suspend with EKEYREVOKED, as a bag lock does, and
leave the generation alone. No context can start unrevoked: NEW refuses
while 'suspending' is set, under the registry lock the revocation's
snapshot takes. Driver removal still ends contexts with ENODEV and
stales every file.

Signed-off-by: Cameron Cooper <c@meron.cc>
The header gave EKEYEXPIRED for any ended context. The driver answers
EKEYEXPIRED only once the lifetime has passed, and EKEYREVOKED for a
context ended early: REVOKE, the bag locking, a suspend or a failed
AUTH. Say so, and add ENODEV after the driver stops and the brief
ENOSPC while a closed context's credential is still being deleted.

Signed-off-by: Cameron Cooper <c@meron.cc>
@cameroncooper
cameroncooper force-pushed the review/sep-retained-authorization branch from c2096f7 to 02efe89 Compare November 23, 2026 21:14
@cameroncooper

Copy link
Copy Markdown
Author

The newer-Mac rows are done: J716c (M5 Max, T6050, variant-5 store), 2026-11-23, this stack with #3001 and #2162.

  • NEW for another uid's bag: EPERM. Lifetime 0: EINVAL.
  • BIND to A, one Touch ID AUTH, then BIND again: EBUSY. Two signs with A without a touch: ok. Sign with B: EPERM.
  • The user with root's context: SIGN and QUERY get EPERM.
  • After REVOKE, a bag lock and an s2idle suspend: EKEYREVOKED each time, and NEW on the same file works after the lock and after the resume.
  • The closed descriptor: EBADF. 20 NEW+close in turn: all 20.

The run found a variant-5 bug in the bag import, not in this PR: after a bag lock the password didn't reopen the bag. It's fixed in #2049 (see my comment there); the rows after the lock ran with that fix. The description has the full list.

Assisted-by: LLM (Claude:Opus 5.5)

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.

2 participants