From 65807f899b4616d3961e14e2cf4a7330eade19de Mon Sep 17 00:00:00 2001 From: George Rimar Date: Mon, 17 Oct 2016 10:58:02 +0000 Subject: [PATCH] Recommit r284371 "[Object/ELF] - Check that e_shnum is null when e_shoff is." With fix: hex edited the precompiled inputs from another testcases to pass new checks. Original commit message: [Object/ELF] - Check that e_shnum is null when e_shoff is. Spec says (http://www.sco.com/developers/gabi/1998-04-29/ch4.eheader.html) : e_shnum This member holds the number of entries in the section header table. Thus the product of e_shentsize and e_shnum gives the section header table's size in bytes. If a file has no section header table, e_shnum holds the value zero. Revealed using "id_000037,sig_11,src_000015,op_havoc,rep_8" from PR30540 That was the reason of crash in lld on incorrect input file. Binary reduced using afl-min. Differential revision: https://reviews.llvm.org/D25090 llvm-svn: 284374 --- llvm/include/llvm/Object/ELF.h | 6 +++++- .../Object/Inputs/corrupt-invalid-strtab.elf.x86-64 | Bin 1712 -> 1712 bytes llvm/test/Object/Inputs/invalid-e_shnum.elf | Bin 0 -> 64 bytes llvm/test/Object/Inputs/rel-no-sec-table.elf-x86-64 | Bin 2152 -> 2152 bytes llvm/test/Object/invalid.test | 3 +++ 5 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 llvm/test/Object/Inputs/invalid-e_shnum.elf diff --git a/llvm/include/llvm/Object/ELF.h b/llvm/include/llvm/Object/ELF.h index 31f42f5..03b172f 100644 --- a/llvm/include/llvm/Object/ELF.h +++ b/llvm/include/llvm/Object/ELF.h @@ -319,8 +319,12 @@ ELFFile::ELFFile(StringRef Object, std::error_code &EC) Header = reinterpret_cast(base()); - if (Header->e_shoff == 0) + if (Header->e_shoff == 0) { + if (Header->e_shnum != 0) + report_fatal_error( + "e_shnum should be zero if a file has no section header table"); return; + } const uint64_t SectionTableOffset = Header->e_shoff; diff --git a/llvm/test/Object/Inputs/corrupt-invalid-strtab.elf.x86-64 b/llvm/test/Object/Inputs/corrupt-invalid-strtab.elf.x86-64 index bdaa3cf920824ee8f8b2c8f71617a776e3c5250f..ab9a63ea7966324796a6dab59c9f3057065c4d5c 100755 GIT binary patch delta 12 TcmdnMyMcFt4I{%w+hR5V8o>jv delta 12 TcmdnMyMcFt4I|G++hR5V8vO&i diff --git a/llvm/test/Object/Inputs/invalid-e_shnum.elf b/llvm/test/Object/Inputs/invalid-e_shnum.elf new file mode 100644 index 0000000000000000000000000000000000000000..0b46fbd8cec0caee456e37b8da7d16b6d62de3b6 GIT binary patch literal 64 ecmb<-^>JfjG=KwU20I2g2Z_M|1qdMrusQ%=Ckh(? literal 0 HcmV?d00001 diff --git a/llvm/test/Object/Inputs/rel-no-sec-table.elf-x86-64 b/llvm/test/Object/Inputs/rel-no-sec-table.elf-x86-64 index d527305d65976ad7afc5e0c569a18d5d04285186..33449c960dc1db89ed97095609553965c87913cc 100755 GIT binary patch delta 12 TcmaDM@IqjM4I{%wTWt;iA3p=% delta 12 TcmaDM@IqjM4I}?XTWt;iABqF+ diff --git a/llvm/test/Object/invalid.test b/llvm/test/Object/invalid.test index d940789..c72de06 100644 --- a/llvm/test/Object/invalid.test +++ b/llvm/test/Object/invalid.test @@ -55,6 +55,9 @@ INVALID-SYMTAB-SIZE: Invalid data was encountered while parsing the file 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. +RUN: not llvm-readobj -t %p/Inputs/invalid-e_shnum.elf 2>&1 | FileCheck --check-prefix=INVALID-SH-NUM %s +INVALID-SH-NUM: e_shnum should be zero if a file has no section header table + RUN: not llvm-readobj -t %p/Inputs/invalid-ext-symtab-index.elf-x86-64 2>&1 | \ RUN: FileCheck --check-prefix=INVALID-EXT-SYMTAB-INDEX %s INVALID-EXT-SYMTAB-INDEX: Invalid symbol table index -- 2.7.4