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

Closed
Dukestar69 wants to merge 1 commit from upstream-pr/3066 into main
Dukestar69 commented 2026-11-26 06:02:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:02:25Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6620593438 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3066). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T06:02:25Z. --- ## 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).
upstream-sync closed this pull request 2026-11-26 07:01:35 +00:00
fuzeofficlal commented 2026-11-26 07:01:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T06:20:39Z.

Review: APPROVED


Verified at d2a67c31 on Windows 11 x64 (Rust 1.98.1 MSVC):

  • Checked tls::near: filtering h != 0 on the downward probe prevents VirtualAlloc(NULL, ...) when base is an exact multiple of GRANULE and (k + 1) * GRANULE == base (such as 0x10000), and switching above + above + k * GRANULE to checked arithmetic prevents wraparound near the top of the address space. (Non-blocking note: filtering != 0 on the loop hint itself instead of only the downward arm would also guard the degenerate base == 0, size == 0 input, though real mapped images are never at 0.)
  • cargo test -p victory-loader --locked: 40 passed, 0 failed (including tls::tests::near_never_uses_null_hint_outside_rel32_reach).
  • cargo clippy -p victory-loader --locked --all-targets -- -D warnings: clean.

Looks good.

<!-- greyfinch-sync:Marvisak/VIctory:review:6069440972 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3066#pullrequestreview-6069440972). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T06:20:39Z. **Review:** APPROVED --- Verified at `d2a67c31` on Windows 11 x64 (Rust 1.98.1 MSVC): - Checked `tls::near`: filtering `h != 0` on the downward probe prevents `VirtualAlloc(NULL, ...)` when `base` is an exact multiple of `GRANULE` and `(k + 1) * GRANULE == base` (such as `0x10000`), and switching `above` + `above + k * GRANULE` to checked arithmetic prevents wraparound near the top of the address space. (Non-blocking note: filtering `!= 0` on the loop `hint` itself instead of only the downward arm would also guard the degenerate `base == 0, size == 0` input, though real mapped images are never at `0`.) - `cargo test -p victory-loader --locked`: 40 passed, 0 failed (including `tls::tests::near_never_uses_null_hint_outside_rel32_reach`). - `cargo clippy -p victory-loader --locked --all-targets -- -D warnings`: clean. Looks good.
kotekompotek commented 2026-11-26 07:01:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T06:31:57Z.

Review: APPROVED


Verified: base.checked_sub could yield Some(0) for granule-aligned bases -> VirtualAlloc(NULL) picks any 64-bit address outside rel32 reach; the filter plugs it and the checked_add path covers end/above overflow. The test's rel32 assertion exercises the real constraint.

<!-- greyfinch-sync:Marvisak/VIctory:review:6069530614 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3066#pullrequestreview-6069530614). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T06:31:57Z. **Review:** APPROVED --- Verified: base.checked_sub could yield Some(0) for granule-aligned bases -> VirtualAlloc(NULL) picks any 64-bit address outside rel32 reach; the filter plugs it and the checked_add path covers end/above overflow. The test's rel32 assertion exercises the real constraint.
Marvisak commented 2026-11-26 07:01:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:18:15Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184296669 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3066#issuecomment-7184296669). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T06:18:15Z. --- <!-- 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)_
Dukestar69 commented 2026-11-26 07:01:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:45:36Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184753458 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3066#issuecomment-7184753458). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T06:45:36Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3064
No description provided.