runtime: free unused thunk on zero redirects and harden code_hooks bounds #2056

Open
github-bot wants to merge 1 commit from github-bot/gh-2056 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 19:09 UTC · original
IlyassRS3:rt/code-hooks-thunk-leak-and-granularity-guards → main · +54 −7 · 2 files · 1 commits · open

Claim ID: RT-CODE-HOOKS-THUNK-LEAK-AND-GRANULARITY-GUARDS

Summary

Fixes a thunk + unwind-table leak and hardens bounds checks in runtime/src/code_hooks.h:

  1. Free unused thunk when redirect_image_calls redirects 0 branches: Previously free_thunk(thunk) was only called in the VirtualProtect failure path when r.redirected == 0, so a valid executable target with no direct E8/E9 callers leaked both its executable 4 KiB page and its global RtlAddFunctionTable entry permanently. Calling if (r.redirected == 0) free_thunk(thunk); after the section loop closes the leak.
  2. Guard zero/non-power-of-two allocation granularity in alloc_near: Falls back to 0x10000 if dwAllocationGranularity == 0 (preventing an infinite loop on delta += 0), aligns center without assuming power-of-two granularity, and guards delta addition against wrapping span.
  3. Validate thunk and unwind buffer bounds in finish_thunk: Rejects null t, code_size == 0, code_size > kThunkUnwindOffset, and unwind.size() > kThunkFunctionOffset - kThunkUnwindOffset (freeing the page rather than overwriting RUNTIME_FUNCTION).
  4. Overflow-free rel32 distance check and e_lfanew bound: Replaces signed intptr_t difference casting in redirect_direct_branches with an unsigned magnitude check against [INT32_MIN, INT32_MAX], and guards lfanew > UINT32_MAX - sizeof(IMAGE_NT_HEADERS64) before forming nt.
  5. Regression tests: Extends runtime/test/code_hooks_test.cpp with a finish_thunk(nullptr, ...) check and a zero-caller synthetic PE64 image test verifying RtlLookupFunctionEntry returns null for the freed unused thunk.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind)
  • GPUs tested: none (verified locally on macOS arm64 with Apple Clang 17 and minimal Win32 PE/memory stubs; Windows MSVC CI run of code_hooks pending)
  • Sanitizers & tests (standalone harness exercising 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)
    • Negative control on main: confirmed both leaks when redirected == 0 (live thunk page + live RUNTIME_FUNCTION table left registered, main_exit=1).
  • Repository checks: python3 -I scripts/check_no_game_data.py clean; python3 -I tests/test_text_write_line_endings.py (3/3 OK).
**@⁠IlyassRS3** on GitHub · 2026-11-25 19:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2056) `IlyassRS3:rt/code-hooks-thunk-leak-and-granularity-guards` → `main` · +54 −7 · 2 files · 1 commits · open Claim ID: `RT-CODE-HOOKS-THUNK-LEAK-AND-GRANULARITY-GUARDS` ### Summary Fixes a thunk + unwind-table leak and hardens bounds checks in `runtime/src/code_hooks.h`: 1. **Free unused thunk when `redirect_image_calls` redirects 0 branches:** Previously `free_thunk(thunk)` was only called in the `VirtualProtect` failure path when `r.redirected == 0`, so a valid executable target with no direct `E8`/`E9` callers leaked both its executable 4 KiB page and its global `RtlAddFunctionTable` entry permanently. Calling `if (r.redirected == 0) free_thunk(thunk);` after the section loop closes the leak. 2. **Guard zero/non-power-of-two allocation granularity in `alloc_near`:** Falls back to `0x10000` if `dwAllocationGranularity == 0` (preventing an infinite loop on `delta += 0`), aligns `center` without assuming power-of-two granularity, and guards `delta` addition against wrapping `span`. 3. **Validate thunk and unwind buffer bounds in `finish_thunk`:** Rejects null `t`, `code_size == 0`, `code_size > kThunkUnwindOffset`, and `unwind.size() > kThunkFunctionOffset - kThunkUnwindOffset` (freeing the page rather than overwriting `RUNTIME_FUNCTION`). 4. **Overflow-free `rel32` distance check and `e_lfanew` bound:** Replaces signed `intptr_t` difference casting in `redirect_direct_branches` with an unsigned magnitude check against `[INT32_MIN, INT32_MAX]`, and guards `lfanew > UINT32_MAX - sizeof(IMAGE_NT_HEADERS64)` before forming `nt`. 5. **Regression tests:** Extends `runtime/test/code_hooks_test.cpp` with a `finish_thunk(nullptr, ...)` check and a zero-caller synthetic PE64 image test verifying `RtlLookupFunctionEntry` returns null for the freed unused thunk. ### Verification - **AI-assisted:** yes (Antigravity agent by Google DeepMind) - **GPUs tested:** none (verified locally on macOS arm64 with Apple Clang 17 and minimal Win32 PE/memory stubs; Windows MSVC CI run of `code_hooks` pending) - **Sanitizers & tests (standalone harness exercising `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`) - Negative control on `main`: confirmed both leaks when `redirected == 0` (live thunk page + live `RUNTIME_FUNCTION` table left registered, `main_exit=1`). - **Repository checks:** `python3 -I scripts/check_no_game_data.py` clean; `python3 -I tests/test_text_write_line_endings.py` (3/3 OK).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:16:21 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 19:09 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 19:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2056#pullrequestreview-6063442125) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2056/head:github-bot/gh-2056'
git switch 'github-bot/gh-2056'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2056'
git switch 'github-bot/gh-2056'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2056'
git switch 'github-bot/gh-2056'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2056'
git switch 'main'
git merge --squash 'github-bot/gh-2056'
git switch 'main'
git merge --ff-only 'github-bot/gh-2056'
git switch 'main'
git merge 'github-bot/gh-2056'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.