Loader: guard dir64_relocations against BASERELOC size and page+offset overflow #2411

Open
opened 2026-11-26 05:04:59 +00:00 by github-bot · 3 comments
Owner

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

Summary

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

  • end = start as usize + size as usize without clamping to bytes.len() or using saturating/checked addition;
  • at + block > end without checked_add;
  • page + u32::from(e & 0xFFF) without checked_add, which panics in debug builds on u32 overflow for malformed relocation pages.

Expected

Clamp end to bytes.len() with saturating arithmetic and use checked_add for block bounds and page + offset.

Claiming this issue and opening a PR with unit tests shortly.

**@⁠pushkarreddyy** on GitHub · 2026-11-25 13:07 UTC · [original](https://github.com/Marvisak/VIctory/issues/2411) ### Summary In `crates/victory-loader/src/prepare.rs` (`dir64_relocations`), parsing the `BASERELOC` data directory computes: - `end = start as usize + size as usize` without clamping to `bytes.len()` or using saturating/checked addition; - `at + block > end` without `checked_add`; - `page + u32::from(e & 0xFFF)` without `checked_add`, which panics in debug builds on `u32` overflow for malformed relocation pages. ### Expected Clamp `end` to `bytes.len()` with saturating arithmetic and use `checked_add` for block bounds and `page + offset`. *Claiming this issue and opening a PR with unit tests shortly.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:04:59 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 18:49 UTC · original

Automated triage for the maintainers.

Partly confirmed in dir64_relocations in crates/victory-loader/src/prepare.rs. page + u32::from(e & 0xFFF) is plain u32 addition. A malformed block whose page RVA is near u32::MAX panics there in debug builds and wraps in release builds.

The other two points do not lead to a failure on the 64-bit hosts this runs on:

  • start and size are u32 values widened to usize, so neither start + size nor at + block can overflow.
  • An end past the file is harmless, because every read goes through layout::read_u32 or bytes.get. Those return None, and the loop then stops.

Clamping end is still reasonable as hardening. Open PR #2418 addresses all three.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 18:49 UTC · [original](https://github.com/Marvisak/VIctory/issues/2411#issuecomment-7171448601) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-25T13:07:28Z at=2026-11-25T18:58:00Z --> Automated triage for the maintainers. Partly confirmed in `dir64_relocations` in `crates/victory-loader/src/prepare.rs`. `page + u32::from(e & 0xFFF)` is plain `u32` addition. A malformed block whose page RVA is near `u32::MAX` panics there in debug builds and wraps in release builds. The other two points do not lead to a failure on the 64-bit hosts this runs on: - `start` and `size` are `u32` values widened to `usize`, so neither `start + size` nor `at + block` can overflow. - An `end` past the file is harmless, because every read goes through `layout::read_u32` or `bytes.get`. Those return `None`, and the loop then stops. Clamping `end` is still reasonable as hardening. Open PR #2418 addresses all three. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 14:17 UTC · original

Took the narrow part of this, as the triage above scoped it: only page + u32::from(e & 0xFFF) can actually fail on the 64-bit hosts VIctory builds for. start + size and at + block are a usize plus a u32 and cannot overflow there, and reads past the end already stop through layout::read_u32 and bytes.get.

Commit 2ad0aa0 on fix/2411-dir64-page-overflow:

/// The address a DIR64 relocation entry names. A malformed block can put the
/// page base near `u32::MAX`; the entry then has no address, and is skipped
/// instead of panicking in debug builds or wrapping in release ones.
fn reloc_address(page: u32, entry: u16) -> Option<u32> {
    page.checked_add(u32::from(entry & 0xFFF))
}

A malformed entry is skipped one at a time, so a bad page base does not throw away the well-formed entries after it. end is also clamped to bytes.len() with saturating_add, which changes nothing for valid files — the directory is inside the file — but keeps the bound honest.

Test relocation_addresses_past_u32_are_skipped covers 0x1000 + 0x0A, the last address 0xFFFF_FF0F + 0xF, the overflowing 0xFFFF_FFF0 + 0x10, and the type/offset split 0xA000 -> 0x2000.

On this machine, compiled at 63bcca4:

Check Result
cargo test -p victory-loader 35 passed, 1 failed
cargo clippy -p victory-loader --all-targets -- -D warnings clean
cargo xtask policy 192 files ok

The one failure is prepare::tests::windows_assigns_static_tls_to_a_prepared_dll ("the test binary uses static TLS"). It fails identically with this patch stashed, so it is local to this machine and not this change.

@⁠pushkarreddyy #2418 guards the same three places, but two of them cannot fail here; if the maintainers prefer that one, this can be dropped — the reloc_address part is the only behavioural difference.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 14:17 UTC · [original](https://github.com/Marvisak/VIctory/issues/2411#issuecomment-7194572227) Took the narrow part of this, as the triage above scoped it: only `page + u32::from(e & 0xFFF)` can actually fail on the 64-bit hosts VIctory builds for. `start + size` and `at + block` are a `usize` plus a `u32` and cannot overflow there, and reads past the end already stop through `layout::read_u32` and `bytes.get`. Commit [2ad0aa0](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/2ad0aa0561d3a8de5e02d3ae0796d9266a9640b9) on [`fix/2411-dir64-page-overflow`](https://github.com/andrewpozdnakov7-jpg/VIctory/tree/fix/2411-dir64-page-overflow): ```rust /// The address a DIR64 relocation entry names. A malformed block can put the /// page base near `u32::MAX`; the entry then has no address, and is skipped /// instead of panicking in debug builds or wrapping in release ones. fn reloc_address(page: u32, entry: u16) -> Option<u32> { page.checked_add(u32::from(entry & 0xFFF)) } ``` A malformed entry is skipped one at a time, so a bad page base does not throw away the well-formed entries after it. `end` is also clamped to `bytes.len()` with `saturating_add`, which changes nothing for valid files — the directory is inside the file — but keeps the bound honest. Test `relocation_addresses_past_u32_are_skipped` covers `0x1000 + 0x0A`, the last address `0xFFFF_FF0F + 0xF`, the overflowing `0xFFFF_FFF0 + 0x10`, and the type/offset split `0xA000 -> 0x2000`. On this machine, compiled at `63bcca4`: | Check | Result | |---|---| | `cargo test -p victory-loader` | 35 passed, 1 failed | | `cargo clippy -p victory-loader --all-targets -- -D warnings` | clean | | `cargo xtask policy` | 192 files ok | The one failure is `prepare::tests::windows_assigns_static_tls_to_a_prepared_dll` ("the test binary uses static TLS"). It fails identically with this patch stashed, so it is local to this machine and not this change. @⁠pushkarreddyy #2418 guards the same three places, but two of them cannot fail here; if the maintainers prefer that one, this can be dropped — the `reloc_address` part is the only behavioural difference.
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 14:26 UTC · original

Opened PR #3243 for the narrow part:
eloc_address with checked_add, a malformed entry skipped on its own, and end clamped to ytes.len(). CI check passed (2m42s), the PR is clean on main. Details and the local results are in the PR body; the coordination claim is in #1633.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 14:26 UTC · [original](https://github.com/Marvisak/VIctory/issues/2411#issuecomment-7194834769) Opened [PR #3243](https://git.gtahub.st/github/Marvisak--VIctory/pulls/3243) for the narrow part: eloc_address with checked_add, a malformed entry skipped on its own, and end clamped to ytes.len(). CI check passed (2m42s), the PR is clean on main. Details and the local results are in the PR body; the [coordination claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633) is in #1633.
This discussion has been locked. Commenting is limited to contributors.
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#2411
No description provided.