[PR #2041] runtime: guard base_reservation parsing and VirtualQuery loop overflow #2048

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2041
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-base-reservation-overflow-and-parse-guards


📝 Commits (1)

  • 2e8a92d runtime: guard base_reservation parsing and VirtualQuery loop overflow

📊 Changes

2 files changed (+48 additions, -9 deletions)

View changed files

📝 runtime/src/base_reservation.h (+42 -9)
📝 runtime/test/pe_loader_test.cpp (+6 -0)

📄 Description

Coordination item: RT-BASE-RESERVATION-OVERFLOW-AND-PARSE-GUARDS (claimed on #1608).

Why

runtime/src/base_reservation.h (used on macOS CrossOver / arm64 Wine FEX to keep a relocation-less PE image's preferred base free) had several edge-case parsing and arithmetic bugs:

  1. needed() looped with for (uint64_t at = r.base; at < r.base + r.size;) and stepped at = info.BaseAddress + info.RegionSize. If r.base + r.size wrapped 64-bit address space, the loop skipped immediately; if info.RegionSize == 0 or BaseAddress + RegionSize wrapped around UINT64_MAX, at failed to advance and looped infinitely.
  2. inherited() used swscanf_s(value, L"%llx:%llx", ...) which accepted trailing garbage ("140000000:30000000junk"), wrapped >64-bit hex digits, and accepted ranges where base + size wrapped 64 bits.
  3. commit_reserved() did not reject size == 0 or base + size wraparound before querying/committing.

Changes

  • runtime/src/base_reservation.h:
    • Extract strict parse_range(const wchar_t*) that parses <hex>:<hex> with exact full-string match, overflow-checked hex accumulation, non-zero size, and base <= UINT64_MAX - size.
    • Guard size == 0 and base > UINT64_MAX - size in commit_reserved().
    • Use overflow-free subtraction against info.RegionSize in needed() so at strictly advances and never wraps or infinite-loops.
  • runtime/test/pe_loader_test.cpp: expand test_inherited_reservation() to verify rejection of trailing garbage, base + size wraparound, and >64-bit hex overflow.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (CPU-only verification on macOS arm64 using local Win32 VirtualQuery/env stubs; Windows CI runs full pe_loader_test).
  • Compiled and ran base_reservation.h test harness (including synthetic PE32+ files, multi-region free walk, pathological RegionSize == 0 non-infinite-loop test, and wrapping ImageBase) with clang++ -std=c++20 -Wall -Wextra -Werror:
    • ASan+UBSan (-O1 -g -fsanitize=address,undefined): base_reservation_harness: OK (0 fails).
    • TSan (-O1 -g -fsanitize=thread): base_reservation_harness: OK (0 fails).
    • Release (-O2): base_reservation_harness: OK (0 fails).
  • python3 -I scripts/check_no_game_data.py runtime/src/base_reservation.h runtime/test/pe_loader_test.cpp: 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/2041 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-base-reservation-overflow-and-parse-guards` --- ### 📝 Commits (1) - [`2e8a92d`](https://github.com/ubiquitarianism/leonida/commit/2e8a92d72ab941979c7db8f1dd26c05d289e0909) runtime: guard base_reservation parsing and VirtualQuery loop overflow ### 📊 Changes **2 files changed** (+48 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/base_reservation.h` (+42 -9) 📝 `runtime/test/pe_loader_test.cpp` (+6 -0) </details> ### 📄 Description Coordination item: `RT-BASE-RESERVATION-OVERFLOW-AND-PARSE-GUARDS` (claimed on #1608). ### Why `runtime/src/base_reservation.h` (used on macOS CrossOver / arm64 Wine FEX to keep a relocation-less PE image's preferred base free) had several edge-case parsing and arithmetic bugs: 1. `needed()` looped with `for (uint64_t at = r.base; at < r.base + r.size;)` and stepped `at = info.BaseAddress + info.RegionSize`. If `r.base + r.size` wrapped 64-bit address space, the loop skipped immediately; if `info.RegionSize == 0` or `BaseAddress + RegionSize` wrapped around `UINT64_MAX`, `at` failed to advance and looped infinitely. 2. `inherited()` used `swscanf_s(value, L"%llx:%llx", ...)` which accepted trailing garbage (`"140000000:30000000junk"`), wrapped >64-bit hex digits, and accepted ranges where `base + size` wrapped 64 bits. 3. `commit_reserved()` did not reject `size == 0` or `base + size` wraparound before querying/committing. ### Changes - `runtime/src/base_reservation.h`: - Extract strict `parse_range(const wchar_t*)` that parses `<hex>:<hex>` with exact full-string match, overflow-checked hex accumulation, non-zero size, and `base <= UINT64_MAX - size`. - Guard `size == 0` and `base > UINT64_MAX - size` in `commit_reserved()`. - Use overflow-free subtraction against `info.RegionSize` in `needed()` so `at` strictly advances and never wraps or infinite-loops. - `runtime/test/pe_loader_test.cpp`: expand `test_inherited_reservation()` to verify rejection of trailing garbage, `base + size` wraparound, and >64-bit hex overflow. ### Verification - AI-assisted: yes (Antigravity agent by Google DeepMind). - GPUs tested: none (CPU-only verification on macOS arm64 using local Win32 `VirtualQuery`/env stubs; Windows CI runs full `pe_loader_test`). - Compiled and ran `base_reservation.h` test harness (including synthetic PE32+ files, multi-region free walk, pathological `RegionSize == 0` non-infinite-loop test, and wrapping `ImageBase`) with `clang++ -std=c++20 -Wall -Wextra -Werror`: - ASan+UBSan (`-O1 -g -fsanitize=address,undefined`): `base_reservation_harness: OK (0 fails)`. - TSan (`-O1 -g -fsanitize=thread`): `base_reservation_harness: OK (0 fails)`. - Release (`-O2`): `base_reservation_harness: OK (0 fails)`. - `python3 -I scripts/check_no_game_data.py runtime/src/base_reservation.h runtime/test/pe_loader_test.cpp`: 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#2048
No description provided.