Issue 243574
Summary [lld][MachO] ld64.lld does not check LC_SEGMENT_64 nsects against the command size: reads section headers past the input
Labels lld:MachO, crash
Assignees
Reporter AnantaSrikar
    `ObjFile::parse` takes the section headers of `LC_SEGMENT_64` as `ArrayRef<SectionHeader>{c + 1, c->nsects}` without checking `nsects` against the command's `cmdsize` or the file size. A segment that claims more sections than it has makes `ld64.lld` read section headers from past the end of the input. ASan reports a heap-buffer-overflow in `parseSections`. Without ASan the link fails with errors built from those bytes, for example `alignment 21932 of section ... is too large` with a section name of non-ASCII garbage, and the errors differ on every run (six identical runs gave six different outputs).

**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 > nsects.yaml <<'YAML'
--- !mach-o
IsLittleEndian:  true
FileHeader:
  magic:           0xFEEDFACF
 cputype:         0x1000007
  cpusubtype:      0x3
  filetype:        0x1
 ncmds:           2
  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:          130
 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 nsects.yaml -o nsects.o
ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -o nsects.dylib nsects.o
```

The segment says 130 sections; there is one, and the file is 290 bytes. With `nsects: 1`, the same object links (status 0).

### Observed

```
==45==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c686fde14f8 at pc 0x562856730a24 bp 0x7ffe51394170 sp 0x7ffe51394168
    #0 in lld::macho::Section* lld::make<lld::macho::Section, lld::macho::ObjFile*, llvm::StringRef&, llvm::StringRef&, ...>(...)
    #1 in void lld::macho::ObjFile::parseSections<llvm::MachO::section_64>(llvm::ArrayRef<llvm::MachO::section_64>) lld/MachO/InputFiles.cpp:358:24
    #2 in void lld::macho::ObjFile::parse<lld::macho::LP64>() lld/MachO/InputFiles.cpp:1107:5
```

### Expected

An error that the segment's section headers don't fit in its load command.

### Analysis

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L1102-L1108

`parseDeferredRelocationsImpl` builds the same array again:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/InputFiles.cpp#L1216-L1218

Checking `sizeof(SegmentCommand) + nsects * sizeof(SectionHeader) <= cmdsize`, 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

Reply via email to