[PATCH] D25239: [Object/ELF] - Do not crash on invalid sh_size value of REL[A] section.
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 5 03:31:18 PDT 2016
grimar updated this revision to Diff 73614.
grimar added a comment.
- Fixed bug.
https://reviews.llvm.org/D25239
Files:
include/llvm/Object/ELF.h
test/Object/Inputs/invalid-relocation-sec-sh_size.elf-i386
test/Object/Inputs/invalid-relocation-sec-sh_size.elf-x86-64
test/Object/invalid.test
Index: test/Object/invalid.test
===================================================================
--- test/Object/invalid.test
+++ test/Object/invalid.test
@@ -64,3 +64,9 @@
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 relocation entry offset
+
+RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_size.elf-i386 2>&1 | \
+RUN: FileCheck --check-prefix=INVALID-RELOC-SH-SIZE %s
+RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_size.elf-x86-64 2>&1 | \
+RUN: FileCheck --check-prefix=INVALID-RELOC-SH-SIZE %s
+INVALID-RELOC-SH-SIZE: Invalid relocation entry size
Index: include/llvm/Object/ELF.h
===================================================================
--- include/llvm/Object/ELF.h
+++ include/llvm/Object/ELF.h
@@ -146,7 +146,10 @@
uint64_t Size = sec->sh_size;
if (Size % sizeof(Elf_Rela))
report_fatal_error("Invalid relocation table size");
- return rela_begin(sec) + Size / sizeof(Elf_Rela);
+ const Elf_Rela *Begin = rela_begin(sec);
+ if (reinterpret_cast<const uint8_t *>(Begin) + Size > base() + Buf.size())
+ report_fatal_error("Invalid relocation entry size");
+ return Begin + Size / sizeof(Elf_Rela);
}
Elf_Rela_Range relas(const Elf_Shdr *Sec) const {
@@ -165,7 +168,10 @@
uint64_t Size = sec->sh_size;
if (Size % sizeof(Elf_Rel))
report_fatal_error("Invalid relocation table size");
- return rel_begin(sec) + Size / sizeof(Elf_Rel);
+ const Elf_Rel *Begin = rel_begin(sec);
+ if (reinterpret_cast<const uint8_t *>(Begin) + Size > base() + Buf.size())
+ report_fatal_error("Invalid relocation entry size");
+ return Begin + Size / sizeof(Elf_Rel);
}
Elf_Rel_Range rels(const Elf_Shdr *Sec) const {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25239.73614.patch
Type: text/x-patch
Size: 1903 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161005/dff6a12c/attachment.bin>
More information about the llvm-commits
mailing list