pe: bound data directories to the optional header #396

Closed
lordon1a wants to merge 1 commit from upstream-pr/396 into main
lordon1a commented 2026-11-24 02:54:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lordon1a on 2026-11-24T02:54:56Z.


AI-assisted.

Problem

\ ictory_pe::layout::data_directory_offset\ checked \index < NumberOfRvaAndSizes, but did not verify that the requested 8-byte data directory entry actually fits within \IMAGE_FILE_HEADER::SizeOfOptionalHeader\ and the backing file slice. A truncated PE image or inflated \NumberOfRvaAndSizes\ could return an out-of-bounds offset past the optional header or file end.

Fix

  • Read \SizeOfOptionalHeader\ from \IMAGE_FILE_HEADER\ and slice the optional header within \data.
  • Require \OPT_NUMBER_OF_RVA_AND_SIZES\ and the full 8-byte entry at \OPT_DATA_DIRECTORY + index * DATA_DIRECTORY_SIZE\ to fit inside that bounded optional header slice before returning the file offset.
  • Add unit test \layout::tests::directory_entries_must_fit_in_the_optional_header_and_file\ covering valid entry, out-of-range index, truncated file, and shortened \SizeOfOptionalHeader.

Verification

  • \cargo xtask policy: \151 files ok\
  • \cargo test -p victory-pe: \8 passed; 0 failed\
  • \cargo clippy -p victory-pe --all-targets -- -D warnings: clean (0 warnings)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6573559653 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/396). Originally posted by [lordon1a](https://github.com/lordon1a) on 2026-11-24T02:54:56Z. --- AI-assisted. ### Problem \ ictory_pe::layout::data_directory_offset\ checked \index < NumberOfRvaAndSizes\, but did not verify that the requested 8-byte data directory entry actually fits within \IMAGE_FILE_HEADER::SizeOfOptionalHeader\ and the backing file slice. A truncated PE image or inflated \NumberOfRvaAndSizes\ could return an out-of-bounds offset past the optional header or file end. ### Fix - Read \SizeOfOptionalHeader\ from \IMAGE_FILE_HEADER\ and slice the optional header within \data\. - Require \OPT_NUMBER_OF_RVA_AND_SIZES\ and the full 8-byte entry at \OPT_DATA_DIRECTORY + index * DATA_DIRECTORY_SIZE\ to fit inside that bounded optional header slice before returning the file offset. - Add unit test \layout::tests::directory_entries_must_fit_in_the_optional_header_and_file\ covering valid entry, out-of-range index, truncated file, and shortened \SizeOfOptionalHeader\. ### Verification - \cargo xtask policy\: \151 files ok\ - \cargo test -p victory-pe\: \8 passed; 0 failed\ - \cargo clippy -p victory-pe --all-targets -- -D warnings\: clean (0 warnings)
copilot-pull-request-reviewer[bot commented 2026-11-24 06:52:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T02:55:07Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:Marvisak/VIctory:review:6032981544 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/396#pullrequestreview-6032981544). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/copilot-pull-request-reviewer%5Bbot%5D) on 2026-11-24T02:55:07Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
jeremygold02 commented 2026-11-24 06:55:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:28:19Z.


Thanks @lordon1a for bounding PE data-directory reads. I tested head 10e9f56a50214df67dca98b19daac0d8e9b04075 with:

cargo test --locked -p victory-pe --lib directory_entries_must_fit_in_the_optional_header_and_file -- --test-threads=1

Result: 1 passed, 0 failed (7 filtered out). The synthetic layout regression test checks that directory entries stay within both the optional-header bounds and the file bounds. It covers parser bounds, not loading a real title image.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122049943 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/396#issuecomment-7122049943). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:28:19Z. --- Thanks @lordon1a for bounding PE data-directory reads. I tested head `10e9f56a50214df67dca98b19daac0d8e9b04075` with: ``` cargo test --locked -p victory-pe --lib directory_entries_must_fit_in_the_optional_header_and_file -- --test-threads=1 ``` Result: 1 passed, 0 failed (7 filtered out). The synthetic layout regression test checks that directory entries stay within both the optional-header bounds and the file bounds. It covers parser bounds, not loading a real title image.
mercuryVM commented 2026-11-24 13:01:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T12:40:22Z.

Review: APPROVED


Tested 10e9f56 on Windows 11, RTX 5070, MSVC: cargo test -p victory-pe 8 passed, including directory_entries_must_fit_in_the_optional_header_and_file. cargo clippy -p victory-pe --all-targets is clean.

  • Slicing the optional header to SizeOfOptionalHeader before reading NumberOfRvaAndSizes, and checking that the 8-byte entry fits that slice, bounds the offset by both the declared header and the file. An inflated count or a truncated image now returns None instead of an offset past either.
  • All arithmetic uses checked_add/checked_mul, and the test covers an index past the count, a one-byte-short file and a one-byte-short header.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039347705 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/396#pullrequestreview-6039347705). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:40:22Z. **Review:** APPROVED --- Tested 10e9f56 on Windows 11, RTX 5070, MSVC: `cargo test -p victory-pe` 8 passed, including `directory_entries_must_fit_in_the_optional_header_and_file`. `cargo clippy -p victory-pe --all-targets` is clean. - Slicing the optional header to `SizeOfOptionalHeader` before reading `NumberOfRvaAndSizes`, and checking that the 8-byte entry fits that slice, bounds the offset by both the declared header and the file. An inflated count or a truncated image now returns `None` instead of an offset past either. - All arithmetic uses `checked_add`/`checked_mul`, and the test covers an index past the count, a one-byte-short file and a one-byte-short header. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-24 19:02:04 +00:00

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