| Issue |
243565
|
| Summary |
[lld][MachO] ld64.lld does not check a section's reloff/nreloc against the file: reads past the end
|
| Labels |
lld:MachO,
crash
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
`ObjFile::parseRelocations` builds an `ArrayRef<relocation_info>` from `buf + sec.reloff` and `sec.nreloc` without checking that the table fits in the file. A `reloff` past the end makes `ld64.lld` read from an arbitrary address and segfault. An `nreloc` larger than the table makes it read the following symbol and string table bytes, and then bytes past the end of the file, as relocation records: the link fails with errors built from those bytes, and the errors change from run to run.
**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 > reloc.yaml <<'YAML'
--- !mach-o
IsLittleEndian: true
FileHeader:
magic: 0xFEEDFACF
cputype: 0x100000C
cpusubtype: 0x0
filetype: 0x1
ncmds: 2
sizeofcmds: 176
flags: 0x0
reserved: 0x0
LoadCommands:
- cmd: LC_SEGMENT_64
cmdsize: 152
segname: ''
vmaddr: 0
vmsize: 9
fileoff: 208
filesize: 9
maxprot: 7
initprot: 7
nsects: 1
flags: 0
Sections:
- sectname: __text
segname: __TEXT
addr: 0x0
size: 9
offset: 0xD0
align: 2
reloff: 0xE0
nreloc: 1
flags: 0x80000400
reserved1: 0x0
reserved2: 0x0
reserved3: 0x0
content: 00000094C0035FD600
relocations:
- address: 0x0
symbolnum: 1
pcrel: true
length: 2
extern: true
type: 2
scattered: false
value: 0
- cmd: LC_SYMTAB
cmdsize: 24
symoff: 232
nsyms: 2
stroff: 264
strsize: 34
LinkEditData:
NameList:
- n_strx: 1
n_type: 0xF
n_sect: 1
n_desc: 0
n_value: 0
- n_strx: 17
n_type: 0x1
n_sect: 0
n_desc: 0
n_value: 0
StringTable:
- ''
- _entry_00000000
- _target_00000000
...
YAML
yaml2obj reloc.yaml -o reloc.o
cp reloc.o roff.o
# __text reloff (file offset 160): 0xE0 -> 0x100000, past the end of the 298-byte file
printf '\000\000\020\000' | dd of=roff.o bs=1 seek=160 conv=notrunc
ld64.lld -arch arm64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -o roff.dylib roff.o
```
It segfaults (status 139). `reloc.o` links (status 0). With `nreloc: 10` in the YAML instead (and no patch), the link fails with 14 errors about relocations that don't exist, such as `PAGE21 relocation must be extern at offset 808464432 of __TEXT,__text`, read from the symbol table, the string table and the bytes after the file. Eight identical runs gave seven different error outputs.
### Observed
With ASan:
```
==14==ERROR: AddressSanitizer: SEGV on unknown address 0x7c92a32e13b0 ...
#4 in getRelocAttrs lld/MachO/Target.h:114:5
#5 in hasAttr lld/MachO/Target.h:121:12
#6 in void lld::macho::ObjFile::parseRelocations<llvm::MachO::section_64>(llvm::ArrayRef<llvm::MachO::section_64>, llvm::MachO::section_64 const&, lld::macho::Section&)
#7 in void lld::macho::ObjFile::parseDeferredRelocationsImpl<lld::macho::LP64>() lld/MachO/InputFiles.cpp:1224:7
```
### Expected
An error that the section's relocation table extends past the end of the file.
### Analysis
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L565-L569
Checking `sec.reloff + sec.nreloc * sizeof(relocation_info) <= mb.getBufferSize()` (in 64 bits) before building `relInfos`, and reporting an error otherwise, would fix both cases.
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