Loader: guard dir64_relocations against BASERELOC size and page+offset overflow (fixes #2411) #2418

Closed
github-bot wants to merge 1 commit from github-bot/gh-2418 into main AGit
Owner

@⁠pushkarreddyy on GitHub · 2026-11-25 13:10 UTC · original
pushkarreddyy:fix/loader-dir64-reloc-bounds → main · +36 −7 · 1 files · 1 commits · closed without merging

Fixes #2411.

Root Cause

In crates/victory-loader/src/prepare.rs (dir64_relocations), parsing the BASERELOC data directory used unchecked arithmetic:

  • end = start as usize + size as usize did not saturate or clamp to bytes.len().
  • at + block > end could overflow on malformed block sizes.
  • page + u32::from(e & 0xFFF) could overflow and panic in debug builds when page > u32::MAX - 0xFFF.

Implementation Details

  • Clamped end to (start as usize).saturating_add(size as usize).min(bytes.len()).
  • Replaced block < 8 || at + block > end with at.checked_add(block).filter(|&next| block >= 8 && next <= end).
  • Guarded page + u32::from(e & 0xFFF) with page.checked_add(u32::from(e & 0xFFF)).

Verification

  • Added dir64_relocations_clamps_oversized_directory_and_overflowing_pages unit test in crates/victory-loader/src/prepare.rs covering oversized u32::MAX directory sizes and u32::MAX page base arithmetic.
  • CI check (workspace build, clippy -D warnings, and tests) is green.

Scope & Limitations

  • Valid IMAGE_REL_BASED_DIR64 relocation entries are unchanged; malformed overflowing entries are safely ignored.
  • No title files or proprietary assets were used.
**@⁠pushkarreddyy** on GitHub · 2026-11-25 13:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/2418) `pushkarreddyy:fix/loader-dir64-reloc-bounds` → `main` · +36 −7 · 1 files · 1 commits · closed without merging Fixes #2411. ### Root Cause In `crates/victory-loader/src/prepare.rs` (`dir64_relocations`), parsing the `BASERELOC` data directory used unchecked arithmetic: - `end = start as usize + size as usize` did not saturate or clamp to `bytes.len()`. - `at + block > end` could overflow on malformed block sizes. - `page + u32::from(e & 0xFFF)` could overflow and panic in debug builds when `page > u32::MAX - 0xFFF`. ### Implementation Details - Clamped `end` to `(start as usize).saturating_add(size as usize).min(bytes.len())`. - Replaced `block < 8 || at + block > end` with `at.checked_add(block).filter(|&next| block >= 8 && next <= end)`. - Guarded `page + u32::from(e & 0xFFF)` with `page.checked_add(u32::from(e & 0xFFF))`. ### Verification - Added `dir64_relocations_clamps_oversized_directory_and_overflowing_pages` unit test in `crates/victory-loader/src/prepare.rs` covering oversized `u32::MAX` directory sizes and `u32::MAX` page base arithmetic. - CI `check` (workspace build, clippy `-D warnings`, and tests) is green. ### Scope & Limitations - Valid `IMAGE_REL_BASED_DIR64` relocation entries are unchanged; malformed overflowing entries are safely ignored. - No title files or proprietary assets were used.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:05:13 +00:00
Author
Owner

@⁠pushkarreddyy on GitHub · 2026-11-25 13:36 UTC · original

Hi @⁠Marvisak — this PR resolves #2411 (clamping BASERELOC directory bounds and guarding at + block and page + offset against overflow in dir64_relocations), with unit tests and CI (check) passing. Ready for review and merge whenever convenient!

**@⁠pushkarreddyy** on GitHub · 2026-11-25 13:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7164020990) Hi @⁠Marvisak — this PR resolves #2411 (clamping `BASERELOC` directory bounds and guarding `at + block` and `page + offset` against overflow in `dir64_relocations`), with unit tests and CI (`check`) passing. Ready for review and merge whenever convenient!
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 13:53 UTC · original

Reviewed: end is now clamped to bytes.len(), the block bound is a checked_add, and page + offset no longer overflows u32 — all three panic paths from #2411 covered. The test mutates a copy of its own exe (no fixture), which fits the asset policy.

**@⁠kotekompotek** on GitHub · 2026-11-25 13:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7164435628) Reviewed: `end` is now clamped to `bytes.len()`, the block bound is a `checked_add`, and `page + offset` no longer overflows u32 — all three panic paths from #2411 covered. The test mutates a copy of its own exe (no fixture), which fits the asset policy.
Author
Owner

@⁠pushkarreddyy on GitHub · 2026-11-25 14:18 UTC · original

Thank you @⁠kotekompotek for the review! Testers on untested Windows 10/11 and Wine/Proton configurations are welcome to verify.

AI-assisted: yes (Gemini 4 Neon)

**@⁠pushkarreddyy** on GitHub · 2026-11-25 14:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7165088015) Thank you @⁠kotekompotek for the review! Testers on untested Windows 10/11 and Wine/Proton configurations are welcome to verify. AI-assisted: yes (Gemini 4 Neon)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:29 UTC · original

Automated triage for the maintainers.

Proposed close. This adds guards for cases that cannot fail in prepare.rs dir64_relocations:

  • start + size and at + block are a usize plus a u32 and cannot overflow on the 64-bit targets VIctory builds for;
  • reads past the end already stop safely: layout::read_u32 and bytes.get return None and the loop breaks;
  • page + (e & 0xFFF) can only overflow for a page RVA above 0xFFFFF000 in a corrupt executable, and the result is only compared with the TLS callback field's RVA, so a wrapped value simply never matches in release builds.

The test's first call makes no assertion. An input that reaches a real failure here would change the verdict.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 15:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7166912989) <!-- victory-triage v1 head=2582b9f1cdc21da23986e2a74b23a1ae85f73ac8 verdict=proposed-close at=2026-11-25T15:30:00Z --> Automated triage for the maintainers. **Proposed close.** This adds guards for cases that cannot fail in `prepare.rs` `dir64_relocations`: - `start + size` and `at + block` are a usize plus a u32 and cannot overflow on the 64-bit targets VIctory builds for; - reads past the end already stop safely: `layout::read_u32` and `bytes.get` return `None` and the loop breaks; - `page + (e & 0xFFF)` can only overflow for a page RVA above 0xFFFFF000 in a corrupt executable, and the result is only compared with the TLS callback field's RVA, so a wrapped value simply never matches in release builds. The test's first call makes no assertion. An input that reaches a real failure here would change the verdict. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. --- _Generated by [Claude Code](https://claude.ai/code)_
github-bot closed this pull request 2026-11-26 15:19:51 +00:00
Author
Owner

@⁠DenRakEiw on GitHub · 2026-11-25 13:19 UTC · original · approved these changes

Verified against origin/main (940d32f):

  • Guarding dir64_relocations against oversized BASERELOC directories and page + offset overflows (#2411) is clean and well-tested.
  • cargo test -p victory-loader: 39/39 passed (35 unit including dir64_relocations_clamps_oversized_directory_and_overflowing_pages + 4 integration tests).
  • cargo clippy -p victory-loader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

**@⁠DenRakEiw** on GitHub · 2026-11-25 13:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2418#pullrequestreview-6058346086) · **approved these changes** Verified against `origin/main` (`940d32f`): - Guarding `dir64_relocations` against oversized `BASERELOC` directories and `page + offset` overflows (`#2411`) is clean and well-tested. - `cargo test -p victory-loader`: 39/39 passed (35 unit including `dir64_relocations_clamps_oversized_directory_and_overflowing_pages` + 4 integration tests). - `cargo clippy -p victory-loader --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠catminou4 on GitHub · 2026-11-25 13:25 UTC · original · approved these changes

Tested locally: merged into main 940d32f69b, cargo test -p victory-loader --locked on macOS arm64. All 4 failures — including this PR's new dir64_relocations_clamps_oversized_directory_and_overflowing_pages — are environmental: prepare::tests parse the host's own Mach-O binary as PE ("Unknown file magic"), same as the 3 pre-existing ones on main. They can't pass off Windows by construction; #2334's #[cfg(windows)] gating already handles this class (this new test should get the same gate when it rebases past #2334). The BASERELOC guard logic itself is sound. Approving.

**@⁠catminou4** on GitHub · 2026-11-25 13:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2418#pullrequestreview-6058436137) · **approved these changes** Tested locally: merged into main 940d32f69b, `cargo test -p victory-loader --locked` on macOS arm64. All 4 failures — including this PR's new dir64_relocations_clamps_oversized_directory_and_overflowing_pages — are environmental: prepare::tests parse the host's own Mach-O binary as PE ("Unknown file magic"), same as the 3 pre-existing ones on main. They can't pass off Windows by construction; #2334's #[cfg(windows)] gating already handles this class (this new test should get the same gate when it rebases past #2334). The BASERELOC guard logic itself is sound. Approving.
Author
Owner

@⁠pushkarreddyy on GitHub · 2026-11-26 15:19 UTC · original

Closing this PR after reviewing the triage verification on main:

  • On 64-bit targets, usize + u32 (start + size and at + block) cannot overflow, and layout::read_u32 / bytes.get already stop cleanly on EOF.
  • page + (e & 0xFFF) can only wrap for corrupt executables with page RVAs above 0xFFFFF000, which will not match a valid TLS callback RVA.

Thank you for the audit.

AI-assisted: yes (Gemini 4 Neon)

**@⁠pushkarreddyy** on GitHub · 2026-11-26 15:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7196096032) Closing this PR after reviewing the triage verification on `main`: - On 64-bit targets, `usize + u32` (`start + size` and `at + block`) cannot overflow, and `layout::read_u32` / `bytes.get` already stop cleanly on EOF. - `page + (e & 0xFFF)` can only wrap for corrupt executables with page RVAs above `0xFFFFF000`, which will not match a valid TLS callback RVA. Thank you for the audit. AI-assisted: yes (Gemini 4 Neon)

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2418
No description provided.