Skip to content

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

Closed
SkippySteve wants to merge 1 commit into
Marvisak:mainfrom
SkippySteve:SkippySteve/loader-reloc-overflow
Closed

SkippySteve wants to merge 1 commit into
Marvisak:mainfrom
SkippySteve:SkippySteve/loader-reloc-overflow

Conversation

@SkippySteve

Copy link
Copy Markdown

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.

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.
@CesarLopesO

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants