pe: bound data directory offset by optional header size #3208
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3208
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3208"
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?
@cloph-dsp on GitHub · 2026-11-26 12:46 UTC · original
cloph-dsp:fix/pe-dir-offset→main· +54 −2 · 1 files · 1 commits · openDepends on: #1633 (claim
PE-DIR-OFFSET; earlier PR #2531 was closed unmerged).data_directory_offsetchecked onlyNumberOfRvaAndSizesand the file length. A malformed header could declare more directories thanSizeOfOptionalHeaderholds, making callers read past the optional header. The function now caps the directory count by the declared optional-header size and refuses entries whose 8 bytes run past that size or the end of the buffer.Test:
layout::tests::data_directory_offset_respects_size_of_optional_headerbuilds a synthetic PE32+ prefix with a two-entry optional header and checks the boundary, an inflated count and a truncated buffer.Validation (Windows, Rust 1.100.0/MSVC, worktree-local
CARGO_TARGET_DIR):cargo test --locked -p victory-pe --lib— 37 passed, 0 failed.git diff --check— clean.origin/main.AI-assisted (Hy4 flash). Synthetic inputs only; no game assets, extracted shaders, logs or DRM material were used or uploaded.
@AurumCarlos on GitHub · 2026-11-26 12:57 UTC · original · reviewed
The bound is right:
NumberOfRvaAndSizesalone shouldn't decide where the table ends, and capping it by(SizeOfOptionalHeader - OPT_DATA_DIRECTORY) / 8matches how the optional header is laid out. A few remarks on the shape of the check:1. The last three checks can't fire. After the first guard,
opt + size <= data.len(). Withcount <= room, we getend = table + count * 8 <= opt + OPT_DATA_DIRECTORY + room * 8 <= opt + size. And sinceindex < count,at + 8 <= end. Soend > opt + size,end > data.len()andat + 8 > data.len()are always false, and the function could end with:That's the original tail, with just the
sizeguard and the cap added above it. It's not wrong as written, but a reader will look for the case each check handles and not find one.2. Because of that, the test's last case is not testing what its comment says.
cutends atopt + OPT_DATA_DIRECTORY + 8, whilesizedeclares two entries, so it's rejected byopt + size > data.len()at the top, not by an entry "that runs past the end of the buffer". The assertion holds, but the comment describes a path that can't be reached. Something like "Nor is a buffer that ends inside the declared optional header" would match.That first guard is also a small behaviour change worth stating: a buffer that ends inside the optional header now yields
Nonefor every index, even entries that are fully present. Onmain, a buffer cut right after entry 0 still returned entry 0. All in-tree callers pass whole files (prepare.rs,image.rs), so nothing breaks, but it's the kind of thing the doc comment ondata_directory_offsetcould mention.3. Verification scope. The description runs
victory-peonly, butvictory-loader/src/prepare.rsalso callsdata_directory_offset(L70, L82, L124, and two tests thatunwrap()it on the test binary). They should pass, since a real linker writes a correctSizeOfOptionalHeader, but acargo test -p victory-loaderrun would confirm it.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.