PE: bound data_directory_offset by SizeOfOptionalHeader and buffer length #2529

Closed
reeledMarek wants to merge 1 commit from upstream-pr/2531 into main
reeledMarek commented 2026-11-25 15:23:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T15:23:41Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6607869028 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2531). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T15:23:41Z. --- ### 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`).
Marvisak commented 2026-11-25 16:09:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:52:41Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167391860 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2531#issuecomment-7167391860). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:52:41Z. --- <!-- 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)_
upstream-sync closed this pull request 2026-11-25 23:04:42 +00:00
Marvisak commented 2026-11-25 23:06:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:10:37Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176032912 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2531#issuecomment-7176032912). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:10:37Z. --- 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

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