| Issue |
243642
|
| Summary |
[lld][ELF] ld.lld reads SHT_RELA/SHT_REL entries from sh_offset without checking the section against the file: reads past the input
|
| Labels |
lld:ELF,
crash
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
`InputSectionBase::relsOrRelas` takes a section's relocations as `ArrayRef(buf + sh_offset, sh_size / sizeof(Rela))` from the relocation section's header, without checking that the range lies inside the file. Other sections go through `ELFFile::getSectionContents`, which rejects the same header with "has a sh_offset ... that is greater than the file size". A `.rela.text` that straddles or lies past the end of the file makes `ld.lld` read relocation entries from past the input. ASan reports a heap-buffer-overflow in `getRInfo` during relocation scanning. Without ASan, the link fails with errors built from the bytes past the file, such as `unknown relocation (129) against symbol` or `invalid symbol index`, or succeeds: which one depends on the run and on the input's file name; with `sh_offset` far outside the file, `ld.lld` segfaults, on main and on 21.1.8.
**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 > rela.yaml <<'YAML'
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_X86_64
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x4
Content: E800000000C3C3
- Name: .rela.text
Type: SHT_RELA
Flags: [ SHF_INFO_LINK ]
Link: .symtab
AddressAlign: 0x8
Info: .text
ShOffset: 608
Relocations:
- Offset: 0x1
Symbol: foo
Type: R_X86_64_PLT32
Addend: -4
Symbols:
- Name: _start
Section: .text
Binding: STB_GLOBAL
- Name: foo
Section: .text
Binding: STB_GLOBAL
Value: 0x6
...
YAML
yaml2obj rela.yaml -o rela.o
ld.lld rela.o -o rela.out
```
The file is 608 bytes, and `.rela.text` says its 24-byte entry is at offset 608, the end of the file. Without the `ShOffset` line, the same object links (status 0). With `ShOffset: 0x100000000`, `ld.lld` segfaults (status 139). The same `ShOffset: 0x100000000` on `.text` instead gives `error: section [index 1] has a sh_offset (0x100000000) + sh_size (0x7) that is greater than the file size (0x260)`.
### Observed
```
==9==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7d2f109e0698 at pc 0x55e3f505ae7c bp 0x7fff79a6fbd0 sp 0x7fff79a6fbc8
READ of size 8 at 0x7d2f109e0698 thread T0
#3 in getRInfo llvm/include/llvm/Object/ELFTypes.h:454:18
#4 in getSymbol llvm/include/llvm/Object/ELFTypes.h:475:29
#5 in scanSectionImpl<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::endianness)1, true>, true> > lld/ELF/Arch/X86_64.cpp:653:27
#6 in scanSection1<llvm::object::ELFType<(llvm::endianness)1, true>, (anonymous namespace)::X86_64> lld/ELF/RelocScan.h:190:12
#7 in (anonymous namespace)::X86_64::scanSection(lld::elf::InputSectionBase&, unsigned int) lld/ELF/Arch/X86_64.cpp:776:5
```
### Expected
The same "greater than the file size" error that other sections get.
### Analysis
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/InputSection.cpp#L181-L191
Other sections take their contents through the checked accessor:
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/InputSection.cpp#L46-L52
Checking `sh_offset + sh_size` against the file size for relocation sections (for example by getting the entries through `ELFFile::relas`/`rels`, or checking once when the relocation section is recorded) would fix it. The COFF and Mach-O ports have the same problem: #239205 and #243565.
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