Skip to content

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

Closed
pushkarreddyy wants to merge 1 commit into
Marvisak:mainfrom
pushkarreddyy:fix/loader-dir64-reloc-bounds
Closed

pushkarreddyy wants to merge 1 commit into
Marvisak:mainfrom
pushkarreddyy:fix/loader-dir64-reloc-bounds

Conversation

@pushkarreddyy

@pushkarreddyy pushkarreddyy commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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 DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: merged into main 940d32f, 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

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor Author

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)

Copy link
Copy Markdown
Owner

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

@pushkarreddyy

Copy link
Copy Markdown
Contributor Author

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)

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
What's wrong: `victory translate --validate --jobs 16` under CrossOver
crashed once in two runs inside dxil.dll's Validate (a write fault in the
heap). Each worker had its own IDxcValidator, created again for every
file of the walk. DXC promises that DxcCreateInstance is thread-safe and
that its objects have no thread affinity (DirectXShaderCompiler Marvisak#79,
Marvisak#2418), but instances share the library's global state (LLVM's pass
registry, the CRT locale, the allocator), and DXC's maintainers do not
promise two instances can run at the same time (#5566 was such a race
between per-thread compilers on a 16-core machine).

The fix:
- validator.rs: LoadLibrary + DxcCreateInstance and the final Release are
  serialised process-wide on every host (cheap, a few per run).
- Calls into dxil.dll go through a counting gate: under Wine (ntdll
  exports wine_get_version) one at a time; on Windows no limit, as
  before. VICTORY_VALIDATE_JOBS=N sets the limit on any host.
- The time spent inside Validate and waiting for a turn is counted;
  `translate --validate` prints both, so the cost of the gate on a real
  run is measured, and says "(1 at a time)" when the gate is on.
- translate.rs: validators go back to a pool when a file's workers end,
  so each is made once per run instead of once per file and worker.

Translation, pipeline creation and everything else stay parallel. On
this Linux host under Wine 10, Validate takes about 0.6 ms for a 3 KiB
program and 16 workers get only about 4x from running it in parallel
(8 workers about 2.7x), so the gate costs at most the sum of the
validation times; 240,000 concurrent validations of our own programs with
16 workers did not reproduce the crash here, so the Wine limit rests on
the Mac run.

Tests: emit::validator::tests::validators_on_several_threads_agree
(8 threads making, using and releasing validators at once, each signed
container equal to a single caller's) and a_gate_admits_at_most_its_limit;
emit::own, split_tests, stride_tests, memory_tests, resource and
dispatch_threads pass under Wine with dxil.dll 1.9.2609.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants