diff options
| author | quackottu <224479491+quackottu@users.noreply.github.com> | 2026-11-22 12:48:35 +0530 |
|---|---|---|
| committer | quackottu <224479491+quackottu@users.noreply.github.com> | 2026-11-22 12:48:35 +0530 |
| commit | 236b0acec3891b773ead57d3113b2f469d0e4a9f (patch) | |
| tree | beeaa4ed1f7b1ffa6e86d8bf3bc1b536b596cdc0 /runtime/src/pe_loader.cpp | |
| parent | c63773d51235d72250d48699f29830ac9ddafd60 (diff) | |
pe_loader: honour NumberOfRvaAndSizes, and refuse a forwarder ordinal past 65535
Data directories past NumberOfRvaAndSizes were read anyway (only the
exception directory checked the count), so stale bytes there were parsed
as import, delay-import, export or relocation tables. And resolve_forwarder
range-checked the ordinal before adding each digit, so '#65537' passed and
was cast to ordinal 1. The hand-built test images now declare their 16
directories.
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index ef6fab7a8..e82cee3ed 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -47,6 +47,11 @@ std::string read_string(const std::string& name, uint8_t* base, uint32_t image_s | |||
| 47 | return {s, end}; | 47 | return {s, end}; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | // PE/COFF: NumberOfRvaAndSizes counts the data directories; entries past it are not directories, whatever they hold. | ||
| 51 | IMAGE_DATA_DIRECTORY directory(const IMAGE_NT_HEADERS64* nt, unsigned index) { | ||
| 52 | return index < nt->OptionalHeader.NumberOfRvaAndSizes ? nt->OptionalHeader.DataDirectory[index] : IMAGE_DATA_DIRECTORY{}; | ||
| 53 | } | ||
| 54 | |||
| 50 | // The TLS directory holds virtual addresses, which relocations have already moved to this mapping. | 55 | // The TLS directory holds virtual addresses, which relocations have already moved to this mapping. |
| 51 | uint64_t rva_in(const Module& m, uint64_t va, const char* what) { | 56 | uint64_t rva_in(const Module& m, uint64_t va, const char* what) { |
| 52 | const uint64_t base = reinterpret_cast<uint64_t>(m.base); | 57 | const uint64_t base = reinterpret_cast<uint64_t>(m.base); |
| @@ -190,8 +195,7 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 190 | // allowed. No entry in that set uses the indirect form (UnwindData bit 0). | 195 | // allowed. No entry in that set uses the indirect form (UnwindData bit 0). |
| 191 | void Loader::register_unwind(Module& m) { | 196 | void Loader::register_unwind(Module& m) { |
| 192 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 197 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 193 | if (nt->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXCEPTION) return; | 198 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_EXCEPTION); |
| 194 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION]; | ||
| 195 | // Like the system loader, a trailing partial entry is ignored. | 199 | // Like the system loader, a trailing partial entry is ignored. |
| 196 | const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION); | 200 | const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION); |
| 197 | if (!count) return; | 201 | if (!count) return; |
| @@ -276,7 +280,7 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 276 | m->base = static_cast<uint8_t*>( | 280 | m->base = static_cast<uint8_t*>( |
| 277 | VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 281 | VirtualAlloc(reinterpret_cast<void*>(oh.ImageBase), oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 278 | if (!m->base) { | 282 | if (!m->base) { |
| 279 | auto& relocs = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; | 283 | const auto relocs = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); |
| 280 | if (!relocs.Size) | 284 | if (!relocs.Size) |
| 281 | throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations"); | 285 | throw std::runtime_error(m->name + ": preferred base is taken and the image has no relocations"); |
| 282 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 286 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| @@ -297,7 +301,7 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 297 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); | 301 | int64_t delta = reinterpret_cast<int64_t>(m->base) - static_cast<int64_t>(oh.ImageBase); |
| 298 | if (delta) { | 302 | if (delta) { |
| 299 | m->relocated = true; | 303 | m->relocated = true; |
| 300 | auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; | 304 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_BASERELOC); |
| 301 | if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); | 305 | if (!dir.Size) throw std::runtime_error(m->name + ": the image has no relocations and isn't at its base"); |
| 302 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); | 306 | require_range(m->name, dir.VirtualAddress, dir.Size, m->size, "the relocation table"); |
| 303 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); | 307 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); |
| @@ -455,7 +459,7 @@ void Loader::run_tls_callbacks(Module& m, uint32_t reason) { | |||
| 455 | 459 | ||
| 456 | void Loader::parse_exports(Module& m) { | 460 | void Loader::parse_exports(Module& m) { |
| 457 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); | 461 | auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew); |
| 458 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | 462 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_EXPORT); |
| 459 | if (!dir.Size) return; | 463 | if (!dir.Size) return; |
| 460 | require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_EXPORT_DIRECTORY), m.size, "the export directory"); | 464 | require_range(m.name, dir.VirtualAddress, sizeof(IMAGE_EXPORT_DIRECTORY), m.size, "the export directory"); |
| 461 | auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress); | 465 | auto* ed = at<IMAGE_EXPORT_DIRECTORY>(m.base, dir.VirtualAddress); |
| @@ -509,8 +513,9 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d | |||
| 509 | uint32_t ordinal = 0; | 513 | uint32_t ordinal = 0; |
| 510 | if (by_ordinal) { | 514 | if (by_ordinal) { |
| 511 | for (size_t i = 1; i < symbol.size(); ++i) { | 515 | for (size_t i = 1; i < symbol.size(); ++i) { |
| 512 | if (symbol[i] < '0' || symbol[i] > '9' || ordinal > 0xFFFF) return nullptr; | 516 | if (symbol[i] < '0' || symbol[i] > '9') return nullptr; |
| 513 | ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0'); | 517 | ordinal = ordinal * 10 + static_cast<uint32_t>(symbol[i] - '0'); |
| 518 | if (ordinal > 0xFFFF) return nullptr; // checked after the digit, so "#65537" can't truncate to 1 | ||
| 514 | } | 519 | } |
| 515 | } | 520 | } |
| 516 | 521 | ||
| @@ -595,7 +600,7 @@ void Loader::bind_imports(Module& m) { | |||
| 595 | } | 600 | } |
| 596 | }; | 601 | }; |
| 597 | 602 | ||
| 598 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; | 603 | const auto dir = directory(nt, IMAGE_DIRECTORY_ENTRY_IMPORT); |
| 599 | if (dir.Size) { | 604 | if (dir.Size) { |
| 600 | for (uint64_t i = 0;; ++i) { | 605 | for (uint64_t i = 0;; ++i) { |
| 601 | uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR); | 606 | uint64_t rva = dir.VirtualAddress + i * sizeof(IMAGE_IMPORT_DESCRIPTOR); |
| @@ -608,7 +613,7 @@ void Loader::bind_imports(Module& m) { | |||
| 608 | } | 613 | } |
| 609 | } | 614 | } |
| 610 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. | 615 | // Delay imports are bound eagerly: simpler, and the full import surface shows up in --dry-run. |
| 611 | auto& ddir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; | 616 | const auto ddir = directory(nt, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT); |
| 612 | if (ddir.Size) { | 617 | if (ddir.Size) { |
| 613 | for (uint64_t i = 0;; ++i) { | 618 | for (uint64_t i = 0;; ++i) { |
| 614 | uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); | 619 | uint64_t rva = ddir.VirtualAddress + i * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); |