Fix #2412: reject out-of-range export addresses in parse_exports #3278

Open
tharun-37 wants to merge 5 commits from upstream-pr/3280 into main
tharun-37 commented 2026-11-26 15:39:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by tharun-37 on 2026-11-26T15:39:17Z.


Fix #2412: parse_exports now uses the existing va_to_rva helper, so export address conversion is checked and shared with TLS callbacks.

This is a defensive check: object supplies addresses as image_base + rva with a 32-bit RVA, so valid inputs are unaffected. Out-of-range VAs now return PeError::Malformed instead of wrapping/truncating.

Tests: keeps_valid_export_rva_when_image_base_is_high covers the parser path; va_to_rva_rejects_an_address_below_the_image_base pins the checked conversion (below base, u64::MAX, and RVA past u32).

<!-- greyfinch-sync:Marvisak/VIctory:issue:5512954620 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3280). Originally posted by [tharun-37](https://github.com/tharun-37) on 2026-11-26T15:39:17Z. --- Fix #2412: `parse_exports` now uses the existing `va_to_rva` helper, so export address conversion is checked and shared with TLS callbacks. This is a defensive check: `object` supplies addresses as `image_base + rva` with a 32-bit RVA, so valid inputs are unaffected. Out-of-range VAs now return `PeError::Malformed` instead of wrapping/truncating. Tests: `keeps_valid_export_rva_when_image_base_is_high` covers the parser path; `va_to_rva_rejects_an_address_below_the_image_base` pins the checked conversion (below base, `u64::MAX`, and RVA past `u32`).
AurumCarlos commented 2026-11-26 15:41:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T15:41:54Z.

Review: COMMENTED


Thanks for splitting it out, the diff is now just the parse_exports hunk. A few loose ends:

  • The description still mentions rejects_export_address_below_image_base, but this PR adds no test. Either drop that line, or add a test that really feeds an out-of-range address (the old one passed on the valid path, see my note on #3165).
  • As noted on #3165, object gives image_base + rva with a 32-bit RVA, so this can't trigger today. "Preventing integer overflow" would read better as a defensive check.
  • #3165 is still open with the same title and the other hunks; closing it (or retitling it for what's left) would avoid two PRs for #2412.
  • The new lines look longer than rustfmt allows; cargo fmt should reflow them.
<!-- greyfinch-sync:Marvisak/VIctory:review:6076937438 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3280#pullrequestreview-6076937438). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T15:41:54Z. **Review:** COMMENTED --- Thanks for splitting it out, the diff is now just the `parse_exports` hunk. A few loose ends: - The description still mentions `rejects_export_address_below_image_base`, but this PR adds no test. Either drop that line, or add a test that really feeds an out-of-range address (the old one passed on the valid path, see my note on #3165). - As noted on #3165, `object` gives `image_base + rva` with a 32-bit RVA, so this can't trigger today. "Preventing integer overflow" would read better as a defensive check. - #3165 is still open with the same title and the other hunks; closing it (or retitling it for what's left) would avoid two PRs for #2412. - The new lines look longer than `rustfmt` allows; `cargo fmt` should reflow them.
Dukestar69 commented 2026-11-26 16:11:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T16:11:07Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Replacing address.wrapping_sub(image_base) as u32 with checked_sub(image_base) + u32::try_from cleanly rejects out-of-range export addresses with PeError::Malformed instead of silently truncating/wrapping.
  • cargo test -p victory-pe -- --test-threads=1: 36 passed, 0 failed.
  • cargo clippy -p victory-pe --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6077316056 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3280#pullrequestreview-6077316056). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T16:11:07Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Replacing `address.wrapping_sub(image_base) as u32` with `checked_sub(image_base)` + `u32::try_from` cleanly rejects out-of-range export addresses with `PeError::Malformed` instead of silently truncating/wrapping. - `cargo test -p victory-pe -- --test-threads=1`: 36 passed, 0 failed. - `cargo clippy -p victory-pe --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
tharun-37 commented 2026-11-26 17:24:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by tharun-37 on 2026-11-26T17:24:06Z.


Addressed the review notes: replaced the misleading test with keeps_valid_export_rva_when_image_base_is_high, reworded the description as a defensive check, and verified formatting. CI passes. Left #3165 untouched.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7198953321 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3280#issuecomment-7198953321). Originally posted by [tharun-37](https://github.com/tharun-37) on 2026-11-26T17:24:06Z. --- Addressed the review notes: replaced the misleading test with `keeps_valid_export_rva_when_image_base_is_high`, reworded the description as a defensive check, and verified formatting. CI passes. Left #3165 untouched.
cloph-dsp commented 2026-11-27 01:07:58 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-27T01:07:58Z.


Independent Windows verification of PR #3280 (Fix #2412: reject out-of-range export addresses in parse_exports), head d3242dfd, merge base 940d32f.

  • cargo test --locked -p victory-pe --lib -- image::tests::keeps_valid_export_rva_when_image_base_is_high --exact --test-threads=1 --quiet → test result: ok. 1 passed; 0 failed; 36 filtered out, exit 0.
  • git diff --check 940d32f HEAD → clean, exit 0.
  • Negative control: the test name is absent from origin/main at 940d32f (git grep exit 1). The earlier finding: referenced old head 0a7b5e6; this head adds the named test, so that finding is stale. Only the named synthetic test was run; full crate suite not run here.

Windows, Rust 1.100.0/MSVC, worktree-local CARGO_TARGET_DIR, CARGO_BUILD_JOBS=2. AI-assisted (Hy4 flash). GPU/driver N/A (CPU parser test). No game data, assets, extracted shaders or DRM used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7207037918 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3280#issuecomment-7207037918). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-27T01:07:58Z. --- Independent Windows verification of PR #3280 (`Fix #2412: reject out-of-range export addresses in parse_exports`), head `d3242dfd`, merge base `940d32f`. - `cargo test --locked -p victory-pe --lib -- image::tests::keeps_valid_export_rva_when_image_base_is_high --exact --test-threads=1 --quiet` → `test result: ok. 1 passed; 0 failed; 36 filtered out`, exit 0. - `git diff --check 940d32f HEAD` → clean, exit 0. - Negative control: the test name is absent from `origin/main` at `940d32f` (`git grep` exit 1). The earlier `finding:` referenced old head `0a7b5e6`; this head adds the named test, so that finding is stale. Only the named synthetic test was run; full crate suite not run here. Windows, Rust 1.100.0/MSVC, worktree-local `CARGO_TARGET_DIR`, `CARGO_BUILD_JOBS=2`. AI-assisted (Hy4 flash). GPU/driver N/A (CPU parser test). No game data, assets, extracted shaders or DRM used.
Jackster commented 2026-11-27 01:18:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-27T01:18:20Z.


On the missing regression test for this guard (FIND-41) — I looked, and it cannot be written against ImageInfo::parse:

  • This file only accepts PE32+ (PeFile64, MACHINE_AMD64, magic 0x20B), and object supplies export addresses as image_base + rva with a 32-bit RVA, so on a parsed image checked_sub can never fail and try_from can never fail. I checked your head d3242df: an address-table entry of 0xFFFF_FFFF parses fine and comes back as rva = Some(0xFFFFFFFF). So a test that goes through the parser would pass with the guard deleted — which is presumably why the one you had was misleading.

The file already has the conversion you need, for the TLS callbacks: va_to_rva (image.rs:442), which does the same checked_sub + u32::try_from and is reachable, because a TLS callback VA is read straight from the image. Two small changes:

  1. in parse_exports, hand the address to it:
ReadTarget::Address { address } => (Some(va_to_rva(address, image_base)?), None),
  1. pin the helper:
#[test]
fn va_to_rva_rejects_an_address_below_the_image_base() {
    const BASE: u64 = 0x1_8000_0000;
    assert_eq!(va_to_rva(BASE + 0x1200, BASE).unwrap(), 0x1200);
    assert_eq!(va_to_rva(BASE, BASE).unwrap(), 0, "the base itself is RVA 0");
    assert!(va_to_rva(0x1200, BASE).is_err(), "below the base has no RVA");
    assert!(va_to_rva(u64::MAX, BASE).is_err());
    assert!(
        va_to_rva(BASE + 0x1_0000_0000, BASE).is_err(),
        "an RVA past u32 is refused rather than truncated"
    );
}

Verified on Windows 11 with your head as the base: cargo test -p victory-pe 38 passed / 0 failed, cargo clippy -p victory-pe --all-targets -- -D warnings clean. Control: with va_to_rva's body replaced by the old wrapping_sub(image_base) as u32, the new test fails at its first below-base assertion, so it really does catch the regression.

The only visible change is the error text (VA 0x… is outside the image instead of export address … outside image_base …); nothing in the tree asserts the old wording, and it makes this file's two conversions one.

Synthetic only, no game data. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7207181247 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3280#issuecomment-7207181247). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-27T01:18:20Z. --- On the missing regression test for this guard (FIND-41) — I looked, and it cannot be written against `ImageInfo::parse`: - This file only accepts PE32+ (`PeFile64`, `MACHINE_AMD64`, magic `0x20B`), and `object` supplies export addresses as `image_base + rva` with a 32-bit RVA, so on a parsed image `checked_sub` can never fail and `try_from` can never fail. I checked your head `d3242df`: an address-table entry of `0xFFFF_FFFF` parses fine and comes back as `rva = Some(0xFFFFFFFF)`. So a test that goes through the parser would pass with the guard deleted — which is presumably why the one you had was misleading. The file already has the conversion you need, for the TLS callbacks: `va_to_rva` (`image.rs:442`), which does the same `checked_sub` + `u32::try_from` and *is* reachable, because a TLS callback VA is read straight from the image. Two small changes: 1. in `parse_exports`, hand the address to it: ```rust ReadTarget::Address { address } => (Some(va_to_rva(address, image_base)?), None), ``` 2. pin the helper: ```rust #[test] fn va_to_rva_rejects_an_address_below_the_image_base() { const BASE: u64 = 0x1_8000_0000; assert_eq!(va_to_rva(BASE + 0x1200, BASE).unwrap(), 0x1200); assert_eq!(va_to_rva(BASE, BASE).unwrap(), 0, "the base itself is RVA 0"); assert!(va_to_rva(0x1200, BASE).is_err(), "below the base has no RVA"); assert!(va_to_rva(u64::MAX, BASE).is_err()); assert!( va_to_rva(BASE + 0x1_0000_0000, BASE).is_err(), "an RVA past u32 is refused rather than truncated" ); } ``` Verified on Windows 11 with your head as the base: `cargo test -p victory-pe` **38 passed / 0 failed**, `cargo clippy -p victory-pe --all-targets -- -D warnings` clean. Control: with `va_to_rva`'s body replaced by the old `wrapping_sub(image_base) as u32`, the new test fails at its first below-base assertion, so it really does catch the regression. The only visible change is the error text (`VA 0x… is outside the image` instead of `export address … outside image_base …`); nothing in the tree asserts the old wording, and it makes this file's two conversions one. Synthetic only, no game data. AI-assisted.
tharun-37 commented 2026-11-27 02:29:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by tharun-37 on 2026-11-27T02:29:06Z.


Applied @Jackster's suggestion: parse_exports now delegates to va_to_rva, and the helper has a regression test covering below-base, u64::MAX, and RVA past u32. 38 tests pass; fmt/clippy clean; CI green. Thanks @cloph-dsp for the verification. No other PRs touched.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7208164530 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3280#issuecomment-7208164530). Originally posted by [tharun-37](https://github.com/tharun-37) on 2026-11-27T02:29:06Z. --- Applied @Jackster's suggestion: `parse_exports` now delegates to `va_to_rva`, and the helper has a regression test covering below-base, `u64::MAX`, and RVA past `u32`. 38 tests pass; fmt/clippy clean; CI green. Thanks @cloph-dsp for the verification. No other PRs touched.
Headache3318 commented 2026-11-27 07:17:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Headache3318 on 2026-11-27T07:17:20Z.


Independent test run of this head, no GPU or game files used.

Environment: Arch Linux x86-64, host x86_64-unknown-linux-gnu, pinned toolchain from rust-toolchain.toml (rustc 1.98.1, cargo 1.98.1).

Head 021b569a (Use va_to_rva for export addresses):

  • cargo test -p victory-pe --lib -- image::tests::va_to_rva_rejects_an_address_below_the_image_base image::tests::keeps_valid_export_rva_when_image_base_is_high → 2 passed, 0 failed (0.00 s).
  • Full cargo test -p victory-pe: 34 passed, 4 failed. The four failures are image::tests::parses_own_test_binary, rejects_iat_outside_image, rva_to_file_offset_maps_entry_point, rva_to_file_offset_rejects_offsets_past_4_gib — all four fail identically on main at 63bcca4 on this host: they parse the test binary itself as a PE, and on a Linux host that binary is ELF. Not caused by this head, and not Windows-behaviour-dependent in a way that should change on Windows hosts.

So the two new tests exercise the guard in both directions (valid RVA kept under a high image base; below-base and past-u32 refused) and pass unchanged on a non-Windows host.

Also for the record on Marvisak/VIctory#1633 FIND-41: that finding scanned head 0a7b5e6 and found no regression test. This head adds va_to_rva_rejects_an_address_below_the_image_base (and the high-image-base keep test), and both pass as above — FIND-41 can be closed.

No game files, no title data; synthetic fixtures only. AI-assisted (Hermes Agent), runs and verification my own.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7212326838 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3280#issuecomment-7212326838). Originally posted by [Headache3318](https://github.com/Headache3318) on 2026-11-27T07:17:20Z. --- Independent test run of this head, no GPU or game files used. **Environment:** Arch Linux x86-64, host `x86_64-unknown-linux-gnu`, pinned toolchain from `rust-toolchain.toml` (rustc 1.98.1, cargo 1.98.1). **Head `021b569a` (`Use va_to_rva for export addresses`):** - `cargo test -p victory-pe --lib -- image::tests::va_to_rva_rejects_an_address_below_the_image_base image::tests::keeps_valid_export_rva_when_image_base_is_high` → **2 passed, 0 failed** (0.00 s). - Full `cargo test -p victory-pe`: **34 passed, 4 failed**. The four failures are `image::tests::parses_own_test_binary`, `rejects_iat_outside_image`, `rva_to_file_offset_maps_entry_point`, `rva_to_file_offset_rejects_offsets_past_4_gib` — all four fail identically on `main` at `63bcca4` on this host: they parse the test binary itself as a PE, and on a Linux host that binary is ELF. Not caused by this head, and not Windows-behaviour-dependent in a way that should change on Windows hosts. So the two new tests exercise the guard in both directions (valid RVA kept under a high image base; below-base and past-`u32` refused) and pass unchanged on a non-Windows host. Also for the record on Marvisak/VIctory#1633 `FIND-41`: that finding scanned head `0a7b5e6` and found no regression test. This head adds `va_to_rva_rejects_an_address_below_the_image_base` (and the high-image-base keep test), and both pass as above — FIND-41 can be closed. No game files, no title data; synthetic fixtures only. AI-assisted (Hermes Agent), runs and verification my own.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3280:upstream-pr/3280'
git switch 'upstream-pr/3280'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3280'
git switch 'upstream-pr/3280'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3280'
git switch 'upstream-pr/3280'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3280'
git switch 'main'
git merge --squash 'upstream-pr/3280'
git switch 'main'
git merge --ff-only 'upstream-pr/3280'
git switch 'main'
git merge 'upstream-pr/3280'
git push origin 'main'
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!3278
No description provided.