pe_loader: refuse an import address table or TLS index in the headers (RT-LOADER-HEADER-WRITES) #501
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/501"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-24T06:30:14Z.
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 beforeprotect_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. AFirstThunkor anAddressOfIndexbelowSizeOfHeaderstherefore replacese_lfanewor 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_importsreadsSizeOfHeadersbefore 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 samebind_thunks.initialize_tlsrefuses anAddressOfIndexbelowSizeOfHeaders("the TLS index lands in the headers"). RVA 0 is refused too:index_rvadoubles as "no index", so an index at 0 was silently never written.Found by mutation fuzzing
rt::Loader::load(in-process, neverinitialise(), so no image code runs). Without this change, 11 of 30,000 mutated images crashedprotect_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.test_pe_loader_boundswith this changeruntime/src/pe_loader.cpptaken frommainexpected image rejection, but Loader::load succeeded); the two "at exactly SizeOfHeaders loads" cases pass both wayspe_loader,pe_loader_bounds,loader,loader_tls,run_tiny_game*(15 tests)ctestgameinputis disabled in CMakepython -m compileall -q make_game.py tools testsscripts/check_no_game_data.pyon the two changed filesThe one failure is
d3d12_execute_indirect_validation. Its target does not linkpe_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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (Not applicable: no translator rule.)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-24T07:38:35Z.
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).