Issue 243587
Summary [lld][MachO] ld64.lld builds symbol names with StringRef(strtab + n_strx) and never checks strsize: reads past the input
Labels lld:MachO, crash
Assignees
Reporter AnantaSrikar
    `ObjFile::parse` computes the string table as `buf + stroff` and never uses `LC_SYMTAB.strsize`. Symbol names are then built with `StringRef(strtab + sym.n_strx)`, which runs `strlen` from that address. An `n_strx` at or past the end of the string table makes `ld64.lld` read past the input until it finds a zero byte. ASan reports a heap-buffer-overflow in `strlen`. Without ASan the link succeeds and the symbol gets whatever name happens to be in memory: three identical `-no_uuid` links of the object below gave three different dylibs.

**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 > strx.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:          35
      n_type: 0x1
      n_sect:          0
      n_desc:          0
      n_value: 0
  StringTable:
    - ''
    - _entry_00000000
    - _target_00000000
...
YAML
yaml2obj strx.yaml -o strx.o
ld64.lld -arch arm64 -platform_version macos 11.0 11.0 -dylib -undefined dynamic_lookup -no_uuid -o strx.dylib strx.o
```

The string table is the last 34 bytes of the file, and the second symbol's `n_strx` is 35. With `n_strx: 17` (`_target_00000000`), the same object links (status 0) and the output is the same on every run.

### Observed

```
==9==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c51545e14e4 at pc 0x55c696ff416f bp 0x7ffc27f63930 sp 0x7ffc27f630f8
    #0 in strlen
    #3 in StringRef llvm/include/llvm/ADT/StringRef.h:95:25
    #4 in lld::macho::Symbol* lld::macho::ObjFile::parseNonSectionSymbol<lld::structs::nlist_64>(lld::structs::nlist_64 const&, char const*) lld/MachO/InputFiles.cpp:821:20
    #5 in void lld::macho::ObjFile::parseSymbols<lld::macho::LP64>(...)
    #6 in void lld::macho::ObjFile::parse<lld::macho::LP64>() lld/MachO/InputFiles.cpp:1120:5
```

### Expected

An error that the symbol's name offset is outside the string table.

### Analysis

The string table pointer, without its size:

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

and one of the name constructions (the others are at lines 880, 884, 917, 934 and 1174):

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

Keeping the string table as a bounded `StringRef` of `strsize` bytes (after checking `stroff + strsize` against the file), and rejecting an `n_strx` outside it or a name without a terminator inside it, 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