Stop blind rollbacks in the Keychain delete paths

Fifth review pass, which prompted measuring whether SecItemDelete can be
conditioned on a value. It cannot: a delete issued with a deliberately
wrong kSecValueData still returns 0 and removes the item, so there is no
compare-and-delete and every check-then-delete is a race by construction.
That measurement decides both fixes here.

Adoption no longer rolls back its own write when the verification read
comes back empty. The rollback could only ever be a blind delete of
whatever occupied the key by then - including a credential synced from
another device in the gap - and it would propagate. Nothing is deleted; no
marker is written, so the account stays signed out and an ordinary sign-in
recovers it.

Legacy deletion re-reads immediately before deleting and aborts if the
value moved, shrinking the window to a single call gap and catching a
replacement that landed while the fingerprint was being computed. A
residual race remains and is documented at the call site: closing it is not
possible with these APIs, and never deleting would leave a credential the
user explicitly removed sitting in their iCloud Keychain.

The debug provenance report repeated the same nil-token ambiguity the scope
logic was fixed for, reporting "no Apple account signed in" when iCloud
Drive is merely off. It now reports the token as unavailable and shows
CloudKit's account status separately, so a re-run cannot draw a false
conclusion from it.
This commit is contained in:
Arkadiusz Fal
2026-08-23 23:59:59 +02:00
parent d2e9264119
commit 2d23820e14
3 changed files with 71 additions and 10 deletions

View File

@@ -21,6 +21,7 @@ import UIKit
struct KeychainProvenanceExperimentView: View {
@State private var label = ""
@State private var userRecordID: String?
@State private var accountAvailability: CloudKitIdentity.AccountAvailability = .indeterminate
@State private var resolvingUserRecordID = true
@State private var reports: [KeychainProvenanceReport] = KeychainProvenanceReportStore.load()
@State private var latestStatus: String?
@@ -38,6 +39,7 @@ struct KeychainProvenanceExperimentView: View {
.navigationBarTitleDisplayMode(.inline)
#endif
.task {
accountAvailability = await CloudKitIdentity.accountAvailability()
userRecordID = await CloudKitIdentity.userRecordID()
resolvingUserRecordID = false
}
@@ -78,10 +80,13 @@ struct KeychainProvenanceExperimentView: View {
private var identitySection: some View {
SettingsFormSection(
"Identity",
footer: "The fingerprint is an opaque hash of ubiquityIdentityToken. It must differ between accounts A and B — if it does not, the sign-out did not take effect."
footer: "The fingerprint is an opaque hash of ubiquityIdentityToken and must differ between accounts A and B — if it does not, the sign-out did not take effect. An unavailable token does not by itself mean signed out: it also goes missing when iCloud Drive is off, which is why the account status is shown separately."
) {
row("CloudKit entitlement", CloudKitAvailability.isAvailable ? "available" : "missing")
row("Apple account", CloudKitIdentity.ubiquityFingerprint.map { "fp=\($0)" } ?? "signed out")
// A nil token is ambiguous — it also means iCloud Drive is off —
// so the account status is shown beside it rather than inferred.
row("Ubiquity token", CloudKitIdentity.ubiquityFingerprint.map { "fp=\($0)" } ?? "unavailable")
row("Apple account", accountStatusText)
row("User record", resolvingUserRecordID ? "resolving…" : (userRecordID ?? "unresolved"))
}
}
@@ -179,7 +184,11 @@ struct KeychainProvenanceExperimentView: View {
}
private func capture() {
let text = KeychainProvenanceReportBuilder.build(label: label, userRecordID: userRecordID)
let text = KeychainProvenanceReportBuilder.build(
label: label,
userRecordID: userRecordID,
accountAvailability: accountAvailability
)
reports = KeychainProvenanceReportStore.append(
KeychainProvenanceReport(label: label, text: text)
)
@@ -195,6 +204,15 @@ struct KeychainProvenanceExperimentView: View {
latestStatus = "Captured report \(reports.count)."
}
private var accountStatusText: String {
if resolvingUserRecordID { return "checking…" }
switch accountAvailability {
case .noAccount: return "no account signed in"
case .available: return "signed in"
case .indeterminate: return "could not be determined"
}
}
private func payloadText() -> String {
let fingerprint = CloudKitIdentity.ubiquityFingerprint ?? "none"
let written = Date().formatted(.iso8601)