Skip to content

Commit 71f3c19

Browse files
author
George Rimar
committedOct 17, 2016
[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 llvm-svn: 284369
1 parent ada2862 commit 71f3c19

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed
 

‎llvm/include/llvm/Object/ELF.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,11 @@ ELFFile<ELFT>::getSection(uint32_t Index) const {
399399
if (Index >= getNumSections())
400400
return object_error::invalid_section_index;
401401

402-
return reinterpret_cast<const Elf_Shdr *>(
403-
reinterpret_cast<const char *>(SectionHeaderTable) +
404-
(Index * Header->e_shentsize));
402+
const uint8_t *Addr = reinterpret_cast<const uint8_t *>(SectionHeaderTable) +
403+
(Index * Header->e_shentsize);
404+
if (Addr >= base() + getBufSize())
405+
return object_error::invalid_section_index;
406+
return reinterpret_cast<const Elf_Shdr *>(Addr);
405407
}
406408

407409
template <class ELFT>
435 Bytes
Binary file not shown.

‎llvm/test/Object/invalid.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ RUN: not llvm-readobj --dyn-symbols %p/Inputs/invalid-sh_entsize.elf 2>&1 | File
4141
INVALID-DYNSYM-SIZE: Invalid entity size
4242

4343
RUN: not llvm-readobj -t %p/Inputs/invalid-section-index.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s
44-
44+
RUN: not llvm-readobj -t %p/Inputs/invalid-section-index2.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-INDEX %s
4545
INVALID-SECTION-INDEX: Invalid section index
4646

4747
RUN: not llvm-readobj -s %p/Inputs/invalid-section-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SECTION-SIZE %s

0 commit comments

Comments
 (0)