| Issue |
243328
|
| Summary |
[lld][MachO] ld64.lld reads past the input when walking load commands: ncmds and cmdsize are not checked
|
| Labels |
lld:MachO,
crash
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
`detail::findCommands` walks `ncmds` load commands from the header, advancing by each command's `cmdsize`, without checking either value against `sizeofcmds` or the file size. An `ncmds` larger than the number of commands present makes it read past the end of the input buffer, and a bad `cmdsize` makes it read later commands at the wrong offset. ASan reports a heap-buffer-overflow. Without sanitizers, the same commit links the object (status 0) after the out-of-bounds reads.
**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 > ncmds.yaml <<'YAML'
--- !mach-o
IsLittleEndian: true
FileHeader:
magic: 0xFEEDFACF
cputype: 0x1000007
cpusubtype: 0x3
filetype: 0x1
ncmds: 235
sizeofcmds: 176
flags: 0x0
reserved: 0x0
LoadCommands:
- cmd: LC_SEGMENT_64
cmdsize: 152
segname: ''
vmaddr: 0
vmsize: 7
fileoff: 208
filesize: 7
maxprot: 7
initprot: 7
nsects: 1
flags: 0
Sections:
- sectname: __text
segname: __TEXT
addr: 0x0
size: 7
offset: 0xD0
align: 2
reloff: 0xD8
nreloc: 1
flags: 0x80000400
reserved1: 0x0
reserved2: 0x0
reserved3: 0x0
content: E800000000C300
relocations:
- address: 0x1
symbolnum: 1
pcrel: true
length: 2
extern: true
type: 2
scattered: false
value: 0
- cmd: LC_SYMTAB
cmdsize: 24
symoff: 224
nsyms: 2
stroff: 256
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 ncmds.yaml -o ncmds.o
ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -o ncmds.dylib ncmds.o
```
The header says 235 load commands; there are 2. With `ncmds: 2`, the same object links (status 0). Leaving `ncmds: 2` and setting the first command's `cmdsize` to 93 instead gives the misaligned access below; without sanitizers that object fails later, on the `symbols` assertion from #243309, because the symbol table command is never found.
### Observed
```
==33==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c6e933ed780 at pc 0x55ec547e8417 bp 0x7ffe18dc8010 sp 0x7ffe18dc8008
READ of size 4 at 0x7c6e933ed780 thread T0
#0 in is_contained<unsigned int, unsigned int> llvm/include/llvm/ADT/STLExtras.h:1979:14
#1 in std::vector<llvm::MachO::build_version_command const*, ...> lld::macho::detail::findCommands<llvm::MachO::build_version_command, ...>(void const*, unsigned long, ...) lld/MachO/InputFiles.h:351:9
#2 in findCommands<llvm::MachO::build_version_command, llvm::MachO::LoadCommandType> lld/MachO/InputFiles.h:374:10
#3 in getPlatformInfos lld/MachO/InputFiles.cpp:124:20
#4 in checkCompatibility(lld::macho::InputFile const*) lld/MachO/InputFiles.cpp:156:45
#5 in bool compatWithTargetArch<llvm::MachO::mach_header_64>(lld::macho::InputFile const*, llvm::MachO::mach_header_64 const*) lld/MachO/InputFiles.cpp:213:10
```
With `cmdsize: 93`:
```
lld/MachO/InputFiles.h:351:44: runtime error: member access within misaligned address 0x7d1f4bfe142d for type 'const llvm::MachO::build_version_command', which requires 4 byte alignment
```
### Expected
An error that the load commands run past `sizeofcmds` or the end of the file.
### Analysis
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.h#L341-L359
Each iteration reads `cmd->cmd` and `cmd->cmdsize` at `p` without checking that a `load_command` fits there, and advances by `cmdsize` with no lower bound, alignment or end check. Checking, before the loop, that the header plus `sizeofcmds` fits in the file, and in the loop, that each command's header and `cmdsize` fit inside `sizeofcmds` (with `cmdsize` at least `sizeof(load_command)` and a multiple of 8 for 64-bit files), would fix it. `llvm::object::MachOObjectFile` already does checks like these.
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