From: Rafael Espindola Date: Mon, 24 Aug 2015 21:09:41 +0000 (+0000) Subject: Report an error if a SHT_SYMTAB_SHNDX section has the wrong size. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=850ecaba66f1902896b670cc3db24f4f144b70fb;p=oota-llvm.git Report an error if a SHT_SYMTAB_SHNDX section has the wrong size. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245873 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 9750d3be175..092ac6db0bd 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -197,9 +197,9 @@ public: uint64_t getNumSections() const; uintX_t getStringTableIndex() const; - ELF::Elf64_Word - getExtendedSymbolTableIndex(const Elf_Sym *Sym, const Elf_Shdr *SymTab, - ArrayRef ShndxTable) const; + uint32_t getExtendedSymbolTableIndex(const Elf_Sym *Sym, + const Elf_Shdr *SymTab, + ArrayRef ShndxTable) const; const Elf_Ehdr *getHeader() const { return Header; } ErrorOr getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, @@ -220,13 +220,13 @@ typedef ELFFile> ELF32BEFile; typedef ELFFile> ELF64BEFile; template -ELF::Elf64_Word ELFFile::getExtendedSymbolTableIndex( +uint32_t ELFFile::getExtendedSymbolTableIndex( const Elf_Sym *Sym, const Elf_Shdr *SymTab, ArrayRef ShndxTable) const { assert(Sym->st_shndx == ELF::SHN_XINDEX); unsigned Index = Sym - symbol_begin(SymTab); - // FIXME: error checking + // The size of the table was checked in getSHNDXTable. return ShndxTable[Index]; } @@ -471,12 +471,22 @@ ELFFile::getSHNDXTable(const Elf_Shdr &Section) const { assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX); const Elf_Word *ShndxTableBegin = reinterpret_cast(base() + Section.sh_offset); - uintX_t Size = Section.sh_offset; - if (Size % sizeof(uintX_t)) + uintX_t Size = Section.sh_size; + if (Size % sizeof(uint32_t)) return object_error::parse_failed; - const Elf_Word *ShndxTableEnd = ShndxTableBegin + Size / sizeof(uintX_t); + uintX_t NumSymbols = Size / sizeof(uint32_t); + const Elf_Word *ShndxTableEnd = ShndxTableBegin + NumSymbols; if (reinterpret_cast(ShndxTableEnd) > Buf.end()) return object_error::parse_failed; + ErrorOr SymTableOrErr = getSection(Section.sh_link); + if (std::error_code EC = SymTableOrErr.getError()) + return EC; + const Elf_Shdr &SymTable = **SymTableOrErr; + if (SymTable.sh_type != ELF::SHT_SYMTAB && + SymTable.sh_type != ELF::SHT_DYNSYM) + return object_error::parse_failed; + if (NumSymbols != (SymTable.sh_size / sizeof(Elf_Sym))) + return object_error::parse_failed; return ArrayRef(ShndxTableBegin, ShndxTableEnd); } diff --git a/test/Object/Inputs/invalid-xindex-size.elf b/test/Object/Inputs/invalid-xindex-size.elf new file mode 100644 index 00000000000..2852b85ca04 Binary files /dev/null and b/test/Object/Inputs/invalid-xindex-size.elf differ diff --git a/test/Object/invalid.test b/test/Object/invalid.test index a4219ed592d..cc5cd68304c 100644 --- a/test/Object/invalid.test +++ b/test/Object/invalid.test @@ -48,3 +48,7 @@ INVALID-SECTION-SIZE: Invalid section header entry size (e_shentsize) in ELF hea RUN: not llvm-readobj -t %p/Inputs/invalid-symbol-table-size.elf 2>&1 | FileCheck --check-prefix=INVALID-SYMTAB-SIZE %s INVALID-SYMTAB-SIZE: Invalid symbol table size + + +RUN: not llvm-readobj -t %p/Inputs/invalid-xindex-size.elf 2>&1 | FileCheck --check-prefix=INVALID-XINDEX-SIZE %s +INVALID-XINDEX-SIZE: Invalid data was encountered while parsing the file.