PE loader: saturate TlsDir::total_len and bound rva_to_offset in Layout::File #1387
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1397"
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?
Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T15:34:36Z.
Fixes two bounds/overflow bugs in
crates/xcl/src/pe.rs:TlsDir::total_lenaddedself.template_len() + self.size_of_zero_fill as u64without saturating, panicking in debug or wrapping in release whenend_raw - start_rawis nearu64::MAX.Pe::rva_to_offsetinLayout::FilereturnedSome(rva as usize)(for headers) orSome(s.raw_ptr as usize + delta as usize)(for sections) without checking that the offset lies withinself.data.len(), unlikeLayout::Mapped.This uses
saturating_addinTlsDir::total_lenand checksoff < self.data.len()(with checked addition) inLayout::File.What is in it
crates/xcl/src/pe.rs: saturateTlsDir::total_len; bounds-checkLayout::Fileoffsets inPe::rva_to_offset; add unit teststls_dir_total_len_saturates_instead_of_overflowingandrva_to_offset_in_file_layout_rejects_offsets_past_eof(including exactraw_ptr + delta == data.len()boundary check; both verified failing before the fix).Tested
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: 2no 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:
PR:
Not tested
AI use
pe.rs, wrote the fixes and unit tests, added the exactraw_ptr + delta == data.len()boundary assertion per review feedback, ran the test suite andscripts/smoke.ps1regression runs, and drafted this PR description.Imported from ivall/xcl. Originally posted by pickles976 on 2026-11-24T15:37:06Z.
Review: correct and well-scoped —
saturatingTLStotal_lenkills the wrap-to-small-length case (allocation sized from the overflowed value), and boundingrva_to_offsetatraw_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 + deltalanding exactly ondata.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.Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T15:54:18Z.
Full-suite run, since this PR has no test report yet. Tested exact head
6cb357b(on its base930e8b4).Tested
cargo build --workspace, thenXCL_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.tls_dir_total_len_saturates_instead_of_overflowing,rva_to_offset_in_file_layout_rejects_offsets_past_eof.XCL_ADAPTER=radeon),cargo test --workspace -- --test-threads=4: 753 passed, 0 failed, 41 ignored, no skips.Not tested
AI use
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T17:01:41Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.Thanks!
Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T18:50:56Z.
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.
upstream-sync referenced this pull request2026-11-25 06:04:27 +00:00
upstream-sync referenced this pull request2026-11-25 16:10:51 +00:00
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T05:03:24Z.
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
Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:29:29Z.
merge-check claim
head:
1e920a0114Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T23:38:06Z.
merge-check
base:
d0832e614fhead:
1e920a0114merge: 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_lensaturates instead of overflowing, andrva_to_offsetin the file layout returnsNonefor an offset at or past the end of the file data (headers and sections) and for araw_ptr + deltathat 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:37:18Z.
Triage (#1607) – head
1e920a0No 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 andTlsDir::total_lenstill 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.
Imported from ivall/xcl. Originally posted by Decodere on 2026-12-02T10:27:35Z.
merge-check
base:
1c8b551475head:
1e920a0114merge: clean (trial-merge commit 515fc669b9c9011a404c974540ab63f77b1416d6, git diff --check clean)
build: cargo build --workspace ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 --nocapture (43 targets): 2911 passed, 0 failed, 51 ignored (+2 new tests vs main baseline). All 12/12 pe::tests passed with 0 skips, including both new tests (tls_dir_total_len_saturates_instead_of_overflowing, rva_to_offset_in_file_layout_rejects_offsets_past_eof). 28 SKIPPED lines (host environment only: 22 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 2 no heap windows, 1 cannot disable the driver cache (Windows Developer Mode required)).
review: Read the full merged diff (+43/-3 in crates/xcl/src/pe.rs). Changes
TlsDir::total_lento usesaturating_addso malformed TLS templates cannot overflowu64, and boundsPe::rva_to_offsetinLayout::Fileagainstself.data.len()for both header RVAs (off < self.data.len()) and section RVAs (checked_addplusoff < self.data.len()), matchingLayout::Mappedbounds checking. Both new unit tests verifyu64::MAXsaturation and EOF/post-EOF rejection on truncatedkernel32.dllimages. Minimal, exact, no scope creep or UB.performance: not applicable (Tier 1 — PE loader parsing bounds only, runs once per loaded image).
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 4060 8 GB (driver 621.40), Intel Core i5-11400F, 32 GB RAM, Windows 11 Build 26200, Rust 1.100.1 MSVC, DXC 1.9.2602.17 / Antigravity (Gemini 4 Neon)
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.