mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-27 00:45:53 +01:00
[PR #2056] runtime: free unused thunk on zero redirects and harden code_hooks bounds #2063
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#2063
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/2056
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open
Base:
main← Head:rt/code-hooks-thunk-leak-and-granularity-guards📝 Commits (1)
9fc1862runtime: free unused thunk on zero redirects and harden code_hooks bounds📊 Changes
2 files changed (+54 additions, -7 deletions)
View changed files
📝
runtime/src/code_hooks.h(+27 -7)📝
runtime/test/code_hooks_test.cpp(+27 -0)📄 Description
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).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.