| Issue |
239476
|
| Summary |
[gold] SIGBUS in LLVMgold.so when GNU nm reads a truncated .a: MemoryBuffer::getOpenFileSlice maps the archive member past EOF without checking
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
benjamin-rood
|
## Summary
GNU `nm` (binutils 2.47) with `LLVMgold.so` installed in `lib/bfd-plugins` dies with **SIGBUS** when it is given a **truncated static archive** (`.a`) of ordinary, non-LTO ELF objects. Without the plugin the same `nm` reports `file format not recognized` and exits normally.
The cause is that `claim_file_hook` in the gold plugin asks `MemoryBuffer::getOpenFileSlice` to map an archive member using the size from the member's `ar` header, and `getOpenFileSlice` never compares `Offset + MapSize` with the real size of the file. When the file is shorter than the header claims, the slice is `mmap`ed past the end of the file, and the first read of a page that has no file data behind it raises SIGBUS (`si_code = BUS_ADRERR`).
A truncated archive is a realistic input: a build that was killed, a full disk, a partial download, or a cleanup that races a reader.
## Reproducer
Self-contained; needs `clang`, `ar`, and GNU `nm` with `LLVMgold.so` in its `bfd-plugins` directory.
```sh
#!/bin/sh
set -eu
d=$(mktemp -d)
cd "$d"
# Four ordinary (non-LTO) ELF objects, each a few hundred KB.
for n in a b c d; do
awk -v n="$n" 'BEGIN { for (i = 0; i < 3000; i++) printf "int f_%s_%d(int x){ return x*%d+%d; }\n", n, i, (i*37+11)%9973+2, i }' > "$n.c"
clang -O0 -c "$n.c" -o "$n.o"
done
ar rcs libdemo.a a.o b.o c.o d.o
size=$(wc -c < libdemo.a)
echo "intact archive: $size bytes"
nm --defined-only libdemo.a | wc -l
# Cut the archive in half.
head -c $((size / 2)) libdemo.a > half.a
echo "truncated archive: $(wc -c < half.a) bytes"
set +e
nm --defined-only half.a > out.txt 2> err.txt
echo "nm exit status: $? (135 = killed by SIGBUS)"
echo "stderr: $(cat err.txt)"
```
Output on my machine:
```
intact archive: 1602528 bytes
12008
truncated archive: 801264 bytes
nm exit status: 135 (135 = killed by SIGBUS)
stderr:
```
Control, the same truncated archive with a copy of `nm` placed where `../lib/bfd-plugins` does not exist (so no plugin is loaded):
```
exit=0
./bin/nm: b.o: file format not recognized
```
A single truncated object file (not an archive) does not crash; it is the archive-member slice that does.
## Backtrace (gdb, `nm --defined-only half.a`)
```
Program received signal SIGBUS, Bus error.
#0 llvm::object::ELFObjectFile<llvm::object::ELFType<(llvm::endianness)1, true> >::initContent() () from /usr/lib/libLLVM.so.23.1
#1 llvm::object::ObjectFile::createELFObjectFile(llvm::MemoryBufferRef, bool) () from /usr/lib/libLLVM.so.23.1
#2 llvm::object::ObjectFile::createObjectFile(llvm::MemoryBufferRef, llvm::file_magic, bool) () from /usr/lib/libLLVM.so.23.1
#3 llvm::object::IRObjectFile::findBitcodeInMemBuffer(llvm::MemoryBufferRef) () from /usr/lib/libLLVM.so.23.1
#4 llvm::object::readIRSymtab(llvm::MemoryBufferRef) () from /usr/lib/libLLVM.so.23.1
#5 llvm::lto::InputFile::create(llvm::MemoryBufferRef) () from /usr/lib/libLLVM.so.23.1
#6 ?? () from /usr/bin/../lib/bfd-plugins/LLVMgold.so
#7 ?? () from /usr/lib/libbfd-2.47.so
#8 ?? () from /usr/lib/libbfd-2.47.so
#9 bfd_check_format_matches () from /usr/lib/libbfd-2.47.so
#10 ?? () from /usr/bin/nm
```
Fault details from the same gdb session: `si_code = 2` (`BUS_ADRERR`), `si_addr = 0x7fffed151fdc`. The mapping of the archive that contains the fault address is:
```
0x7fffed0f9000 0x7fffed153000 size 0x5a000 file offset 0x7d000 r--p half.a
```
`half.a` is 801,264 bytes. The mapping covers file offsets `0x7d000` to `0xd7000` (880,640), which is 79,376 bytes past the end of the file. The faulting address corresponds to file offset `0xd5fdc` (876,508), inside that tail.
## Where the size goes unchecked
All references are to `llvm/llvm-project` at `bd654a85865bdfa48f926e679a12d32b39d1eba0` (main, fetched 2026-11-06).
- `llvm/tools/gold/gold-plugin.cpp:551-556`: for a member found inside an archive, the plugin calls `MemoryBuffer::getOpenFileSlice(fd, file->name, file->filesize, offset)`. `file->filesize` and `file->offset` come from the linker or tool (here, binutils), and are the archive member's header size and position.
- `llvm/lib/Support/MemoryBuffer.cpp:581-588`: `getOpenFileSlice` passes `FileSize = -1` and `RequiresNullTerminator = false` to `getOpenFileImpl`.
- `llvm/lib/Support/MemoryBuffer.cpp:391-392`, in `shouldUseMmap`: `if (!RequiresNullTerminator) return true;`. For any `MapSize >= 4 * 4096` this returns before the code at lines 398-408 that looks up the real file size and has `assert(End <= FileSize)`. That check only runs when a null terminator is required, and `getOpenFileSlice` never requires one, so a slice is never compared with the real file size.
- `MemoryBuffer.cpp:515-520`: the buffer is then built as `MemoryBufferMMapFile` over `Offset .. Offset + MapSize`, which extends past EOF.
Binutils passes the unvalidated header size in as well: `bfd/plugin.c:454-455` sets `file->offset = ibfd->origin; file->filesize = arelt_size (ibfd);`. So a binutils-side check would also help, but the LLVM side is the one that turns a bad number into a crash.
## Expected behaviour
A truncated archive should produce an error, as it does with no plugin loaded, not a signal. For example, `getOpenFileSlice` could `fstat` the file and return `errc::invalid_argument` (or `errc::value_too_large`) when `Offset + MapSize > FileSize`, so that `claim_file_hook` takes its existing error path (`message(LDPL_ERROR, EC.message().c_str()); return LDPS_ERR;`, `gold-plugin.cpp:557-559`) instead of crashing. Other callers of `getOpenFileSlice` would get the same protection.
## Environment
- Arch Linux x86-64, Linux 7.2
- LLVM / `libLLVM.so.23.1` and `LLVMgold.so` from Arch package `llvm 23.1.3-4`; clang from the same package
- GNU binutils 2.47 (Arch package `binutils 2.47-4`); the plugin is picked up automatically from `/usr/lib/bfd-plugins/LLVMgold.so`
- I did not try other platforms or older LLVM releases.
I found this when a test deliberately truncated an archive to half its size and a script then ran `nm -g --defined-only` over it.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs