loader: use checked offsets and sizes in image preparation #670
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/670"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by AdvaitPalve11 on 2026-11-24T06:31:10Z.
Guards header, TLS, and base relocation offset calculations against potential integer overflow when processing corrupt or malformed PE images:
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T06:38:59Z.
Review: CHANGES_REQUESTED
The production change looks right, but the PR's own new unit test does not compile — on any platform, not just macOS:
ImageInfoderives onlyDebug, Clone(crates/victory-pe/src/image.rs:13), soImageInfo::default()doesn't exist.cargo test -p victory-loaderfails at compile time;cargo check -p victory-loader(lib only) passes, which is why this slipped through — there is no CI on main yet.Verified on macOS 26 arm64, rustc 1.98.1, head
95791fb.Fix options: parse a minimal synthetic PE in the test, build the struct literal (all fields are
pub), or dropDefaultand construct viaImageInfo::parseof a tiny valid image. (The 3 pre-existingprepare::testsfailures on macOS —patches_own_test_binaryetc. — also fail on main and are unrelated to this PR.)Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T06:39:23Z.
Review: COMMENTED
Tested on Windows 11 with MSVC Rust 1.98.1. cargo test -p victory-loader fails to compile with:
ext error[E0599]: no associated function or constant named default found for struct ictory_pe::ImageInfo in the current scope --> crates\victory-loader\src\prepare.rs:323:31 | 323 | let info = ImageInfo::default(); | ^^^^^^^ associated function or constant not found in ictory_pe::ImageInfoictory_pe::ImageInfo does not derive or implement Default. In other tests in prepare.rs, ImageInfo is parsed from std::fs::read(std::env::current_exe().unwrap()).unwrap(), e.g.:
ust let original = std::fs::read(std::env::current_exe().unwrap()).unwrap(); let info = ImageInfo::parse(&original).unwrap(); let relocs = dir64_relocations(&bytes, &info);Please update the test to construct/parse ImageInfo so the build and tests compile cleanly.
Imported from Marvisak/VIctory. Originally posted by AdvaitPalve11 on 2026-11-24T06:45:58Z.
Review: COMMENTED
No description provided.
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T06:38:59Z.
Code review location:
crates/victory-loader/src/prepare.rs; line 323ImageInfohas noDefaultimpl — this test target fails to compile everywhere (cargo test -p victory-loader). See review summary for alternatives.Imported from Marvisak/VIctory. Originally posted by AdvaitPalve11 on 2026-11-24T06:45:58Z.
Code review location:
crates/victory-loader/src/prepare.rs; line 323Thank you for testing it, Updated the test to parse ImageInfo from the current test binary to match the other tests in prepare.rs. Pushed commit
44bbd6aImported from Marvisak/VIctory. Originally posted by sebastianspicker on 2026-11-24T10:48:56Z.
I ran
victory_loader::prepareunder libFuzzer, seeded with a small synthetic PE32+ (one section, no game bytes). Onmain(ffe310e,prepare.rsunchanged since8e79c80) it panics within a minute atprepare.rs:151withattempt to add with overflow. A base-relocation block whose page RVA is nearu32::MAXmakespage + u32::from(e & 0xFFF)overflow. Debug builds panic; release builds have no overflow checks, so they wrap and compare against the wrong RVA.This PR fixes it. At
44bbd6athe crashing input passes, and 16 million seeded runs in 120 s found nothing else.The two new tests read the test binary as a PE, so they only run on Windows, and neither one hits this case. Here is a test that builds its own image, so it also runs on Linux and macOS. It fails on
mainwith the same overflow and passes on this branch:The fuzz target itself is a few lines in a separate
fuzz/crate outside the workspace. I can put it on a fork branch if that's useful.AI-assisted.
Imported from Marvisak/VIctory. Originally posted by ErRusso on 2026-11-24T11:19:18Z.
Audit: ✅ Looks good — verified & tested (safety improvement)
Reviewed this PR as part of an audit of mergeable PRs.
crates/victory-loader/src/prepare.rs(+63/-17). Replaces bare+arithmetic in PE image patching withchecked_add()everywhere: file/optional header field offsets, TLS callback offset & RVA, the relocation-table walk bounds, and thewrite()helper.dir64_relocations()additionally guards the per-entry page-offset addition.main.write_rejects_overflowing_offsets→ passed;dir64_relocations_handles_empty_or_truncated_table→ passed on a proper PE (fails on Linux only because it parsescurrent_exe(), which is a non-PE binary here — the same pre-existing limitation affects the other 3 prepare tests on Linux; the Windows CI matrix covers them).usizeoverflow panics (or worse, release-mode wrap). This closes that class of bug.No issues found. Nice defensive hardening.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:27:11Z.
Automated triage for the maintainers. Closing because it does not change behaviour the project needs (formatting, guards for cases that cannot occur, or notes and translations that cannot be kept current).
Replaces plain offset arithmetic in image preparation with checked_add chains. The operands come from 32-bit header fields added in 64-bit usize, so none of the usize sums can overflow; the one 32-bit sum only differs for a malformed relocation block and is harmless there. No reachable case, and it triples the size of each header write.
If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.
Pull request closed