diff options
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 533a0264f..1ab9c7801 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -487,12 +487,15 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 487 | nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) | 487 | nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) |
| 488 | throw std::runtime_error(name + ": not a PE32+ image"); | 488 | throw std::runtime_error(name + ": not a PE32+ image"); |
| 489 | auto& oh = nt->OptionalHeader; | 489 | auto& oh = nt->OptionalHeader; |
| 490 | uint64_t sections = static_cast<uint64_t>(dos->e_lfanew) + 4 + sizeof(IMAGE_FILE_HEADER) + | 490 | const uint64_t sections = static_cast<uint64_t>(dos->e_lfanew) + 4 + sizeof(IMAGE_FILE_HEADER) + |
| 491 | nt->FileHeader.SizeOfOptionalHeader; | 491 | nt->FileHeader.SizeOfOptionalHeader; |
| 492 | if (sections + static_cast<uint64_t>(nt->FileHeader.NumberOfSections) * sizeof(IMAGE_SECTION_HEADER) > file.size()) | 492 | const uint64_t headers_end = |
| 493 | sections + static_cast<uint64_t>(nt->FileHeader.NumberOfSections) * sizeof(IMAGE_SECTION_HEADER); | ||
| 494 | if (headers_end > file.size()) | ||
| 493 | throw std::runtime_error(name + ": the section table runs past the end of the file"); | 495 | throw std::runtime_error(name + ": the section table runs past the end of the file"); |
| 494 | if (!oh.SizeOfImage || oh.SizeOfHeaders > oh.SizeOfImage || oh.SizeOfHeaders > file.size()) | 496 | if (!oh.SizeOfImage || oh.SizeOfHeaders < headers_end || oh.SizeOfHeaders > oh.SizeOfImage || |
| 495 | throw std::runtime_error(name + ": SizeOfHeaders and SizeOfImage don't fit the file"); | 497 | oh.SizeOfHeaders > file.size()) |
| 498 | throw std::runtime_error(name + ": SizeOfHeaders and SizeOfImage don't fit the headers and file"); | ||
| 496 | 499 | ||
| 497 | auto* m = new Module; | 500 | auto* m = new Module; |
| 498 | m->name = name; | 501 | m->name = name; |
| @@ -533,8 +536,9 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 533 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); | 536 | auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); |
| 534 | auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; | 537 | auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; |
| 535 | while (reinterpret_cast<uint8_t*>(block) < end) { | 538 | while (reinterpret_cast<uint8_t*>(block) < end) { |
| 536 | if (block->SizeOfBlock < sizeof(IMAGE_BASE_RELOCATION) || | 539 | const size_t remaining = static_cast<size_t>(end - reinterpret_cast<uint8_t*>(block)); |
| 537 | reinterpret_cast<uint8_t*>(block) + block->SizeOfBlock > end) | 540 | if (remaining < sizeof(IMAGE_BASE_RELOCATION) || block->SizeOfBlock < sizeof(IMAGE_BASE_RELOCATION) || |
| 541 | block->SizeOfBlock > remaining) | ||
| 538 | throw std::runtime_error(m->name + ": a relocation block doesn't fit the relocation table"); | 542 | throw std::runtime_error(m->name + ": a relocation block doesn't fit the relocation table"); |
| 539 | auto* entries = reinterpret_cast<uint16_t*>(block + 1); | 543 | auto* entries = reinterpret_cast<uint16_t*>(block + 1); |
| 540 | size_t count = (block->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2; | 544 | size_t count = (block->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2; |