| Issue |
243558
|
| Summary |
[lld][MachO] ld64.lld applies a relocation whose r_address is past the end of its section: out-of-section write or crash
|
| Labels |
lld:MachO,
crash
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
`ObjFile::parseRelocations` copies a relocation's `r_address` into `Relocation::offset` without checking it against the section size. When no subsection contains the offset, `findContainingSubsection` returns the last subsection that starts before it, again without an upper-bound check. `ConcatInputSection::writeTo` then applies the relocation at `buf + r.offset`. With a large `r_address`, `ld64.lld` segfaults. With an `r_address` just past the end of the section, it links with status 0 and writes the relocated bytes after the section in the output.
**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 > raddr.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: 0x40000000
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 raddr.yaml -o raddr.o
ld64.lld -arch arm64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -o raddr.dylib raddr.o
```
It segfaults (status 139). With `address: 0x0`, the same object links (status 0).
With `address: 0x8` (the 4-byte `ARM64_RELOC_BRANCH26` fixup then starts at byte 8 of the 9-byte `__text`), and the second symbol made absolute so the branch value isn't zero (`n_type: 0x3`, `n_value: 1048576`), the link succeeds and the two bytes after `__text` in the output are `ff 03`. With `address: 0x0` they are `00 00`.
### Observed
```
Segmentation fault (core dumped)
```
With ASan, the fault is a 4-byte read through `llvm::support::endian::read` while applying the relocation.
### Expected
An error that the relocation's offset is outside its section.
### Analysis
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L619
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L660-L666
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputSection.cpp#L232-L234
Checking in `validateRelocationInfo` (or before `findContainingSubsection`) that `r_address + (1 << r_length) <= sec.size`, and reporting an error otherwise, would fix both cases. #46325 is the same missing check in the ELF port.
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