[PATCH] ld/pdb: Fix uint16_t overflow in calculate_symbols_size and parse_symbols
Haolin Xue
xuehaolin@uniontech.com
Mon Nov 30 02:22:33 GMT 2026
The calculate_symbols_size() and parse_symbols() functions in ld/pdb.c
use a uint16_t variable to hold the length of CodeView symbol records:
uint16_t len = bfd_getl16 (data) + sizeof (uint16_t);
When bfd_getl16() returns 0xFFFE, the addition produces 0x10000, which
truncates to 0 when stored in uint16_t. This causes the loop to stall
(data += 0, size -= 0) resulting in an infinite loop.
In parse_symbols(), the "if (len > size)" check (added by 81814b6f5bd,
2022-12-09) is defeated when len wraps to 0, because (0 > size) is
false. calculate_symbols_size() has no upper-bound check at all.
Commit 147e60cae54 addressed other integer-overflow issues in this file
but left these two functions unchanged in this respect.
Additionally, parse_symbols() uses "while (size >= sizeof (uint16_t))"
but then reads 4 bytes (len + type), which can cause a heap-buffer-overflow
when only 2-3 bytes remain.
Fix by changing uint16_t len to unsigned int in both functions, changing
the while condition to sizeof (uint32_t) in parse_symbols, and adding
"if (len > size)" bounds checks in the S_GPROC32/S_LPROC32 skip paths
of both functions. The skip path checks are defense-in-depth; under
normal pipeline operation they are not reachable because
calculate_symbols_size rejects the malformed input first.
ld/
PR ld/35058
* pdb.c (calculate_symbols_size): Change uint16_t len to unsigned
int. Add bounds check for len > size. Add bounds check in
S_GPROC32/S_LPROC32 skip path.
(parse_symbols): Change uint16_t len to unsigned int. Change while
condition from sizeof(uint16_t) to sizeof(uint32_t). Add bounds
check in S_GPROC32/S_LPROC32 skip path.
* testsuite/ld-pe/pdb5a.s: New test source.
* testsuite/ld-pe/pdb5b.s: New test source.
* testsuite/ld-pe/pdb.exp (test12, test13): Run the new tests.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=35058
Signed-off-by: Haolin Xue <xuehaolin@uniontech.com>
---
ld/pdb.c | 25 ++++++++++++++---
ld/testsuite/ld-pe/pdb.exp | 55 ++++++++++++++++++++++++++++++++++++++
ld/testsuite/ld-pe/pdb5a.s | 30 +++++++++++++++++++++
ld/testsuite/ld-pe/pdb5b.s | 24 +++++++++++++++++
4 files changed, 131 insertions(+), 3 deletions(-)
create mode 100644 ld/testsuite/ld-pe/pdb5a.s
create mode 100644 ld/testsuite/ld-pe/pdb5b.s
diff --git a/ld/pdb.c b/ld/pdb.c
index 39bba132e49..b0ac2f2fd8b 100644
--- a/ld/pdb.c
+++ b/ld/pdb.c
@@ -1022,9 +1022,10 @@ parse_symbols (uint8_t * data,
unsigned int scope_level = 0;
uint8_t * scope = NULL;
- while (size >= sizeof (uint16_t))
+ while (size >= sizeof (uint32_t))
{
- uint16_t len, type;
+ unsigned int len;
+ uint16_t type;
len = bfd_getl16 (data) + sizeof (uint16_t);
@@ -1148,6 +1149,12 @@ parse_symbols (uint8_t * data,
len = bfd_getl16 (data) + sizeof (uint16_t);
+ if (len > size)
+ {
+ bfd_set_error (bfd_error_bad_value);
+ return false;
+ }
+
data += len;
size -= len;
@@ -1791,9 +1798,15 @@ calculate_symbols_size (uint8_t *data, uint32_t size, uint32_t *sym_size)
while (size >= sizeof (uint32_t))
{
- uint16_t len = bfd_getl16 (data) + sizeof (uint16_t);
+ unsigned int len = bfd_getl16 (data) + sizeof (uint16_t);
uint16_t type = bfd_getl16 (data + sizeof (uint16_t));
+ if (len > size)
+ {
+ bfd_set_error (bfd_error_bad_value);
+ return false;
+ }
+
switch (type)
{
case S_LDATA32:
@@ -1861,6 +1874,12 @@ calculate_symbols_size (uint8_t *data, uint32_t size, uint32_t *sym_size)
len = bfd_getl16 (data) + sizeof (uint16_t);
+ if (len > size)
+ {
+ bfd_set_error (bfd_error_bad_value);
+ return false;
+ }
+
data += len;
size -= len;
diff --git a/ld/testsuite/ld-pe/pdb.exp b/ld/testsuite/ld-pe/pdb.exp
index 477c573fc05..0a642e60dd7 100644
--- a/ld/testsuite/ld-pe/pdb.exp
+++ b/ld/testsuite/ld-pe/pdb.exp
@@ -1865,6 +1865,59 @@ proc test11 { } {
}
}
+# Check that a .debug$S section with a symbol record whose length field
+# would overflow a uint16_t is rejected, rather than causing an infinite
+# loop or out-of-bounds read.
+
+proc test12 { } {
+ global as
+ global ld
+ global srcdir
+ global subdir
+ global exec_output
+
+ if ![ld_assemble $as $srcdir/$subdir/pdb5a.s tmpdir/pdb5a.o] {
+ unsupported "Build pdb5a.o"
+ return
+ }
+
+ # With the fix, the linker rejects the malformed .debug$S section
+ # with a "bad value" warning instead of entering an infinite loop.
+ # ld_link returns false when any warning is printed; we additionally
+ # check that the failure is specifically due to the PDB bad value,
+ # not an unrelated link error.
+ set result [ld_link $ld "tmpdir/pdb5.exe" "--pdb=tmpdir/pdb5.pdb -e foo tmpdir/pdb5a.o"]
+ if { !$result && [regexp "bad value" $exec_output] } {
+ pass "Reject malformed .debug\$S symbol record with overflowing length"
+ } else {
+ fail "Reject malformed .debug\$S symbol record with overflowing length"
+ }
+}
+
+# Check that parse_symbols handles a .debug$S section where a valid
+# symbol record is followed by trailing bytes that would cause the old
+# while (size >= sizeof(uint16_t)) loop to enter and read past the buffer.
+# With the fix (while (size >= sizeof(uint32_t))), the loop exits before
+# processing the trailing bytes.
+
+proc test13 { } {
+ global as
+ global ld
+ global srcdir
+ global subdir
+
+ if ![ld_assemble $as $srcdir/$subdir/pdb5b.s tmpdir/pdb5b.o] {
+ unsupported "Build pdb5b.o"
+ return
+ }
+
+ if ![ld_link $ld "tmpdir/pdb5b.exe" "--pdb=tmpdir/pdb5b.pdb -e main tmpdir/pdb5b.o"] {
+ fail "parse_symbols handles short symbol data"
+ } else {
+ pass "parse_symbols handles short symbol data"
+ }
+}
+
test1
test2
test3
@@ -1879,3 +1932,5 @@ test8
test9
test10
test11
+test12
+test13
diff --git a/ld/testsuite/ld-pe/pdb5a.s b/ld/testsuite/ld-pe/pdb5a.s
new file mode 100644
index 00000000000..aad1c81707f
--- /dev/null
+++ b/ld/testsuite/ld-pe/pdb5a.s
@@ -0,0 +1,30 @@
+/* Test source for malformed .debug$S symbol record. The symbol record's
+ length field is 0xFFFE, so that bfd_getl16() + sizeof(uint16_t) produces
+ 0x10000, which wraps to 0 when stored in a uint16_t. The linker should
+ reject this input rather than entering an infinite loop or reading out
+ of bounds. */
+
+ .equ CV_SIGNATURE_C13, 4
+ .equ DEBUG_S_SYMBOLS, 0xf1
+
+ .text
+
+ .global foo
+foo:
+ .secrel32 bar
+
+ .data
+
+ .global bar
+bar:
+ .long 0x9abcdef0
+
+ .section ".debug$S", "rn"
+ .long CV_SIGNATURE_C13
+ .long DEBUG_S_SYMBOLS
+ .long .syms_end - .syms_start
+
+.syms_start:
+ .short 0xFFFE
+ .short 0x0006
+.syms_end:
diff --git a/ld/testsuite/ld-pe/pdb5b.s b/ld/testsuite/ld-pe/pdb5b.s
new file mode 100644
index 00000000000..ea1853a525a
--- /dev/null
+++ b/ld/testsuite/ld-pe/pdb5b.s
@@ -0,0 +1,24 @@
+/* Test for parse_symbols: a valid S_LABEL32 record (6 bytes) followed
+ by 2 trailing bytes (0xFFFE). calculate_symbols_size exits its loop
+ (size=2 < sizeof(uint32_t)=4) and returns true, so parse_symbols is
+ called. In the unfixed code, parse_symbols uses
+ while (size >= sizeof(uint16_t)) which enters the loop with 2 bytes
+ remaining, then reads past the buffer (heap-buffer-overflow).
+ With the fix (while (size >= sizeof(uint32_t))), the loop exits
+ before processing the trailing bytes. */
+
+ .equ CV_SIGNATURE_C13, 4
+ .equ DEBUG_S_SYMBOLS, 0xf1
+ .equ S_LABEL32, 0x1105
+
+ .section ".debug$S", "rn"
+ .long CV_SIGNATURE_C13
+ .long DEBUG_S_SYMBOLS
+ .long .syms_end - .syms_start
+
+.syms_start:
+ .short 4
+ .short S_LABEL32
+ .short 0
+ .short 0xFFFE
+.syms_end:
--
2.43.0
More information about the Binutils
mailing list