PE: check SizeOfOptionalHeader and slice bounds in data_directory_offset #3022
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3024"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T03:41:57Z.
Summary
IMAGE_FILE_HEADER::SizeOfOptionalHeader(FILE_HEADER_SIZE_OF_OPTIONAL_HEADER = 16) and the input slice length incrates/victory-pe/src/layout.rs::data_directory_offset.data_directory_offsettrustedNumberOfRvaAndSizeswithout checking thatNumberOfRvaAndSizesor the full 8-byte data directory entry[off..off + DATA_DIRECTORY_SIZE)lay within the declared optional header (opt + SizeOfOptionalHeader) or withindata.len(), allowing callers such asvictory-loader/src/prepare.rsto receive offsets past the optional header (into the section table) or past EOF.read_u16, update the synthetic PE32+tests::image()fixture to populateSizeOfOptionalHeader, and add two unit tests coveringSizeOfOptionalHeaderboundary 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: 38victory-peunit tests + 35victory-loaderunit 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.Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T04:39:08Z.
Review: APPROVED
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.
Imported from Marvisak/VIctory. Originally posted by pawrenity on 2026-11-26T05:04:12Z.
VERIFY-3024: verified
a17208fon 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).data_directory_offsetback to its origin/main body and kept the newread_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) anddata_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.63bcca4): clean. The PR is based on it.Review against PE/COFF
NumberOfRvaAndSizesas the number of data-directory entries "in the remainder of the optional header", andSizeOfOptionalHeadergives that header's length. So an entry that ends pastopt + SizeOfOptionalHeader, or a count field outside it, is malformed, and rejecting it is correct. The new checks usechecked_addthroughout, and theend <= data.len()bound covers the whole 8-byte entry. No defects found.victory_loader::preparecallsImageInfo::parsefirst. That calls object 0.41'sImageNtHeaders64::parse, which already rejects an image whoseNumberOfRvaAndSizes * 8exceedsSizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER64), and one whose optional header runs past EOF. Soprepare()could not actually receive an out-of-header offset before this PR. The fix still matters forlayoutas a standalone public helper.pub fn read_u16has no doc comment. Neither does the existingread_u32.Overlap: an open-PR search for
data_directory_offset,SizeOfOptionalHeaderand "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).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:16:27Z.
Automated triage for the maintainers.
The new checks in
crates/victory-pe/src/layout.rs::data_directory_offsetare correct as written, but they guard a case that cannot reach this function. Its only production caller isvictory_loader::prepare(crates/victory-loader/src/prepare.rs), which first runsImageInfo::parse(crates/victory-pe/src/image.rs). That goes through object 0.41'sPeFile64::parse, which already rejects an image whoseNumberOfRvaAndSizesentries do not fit insideSizeOfOptionalHeader, or whose optional header runs past the end of the file. The writes inprepareare bounds-checked too (write_u32/writereturn errors). Sopreparecannot receive an offset outside the optional header or past EOF onmain, and no other crate callsdata_directory_offset(only a test inimage.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_offsetwithoutImageInfo::parse) or a maintainer removes the label.CI:
checkpassing.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T06:26:28Z.
Agreed —
PeFile64::parseinobjectalready validatesSizeOfOptionalHeaderandNumberOfRvaAndSizesbeforevictory_loader::preparecallsdata_directory_offset, and the writes are bounds-checked. Closing to avoid unreachable defensive checks.Pull request closed