PE: refuse a tls directory that runs outside the image #2630
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!2630
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2630"
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?
@imjustprism on GitHub · 2026-11-25 17:12 UTC · original
imjustprism:fix/tls-directory-outside-image→main· +70 −1 · 1 files · 1 commits · opena tls directory near the top of the rva range parsed fine, and
preparethen overflowed adding the callbacks field offset to its rva: a panic in a debug build, the wrong relocation entry cleared in release. the loader also reads the directory's index field from the mapped image with nothing bounding it.parse_tlsnow refuses a directory whose 40 bytes do not fit inside SizeOfImage, the same way the iat check next to it does.three tests on hand-built images: a directory ending exactly at the image end is accepted, one byte past it is refused, and one running past 4 gib is refused. both refusals parse fine without the check. the title's own executables and dlls still parse. gate run on an rx 9070 xt.
@cloph-dsp on GitHub · 2026-11-25 17:30 UTC · original
Independent Windows verification of PR #2630 (head
f4a7b64), CPU-only and synthetic.Toolchain: rustc/cargo 1.100.0, Windows MSVC (repo pins 1.98.1 in
rust-toolchain.toml; no clippy or rustup used). No game data, no source edits, no competing PR.cargo test --locked -p victory-pe --lib tls_directory -- --test-threads=1on the PR head: 3 passed, 36 filtered —accepts_tls_directory_ending_at_the_image_end,rejects_tls_directory_running_past_the_image_end,rejects_tls_directory_running_past_the_rva_range.cargo test --locked -p victory-pe --lib: 39 passed, 0 failed, 0 ignored.origin/main13f91ca: the sametls_directoryfilter reports 0 tests (the new tests are absent).No bug found. AI-assisted (Hy4 flash).
@kotekompotek on GitHub · 2026-11-25 17:43 UTC · original
Reviewed: the bounds check is correct —
directory_rva + TLS_DIRECTORY_SIZEvia checked_add, rejected when it exceedssize_of_image, which is what the loader's later read of the index field assumes. Edge tests cover the exact boundary (ends at image end → accepted). CI green.@Marvisak on GitHub · 2026-11-25 22:20 UTC · original
Automated triage for the maintainers.
Recommended for a maintainer's look, as low-impact hardening.
parse_tlsincrates/victory-pe/src/image.rsnow refuses a TLS directory whose 40 bytes (IMAGE_TLS_DIRECTORY64) do not fit insideSizeOfImage, using achecked_add, in the same style as the import-table bounds check beside it. Onmainsuch a directory parsed, and the later offset arithmetic could overflow (a debug-build panic) and the loader would read the index field outside the mapped image. This only matters for malformed images, since the title's own executables are well-formed (the author reports they still parse), but the parser treats header values as untrusted elsewhere and this closes a real gap. Three boundary tests (ends exactly at the image end, one byte past it, past 4 GiB). One file, merges cleanly, CIcheckpassing.Generated by Claude Code
refuse a tls directory that runs outside the imageto PE: refuse a tls directory that runs outside the image@lcg007xd-c on GitHub · 2026-11-25 21:19 UTC · original · approved these changes
Verified on Linux (Rust 1.99.0)
Branch: current
main(13f91ca) + this PR (merges cleanly).cargo test --locked -p victory-pe -- --skip parses_own_test_binary --skip rejects_iat_outside_image --skip rva_to_file_offset_maps_entry_point --skip rva_to_file_offset_rejects_offsets_past_4_gib: 35 passed. The four skipped tests read the test executable as a PE and fail on Linux on main too.parse_tlsremoved,rejects_tls_directory_running_past_the_image_endandrejects_tls_directory_running_past_the_rva_rangeboth fail. So main does accept both images, as the description says.cargo clippy --locked --target x86_64-pc-windows-msvc -p victory-pe --all-targets -- -D warnings: clean, same as main.Review
The bound is right, and it closes the two consumers that take
directory_rvaon trust:crates/victory-loader/src/prepare.rs:101(and :194) computetls.directory_rva + TLS64_ADDRESS_OF_CALLBACKS as u32unchecked. Withdirectory_rva + 40 <= SizeOfImage <= u32::MAX, that+ 24can no longer wrap.crates/victory-loader/src/tls.rs:37readsbase + directory_rva + 16from the mapped image. Main'sparse_tlsonly guarantees that the first 32 bytes are file-backed in some section, and it never compares that againstSizeOfImage.size_of::<pe::ImageTlsDirectory64>()is 40 (4 x u64 + 2 x u32), so the "ends exactly at the image end" test is the right boundary case.No defect found. One coordination note: this conflicts textually with #1639 ("PE: map header RVAs and read TLS data from the headers", open), which rewrites the same lines of
parse_tls. The check here should survive that rebase as is. It merges cleanly with #2532, #2595 and #2741.Not verified
own_imagetests, and loading the title's real binaries (the description says they still parse).Reviewed with Claude Code.
@fuzeofficlal on GitHub · 2026-11-26 11:15 UTC · original · approved these changes
Reviewed
f4a7b645. Thedirectory_rva.checked_add(TLS_DIRECTORY_SIZE).is_none_or(|end| end > size_of_image)check inparse_tlscleanly handles bothu32wraparound and TLS directories extending pastsize_of_image, matching the adjacent IAT bounds check and preventing invalid TLS index/callback offsets downstream.Verified locally on Windows 11:
cargo xtask policy(184 files ok) andcargo clippy -p victory-pe --tests -- -D warnings(default and--all-features): clean.cargo test -p victory-pe: 39/39 passed, includingparses_own_test_binaryand all three new TLS boundary tests (accepts_tls_directory_ending_at_the_image_end,rejects_tls_directory_running_past_the_image_end, andrejects_tls_directory_running_past_the_rva_range).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.