diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 03b172fe304..54d2fa1a583 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -334,6 +334,12 @@ ELFFile::ELFFile(StringRef Object, std::error_code &EC) return; } + if (SectionTableOffset & (AlignOf::Alignment - 1)) { + // Invalid address alignment of section headers + EC = object_error::parse_failed; + return; + } + // The getNumSections() call below depends on SectionHeaderTable being set. SectionHeaderTable = reinterpret_cast(base() + SectionTableOffset); diff --git a/test/Object/Inputs/invalid-sections-address-alignment.x86-64 b/test/Object/Inputs/invalid-sections-address-alignment.x86-64 new file mode 100644 index 00000000000..c0653d1d1b3 Binary files /dev/null and b/test/Object/Inputs/invalid-sections-address-alignment.x86-64 differ diff --git a/test/Object/invalid.test b/test/Object/invalid.test index c72de062059..fda5d6d66d6 100644 --- a/test/Object/invalid.test +++ b/test/Object/invalid.test @@ -67,3 +67,7 @@ RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s 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 data was encountered while parsing the file