Loader: a relocation past the top of the address space is not wrapped #344

Closed
github-bot wants to merge 1 commit from gh-pr/344/head into gh-pr/344/base
Owner

@⁠SkippySteve on GitHub · 2026-11-24 02:26 UTC · original
SkippySteve/loader-reloc-overflow → main · +65 −9 · 1 files · 1 commits · closed without merging

prepare() walks the base relocation table to find the DIR64 entry that patches the TLS callback field, so it can neutralise it. The walk added each entry's page offset to its block's page RVA unchecked:

out.push((entry, page + u32::from(e & 0xFFF)));

A damaged block whose page RVA sits near u32::MAX (e.g. 0xFFFF_FFF0 with an entry at +0x10) panics with attempt to add with overflow in debug builds. In release it wraps to a low RVA. If that RVA happens to equal the TLS callback field, prepare() zeroes an unrelated relocation entry.

Change (crates/victory-loader/src/prepare.rs only)

  • The block walk becomes dir64_entries(bytes, start, end) over plain bytes. It uses checked_add and leaves out targets that don't fit in an RVA. dir64_relocations still finds the directory and delegates to it.
  • The directory's end is start.saturating_add(size).
  • The TLS callback field's RVA (directory_rva + 12) uses checked_add. On overflow it returns PrepareError::Patch instead of wrapping.

Behaviour for well-formed images doesn't change.

Tests

Two new unit tests build relocation blocks in memory and need no PE, so they run on every host:

  • dir64_entries_walk_blocks_and_skip_other_types: walks several blocks, skips ABSOLUTE and HIGHLOW entries, and stops at a block that claims more than the directory holds.
  • dir64_entries_drop_targets_past_the_top_of_the_address_space: entries that fit up to 0xFFFF_FFFF are kept, entries that overflow are dropped, and an end past the buffer stops at the bytes that are there.

On the old code the second test fails with attempt to add with overflow.

On Linux, cargo test -p victory-loader --lib dir64 passes 2/2, and cargo clippy -p victory-loader --all-targets -- -D warnings is clean. rustfmt --check on the file is clean too. The three existing prepare tests read current_exe() as a PE, so they already fail on Linux on main (#235 gates them). This PR doesn't change them.

**@⁠SkippySteve** on GitHub · 2026-11-24 02:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/344) `SkippySteve/loader-reloc-overflow` → `main` · +65 −9 · 1 files · 1 commits · closed without merging `prepare()` walks the base relocation table to find the `DIR64` entry that patches the TLS callback field, so it can neutralise it. The walk added each entry's page offset to its block's page RVA unchecked: ```rust out.push((entry, page + u32::from(e & 0xFFF))); ``` A damaged block whose page RVA sits near `u32::MAX` (e.g. `0xFFFF_FFF0` with an entry at `+0x10`) panics with `attempt to add with overflow` in debug builds. In release it wraps to a low RVA. If that RVA happens to equal the TLS callback field, `prepare()` zeroes an unrelated relocation entry. ## Change (`crates/victory-loader/src/prepare.rs` only) - The block walk becomes `dir64_entries(bytes, start, end)` over plain bytes. It uses `checked_add` and leaves out targets that don't fit in an RVA. `dir64_relocations` still finds the directory and delegates to it. - The directory's end is `start.saturating_add(size)`. - The TLS callback field's RVA (`directory_rva + 12`) uses `checked_add`. On overflow it returns `PrepareError::Patch` instead of wrapping. Behaviour for well-formed images doesn't change. ## Tests Two new unit tests build relocation blocks in memory and need no PE, so they run on every host: - `dir64_entries_walk_blocks_and_skip_other_types`: walks several blocks, skips ABSOLUTE and HIGHLOW entries, and stops at a block that claims more than the directory holds. - `dir64_entries_drop_targets_past_the_top_of_the_address_space`: entries that fit up to `0xFFFF_FFFF` are kept, entries that overflow are dropped, and an `end` past the buffer stops at the bytes that are there. On the old code the second test fails with `attempt to add with overflow`. On Linux, `cargo test -p victory-loader --lib dir64` passes 2/2, and `cargo clippy -p victory-loader --all-targets -- -D warnings` is clean. `rustfmt --check` on the file is clean too. The three existing `prepare` tests read `current_exe()` as a PE, so they already fail on Linux on `main` (#235 gates them). This PR doesn't change them.
prepare() walks the base relocations to find the one that patches the TLS
callback field. It added each DIR64 entry's page offset to the block's
page RVA unchecked: a damaged block near u32::MAX panicked with an
overflow in debug builds and, in release, wrapped to a low RVA that could
match the field and zero an unrelated relocation entry.

Targets that do not fit in an RVA are now left out, the TLS field's own
RVA is computed with checked_add, and the block walk is split into
dir64_entries over plain bytes so it is covered by tests on every host.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:00:00 +00:00
github-bot closed this pull request 2026-11-26 04:00:00 +00:00
Author
Owner

@⁠CesarLopesO on GitHub · 2026-11-24 02:30 UTC · original

Independent validation — Debian 13 (Trixie)

I tested the current PR head dfefedd locally on Debian 13.

Results

  • cargo test --locked -p victory-loader --lib dir64 — 2 passed, 0 failed (3 filtered out).
  • cargo clippy --locked -p victory-loader --all-targets -- -D warnings — passed with no warnings.
  • rustfmt --edition 2024 --check crates/victory-loader/src/prepare.rs — passed.
  • cargo xtask policy — passed (151 files).

The two new dir64_entries tests pass on Linux, including the overflow-boundary regression case. I did not run the full workspace test suite as part of this validation.

No game files or game data were used.

Thanks for the fix!

**@⁠CesarLopesO** on GitHub · 2026-11-24 02:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/344#issuecomment-7120214844) ## Independent validation — Debian 13 (Trixie) I tested the current PR head `dfefedd` locally on Debian 13. **Results** - `cargo test --locked -p victory-loader --lib dir64` — 2 passed, 0 failed (3 filtered out). - `cargo clippy --locked -p victory-loader --all-targets -- -D warnings` — passed with no warnings. - `rustfmt --edition 2024 --check crates/victory-loader/src/prepare.rs` — passed. - `cargo xtask policy` — passed (151 files). The two new `dir64_entries` tests pass on Linux, including the overflow-boundary regression case. I did not run the full workspace test suite as part of this validation. No game files or game data were used. Thanks for the fix!
Author
Owner

@⁠SkippySteve on GitHub · 2026-11-24 02:32 UTC · original

Superseded by #287, which now carries this commit (relocation overflow in prepare) alongside the other loader/PE tests.

**@⁠SkippySteve** on GitHub · 2026-11-24 02:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/344#issuecomment-7120245533) Superseded by #287, which now carries this commit (relocation overflow in prepare) alongside the other loader/PE tests.

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!344
No description provided.