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:
authortotonii <totonicontato@gmail.com>2026-11-22 08:55:31 -0300
committertotonii <totonicontato@gmail.com>2026-11-22 08:55:31 -0300
commite49a735134b7ba8236e9914b57f5c0ce30727300 (patch)
treefe897aaf7c61d3281f4bd23aed904bcea21c363e /runtime/src/pe_loader.cpp
parentbb2e18421dac527374eb837c88b5063c5d96f81f (diff)
fix(pe_loader): validate SizeOfHeaders covers NT/section headers and guard truncated relocation blocks
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp18
1 files changed, 11 insertions, 7 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index 15a62d9b4..3e2a1a725 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -292,12 +292,15 @@ Module& Loader::map_image(const std::filesystem::path& path) {
292 nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) 292 nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC)
293 throw std::runtime_error(name + ": not a PE32+ image"); 293 throw std::runtime_error(name + ": not a PE32+ image");
294 auto& oh = nt->OptionalHeader; 294 auto& oh = nt->OptionalHeader;
295 uint64_t sections = static_cast<uint64_t>(dos->e_lfanew) + 4 + sizeof(IMAGE_FILE_HEADER) + 295 const uint64_t sections = static_cast<uint64_t>(dos->e_lfanew) + 4 + sizeof(IMAGE_FILE_HEADER) +
296 nt->FileHeader.SizeOfOptionalHeader; 296 nt->FileHeader.SizeOfOptionalHeader;
297 if (sections + static_cast<uint64_t>(nt->FileHeader.NumberOfSections) * sizeof(IMAGE_SECTION_HEADER) > file.size()) 297 const uint64_t headers_end =
298 sections + static_cast<uint64_t>(nt->FileHeader.NumberOfSections) * sizeof(IMAGE_SECTION_HEADER);
299 if (headers_end > file.size())
298 throw std::runtime_error(name + ": the section table runs past the end of the file"); 300 throw std::runtime_error(name + ": the section table runs past the end of the file");
299 if (!oh.SizeOfImage || oh.SizeOfHeaders > oh.SizeOfImage || oh.SizeOfHeaders > file.size()) 301 if (!oh.SizeOfImage || oh.SizeOfHeaders < headers_end || oh.SizeOfHeaders > oh.SizeOfImage ||
300 throw std::runtime_error(name + ": SizeOfHeaders and SizeOfImage don't fit the file"); 302 oh.SizeOfHeaders > file.size())
303 throw std::runtime_error(name + ": SizeOfHeaders and SizeOfImage don't fit the headers and file");
301 304
302 auto* m = new Module; 305 auto* m = new Module;
303 m->name = name; 306 m->name = name;
@@ -338,8 +341,9 @@ Module& Loader::map_image(const std::filesystem::path& path) {
338 auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress); 341 auto* block = at<IMAGE_BASE_RELOCATION>(m->base, dir.VirtualAddress);
339 auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size; 342 auto* end = reinterpret_cast<uint8_t*>(block) + dir.Size;
340 while (reinterpret_cast<uint8_t*>(block) < end) { 343 while (reinterpret_cast<uint8_t*>(block) < end) {
341 if (block->SizeOfBlock < sizeof(IMAGE_BASE_RELOCATION) || 344 const size_t remaining = static_cast<size_t>(end - reinterpret_cast<uint8_t*>(block));
342 reinterpret_cast<uint8_t*>(block) + block->SizeOfBlock > end) 345 if (remaining < sizeof(IMAGE_BASE_RELOCATION) || block->SizeOfBlock < sizeof(IMAGE_BASE_RELOCATION) ||
346 block->SizeOfBlock > remaining)
343 throw std::runtime_error(m->name + ": a relocation block doesn't fit the relocation table"); 347 throw std::runtime_error(m->name + ": a relocation block doesn't fit the relocation table");
344 auto* entries = reinterpret_cast<uint16_t*>(block + 1); 348 auto* entries = reinterpret_cast<uint16_t*>(block + 1);
345 size_t count = (block->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2; 349 size_t count = (block->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2;