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:
authorMaxime Mansiet <AirKyzzZ@users.noreply.github.com>2026-11-23 01:06:12 +0100
committerMaxime Mansiet <AirKyzzZ@users.noreply.github.com>2026-11-23 01:42:59 +0100
commitbac101ae8bde293ee1568854ab13f159ddc83c0d (patch)
treed129d94ab2056783ab8abfb053a7269c3f4c0a39 /runtime/src/pe_loader.cpp
parent03b09a086592a197db64d91655c6e65c30764d96 (diff)
pe_loader: refuse a section that overlaps the headers
Signed-off-by: Maxime Mansiet <AirKyzzZ@users.noreply.github.com>
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp5
1 files changed, 5 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index f4ecc0de2..ffa1e55bc 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -854,6 +854,8 @@ Module& Loader::map_image(const std::filesystem::path& path) {
854 std::memcpy(m->base, file.data(), oh.SizeOfHeaders); 854 std::memcpy(m->base, file.data(), oh.SizeOfHeaders);
855 auto* sec = IMAGE_FIRST_SECTION(nt); 855 auto* sec = IMAGE_FIRST_SECTION(nt);
856 for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { 856 for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) {
857 if (sec->VirtualAddress < oh.SizeOfHeaders)
858 throw std::runtime_error(m->name + ": section " + std::to_string(i) + " overlaps the headers");
857 uint32_t n = std::min(sec->SizeOfRawData, sec->Misc.VirtualSize ? sec->Misc.VirtualSize : sec->SizeOfRawData); 859 uint32_t n = std::min(sec->SizeOfRawData, sec->Misc.VirtualSize ? sec->Misc.VirtualSize : sec->SizeOfRawData);
858 if (!n) continue; 860 if (!n) continue;
859 if (sec->PointerToRawData > file.size() || n > file.size() - sec->PointerToRawData) 861 if (sec->PointerToRawData > file.size() || n > file.size() - sec->PointerToRawData)
@@ -879,6 +881,9 @@ Module& Loader::map_image(const std::filesystem::path& path) {
879 size_t count = (block->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2; 881 size_t count = (block->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / 2;
880 for (size_t i = 0; i < count; ++i) { 882 for (size_t i = 0; i < count; ++i) {
881 uint16_t type = entries[i] >> 12, off = entries[i] & 0xFFF; 883 uint16_t type = entries[i] >> 12, off = entries[i] & 0xFFF;
884 if (type != IMAGE_REL_BASED_ABSOLUTE &&
885 static_cast<uint64_t>(block->VirtualAddress) + off < oh.SizeOfHeaders)
886 throw std::runtime_error(m->name + ": a relocation lands in the headers");
882 if (type == IMAGE_REL_BASED_DIR64) { 887 if (type == IMAGE_REL_BASED_DIR64) {
883 require_range(m->name, static_cast<uint64_t>(block->VirtualAddress) + off, sizeof(uint64_t), m->size, 888 require_range(m->name, static_cast<uint64_t>(block->VirtualAddress) + off, sizeof(uint64_t), m->size,
884 "a relocation"); 889 "a relocation");