loader: use checked offsets and sizes in image preparation #670

Closed
AdvaitPalve11 wants to merge 2 commits from upstream-pr/670 into main
AdvaitPalve11 commented 2026-11-24 06:31:10 +00:00 (Migrated from github.com)

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:

  • Use checked_add for optional header and file header field offsets
  • Use checked_add for TLS callback and field RVA offsets
  • Use checked_add when iterating relocation blocks in dir64_relocations
  • Use checked_add in write() buffer bounds
  • Add unit tests for overflowing write and truncated relocation tables
<!-- greyfinch-sync:Marvisak/VIctory:issue:6576082366 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670). Originally posted by [AdvaitPalve11](https://github.com/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: - Use checked_add for optional header and file header field offsets - Use checked_add for TLS callback and field RVA offsets - Use checked_add when iterating relocation blocks in dir64_relocations - Use checked_add in write() buffer bounds - Add unit tests for overflowing write and truncated relocation tables
catminou4 commented 2026-11-24 06:54:21 +00:00 (Migrated from github.com)

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:

error[E0599]: no associated function or constant named `default` found for struct `victory_pe::ImageInfo`
   --> crates/victory-loader/src/prepare.rs:323:31
    |
323 |         let info = ImageInfo::default();

ImageInfo derives only Debug, Clone (crates/victory-pe/src/image.rs:13), so ImageInfo::default() doesn't exist. cargo test -p victory-loader fails 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 drop Default and construct via ImageInfo::parse of a tiny valid image. (The 3 pre-existing prepare::tests failures on macOS — patches_own_test_binary etc. — also fail on main and are unrelated to this PR.)

<!-- greyfinch-sync:Marvisak/VIctory:review:6034761286 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670#pullrequestreview-6034761286). Originally posted by [catminou4](https://github.com/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: ``` error[E0599]: no associated function or constant named `default` found for struct `victory_pe::ImageInfo` --> crates/victory-loader/src/prepare.rs:323:31 | 323 | let info = ImageInfo::default(); ``` `ImageInfo` derives only `Debug, Clone` (crates/victory-pe/src/image.rs:13), so `ImageInfo::default()` doesn't exist. `cargo test -p victory-loader` fails 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 drop `Default` and construct via `ImageInfo::parse` of a tiny valid image. (The 3 pre-existing `prepare::tests` failures on macOS — `patches_own_test_binary` etc. — also fail on main and are unrelated to this PR.)
LAKSHYA-cmd-tech commented 2026-11-24 06:54:22 +00:00 (Migrated from github.com)

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::ImageInfo

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6034764628 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670#pullrequestreview-6034764628). Originally posted by [LAKSHYA-cmd-tech](https://github.com/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::ImageInfo ` ictory_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.
AdvaitPalve11 commented 2026-11-24 06:54:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AdvaitPalve11 on 2026-11-24T06:45:58Z.

Review: COMMENTED


No description provided.

<!-- greyfinch-sync:Marvisak/VIctory:review:6034822371 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670#pullrequestreview-6034822371). Originally posted by [AdvaitPalve11](https://github.com/AdvaitPalve11) on 2026-11-24T06:45:58Z. **Review:** COMMENTED --- _No description provided._
catminou4 commented 2026-11-24 06:56:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T06:38:59Z.

Code review location: crates/victory-loader/src/prepare.rs; line 323

@@ -280,4 +308,21 @@ mod tests {
         let b = prepare(&original).unwrap();
         assert_eq!(a.file_name(), b.file_name());
     }
+
+    #[test]
+    fn write_rejects_overflowing_offsets() {
+        let mut bytes = vec![0u8; 16];
+        assert!(matches!(
+            write(&mut bytes, usize::MAX, &[1, 2]),
+            Err(PrepareError::Patch(_))
+        ));
+    }
+
+    #[test]
+    fn dir64_relocations_handles_empty_or_truncated_table() {
+        let info = ImageInfo::default();

ImageInfo has no Default impl — this test target fails to compile everywhere (cargo test -p victory-loader). See review summary for alternatives.

<!-- greyfinch-sync:Marvisak/VIctory:review-comment:4676379346 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670#discussion_r4676379346). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T06:38:59Z. **Code review location:** `crates/victory-loader/src/prepare.rs`; line 323 ~~~~diff @@ -280,4 +308,21 @@ mod tests { let b = prepare(&original).unwrap(); assert_eq!(a.file_name(), b.file_name()); } + + #[test] + fn write_rejects_overflowing_offsets() { + let mut bytes = vec![0u8; 16]; + assert!(matches!( + write(&mut bytes, usize::MAX, &[1, 2]), + Err(PrepareError::Patch(_)) + )); + } + + #[test] + fn dir64_relocations_handles_empty_or_truncated_table() { + let info = ImageInfo::default(); ~~~~ --- `ImageInfo` has no `Default` impl — this test target fails to compile everywhere (`cargo test -p victory-loader`). See review summary for alternatives.
AdvaitPalve11 commented 2026-11-24 06:56:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AdvaitPalve11 on 2026-11-24T06:45:58Z.

Code review location: crates/victory-loader/src/prepare.rs; line 323

@@ -280,4 +308,21 @@ mod tests {
         let b = prepare(&original).unwrap();
         assert_eq!(a.file_name(), b.file_name());
     }
+
+    #[test]
+    fn write_rejects_overflowing_offsets() {
+        let mut bytes = vec![0u8; 16];
+        assert!(matches!(
+            write(&mut bytes, usize::MAX, &[1, 2]),
+            Err(PrepareError::Patch(_))
+        ));
+    }
+
+    #[test]
+    fn dir64_relocations_handles_empty_or_truncated_table() {
+        let info = ImageInfo::default();

Thank 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 44bbd6a

<!-- greyfinch-sync:Marvisak/VIctory:review-comment:4676430055 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670#discussion_r4676430055). Originally posted by [AdvaitPalve11](https://github.com/AdvaitPalve11) on 2026-11-24T06:45:58Z. **Code review location:** `crates/victory-loader/src/prepare.rs`; line 323 ~~~~diff @@ -280,4 +308,21 @@ mod tests { let b = prepare(&original).unwrap(); assert_eq!(a.file_name(), b.file_name()); } + + #[test] + fn write_rejects_overflowing_offsets() { + let mut bytes = vec![0u8; 16]; + assert!(matches!( + write(&mut bytes, usize::MAX, &[1, 2]), + Err(PrepareError::Patch(_)) + )); + } + + #[test] + fn dir64_relocations_handles_empty_or_truncated_table() { + let info = ImageInfo::default(); ~~~~ --- Thank 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 44bbd6a
sebastianspicker commented 2026-11-24 11:04:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sebastianspicker on 2026-11-24T10:48:56Z.


I ran victory_loader::prepare under libFuzzer, seeded with a small synthetic PE32+ (one section, no game bytes). On main (ffe310e, prepare.rs unchanged since 8e79c80) it panics within a minute at prepare.rs:151 with attempt to add with overflow. A base-relocation block whose page RVA is near u32::MAX makes page + 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 44bbd6a the 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 main with the same overflow and passes on this branch:

/// A one-section PE32+ whose only base-relocation block has a page RVA
/// near u32::MAX: page + offset must not overflow.
#[test]
fn relocation_page_near_u32_max_is_skipped() {
    let mut d = vec![0u8; 0x400];
    d[0..2].copy_from_slice(b"MZ");
    d[0x3C..0x40].copy_from_slice(&0x40u32.to_le_bytes());
    d[0x40..0x44].copy_from_slice(b"PE\0\0");
    d[0x44..0x46].copy_from_slice(&0x8664u16.to_le_bytes());
    d[0x46..0x48].copy_from_slice(&1u16.to_le_bytes());
    d[0x54..0x56].copy_from_slice(&240u16.to_le_bytes());
    d[0x56..0x58].copy_from_slice(&0x0022u16.to_le_bytes());
    let opt = 0x58;
    d[opt..opt + 2].copy_from_slice(&0x20Bu16.to_le_bytes());
    d[opt + 16..opt + 20].copy_from_slice(&0x1000u32.to_le_bytes());
    d[opt + 24..opt + 32].copy_from_slice(&0x1_8000_0000u64.to_le_bytes());
    d[opt + 32..opt + 36].copy_from_slice(&0x1000u32.to_le_bytes());
    d[opt + 36..opt + 40].copy_from_slice(&0x200u32.to_le_bytes());
    d[opt + 56..opt + 60].copy_from_slice(&0x2000u32.to_le_bytes());
    d[opt + 60..opt + 64].copy_from_slice(&0x200u32.to_le_bytes());
    d[opt + 108..opt + 112].copy_from_slice(&16u32.to_le_bytes());
    let reloc = opt + 112 + 5 * 8; // IMAGE_DIRECTORY_ENTRY_BASERELOC
    d[reloc..reloc + 4].copy_from_slice(&0x1000u32.to_le_bytes());
    d[reloc + 4..reloc + 8].copy_from_slice(&10u32.to_le_bytes());
    let sec = opt + 240;
    d[sec..sec + 8].copy_from_slice(b".text\0\0\0");
    d[sec + 8..sec + 12].copy_from_slice(&0x200u32.to_le_bytes());
    d[sec + 12..sec + 16].copy_from_slice(&0x1000u32.to_le_bytes());
    d[sec + 16..sec + 20].copy_from_slice(&0x200u32.to_le_bytes());
    d[sec + 20..sec + 24].copy_from_slice(&0x200u32.to_le_bytes());
    d[sec + 36..sec + 40].copy_from_slice(&0x6000_0020u32.to_le_bytes());
    d[0x200..0x204].copy_from_slice(&u32::MAX.to_le_bytes()); // page RVA
    d[0x204..0x208].copy_from_slice(&10u32.to_le_bytes()); // block size
    d[0x208..0x20A].copy_from_slice(&0xA001u16.to_le_bytes()); // DIR64, offset 1
    let info = ImageInfo::parse(&d).unwrap();
    assert!(dir64_relocations(&d, &info).is_empty());
}

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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7129280776 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670#issuecomment-7129280776). Originally posted by [sebastianspicker](https://github.com/sebastianspicker) on 2026-11-24T10:48:56Z. --- I ran `victory_loader::prepare` under libFuzzer, seeded with a small synthetic PE32+ (one section, no game bytes). On `main` (`ffe310e`, `prepare.rs` unchanged since `8e79c80`) it panics within a minute at `prepare.rs:151` with `attempt to add with overflow`. A base-relocation block whose page RVA is near `u32::MAX` makes `page + 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 `44bbd6a` the 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 `main` with the same overflow and passes on this branch: ```rust /// A one-section PE32+ whose only base-relocation block has a page RVA /// near u32::MAX: page + offset must not overflow. #[test] fn relocation_page_near_u32_max_is_skipped() { let mut d = vec![0u8; 0x400]; d[0..2].copy_from_slice(b"MZ"); d[0x3C..0x40].copy_from_slice(&0x40u32.to_le_bytes()); d[0x40..0x44].copy_from_slice(b"PE\0\0"); d[0x44..0x46].copy_from_slice(&0x8664u16.to_le_bytes()); d[0x46..0x48].copy_from_slice(&1u16.to_le_bytes()); d[0x54..0x56].copy_from_slice(&240u16.to_le_bytes()); d[0x56..0x58].copy_from_slice(&0x0022u16.to_le_bytes()); let opt = 0x58; d[opt..opt + 2].copy_from_slice(&0x20Bu16.to_le_bytes()); d[opt + 16..opt + 20].copy_from_slice(&0x1000u32.to_le_bytes()); d[opt + 24..opt + 32].copy_from_slice(&0x1_8000_0000u64.to_le_bytes()); d[opt + 32..opt + 36].copy_from_slice(&0x1000u32.to_le_bytes()); d[opt + 36..opt + 40].copy_from_slice(&0x200u32.to_le_bytes()); d[opt + 56..opt + 60].copy_from_slice(&0x2000u32.to_le_bytes()); d[opt + 60..opt + 64].copy_from_slice(&0x200u32.to_le_bytes()); d[opt + 108..opt + 112].copy_from_slice(&16u32.to_le_bytes()); let reloc = opt + 112 + 5 * 8; // IMAGE_DIRECTORY_ENTRY_BASERELOC d[reloc..reloc + 4].copy_from_slice(&0x1000u32.to_le_bytes()); d[reloc + 4..reloc + 8].copy_from_slice(&10u32.to_le_bytes()); let sec = opt + 240; d[sec..sec + 8].copy_from_slice(b".text\0\0\0"); d[sec + 8..sec + 12].copy_from_slice(&0x200u32.to_le_bytes()); d[sec + 12..sec + 16].copy_from_slice(&0x1000u32.to_le_bytes()); d[sec + 16..sec + 20].copy_from_slice(&0x200u32.to_le_bytes()); d[sec + 20..sec + 24].copy_from_slice(&0x200u32.to_le_bytes()); d[sec + 36..sec + 40].copy_from_slice(&0x6000_0020u32.to_le_bytes()); d[0x200..0x204].copy_from_slice(&u32::MAX.to_le_bytes()); // page RVA d[0x204..0x208].copy_from_slice(&10u32.to_le_bytes()); // block size d[0x208..0x20A].copy_from_slice(&0xA001u16.to_le_bytes()); // DIR64, offset 1 let info = ImageInfo::parse(&d).unwrap(); assert!(dir64_relocations(&d, &info).is_empty()); } ``` 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.
ErRusso commented 2026-11-24 12:04:29 +00:00 (Migrated from github.com)

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.

  • Scope: crates/victory-loader/src/prepare.rs (+63/-17). Replaces bare + arithmetic in PE image patching with checked_add() everywhere: file/optional header field offsets, TLS callback offset & RVA, the relocation-table walk bounds, and the write() helper. dir64_relocations() additionally guards the per-entry page-offset addition.
  • Merges cleanly into main.
  • Tests: write_rejects_overflowing_offsets → passed; dir64_relocations_handles_empty_or_truncated_table → passed on a proper PE (fails on Linux only because it parses current_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).
  • Significance: this is the module that turns a title's DLL into something the Windows loader maps. Malformed/crafted images could previously trigger usize overflow panics (or worse, release-mode wrap). This closes that class of bug.

No issues found. Nice defensive hardening.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7129946020 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670#issuecomment-7129946020). Originally posted by [ErRusso](https://github.com/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. - **Scope:** `crates/victory-loader/src/prepare.rs` (+63/-17). Replaces bare `+` arithmetic in PE image patching with `checked_add()` everywhere: file/optional header field offsets, TLS callback offset & RVA, the relocation-table walk bounds, and the `write()` helper. `dir64_relocations()` additionally guards the per-entry page-offset addition. - **Merges cleanly** into `main`. - **Tests:** `write_rejects_overflowing_offsets` → **passed**; `dir64_relocations_handles_empty_or_truncated_table` → passed on a proper PE (fails on Linux only because it parses `current_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). - **Significance:** this is the module that turns a title's DLL into something the Windows loader maps. Malformed/crafted images could previously trigger `usize` overflow panics (or worse, release-mode wrap). This closes that class of bug. No issues found. Nice defensive hardening.
upstream-sync closed this pull request 2026-11-25 01:04:44 +00:00
Marvisak commented 2026-11-25 01:07:49 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148524741 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/670#issuecomment-7148524741). Originally posted by [Marvisak](https://github.com/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

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!670
No description provided.