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

Closed
pushkarreddyy wants to merge 1 commit from upstream-pr/2418 into main
pushkarreddyy commented 2026-11-25 13:10:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-25T13:10:06Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6605235433 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2418). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-25T13:10:06Z. --- 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.
DenRakEiw commented 2026-11-25 14:06:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T13:19:28Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6058346086 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2418#pullrequestreview-6058346086). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T13:19:28Z. **Review:** APPROVED --- 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).
catminou4 commented 2026-11-25 14:06:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T13:25:25Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6058436137 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2418#pullrequestreview-6058436137). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T13:25:25Z. **Review:** APPROVED --- 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.
pushkarreddyy commented 2026-11-25 14:07:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-25T13:36:20Z.


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!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7164020990 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7164020990). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-25T13:36:20Z. --- 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!
kotekompotek commented 2026-11-25 14:07:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T13:53:18Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7164435628 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7164435628). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T13:53:18Z. --- 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.
pushkarreddyy commented 2026-11-25 15:04:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-25T14:18:58Z.


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)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165088015 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7165088015). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-25T14:18:58Z. --- 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)
Marvisak commented 2026-11-25 16:09:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:29:02Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166912989 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7166912989). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:29:02Z. --- <!-- 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)_
upstream-sync closed this pull request 2026-11-26 16:04:27 +00:00
pushkarreddyy commented 2026-11-26 16:05:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pushkarreddyy on 2026-11-26T15:19:08Z.


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)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7196096032 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2418#issuecomment-7196096032). Originally posted by [pushkarreddyy](https://github.com/pushkarreddyy) on 2026-11-26T15:19:08Z. --- 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

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
Marvisak/VIctory!2416
No description provided.