Report an error on invalid sh_entsize.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 30 Jun 2015 14:59:20 +0000 (14:59 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 30 Jun 2015 14:59:20 +0000 (14:59 +0000)
llvm-svn: 241070

llvm/include/llvm/Object/ELF.h
llvm/test/Object/Inputs/invalid-sh_entsize.elf [new file with mode: 0755]
llvm/test/Object/invalid.test

index 0cb5c7a..4c9cacd 100644 (file)
@@ -285,17 +285,18 @@ public:
   }
 
   const Elf_Sym *begin_dynamic_symbols() const {
-    if (DynSymRegion.Addr)
-      return reinterpret_cast<const Elf_Sym *>(DynSymRegion.Addr);
-    return nullptr;
+    if (!DynSymRegion.Addr)
+      return nullptr;
+    if (DynSymRegion.EntSize != sizeof(Elf_Sym))
+      report_fatal_error("Invalid symbol size");
+    return reinterpret_cast<const Elf_Sym *>(DynSymRegion.Addr);
   }
 
   const Elf_Sym *end_dynamic_symbols() const {
-    if (DynSymRegion.Addr)
-      return reinterpret_cast<const Elf_Sym *>(
-          ((const char *)DynSymRegion.Addr + DynSymRegion.Size));
-
-    return nullptr;
+    if (!DynSymRegion.Addr)
+      return nullptr;
+    return reinterpret_cast<const Elf_Sym *>(
+        ((const char *)DynSymRegion.Addr + DynSymRegion.Size));
   }
 
   Elf_Sym_Range dynamic_symbols() const {
@@ -766,6 +767,8 @@ template <class ELFT>
 const typename ELFFile<ELFT>::Elf_Sym *ELFFile<ELFT>::begin_symbols() const {
   if (!dot_symtab_sec)
     return nullptr;
+  if (dot_symtab_sec->sh_entsize != sizeof(Elf_Sym))
+    report_fatal_error("Invalid symbol size");
   return reinterpret_cast<const Elf_Sym *>(base() + dot_symtab_sec->sh_offset);
 }
 
diff --git a/llvm/test/Object/Inputs/invalid-sh_entsize.elf b/llvm/test/Object/Inputs/invalid-sh_entsize.elf
new file mode 100755 (executable)
index 0000000..9ea8073
Binary files /dev/null and b/llvm/test/Object/Inputs/invalid-sh_entsize.elf differ
index 73a6ad8..88685fa 100644 (file)
@@ -5,3 +5,35 @@ CHECK: Invalid data was encountered while parsing the file
 
 RUN: not llvm-objdump -s %p/Inputs/invalid-strtab-non-null.elf 2>&1 | FileCheck --check-prefix=NON-NULL %s
 NON-NULL: String table must end with a null terminator
+
+Test the sh_entsize are invalid
+RUN: llvm-readobj -s %p/Inputs/invalid-sh_entsize.elf | FileCheck --check-prefix=SECTION %s
+
+SECTION:         Name: .dynsym
+SECTION-NEXT:    Type: SHT_DYNSYM
+SECTION-NEXT:    Flags [
+SECTION-NEXT:      SHF_ALLOC
+SECTION-NEXT:    ]
+SECTION-NEXT:    Address:
+SECTION-NEXT:    Offset:
+SECTION-NEXT:    Size:
+SECTION-NEXT:    Link:
+SECTION-NEXT:    Info:
+SECTION-NEXT:    AddressAlignment:
+SECTION-NEXT:    EntrySize: 32
+
+SECTION:         Name: .symtab
+SECTION-NEXT:    Type: SHT_SYMTAB
+SECTION-NEXT:    Flags [
+SECTION-NEXT:    ]
+SECTION-NEXT:    Address:
+SECTION-NEXT:    Offset:
+SECTION-NEXT:    Size:
+SECTION-NEXT:    Link:
+SECTION-NEXT:    Info:
+SECTION-NEXT:    AddressAlignment:
+SECTION-NEXT:    EntrySize: 32
+
+RUN: not llvm-readobj -t %p/Inputs/invalid-sh_entsize.elf 2>&1 | FileCheck --check-prefix=INVALID-SYM-SIZE %s
+RUN: not llvm-readobj --dyn-symbols %p/Inputs/invalid-sh_entsize.elf 2>&1 | FileCheck --check-prefix=INVALID-SYM-SIZE %s
+INVALID-SYM-SIZE: Invalid symbol size