Repository navigation
fix(api): never render a POST response from a module a concurrent delete can free - #813
Conversation
|
@codex review |
|
/ai-review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
AI Code Review — risk tier: Advisory only. A human owns the merge decision. Summary — This is a net-positive fix: it closes a real use-after-free (CWE-416) where a concurrent DELETE could free a tap/policy module before its POST response was rendered, plus two related races in policy load/remove ordering. Both the correctness and security reviews confirm the fix is applied consistently (matching lock order across TapManager/PolicyManager, responses rendered while still under the relevant lock) and is backed by concurrency tests. No issues found. 🤖 AI Code Review · run · prompts: |
|
AI Code Review — risk tier: Advisory only. A human owns the merge decision. Summary — This is a net-positive fix: POST responses for taps/policies are now rendered under the load's lock before the module can be freed by a racing DELETE, and 🤖 AI Code Review · run · prompts: |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…ete can free POST /api/v1/taps and POST /api/v1/policies built their 201 answer from the module pointers the load returned, after every manager lock was released. A DELETE of the same name landing in between freed the module, and info_json read freed memory: pktvisord crashed. - Both loads take an optional json to render each module's info_json into before it is registered, and both POST routes answer with that. A tap is rendered while it is still only the load's own. A policy is rendered once it has started, while the load still holds _load_mutex, which every tap and policy removal now takes. A POST that creates its module so always answers 201 with it as loaded, and a DELETE ordered after it removes it, as if it came after the answer. - remove_policy takes _load_mutex first, as remove_tap does. A load checked that the input it reuses existed and took it later, and attached to it before its handlers consumed it: a removal of that input's last policy in between made the load roll back and the POST answer 500 "module name '<input>' does not exist", or freed the input under the new policy, or failed the DELETE with 500 and left the policy registered. A policy DELETE now waits for a load in progress, as a tap DELETE already does, except for a policy already missing, which is reported at once, as remove_tap does. The rollback never calls remove_policy, so it cannot deadlock on the plain mutex. - The POST route bodies move to visor::api, like the other routes. - Tests race POST and DELETE of one tap and of one policy, hold a tap load and a policy load just after they register and a policy load at its input lookup to order a removal against them, and churn two policies on one input. Each crashes or fails without the fix. GatedPolicyManager can now hold a load just after a given registration, and hold_load_at_gate runs any load. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
c5cc07f to
978ae6c
Compare
LCOV of commit
|
Follows #812, which this builds on (
module_find_locked, thevisor::apiroute functions and their tests).pktvisord crashed when a
DELETEraced thePOSTthat created the same tap or policy.POST /api/v1/tapsandPOST /api/v1/policiesbuilt their 201 response from the raw pointersload_from_strreturned, after every manager lock was released. ADELETElanding in between freed the module, and the response read freed memory.With pktvisord running with
--admin-apiand a client sending POST and DELETE for the same names at once, develop crashed in 5 runs out of 5, withEXC_BAD_ACCESSinPolicy::info_jsonor in the POST /taps route.Two more races sat in the same place:
PolicyManager::loadchecked that an input existed, then fetched it, holding only_load_mutex, whichremove_policydidn't take. ADELETEof the last policy on that input in between made thePOSTroll back and answer 500module name '<input>' does not exist.remove_policysaw an input with no policies left and removed it. A load could attach a new policy to that input in between, before the new policy's handlers started, and the input was freed under it.The fix
TapManager::loadandPolicyManager::load(and theirload_from_str) take an optionaljson *info, and the routes answer 201 from it, never from the returned pointers._load_mutex, just before it is registered. A render that throws goes through the load's existing rollback, where before it answered 500 for a policy that had already been created.remove_policytakes_load_mutexfirst, asremove_tapdoes since fix(policies): delete a tap without deadlocking or racing a policy load #809. No input can be removed while a load is in progress, so the input lookup is atomic with its reuse, and an input can't be freed under a policy that's attaching to it.api::post_tapsandapi::post_policies, like the other routes in fix(api): answer 404, not 500, when a request loses a race to a delete #812, so tests can call them directly.Lock order: unchanged:
_load_mutex< policy map < tap map < input map < handler map. Rendering a policy under_load_mutextakes no manager map lock._load_mutexis taken only by the load,remove_policyandremove_tap, which never call each other, and the load's rollback doesn't take it.What a racing client sees
A
POSTthat creates its module always answers 201 with the module as loaded. ADELETEordered after it answers 200 and removes it; one ordered before it answers 404. Every outcome matches some sequential order of the two requests.Tests
In
src/tests/test_policies.cpp, each with a 180 s watchdog:In
src/tests/test_core_server.cpp, the POST routes answer 201, 409 for a duplicate name, and 422 for a tap selector that matches nothing.Before the fix: on develop plus only the route extraction, these tests crash (SIGSEGV, or SIGABRT with "Pure virtual function called!" or "mutex lock failed") or fail. The input-reuse test answers 500 every time.
Mutations, each caught by a test:
_load_mutexinremove_policy;_load_mutexis released.Verification
ctest on macOS: 24/25.
unit-tests-input-netprobefails the same way on develop on this machine, and passes in CI.e2e: pktvisord with
--admin-api, with clients sending POST, DELETE and GET of the same names at once (10 s chaos runs, a 20 s race run):Behaviour to know
loadandload_from_strstill return raw pointers, documented as freeable once the load ends, but no caller reads them any more.🤖 Generated with Claude Code