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

Open
github-bot wants to merge 1 commit from github-bot/gh-1560 into main AGit
Owner

@⁠default-cybe on GitHub · 2026-11-24 19:10 UTC · original
default-cybe:fix/pe-refuse-iat-in-headers → main · +85 −8 · 1 files · 1 commits · open

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.

**@⁠default-cybe** on GitHub · 2026-11-24 19:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/1560) `default-cybe:fix/pe-refuse-iat-in-headers` → `main` · +85 −8 · 1 files · 1 commits · open ## 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.
ImageInfo::parse checked only that each IAT slot, normal or delay-load,
ends at or below SizeOfImage. The loader writes every slot after making
it writable, so a FirstThunk or delay ImportAddressTableRVA below
SizeOfHeaders would make it overwrite the image's own mapped headers.

A slot must now also start at or past SizeOfHeaders. The new test builds
a synthetic PE32+ (SizeOfHeaders 0x200) for both import kinds: an IAT at
0x80 or 0x1F8 is refused, one at 0x200 or 0x1080 still parses.

Committed-by: Conclave (cliproxy/claude-opus-5-5)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:36:29 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:30 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1560#issuecomment-7149630478) <!-- 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)_
Author
Owner

@⁠Headache3318 on GitHub · 2026-11-25 01:32 UTC · original

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.

**@⁠Headache3318** on GitHub · 2026-11-25 01:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1560#issuecomment-7149674090) 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 has changes conflicting with the target branch.
  • crates/victory-pe/src/image.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1560/head:github-bot/gh-1560'
git switch 'github-bot/gh-1560'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1560'
git switch 'github-bot/gh-1560'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1560'
git switch 'github-bot/gh-1560'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1560'
git switch 'main'
git merge --squash 'github-bot/gh-1560'
git switch 'main'
git merge --ff-only 'github-bot/gh-1560'
git switch 'main'
git merge 'github-bot/gh-1560'
git push origin 'main'
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!1560
No description provided.