From db8a0cc793b722be588ddbeade2d2c2b7d4b7373 Mon Sep 17 00:00:00 2001 From: Kal0s-design <356578040+Kal0s-design@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:57:43 +0100 Subject: pe_loader: refuse an import address table or TLS index in the headers 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 --- runtime/src/pe_loader.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index ff0693a15..c29a288a9 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -1527,6 +1527,11 @@ void Loader::initialize_tls(Module& m) { if (tls->AddressOfIndex) { index_rva = rva_in(m, tls->AddressOfIndex, "the TLS index"); require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index"); + // The index is written below, and every later step reads the headers back from the mapping. An index at + // RVA 0 would not be written at all (index_rva doubles as "no index"). + const auto* nt = at(m.base, reinterpret_cast(m.base)->e_lfanew); + if (index_rva < nt->OptionalHeader.SizeOfHeaders) + throw std::runtime_error(m.name + ": the TLS index lands in the headers"); } if (!module_tls_slots()) throw std::runtime_error(m.name + ": this thread has no TLS array"); @@ -1786,6 +1791,9 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name void Loader::bind_imports(Module& m) { auto* nt = at(m.base, reinterpret_cast(m.base)->e_lfanew); + // Read before the first address is written: an entry in the headers would replace what the steps after this + // one read back from the mapping (e_lfanew, the section table), so it is refused like a section there. + const uint64_t headers_end = nt->OptionalHeader.SizeOfHeaders; // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image. auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva) { @@ -1802,6 +1810,8 @@ void Loader::bind_imports(Module& m) { uint32_t hint = static_cast(lookup->u1.AddressOfData); name = read_string(m.name, m.base, m.size, hint + sizeof(uint16_t), "an import name"); } + if (iat_rva < headers_end) + throw std::runtime_error(m.name + ": an import address table lands in the headers"); at(m.base, static_cast(iat_rva))->u1.Function = reinterpret_cast(resolve(m, dll, name, ord, by_ord)); } -- cgit v1.2.3