upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
authorKal0s-design <356578040+Kal0s-design@users.noreply.github.com>2026-11-23 09:57:43 +0100
committerKal0s-design <356578040+Kal0s-design@users.noreply.github.com>2026-11-24 07:29:32 +0100
commitdb8a0cc793b722be588ddbeade2d2c2b7d4b7373 (patch)
treec39d633874e6eca67550f861656d932bda4ea556 /runtime/src/pe_loader.cpp
parent8f481cf16f261c7f37458aeb45e8f4e19994146d (diff)
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp10
1 files changed, 10 insertions, 0 deletions
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) {
1527 if (tls->AddressOfIndex) { 1527 if (tls->AddressOfIndex) {
1528 index_rva = rva_in(m, tls->AddressOfIndex, "the TLS index"); 1528 index_rva = rva_in(m, tls->AddressOfIndex, "the TLS index");
1529 require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index"); 1529 require_range(m.name, index_rva, sizeof(uint32_t), m.size, "the TLS index");
1530 // The index is written below, and every later step reads the headers back from the mapping. An index at
1531 // RVA 0 would not be written at all (index_rva doubles as "no index").
1532 const auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
1533 if (index_rva < nt->OptionalHeader.SizeOfHeaders)
1534 throw std::runtime_error(m.name + ": the TLS index lands in the headers");
1530 } 1535 }
1531 1536
1532 if (!module_tls_slots()) throw std::runtime_error(m.name + ": this thread has no TLS array"); 1537 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
1786 1791
1787void Loader::bind_imports(Module& m) { 1792void Loader::bind_imports(Module& m) {
1788 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); 1793 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
1794 // Read before the first address is written: an entry in the headers would replace what the steps after this
1795 // one read back from the mapping (e_lfanew, the section table), so it is refused like a section there.
1796 const uint64_t headers_end = nt->OptionalHeader.SizeOfHeaders;
1789 1797
1790 // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image. 1798 // Thunk tables have no length field: they end at a zero entry, so each step is checked against the image.
1791 auto bind_thunks = [&](const std::string& dll, uint64_t lookup_rva, uint64_t iat_rva) { 1799 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) {
1802 uint32_t hint = static_cast<uint32_t>(lookup->u1.AddressOfData); 1810 uint32_t hint = static_cast<uint32_t>(lookup->u1.AddressOfData);
1803 name = read_string(m.name, m.base, m.size, hint + sizeof(uint16_t), "an import name"); 1811 name = read_string(m.name, m.base, m.size, hint + sizeof(uint16_t), "an import name");
1804 } 1812 }
1813 if (iat_rva < headers_end)
1814 throw std::runtime_error(m.name + ": an import address table lands in the headers");
1805 at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function = 1815 at<IMAGE_THUNK_DATA64>(m.base, static_cast<uint32_t>(iat_rva))->u1.Function =
1806 reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord)); 1816 reinterpret_cast<uint64_t>(resolve(m, dll, name, ord, by_ord));
1807 } 1817 }