Issue 243369
Summary [lld][ELF] ld.lld -r crashes on a kept SHT_GROUP whose member is section 0 or past the section table
Labels lld:ELF, crash
Assignees
Reporter AnantaSrikar
    When `ObjFile::parse` discards a COMDAT group, it rejects member indices `>= size` with `invalid section index in group`. When it keeps the group, it doesn't check the members at all. In a `-r` link, `finalizeShtGroup` then reads `sections[member]` and calls `getOutputSection()` on it. A member of 0 gives a null pointer, and `ld.lld` segfaults. A member past the section table fails the `ArrayRef` bounds assertion; Ubuntu LLD 21.1.8 links that one with status 0 after reading past the array.

**Version**: main at c7014922f582 (2026-11-16), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`, on x86_64 Linux.

### Reproducer

```sh
cat > group.yaml <<'YAML'
--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data:    ELFDATA2LSB
  Type: ET_REL
  Machine: EM_X86_64
Sections:
  - Name:    .group
    Type: SHT_GROUP
    Link:    .symtab
    Info:    sig
    Members:
      - SectionOrType: GRP_COMDAT
      - SectionOrType: 0
  - Name:    .text.sig
 Type:    SHT_PROGBITS
    Flags:   [ SHF_ALLOC, SHF_EXECINSTR, SHF_GROUP ]
    Content: C3
Symbols:
  - Name:    sig
    Section: .text.sig
 Binding: STB_GLOBAL
...
YAML
yaml2obj group.yaml -o group.o
ld.lld -r group.o -o out.o
```

It segfaults (status 139). With the member set to `.text.sig`, the same object links (status 0). With `SectionOrType: 10` (past the section table), an assertions build fails `Index < Length && "Invalid index!"` in `ArrayRef<InputSectionBase *>::operator[]`.

### Observed

```
lld::elf::SectionBase::getOutputSection()
lld::elf::OutputSection::finalize(lld::elf::Ctx&)
void lld::elf::writeResult<llvm::object::ELFType<(llvm::endianness)1, true>>(lld::elf::Ctx&)
```

With UBSan:

```
lld/ELF/OutputSections.cpp:624:60: runtime error: member call on null pointer of type 'lld::elf::SectionBase'
    #0 in finalizeShtGroup lld/ELF/OutputSections.cpp:624:60
    #1 in lld::elf::OutputSection::finalize(lld::elf::Ctx&) lld/ELF/OutputSections.cpp:746:5
```

### Expected

An error like the one the discard path gives for an index past the table, `invalid section index in group: ...`.

### Analysis

Only the discard path checks the member indices:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/InputFiles.cpp#L647-L676

`finalizeShtGroup` uses them for kept groups:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/OutputSections.cpp#L619-L625

The discard path doesn't reject 0 either. Rejecting `secIndex == 0 || secIndex >= size` for every member, before the keep/discard decision, would cover both paths.

This report was drafted with help from an AI assistant (Claude); I reviewed it and reproduced it myself.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to