[ELF] - Do not crash on invalid size of dynamic section.
authorGeorge Rimar <grimar@accesssoftek.com>
Fri, 7 Oct 2016 09:01:04 +0000 (09:01 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Fri, 7 Oct 2016 09:01:04 +0000 (09:01 +0000)
Previously if sh_size of dynamic section was broken,
lld may crash. Or even may not crash if used 32 bits host.
(then value may be truncated to 32 bits when doing pointer arithmetic
and could be just zero).
Patch fixes the issue.

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

llvm-svn: 283533

lld/ELF/InputFiles.cpp
lld/test/ELF/invalid/Inputs/dynamic-section-sh_size.elf [new file with mode: 0644]
lld/test/ELF/invalid/dynamic-section-size.s [new file with mode: 0644]

index 5b0ce10..dda18aa 100644 (file)
@@ -523,11 +523,11 @@ template <class ELFT> void SharedFile<ELFT>::parseSoName() {
 
   if (!DynamicSec)
     return;
-  auto *Begin =
-      reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
-  const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
 
-  for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
+  ArrayRef<Elf_Dyn> Arr =
+      check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
+            getFilename(this) + ": getSectionContentsAsArray failed");
+  for (const Elf_Dyn &Dyn : Arr) {
     if (Dyn.d_tag == DT_SONAME) {
       uintX_t Val = Dyn.getVal();
       if (Val >= this->StringTable.size())
diff --git a/lld/test/ELF/invalid/Inputs/dynamic-section-sh_size.elf b/lld/test/ELF/invalid/Inputs/dynamic-section-sh_size.elf
new file mode 100644 (file)
index 0000000..ea35dba
Binary files /dev/null and b/lld/test/ELF/invalid/Inputs/dynamic-section-sh_size.elf differ
diff --git a/lld/test/ELF/invalid/dynamic-section-size.s b/lld/test/ELF/invalid/dynamic-section-size.s
new file mode 100644 (file)
index 0000000..7d12904
--- /dev/null
@@ -0,0 +1,4 @@
+## dynamic-section-sh_size.elf has incorrect sh_size of dynamic section.
+# RUN: not ld.lld %p/Inputs/dynamic-section-sh_size.elf -o %t2 2>&1 | \
+# RUN:   FileCheck %s
+# CHECK: getSectionContentsAsArray failed: Invalid data was encountered while parsing the file