Issue 243352
Summary [Object][COFF] COFFObjectFile accepts NumberOfSymbols past the end of the file; lld-link reads out of bounds
Labels lld:COFF
Assignees
Reporter AnantaSrikar
    `COFFObjectFile::initSymbolTablePtr` is meant to check that the symbol table fits in the file, but the check is a no-op: it computes the table size with `getNumberOfSymbols()`, which returns 0 until `SymbolTable16`/`SymbolTable32` is set, and that pointer is what the check is assigning. The string table offset that follows is computed in 32 bits, so a `NumberOfSymbols` increased by 2^31 wraps back to the same offset (2^31 × 18 is a multiple of 2^32) and that check passes too. `lld-link` then walks two billion symbols and reads past the object buffer.

**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 -DLLVM_USE_SANITIZER="Address;Undefined"`, on x86_64 Linux.

### Reproducer

```sh
cat > x64.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:     C3
symbols:
  - Name:            f
    Value:           0
    SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj x64.yaml -o x64.obj
cp x64.obj nsyms.obj
# NumberOfSymbols is at offset 12; make it 0x80000001 instead of 1.
printf '\200' | dd of=nsyms.obj bs=1 seek=15 conv=notrunc
lld-link /dll /noentry /machine:x64 /out:nsyms.dll nsyms.obj
```

`x64.obj` links (status 0).

### Observed

```
==16==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c24137e00cf at pc 0x55f8ba440498 bp 0x7ffcb0c988c0 sp 0x7ffcb0c988b8
    #0 in getStorageClass llvm/include/llvm/Object/COFF.h:345
    #1 in isExternal llvm/include/llvm/Object/COFF.h:383:12
    #2 in llvm::object::COFFSymbolRef::isUndefined() const llvm/include/llvm/Object/COFF.h:392:12
    #3 in lld::coff::ObjFile::initializeSymbols() lld/COFF/InputFiles.cpp:603:17
    #4 in lld::coff::ObjFile::parse() lld/COFF/InputFiles.cpp:357:3
```

Without sanitizers, the same commit reads the bytes past the buffer as symbols and fails with `error: nsyms.obj:  should not refer to special section 0`. `llvm-readobj --file-headers` reports the bogus count (`SymbolCount: 2147483649`) without complaint.

### Expected

An error that the symbol table extends past the end of the file, from `COFFObjectFile::create`.

### Analysis

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/llvm/lib/Object/COFFObjectFile.cpp#L416-L433

`getNumberOfSymbols()` returns 0 while both table pointers are null:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/llvm/include/llvm/Object/COFF.h#L1011-L1023

Using `getRawNumberOfSymbols()` in the two `getObject` size calculations, and computing `StringTableOffset` in 64 bits, would make both checks work.

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