Skip to content

PE loader: saturate TlsDir::total_len and bound rva_to_offset in Layout::File - #1397

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:pe/tls-len-and-rva-file-bounds
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:pe/tls-len-and-rva-file-bounds

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes two bounds/overflow bugs in crates/xcl/src/pe.rs:

  • TlsDir::total_len added self.template_len() + self.size_of_zero_fill as u64 without saturating, panicking in debug or wrapping in release when end_raw - start_raw is near u64::MAX.
  • Pe::rva_to_offset in Layout::File returned Some(rva as usize) (for headers) or Some(s.raw_ptr as usize + delta as usize) (for sections) without checking that the offset lies within self.data.len(), unlike Layout::Mapped.

This uses saturating_add in TlsDir::total_len and checks off < self.data.len() (with checked addition) in Layout::File.

What is in it

  • crates/xcl/src/pe.rs: saturate TlsDir::total_len; bounds-check Layout::File offsets in Pe::rva_to_offset; add unit tests tls_dir_total_len_saturates_instead_of_overflowing and rva_to_offset_in_file_layout_rejects_offsets_past_eof (including exact raw_ptr + delta == data.len() boundary check; both verified failing before the fix).

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 753 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib pe::tests: 12 passed, 0 failed (both new tests failed before the fix)

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 1e920a01147c135577d1e6dae3b185f5d72de5c6
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1644 (PresentX 1644)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14468 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited pe.rs, wrote the fixes and unit tests, added the exact raw_ptr + delta == data.len() boundary assertion per review feedback, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.

@pickles976

Copy link
Copy Markdown

Review: correct and well-scoped — saturating TLS total_len kills the wrap-to-small-length case (allocation sized from the overflowed value), and bounding rva_to_offset at raw_ptr + delta >= data.len() is the consumer-side half of the parse-time invariant I proposed in #836; the truncated-image test is exactly the right regression harness. One edge row worth adding while the test is fresh: raw_ptr + delta landing exactly on data.len() — the message already specifies >= so the implementation reads right, but pinning the boundary value makes a future off-by-one regression fail loudly. Combined with #1367 (parser-side directory validation), this pair implements the full invariant: five independent finds across three loaders today were all instances of these two gaps.

@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 6cb357b (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: tls_dir_total_len_saturates_instead_of_overflowing, rva_to_offset_in_file_layout_rejects_offsets_past_eof.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.

Thanks!

…ut::File

Two bounds/overflow bugs in pe.rs:
- TlsDir::total_len added `template_len() + size_of_zero_fill as u64`
  without saturating, panicking in debug or wrapping in release when
  `end_raw - start_raw` is near u64::MAX.
- Pe::rva_to_offset in Layout::File returned `Some(rva)` (for headers)
  or `Some(raw_ptr + delta)` (for sections) without checking that the
  offset lies within `self.data.len()`, unlike Layout::Mapped.

Use saturating_add in TlsDir::total_len and validate `off < self.data.len()`
(with checked addition) in Layout::File, with unit tests for both.

Assisted-by: OpenCode (Gemini 4 Neon)
@b3IKK
b3IKK force-pushed the pe/tls-len-and-rva-file-bounds branch from 6cb357b to 1e920a0 Compare November 24, 2026 18:50
@b3IKK

b3IKK commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @pickles976 — pinned the exact boundary in \1e920a0\ (
aw_ptr + delta == data.len() - 1\ returns \Some,
aw_ptr + delta == data.len()\ returns \None) and re-ran \scripts/test.ps1\ and the 60 s \scripts/smoke.ps1\ run.

@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
xcl-dev#1397)

Port live head 1e920a0 (still OPEN, merge-base exit 1). WHOLE fix in
shared pe.rs: saturating_add in total_len; off < data.len() with
checked addition on both File-layout rva_to_offset branches.
Tests: tls_dir_total_len unit as-is; kernel32 truncation test as-is
behind cfg(windows) (needs kernel32.dll) + synthetic pe_with_headers
twin covering the same two boundaries plus the header branch on Linux.
Failing-first: overflow panic at :145 + Some(0x410)==len pre-fix.
Gates: both checks 0 errors, lib 745/0/1 (+2), pe:: 23/23, title e2e
fresh-root 12 unique FAILs all D3D12.X 0 non-GPU, retail fresh-root
HOLDS (+0x2237320, stubs 20, xgr 3 apis, unmapped []). Lock audit 0.
@BBareth

BBareth commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

merge-check claim
head: 1e920a0

@github-actions github-actions Bot added area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Nov 30, 2026
@BBareth

BBareth commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: d0832e6
head: 1e920a0
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2892 passed, 0 failed, 51 ignored; 28 SKIPPED lines, as on the base; failing: none. The base alone, run earlier today on the same commit: 2890 passed, 0 failed, 51 ignored, 28 SKIPPED lines (22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 cannot disable the driver cache). Both new tests ran and passed: tls_dir_total_len_saturates_instead_of_overflowing, rva_to_offset_in_file_layout_rejects_offsets_past_eof.
review: no blocking finding; one file (+43/-3), every line read. TlsDir::total_len saturates instead of overflowing, and rva_to_offset in the file layout returns None for an offset at or past the end of the file data (headers and sections) and for a raw_ptr + delta that overflows, where it used to return an offset the caller could not read. For a well-formed image every offset is inside the file, so the result is unchanged; the test truncates a copy of kernel32.dll to check both edges (last byte still mapped, first byte past the end refused).
performance: none: one comparison per lookup.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 1e920a0

No open or merged PR makes the same change; main's Pe::rva_to_offset (Layout::File) still returns offsets past the end of the data and TlsDir::total_len still adds without saturating. The other PE loader hardening PRs (#1203, #1212, #1416, #1431) change other functions.
No problems found in the diff.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants