Skip to content

PE loader: refuse IAT and TLS-index writes that straddle SizeOfImage - #1203

Open
Longno242 wants to merge 2 commits into
xcl-dev:mainfrom
Longno242:pe-loader-write-span
Open

Longno242 wants to merge 2 commits into
xcl-dev:mainfrom
Longno242:pe-loader-write-span

Conversation

@Longno242

@Longno242 Longno242 commented Nov 24, 2026 •

Copy link
Copy Markdown

Stacked on #854; only the top commit belongs here.

#854 rejects IAT / delay-load HMODULE / TLS-index writes whose first byte is inside the headers or at/after SizeOfImage. The loader still writes 8 bytes (IAT, HMODULE) or 4 bytes (TLS index), so a slot at SizeOfImage - 4 could overwrite past the mapped image. Require the whole write to fit — the same bounds idea #291 applied to relocations.

What is in it

  • crates/xcl/src/pe.rs: ensure_data_write_rva(rva, width) and tests for an IAT and TLS index that straddle the image end

Tested

  • Hardware/OS (full suite by @jennydeth): RTX 3060 12 GB (32-lane waves, driver 32.0.16.2101), Ryzen 5 5600X, 32 GB RAM, Windows 11 build 26300, Rust 1.100.0, DXC 1.9.2609.5, debug build. Head c97bd9f
  • Commit tested: c97bd9f98c2f136301658f5532709b5e313339f2 (clean tree)
  • cargo test --workspace -- --test-threads=4: 747 passed, 0 failed, 41 ignored. SKIPPED: 2 GPU tests for no wave64 (expected on this card)
  • SKIPPED: 2 GPU tests for no wave64 (expected on this card)
  • Retail smoke: see Regression run (@Toons29, RX 9070)

Regression run

  • Retail scripts/smoke.ps1 from @Toons29 (RX 9070, 60 s, same title/time; blocks as printed):
xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: c97bd9f98c2f136301658f5532709b5e313339f2
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1633 (PresentX 1633)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14809 MB

Not tested

  • Full cargo test --workspace
  • Delay-load HMODULE straddle case (same width=8 path as IAT)

AI use

Longno242 and others added 2 commits November 24, 2026 13:03
A crafted image whose FirstThunk or TLS AddressOfIndex lands below
SizeOfHeaders would overwrite the mapped headers before they are
re-read. Reject those write targets in pe.rs once
(PeError::HeaderWriteTarget), including delay-load HMODULE slots.

Fixes xcl-dev#836

Assisted-by: Cursor Auto (Composer)
Co-authored-by: Cursor <cursoragent@cursor.com>
ensure_data_write_rva only checked the first byte of the slot. The loader
writes 8 bytes for IAT/HMODULE and 4 for AddressOfIndex, so a target at
SizeOfImage-4 could still overwrite past the mapping. Require the whole
write to fit, matching the reloc bounds idea from xcl-dev#291 for these slots.

Stacked on xcl-dev#854.

Assisted-by: Cursor Auto (Composer)
Co-authored-by: Cursor <cursoragent@cursor.com>
@DavidM1911

Copy link
Copy Markdown
Contributor

Independent verification on current main 384f7f7 (branch already contains it — clean).

Machine: Windows 11, Ryzen 9 7900X; Rust stable MSVC, debug.

  • cargo test -p xcl --lib pe::: 16 passed, 0 failed — includes the new import_address_table_in_headers_is_error, tls_index_in_headers_is_error, delay_import_module_handle_in_headers_is_error, import_address_table_straddling_image_end_is_error, tls_index_straddling_image_end_is_error, write_targets_at_size_of_headers_are_accepted — IAT/TLS-index writes straddling SizeOfImage are refused as intended.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

@JoaoPauluu

Copy link
Copy Markdown

A run on Linux under Wine.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. c97bd9f as it is (based on 47efa2e), no uncommitted changes. The lib test binary runs under Wine, one test per process (--exact).
  • pe::tests::delay_import_module_handle_in_headers_is_error: ok, no SKIPPED.
  • pe::tests::import_address_table_in_headers_is_error: ok, no SKIPPED.
  • pe::tests::import_address_table_straddling_image_end_is_error: ok, no SKIPPED.
  • pe::tests::tls_index_in_headers_is_error: ok, no SKIPPED.
  • pe::tests::tls_index_straddling_image_end_is_error: ok, no SKIPPED.
  • pe::tests::write_targets_at_size_of_headers_are_accepted: ok, no SKIPPED.

Not tested

  • whether the new tests fail without the change on this stack; the rest of the suite on this head; a retail title; native Windows.

AI use

  • Claude Code (Claude Opus 5.5) built the head, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.

@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of c97bd9f9 on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: c97bd9f98c2f136301658f5532709b5e313339f2
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1633 (PresentX 1633)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14809 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

@Longno242

Copy link
Copy Markdown
Author

Thanks @Toons29 — pasted both of your xcl-smoke v1 blocks under Regression run so the description check can see them.

@Longno242
Longno242 marked this pull request as ready for review November 25, 2026 12:43
@Longno242

Copy link
Copy Markdown
Author

CI is stuck on action_required (first-time contributor workflow approval). Smoke + PR description are green; please approve the pending CI workflow when you get a chance.

@jennydeth

Copy link
Copy Markdown

Full workspace run (listed under Not tested).

Setup: RTX 3060 12 GB (32-lane waves, driver 32.0.16.2101), Ryzen 5 5600X, 32 GB RAM, Windows 11 build 26300, Rust 1.100.0, DXC 1.9.2609.5, debug build. Head c97bd9f.

Result: cargo test --workspace -- --test-threads=4: 747 passed, 0 failed, 41 ignored. SKIPPED: 2 GPU tests for no wave64 (expected on this card).

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

@Longno242

Copy link
Copy Markdown
Author

Thanks @jennydeth — copied your workspace counts into Tested (747 passed, 0 failed, 41 ignored. SKIPPED: 2 GPU tests for no wave64 (expected on this card)).

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

Port the combined final shape (pr1203head = c97bd9f, child of
pr854head = 6ce1240; both still OPEN vs upstream/main, merge-base false):
PeError::HeaderWriteTarget + ensure_data_write_rva(rva, width) with the
whole-width [headers, image) guard, checked on the import-thunk IAT slot
(8B), the delay-load HMODULE slot (8B), and the TLS AddressOfIndex slot
(4B), plus pe_with_headers + 6 boundary tests as-is.

Reconcile: tree already has checked_add(2) from step-82 where xcl-dev#854's base
shows wrapping_add; kept checked_add and added the ensure call on top.
Pre-existing saturating_add(8) outside-image guards stay; ensure adds the
header-floor and the straddle (checked_add(width)) ceiling.

Failing-first: new tests reference HeaderWriteTarget/ensure, both absent
pre-fix (E0599); header payloads that pre-fix mis-read as BadRva(0x8000…)
now report HeaderWriteTarget(0x80).

Gates: both checks 0 errors; lib 743/0/1 (+6); title 12 unique FAILs all
D3D12.X GPU-only 0 non-GPU; retail SIGSEGV +0x2237320 exit 139 stubs 20.
@github-actions

Copy link
Copy Markdown

Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 29, 2026
@Longno242

Copy link
Copy Markdown
Author

Still waiting on a maintainer: description/smoke are in place; CI needs first-time-contributor workflow approval. Not abandoned.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head c97bd9f

Stacked on #854 (same author): the shared lines are #854's commits. This PR's own change makes ensure_data_write_rva take the write width and checks the end of the 8-byte IAT / HMODULE and 4-byte TLS-index writes against SizeOfImage, with tests for slots that straddle the image end. Neither is on main yet.
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

stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants