[Object/ELF] - Do not crash on invalid section index.

If object has wrong (large) string table index and
also incorrect large value for amount of sections in total,
then section index passes the check:

  if (Index >= getNumSections())
    return object_error::invalid_section_index;

But result pointer then is far after end of file data, what
result in a crash.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@284369 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
George Rimar 2016-10-17 09:30:06 +00:00
parent 0ebe9c2f81
commit b987363712
3 changed files with 6 additions and 4 deletions

View File

@ -399,9 +399,11 @@ ELFFile<ELFT>::getSection(uint32_t Index) const {
if (Index >= getNumSections())
return object_error::invalid_section_index;
return reinterpret_cast<const Elf_Shdr *>(
reinterpret_cast<const char *>(SectionHeaderTable) +
(Index * Header->e_shentsize));
const uint8_t *Addr = reinterpret_cast<const uint8_t *>(SectionHeaderTable) +
(Index * Header->e_shentsize);
if (Addr >= base() + getBufSize())
return object_error::invalid_section_index;
return reinterpret_cast<const Elf_Shdr *>(Addr);
}
template <class ELFT>

Binary file not shown.

View File

@ -41,7 +41,7 @@ RUN: not llvm-readobj --dyn-symbols %p/Inputs/invalid-sh_entsize.elf 2>&1 | File
INVALID-DYNSYM-SIZE: Invalid entity size
RUN: not llvm-readobj -t %p/Inputs/invalid-section-index.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s
RUN: not llvm-readobj -t %p/Inputs/invalid-section-index2.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s
INVALID-SECTION-INDEX: Invalid section index
RUN: not llvm-readobj -s %p/Inputs/invalid-section-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-SIZE %s