loader/prepare: use checked arithmetic and block-bounded reads for base relocations #490

Open
IlyassRS3 wants to merge 1 commit from upstream-pr/490 into main
IlyassRS3 commented 2026-11-25 01:30:12 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by IlyassRS3 on 2026-11-25T01:30:12Z.


Summary

Hardens victory-loader's PE patching pass (crates/victory-loader/src/prepare.rs) against arithmetic overflow and out-of-block reads on untrusted/malformed relocation tables and header offsets:

  1. Checked relocation RVA & table bounds (dir64_relocations):
    • page + u32::from(e & 0xFFF) previously panicked in debug builds (and wrapped in release builds, potentially zeroing an unrelated relocation) when a block had page near u32::MAX. It now uses checked_add.
    • Clamps the relocation directory end (start + size) to bytes.len() with checked_add, and checks at + 8 and at + block.
  2. Block-bounded 2-byte reads on odd relocation blocks:
    • (at + 8..at + block).step_by(2) with bytes.get(entry..entry + 2) previously read 1 byte past at + block into the next block when block was odd. Using bytes[header_end..block_end].as_chunks::<2>().0 keeps reads strictly within each block.
  3. Checked TLS field offset/RVA & write() bounds:
    • Uses checked_add for at + TLS64_ADDRESS_OF_CALLBACKS, tls.directory_rva + TLS64_ADDRESS_OF_CALLBACKS, and write()'s at + v.len().

Verification

  • AI-assisted: Yes (written and verified with AI assistance).
  • Environment: macOS arm64 (Apple M5), CPU-only, Rust 1.98.1, no Windows GPU / no game files.
  • CARGO_FEATURE_PURE=1 cargo clippy --locked -p victory-loader --all-targets --target x86_64-pc-windows-msvc -- -D warnings and --all-features: 0 warnings.
  • cargo test --locked -p xtask: 4 passed, 0 failed (including repository_passes_policy).
  • Native rustc --edition=2024 --test crates/victory-loader/src/prepare.rs (linked against host victory-pe, blake3, thiserror): both new tests (dir64_relocations_handle_overflowing_rva_and_odd_blocks, write_rejects_overflowing_offset) passed.
  • Mutation check on unpatched upstream/main (a803c87): both new tests panic with attempt to add with overflow on unpatched prepare.rs (dir64_relocations line 151 and write line 170).
<!-- greyfinch-sync:fxceless/victory-fork:issue:6595034027 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/490). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T01:30:12Z. --- ### Summary Hardens `victory-loader`'s PE patching pass (`crates/victory-loader/src/prepare.rs`) against arithmetic overflow and out-of-block reads on untrusted/malformed relocation tables and header offsets: 1. **Checked relocation RVA & table bounds (`dir64_relocations`)**: - `page + u32::from(e & 0xFFF)` previously panicked in debug builds (and wrapped in release builds, potentially zeroing an unrelated relocation) when a block had `page` near `u32::MAX`. It now uses `checked_add`. - Clamps the relocation directory end (`start + size`) to `bytes.len()` with `checked_add`, and checks `at + 8` and `at + block`. 2. **Block-bounded 2-byte reads on odd relocation blocks**: - `(at + 8..at + block).step_by(2)` with `bytes.get(entry..entry + 2)` previously read 1 byte past `at + block` into the next block when `block` was odd. Using `bytes[header_end..block_end].as_chunks::<2>().0` keeps reads strictly within each block. 3. **Checked TLS field offset/RVA & `write()` bounds**: - Uses `checked_add` for `at + TLS64_ADDRESS_OF_CALLBACKS`, `tls.directory_rva + TLS64_ADDRESS_OF_CALLBACKS`, and `write()`'s `at + v.len()`. ### Verification - **AI-assisted**: Yes (written and verified with AI assistance). - **Environment**: macOS arm64 (Apple M5), CPU-only, Rust `1.98.1`, no Windows GPU / no game files. - `CARGO_FEATURE_PURE=1 cargo clippy --locked -p victory-loader --all-targets --target x86_64-pc-windows-msvc -- -D warnings` and `--all-features`: **0 warnings**. - `cargo test --locked -p xtask`: **4 passed, 0 failed** (including `repository_passes_policy`). - Native `rustc --edition=2024 --test crates/victory-loader/src/prepare.rs` (linked against host `victory-pe`, `blake3`, `thiserror`): both new tests (`dir64_relocations_handle_overflowing_rva_and_odd_blocks`, `write_rejects_overflowing_offset`) **passed**. - **Mutation check on unpatched `upstream/main` (`a803c87`)**: both new tests panic with `attempt to add with overflow` on unpatched `prepare.rs` (`dir64_relocations` line 151 and `write` line 170).
copilot-pull-request-reviewer[bot commented 2026-11-25 02:09:45 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T01:30:24Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:fxceless/victory-fork:review:6050187776 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/490#pullrequestreview-6050187776). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/copilot-pull-request-reviewer%5Bbot%5D) on 2026-11-25T01:30:24Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/490:upstream-pr/490'
git switch 'upstream-pr/490'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/490'
git switch 'upstream-pr/490'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/490'
git switch 'upstream-pr/490'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/490'
git switch 'main'
git merge --squash 'upstream-pr/490'
git switch 'main'
git merge --ff-only 'upstream-pr/490'
git switch 'main'
git merge 'upstream-pr/490'
git push origin 'main'
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
fxceless/victory-fork!490
No description provided.