← Back to Pull requests

Mark reviewed LocalAuthentication methods safe #864

Open
LucaCappelletti94 opened this pull request with 84 lines diff across 3 files
View on GitHub
LucaCappelletti94 LucaCappelletti94 commented on Sep 25, 2026 18:55 UTC
Author

Enables the blanket safety defaults for LocalAuthentication and keeps an explicit unsafe opt-out only where there is a concrete reason, with that reason written as a comment in translation-config.toml. The per-instance LAContext and LARight accessors become safe, since neither class is Send.

The constructors of LAContext and LARight stay unsafe. On macOS 26.6.2 they increment process-wide instance and command counters with a plain load, add and store, so concurrent construction is a data race. canEvaluatePolicy:error:, biometryType, domainState and LARight.state stay unsafe because they touch shared state of the same kind, as do the completion-handler methods whose error pointers can be null. Behaviour on other OS releases is unverified.

The regenerated bindings are in the generated fork.

Diff: 84 lines (+83 / -1) across 3 files Reviewer: madsmtm Review duration: 29021m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.56) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.56 · 84 lines diff across 3 files
0.56
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.92 · 5 substantive comment(s)
0.92
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.266
Sensitive Paths sensitive_paths
0.056
Real PR Conversation Timeline
16 items
madsmtm madsmtm requested changes on Oct 01, 2026 17:08 UTC
Changes Requested Reviewer

Thanks for the PR! I think I'd prefer blanket:

unsafe-default-safety.documentation-is-reviewed = true
unsafe-default-safety.bounds-checked-internally = true

With opt-outs for the things that aren't safe?

LAContext.biometryType, LAContext.domainState and LARight.state also stay unsafe because the framework updates their backing state from its own queue without a lock

I suspect these are safe because LAContext isn't Sendable? Unless the queue (unsure which queue you're talking about?) runs on another thread?

LucaCappelletti94 LucaCappelletti94 commented on Oct 01, 2026 18:46 UTC
Author

Ok to go for blanket safety defaults with explicit opt-outs.

My proposed opt-outs are speculations informed by public decompilations of the iOS 18.2 and 26.1 binaries, I am not certain about how well they generalize across versions and variants.

In those snapshots, LAClient creates the la_client queue, assigns its XPC connection to it, and dispatches asynchronous evaluation completions onto a global queue (queue, XPC connection, completion).

The decompilations appear to show a process-wide _cachedBiometryType read by LAContext.biometryType and replaced by domainState fetches and evaluation completions without visible synchronization (getter and fetch, completion). If those accesses are not synchronized elsewhere, two threads owning separate LAContext instances could race on that cache despite LAContext lacking Send. The PR also marks canEvaluatePolicy:error: safe, although its evaluation error path can replace the cache, so I suspect it should remain unsafe, which I had not noticed before and I had previously marked as safe.

LARight.state appears to read _state directly while an authorization completion writes it through setState: (writer, getter), which raises a similar concern. Apple marks LAEnvironmentState and LAEnvironmentMechanism NS_SWIFT_SENDABLE at the class level, while LAContext and LARight lack that marker. The LAContext reply block is marked separately.

It seems to me that using these methods unwisely could lead to very odd behaviours. I would keep them unsafe under the blanket defaults.

LucaCappelletti94 LucaCappelletti94 commented on Oct 01, 2026 20:20 UTC
Author

I am spending a little while trying to prove unsafety by provoking crashes or misbehaviour, so we can be surer of the choice made.

LucaCappelletti94 LucaCappelletti94 commented on Oct 01, 2026 22:03 UTC
Author

Now the translator emits configured # Safety text for ordinary methods and synthesized property accessors, and the framework uses blanket safety defaults with explicit unsafe opt-outs.

I tested that on macOS 26.6.2 constructing LAContext and LARight really does update process-wide counters without synchronization, so they really should be kept unsafe in my opinion.

I audited all 53 added safety contracts against the Xcode 27 headers, generated selectors, and available runtime evidence. The corrections cover nullable callback errors, deprecated properties, direct initialization, and concurrency statements that lacked support.

I was only wondering whether to include permalinks to the lines of the decompiled code in the docs to show the source of the claims. Currently I omitted them, what do you think?

Double checked all of the comments with Astra.

madsmtm madsmtm commented on code on Oct 05, 2026 17:37 UTC
Code Review
crates/header-translator/src/config.rs:1
@@ -822,6 +822,9 @@ pub struct MethodData {
     #[serde(rename = "unsafe")]
     #[serde(default)]
     pub unsafe_: Option<bool>,
+    #[serde(rename = "unsafe-reason")]

I think this is valuable, but I'd rather have it done separately (because there's a bunch of existing places where such documentation could be useful).

Would you be up for filing a PR doing that across all the translation-config.tomls? Regardless, for this PR I'd be fine with having the safety docs as comments in just the toml.

madsmtm madsmtm commented on code on Oct 05, 2026 17:38 UTC
Code Review
crates/header-translator/src/config.rs:1
@@ -822,6 +822,9 @@ pub struct MethodData {
     #[serde(rename = "unsafe")]
     #[serde(default)]
     pub unsafe_: Option<bool>,
+    #[serde(rename = "unsafe-reason")]

If you do get around to making a PR for it, I think I would rather have it like class.LAContext.methods.new.unsafe = "xyz", where it automatically detects string vs boolean, and treats a string as unsafe + safety reason.

madsmtm madsmtm requested changes on Oct 05, 2026 17:45 UTC
Changes Requested Reviewer

Thanks for investigating further.

I tested that on macOS 26.6.2 constructing LAContext and LARight really does update process-wide counters without synchronization, so they really should be kept unsafe in my opinion.

Hmm, are you sure it's not just done atomically?

If it really is true that LAContext.new could race with other such calls, that sounds like a bug in Apple's framework?

Is it possible for you to reproduce the racing behaviour somehow?

I was only wondering whether to include permalinks to the lines of the decompiled code in the docs to show the source of the claims. Currently I omitted them, what do you think?

I'd prefer to omit that, since it's not really an autoritative source.

LucaCappelletti94 LucaCappelletti94 commented on Oct 05, 2026 17:50 UTC
Author

Hmm, are you sure it's not just done atomically?

The decompiled code I linked does not show any sort of thread-awareness, but I did not red team it, I could try to see whether I can make it crash by assuming it is safe.

If it really is true that LAContext.new could race with other such calls, that sounds like a bug in Apple's framework?

Wouldn't be the first, and that is going in the top of the list of stuff to fuzz now.
In the last few days it has really been a madness of bug reporting & fixing.

Ok on the other notes.

LucaCappelletti94 LucaCappelletti94 commented on Oct 05, 2026 18:47 UTC
Author

Reproduced the counter race directly this time around.

+[LAContext newInstanceId] and +[LAContext newCommandId] disassemble to a plain adrp/ldr/add/str sequence on a process-global address, no atomic instruction, no lock. Calling that selector from 16 threads at a million iterations each on macOS 26.6.2 advanced the counter by only 1,201,097 instead of 16,000,000, so it is not done atomically.

I could not make (LAContext.new / LARight.new) analogously race, but I believe it is because each construction also does an XPC round trip to the LA daemon, which happens to space the threads out past the tiny racy window. The primitive is still very much unsound, it is just more unlikely to misbehave.

biometryType and domainState read a process-wide cached NSNumber pointer without retaining it first, while the writer stores a new value and releases the old one right after, both unguarded. That I suppose could be a potential use-after-free, worse than a plain race.

Also found https://developer.apple.com/forums/thread/710327 which suggests to me somebody slammed into this unsafety.

LucaCappelletti94 LucaCappelletti94 commented on code on Oct 05, 2026 19:44 UTC
Code Review
crates/header-translator/src/config.rs:1
@@ -822,6 +822,9 @@ pub struct MethodData {
     #[serde(rename = "unsafe")]
     #[serde(default)]
     pub unsafe_: Option<bool>,
+    #[serde(rename = "unsafe-reason")]

Moved the reasons to TOML comments. I'll do the unsafe = "reason" follow-up once this lands if I get the time.

LucaCappelletti94 LucaCappelletti94 commented on Oct 05, 2026 19:44 UTC
Author

LAContext and LARight accessors are now safe since neither class is Send, and only methods touching process-wide state or callbacks stay unsafe.

Three jobs were cancelled for lack of a GitHub runner. Could you rerun them?

madsmtm madsmtm commented on code on Oct 11, 2026 13:26 UTC
Code Review
framework-crates/objc2-local-authentication/translation-config.toml:1
@@ -15,3 +15,73 @@ enum.LAPolicy.use-value = true
 enum.LACredentialType.use-value = true
 enum.LAError.use-value = true
 enum.LAAccessControlOperation.use-value = true
+
+unsafe-default-safety.documentation-is-reviewed = true
+unsafe-default-safety.bounds-checked-internally = true
+
+# Construction increments process-wide `+newInstanceId` and `+newCommandId`
+# counters without atomics or a lock (observed on macOS 26.6.2).
+class.LAContext.methods.init.unsafe = true
+class.LAContext.methods.new.unsafe = true
+class.LARight.methods.init.unsafe = true
+class.LARight.methods."initWithRequirement:".unsafe = true
+class.LARight.methods.new.unsafe = true
+
+# Increments the process-wide command counter, and its error path replaces the
+# process-wide cached biometry type.
+class.LAContext.methods."canEvaluatePolicy:error:".unsafe = true
+
+# Reads the process-wide cached biometry type without retaining it, while
+# other contexts and evaluation completions replace and release it unguarded.
+class.LAContext.methods.biometryType.unsafe = true
+class.LAContext.methods.domainState.unsafe = true
+class.LAContext.methods.evaluatedPolicyDomainState.unsafe = true
+
+# Deprecated and not reviewed.
+class.LAContext.methods.maxBiometryFailures.unsafe = true
+class.LAContext.methods."setMaxBiometryFailures:".unsafe = true
+
+# The reply runs on a framework queue and updates process-wide state shared
+# with construction. Its error pointer may be null.
+class.LAContext.methods."evaluatePolicy:localizedReason:reply:".unsafe = true
+class.LAContext.methods."evaluateAccessControl:operation:localizedReason:reply:".unsafe = true
+
+# The completion runs on a framework queue and writes the right's `state`.
+# Its error pointer may be null.
+class.LARight.methods."authorizeWithLocalizedReason:completion:".unsafe = true
+class.LARight.methods."checkCanAuthorizeWithCompletion:".unsafe = true
+class.LARight.methods."deauthorizeWithCompletion:".unsafe = true
+
+# Reads `_state` directly while authorization completions write it from a
+# framework queue.
+class.LARight.methods.state.unsafe = true
+
+# Completion result and error pointers may be null.
+class.LARightStore.methods."rightForIdentifier:completion:".unsafe = true
+class.LARightStore.methods."saveRight:identifier:completion:".unsafe = true
+class.LARightStore.methods."saveRight:identifier:secret:completion:".unsafe = true
+class.LARightStore.methods."removeRight:completion:".unsafe = true
+class.LARightStore.methods."removeRightForIdentifier:completion:".unsafe = true
+class.LARightStore.methods."removeAllRightsWithCompletion:".unsafe = true
+class.LAPrivateKey.methods."signData:secKeyAlgorithm:completion:".unsafe = true
+class.LAPrivateKey.methods."decryptData:secKeyAlgorithm:completion:".unsafe = true
+class.LAPrivateKey.methods."exchangeKeysWithPublicKey:secKeyAlgorithm:secKeyParameters:completion:".unsafe = true
+class.LAPublicKey.methods."exportBytesWithCompletion:".unsafe = true
+class.LAPublicKey.methods."encryptData:secKeyAlgorithm:completion:".unsafe = true
+class.LAPublicKey.methods."verifyData:signature:secKeyAlgorithm:completion:".unsafe = true
+class.LASecret.methods."loadDataWithCompletion:".unsafe = true

This is not a soundness problem, since to actually use the pointers, you have to write unsafe { err.as_ref() } or similar in the completion handler. So I think these can be safe.

madsmtm madsmtm commented on Oct 11, 2026 13:41 UTC
Reviewer

Ugh, annoying, thanks for reproducing it!

I'm not really happy with this (which is due to no fault of yours), I feel like it doesn't really help to mark these methods as unsafe (since you wouldn't be able to soundly use them in a library, the only sound usage is in an application where you state "I don't use this from any other thread"). Ideally we'd find some way to ensure that these APIs are used from a single thread, but I don't think there is such a way (it's the same situation as with std::env::set_var, we can fix it Rust-only with a Mutex, but that doesn't help with C libraries using these).

---

Could I get you to file a FB with Apple through their Feedback Assistant? This is also a bug affecting Obj-C and Swift as you found, and Apple should either make the accesses here atomic, mark the APIs as @MainActor, or in some other way give it actor isolation.

Without a reply from Apple, I'm a bit hesitant to mark methods safe, since IIUC there's nothing stopping Apple from changing e.g. LADomainStateBiometry.biometryType to read the process-wide biometry type instead in a future macOS version? And then we'd have to mark that method as unsafe again.

Does that make sense? Sorry for the big asks, but I'm not too familiar with LocalAuthentication, and I'd rather be conservative here.

LucaCappelletti94 LucaCappelletti94 commented on Oct 13, 2026 09:54 UTC
Author

I'm not really happy with this (which is due to no fault of yours), I feel like it doesn't really help to mark these methods as unsafe (since you wouldn't be able to soundly use them in a library, the only sound usage is in an application where you state "I don't use this from any other thread"). Ideally we'd find some way to ensure that these APIs are used from a single thread, but I don't think there is such a way (it's the same situation as with std::env::set_var, we can fix it Rust-only with a Mutex, but that doesn't help with C libraries using these).

Yeah these are not "unsafeties" as much as just plain bad design, as "unsafe" does not mean race-y and buggy. In the context of https://github.com/open-source-cooperative/apple-native-keyring-store I am handling these APIs with the necessary care (if the user does not directly call the underlying APIs also elsewhere, which would immediately defeat the purpose)

Could I get you to file a FB with Apple through their Feedback Assistant? This is also a bug affecting Obj-C and Swift as you found, and Apple should either make the accesses here atomic, mark the APIs as @MainActor, or in some other way give it actor isolation.

Yeah, I went Hail Mary and started putting reproductions for each of the API crashes in https://github.com/LucaCappelletti94/apple-sdk-repros alongside the FB where I filed them already. It is rather unfortunate that Apple keeps all FBs private, I get it for CVEs but these are just bugs I believe.

Without a reply from Apple, I'm a bit hesitant to mark methods safe, since IIUC there's nothing stopping Apple from changing e.g. LADomainStateBiometry.biometryType to read the process-wide biometry type instead in a future macOS version? And then we'd have to mark that method as unsafe again.

True, and there is no harness we could use to prove safety. When I started with this PR work I honestly expected this to be somewhat more straightforward and while it kindof is for certain methods, there is no guarantee that between versions the same safety guarantee holds.

The only thing I can think of would be, in the brave new LLM world, to add some automation that detects safety/unsafety, but I would only ever take the result of an LLM interpretation of a method's safety as a suggestion.

Given that these two PRs only aimed at bringing better Rust clarify to the bindings, and make it clear which ones can be considered safe and not, I would suggest to pivot from trying to add the safe/unsafe marker and add documentation of bugginess in some way.

madsmtm madsmtm commented on Oct 14, 2026 19:30 UTC
Contributor

Yeah, I went Hail Mary and started putting reproductions for each of the API crashes in https://github.com/LucaCappelletti94/apple-sdk-repros alongside the FB where I filed them already.

Thanks a lot!

I would suggest to pivot from trying to add the safe/unsafe marker and add documentation of bugginess in some way.

Yeah, that might be better. Let's wait a while though (probably at least a month if I know Apple Feedback right) and see whether they decide to want to fix it or not.

Reviewers
Assignees
LucaCappelletti94 LucaCappelletti94
Labels
risk: low confidence: 91.9% reviewed sensitive_module
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 55.7% (MEDIUM) · Higher = larger blast radius
55.7%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 91.9% · Lower = rushed or shallow review
91.9%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 4.5% · Safe to merge
4.5%
Repository