| Issue |
243570
|
| Summary |
[lld][MachO] ld64.lld does not check a section's offset/size against the file and copies bytes past the input into the output
|
| Labels |
lld:MachO,
crash
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
`ObjFile::parseSections` builds each section's data as `{buf + sec.offset, sec.size}` without checking that the range is inside the file, and `ConcatInputSection::writeTo` later `memcpy`s all `sec.size` bytes into the output. With a `size` larger than the file, `ld64.lld` reads past the input buffer. ASan reports a heap-buffer-overflow. Without ASan the link succeeds and the output's `__text` contains whatever followed the buffer in memory: three identical `-no_uuid` links of the object below gave three different `__text` sections. With a very large `size` it segfaults.
`MachOObjectFile` already rejects this object (`obj2yaml`: `offset field plus size field of section 0 in LC_SEGMENT_64 command 0 extends past the end of the file`); `ld64.lld` does its own parsing and has no such check.
**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 > ctl.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 ctl.yaml -o ctl.o
cp ctl.o big.o
# __text size (file offset 144): 9 -> 223; the file is 298 bytes and __text starts at 208
printf '\337' | dd of=big.o bs=1 seek=144 conv=notrunc
ld64.lld -arch arm64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -no_uuid -o big.dylib big.o
```
`ctl.o` links (status 0) with a 9-byte `__text`.
### Observed
```
==14==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c4eaffe14e1 at pc 0x5563f5f91e9f bp 0x7b1eabef23d0 sp 0x7b1eabef1b90
#0 in __asan_memcpy
#1 in lld::macho::ConcatInputSection::writeTo(unsigned char*) lld/MachO/InputSection.cpp:230:3
```
Without ASan: status 0, and `big.dylib`'s `__text` is 223 bytes, of which only the first 90 come from `big.o`.
### Expected
An error that the section extends past the end of the file.
### Analysis
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L365-L368
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputSection.cpp#L224-L230
Checking `sec.offset + sec.size <= mb.getBufferSize()` (in 64 bits) for non-zerofill sections in `parseSections`, and reporting an error otherwise, would fix it.
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