Issue 233265
Summary [lld][MachO] dupSymDiags is never cleared — use-after-free when linking multiple times in one process via liblld
Labels lld
Assignees
Reporter Da-AiXZ
    **Title:** `[lld][MachO] dupSymDiags is never cleared — use-after-free when linking multiple times in one process via liblld`

**Summary**

`lld/MachO/SymbolTable.cpp` keeps duplicate-symbol diagnostics in a file-scope static container:

```cpp
namespace {
struct DuplicateSymbolDiag {
  const std::pair<std::string, std::string> src1;
  const std::pair<std::string, std::string> src2;
  const Symbol *sym;
  ...
};
SmallVector<DuplicateSymbolDiag> dupSymDiags;
} // namespace
```

Entries are only ever `push_back`-ed (in `SymbolTable::addDefined`); **nothing ever clears `dupSymDiags`**. Notably, the analogous undefined-symbol container *is* cleared: `reportPendingUndefinedSymbols()` ends with `undefs.clear()` ("Clear the printed diagnostics to avoid printing the same things again the next time"), while `reportPendingDuplicateSymbols()` has no counterpart.

`DuplicateSymbolDiag::sym` points into the per-link `SymbolTable` allocation arena. When `lld::macho::link()` runs a second time in the same process, that arena from the first link is released, but the stale entries remain in the static container. The second link's `Writer::run()` calls `reportPendingDuplicateSymbols()` **unconditionally** (not only when the second link itself produced duplicate diagnostics), which then does `toString(*duplicate.sym)` on the dangling `Symbol*` → use-after-free.

**Impact**

Only affects in-process multi-link users (liblld / `unsafeLldMain` embedded harnesses). The standard `ld64.lld` CLI process links once and exits, so this never fires there — which is presumably why it went unnoticed.

Observed with an embedded linker harness on iOS/arm64 (lld from the `swift-6.4.0-RELEASE` toolchain; the same structure — file-scope `dupSymDiags`, no clear — is present on llvm main as of this writing):

1. First link fails with `duplicate symbol` diagnostics → entries land in `dupSymDiags`.
2. Sources fixed, second link runs in the same process → `Writer::run()` unconditionally replays the stale entries → `toString(*duplicate.sym)` reads freed arena memory.
3. Symptoms seen in the wild: crash (SIGSEGV) and, in non-fatal runs, garbage output like `duplicate symbol: ` with an empty symbol name, and diagnostics referencing symbols/objects from the *first* link's project — matching reads of recycled heap reused across links.

**Crash report excerpt** (second link, `EXC_BAD_ACCESS / SIGSEGV / KERN_INVALID_ADDRESS at 0x157538000`; note the faulting address lies exactly one byte past a freed `MALLOC_LARGE` 64 KB block `0x157528000-0x157538000`):

```
libsystem_platform.dylib  _platform_memmove + 96
libc++.1.dylib            std::string::__grow_by_and_replace(...) + 204
libc++.1.dylib            std::string::__assign_external(char const*, unsigned long) + 108
CoreCompiler              lld::macho::reportPendingDuplicateSymbols() + 148
CoreCompiler              void lld::macho::writeResult<lld::macho::LP64>() + 252
CoreCompiler              lld::macho::link(...) + 29820
CoreCompiler              lld::unsafeLldMain(...) + 1784
CoreCompiler              llvm::CrashRecoveryContext::RunSafely(...) + 160
CoreCompiler              lld::lldMain(...)
```

**Suggested fix**

Mirror the existing `undefs` handling — clear after reporting:

```cpp
void macho::reportPendingDuplicateSymbols() {
  for (const auto &duplicate : dupSymDiags) { ... }
  dupSymDiags.clear();   // ← missing today (compare: undefs.clear())
}
```

or, more structurally, move the container into the per-link context so it cannot outlive the arena it points into.

**Repro sketch**

```cpp
// In one process, twice:
const char *args[] = {"ld64.lld", ...};   // link A: deliberately duplicate a symbol
lld::unsafeLldMain(args, outs(), errs(), {{...}, "ld64.lld"}, /*canExit=*/false);
// link B: fix the duplicate, link again — Writer::run replays stale dupSymDiags → UAF
```

Happy to provide the full crash report and a standalone reproducer if useful.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to