pe: bound data directories to the optional header #396

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

@⁠lordon1a on GitHub · 2026-11-24 02:54 UTC · original
fix/pe-data-dir-bounds → main · +37 −4 · 1 files · 1 commits · closed without merging

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)
**@⁠lordon1a** on GitHub · 2026-11-24 02:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/396) `fix/pe-data-dir-bounds` → `main` · +37 −4 · 1 files · 1 commits · closed without merging 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:02:25 +00:00
github-bot closed this pull request 2026-11-26 04:02:25 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:28 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/396#issuecomment-7122049943) 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.

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