Repository navigation
security: enforce SEP-signed SIP/Full-Security MDA with bounded freshness (#302) #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bd7a879
8318210
b305753
6b09ef4
7e691b9
6d222fb
286c024
41d9962
0072c9e
e7b94fa
d370dee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,379 @@ | ||
| package api | ||
|
|
||
| // Tests for the MDA routing verdict (issue #302). evaluateMDA is the pure | ||
| // decision core of verifyAppleDeviceAttestation — the full matrix runs without | ||
| // an MDM round-trip. The properties pinned here: | ||
| // - only a FRESH (epoch-nonce), SE-key-bound, SIP-on, Full-Security, | ||
| // serial-matching Apple verdict routes; | ||
| // - Apple-signed violations (chain/serial/SIP/boot) are DEFINITIVE; | ||
| // - cached/legacy/stale responses are transient — no verdict, no untrust. | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/eigeninference/d-inference/coordinator/attestation" | ||
| "github.com/eigeninference/d-inference/coordinator/protocol" | ||
| "github.com/eigeninference/d-inference/coordinator/registry" | ||
| "github.com/eigeninference/d-inference/coordinator/store" | ||
| ) | ||
|
|
||
| func TestEvaluateMDA(t *testing.T) { | ||
| seed := []byte("test-seed-0123456789abcdef0123456789abcdef") | ||
| now := time.Unix(1_770_000_000, 0) | ||
| const serial = "DRXYPT2MKX" | ||
| const seKey = "se-public-key-b64" | ||
| freshNonce := attestation.MDAEpochNonce(seed, attestation.MDANonceEpoch(now), seKey) | ||
| prevNonce := attestation.MDAEpochNonce(seed, attestation.MDANonceEpoch(now)-1, seKey) | ||
| staleNonce := attestation.MDAEpochNonce(seed, attestation.MDANonceEpoch(now)-attestation.MDANonceAcceptedEpochs, seKey) | ||
|
|
||
| good := func() *attestation.MDAResult { | ||
| return &attestation.MDAResult{ | ||
| Valid: true, | ||
| DeviceSerial: serial, | ||
| SIPEnabled: true, | ||
| SecureBootEnabled: true, | ||
| BootState: "Full Security", | ||
| FreshnessCode: freshNonce, | ||
| LeafNotBefore: now.Add(-24 * time.Hour), | ||
| } | ||
| } | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| mutate func(*attestation.MDAResult) | ||
| verified bool | ||
| definitive bool | ||
| reason string | ||
| }{ | ||
| {"all good — verdict granted", func(m *attestation.MDAResult) {}, true, false, "ok"}, | ||
| {"previous-epoch nonce (Apple cached cert) still fresh", func(m *attestation.MDAResult) { | ||
| m.FreshnessCode = prevNonce | ||
| }, true, false, "ok"}, | ||
| {"chain invalid is definitive", func(m *attestation.MDAResult) { | ||
| m.Valid = false | ||
| }, false, true, "chain_invalid"}, | ||
| {"serial mismatch is definitive (impersonation)", func(m *attestation.MDAResult) { | ||
| m.DeviceSerial = "OTHER-DEVICE" | ||
| }, false, true, "serial_mismatch"}, | ||
| {"empty MDA serial fails closed", func(m *attestation.MDAResult) { | ||
| m.DeviceSerial = "" | ||
| }, false, true, "serial_missing"}, | ||
| {"SIP disabled is definitive — even on a cached cert", func(m *attestation.MDAResult) { | ||
| m.SIPEnabled = false | ||
| m.FreshnessCode = staleNonce | ||
| }, false, true, "sip_disabled"}, | ||
| {"non-Full-Security boot is definitive", func(m *attestation.MDAResult) { | ||
| m.SecureBootEnabled = false | ||
| m.BootState = "Permissive Security" | ||
| }, false, true, "not_full_security"}, | ||
| {"third-party kexts allowed is definitive — never a routable verdict", func(m *attestation.MDAResult) { | ||
| // Should be unreachable on a real Full-Security boot, but the .13.3 | ||
| // fail-closed parse must actually gate (defense-in-depth): a cert that | ||
| // reads SIP-on + Full-Security yet allows kexts must NOT route. | ||
| m.ThirdPartyKexts = true | ||
| }, false, true, "third_party_kexts"}, | ||
| {"legacy constant nonce — migration state, transient", func(m *attestation.MDAResult) { | ||
| m.FreshnessCode = attestation.LegacyMDANonce(seKey) | ||
| }, false, false, "legacy_nonce"}, | ||
| {"out-of-window nonce — the replay bound, transient", func(m *attestation.MDAResult) { | ||
| m.FreshnessCode = staleNonce | ||
| }, false, false, "nonce_unrecognized"}, | ||
| {"another device's fresh nonce does not bind", func(m *attestation.MDAResult) { | ||
| m.FreshnessCode = attestation.MDAEpochNonce(seed, attestation.MDANonceEpoch(now), "other-se-key") | ||
| }, false, false, "nonce_unrecognized"}, | ||
| {"missing freshness code — transient", func(m *attestation.MDAResult) { | ||
| m.FreshnessCode = nil | ||
| }, false, false, "nonce_unrecognized"}, | ||
| {"cert older than MDAMaxCertAge — age backstop", func(m *attestation.MDAResult) { | ||
| m.LeafNotBefore = now.Add(-attestation.MDAMaxCertAge - time.Hour) | ||
| }, false, false, "cert_too_old"}, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| m := good() | ||
| tc.mutate(m) | ||
| eval := evaluateMDA(m, serial, seKey, seed, now) | ||
| if eval.SIPVerified != tc.verified { | ||
| t.Errorf("SIPVerified = %v, want %v", eval.SIPVerified, tc.verified) | ||
| } | ||
| if eval.Definitive != tc.definitive { | ||
| t.Errorf("Definitive = %v, want %v", eval.Definitive, tc.definitive) | ||
| } | ||
| if eval.Reason != tc.reason { | ||
| t.Errorf("Reason = %q, want %q", eval.Reason, tc.reason) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // The replay scenario from issue #302 Gap 1, end to end at the decision layer: | ||
| // an attacker captures a SIP-on cert while genuinely SIP-on, reboots SIP-off, | ||
| // and replays it. Under the legacy constant nonce that cert verified forever; | ||
| // under epoch nonces it stops binding once its epoch ages out, and the routing | ||
| // verdict is denied (transient — the device can re-earn by re-attesting, which | ||
| // would then carry SIP-off and untrust it). | ||
| func TestEvaluateMDA_ReplayedCertAgesOut(t *testing.T) { | ||
| seed := []byte("test-seed-0123456789abcdef0123456789abcdef") | ||
| const serial, seKey = "SERIAL", "se-key" | ||
| captureTime := time.Unix(1_770_000_000, 0) | ||
|
|
||
| captured := &attestation.MDAResult{ | ||
| Valid: true, | ||
| DeviceSerial: serial, | ||
| SIPEnabled: true, | ||
| SecureBootEnabled: true, | ||
| BootState: "Full Security", | ||
| FreshnessCode: attestation.MDAEpochNonce(seed, attestation.MDANonceEpoch(captureTime), seKey), | ||
| LeafNotBefore: captureTime, | ||
| } | ||
|
|
||
| // Immediately after capture: verdict granted (the cert IS genuine + fresh). | ||
| if eval := evaluateMDA(captured, serial, seKey, seed, captureTime.Add(time.Hour)); !eval.SIPVerified { | ||
| t.Fatalf("freshly captured cert should verify, got %+v", eval) | ||
| } | ||
|
|
||
| // Replayed after the accepted-epoch window: denied. | ||
| replayAt := captureTime.Add(attestation.MDANonceAcceptedEpochs*attestation.MDANonceEpochLength + time.Hour) | ||
| eval := evaluateMDA(captured, serial, seKey, seed, replayAt) | ||
| if eval.SIPVerified { | ||
| t.Fatal("replayed cert past the epoch window must NOT verify — this was the unbounded-replay hole") | ||
| } | ||
| if eval.Definitive { | ||
| t.Fatal("an aged-out cert is transient (the honest device re-mints), not a definitive violation") | ||
| } | ||
| } | ||
|
|
||
| // HandleLateMDA must only attribute a cert that verifies to Apple's root: an | ||
| // unparseable or non-Apple chain is ignored (never sets a verdict on any | ||
| // provider). The full verdict matrix for an attributed cert is covered by | ||
| // TestEvaluateMDA; here we pin the safe rejection path and that the late | ||
| // callback runs without panicking against a populated registry. | ||
| func TestHandleLateMDA_RejectsUnattributableCert(t *testing.T) { | ||
| logger := quietLogger() | ||
| reg := registry.New(logger) | ||
| srv := NewServer(reg, store.NewMemory(store.Config{}), ServerConfig{}, logger) | ||
| srv.SetMDANonceSeed([]byte("test-seed-0123456789abcdef0123456789abcdef")) | ||
|
|
||
| // A provider that would match by serial IF a cert verified — to prove the | ||
| // chain check (not just "no provider") is what rejects. | ||
| p := reg.Register("p-late", nil, &protocol.RegisterMessage{PublicKey: "k"}) | ||
| p.Mu().Lock() | ||
| p.AttestationResult = &attestation.VerificationResult{SerialNumber: "SERIAL-1", PublicKey: "k"} | ||
| p.MDASIPVerified = false | ||
| p.Mu().Unlock() | ||
|
|
||
| // Garbage and empty chains: parse/verify fails → no-op, no panic. | ||
| srv.HandleLateMDA("udid-x", [][]byte{[]byte("not-a-cert")}) | ||
| srv.HandleLateMDA("udid-x", nil) | ||
|
|
||
| p.Mu().Lock() | ||
| defer p.Mu().Unlock() | ||
| if p.MDASIPVerified { | ||
| t.Error("an unverifiable late cert must never grant the MDA routing verdict") | ||
| } | ||
| if p.MDAVerified { | ||
| t.Error("an unverifiable late cert must not set MDAVerified") | ||
| } | ||
| } | ||
|
|
||
| // Regression for the lock-ordering deadlock (audit of #302): the late-MDA path | ||
| // applies the verdict via registry write-lock methods (MDAEnforced / | ||
| // MarkUntrusted), which must NOT be called inside ForEachProvider's read lock. | ||
| // attributeAndApplyMDA collects under the RLock then applies after. Driven with | ||
| // a hand-built verified result (a real Apple chain can't be forged in-tree): a | ||
| // late SIP-off cert under enforcement must untrust the matching provider AND | ||
| // the call must complete promptly (a deadlock would hang the whole coordinator). | ||
| func TestAttributeAndApplyMDA_LateSIPOffUntrustsWithoutDeadlock(t *testing.T) { | ||
| logger := quietLogger() | ||
| reg := registry.New(logger) | ||
| srv := NewServer(reg, store.NewMemory(store.Config{}), ServerConfig{}, logger) | ||
| srv.SetMDANonceSeed([]byte("test-seed-0123456789abcdef0123456789abcdef")) | ||
| reg.SetMDAEnforceDeadline(time.Now().Add(-time.Minute)) // enforced | ||
|
|
||
| const serial = "SERIAL-LATE" | ||
| p := reg.Register("p-late", nil, &protocol.RegisterMessage{PublicKey: "k"}) | ||
| p.Mu().Lock() | ||
| p.AttestationResult = &attestation.VerificationResult{SerialNumber: serial, PublicKey: "k"} | ||
| p.TrustLevel = registry.TrustHardware | ||
| p.Mu().Unlock() | ||
|
|
||
| // A genuinely-Apple-signed SIP-off cert (Valid + serial match + SIP off) is | ||
| // the reachable definitive case on the late path. Hand-build the parsed | ||
| // result to bypass the un-forgeable real-root chain check. | ||
| mdaResult := &attestation.MDAResult{ | ||
| Valid: true, | ||
| DeviceSerial: serial, | ||
| SIPEnabled: false, | ||
| SecureBootEnabled: true, | ||
| BootState: "Full Security", | ||
| } | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| srv.attributeAndApplyMDA(mdaResult, [][]byte{{0x01}}) | ||
| close(done) | ||
| }() | ||
| select { | ||
| case <-done: | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("attributeAndApplyMDA deadlocked — registry write-lock method called inside ForEachProvider's RLock") | ||
| } | ||
|
|
||
| got := reg.GetProvider("p-late") | ||
| if got == nil { | ||
| t.Fatal("provider vanished") | ||
| } | ||
| got.Mu().Lock() | ||
| status := got.Status | ||
| got.Mu().Unlock() | ||
| if status != registry.StatusUntrusted { | ||
| t.Errorf("a late Apple-signed SIP-off cert must untrust the provider under enforcement; status=%v", status) | ||
| } | ||
| } | ||
|
|
||
| // The late path attributes a cert by its Apple-signed SERIAL + SE-key-derived | ||
| // nonce, NOT by comparing the cert's .8.9.2 UDID to the webhook UDID (those are | ||
| // different namespaces on a Mac and never match — see applyLateMDAResult). A | ||
| // valid late cert for the provider's serial grants the verdict regardless of | ||
| // the (irrelevant) webhook udid; a cert for a DIFFERENT serial never touches it. | ||
| func TestApplyLateMDAAttributesBySerial(t *testing.T) { | ||
| logger := quietLogger() | ||
| reg := registry.New(logger) | ||
| srv := NewServer(reg, store.NewMemory(store.Config{}), ServerConfig{}, logger) | ||
| seed := []byte("test-seed-0123456789abcdef0123456789abcdef") | ||
| srv.SetMDANonceSeed(seed) | ||
| now := time.Now() | ||
|
|
||
| const serialB = "SERIAL-B" | ||
| const seKeyB = "se-key-b" | ||
|
|
||
| p := reg.Register("p-b", nil, &protocol.RegisterMessage{PublicKey: "k"}) | ||
| p.Mu().Lock() | ||
| p.AttestationResult = &attestation.VerificationResult{SerialNumber: serialB, PublicKey: seKeyB} | ||
| p.Mu().Unlock() | ||
|
|
||
| freshFor := func(serial, seKey string) *attestation.MDAResult { | ||
| return &attestation.MDAResult{ | ||
| Valid: true, | ||
| DeviceSerial: serial, | ||
| DeviceUDID: "irrelevant-provisioning-udid", | ||
| SIPEnabled: true, | ||
| SecureBootEnabled: true, | ||
| BootState: "Full Security", | ||
| FreshnessCode: attestation.MDAEpochNonce(seed, attestation.MDANonceEpoch(now), seKey), | ||
| LeafNotBefore: now, | ||
| } | ||
| } | ||
|
|
||
| // A cert for a DIFFERENT serial must never touch this provider. | ||
| srv.applyLateMDAResult("any-webhook-udid", freshFor("SERIAL-OTHER", "se-key-other"), [][]byte{{0x01}}) | ||
| p.Mu().Lock() | ||
| leaked := p.MDASIPVerified | ||
| p.Mu().Unlock() | ||
| if leaked { | ||
| t.Fatal("a late cert for another serial must not grant THIS provider a verdict") | ||
| } | ||
|
|
||
| // A valid late cert for the provider's serial grants the verdict — the | ||
| // webhook udid is irrelevant (the old cross-namespace UDID equality check | ||
| // would have dropped every real Mac cert here). | ||
| srv.applyLateMDAResult("mismatched-hardware-uuid", freshFor(serialB, seKeyB), [][]byte{{0x01}}) | ||
| p.Mu().Lock() | ||
| defer p.Mu().Unlock() | ||
| if !p.MDASIPVerified { | ||
| t.Fatal("a valid late cert for the provider's serial must grant the MDA verdict") | ||
| } | ||
| } | ||
|
|
||
| func TestApplyMDAVerdictDrainsQueuedRequests(t *testing.T) { | ||
| logger := quietLogger() | ||
| reg := registry.New(logger) | ||
| srv := NewServer(reg, store.NewMemory(store.Config{}), ServerConfig{}, logger) | ||
| seed := []byte("test-seed-0123456789abcdef0123456789abcdef") | ||
| srv.SetMDANonceSeed(seed) | ||
| reg.SetMDAEnforceDeadline(time.Now().Add(-time.Minute)) | ||
|
|
||
| const model = "mda-drain-model" | ||
| const serial = "SERIAL-DRAIN" | ||
| const seKey = "se-key-drain" | ||
| registerBuildsProvider(srv, "p-drain", model) | ||
| p := reg.GetProvider("p-drain") | ||
| if p == nil { | ||
| t.Fatal("provider not registered") | ||
| } | ||
| p.Mu().Lock() | ||
| p.AttestationResult = &attestation.VerificationResult{Valid: true, SerialNumber: serial, PublicKey: seKey} | ||
| p.MDASIPVerified = false | ||
| p.Mu().Unlock() | ||
|
|
||
| req := ®istry.QueuedRequest{ | ||
| RequestID: "queued-mda", | ||
| Model: model, | ||
| ResponseCh: make(chan *registry.Provider, 1), | ||
| Pending: ®istry.PendingRequest{ | ||
| RequestID: "queued-mda", | ||
| Model: model, | ||
| RequestedMaxTokens: 128, | ||
| EstimatedPromptTokens: 32, | ||
| }, | ||
| } | ||
| if err := reg.Queue().Enqueue(req); err != nil { | ||
| t.Fatalf("enqueue: %v", err) | ||
| } | ||
| reg.DrainQueuedRequestsForProvider(p) | ||
| select { | ||
| case assigned := <-req.ResponseCh: | ||
| t.Fatalf("request dispatched before MDA verdict: %+v", assigned) | ||
| default: | ||
| } | ||
|
|
||
| now := time.Now() | ||
| mdaResult := &attestation.MDAResult{ | ||
| Valid: true, | ||
| DeviceSerial: serial, | ||
| DeviceUDID: "UDID-DRAIN", | ||
| SIPEnabled: true, | ||
| SecureBootEnabled: true, | ||
| BootState: "Full Security", | ||
| FreshnessCode: attestation.MDAEpochNonce(seed, attestation.MDANonceEpoch(now), seKey), | ||
| LeafNotBefore: now, | ||
| } | ||
| srv.applyMDAVerdict(p.ID, p, mdaResult, [][]byte{{0x01}}, serial, seKey) | ||
|
|
||
| select { | ||
| case assigned := <-req.ResponseCh: | ||
| if assigned == nil || assigned.ID != p.ID { | ||
| t.Fatalf("expected dispatch to %q, got %+v", p.ID, assigned) | ||
| } | ||
| case <-time.After(2 * time.Second): | ||
| t.Fatal("MDA verdict did not drain queued request") | ||
| } | ||
| } | ||
|
|
||
| // The legacy constant nonce — what every pre-#302 cert carries, and what an | ||
| // attacker can compute (sha256 of the public SE key) — must never satisfy the | ||
| // routing verdict, regardless of cert age. | ||
| func TestEvaluateMDA_LegacyConstantNeverRoutes(t *testing.T) { | ||
| seed := []byte("test-seed-0123456789abcdef0123456789abcdef") | ||
| now := time.Unix(1_770_000_000, 0) | ||
| const serial, seKey = "SERIAL", "se-key" | ||
|
|
||
| m := &attestation.MDAResult{ | ||
| Valid: true, | ||
| DeviceSerial: serial, | ||
| SIPEnabled: true, | ||
| SecureBootEnabled: true, | ||
| BootState: "Full Security", | ||
| FreshnessCode: attestation.LegacyMDANonce(seKey), | ||
| LeafNotBefore: now.Add(-time.Hour), // even brand-new | ||
| } | ||
| eval := evaluateMDA(m, serial, seKey, seed, now) | ||
| if eval.SIPVerified { | ||
| t.Fatal("the legacy constant sha256(SE key) nonce must never grant the routing verdict") | ||
| } | ||
| if !eval.SEKeyBound { | ||
| t.Error("legacy nonce still proves key binding (display), just not freshness") | ||
| } | ||
| } | ||
|
anupsv marked this conversation as resolved.
Resolved
anupsv marked this conversation as resolved.
Resolved
Comment on lines
+1
to
+379
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 [INFO] 🧩 Test setup helpers create similar test certificates repeatedly 💡 Suggestion: Extract common test certificate creation logic into shared test utilities to reduce duplication across test cases. 📊 Score: 2×2 = 4 · Category: duplicate logic
Comment on lines
+1
to
+379
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 [INFO] 🧩 Test file has extensive setup helpers that may be over-engineered 💡 Suggestion: Consider if the test matrix could be simplified. The 'good()' function and mutation pattern (lines 39-49) adds indirection that makes tests harder to read. 📊 Score: 2×2 = 4 · Category: over-abstraction |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 [INFO] 🧩 Test setup creates multiple similar test result objects
💡 Suggestion: Consider using a test builder pattern or factory function to reduce duplication
📊 Score: 2×2 = 4 · Category: duplicate logic