[llvm] [Object] Validate the big archive global symbol table's symbol count (PR #230777)
Prasad Deshpande via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 03:34:40 PDT 2026
https://github.com/Prasad-desh created https://github.com/llvm/llvm-project/pull/230777
The symbol count is read from the archive. The offset and string tables were derived from it as 8 * (SymNum + 1), held in an unsigned so it also truncates to 32 bits, and subtracting that from the table's size underflows. A count of 6 in a 43-byte table is enough: the string table ends up pointing past the end of the buffer with a length near 2^64, and llvm-nm aborts in basic_string::append. A count of 2^64-1 wraps the multiply instead and lands in the same place.
Bound the count against the table's size before using it, and take the subtables with substr rather than building StringRefs from raw pointers.
Separately, the existing test for this area, llvm/test/Object/bigarchive-malformed-global-symbol-table, has no suffix lit recognises, so it has never run. Renamed it - it passes as it is.
Assisted-by: Claude (Anthropic)
>From 382ccd7373c2ae6e311051198af019cb4da09e7c Mon Sep 17 00:00:00 2001
From: Prasad Deshpande <officiallyprasad at gmail.com>
Date: Sat, 10 Oct 2026 16:00:34 +0530
Subject: [PATCH 1/3] [Object] Validate the big archive global symbol table's
symbol count
The count is read from the archive and the offset and string tables were derived from it as 8 * (SymNum + 1), held in an unsigned so it also truncates to 32 bits. Subtracting that from the table's size underflows: a count of 6 in a 43-byte table leaves the string table pointing past the end of the buffer with a length near 2^64, and llvm-nm aborts on it. Bound the count against the table's size first, and take the subtables with substr so a slip cannot produce an out-of-bounds StringRef.
Assisted-by: Claude (Anthropic)
---
llvm/lib/Object/Archive.cpp | 54 ++++++++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 15 deletions(-)
diff --git a/llvm/lib/Object/Archive.cpp b/llvm/lib/Object/Archive.cpp
index be0dcaab9ba5e..6960062ef5f72 100644
--- a/llvm/lib/Object/Archive.cpp
+++ b/llvm/lib/Object/Archive.cpp
@@ -1479,9 +1479,10 @@ struct GlobalSymtabInfo {
StringRef StringTable;
};
-static void
+static Error
appendGlobalSymbolTableInfo(SmallVector<GlobalSymtabInfo> &SymtabInfos,
- const char *GlobalSymtabLoc, uint64_t Size) {
+ const char *GlobalSymtabLoc, uint64_t Size,
+ const char *BitMessage) {
// In a big archive, a global symbol table contains the following information:
// - The number of symbols.
// - The array of offsets into the archive file. The length is eight
@@ -1489,15 +1490,30 @@ appendGlobalSymbolTableInfo(SmallVector<GlobalSymtabInfo> &SymtabInfos,
// - The name-string table. The size is:
// Size-(8*(the number of symbols + 1)).
+ if (Size < sizeof(uint64_t))
+ return malformedError(Twine(BitMessage) +
+ " global symbol table is too small to read the "
+ "symbol count, size is " +
+ Twine(Size));
+
StringRef SymbolTable =
StringRef(GlobalSymtabLoc + sizeof(BigArMemHdrType), Size);
- uint64_t SymNum = read64be(GlobalSymtabLoc + sizeof(BigArMemHdrType));
- StringRef SymbolOffsetTable = StringRef(SymbolTable.data() + 8, 8 * SymNum);
- unsigned SymOffsetsSize = 8 * (SymNum + 1);
- uint64_t SymbolTableStringSize = Size - SymOffsetsSize;
- StringRef StringTable =
- StringRef(SymbolTable.data() + SymOffsetsSize, SymbolTableStringSize);
+ uint64_t SymNum = read64be(SymbolTable.data());
+
+ // SymNum is read from the archive. Bound it by dividing rather than by
+ // forming 8 * (SymNum + 1), which wraps for a large symbol count and would
+ // then leave the offset and string tables pointing outside SymbolTable.
+ if (SymNum > Size / sizeof(uint64_t) - 1)
+ return malformedError(Twine(BitMessage) + " global symbol table has " +
+ Twine(SymNum) + " symbols, more than its size of " +
+ Twine(Size) + " can hold");
+
+ uint64_t SymOffsetsSize = (SymNum + 1) * sizeof(uint64_t);
+ StringRef SymbolOffsetTable =
+ SymbolTable.substr(sizeof(uint64_t), SymNum * sizeof(uint64_t));
+ StringRef StringTable = SymbolTable.substr(SymOffsetsSize);
SymtabInfos.push_back({SymNum, SymbolTable, SymbolOffsetTable, StringTable});
+ return Error::success();
}
BigArchive::BigArchive(MemoryBufferRef Source, Error &Err)
@@ -1572,10 +1588,18 @@ BigArchive::BigArchive(MemoryBufferRef Source, Error &Err)
SmallVector<GlobalSymtabInfo> SymtabInfos;
- if (GlobSymtab32Offset)
- appendGlobalSymbolTableInfo(SymtabInfos, GlobSymtab32Loc, GlobSymtab32Size);
- if (GlobSymtab64Offset)
- appendGlobalSymbolTableInfo(SymtabInfos, GlobSymtab64Loc, GlobSymtab64Size);
+ if (GlobSymtab32Offset) {
+ Err = appendGlobalSymbolTableInfo(SymtabInfos, GlobSymtab32Loc,
+ GlobSymtab32Size, "32-bit");
+ if (Err)
+ return;
+ }
+ if (GlobSymtab64Offset) {
+ Err = appendGlobalSymbolTableInfo(SymtabInfos, GlobSymtab64Loc,
+ GlobSymtab64Size, "64-bit");
+ if (Err)
+ return;
+ }
if (SymtabInfos.size() == 1) {
SymbolTable = SymtabInfos[0].SymbolTable;
@@ -1594,9 +1618,9 @@ BigArchive::BigArchive(MemoryBufferRef Source, Error &Err)
Out << SymtabInfos[1].StringTable;
SymbolTable = MergedGlobalSymtabBuf;
// The size of the symbol offset to the member file is 8 bytes.
- StringTable = StringRef(SymbolTable.begin() + (SymNum + 1) * 8,
- SymtabInfos[0].StringTable.size() +
- SymtabInfos[1].StringTable.size());
+ StringTable = SymbolTable.substr((SymNum + 1) * sizeof(uint64_t),
+ SymtabInfos[0].StringTable.size() +
+ SymtabInfos[1].StringTable.size());
}
child_iterator I = child_begin(Err, false);
>From 958229a4a3faecec3c9f958582c60e0aed3a9077 Mon Sep 17 00:00:00 2001
From: Prasad Deshpande <officiallyprasad at gmail.com>
Date: Sat, 10 Oct 2026 16:02:18 +0530
Subject: [PATCH 2/3] [Object] Add tests for a malformed big archive symbol
count
---
.../bigarchive-malformed-symbol-count.test | 54 +++++++++++++++++++
1 file changed, 54 insertions(+)
create mode 100644 llvm/test/Object/bigarchive-malformed-symbol-count.test
diff --git a/llvm/test/Object/bigarchive-malformed-symbol-count.test b/llvm/test/Object/bigarchive-malformed-symbol-count.test
new file mode 100644
index 0000000000000..94dc0cb165ff7
--- /dev/null
+++ b/llvm/test/Object/bigarchive-malformed-symbol-count.test
@@ -0,0 +1,54 @@
+## Test a big archive whose global symbol table declares more symbols than it
+## can hold. The symbol count is read from the archive, so the table's extent
+## has to be derived from it without overflowing.
+
+# RUN: rm -rf %t && mkdir %t && cd %t
+# RUN: yaml2obj %s -o t.o
+# RUN: llvm-ar q t.a t.o
+
+## The 32-bit global symbol table offset lives in the fixed-length header, and
+## its content starts after a 114-byte member header. Both helpers below seek
+## relative to it rather than to a fixed file offset.
+
+# RUN: cp t.a sym_count.a
+# RUN: cp t.a sym_count_max.a
+# RUN: cp t.a sym_count_tiny.a
+
+## 8 * (6 + 1) is larger than the table, which underflowed the string table's
+## length.
+# RUN: %python -c "import struct; f = open('sym_count.a', 'r+b'); d = f.read(); o = int(d[28:48]); f.seek(o + 114); f.write(struct.pack('>Q', 6))"
+# RUN: not llvm-ar t sym_count.a 2>&1 | FileCheck -DFILE=sym_count.a --check-prefix=COUNT %s
+
+## A symbol count of 2^64-1 wraps to zero in 8 * (count + 1).
+# RUN: %python -c "import struct; f = open('sym_count_max.a', 'r+b'); d = f.read(); o = int(d[28:48]); f.seek(o + 114); f.write(struct.pack('>Q', 0xFFFFFFFFFFFFFFFF))"
+# RUN: not llvm-ar t sym_count_max.a 2>&1 | FileCheck -DFILE=sym_count_max.a --check-prefix=COUNT-MAX %s
+
+## A symbol table too small to hold the symbol count at all.
+# RUN: %python -c "f = open('sym_count_tiny.a', 'r+b'); d = f.read(); o = int(d[28:48]); f.seek(o); f.write(b'4'.ljust(20))"
+# RUN: not llvm-ar t sym_count_tiny.a 2>&1 | FileCheck -DFILE=sym_count_tiny.a --check-prefix=TINY %s
+
+## The unmodified archive still reads back.
+# RUN: llvm-nm --print-armap t.a | FileCheck --check-prefix=VALID %s
+
+# COUNT: error: unable to load '[[FILE]]': truncated or malformed archive (32-bit global symbol table has 6 symbols, more than its size of {{[0-9]+}} can hold)
+# COUNT-MAX: error: unable to load '[[FILE]]': truncated or malformed archive (32-bit global symbol table has 18446744073709551615 symbols, more than its size of {{[0-9]+}} can hold)
+# TINY: error: unable to load '[[FILE]]': truncated or malformed archive (32-bit global symbol table is too small to read the symbol count, size is 4)
+
+# VALID: Archive map
+# VALID: export_protected_var in t.o
+
+--- !XCOFF
+FileHeader:
+ MagicNumber: 0x1DF
+Sections:
+ - Name: .data
+ Flags: [ STYP_DATA ]
+Symbols:
+ - Name: export_protected_var
+ Section: .data
+ Type: 0x4000
+ StorageClass: C_EXT
+ AuxEntries:
+ - Type: AUX_CSECT
+ SymbolAlignmentAndType: 0x09
+ StorageMappingClass: XMC_RW
>From 5ca9e566e7b67e5c7f75992fe34725d831d2584e Mon Sep 17 00:00:00 2001
From: Prasad Deshpande <officiallyprasad at gmail.com>
Date: Sat, 10 Oct 2026 16:03:39 +0530
Subject: [PATCH 3/3] [Object] Give the big archive symbol table test a .test
suffix so lit runs it
---
...symbol-table => bigarchive-malformed-global-symbol-table.test} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename llvm/test/Object/{bigarchive-malformed-global-symbol-table => bigarchive-malformed-global-symbol-table.test} (100%)
diff --git a/llvm/test/Object/bigarchive-malformed-global-symbol-table b/llvm/test/Object/bigarchive-malformed-global-symbol-table.test
similarity index 100%
rename from llvm/test/Object/bigarchive-malformed-global-symbol-table
rename to llvm/test/Object/bigarchive-malformed-global-symbol-table.test
More information about the llvm-commits
mailing list