Skip to content

PE: check SizeOfOptionalHeader and slice bounds in data_directory_offset - #3024

Closed
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/pe-data-directory-offset-bounds
Closed

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/pe-data-directory-offset-bounds

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — honoring SizeOfOptionalHeader closes the lie where a truncated optional header claims more directories than it holds; both count_at+4 and entry-end bounds checked against opt_end and data.len. Test constructs the trailing-garbage case.

@pawrenity

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Owner

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

@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

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.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants