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

Closed
opened 2026-11-26 20:18:15 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/501
Author: @Kal0s-design
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @QemmHD

Base: main ← Head: fix/loader-iat-tls-header-writes


📝 Commits (1)

  • db8a0cc pe_loader: refuse an import address table or TLS index in the headers

📊 Changes

2 files changed (+60 additions, -0 deletions)

View changed files

📝 runtime/src/pe_loader.cpp (+10 -0)
📝 runtime/test/test_pe_loader_bounds.cpp (+50 -0)

📄 Description

Claimed in the coordination issue as: RT-LOADER-HEADER-WRITES (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7123988950)

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/501 **Author:** [@Kal0s-design](https://github.com/Kal0s-design) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@QemmHD](https://github.com/QemmHD) **Base:** `main` ← **Head:** `fix/loader-iat-tls-header-writes` --- ### 📝 Commits (1) - [`db8a0cc`](https://github.com/ubiquitarianism/leonida/commit/db8a0cc793b722be588ddbeade2d2c2b7d4b7373) pe_loader: refuse an import address table or TLS index in the headers ### 📊 Changes **2 files changed** (+60 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/pe_loader.cpp` (+10 -0) 📝 `runtime/test/test_pe_loader_bounds.cpp` (+50 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-LOADER-HEADER-WRITES (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7123988950) ### 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:18:15 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#551
No description provided.