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

Closed
github-bot wants to merge 2 commits from gh-pr/670/head into gh-pr/670/base
Owner

@⁠AdvaitPalve11 on GitHub · 2026-11-24 06:31 UTC · original
AdvaitPalve11:fix/loader-prepare-checked-offsets → main · +63 −17 · 1 files · 2 commits · closed without merging

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
**@⁠AdvaitPalve11** on GitHub · 2026-11-24 06:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/670) `AdvaitPalve11:fix/loader-prepare-checked-offsets` → `main` · +63 −17 · 1 files · 2 commits · closed without merging 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
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
ImageInfo does not implement Default; parse the test executable's image info instead, matching existing tests in prepare.rs.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:12:21 +00:00
github-bot closed this pull request 2026-11-26 04:12:21 +00:00
Author
Owner

@⁠sebastianspicker on GitHub · 2026-11-24 10:48 UTC · original

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.

**@⁠sebastianspicker** on GitHub · 2026-11-24 10:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/670#issuecomment-7129280776) 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.
Author
Owner

@⁠ErRusso on GitHub · 2026-11-24 11:19 UTC · original

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.

**@⁠ErRusso** on GitHub · 2026-11-24 11:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/670#issuecomment-7129946020) ## 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.
Author
Owner

@⁠catminou4 on GitHub · 2026-11-24 06:38 UTC · original
On crates/victory-loader/src/prepare.rs line 323 (outdated):

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

**@⁠catminou4** on GitHub · 2026-11-24 06:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/670#discussion_r4676379346) On `crates/victory-loader/src/prepare.rs` line 323 (outdated): ```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.
Author
Owner

@⁠AdvaitPalve11 on GitHub · 2026-11-24 06:45 UTC · original
On crates/victory-loader/src/prepare.rs line 323 (outdated):

@@ -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

**@⁠AdvaitPalve11** on GitHub · 2026-11-24 06:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/670#discussion_r4676430055) On `crates/victory-loader/src/prepare.rs` line 323 (outdated): ```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
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:27 UTC · original

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.

**@⁠Marvisak** on GitHub · 2026-11-25 00:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/670#issuecomment-7148524741) 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!670
No description provided.