PE: refuse an import address table inside the headers #1559

Open
default-cybe wants to merge 1 commit from upstream-pr/1560 into main
default-cybe commented 2026-11-24 19:10:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by default-cybe on 2026-11-24T19:10:37Z.


What

ImageInfo::parse now refuses an import whose IAT slot starts below SizeOfHeaders. This covers normal and delay-load imports. Before, entries() (crates/victory-pe/src/image.rs) checked only the top end: the slot had to end at or below SizeOfImage.

Why

victory-loader binds every slot itself. bind_imports collects each iat_slot_rva from imports and delay_imports. write_slots (crates/victory-loader/src/windows.rs) then calls VirtualProtect(PAGE_READWRITE) on base + rva and writes 8 bytes there. Its SAFETY comment relies on "the parser only yields slots inside the image". An image with FirstThunk (or a delay ImportAddressTableRVA) below SizeOfHeaders passed that check, so the loader would overwrite the image's own mapped headers with host pointers. This follows from reading bind_imports and write_slots; no such image was loaded (see Not tested).

Linkers place the IAT in a section, never in the headers, so well-formed images are unaffected. The bound is rva >= SizeOfHeaders, written from the PE format and VIctory's own code. The bytes between SizeOfHeaders and the first section are accepted as before.

Change

  • parse_imports, parse_delay_imports and entries take size_of_headers next to size_of_image.
  • A slot below it gets the existing Malformed error, now worded "IAT at .. overlaps the headers or runs outside the image".
  • New test refuses_iat_in_headers: a synthetic in-memory PE32+ (the existing synthetic_image helper, SizeOfHeaders 0x200) with one import, built once as a normal import and once as a delay-load import. An IAT at 0x80 or 0x1F8 is refused. An IAT at 0x200 or 0x1080 still parses, with that slot RVA.

Tested

Windows 11 x64, Rust 1.98.1 (the pinned toolchain), synthetic inputs only, on main ffe310e plus this commit.

  • Before the fix (main plus only the new test module), the new test fails: IAT at 0x80 (delay: false) was accepted. With the loop narrowed to [true] in a scratch copy, the delay-load case fails the same way: IAT at 0x80 (delay: true) was accepted.
  • After the fix: cargo test -p victory-pe --lib iat: 2 passed, 0 failed.
  • cargo test --workspace: 343 passed, 0 failed, 6 ignored.
  • cargo xtask policy: asset policy: 154 files ok.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (the CI command): clean.
  • cargo fmt --check -p victory-pe: clean.

Not tested

  • I did not load a title or a crafted image through the Windows loader. The fix is in the parser, and the test covers it.
  • Linux host.

AI use

Written with Conclave (model cliproxy/claude-opus-5-5), which also ran the commands listed under Tested.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6589395248 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1560). Originally posted by [default-cybe](https://github.com/default-cybe) on 2026-11-24T19:10:37Z. --- ## What `ImageInfo::parse` now refuses an import whose IAT slot starts below `SizeOfHeaders`. This covers normal and delay-load imports. Before, `entries()` (`crates/victory-pe/src/image.rs`) checked only the top end: the slot had to end at or below `SizeOfImage`. ## Why `victory-loader` binds every slot itself. `bind_imports` collects each `iat_slot_rva` from `imports` and `delay_imports`. `write_slots` (`crates/victory-loader/src/windows.rs`) then calls `VirtualProtect(PAGE_READWRITE)` on `base + rva` and writes 8 bytes there. Its SAFETY comment relies on "the parser only yields slots inside the image". An image with `FirstThunk` (or a delay `ImportAddressTableRVA`) below `SizeOfHeaders` passed that check, so the loader would overwrite the image's own mapped headers with host pointers. This follows from reading `bind_imports` and `write_slots`; no such image was loaded (see Not tested). Linkers place the IAT in a section, never in the headers, so well-formed images are unaffected. The bound is `rva >= SizeOfHeaders`, written from the PE format and VIctory's own code. The bytes between `SizeOfHeaders` and the first section are accepted as before. ## Change - `parse_imports`, `parse_delay_imports` and `entries` take `size_of_headers` next to `size_of_image`. - A slot below it gets the existing `Malformed` error, now worded "IAT at .. overlaps the headers or runs outside the image". - New test `refuses_iat_in_headers`: a synthetic in-memory PE32+ (the existing `synthetic_image` helper, `SizeOfHeaders` 0x200) with one import, built once as a normal import and once as a delay-load import. An IAT at 0x80 or 0x1F8 is refused. An IAT at 0x200 or 0x1080 still parses, with that slot RVA. ## Tested Windows 11 x64, Rust 1.98.1 (the pinned toolchain), synthetic inputs only, on `main` ffe310e plus this commit. - Before the fix (`main` plus only the new test module), the new test fails: `IAT at 0x80 (delay: false) was accepted`. With the loop narrowed to `[true]` in a scratch copy, the delay-load case fails the same way: `IAT at 0x80 (delay: true) was accepted`. - After the fix: `cargo test -p victory-pe --lib iat`: 2 passed, 0 failed. - `cargo test --workspace`: 343 passed, 0 failed, 6 ignored. - `cargo xtask policy`: asset policy: 154 files ok. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (the CI command): clean. - `cargo fmt --check -p victory-pe`: clean. ## Not tested - I did not load a title or a crafted image through the Windows loader. The fix is in the parser, and the test covers it. - Linux host. ## AI use Written with Conclave (model `cliproxy/claude-opus-5-5`), which also ran the commands listed under Tested.
catminou4 commented 2026-11-24 20:02:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T19:20:58Z.

Review: APPROVED


IAT slots must now lie past SizeOfHeaders as well as inside the image — the loader writes every slot, so an IAT overlapping the headers would corrupt them. Applied to both normal and delay-load imports via the shared entries() helper, with the error message updated. The new refuses_iat_in_headers test covers both import kinds at 0x80/0x1F8 (rejected) and 0x200/0x1080 (accepted) and passes on macOS.

Note: cargo test -p victory-pe on macOS shows 4 pre-existing failures in image::tests (they parse the Mach-O test binary as a PE — present on unmodified main, unrelated to this PR); the other 4 tests pass including the new one.

<!-- greyfinch-sync:Marvisak/VIctory:review:6045811992 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1560#pullrequestreview-6045811992). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T19:20:58Z. **Review:** APPROVED --- IAT slots must now lie past `SizeOfHeaders` as well as inside the image — the loader writes every slot, so an IAT overlapping the headers would corrupt them. Applied to both normal and delay-load imports via the shared `entries()` helper, with the error message updated. The new `refuses_iat_in_headers` test covers both import kinds at 0x80/0x1F8 (rejected) and 0x200/0x1080 (accepted) and passes on macOS. Note: `cargo test -p victory-pe` on macOS shows 4 pre-existing failures in `image::tests` (they parse the Mach-O test binary as a PE — present on unmodified main, unrelated to this PR); the other 4 tests pass including the new one.
Marvisak commented 2026-11-25 02:06:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:30:47Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-pe/src/image.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149630478 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1560#issuecomment-7149630478). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:30:47Z. --- <!-- victory-triage v1 head=db470df5325d24cd9b17516a3827ca87a2c1b4a0 verdict=needs-rebase at=2026-11-25T01:30:47Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-pe/src/image.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Headache3318 commented 2026-11-25 02:06:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Headache3318 on 2026-11-25T01:32:58Z.


Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head db470df applied cleanly onto current main ccfb029.

  • cargo test -p victory-pe --locked: the new test refuses_iat_in_headers passes on Linux. The 4 image tests that parse the test binary as a PE fail (expected on Linux — they fail identically on main).
  • cargo clippy -p victory-pe --all-targets --all-features --locked -- -D warnings: clean.

Not tested: Windows MSVC build, the title itself. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149674090 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1560#issuecomment-7149674090). Originally posted by [Headache3318](https://github.com/Headache3318) on 2026-11-25T01:32:58Z. --- Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head `db470df` applied cleanly onto current `main` `ccfb029`. - `cargo test -p victory-pe --locked`: the new test `refuses_iat_in_headers` passes on Linux. The 4 `image` tests that parse the test binary as a PE fail (expected on Linux — they fail identically on main). - `cargo clippy -p victory-pe --all-targets --all-features --locked -- -D warnings`: clean. Not tested: Windows MSVC build, the title itself. AI-assisted.
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/1560:upstream-pr/1560'
git switch 'upstream-pr/1560'

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