Skip to content

pe_loader: refuse an import address table or TLS index in the headers - #5677

Closed
Kal0s-design wants to merge 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/loader-iat-tls-header-writes
Closed

Kal0s-design wants to merge 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/loader-iat-tls-header-writes

Conversation

@Kal0s-design

@Kal0s-design Kal0s-design commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: mutation fuzzer for the PE loader

What and why

This is the follow-up #4329 left open ("an import address table entry can still land in the headers"), plus the one other write of the same kind.

After map_image the loader writes into the mapped image twice before protect_sections: the bound address of every import (bind_imports) and the image's TLS index (initialize_tls). Every step after that reads the headers back from the mapping (parse_exports, bind_imports, protect_sections, register_unwind, initialise, ~Loader). A FirstThunk or an AddressOfIndex below SizeOfHeaders therefore replaced e_lfanew or the section table, and the next step read outside the image or used headers nothing had validated.

Both writes are now refused when they land below SizeOfHeaders, the same rule #4329 applies to sections and base relocations. A TLS index at RVA 0 is refused too, because index_rva doubles as "no index" and such an index was silently never written.

I found it by mutation fuzzing rt::Loader::load on top of #4329, using the locally built tiny_* fixtures as seeds and never running image code. On that tree 30,000 mutated images gave 11 access violations, all in protect_sections, and all were an import address table inside the headers. The fuzzer itself is not part of this PR.

#4313 also changes initialize_tls, a few lines below the new check. It already conflicts with main in the same file since #4329, so this adds no new conflict, but whichever lands second needs a small rebase.

Tested

Windows, MSVC RelWithDebInfo, NVIDIA for the GPU tests, on 561b65a83.

What Before After
test_pe_loader_bounds, six new hand-built images 4 of 6 fail (the image loads with overwritten headers) 60/60 checks pass
Full ctest 235/237 pass
The 11 fuzzer crash images, replayed 11 access violations 11 clean rejections, also under AddressSanitizer
Fuzzing the fixed loader 30,000 images with 0 crashes, then 20,000 under AddressSanitizer with 0 reports

The two ctest failures are d3d12_indirect_dispatch_count and d3d12_bindless_churn_validated. Neither executable compiles or links pe_loader.cpp, so they cannot be affected by this change. I did not investigate them further.

Not covered:

  • No Linux run. The loader and its tests only build on Windows.
  • Only load() is exercised, not initialise(), which runs image code.
  • No game files or game executable were used.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable, no translator change.)

AI-assisted: yes (Claude Code).

🤖 Generated with Claude Code

After map_image, the loader writes into the mapped image twice: the bound
address of every import and the image's TLS index. Every step after it
(parse_exports, bind_imports, protect_sections, register_unwind,
initialise, ~Loader) reads the headers back from the mapping. A
FirstThunk or AddressOfIndex below SizeOfHeaders therefore replaced
e_lfanew or the section table, and the next read went outside the image
or used headers nobody validated. ProjectLeonida#4329 refuses a section or a base
relocation there; this closes the two remaining writes.

An index at RVA 0 is refused too: index_rva doubles as "no index", so the
index was silently never written.

Found by mutation fuzzing rt::Loader::load. The six new cases in
test_pe_loader_bounds build their images by hand; four of them fail
without this change.

Depends on: ProjectLeonida#4329

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milas6669

Copy link
Copy Markdown

Independent check of 5174d58b (on current main 561b65a8, the same base): pass, and the test fails without the change.

Windows 11, MSVC 19.44 + Ninja, RelWithDebInfo, CPU only (no GPU involved, no image code run), NVIDIA GeForce RTX 3070 present but unused:

  • test_pe_loader_bounds at the PR head: ALL 60 CHECKS PASSED, exit 0, including Import address table at RVA 0x38 is rejected, Import address table ending on SizeOfHeaders is rejected, TLS index at RVA 0x3C is rejected, TLS index at RVA 0 is rejected, and the two boundary cases that must still load (... at exactly SizeOfHeaders loads).
  • Negative control: only runtime/src/pe_loader.cpp reverted to origin/main, test file unchanged: FAILED (60 checks, 4 failures), exit 1. The four failures are exactly the four "must be rejected" cases (expected image rejection, but Loader::load succeeded); the two "at exactly SizeOfHeaders loads" cases and the DIR64 relocation case still pass. So the test pins this change and nothing else.
  • Regression: loader_test at the PR head: 86 checks, 0 failed, exit 0.

Not tested: the mutation fuzzer behind the finding (it is not part of this PR, so the 30,000-image / 11-access-violation count is not reproduced here), Wine, and the interaction with #4313, which the description says also changes initialize_tls a few lines below (it conflicts with main already, so whichever lands second needs a rebase).

AI-assisted. Synthetic images only; no game data.

@kapitche

Copy link
Copy Markdown
Collaborator

Tested 5174d58 merged with main 561b65a on Windows 11 (MSVC 14.51 / VS 18, RelWithDebInfo, Ninja, native; the test is Windows-only) and Python 3.12.16 on Linux for the merge: pass

runtime/test/test_pe_loader_bounds.exe built from the merged tree (PR head is already on current main, merge was a no-op): ALL 60 CHECKS PASSED, including the 6 new ones (IAT at RVA 0x38 and 0x1F8 rejected, IAT at exactly SizeOfHeaders loads, TLS index at RVA 0x3C and 0 rejected, TLS index at exactly SizeOfHeaders loads).

Negative control: with runtime/src/pe_loader.cpp taken from main 561b65a and everything else unchanged, the same test rebuilt gives "FAILED (60 checks, 4 failures)": the four rejection cases (IAT at 0x38, IAT ending on SizeOfHeaders, TLS index at 0x3C, TLS index at 0) load successfully instead of being refused; the two "exactly SizeOfHeaders loads" cases pass on both. The diff reads correctly: headers_end is read before the first thunk write, and the boundary matches the tests (a slot below SizeOfHeaders is refused, one at it is not).

@comtek21516

Copy link
Copy Markdown

Tested 5174d58bc merged onto main at dde82f27e (it merges without conflicts) on Windows Server 2025, MSVC 14.52 (Visual Studio 18), Ninja RelWithDebInfo, no GPU: pass.

  • Build: all 428 steps, no new warnings from the two changed files.
  • ctest: pe_loader_bounds, pe_loader, loader, run_tiny_game and the gta6rt_* tests pass. Whole suite: 146 passed, 122 skipped (no Vulkan driver here), 4 failed; the four are gameinput_host, d3d12_sparse_queue_family, vk_bindless_ps_tex and vk_bindless_ps_imgatomic, which fail the same way on main on this machine (runtime tests: skip, not fail, without a Vulkan driver or a working host GameInput (4 ctests) #7452) and do not touch the loader.
  • Negative control: with runtime/src/pe_loader.cpp taken from main and this PR's test kept, pe_loader_bounds fails on exactly the four new rejections ("Import address table at RVA 0x38", "... ending on SizeOfHeaders", "TLS index at RVA 0x3C", "TLS index at RVA 0": expected image rejection, but Loader::load succeeded), and the two "at exactly SizeOfHeaders loads" cases pass. With the PR's loader all six pass.
  • Code read: both writers go through the new check: the import descriptors and the delay-import descriptors call the same bind_thunks, and the check sits before the write of each slot, so a table that starts above the headers cannot walk back into them.

Not checked: the retail executable (no game files used), so whether any shipped module has such a table is not covered by this run.

AI-assisted (Claude Code).

@mrbean56

Copy link
Copy Markdown

Read 5174d58bc7 on Linux: the fix is right in kind; this target is MSVC-only (/W4 /permissive-), so this is a read, not a run.

  • Both writes that happen before protect_sections() are covered: the IAT slot (an 8-byte write that can cover e_lfanew at 0x3C) and the TLS index. Refusing any of them below SizeOfHeaders keeps the later parse_exports/initialize_tls reads of the headers meaningful, and it mirrors the existing refusal of a section or relocation there.
  • The boundary is the useful part: RVA 0x1F8 (the last slot that ends at SizeOfHeaders) is rejected, exactly 0x200 loads. The TLS test also covers RVA 0, which previously meant "no index" and was silently skipped.
  • Call order checked: initialize_tls runs inside map_image after relocations (which are themselves header-checked), and bind_imports after it, so both reads see intact headers.
  • CI on Windows should run test_pe_loader_bounds before this lands; I cannot build it here.

No GPU, no game data. AI-assisted.

@DaniiL35

Copy link
Copy Markdown
Contributor

Independent fuzz check of 5174d58, on Windows 11 with MSVC 14.44 and /fsanitize=address. CPU only, no image code run (load() only, never initialise()), seeds are the locally built tiny_dll, tiny_exe and tiny_tls fixtures, no game files.

  • Without this PR, at 867d922: 6 parallel mutation fuzzers on rt::Loader::load all stopped on an ASan access-violation in protect_sections, three at pe_loader.cpp:1667 (the SizeOfHeaders read through a clobbered e_lfanew) and three at :1670 (the section-table walk). In all six cases the fuzzer had moved one import descriptor's FirstThunk to 0xff, 7, 0x10 or 1. In one case that was the only byte changed.
  • With this PR: the same six inputs are each refused with "an import address table lands in the headers".
  • 20 more minutes on top of this PR: 72,085 mutated images (35,391 of them loaded), reaching 59–62 distinct loader outcomes per worker, with 0 ASan reports and no exception other than std::runtime_error.

So this independently confirms the crash and the fix, with inputs other than the ones in the PR. The fuzzer is a local harness, not part of any PR.

AI-assisted (Claude Code).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants