Skip to content

Commit

Permalink
[ELF] Check if LinkSec is nullptr when initializing SHF_LINK_ORDER se…
Browse files Browse the repository at this point in the history
…ctions

Summary: This protects lld from a null pointer dereference when a faulty input file has such invalid sh_link fields.

Reviewers: ruiu, espindola

Reviewed By: ruiu

Subscribers: emaste, arichardson, llvm-commits

Differential Revision: https://reviews.llvm.org/D51743

llvm-svn: 341611
  • Loading branch information
MaskRay committed Sep 7, 2018
1 parent f9ec62c commit 1fe3e8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lld/ELF/InputFiles.cpp
Expand Up @@ -478,11 +478,13 @@ void ObjFile<ELFT>::initializeSections(
// .ARM.exidx sections have a reverse dependency on the InputSection they
// have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
if (Sec.sh_flags & SHF_LINK_ORDER) {
if (Sec.sh_link >= this->Sections.size())
InputSectionBase *LinkSec = nullptr;
if (Sec.sh_link < this->Sections.size())
LinkSec = this->Sections[Sec.sh_link];
if (!LinkSec)
fatal(toString(this) +
": invalid sh_link index: " + Twine(Sec.sh_link));

InputSectionBase *LinkSec = this->Sections[Sec.sh_link];
InputSection *IS = cast<InputSection>(this->Sections[I]);
LinkSec->DependentSections.push_back(IS);
if (!isa<InputSection>(LinkSec))
Expand Down
16 changes: 16 additions & 0 deletions lld/test/ELF/invalid/linkorder-invalid-sec2.test
@@ -0,0 +1,16 @@
# REQUIRES: x86
# RUN: yaml2obj %s -o %t.o
# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
# CHECK: invalid sh_link index: 0

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_X86_64
Sections:
- Name: .linkorder
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR, SHF_LINK_ORDER ]
Link: 0

0 comments on commit 1fe3e8b

Please sign in to comment.