Loader: never pass NULL address hint to VirtualAlloc in tls::near #3066

Closed
github-bot wants to merge 1 commit from github-bot/gh-3066 into main AGit
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 06:02 UTC · original
Dukestar69:fix/tls-near-null-hint → main · +23 −3 · 1 files · 1 commits · closed without merging

Summary

Prevents tls::near from passing a NULL (0) address hint to VirtualAlloc and uses checked arithmetic for upper candidate addresses so allocated TLS thunk pages are guaranteed to stay within ±2 GiB rel32 reach of the image.

Why

In crates/victory-loader/src/tls.rs, near searched downward with base.checked_sub((k + 1) * GRANULE). Whenever base is a multiple of GRANULE (0x1_0000) and <= 1 GiB, (k + 1) * GRANULE == base yields Some(0). Passing 0 (NULL) as lpAddress tells VirtualAlloc to ignore the hint and allocate anywhere in the 64-bit address space (e.g., 0x1d068de0000 for base = 0x10000), which patch then silently truncates via rel as i32 into an invalid call rel32 target.

Filtering out 0 (.filter(|&h| h != 0)) and using checked_add / checked_next_multiple_of for the upper hints avoids both the NULL-hint escape and high-address overflow.

Verification

  • Added unit test tls::tests::near_never_uses_null_hint_outside_rel32_reach:
    • Failed on 63bcca4 (allocated at 0x1d068de0000, outside rel32 reach of 0x10000).
    • Passes with fix.
  • cargo test -p victory-loader -- --test-threads=1: 40 passed, 0 failed (36 unit + 4 integration).
  • cargo clippy -p victory-loader --all-targets -- -D warnings: clean.
  • rustfmt --edition 2024 --check crates/victory-loader/src/tls.rs: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 06:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/3066) `Dukestar69:fix/tls-near-null-hint` → `main` · +23 −3 · 1 files · 1 commits · closed without merging ## Summary Prevents `tls::near` from passing a `NULL` (`0`) address hint to `VirtualAlloc` and uses checked arithmetic for upper candidate addresses so allocated TLS thunk pages are guaranteed to stay within ±2 GiB `rel32` reach of the image. ### Why In `crates/victory-loader/src/tls.rs`, `near` searched downward with `base.checked_sub((k + 1) * GRANULE)`. Whenever `base` is a multiple of `GRANULE` (`0x1_0000`) and `<= 1 GiB`, `(k + 1) * GRANULE == base` yields `Some(0)`. Passing `0` (`NULL`) as `lpAddress` tells `VirtualAlloc` to ignore the hint and allocate anywhere in the 64-bit address space (e.g., `0x1d068de0000` for `base = 0x10000`), which `patch` then silently truncates via `rel as i32` into an invalid `call rel32` target. Filtering out `0` (`.filter(|&h| h != 0)`) and using `checked_add` / `checked_next_multiple_of` for the upper hints avoids both the `NULL`-hint escape and high-address overflow. ### Verification - Added unit test `tls::tests::near_never_uses_null_hint_outside_rel32_reach`: - Failed on `63bcca4` (`allocated at 0x1d068de0000, outside rel32 reach of 0x10000`). - Passes with fix. - `cargo test -p victory-loader -- --test-threads=1`: 40 passed, 0 failed (36 unit + 4 integration). - `cargo clippy -p victory-loader --all-targets -- -D warnings`: clean. - `rustfmt --edition 2024 --check crates/victory-loader/src/tls.rs`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:27:57 +00:00
github-bot closed this pull request 2026-11-26 06:55:42 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 06:18 UTC · original

Automated triage for the maintainers.

Proposed close: guard for a case that does not occur. In crates/victory-loader/src/tls.rs (near), the search alternates between a hint below the image and one above it, and returns at the first free page. For the downward hint to reach 0, the title image would need to sit at or below 1 GiB, with every 64 KiB granule below it taken, and the same number of granules just above its end taken too. The only caller is tls::redirect, with the image the OS loaded. The new test only reaches the NULL hint by passing base = 0x10000, where the very first downward hint is 0, which is not an address an image is loaded at. The checked_add and checked_next_multiple_of on the upward hint likewise guard against overflowing 64-bit user space, which cannot happen.

This PR will be closed in 48 hours unless the author pushes a change, or a maintainer removes the label. A report of the title's image actually landing that low would change this.

CI: no checks ran on this head.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 06:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/3066#issuecomment-7184296669) <!-- victory-triage v1 head=d2a67c317d6eb5cea65d804c1ffb1f1202d89e94 verdict=proposed-close at=2026-11-26T06:21:30Z --> Automated triage for the maintainers. **Proposed close: guard for a case that does not occur.** In `crates/victory-loader/src/tls.rs` (`near`), the search alternates between a hint below the image and one above it, and returns at the first free page. For the downward hint to reach 0, the title image would need to sit at or below 1 GiB, with every 64 KiB granule below it taken, and the same number of granules just above its end taken too. The only caller is `tls::redirect`, with the image the OS loaded. The new test only reaches the NULL hint by passing `base = 0x10000`, where the very first downward hint is 0, which is not an address an image is loaded at. The `checked_add` and `checked_next_multiple_of` on the upward hint likewise guard against overflowing 64-bit user space, which cannot happen. This PR will be closed in 48 hours unless the author pushes a change, or a maintainer removes the label. A report of the title's image actually landing that low would change this. CI: no checks ran on this head. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 06:45 UTC · original

Fair point from triage — a real 64-bit PE image will not sit at or below 1 GiB with the entire +1 GiB range above it occupied. Closing to keep the queue clean. AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 06:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/3066#issuecomment-7184753458) Fair point from triage — a real 64-bit PE image will not sit at or below 1 GiB with the entire +1 GiB range above it occupied. Closing to keep the queue clean. AI-assisted (Gemini 4 Neon).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
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
github/Marvisak--VIctory!3066
No description provided.