runtime: free unused thunk on zero redirects and harden code_hooks bounds #2056
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!2056
Loading…
Reference in a new issue
No description provided.
Delete branch "rt/code-hooks-thunk-leak-and-granularity-guards"
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
Claim ID:
RT-CODE-HOOKS-THUNK-LEAK-AND-GRANULARITY-GUARDSSummary
Fixes a thunk + unwind-table leak and hardens bounds checks in
runtime/src/code_hooks.h:redirect_image_callsredirects 0 branches: Previouslyfree_thunk(thunk)was only called in theVirtualProtectfailure path whenr.redirected == 0, so a valid executable target with no directE8/E9callers leaked both its executable 4 KiB page and its globalRtlAddFunctionTableentry permanently. Callingif (r.redirected == 0) free_thunk(thunk);after the section loop closes the leak.alloc_near: Falls back to0x10000ifdwAllocationGranularity == 0(preventing an infinite loop ondelta += 0), alignscenterwithout assuming power-of-two granularity, and guardsdeltaaddition against wrappingspan.finish_thunk: Rejects nullt,code_size == 0,code_size > kThunkUnwindOffset, andunwind.size() > kThunkFunctionOffset - kThunkUnwindOffset(freeing the page rather than overwritingRUNTIME_FUNCTION).rel32distance check ande_lfanewbound: Replaces signedintptr_tdifference casting inredirect_direct_brancheswith an unsigned magnitude check against[INT32_MIN, INT32_MAX], and guardslfanew > UINT32_MAX - sizeof(IMAGE_NT_HEADERS64)before formingnt.runtime/test/code_hooks_test.cppwith afinish_thunk(nullptr, ...)check and a zero-caller synthetic PE64 image test verifyingRtlLookupFunctionEntryreturns null for the freed unused thunk.Verification
code_hookspending)code_hooks.h):clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror: PASS (failures=0)clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror: PASS (failures=0)clang++ -std=c++20 -O2 -Wall -Wextra -Werror: PASS (failures=0)main: confirmed both leaks whenredirected == 0(live thunk page + liveRUNTIME_FUNCTIONtable left registered,main_exit=1).python3 -I scripts/check_no_game_data.pyclean;python3 -I tests/test_text_write_line_endings.py(3/3 OK).Verified on Windows 11 (
10.0.26200.9974) with MSVC19.44.35228(x64,/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) againstmain4a54aedcb+9fc1862fa(claim#2570#issuecomment-7238486276):runtime/src/code_hooks.h,runtime/test/code_hooks_test.cpp); zero-initializesSYSTEM_INFO, handles arbitrary non-power-of-twodwAllocationGranularityanddeltastep bounds inalloc_near, validatesfinish_thunknull/size bounds (freeington invalid size), computes unsignedrel32displacements without UB, boundse_lfanew, and frees unused thunks + unwind tables whenr.redirected == 0.check_no_game_data.pyclean.4a54aedcb+ PR test):code_hooks_test.execrashes with0xc0000005(STATUS_ACCESS_VIOLATIONonfinish_thunk(nullptr, ...)).4a54aedcb+9fc1862famerge): Merges cleanly (rc=0), compiles with0/W4warnings, andcode_hooks_test.exepasses (code_hooks_test: 62 checks, 0 failures,rc=0).AI-assisted.
Red/green: On main code_hooks_test crashes with a segmentation fault, and with the PR ctest -R "^code_hooks$" passes.
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 head9fc1862fa; green run in a merge package of 16 PRs, no new ctest failure vs mainctest -R "^code_hooks$"AI-assisted (Claude Code, Claude Opus 5.5).