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

Open
github-bot wants to merge 1 commit from github-bot/gh-490 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 01:30 UTC · original
IlyassRS3:fix/loader-prepare-reloc-bounds-and-overflow → main · +118 −23 · 1 files · 1 commits · open

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).
**@⁠IlyassRS3** on GitHub · 2026-11-25 01:30 UTC · [original](https://github.com/fxceless/victory-fork/pull/490) `IlyassRS3:fix/loader-prepare-reloc-bounds-and-overflow` → `main` · +118 −23 · 1 files · 1 commits · open ### 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).
Fixes unchecked additions and out-of-block reads in `victory-loader`'s PE
preparation pass (`crates/victory-loader/src/prepare.rs`):

1. `dir64_relocations`: `page + u32::from(e & 0xFFF)` panicked in debug
   builds (and wrapped to a low RVA in release builds) when a relocation
   page near `u32::MAX` overflowed; `start + size`, `at + 8`, and
   `at + block` were also unchecked and `end` was not clamped to
   `bytes.len()`.
2. Odd relocation block sizes: iterating `(at + 8..at + block).step_by(2)`
   and slicing `bytes.get(entry..entry + 2)` read one byte past `at + block`
   into the next block's header when `block` was odd. Iterating
   `bytes[header_end..block_end].as_chunks::<2>().0` keeps all reads strictly
   within the block.
3. TLS callback patching and `write()`: check `at + TLS64_ADDRESS_OF_CALLBACKS`,
   `tls.directory_rva + TLS64_ADDRESS_OF_CALLBACKS`, and `at + v.len()`
   with `checked_add`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:44:33 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/490/head:github-bot/gh-490'
git switch 'github-bot/gh-490'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-490'
git switch 'github-bot/gh-490'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-490'
git switch 'github-bot/gh-490'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-490'
git switch 'main'
git merge --squash 'github-bot/gh-490'
git switch 'main'
git merge --ff-only 'github-bot/gh-490'
git switch 'main'
git merge 'github-bot/gh-490'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.