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:
authorQemmHD <stephenlethco2@gmail.com>2026-11-24 02:38:37 -0500
committerGitHub <noreply@github.com>2026-11-24 02:38:37 -0500
commit5aedbc2230d126257c1690d8f169661d1329e6e8 (patch)
treed9be4a87650f311ac865031e52e223edd44f84a7 /runtime/src/pe_loader.cpp
parentb659fdbc3983452de2c3611c4945ab4d19363efb (diff)
parentdb8a0cc793b722be588ddbeade2d2c2b7d4b7373 (diff)
Merge pull request #501 from Kal0s-design/fix/loader-iat-tls-header-writes
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).
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 }