From ba5c77c9e52a89e705e60853e96a813bffad9949 Mon Sep 17 00:00:00 2001 From: John Reiser Date: Sun, 14 Mar 2021 15:20:15 -0700 Subject: [PATCH] Reject if MZ and PE headers overlap Overlap ('leanify', etc.) causes headaches. https://github.com/upx/upx/issues/231 modified: p_w32pe.cpp modified: p_w64pep.cpp modified: pefile.cpp --- src/p_w32pe.cpp | 5 +++-- src/p_w64pep.cpp | 5 +++-- src/pefile.cpp | 8 ++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/p_w32pe.cpp b/src/p_w32pe.cpp index 28863230..b02d9e3b 100644 --- a/src/p_w32pe.cpp +++ b/src/p_w32pe.cpp @@ -276,9 +276,10 @@ void PackW32Pe::setOhDataBase(const pe_section_t *osection) oh.database = osection[2].vaddr; } -void PackW32Pe::setOhHeaderSize(const pe_section_t *) +void PackW32Pe::setOhHeaderSize(const pe_section_t *osection) { - oh.headersize = rvamin; // FIXME + (void)osection; + oh.headersize = rvamin; // FIXME } void PackW32Pe::pack(OutputFile *fo) diff --git a/src/p_w64pep.cpp b/src/p_w64pep.cpp index 121d3287..4a35be51 100644 --- a/src/p_w64pep.cpp +++ b/src/p_w64pep.cpp @@ -266,9 +266,10 @@ void PackW64Pep::defineSymbols(unsigned ncsection, unsigned upxsection, linker->defineSymbol("START", upxsection); } -void PackW64Pep::setOhHeaderSize(const pe_section_t *) +void PackW64Pep::setOhHeaderSize(const pe_section_t *osection) { - oh.headersize = rvamin; // FIXME + (void)osection; + oh.headersize = rvamin; // FIXME } void PackW64Pep::pack(OutputFile *fo) diff --git a/src/pefile.cpp b/src/pefile.cpp index 0bc5e53c..06b0cf9f 100644 --- a/src/pefile.cpp +++ b/src/pefile.cpp @@ -163,6 +163,13 @@ int PeFile::readFileHeader() if (h.mz == 'M' + 'Z'*256) // dos exe { + if (h.nexepos && h.nexepos < sizeof(exe_header_t)) { + // Overlapping MZ and PE headers by 'leanify', etc. + char buf[64]; snprintf(buf, sizeof(buf), + "PE and MZ header overlap: %#x < %#x", + (unsigned)h.nexepos, (unsigned)sizeof(exe_header_t)); + throwCantPack(buf); + } unsigned const delta = (h.relocoffs >= 0x40) ? h.nexepos // new format exe : (h.p512*512+h.m512 - h.m512 ? 512 : h.nexepos); @@ -3125,6 +3132,7 @@ void PeFile32::pack0(OutputFile *fo, unsigned subsystem_mask, { super::pack0(fo, ih, oh, subsystem_mask, default_imagebase, last_section_rsrc_only); + infoWarning("End of PeFile32::pack0"); } void PeFile32::unpack(OutputFile *fo)