Skip to content

PE loader: refuse IAT and TLS-index writes inside the headers (#836) - #854

Open
Longno242 wants to merge 1 commit into
xcl-dev:mainfrom
Longno242:pe-loader-header-write-targets
Open

Longno242 wants to merge 1 commit into
xcl-dev:mainfrom
Longno242:pe-loader-header-write-targets

Conversation

@Longno242

@Longno242 Longno242 commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes #836.

A crafted PE whose import address table, delay-load HMODULE slot, or TLS AddressOfIndex lands below SizeOfHeaders would let the manual mapper overwrite its own headers before later steps re-read them. Reject those directory-derived write targets once in pe.rs (PeError::HeaderWriteTarget). Targets must lie in [size_of_headers, size_of_image).

What is in it

  • crates/xcl/src/pe.rs: ensure_data_write_rva, checks in import thunks / delay-load HMODULE / TLS index parsing, and unit tests for IAT, delay-load HMODULE, and TLS index inside the headers plus the SizeOfHeaders boundary

Tested

  • Hardware/OS (full suite by @upipibi, head 6ce1240498cd787448963da24cd8f5e892feb0da as-is, base 47efa2e): NVIDIA RTX 3070 8 GB (Ampere, 32-lane waves), driver 616.56, Windows 11, debug, after cargo build --workspace
  • cargo test --workspace --no-fail-fast -- --test-threads=4 --skip hle::xaudio2: 736 passed, 0 failed, 41 ignored. End-to-end title tests passed.
  • SKIPPED: 2 GPU tests, both no wave64 on this card. hle::xaudio2 was skipped because those tests fail or hang on this machine on main too; this PR does not touch it.
  • @Decodere, release, head 6ce1240498cd787448963da24cd8f5e892feb0da, RTX 4060 8 GB (Ada, wave32), driver 32.0.16.2114, i5-11400F, 32 GB, Windows 11 build 26200, rustc 1.100.0, DXC 1.9.2609.5: the four header-write tests passed 3/3 (4 passed, 0 failed), and cargo test --release -p xcl --lib pe:: passed 3/3 (14 passed, 0 failed). cargo build --release -p xcl was clean.
  • Retail smoke: see Regression run (@Toons29, RX 9070)
  • Earlier on this machine: cargo test -p xcl --lib pe::tests: 14 passed (includes the delay-load HMODULE case). @DavidM1911 also ran cargo test -p xcl --lib after merging the branch with then-current main (RTX 3060 Ti): passed, with a note about intermittent Test suite: parallel cargo test intermittently dies with 0xc0000005 (hle::d3d12x queue_tests × list_x race) #1978 under parallel scheduling.

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: 6ce1240498cd787448963da24cd8f5e892feb0da
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: 14686 MB

Not tested

  • hle::xaudio2 on the RTX 3070 report (pre-existing on main for that machine)
  • wave64-only GPU paths
  • live title coverage of the header-write rejection itself (smoke is a regression check only)

AI use

@dcastell1

Copy link
Copy Markdown

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on clean main@beaa245 (713 passed, 0 failed, 41 ignored on this box):

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
716 passed; 0 failed; 41 ignored   (exit 0)

Three more passing tests than main, so the PE-loader refusals for IAT and TLS-index writes inside the headers are exercised here; nothing else changes verdict.

Tree restored afterwards (md5 and a full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.

@SlopTart

Copy link
Copy Markdown

Linux / AMD RDNA3 datapoint.

Merged state: head 46376c5 onto main 1ec4c98, clean merge.

Full suite, cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 707 passed, 11 failed, 41 ignored. Failures = the known Wine-lane set from my #42 rows plus #718 trio members and a hit of gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes (new flake, see #718 addendum). PE-header write-rejection tests green - closes the #836 hardening on this rig.

Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. My Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@Longno242

Copy link
Copy Markdown
Author

Thanks for the independent verification runs (@dcastell1, @SlopTart).

This PR still needs the two retail xcl-smoke blocks under Regression run (main + this head, same title/time) before the description check can pass. I do not have an Xbox Series X|S package here — only a PC Xbox Minecraft Launcher install, which is not a Scarlett title.

If someone with a Series title can run:

# on main, then on this PR head (clean tree, no uncommitted changes)
cargo build --release -p xcl
.\scripts\smoke.ps1 -Game '<Series title folder>' -Seconds 60

and paste both xcl-smoke v1 blocks (or drop them here so I can add them to the description), that would unblock merge review. Happy to update the PR body and mark ready once those are in.

No game data needed in the comment — only the smoke block text.

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>
@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of 6ce12404 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: 6ce1240498cd787448963da24cd8f5e892feb0da
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: 14686 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 99 % 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.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

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

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

NVIDIA test report for 6ce1240 (head as is, base 47efa2e), for #1607's "one more test report" list. RTX 3070 8 GB (Ampere, 32-lane waves), driver 616.56, Windows 11, debug, after cargo build --workspace:

  • cargo test --workspace --no-fail-fast -- --test-threads=4 --skip hle::xaudio2: 736 passed, 0 failed, 41 ignored. The end-to-end title tests pass.
  • Skipped hle::xaudio2, because its tests fail or hang on this machine on main too. The PR doesn't touch it.
  • SKIPPED lines: 2, both no wave64 on this card.

AI-assisted (Claude Code, Claude Opus 5.5). Synthetic tests only; no title run.

@Longno242

Copy link
Copy Markdown
Author

Thanks @upipibi — copied the workspace counts for 6ce1240 into Tested (736 passed, 0 failed, 41 ignored, hle::xaudio2 skipped).

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.

@gut0to gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted review: I inspected the diff and the loader write sites; I did not run tests.

Comment thread crates/xcl/src/pe.rs
/// Refuse an RVA the loader would write into when it still needs the headers intact
/// (IAT thunks, delay-load module handles, TLS `AddressOfIndex`). Targets must lie in
/// `[size_of_headers, size_of_image)`.
fn ensure_data_write_rva(&self, rva: u32) -> Result<(), PeError> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this validate the full write width, not only the starting RVA? thunks accepts an IAT slot at SizeOfImage - 4 because that start is in range, but the loader writes an entire u64 there (loader.rs IAT and delay-load module-handle writes); similarly, the TLS index write is a u32. A malformed image can therefore pass this new guard and make the loader write past the mapped image. Please use a checked [rva, rva + width) range check for each target and add upper-bound cases where the slot starts inside the image but ends beyond SizeOfImage. AI-assisted review: I inspected the diff and the loader write sites; I did not run tests.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The start-only check on this PR is the header case (#836). The full write is checked in #1203, which is the next commit on this branch.

ensure_data_write_rva(rva, width) requires [rva, rva + width) to sit in [SizeOfHeaders, SizeOfImage), with checked_add so the end cannot wrap. Call sites pass the width the loader actually stores: 8 for an IAT slot and a delay-load HMODULE, 4 for the TLS AddressOfIndex.

Upper-bound tests on that PR:

  • import_address_table_straddling_image_end_is_error — IAT at SizeOfImage - 4, rejected as BadRva
  • tls_index_straddling_image_end_is_error — TLS index at SizeOfImage - 2, rejected as BadRva

There is no separate delay-load HMODULE straddle test; that slot uses the same width-8 check as the IAT.

@Decodere

Copy link
Copy Markdown
Contributor

Independent desktop NVIDIA verification of PR #854 at head 6ce1240498cd787448963da24cd8f5e892feb0da (1 commit; exact parent & merge-base: 47efa2ecea0a12934719ae69b81930cf02a4b1c3, pre-#2555 ancestor of main).

Environment

  • GPU: NVIDIA GeForce RTX 4060 (8 GB / 7956 MiB reported by D3D12, Wave32-only Ada), driver 32.0.16.2114 (NVIDIA 621.14, WDDM 3.2)
  • CPU / RAM / OS: Intel Core i5-11400F (6C / 12T), 32 GB RAM (31.9 GiB usable), Windows 11 Home 26200 (Developer Mode off)
  • Toolchain: rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC, DXC 1.9.2609.5, --release

Targeted tests & build (6ce12404)

Command Result
cargo test --release -p xcl --lib -- --nocapture import_address_table_in_headers_is_error tls_index_in_headers_is_error delay_import_module_handle_in_headers_is_error write_targets_at_size_of_headers_are_accepted (3 consecutive runs) 4 passed; 0 failed; 0 ignored; 719 filtered out × 3 (0.00s, 0.00s, 0.00s, 0 skipped)
cargo test --release -p xcl --lib pe:: -- --nocapture (3 consecutive runs) 14 passed; 0 failed; 0 ignored; 709 filtered out × 3 (0.00s, 0.00s, 0.01s, 0 skipped)
cargo build --release -p xcl Clean build (55.45s, exit 0, 0 new warnings in pe.rs)

Paired quiet 60 s retail smoke (title: 29640639)

(Note: scripts/smoke.ps1 at 47efa2ec predates 9683e7a6 and printed the local folder basename on title:; replaced below with the standard 9683e7a6 title hash 29640639, with all measured fields verbatim.)

Exact parent & merge-base (47efa2ecea0a12934719ae69b81930cf02a4b1c3):

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1527 (PresentX 1527)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14723 MB

PR #854 (6ce1240498cd787448963da24cd8f5e892feb0da):

xcl-smoke v1
commit: 6ce1240498cd787448963da24cd8f5e892feb0da
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1586 (PresentX 1587)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14712 MB

Limits of coverage

  • All 4 new #836 header-write rejection / boundary tests (import_address_table_in_headers_is_error, tls_index_in_headers_is_error, delay_import_module_handle_in_headers_is_error, write_targets_at_size_of_headers_are_accepted) plus the full pe:: suite (14/14, 0 skipped) passed 3/3.
  • The 60 s retail smoke confirms real retail PE loading and runtime execution remain completely unaffected (0 device removals, 0 crashes, steady 60 Hz); single-run frame/commit delta vs. parent reflects normal startup variance.

AI disclosure: test execution and report drafting assisted by Google Antigravity (Gemini 4 Neon).

@Longno242

Copy link
Copy Markdown
Author

Thanks @Decodere. The release pe:: results for 6ce12404 are in Tested (four header-write tests 3/3, full pe:: 14/14 3/3).

The smoke blocks stay in this comment. scripts/smoke.ps1 printed a folder name on title:, and that line was replaced with 29640639 before posting. Regression run keeps the unedited pair from @Toons29 for this same head.

@relativespace2K

relativespace2K commented Nov 30, 2026 •

Copy link
Copy Markdown

merge-check
base: 53e85fb
head: 6ce1240
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2792 passed, 6 failed, 49 ignored; failing: gpu::dxil::compact_tests::gpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form, gpu::recomp::pso::shader_cache_tests::console_mesh_caches_hlsl_compute_pass_in_memory_and_on_disk, hle::d3d12x::standin::tests::concurrent_shell_miss_reuses_winner_and_releases_loser_pipeline, ram_budget::tests::windows_takes_a_working_set_maximum_and_lifts_it, and two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs (in --test gpu_two_pass_cache_e2e, so 5 failing tests give 6 failures). The same five fail on base 53e85fb alone (2788 passed, 6 failed, 49 ignored there), so none comes from this PR; its four new tests passed. 13 SKIPPED lines, all environmental: no D3D12 debug layer / d3d12SDKLayers.dll, no heap windows, no GPU_UPLOAD, per-lane f16/f32 forms refused.
review: no blocking finding; one file, every hunk read. One predicate, rva >= SizeOfHeaders && rva < SizeOfImage, now guards the three places the loader writes into a mapped image: each IAT slot as the thunks are walked, the delay-load HMODULE slot, and the TLS AddressOfIndex (with RVA 0 still meaning "no index"). Refusing the header range is right because those writes would land on the DOS/NT headers that the loader itself has already read and that a later step still needs; the upper bound reuses the existing BadRva, so nothing that was previously accepted beyond SizeOfImage changes. The tests build real little PE images for the three refusals and one acceptance at exactly SizeOfHeaders, which is the boundary that decides whether a legitimate image is turned away. Notes, not blocking: the TLS branch reports BadRva(0) rather than the address it refused (cosmetic in the message only), and a title whose IAT really does live in the headers would now fail to load instead of corrupting itself, which is the intent of #836 but is the one behaviour change to keep in mind. No duplicate: #854 is the only one touching this; #291 and #1217 are other loader fixes.
performance: one comparison per IAT slot and per delay-load entry, at load time.
verdict: safe to merge
hardware / tool and model: AMD Radeon RX 6800 XT 16 GB (RDNA2, wave64), driver 32.0.21045.29003; Ryzen 5 5600, 32 GB RAM; Windows 10 IoT Enterprise LTSC 2024, 10.0.26100 build 26100.9974; Rust 1.100.0; DXC 1.9.2602.17; debug build. DeepSeek Harness coding agent, model DeepSeek V4.1 Flash.

@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
@brainhay5

Copy link
Copy Markdown

merge-check claim
head: 6ce1240

@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
@brainhay5

Copy link
Copy Markdown

merge-check
base: cfb9753
head: 6ce1240
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2837 passed, 1 failed, 51 ignored; 29 SKIPPED lines
failing: test_title_runs_and_game_folder_is_untouched
review: automated overnight check: diff read for safety (no build/CI/toolchain/script changes, no process/network/runtime-loading code); no duplicate search performed.
performance: not assessed beyond the suite.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB, driver 610.88 (32.0.16.1088); Ryzen 7 5800X3D, 64 GB; Windows 11 22631; OpenCode agent, DeepSeek V4.1 Flash 1017

@Elytes

Elytes commented Nov 30, 2026

Copy link
Copy Markdown

merge-check
base: 9dfbe3e
head: 6ce1240
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2889 passed, 1 failed, 51 ignored; SKIPPED: 28 (22 no wave64, 2 no heap windows, 3 no D3D12 debug layer, 1 driver cache needs Developer Mode); failing: gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16, also failing on the base alone (base lib 2737 passed / 1 failed, merge 2741 / 1; the +4 are this PR's tests, passing).
review: no blocking finding after reading the whole diff and the comments. Real PEs: xcl inspect --json on 18 PE files of a retail title (its exe, 16 DLLs and one .bin; 1,924 IAT slots, 4 with TLS, no delay imports) gives byte-identical output on base and merge, so no legitimate import or TLS index is refused there. The delay-load HMODULE check is covered by its unit test only. Note, not blocking: loader.rs fails the load on an imports() error, but only warns on a delay_imports() error (the module then loads with no delay imports) and skips TLS silently on a tls() error, so a crafted delay-load HMODULE or TLS index in the headers now gives a module loaded without them rather than a refused load. That is how those paths already treat BadRva.
performance: one bounds check per IAT slot and per delay-load / TLS directory at load time (reasoned).
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 5090 (driver 32.0.16.1742, wave32), Intel Core i9-14900KS, 192 GB, Windows 11 build 26300.10075, rustc 1.100.1, debug / Claude Code (Claude Opus 5.5) ran the merge, build, suite and the inspect comparison on base and merge, and reviewed the diff, loader.rs and the comments.

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 6ce1240

#1203 (same author) is stacked on this PR: it contains this commit and adds a width check on top, so it is not a duplicate and this PR should land first. #1367 bounds IAT/HMODULE/relocation writes against SizeOfImage in loader.rs and pe.rs but does not refuse targets inside the headers, so it does not cover #836.
Minor: pe.rs tls() reports a TLS AddressOfIndex that va_to_rva cannot map as BadRva(0) rather than the VA.
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 ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loader: IAT thunk and TLS-index writes don't reject targets inside the headers (hardening parity with leonida#501)