diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index fd3cb5efcfb..fcd7099954a 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -751,6 +751,7 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, const char *CodeSignDrsLoadCmd = nullptr; const char *VersLoadCmd = nullptr; const char *SourceLoadCmd = nullptr; + const char *EntryPointLoadCmd = nullptr; for (unsigned I = 0; I < LoadCommandCount; ++I) { if (is64Bit()) { if (Load.C.cmdsize % 8 != 0) { @@ -891,6 +892,17 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, return; } SourceLoadCmd = Load.Ptr; + } else if (Load.C.cmd == MachO::LC_MAIN) { + if (Load.C.cmdsize != sizeof(MachO::entry_point_command)) { + Err = malformedError("LC_MAIN command " + Twine(I) + + " has incorrect cmdsize"); + return; + } + if (EntryPointLoadCmd) { + Err = malformedError("more than one LC_MAIN command"); + return; + } + EntryPointLoadCmd = Load.Ptr; } if (I < LoadCommandCount - 1) { if (auto LoadOrErr = getNextLoadCommandInfo(this, I, Load)) diff --git a/test/Object/Inputs/macho-invalid-entry-bad-size b/test/Object/Inputs/macho-invalid-entry-bad-size new file mode 100644 index 00000000000..b7944be6be8 Binary files /dev/null and b/test/Object/Inputs/macho-invalid-entry-bad-size differ diff --git a/test/Object/Inputs/macho-invalid-entry-more-than-one b/test/Object/Inputs/macho-invalid-entry-more-than-one new file mode 100644 index 00000000000..de08dd5a830 Binary files /dev/null and b/test/Object/Inputs/macho-invalid-entry-more-than-one differ diff --git a/test/Object/macho-invalid.test b/test/Object/macho-invalid.test index c7d7b05d5da..9c902aff814 100644 --- a/test/Object/macho-invalid.test +++ b/test/Object/macho-invalid.test @@ -328,3 +328,9 @@ INVALID-SOURCE-BAD-SIZE: macho-invalid-source-bad-size': truncated or malformed RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-source-more-than-one 2>&1 | FileCheck -check-prefix INVALID-SOURCE-MORE-THAN-ONE %s INVALID-SOURCE-MORE-THAN-ONE: macho-invalid-source-more-than-one': truncated or malformed object (more than one LC_SOURCE_VERSION command) + +RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-entry-bad-size 2>&1 | FileCheck -check-prefix INVALID-ENTRY-BAD-SIZE %s +INVALID-ENTRY-BAD-SIZE: macho-invalid-entry-bad-size': truncated or malformed object (LC_MAIN command 0 has incorrect cmdsize) + +RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-entry-more-than-one 2>&1 | FileCheck -check-prefix INVALID-ENTRY-MORE-THAN-ONE %s +INVALID-ENTRY-MORE-THAN-ONE: macho-invalid-entry-more-than-one': truncated or malformed object (more than one LC_MAIN command)