bcrypt_rng: BCryptCloseAlgorithmProvider waits for a BCryptGenRandom in flight (RT-BCRYPT-GEN-CLOSE-RACE) #2529
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!2529
Loading…
Reference in a new issue
No description provided.
Delete branch "rt-bcrypt-gen-close-race"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request successfully merged and closed
Claimed in the coordination issue as: RT-BCRYPT-GEN-CLOSE-RACE (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231103456)
What and why
This fixes item 3 of kevincommits' finding in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230881417), in
runtime/shims/bcrypt_rng.cpp.gen_random()looked up the handle underg_lock(shared), released the lock, and only then called the hostBCryptGenRandomwith the host handle. In that window, a concurrentclose_algorithm_provider()could unlink the wrapper, close the host handle and free the wrapper, while the host call was still using the handle. That breaks the guarantee in the file's header comment.The fix:
gen_random()keepsg_lockshared across both the lookup and the host call.close_algorithm_provider()closes the host handle under the exclusive lock.So a close waits for any call in flight, and no later call can find the closed handle. Generate calls still run side by side, because the lock is shared.
The other paths:
BCRYPT_USE_SYSTEM_PREFERRED_RNG) uses no handle, so it takes no lock.fill_seeded()takes the lock exclusive.Tested
A test hook,
swap_host_functions_for_test(), letsbcrypt_rng_testinstall fake host functions. The fake generate call blocks until the test releases it, and the fake close records whether a generate call was still running.The new case:
BCryptGenRandomand waits until it is inside the host call.BCryptCloseAlgorithmProvideron the same handle from another thread.With main's
bcrypt_rng.cppplus only the hook, 2 checks fail. With the fix,bcrypt_rngandbcrypt_rng_seededpass.GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU-only test)
AI-assisted: yes (Claude Code)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code
Reviewed at head
b5844c443on Windows 11 (MSVC 202219.44).Verification
bcrypt_rng_testandgta6rt.bcrypt_rng_test(5 consecutive runs): 27/27 checks passed on all 5 runs.Notes
BCryptGenRandomand taking the exclusive lock inBCryptCloseAlgorithmProviderfixes the provider-close UAF race cleanly (bcrypt_rng.cpp:156-232). Seeded generation drops the shared lock before acquiring the exclusive PRNG lock and does not touch the provider handle afterwards.bcrypt_rng_test.cpp:203-221) waits heuristically rather than signaling that the closer entered; and the test-only host-function swap assumes quiescent opens / system-preferred calls. None of these block the lifetime fix.Approve.
AI-assisted review + local MSVC verification.
#2529 | head b5844c443f1b1323fa95f873e3d60eac9a0f7e1d (merged cleanly with main 09c153948386fd658fe2f999f6176efbaa8c8345) |
ctest --test-dir runtime/build -C RelWithDebInfo -R "^bcrypt_rng" --output-on-failure| red on main:bcrypt_rng_test.cppfails to link (LNK2019:swap_host_functions_for_testmissing; onmain,lookup_live_handlereleasedg_lockbeforeg_host_genran, allowing concurrentBCryptCloseAlgorithmProviderto close the host handle mid-call) | green on PR+main:2/2CTests pass (bcrypt_rng27/27checks in0.27sandbcrypt_rng_seeded21/21checks in0.03s, plus5/5consecutive concurrency stress runs ofbcrypt_rng_test);check_no_game_data.py0violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yesReviewed
b5844c443and verified thebcrypt_rngsuites on Windows 11.Code audit:
runtime/shims/bcrypt_rng.cpp: holdingSharedLockacross bothlookup_live_handle()andhost_random()whileclose_algorithm_provider()unlinks and callsg_host_close()underAcquireSRWLockExclusive(&g_lock)cleanly closes the concurrent generate-vs-close race;fill_seeded()runs after releasing the shared hold so it cannot self-deadlock on the exclusive lock.@timbot103noted, replacing the fixed 200 ms sleep inbcrypt_rng_test.cppwith explicit synchronization on close entry would make the race test even more deterministic, though the production fix itself is solid.Verification (Windows 11, MSVC 19.44, Ninja Release):
bcrypt_rng_test.ctest -R '^bcrypt_rng': 2/2 passed (bcrypt_rng,bcrypt_rng_seeded).python -I scripts/check_no_game_data.py: passed.AI-assisted (Antigravity, Gemini).
Red/green: On main bcrypt_rng_test does not link because the test hook swap_host_functions_for_test is missing and main releases g_lock before the host BCryptGenRandom call, and with the PR ctest -R "^bcrypt_rng$" passes, including the check that BCryptCloseAlgorithmProvider waits for an in-flight BCryptGenRandom.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
Verified by OreoFR
c1ecf04ed+ PR headb5844c443; green run in a merge package of 11 PRs, no new ctest failure vs mainctest -R "^bcrypt_rng$"AI-assisted (Claude Code, Claude Opus 5.5).