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

Open
imjustprism wants to merge 1 commit from upstream-pr/2630 into main
imjustprism commented 2026-11-25 17:12:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by imjustprism on 2026-11-25T17:12:14Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6609725371 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2630). Originally posted by [imjustprism](https://github.com/imjustprism) on 2026-11-25T17:12:14Z. --- 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.
cloph-dsp commented 2026-11-25 18:02:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-25T17:30:21Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169598976 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2630#issuecomment-7169598976). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-25T17:30:21Z. --- 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).
kotekompotek commented 2026-11-25 18:02:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T17:43:49Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7169917269 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2630#issuecomment-7169917269). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T17:43:49Z. --- 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.
lcg007xd-c commented 2026-11-25 22:03:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lcg007xd-c on 2026-11-25T21:19:50Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065134616 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2630#pullrequestreview-6065134616). Originally posted by [lcg007xd-c](https://github.com/lcg007xd-c) on 2026-11-25T21:19:50Z. **Review:** APPROVED --- ### 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.
Marvisak commented 2026-11-25 23:06:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:20:53Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176248940 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2630#issuecomment-7176248940). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:20:53Z. --- <!-- 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)_
upstream-sync changed title from [PR #2630] refuse a tls directory that runs outside the image to [PR #2630] PE: refuse a tls directory that runs outside the image 2026-11-26 09:05:19 +00:00
fuzeofficlal commented 2026-11-26 12:05:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T11:15:39Z.

Review: APPROVED


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).
<!-- greyfinch-sync:Marvisak/VIctory:review:6073185357 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2630#pullrequestreview-6073185357). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T11:15:39Z. **Review:** APPROVED --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2630:upstream-pr/2630'
git switch 'upstream-pr/2630'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2630'
git switch 'upstream-pr/2630'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2630'
git switch 'upstream-pr/2630'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2630'
git switch 'main'
git merge --squash 'upstream-pr/2630'
git switch 'main'
git merge --ff-only 'upstream-pr/2630'
git switch 'main'
git merge 'upstream-pr/2630'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2628
No description provided.