PE: bound data_directory_offset by SizeOfOptionalHeader and buffer length #2531

Closed
github-bot wants to merge 1 commit from gh-pr/2531/head into gh-pr/2531/base
Owner

@⁠reeledMarek on GitHub · 2026-11-25 15:23 UTC · original
reeledMarek:fix/pe-data-directory-bounds → main · +44 −3 · 1 files · 1 commits · closed without merging

Problem

victory_pe::layout::data_directory_offset checked only index < NumberOfRvaAndSizes without checking IMAGE_FILE_HEADER::SizeOfOptionalHeader or the input buffer length.

For a PE32+ image with a short SizeOfOptionalHeader (e.g. 128 bytes / 2 directories) and NumberOfRvaAndSizes = 16, ImageInfo::parse (via object) clamps directories to SizeOfOptionalHeader and accepts the image, while data_directory_offset returned offsets past the optional header directly into the section header table (which victory-loader::prepare then patches). Similarly, truncated buffers past NumberOfRvaAndSizes returned out-of-bounds offsets.

Fix

  • Add FILE_HEADER_SIZE_OF_OPTIONAL_HEADER and read_u16.
  • Clamp NumberOfRvaAndSizes to 16, verify the directory entry ends within SizeOfOptionalHeader, and verify offset + DATA_DIRECTORY_SIZE <= data.len().

Testing

  • Added data_directory_offset_respects_size_of_optional_header and truncated-buffer check in crates/victory-pe/src/layout.rs.
  • Verified cargo test -p victory-pe --locked layout and Windows cross-clippy (-D warnings).
**@⁠reeledMarek** on GitHub · 2026-11-25 15:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2531) `reeledMarek:fix/pe-data-directory-bounds` → `main` · +44 −3 · 1 files · 1 commits · closed without merging ### Problem `victory_pe::layout::data_directory_offset` checked only `index < NumberOfRvaAndSizes` without checking `IMAGE_FILE_HEADER::SizeOfOptionalHeader` or the input buffer length. For a PE32+ image with a short `SizeOfOptionalHeader` (e.g. 128 bytes / 2 directories) and `NumberOfRvaAndSizes = 16`, `ImageInfo::parse` (via `object`) clamps directories to `SizeOfOptionalHeader` and accepts the image, while `data_directory_offset` returned offsets past the optional header directly into the section header table (which `victory-loader::prepare` then patches). Similarly, truncated buffers past `NumberOfRvaAndSizes` returned out-of-bounds offsets. ### Fix - Add `FILE_HEADER_SIZE_OF_OPTIONAL_HEADER` and `read_u16`. - Clamp `NumberOfRvaAndSizes` to 16, verify the directory entry ends within `SizeOfOptionalHeader`, and verify `offset + DATA_DIRECTORY_SIZE <= data.len()`. ### Testing - Added `data_directory_offset_respects_size_of_optional_header` and truncated-buffer check in `crates/victory-pe/src/layout.rs`. - Verified `cargo test -p victory-pe --locked layout` and Windows cross-clippy (`-D warnings`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:25 +00:00
github-bot closed this pull request 2026-11-26 05:08:25 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:52 UTC · original

Automated triage for the maintainers.

Recommended for a look. layout::data_directory_offset (crates/victory-pe/src/layout.rs) now also bounds the entry by SizeOfOptionalHeader, clamps NumberOfRvaAndSizes to 16 and checks the buffer length, so victory-loader's prepare can no longer patch bytes past the optional header (e.g. in the section table) on a malformed image. I checked the callers in prepare.rs and image.rs (they use real linker output, which sets SizeOfOptionalHeader correctly) and the updated test fixture. CI (clippy + tests on Windows): passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 15:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/2531#issuecomment-7167391860) <!-- victory-triage v1 head=e27577303df6114bb6b45988734399f4aea6744e verdict=merge-candidate at=2026-11-25T15:52:24Z --> Automated triage for the maintainers. Recommended for a look. `layout::data_directory_offset` (crates/victory-pe/src/layout.rs) now also bounds the entry by `SizeOfOptionalHeader`, clamps `NumberOfRvaAndSizes` to 16 and checks the buffer length, so `victory-loader`'s `prepare` can no longer patch bytes past the optional header (e.g. in the section table) on a malformed image. I checked the callers in prepare.rs and image.rs (they use real linker output, which sets `SizeOfOptionalHeader` correctly) and the updated test fixture. CI (clippy + tests on Windows): passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:10 UTC · original

Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because the premise does not hold. object 0.41.0's DataDirectories::parse reads NumberOfRvaAndSizes entries from the SizeOfOptionalHeader-sized slice and fails if they do not fit: a PE32+ with a 128-byte optional header and 16 directories gives Malformed("Invalid PE number of RVA and sizes") from ImageInfo::parse. The only production caller parses the same bytes with it first, so neither case can reach data_directory_offset.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 22:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/2531#issuecomment-7176032912) Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because the premise does not hold. `object` 0.41.0's `DataDirectories::parse` reads `NumberOfRvaAndSizes` entries from the `SizeOfOptionalHeader`-sized slice and fails if they do not fit: a PE32+ with a 128-byte optional header and 16 directories gives `Malformed("Invalid PE number of RVA and sizes")` from `ImageInfo::parse`. The only production caller parses the same bytes with it first, so neither case can reach `data_directory_offset`. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

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