[Object/ELF] - Check Header->e_shoff value earlier and do not crash.
authorGeorge Rimar <grimar@accesssoftek.com>
Mon, 17 Oct 2016 14:28:12 +0000 (14:28 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Mon, 17 Oct 2016 14:28:12 +0000 (14:28 +0000)
Patch checks that section pointer is aligned properly.
This should be done before getStringTable() call.

Differential revision: https://reviews.llvm.org/D25462

llvm-svn: 284387

llvm/include/llvm/Object/ELF.h
llvm/test/Object/Inputs/invalid-sections-address-alignment.x86-64 [new file with mode: 0644]
llvm/test/Object/invalid.test

index 03b172fe304fc825ed32a91b40b9d8ef81277865..54d2fa1a5837624d317e15f1494940e64ba20c1f 100644 (file)
@@ -334,6 +334,12 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
     return;
   }
 
+  if (SectionTableOffset & (AlignOf<Elf_Shdr>::Alignment - 1)) {
+    // Invalid address alignment of section headers
+    EC = object_error::parse_failed;
+    return;
+  }
+
   // The getNumSections() call below depends on SectionHeaderTable being set.
   SectionHeaderTable =
     reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
diff --git a/llvm/test/Object/Inputs/invalid-sections-address-alignment.x86-64 b/llvm/test/Object/Inputs/invalid-sections-address-alignment.x86-64
new file mode 100644 (file)
index 0000000..c0653d1
Binary files /dev/null and b/llvm/test/Object/Inputs/invalid-sections-address-alignment.x86-64 differ
index c72de062059baac6338fd5c1fe82b0c8495d5428..fda5d6d66d6bc2480cd1fa78202295ea903aaf60 100644 (file)
@@ -67,3 +67,7 @@ RUN:   FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s
 RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-x86-64 2>&1 | \
 RUN:   FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s
 INVALID-RELOC-SH-OFFSET: Invalid data was encountered while parsing the file
+
+RUN: not llvm-readobj -t %p/Inputs/invalid-sections-address-alignment.x86-64 2>&1 | \
+RUN:   FileCheck --check-prefix=INVALID-SEC-ADDRESS-ALIGNMENT %s
+INVALID-SEC-ADDRESS-ALIGNMENT: Invalid data was encountered while parsing the file