Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(egress): read subjFence under s.mu in the data-plane-ready hook
高然7c9c380be3c0
diff --git a/components/egress/fastsandbox_actions.go b/components/egress/fastsandbox_actions.goindex bc062e15c..fe6098c79 100644--- a/components/egress/fastsandbox_actions.go+++ b/components/egress/fastsandbox_actions.go@@ -307,16 +307,21 @@ func (s *fastSandboxPolicyServer) applyLifecycleHook(env *actionhandler.Envelope// ever consume.s.policyCommitMu.Lock()defer s.policyCommitMu.Unlock()- // In-lock fence re-validation: the pre-lock check cannot see a- // rebind that completes while this Hook waits on policyCommitMu —+ // Fence re-validation AND the pending peek under one s.mu hold+ // (policyCommitMu → s.mu, the established order): fenceMatchesLocked+ // reads subjFence, which terminal cleanup deletes under s.mu without+ // policyCommitMu (dropSubjectState is called under the registry write+ // lock from takeover) — an unsynchronized read here would race that+ // delete. The pre-lock fence check above cannot see a rebind or+ // removal that completes while this Hook waits on policyCommitMu —// a stale Hook must never activate the successor.- if !s.fenceMatchesLocked(subj, fence) {+ s.mu.Lock()+ stale := !s.fenceMatchesLocked(subj, fence)+ pol := s.pendingPolicies[subj]+ s.mu.Unlock()+ if stale {return http.StatusConflict, fmt.Errorf("stale %s (fence mismatch) for subject %s", env.Hook.Name, subj)}- // Peek, do not consume: a transient nft failure must leave the- // pending policy in place so the Fastlet's retry of the same Hook- // succeeds instead of 409-ing forever with the subject deny-first.- pol := s.pendingPolicy(subj)if pol == nil {// No pending policy: SET_BINDING has not landed (protocol// ordering violation) or the binding was removed. Fail closed —diff --git a/components/egress/fastsandbox_server_test.go b/components/egress/fastsandbox_server_test.goindex cb1370bd9..dd9637fa9 100644--- a/components/egress/fastsandbox_server_test.go+++ b/components/egress/fastsandbox_server_test.go@@ -511,6 +511,38 @@ func TestActionsHookForUnregisteredSubjectConflict(t *testing.T) {}// ---------------------------------------------------------------------------+// TestHookRemoveConcurrentNoRace (review: subjFence map race): a+// data-plane-ready hook (holding policyCommitMu, reading subjFence under+// s.mu) racing terminal cleanup (which deletes subjFence under s.mu without+// policyCommitMu) must be data-race-free and converge to a consistent+// outcome. Run under -race in CI.+func TestHookRemoveConcurrentNoRace(t *testing.T) {+ srv, reg, _ := fastSandboxTestServer(t)+ s := subject.FromSandboxUID(policyTestUID)++ rec := doAction(t, srv, bindBody(t, policyTestUID, vaultTestIP, testFenceR, testFenceA, 1, strPtr(policyAllow())))+ require.Equal(t, http.StatusOK, rec.Code)+ rec = doAction(t, srv, hookBody(t, policyTestUID, testFenceR, testFenceA, constants.HookRuntimeReady))+ require.Equal(t, http.StatusOK, rec.Code)++ done := make(chan struct{})+ go func() {+ defer close(done)+ r := doAction(t, srv, removeBody(t, policyTestUID, testFenceR, testFenceA))+ assert.Equal(t, http.StatusOK, r.Code)+ }()+ rec = doAction(t, srv, hookBody(t, policyTestUID, testFenceR, testFenceA, constants.HookDataPlaneReady))+ hookCode := rec.Code+ <-done++ // The removal always completes (we waited), so the converged terminal+ // state is absent regardless of the hook's outcome (200 activation+ // first → removal reverts; removal first → the hook is a 404/409).+ assert.Contains(t, []int{http.StatusOK, http.StatusNotFound, http.StatusConflict}, hookCode)+ _, present := reg.Get(s)+ assert.False(t, present, "terminal cleanup wins: the subject is gone")+}+// Stale-subject takeover on slot reuse (OSEP-0022)// ---------------------------------------------------------------------------