PE: check SizeOfOptionalHeader and slice bounds in data_directory_offset #3024

Closed
github-bot wants to merge 1 commit from github-bot/gh-3024 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 03:41 UTC · original
fuzeofficlal:fix/pe-data-directory-offset-bounds → main · +77 −4 · 1 files · 1 commits · closed without merging

Summary

  • Validate both IMAGE_FILE_HEADER::SizeOfOptionalHeader (FILE_HEADER_SIZE_OF_OPTIONAL_HEADER = 16) and the input slice length in crates/victory-pe/src/layout.rs::data_directory_offset.
  • Previously data_directory_offset trusted NumberOfRvaAndSizes without checking that NumberOfRvaAndSizes or the full 8-byte data directory entry [off..off + DATA_DIRECTORY_SIZE) lay within the declared optional header (opt + SizeOfOptionalHeader) or within data.len(), allowing callers such as victory-loader/src/prepare.rs to receive offsets past the optional header (into the section table) or past EOF.
  • Add read_u16, update the synthetic PE32+ tests::image() fixture to populate SizeOfOptionalHeader, and add two unit tests covering SizeOfOptionalHeader boundary enforcement and truncated 8-byte directory entries at slice end.

Verification

  • rustfmt --edition 2024 --check crates/victory-pe/src/layout.rs: ok.
  • cargo test --locked -p victory-pe -p victory-loader: 38 victory-pe unit tests + 35 victory-loader unit tests + 4 integration tests passed.
  • cargo clippy --locked -p victory-pe -p victory-loader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 191 files ok.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 03:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/3024) `fuzeofficlal:fix/pe-data-directory-offset-bounds` → `main` · +77 −4 · 1 files · 1 commits · closed without merging ## Summary - Validate both `IMAGE_FILE_HEADER::SizeOfOptionalHeader` (`FILE_HEADER_SIZE_OF_OPTIONAL_HEADER = 16`) and the input slice length in `crates/victory-pe/src/layout.rs::data_directory_offset`. - Previously `data_directory_offset` trusted `NumberOfRvaAndSizes` without checking that `NumberOfRvaAndSizes` or the full 8-byte data directory entry `[off..off + DATA_DIRECTORY_SIZE)` lay within the declared optional header (`opt + SizeOfOptionalHeader`) or within `data.len()`, allowing callers such as `victory-loader/src/prepare.rs` to receive offsets past the optional header (into the section table) or past EOF. - Add `read_u16`, update the synthetic PE32+ `tests::image()` fixture to populate `SizeOfOptionalHeader`, and add two unit tests covering `SizeOfOptionalHeader` boundary enforcement and truncated 8-byte directory entries at slice end. ## Verification - `rustfmt --edition 2024 --check crates/victory-pe/src/layout.rs`: ok. - `cargo test --locked -p victory-pe -p victory-loader`: 38 `victory-pe` unit tests + 35 `victory-loader` unit tests + 4 integration tests passed. - `cargo clippy --locked -p victory-pe -p victory-loader --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 191 files ok.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:20:24 +00:00
github-bot 2026-11-26 06:32:15 +00:00
Author
Owner

@⁠pawrenity on GitHub · 2026-11-26 05:04 UTC · original

VERIFY-3024: verified a17208f on NVIDIA GeForce RTX 4070, Windows 11 (MSVC toolchain).

Tests

  • cargo test --locked -p victory-pe -p victory-loader: all pass (victory-pe 38 unit tests, victory-loader 35 unit tests plus its integration tests).
  • Mutation check: I put data_directory_offset back to its origin/main body and kept the new read_u16/constant/fixture so the tests still compile. Both new tests then fail: data_directory_offset_rejects_entries_past_size_of_optional_header (layout.rs:278) and data_directory_offset_rejects_truncated_entry_at_end_of_slice (layout.rs:292). The other 10 layout tests still pass.
  • cargo clippy --locked -p victory-pe -p victory-loader --all-targets -- -D warnings: clean. cargo fmt --check -p victory-pe: clean.
  • Trial merge onto current origin/main (63bcca4): clean. The PR is based on it.

Review against PE/COFF

  • The spec describes NumberOfRvaAndSizes as the number of data-directory entries "in the remainder of the optional header", and SizeOfOptionalHeader gives that header's length. So an entry that ends past opt + SizeOfOptionalHeader, or a count field outside it, is malformed, and rejecting it is correct. The new checks use checked_add throughout, and the end <= data.len() bound covers the whole 8-byte entry. No defects found.
  • Note (from reading the code only): on the current caller path this is defense in depth. victory_loader::prepare calls ImageInfo::parse first. That calls object 0.41's ImageNtHeaders64::parse, which already rejects an image whose NumberOfRvaAndSizes * 8 exceeds SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER64), and one whose optional header runs past EOF. So prepare() could not actually receive an out-of-header offset before this PR. The fix still matters for layout as a standalone public helper.
  • Nit: the new pub fn read_u16 has no doc comment. Neither does the existing read_u32.

Overlap: an open-PR search for data_directory_offset, SizeOfOptionalHeader and "optional header" turned up no competing PR. #2595 (entry points outside the mapped image) is in the same crate area but is a different check.

AI-assisted (Claude Code).

**@⁠pawrenity** on GitHub · 2026-11-26 05:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/3024#issuecomment-7183152417) VERIFY-3024: verified `a17208f` on NVIDIA GeForce RTX 4070, Windows 11 (MSVC toolchain). **Tests** - `cargo test --locked -p victory-pe -p victory-loader`: all pass (victory-pe 38 unit tests, victory-loader 35 unit tests plus its integration tests). - Mutation check: I put `data_directory_offset` back to its origin/main body and kept the new `read_u16`/constant/fixture so the tests still compile. Both new tests then fail: `data_directory_offset_rejects_entries_past_size_of_optional_header` (layout.rs:278) and `data_directory_offset_rejects_truncated_entry_at_end_of_slice` (layout.rs:292). The other 10 layout tests still pass. - `cargo clippy --locked -p victory-pe -p victory-loader --all-targets -- -D warnings`: clean. `cargo fmt --check -p victory-pe`: clean. - Trial merge onto current origin/main (`63bcca4`): clean. The PR is based on it. **Review against PE/COFF** - The spec describes `NumberOfRvaAndSizes` as the number of data-directory entries "in the remainder of the optional header", and `SizeOfOptionalHeader` gives that header's length. So an entry that ends past `opt + SizeOfOptionalHeader`, or a count field outside it, is malformed, and rejecting it is correct. The new checks use `checked_add` throughout, and the `end <= data.len()` bound covers the whole 8-byte entry. No defects found. - Note (from reading the code only): on the current caller path this is defense in depth. `victory_loader::prepare` calls `ImageInfo::parse` first. That calls object 0.41's `ImageNtHeaders64::parse`, which already rejects an image whose `NumberOfRvaAndSizes * 8` exceeds `SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER64)`, and one whose optional header runs past EOF. So `prepare()` could not actually receive an out-of-header offset before this PR. The fix still matters for `layout` as a standalone public helper. - Nit: the new `pub fn read_u16` has no doc comment. Neither does the existing `read_u32`. **Overlap:** an open-PR search for `data_directory_offset`, `SizeOfOptionalHeader` and "optional header" turned up no competing PR. #2595 (entry points outside the mapped image) is in the same crate area but is a different check. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 06:16 UTC · original

Automated triage for the maintainers.

The new checks in crates/victory-pe/src/layout.rs::data_directory_offset are correct as written, but they guard a case that cannot reach this function. Its only production caller is victory_loader::prepare (crates/victory-loader/src/prepare.rs), which first runs ImageInfo::parse (crates/victory-pe/src/image.rs). That goes through object 0.41's PeFile64::parse, which already rejects an image whose NumberOfRvaAndSizes entries do not fit inside SizeOfOptionalHeader, or whose optional header runs past the end of the file. The writes in prepare are bounds-checked too (write_u32/write return errors). So prepare cannot receive an offset outside the optional header or past EOF on main, and no other crate calls data_directory_offset (only a test in image.rs).

Proposed for closing as a guard for a case that cannot occur. The PR will be closed in 48 hours unless the author pushes a change (for example, showing a caller that reaches data_directory_offset without ImageInfo::parse) or a maintainer removes the label.

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 06:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/3024#issuecomment-7184266222) <!-- victory-triage v1 head=a17208f6873375ee6ccc635f8a1fee50540dc390 verdict=proposed-close at=2026-11-26T06:17:00Z --> Automated triage for the maintainers. The new checks in `crates/victory-pe/src/layout.rs::data_directory_offset` are correct as written, but they guard a case that cannot reach this function. Its only production caller is `victory_loader::prepare` (`crates/victory-loader/src/prepare.rs`), which first runs `ImageInfo::parse` (`crates/victory-pe/src/image.rs`). That goes through object 0.41's `PeFile64::parse`, which already rejects an image whose `NumberOfRvaAndSizes` entries do not fit inside `SizeOfOptionalHeader`, or whose optional header runs past the end of the file. The writes in `prepare` are bounds-checked too (`write_u32`/`write` return errors). So `prepare` cannot receive an offset outside the optional header or past EOF on `main`, and no other crate calls `data_directory_offset` (only a test in `image.rs`). Proposed for closing as a guard for a case that cannot occur. The PR will be closed in 48 hours unless the author pushes a change (for example, showing a caller that reaches `data_directory_offset` without `ImageInfo::parse`) or a maintainer removes the label. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 06:26 UTC · original

Agreed — PeFile64::parse in object already validates SizeOfOptionalHeader and NumberOfRvaAndSizes before victory_loader::prepare calls data_directory_offset, and the writes are bounds-checked. Closing to avoid unreachable defensive checks.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 06:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3024#issuecomment-7184431494) Agreed — `PeFile64::parse` in `object` already validates `SizeOfOptionalHeader` and `NumberOfRvaAndSizes` before `victory_loader::prepare` calls `data_directory_offset`, and the writes are bounds-checked. Closing to avoid unreachable defensive checks.

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