]> granicus.if.org Git - llvm/commitdiff
Add error checking for Mach-O universal files.
authorKevin Enderby <enderby@apple.com>
Mon, 28 Nov 2016 22:40:50 +0000 (22:40 +0000)
committerKevin Enderby <enderby@apple.com>
Mon, 28 Nov 2016 22:40:50 +0000 (22:40 +0000)
Add the checking for both the MachO::fat_header and the
MachO::fat_arch struct values in the constructor for
MachOUniversalBinary. Such that when the constructor
for ObjectForArch is called it can assume the values in
the MachO::fat_arch for the offset and size are contained
in the file after the MachOUniversalBinary constructor
is called for the Parent.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288084 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Object/MachOUniversal.cpp
test/Object/Inputs/macho-invalid-fat-arch-badalign [new file with mode: 0644]
test/Object/Inputs/macho-invalid-fat-arch-bigalign [new file with mode: 0644]
test/Object/Inputs/macho-invalid-fat-arch-overlap [new file with mode: 0644]
test/Object/Inputs/macho-invalid-fat-arch-overlapheaders [new file with mode: 0644]
test/Object/Inputs/macho-invalid-fat-arch-size [new file with mode: 0644]
test/Object/Inputs/macho-invalid-fat-arch-twosame [new file with mode: 0644]
test/Object/Inputs/macho-invalid-fat-header [new file with mode: 0644]
test/Object/macho-invalid.test

index 9ab0ae656bff9d662133878bf95f26f05a80bdd5..7ddc427dedea84dd27b6ed98cafebfb763082ff6 100644 (file)
@@ -42,6 +42,7 @@ static T getUniversalBinaryStruct(const char *Ptr) {
 MachOUniversalBinary::ObjectForArch::ObjectForArch(
     const MachOUniversalBinary *Parent, uint32_t Index)
     : Parent(Parent), Index(Index) {
+  // The iterators use Parent as a nullptr and an Index+1 == NumberOfObjects.
   if (!Parent || Index >= Parent->getNumberOfObjects()) {
     clear();
   } else {
@@ -51,16 +52,10 @@ MachOUniversalBinary::ObjectForArch::ObjectForArch(
       const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
                               Index * sizeof(MachO::fat_arch);
       Header = getUniversalBinaryStruct<MachO::fat_arch>(HeaderPos);
-      if (ParentData.size() < Header.offset + Header.size) {
-        clear();
-      }
     } else { // Parent->getMagic() == MachO::FAT_MAGIC_64
       const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
                               Index * sizeof(MachO::fat_arch_64);
       Header64 = getUniversalBinaryStruct<MachO::fat_arch_64>(HeaderPos);
-      if (ParentData.size() < Header64.offset + Header64.size) {
-        clear();
-      }
     }
   }
 }
@@ -131,6 +126,10 @@ MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
       getUniversalBinaryStruct<MachO::fat_header>(Buf.begin());
   Magic = H.magic;
   NumberOfObjects = H.nfat_arch;
+  if (NumberOfObjects == 0) {
+    Err = malformedError("contains zero architecture types");
+    return;
+  }
   uint32_t MinSize = sizeof(MachO::fat_header);
   if (Magic == MachO::FAT_MAGIC)
     MinSize += sizeof(MachO::fat_arch) * NumberOfObjects;
@@ -146,6 +145,68 @@ MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
                          " structs would extend past the end of the file");
     return;
   }
+  for (uint32_t i = 0; i < NumberOfObjects; i++) {
+    ObjectForArch A(this, i);
+    uint64_t bigSize = A.getOffset();
+    bigSize += A.getSize();
+    if (bigSize > Buf.size()) {
+      Err = malformedError("offset plus size of cputype (" +
+        Twine(A.getCPUType()) + ") cpusubtype (" +
+        Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
+        ") extends past the end of the file");
+      return;
+    }
+#define MAXSECTALIGN 15 /* 2**15 or 0x8000 */
+    if (A.getAlign() > MAXSECTALIGN) {
+      Err = malformedError("align (2^" + Twine(A.getAlign()) + ") too large "
+        "for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
+        Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
+        ") (maximum 2^" + Twine(MAXSECTALIGN) + ")");
+      return;
+    }
+    if(A.getOffset() % (1 << A.getAlign()) != 0){
+      Err = malformedError("offset: " + Twine(A.getOffset()) +
+        " for cputype (" + Twine(A.getCPUType()) + ") cpusubtype (" +
+        Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
+        ") not aligned on it's alignment (2^" + Twine(A.getAlign()) + ")");
+      return;
+    }
+    if (A.getOffset() < MinSize) {
+      Err =  malformedError("cputype (" + Twine(A.getCPUType()) + ") "
+        "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
+        ") offset " + Twine(A.getOffset()) + " overlaps universal headers");
+      return;
+    }
+  }
+  for (uint32_t i = 0; i < NumberOfObjects; i++) {
+    ObjectForArch A(this, i);
+    for (uint32_t j = i + 1; j < NumberOfObjects; j++) {
+      ObjectForArch B(this, j);
+      if (A.getCPUType() == B.getCPUType() &&
+          (A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) ==
+          (B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)) {
+        Err = malformedError("contains two of the same architecture (cputype "
+          "(" + Twine(A.getCPUType()) + ") cpusubtype (" +
+          Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) + "))");
+        return;
+      }
+      if ((A.getOffset() >= B.getOffset() &&
+           A.getOffset() < B.getOffset() + B.getSize()) ||
+          (A.getOffset() + A.getSize() > B.getOffset() &&
+           A.getOffset() + A.getSize() < B.getOffset() + B.getSize()) ||
+          (A.getOffset() <= B.getOffset() &&
+           A.getOffset() + A.getSize() >= B.getOffset() + B.getSize())) {
+        Err =  malformedError("cputype (" + Twine(A.getCPUType()) + ") "
+          "cpusubtype (" + Twine(A.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) +
+          ") at offset " + Twine(A.getOffset()) + " with a size of " +
+          Twine(A.getSize()) + ", overlaps cputype (" + Twine(B.getCPUType()) +
+          ") cpusubtype (" + Twine(B.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK)
+          + ") at offset " + Twine(B.getOffset()) + " with a size of "
+          + Twine(B.getSize()));
+        return;
+      }
+    }
+  }
   Err = Error::success();
 }
 
diff --git a/test/Object/Inputs/macho-invalid-fat-arch-badalign b/test/Object/Inputs/macho-invalid-fat-arch-badalign
new file mode 100644 (file)
index 0000000..da9f23a
Binary files /dev/null and b/test/Object/Inputs/macho-invalid-fat-arch-badalign differ
diff --git a/test/Object/Inputs/macho-invalid-fat-arch-bigalign b/test/Object/Inputs/macho-invalid-fat-arch-bigalign
new file mode 100644 (file)
index 0000000..6db7158
Binary files /dev/null and b/test/Object/Inputs/macho-invalid-fat-arch-bigalign differ
diff --git a/test/Object/Inputs/macho-invalid-fat-arch-overlap b/test/Object/Inputs/macho-invalid-fat-arch-overlap
new file mode 100644 (file)
index 0000000..556a3a7
Binary files /dev/null and b/test/Object/Inputs/macho-invalid-fat-arch-overlap differ
diff --git a/test/Object/Inputs/macho-invalid-fat-arch-overlapheaders b/test/Object/Inputs/macho-invalid-fat-arch-overlapheaders
new file mode 100644 (file)
index 0000000..04d7867
Binary files /dev/null and b/test/Object/Inputs/macho-invalid-fat-arch-overlapheaders differ
diff --git a/test/Object/Inputs/macho-invalid-fat-arch-size b/test/Object/Inputs/macho-invalid-fat-arch-size
new file mode 100644 (file)
index 0000000..dfd154c
Binary files /dev/null and b/test/Object/Inputs/macho-invalid-fat-arch-size differ
diff --git a/test/Object/Inputs/macho-invalid-fat-arch-twosame b/test/Object/Inputs/macho-invalid-fat-arch-twosame
new file mode 100644 (file)
index 0000000..437df76
Binary files /dev/null and b/test/Object/Inputs/macho-invalid-fat-arch-twosame differ
diff --git a/test/Object/Inputs/macho-invalid-fat-header b/test/Object/Inputs/macho-invalid-fat-header
new file mode 100644 (file)
index 0000000..007664d
Binary files /dev/null and b/test/Object/Inputs/macho-invalid-fat-header differ
index b193ebb8c4125b266317a7146703c4ca28e283e4..6370228e5986d33b8e3e8e949322ba57d376cf5d 100644 (file)
@@ -484,3 +484,24 @@ INVALID-LAZY_BIND-OVERLAP: macho-invalid-lazy_bind-overlap': truncated or malfor
 
 RUN: not llvm-objdump -macho -private-headers %p/Inputs/macho-invalid-export-overlap 2>&1 | FileCheck -check-prefix INVALID-EXPORT-OVERLAP %s
 INVALID-EXPORT-OVERLAP: macho-invalid-export-overlap': truncated or malformed object (dyld export info at offset 200 with a size of 32, overlaps dyld lazy bind info at offset 176 with a size of 32)
+
+RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat-header 2>&1 | FileCheck -check-prefix INVALID-FAT-HEADER %s
+INVALID-FAT-HEADER: macho-invalid-fat-header': truncated or malformed fat file (contains zero architecture types)
+
+RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat-arch-size 2>&1 | FileCheck -check-prefix INVALID-FAT-ARCH-SIZE %s
+INVALID-FAT-ARCH-SIZE: macho-invalid-fat-arch-size': truncated or malformed fat file (offset plus size of cputype (7) cpusubtype (3) extends past the end of the file)
+
+RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat-arch-bigalign 2>&1 | FileCheck -check-prefix INVALID-FAT-ARCH-BIGALIGN %s
+INVALID-FAT-ARCH-BIGALIGN: macho-invalid-fat-arch-bigalign': truncated or malformed fat file (align (2^212) too large for cputype (7) cpusubtype (3) (maximum 2^15))
+
+RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat-arch-badalign 2>&1 | FileCheck -check-prefix INVALID-FAT-ARCH-BADALIGN %s
+INVALID-FAT-ARCH-BADALIGN: macho-invalid-fat-arch-badalign': truncated or malformed fat file (offset: 28 for cputype (7) cpusubtype (3) not aligned on it's alignment (2^4))
+
+RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat-arch-twosame 2>&1 | FileCheck -check-prefix INVALID-FAT-ARCH-TWOSAME %s
+INVALID-FAT-ARCH-TWOSAME: macho-invalid-fat-arch-twosame': truncated or malformed fat file (contains two of the same architecture (cputype (7) cpusubtype (3)))
+
+RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat-arch-overlap 2>&1 | FileCheck -check-prefix INVALID-FAT-ARCH-OVERLAP %s
+INVALID-FAT-ARCH-OVERLAP: macho-invalid-fat-arch-overlap': truncated or malformed fat file (cputype (7) cpusubtype (5) at offset 48 with a size of 28, overlaps cputype (7) cpusubtype (3) at offset 52 with a size of 28)
+
+RUN: not llvm-objdump -macho -universal-headers %p/Inputs/macho-invalid-fat-arch-overlapheaders 2>&1 | FileCheck -check-prefix INVALID-FAT-ARCH-OVERLAPHEADERS %s
+INVALID-FAT-ARCH-OVERLAPHEADERS: macho-invalid-fat-arch-overlapheaders': truncated or malformed fat file (cputype (7) cpusubtype (3) offset 12 overlaps universal headers)