[Object/ELF] - Do not allow overflow when checking section size/offset.
authorGeorge Rimar <grimar@accesssoftek.com>
Thu, 27 Oct 2016 11:44:56 +0000 (11:44 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Thu, 27 Oct 2016 11:44:56 +0000 (11:44 +0000)
Overflow was the reason of incorrect passing the check,
patch fixes the case.

Differentail revision: https://reviews.llvm.org/D25514

llvm-svn: 285284

llvm/include/llvm/Object/ELF.h
llvm/test/Object/Inputs/invalid-section-size2.elf [new file with mode: 0644]
llvm/test/Object/invalid.test

index b6d4b80..d1de25d 100644 (file)
@@ -229,7 +229,8 @@ ELFFile<ELFT>::getSectionContentsAsArray(const Elf_Shdr *Sec) const {
 
   if (Size % sizeof(T))
     return object_error::parse_failed;
-  if (Offset + Size > Buf.size())
+  if ((std::numeric_limits<uintX_t>::max() - Offset < Size) ||
+      Offset + Size > Buf.size())
     return object_error::parse_failed;
 
   const T *Start = reinterpret_cast<const T *>(base() + Offset);
diff --git a/llvm/test/Object/Inputs/invalid-section-size2.elf b/llvm/test/Object/Inputs/invalid-section-size2.elf
new file mode 100644 (file)
index 0000000..5b7b5bc
Binary files /dev/null and b/llvm/test/Object/Inputs/invalid-section-size2.elf differ
index 3529179..a0016fe 100644 (file)
@@ -72,3 +72,7 @@ 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
+
+RUN: not llvm-readobj -t %p/Inputs/invalid-section-size2.elf 2>&1 | \
+RUN:   FileCheck --check-prefix=INVALID-SECTION-SIZE2 %s
+INVALID-SECTION-SIZE2: Invalid data was encountered while parsing the file.