Issue 239370
Summary [lld][COFF] lld-link indexes its symbol vector with an unchecked relocation SymbolTableIndex
Labels lld:COFF, crash
Assignees
Reporter AnantaSrikar
    I'm Ananta Srikar Puranam, a PhD student at Washington State University. I'm reporting this bug on behalf of my team: Emily West, Dr. Swarn Priya, Dr. Peng Gao and Dr. Basavesh Ammanaghatta Shivakumar.

`lld-link` crashes when a relocation's `SymbolTableIndex` is not less than the number of symbols LLD read from the object. `ObjFile::getSymbol` indexes `symbols` without a bounds check. An assertions build fails the `std::vector::operator[]` assertion in `markLive`; release builds read past the vector and usually segfault.

The index can be too large in two ways. The relocation can name an index past the symbol table, or the symbol table itself can be malformed. In the second case `COFFObjectFile` drops the table and reports zero symbols, while the relocations still name the original indexes.

**Version**: main at 443782fff0c6 (2026-11-05), 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 > symidx.yaml <<'YAML'
--- !COFF
header:
  Machine: IMAGE_FILE_MACHINE_AMD64
  Characteristics: [ ]
sections:
  - Name: .text
    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
    Alignment:       16
    SectionData: E800000000C3
    Relocations:
      - VirtualAddress:  1
 SymbolTableIndex: 2
        Type: IMAGE_REL_AMD64_REL32
symbols:
  - Name:            f
    Value: 0
    SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  - Name:            g
    Value:           5
 SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj symidx.yaml -o symidx.obj
lld-link /dll /noentry /machine:x64 /out:symidx.dll symidx.obj
```

The object has two symbols, and the relocation names index 2. With `SymbolTableIndex: 1`, the same command links (status 0).

The second way: start from the `SymbolTableIndex: 1` object and move the symbol table past the end of the file. The same assertion fails, and a release build (LLD 22.1.2) segfaults:

```sh
sed 's/SymbolTableIndex: 2/SymbolTableIndex: 1/' symidx.yaml | yaml2obj -o nosymtab.obj
printf '\377' | dd of=nosymtab.obj bs=1 seek=8 conv=notrunc   # PointerToSymbolTable = 0xff
lld-link /dll /noentry /machine:x64 /out:nosymtab.dll nosymtab.obj
```

Fuzzed objects that corrupt the string-table size, or move `PointerToSymbolTable` by a few bytes, behave the same way.

### Observed

```
.../bits/stl_vector.h:1253: reference std::vector<lld::coff::Symbol *>::operator[](size_type) [_Tp = lld::coff::Symbol *, _Alloc = std::allocator<lld::coff::Symbol *>]: Assertion '__n < this->size()' failed.
```

```
lld::coff::markLive(lld::coff::COFFLinkerContext&)
lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
lld::coff::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
```

With `/opt:noref`, the same assertion fails later, in `SectionChunk::applyRelocation` called from `SectionChunk::writeTo`.

### Expected

An error for the out-of-range symbol index, or for the malformed symbol table, instead of a crash.

### Analysis

`ObjFile::initializeSymbols` sizes `symbols` from `getNumberOfSymbols()`:

https://github.com/llvm/llvm-project/blob/443782fff0c659be544d190df20adfd2a6294ba3/lld/COFF/InputFiles.cpp#L587-L588

`getNumberOfSymbols()` returns 0 when the symbol table could not be read, and `COFFObjectFile` treats that as recoverable:

https://github.com/llvm/llvm-project/blob/443782fff0c659be544d190df20adfd2a6294ba3/llvm/lib/Object/COFFObjectFile.cpp#L985-L992

https://github.com/llvm/llvm-project/blob/443782fff0c659be544d190df20adfd2a6294ba3/llvm/include/llvm/Object/COFF.h#L1019-L1023

Relocation consumers pass `SymbolTableIndex` to `getSymbol`, which indexes the vector without a check:

https://github.com/llvm/llvm-project/blob/443782fff0c659be544d190df20adfd2a6294ba3/lld/COFF/InputFiles.h#L170-L172

https://github.com/llvm/llvm-project/blob/443782fff0c659be544d190df20adfd2a6294ba3/lld/COFF/Chunks.h#L250

https://github.com/llvm/llvm-project/blob/443782fff0c659be544d190df20adfd2a6294ba3/lld/COFF/MarkLive.cpp#L75-L77

Other relocation consumers (`applyRelocation`, `getBaserels`, the `Writer` relocation scans and ICF) call `getSymbol` the same way. Checking each relocation's `SymbolTableIndex` against `symbols.size()` once, when the section is read, would cover all of them. LLD could also report an error when an object with `NumberOfSymbols != 0` comes back with no symbol table.

Related: #235244 is the same unchecked `symbols[]` indexing from a weak external's `TagIndex`. #238930 is an in-range `SymbolTableIndex` that names an auxiliary record, so `getSymbol` returns null.

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