pe_loader: refuse an import address table or TLS index in the headers (RT-LOADER-HEADER-WRITES) #501

Manually merged
github merged 1 commit from gh-pr/501/head into gh-pr/501/base 2026-11-26 09:04:26 +00:00
Owner

@⁠Kal0s-design on GitHub · 2026-11-24 06:30 UTC · original
Kal0s-design:fix/loader-iat-tls-header-writes → main · +60 −0 · 2 files · 1 commits · merged 2026-11-24 07:38 UTC

Claimed in the coordination issue as: RT-LOADER-HEADER-WRITES (#14)

What and why

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 later step (parse_exports, protect_sections, register_unwind, initialise, ~Loader) reads the headers back from the mapping. A FirstThunk or an AddressOfIndex below SizeOfHeaders therefore replaces e_lfanew or the section table, and the next read goes outside the image or uses headers nobody validated. The loader already refuses a section or a base relocation there; this closes the two remaining writes.

  • bind_imports reads SizeOfHeaders before the first address is written and refuses an import address table slot below it ("an import address table lands in the headers"). Delay imports go through the same bind_thunks.
  • initialize_tls refuses an AddressOfIndex below SizeOfHeaders ("the TLS index lands in the headers"). RVA 0 is refused too: index_rva doubles as "no index", so an index at 0 was silently never written.

Found by mutation fuzzing rt::Loader::load (in-process, never initialise(), so no image code runs). Without this change, 11 of 30,000 mutated images crashed protect_sections, all with an import address table moved into the headers.

Tested

Windows 11, Visual Studio 18 (MSVC), Ninja, RelWithDebInfo, on current main (8f481cf16) plus this commit. CPU only: no GPU is involved and no game files were used.

Check Result
test_pe_loader_bounds with this change 60 of 60 checks pass, including the 6 new ones
Same test with runtime/src/pe_loader.cpp taken from main 4 failures, the four new rejections ("IAT at RVA 0x38", "IAT ending on SizeOfHeaders", "TLS index at RVA 0x3C", "TLS index at RVA 0": expected image rejection, but Loader::load succeeded); the two "at exactly SizeOfHeaders loads" cases pass both ways
pe_loader, pe_loader_bounds, loader, loader_tls, run_tiny_game* (15 tests) all pass
Whole ctest 1 failure out of 293 tests run (the others pass, or skip themselves for lack of game files or a feature); gameinput is disabled in CMake
python -m compileall -q make_game.py tools tests ok
scripts/check_no_game_data.py on the two changed files ok

The one failure is d3d12_execute_indirect_validation. Its target does not link pe_loader.cpp, so this change cannot affect it; I did not investigate it.

Not covered: Linux and macOS (the loader and this test are Windows and MSVC only), and the retail executable (no game files here), so whether any shipped module has such a table is not checked. The same change was checked independently by several people on the pull request in the original repository before it went private; those results do not carry over, so the numbers above are my own.

GPUs tested: n/a (CPU only)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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 rule.)

🤖 Generated with Claude Code

**@⁠Kal0s-design** on GitHub · 2026-11-24 06:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/501) `Kal0s-design:fix/loader-iat-tls-header-writes` → `main` · +60 −0 · 2 files · 1 commits · merged 2026-11-24 07:38 UTC Claimed in the coordination issue as: RT-LOADER-HEADER-WRITES (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why 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 later step (`parse_exports`, `protect_sections`, `register_unwind`, `initialise`, `~Loader`) reads the headers back from the mapping. A `FirstThunk` or an `AddressOfIndex` below `SizeOfHeaders` therefore replaces `e_lfanew` or the section table, and the next read goes outside the image or uses headers nobody validated. The loader already refuses a section or a base relocation there; this closes the two remaining writes. - `bind_imports` reads `SizeOfHeaders` before the first address is written and refuses an import address table slot below it ("an import address table lands in the headers"). Delay imports go through the same `bind_thunks`. - `initialize_tls` refuses an `AddressOfIndex` below `SizeOfHeaders` ("the TLS index lands in the headers"). RVA 0 is refused too: `index_rva` doubles as "no index", so an index at 0 was silently never written. Found by mutation fuzzing `rt::Loader::load` (in-process, never `initialise()`, so no image code runs). Without this change, 11 of 30,000 mutated images crashed `protect_sections`, all with an import address table moved into the headers. ### Tested Windows 11, Visual Studio 18 (MSVC), Ninja, RelWithDebInfo, on current `main` (`8f481cf16`) plus this commit. CPU only: no GPU is involved and no game files were used. | Check | Result | |---|---| | `test_pe_loader_bounds` with this change | 60 of 60 checks pass, including the 6 new ones | | Same test with `runtime/src/pe_loader.cpp` taken from `main` | 4 failures, the four new rejections ("IAT at RVA 0x38", "IAT ending on SizeOfHeaders", "TLS index at RVA 0x3C", "TLS index at RVA 0": `expected image rejection, but Loader::load succeeded`); the two "at exactly SizeOfHeaders loads" cases pass both ways | | `pe_loader`, `pe_loader_bounds`, `loader`, `loader_tls`, `run_tiny_game*` (15 tests) | all pass | | Whole `ctest` | 1 failure out of 293 tests run (the others pass, or skip themselves for lack of game files or a feature); `gameinput` is disabled in CMake | | `python -m compileall -q make_game.py tools tests` | ok | | `scripts/check_no_game_data.py` on the two changed files | ok | The one failure is `d3d12_execute_indirect_validation`. Its target does not link `pe_loader.cpp`, so this change cannot affect it; I did not investigate it. Not covered: Linux and macOS (the loader and this test are Windows and MSVC only), and the retail executable (no game files here), so whether any shipped module has such a table is not checked. The same change was checked independently by several people on the pull request in the original repository before it went private; those results do not carry over, so the numbers above are my own. GPUs tested: n/a (CPU only) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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 rule.) 🤖 Generated with [Claude Code](https://claude.com/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. The loader already 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.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:04:24 +00:00
github manually merged commit db8a0cc793 into gh-pr/501/base 2026-11-26 09:04:26 +00:00
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 07:38 UTC · original

Merging. Why merged: the PE loader now refuses an import address table or TLS index placed inside the headers, where the write would overwrite e_lfanew.
Why it's good: security-reviewed; built with VS 2022 + Ninja on main d42773d: the 4 new rejection cases fail on main; with this PR pe_loader, pe_loader_bounds and pe_loader_fuzz pass, and 7/7 loader-related ctests pass.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-24 07:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/501#issuecomment-7125212177) Merging. Why merged: the PE loader now refuses an import address table or TLS index placed inside the headers, where the write would overwrite e_lfanew. Why it's good: security-reviewed; built with VS 2022 + Ninja on main d42773d: the 4 new rejection cases fail on main; with this PR pe_loader, pe_loader_bounds and pe_loader_fuzz pass, and 7/7 loader-related ctests pass. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.