Issue 243603
Summary [lld][MachO] ld64.lld takes the symbol table as ArrayRef(buf + symoff, nsyms) without checking it against the file: reads past the input
Labels lld:MachO, crash
Assignees
Reporter AnantaSrikar
    `ObjFile::parse` builds the symbol table as `ArrayRef<NList>(buf + c->symoff, c->nsyms)` straight from `LC_SYMTAB`, without checking that `symoff + nsyms * sizeof(nlist_64)` fits in the file. An `nsyms` larger than the table makes `ld64.lld` read symbol entries from past the end of the input. ASan reports a heap-buffer-overflow in `parseSymbols`. Without ASan, the object below crashes with a segmentation fault in `parseNonSectionSymbol` on every run, on main and on 21.1.8.

**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 > nsyms.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:          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:           36866
    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 nsyms.yaml -o nsyms.o
ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -o nsyms.dylib nsyms.o
```

The symbol table has two entries at offset 224 and the file is 290 bytes, but `nsyms` is 36866. With `nsyms: 2`, the same object links (status 0). A smaller count such as `nsyms: 60` also gives the ASan report; without ASan it crashes on some runs and links on others.

### Observed

```
==79==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7cb45efe14e4 at pc 0x556213ff7b11 bp 0x7ffd4227ccd0 sp 0x7ffd4227ccc8
READ of size 1 at 0x7cb45efe14e4 thread T0
    #0 in void lld::macho::ObjFile::parseSymbols<lld::macho::LP64>(llvm::ArrayRef<lld::macho::LP64::section>, llvm::ArrayRef<lld::macho::LP64::nlist>, char const*, bool) lld/MachO/InputFiles.cpp:875:13
    #1 in void lld::macho::ObjFile::parse<lld::macho::LP64>() lld/MachO/InputFiles.cpp:1120:5
```

### Expected

An error that the symbol table extends past the end of the file.

### Analysis

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

`ObjFile::parseLazy` builds the same array:

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

Checking `symoff + nsyms * sizeof(NList)` against the buffer size, with overflow-safe arithmetic, before building the array in both places would fix it. The string table has the same missing check (`stroff`/`strsize`), reported in #243587.

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