PE: refuse a tls directory that runs outside the image #2630

Open
github-bot wants to merge 1 commit from github-bot/gh-2630 into main AGit
Owner

@⁠imjustprism on GitHub · 2026-11-25 17:12 UTC · original
imjustprism:fix/tls-directory-outside-image → main · +70 −1 · 1 files · 1 commits · open

a tls directory near the top of the rva range parsed fine, and prepare then 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_tls now 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.

**@⁠imjustprism** on GitHub · 2026-11-25 17:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/2630) `imjustprism:fix/tls-directory-outside-image` → `main` · +70 −1 · 1 files · 1 commits · open a tls directory near the top of the rva range parsed fine, and `prepare` then 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_tls` now 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:10:44 +00:00
Author
Owner

@⁠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=1 on 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.
  • Full crate cargo test --locked -p victory-pe --lib: 39 passed, 0 failed, 0 ignored.
  • Negative control on the merge base / current origin/main 13f91ca: the same tls_directory filter reports 0 tests (the new tests are absent).

No bug found. AI-assisted (Hy4 flash).

**@⁠cloph-dsp** on GitHub · 2026-11-25 17:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2630#issuecomment-7169598976) 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=1` on 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`. - Full crate `cargo test --locked -p victory-pe --lib`: **39 passed**, 0 failed, 0 ignored. - Negative control on the merge base / current `origin/main` `13f91ca`: the same `tls_directory` filter reports **0 tests** (the new tests are absent). No bug found. AI-assisted (Hy4 flash).
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 17:43 UTC · original

Reviewed: the bounds check is correct — directory_rva + TLS_DIRECTORY_SIZE via checked_add, rejected when it exceeds size_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.

**@⁠kotekompotek** on GitHub · 2026-11-25 17:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/2630#issuecomment-7169917269) Reviewed: the bounds check is correct — `directory_rva + TLS_DIRECTORY_SIZE` via checked_add, rejected when it exceeds `size_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.
Author
Owner

@⁠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_tls in crates/victory-pe/src/image.rs now refuses a TLS directory whose 40 bytes (IMAGE_TLS_DIRECTORY64) do not fit inside SizeOfImage, using a checked_add, in the same style as the import-table bounds check beside it. On main such 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, CI check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 22:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/2630#issuecomment-7176248940) <!-- victory-triage v1 head=f4a7b645d6d55c3aeb291341726b3d3af16af764 verdict=merge-candidate at=2026-11-25T22:21:30Z --> Automated triage for the maintainers. Recommended for a maintainer's look, as low-impact hardening. `parse_tls` in `crates/victory-pe/src/image.rs` now refuses a TLS directory whose 40 bytes (`IMAGE_TLS_DIRECTORY64`) do not fit inside `SizeOfImage`, using a `checked_add`, in the same style as the import-table bounds check beside it. On `main` such 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, CI `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
github-bot changed title from refuse a tls directory that runs outside the image to PE: refuse a tls directory that runs outside the image 2026-11-26 09:03:10 +00:00
Author
Owner

@⁠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.
  • With the new range check in parse_tls removed, rejects_tls_directory_running_past_the_image_end and rejects_tls_directory_running_past_the_rva_range both 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_rva on trust:

  • crates/victory-loader/src/prepare.rs:101 (and :194) compute tls.directory_rva + TLS64_ADDRESS_OF_CALLBACKS as u32 unchecked. With directory_rva + 40 <= SizeOfImage <= u32::MAX, that + 24 can no longer wrap.
  • crates/victory-loader/src/tls.rs:37 reads base + directory_rva + 16 from the mapped image. Main's parse_tls only guarantees that the first 32 bytes are file-backed in some section, and it never compares that against SizeOfImage.

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

  • Windows: the own_image tests, and loading the title's real binaries (the description says they still parse).

Reviewed with Claude Code.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2630#pullrequestreview-6065134616) · **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. - With the new range check in `parse_tls` removed, `rejects_tls_directory_running_past_the_image_end` and `rejects_tls_directory_running_past_the_rva_range` both 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_rva` on trust: - `crates/victory-loader/src/prepare.rs:101` (and :194) compute `tls.directory_rva + TLS64_ADDRESS_OF_CALLBACKS as u32` unchecked. With `directory_rva + 40 <= SizeOfImage <= u32::MAX`, that `+ 24` can no longer wrap. - `crates/victory-loader/src/tls.rs:37` reads `base + directory_rva + 16` from the mapped image. Main's `parse_tls` only guarantees that the first 32 bytes are file-backed in some section, and it never compares that against `SizeOfImage`. `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 - Windows: the `own_image` tests, and loading the title's real binaries (the description says they still parse). Reviewed with Claude Code.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 11:15 UTC · original · approved these changes

Reviewed f4a7b645. The directory_rva.checked_add(TLS_DIRECTORY_SIZE).is_none_or(|end| end > size_of_image) check in parse_tls cleanly handles both u32 wraparound and TLS directories extending past size_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) and cargo clippy -p victory-pe --tests -- -D warnings (default and --all-features): clean.
  • cargo test -p victory-pe: 39/39 passed, including parses_own_test_binary and all three new TLS boundary tests (accepts_tls_directory_ending_at_the_image_end, rejects_tls_directory_running_past_the_image_end, and rejects_tls_directory_running_past_the_rva_range).
**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/2630#pullrequestreview-6073185357) · **approved these changes** Reviewed `f4a7b645`. The `directory_rva.checked_add(TLS_DIRECTORY_SIZE).is_none_or(|end| end > size_of_image)` check in `parse_tls` cleanly handles both `u32` wraparound and TLS directories extending past `size_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`) and `cargo clippy -p victory-pe --tests -- -D warnings` (default and `--all-features`): clean. - `cargo test -p victory-pe`: 39/39 passed, including `parses_own_test_binary` and all three new TLS boundary tests (`accepts_tls_directory_ending_at_the_image_end`, `rejects_tls_directory_running_past_the_image_end`, and `rejects_tls_directory_running_past_the_rva_range`).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2630/head:github-bot/gh-2630'
git switch 'github-bot/gh-2630'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2630'
git switch 'github-bot/gh-2630'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2630'
git switch 'github-bot/gh-2630'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2630'
git switch 'main'
git merge --squash 'github-bot/gh-2630'
git switch 'main'
git merge --ff-only 'github-bot/gh-2630'
git switch 'main'
git merge 'github-bot/gh-2630'
git push origin 'main'
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!2630
No description provided.