Next set of additional error checks for invalid Mach-O files for the
authorKevin Enderby <enderby@apple.com>
Thu, 29 Sep 2016 17:45:23 +0000 (17:45 +0000)
committerKevin Enderby <enderby@apple.com>
Thu, 29 Sep 2016 17:45:23 +0000 (17:45 +0000)
load command that uses the Mach::source_version_command type
but not used in llvm libObject code but used in llvm tool code.

This includes just the LC_SOURCE_VERSION load command.

llvm-svn: 282736

llvm/lib/Object/MachOObjectFile.cpp
llvm/test/Object/Inputs/macho-invalid-source-bad-size [new file with mode: 0644]
llvm/test/Object/Inputs/macho-invalid-source-more-than-one [new file with mode: 0644]
llvm/test/Object/macho-invalid.test

index 3642394..fd3cb5e 100644 (file)
@@ -750,6 +750,7 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
   const char *SplitInfoLoadCmd = nullptr;
   const char *CodeSignDrsLoadCmd = nullptr;
   const char *VersLoadCmd = nullptr;
+  const char *SourceLoadCmd = nullptr;
   for (unsigned I = 0; I < LoadCommandCount; ++I) {
     if (is64Bit()) {
       if (Load.C.cmdsize % 8 != 0) {
@@ -879,6 +880,17 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
     } else if (Load.C.cmd == MachO::LC_RPATH) {
       if ((Err = checkRpathCommand(this, Load, I)))
         return;
+    } else if (Load.C.cmd == MachO::LC_SOURCE_VERSION) {
+      if (Load.C.cmdsize != sizeof(MachO::source_version_command)) {
+        Err = malformedError("LC_SOURCE_VERSION command " + Twine(I) +
+                             " has incorrect cmdsize");
+        return;
+      }
+      if (SourceLoadCmd) {
+        Err = malformedError("more than one LC_SOURCE_VERSION command");
+        return;
+      }
+      SourceLoadCmd = Load.Ptr;
     }
     if (I < LoadCommandCount - 1) {
       if (auto LoadOrErr = getNextLoadCommandInfo(this, I, Load))
diff --git a/llvm/test/Object/Inputs/macho-invalid-source-bad-size b/llvm/test/Object/Inputs/macho-invalid-source-bad-size
new file mode 100644 (file)
index 0000000..5c09e42
Binary files /dev/null and b/llvm/test/Object/Inputs/macho-invalid-source-bad-size differ
diff --git a/llvm/test/Object/Inputs/macho-invalid-source-more-than-one b/llvm/test/Object/Inputs/macho-invalid-source-more-than-one
new file mode 100644 (file)
index 0000000..148565b
Binary files /dev/null and b/llvm/test/Object/Inputs/macho-invalid-source-more-than-one differ
index c4048db..c7d7b05 100644 (file)
@@ -322,3 +322,9 @@ INVALID-RPATH-NAME_OFFSET-TOOBIG: macho-invalid-rpath-name_offset-toobig': trunc
 
 RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-rpath-name_toobig 2>&1 | FileCheck -check-prefix INVALID-RPATH-NAME_TOOBIG %s
 INVALID-RPATH-NAME_TOOBIG: macho-invalid-rpath-name_toobig': truncated or malformed object (load command 0 LC_RPATH library name extends past the end of the load command)
+
+RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-source-bad-size 2>&1 | FileCheck -check-prefix INVALID-SOURCE-BAD-SIZE %s
+INVALID-SOURCE-BAD-SIZE: macho-invalid-source-bad-size': truncated or malformed object (LC_SOURCE_VERSION command 0 has incorrect cmdsize)
+
+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)