From 83d973a424afa3a5c21a52683154b770e4702065 Mon Sep 17 00:00:00 2001 From: Dominik <55330408+Dom-in@users.noreply.github.com> Date: Sun, 22 Nov 2026 15:22:13 +0000 Subject: pe_loader: read the TLS directory through directory(), honouring NumberOfRvaAndSizes Every data directory the loader reads goes through directory(), which returns an empty entry past OptionalHeader.NumberOfRvaAndSizes (PE/COFF: those slots are not directories, whatever bytes they hold) - except the TLS one, which read DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] directly. An image declaring fewer than 10 directories with leftover bytes in slot 9 then failed to load ("the TLS directory runs past the image") or, with plausible bytes, had a TLS directory the Windows loader would ignore. loader_test: with NumberOfRvaAndSizes = 9 and junk in the TLS slot the image loads (it was rejected before); with all 16 the same slot is still checked and refused. Co-Authored-By: Claude Opus 5.5 --- runtime/src/pe_loader.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index bdb3df6f5..b78053d01 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -64,7 +64,8 @@ uint64_t rva_in(const Module& m, uint64_t va, const char* what) { IMAGE_TLS_DIRECTORY64* tls_directory(Module& m) { auto* nt = at(m.base, reinterpret_cast(m.base)->e_lfanew); - const auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS]; + // Through directory(), as every other directory: an entry past NumberOfRvaAndSizes is not a TLS directory. + const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_TLS); if (!dir.Size || !dir.VirtualAddress) return nullptr; require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_TLS_DIRECTORY64), m.size, "the TLS directory"); return at(m.base, dir.VirtualAddress); -- cgit v1.2.3 From 585143544e76954905535335ab98e7ab2ea7c23c Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 22 Nov 2026 18:00:33 +0000 Subject: Reject out-of-image entry points before loader initialization Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- runtime/src/pe_loader.cpp | 3 ++- runtime/test/loader_test.cpp | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index bdb3df6f5..c19321684 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -574,7 +574,6 @@ void Loader::initialise(Module& m) { // Only a DLL gets DllMain. An EXE's entry point has a different signature and is the caller's to run. auto* nt = at(m.base, reinterpret_cast(m.base)->e_lfanew); if ((nt->FileHeader.Characteristics & IMAGE_FILE_DLL) && m.entry_rva) { - if (m.entry_rva >= m.size) throw std::runtime_error(m.name + ": the entry point is outside the image"); auto entry = reinterpret_cast(m.base + m.entry_rva); const BOOL ok = entry(m.base, DLL_PROCESS_ATTACH, nullptr); if (startup_trace::enabled()) @@ -619,6 +618,8 @@ Module& Loader::map_image(const std::filesystem::path& path) { if (!oh.SizeOfImage || oh.SizeOfHeaders < headers_end || oh.SizeOfHeaders > oh.SizeOfImage || oh.SizeOfHeaders > file.size()) throw std::runtime_error(name + ": SizeOfHeaders and SizeOfImage don't fit the headers and file"); + if (oh.AddressOfEntryPoint && oh.AddressOfEntryPoint >= oh.SizeOfImage) + throw std::runtime_error(name + ": the entry point is outside the image"); auto* m = new Module; m->name = name; diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 194174b00..9ff3d08c2 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp @@ -184,6 +184,28 @@ void expect_rejects(const char* what, const std::vector& bytes) { void test_valid_image() { expect_loads("a valid image maps with its section intact", make_image()); } +void test_entry_bounds() { + for (bool is_dll : {false, true}) { + for (uint32_t entry : {0u, 0x1000u, 0x1FFFu, 0x2000u, 0x2001u}) { + auto img = make_image(); + if (is_dll) nt_of(img)->FileHeader.Characteristics |= IMAGE_FILE_DLL; + nt_of(img)->OptionalHeader.AddressOfEntryPoint = entry; + const std::string what = std::string(is_dll ? "DLL" : "EXE") + " entry RVA " + std::to_string(entry); + if (entry < nt_of(img)->OptionalHeader.SizeOfImage) { + expect_loads(what.c_str(), img); + } else { + std::string error; + if (load(write_image(img), error) == 0) + failed(what.c_str(), "the image loaded"); + else if (error.find("the entry point is outside the image") == std::string::npos) + failed(what.c_str(), "unexpected error: " + error); + else + pass(what.c_str()); + } + } + } +} + void test_header_bounds() { { auto img = make_image(); @@ -1093,6 +1115,7 @@ int main() { return 1; } test_valid_image(); + test_entry_bounds(); test_header_bounds(); test_section_bounds(); test_truncation(); -- cgit v1.2.3