[PR #2056] runtime: free unused thunk on zero redirects and harden code_hooks bounds #2063

Open
opened 2026-11-26 21:19:54 +01:00 by 4ld0 · 0 comments
Owner

📋 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)

  • 9fc1862 runtime: 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-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).

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2056 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt/code-hooks-thunk-leak-and-granularity-guards` --- ### 📝 Commits (1) - [`9fc1862`](https://github.com/ubiquitarianism/leonida/commit/9fc1862fadbd2907fdcecb993eb98efd790d7c5e) runtime: free unused thunk on zero redirects and harden code_hooks bounds ### 📊 Changes **2 files changed** (+54 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/code_hooks.h` (+27 -7) 📝 `runtime/test/code_hooks_test.cpp` (+27 -0) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#2063
No description provided.