Repository navigation
Loader: a relocation past the top of the address space is not wrapped - #344
Closed
SkippySteve wants to merge 1 commit into
Closed
SkippySteve wants to merge 1 commit into
SkippySteve wants to merge 1 commit into
Conversation
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.
Independent validation — Debian 13 (Trixie)I tested the current PR head Results
The two new No game files or game data were used. Thanks for the fix! |
Author
|
Superseded by #287, which now carries this commit (relocation overflow in prepare) alongside the other loader/PE tests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
prepare()walks the base relocation table to find theDIR64entry 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:A damaged block whose page RVA sits near
u32::MAX(e.g.0xFFFF_FFF0with an entry at+0x10) panics withattempt to add with overflowin 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.rsonly)dir64_entries(bytes, start, end)over plain bytes. It useschecked_addand leaves out targets that don't fit in an RVA.dir64_relocationsstill finds the directory and delegates to it.start.saturating_add(size).directory_rva + 12) useschecked_add. On overflow it returnsPrepareError::Patchinstead 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 to0xFFFF_FFFFare kept, entries that overflow are dropped, and anendpast 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 dir64passes 2/2, andcargo clippy -p victory-loader --all-targets -- -D warningsis clean.rustfmt --checkon the file is clean too. The three existingpreparetests readcurrent_exe()as a PE, so they already fail on Linux onmain(#235 gates them). This PR doesn't change them.