[PATCH] D25368: [Object/ELF] - Do not crash on invalid Header->e_shoff value.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 7 06:56:32 PDT 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael, davide.
grimar added subscribers: llvm-commits, grimar, evgeny777.

sections_begin() may return unalignment pointer when Header->e_shoff isinvalid.
That may result in a crash in clients, for example we have one in LLD:

  assert((PtrWord & ~PointerBitMask) == 0 &&
         "Pointer is not sufficiently aligned");

fails when trying to push_back Elf_Shdr* (unaligned) into TinyPtrVector.

Patch forces address to be aligned.


https://reviews.llvm.org/D25368

Files:
  include/llvm/Object/ELF.h
  test/Object/Inputs/invalid-sections-address-alignment.x86-64
  test/Object/invalid.test


Index: test/Object/invalid.test
===================================================================
--- test/Object/invalid.test
+++ test/Object/invalid.test
@@ -64,3 +64,7 @@
 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 address alignment of section headers
Index: include/llvm/Object/ELF.h
===================================================================
--- include/llvm/Object/ELF.h
+++ include/llvm/Object/ELF.h
@@ -367,7 +367,10 @@
   if (Header->e_shentsize != sizeof(Elf_Shdr))
     report_fatal_error(
         "Invalid section header entry size (e_shentsize) in ELF header");
-  return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
+  const uint8_t *Addr = base() + Header->e_shoff;
+  if ((uintptr_t)(Addr) & (AlignOf<typename ELFT::uint>::Alignment - 1))
+    report_fatal_error("Invalid address alignment of section headers");
+  return reinterpret_cast<const Elf_Shdr *>(Addr);
 }
 
 template <class ELFT>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25368.73927.patch
Type: text/x-patch
Size: 1333 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161007/d8b90a2e/attachment.bin>


More information about the llvm-commits mailing list