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

Closed
SkippySteve wants to merge 1 commit from upstream-pr/344 into main
SkippySteve commented 2026-11-24 02:26:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SkippySteve on 2026-11-24T02:26:14Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573219709 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/344). Originally posted by [SkippySteve](https://github.com/SkippySteve) on 2026-11-24T02:26:14Z. --- `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.
upstream-sync closed this pull request 2026-11-24 06:52:30 +00:00
CesarLopesO commented 2026-11-24 06:55:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by CesarLopesO on 2026-11-24T02:30:37Z.


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!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7120214844 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/344#issuecomment-7120214844). Originally posted by [CesarLopesO](https://github.com/CesarLopesO) on 2026-11-24T02:30:37Z. --- ## 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!
SkippySteve commented 2026-11-24 06:55:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SkippySteve on 2026-11-24T02:32:32Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7120245533 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/344#issuecomment-7120245533). Originally posted by [SkippySteve](https://github.com/SkippySteve) on 2026-11-24T02:32:32Z. --- Superseded by #287, which now carries this commit (relocation overflow in prepare) alongside the other loader/PE tests.

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