From 516f13c327992358183faa2448fa91c3290c2595 Mon Sep 17 00:00:00 2001 From: boykopovar Date: Tue, 4 Aug 2026 09:49:09 +0300 Subject: [PATCH] fix(patcher): verify header block fits before first pt load --- core/elfpatcher/src/ElfPatcher.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/elfpatcher/src/ElfPatcher.cpp b/core/elfpatcher/src/ElfPatcher.cpp index f2ee7eeac..ca0b8f1f7 100644 --- a/core/elfpatcher/src/ElfPatcher.cpp +++ b/core/elfpatcher/src/ElfPatcher.cpp @@ -242,6 +242,11 @@ std::vector ElfPatcher::Patch( keptMinVaddrAlign = ph.Alignment; } } + if (keptMinVaddr == UINT64_MAX) + throw Domain::RelinkerException("No kept PT_LOAD segment to anchor the header block against"); + if (keptMinVaddrAlign == 0) + throw Domain::RelinkerException("Lowest kept PT_LOAD has zero alignment"); + const std::uint16_t neededPh = keptCount + 4; if (neededPh > phNum) throw Domain::RelinkerException( @@ -249,7 +254,14 @@ std::vector ElfPatcher::Patch( ", available " + std::to_string(phNum)); const std::uint64_t headerBlockSize = phOff + static_cast(neededPh) * phEntSize; + if (headerBlockSize >= keptMinVaddr) + throw Domain::RelinkerException( + "Header block does not fit below the lowest kept PT_LOAD: need " + + std::to_string(headerBlockSize) + " bytes, only " + std::to_string(keptMinVaddr) + " available"); + const std::uint64_t headerBlockVaddr = (keptMinVaddr - headerBlockSize) & ~(keptMinVaddrAlign - 1); + if (headerBlockVaddr + headerBlockSize > keptMinVaddr) + throw Domain::RelinkerException("Header block would overlap the lowest kept PT_LOAD after alignment"); const std::uint64_t extraBlockOff = dynStrOff; const std::uint64_t extraBlockSize = static_cast(buf.size()) - extraBlockOff;