xcoff32_read_symbols reads each symbol's auxiliary records into
&s[j + 1].aux, advancing by e_numaux entries. If a symbol claims more
auxiliary entries than remain in the table, the read writes an 18-byte
auxiliary record past the end of the allocated data->syms array
(AddressSanitizer: heap-buffer-overflow) before any dump pass.
objdump's standalone XCOFF dumper parses the format itself and therefore
bypassed the checks added to the linker entry points by commit
335d76afde5 ("bfd: validate COFF and XCOFF symbol tables before the
linker walks them").
Validate e_numaux against the remaining entries (nsyms - 1 - i). Also
bound the symbol table and the string table against the file size,
reject 32-bit pointer wrap, and terminate the allocated string table so
that malformed symbol names cannot read past it. Discard the table on a
bad count rather than truncating it, so the dump does not present a
partial symbol table. Reset the parser state on all failure paths so
later dump passes see an empty symbol table.
File-size checks are conditional because BFD reports a zero size for
non-seekable input; the overflow checks still run in that case, where
a huge declared size terminates cleanly with a memory-exhausted error
without out-of-bounds access. Guard string table lookups with
data->strings != NULL so a strings_size between 1 and 4 does not compute
NULL + soff.
Add tests for the overrun, the table and string table bounds, pointer
wrap, and a non-NUL-terminated name at the end of the string table.
The fixtures are valid XCOFF objects except for the field under test.
Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
binutils/od-xcoff.c | 79 ++++-
.../binutils-all/xcoff-dump-bounds.exp | 269 ++++++++++++++++++
2 files changed, 340 insertions(+), 8 deletions(-)
create mode 100644 binutils/testsuite/binutils-all/xcoff-dump-bounds.exp
@@ -636,15 +636,49 @@ xcoff32_read_symbols (bfd *abfd, struct xcoff_dump *data)
char stsz_arr[4];
unsigned int stptr;
+ ufile_ptr filesize;
+ size_t symtab_bytes;
+ size_t sym_alloc_bytes;
+
if (data->nsyms == 0)
return;
- stptr = data->symptr
- + data->nsyms * (unsigned)sizeof (struct external_syment);
+ filesize = bfd_get_file_size (abfd);
+ /* filesize is zero when BFD cannot determine the size, for example
+ for a non-seekable stream. Apply file-size bounds only when the
+ size is known; the overflow checks below still apply. */
+ if (_bfd_mul_overflow (data->nsyms,
+ sizeof (struct external_syment), &symtab_bytes)
+ || symtab_bytes > UINT_MAX - data->symptr)
+ {
+ non_fatal (_("symbol table size causes integer overflow"));
+ goto clean;
+ }
+
+ if (filesize > 0
+ && ((ufile_ptr) data->symptr > filesize
+ || symtab_bytes > filesize - data->symptr))
+ {
+ non_fatal (_("symbol table extends beyond end of file"));
+ goto clean;
+ }
+
+ if (_bfd_mul_overflow (data->nsyms,
+ sizeof (union xcoff32_symbol), &sym_alloc_bytes))
+ {
+ non_fatal (_("symbol table size causes integer overflow"));
+ goto clean;
+ }
+
+ stptr = data->symptr + symtab_bytes;
/* Read string table. */
- if (bfd_seek (abfd, stptr, SEEK_SET) != 0
- || bfd_read (&stsz_arr, sizeof (stsz_arr), abfd) != sizeof (stsz_arr))
+ if ((filesize > 0
+ && ((ufile_ptr) stptr > filesize
+ || filesize - stptr < sizeof (stsz_arr)))
+ || bfd_seek (abfd, stptr, SEEK_SET) != 0
+ || bfd_read (&stsz_arr, sizeof (stsz_arr), abfd)
+ != sizeof (stsz_arr))
{
non_fatal (_("cannot read strings table length"));
data->strings_size = 0;
@@ -652,11 +686,23 @@ xcoff32_read_symbols (bfd *abfd, struct xcoff_dump *data)
else
{
data->strings_size = bfd_h_get_32 (abfd, stsz_arr);
+ if (filesize > 0 && data->strings_size > filesize - stptr)
+ {
+ non_fatal (_("strings table extends beyond end of file"));
+ goto clean;
+ }
if (data->strings_size > sizeof (stsz_arr))
{
unsigned int remsz = data->strings_size - sizeof (stsz_arr);
+ size_t strings_alloc = (size_t) data->strings_size + 1;
- data->strings = xmalloc (data->strings_size);
+ if (strings_alloc <= data->strings_size)
+ {
+ non_fatal (_("strings table size causes integer overflow"));
+ goto clean;
+ }
+
+ data->strings = xmalloc (strings_alloc);
memcpy (data->strings, stsz_arr, sizeof (stsz_arr));
if (bfd_read (data->strings + sizeof (stsz_arr), remsz, abfd)
@@ -665,6 +711,10 @@ xcoff32_read_symbols (bfd *abfd, struct xcoff_dump *data)
non_fatal (_("cannot read strings table"));
goto clean;
}
+
+ /* Terminate the string table so that malformed symbol names
+ cannot read past the allocated buffer. */
+ data->strings[data->strings_size] = 0;
}
}
@@ -674,8 +724,7 @@ xcoff32_read_symbols (bfd *abfd, struct xcoff_dump *data)
goto clean;
}
- data->syms = (union xcoff32_symbol *)
- xmalloc (data->nsyms * sizeof (union xcoff32_symbol));
+ data->syms = (union xcoff32_symbol *) xmalloc (sym_alloc_bytes);
for (i = 0; i < data->nsyms; i++)
{
@@ -705,7 +754,9 @@ xcoff32_read_symbols (bfd *abfd, struct xcoff_dump *data)
{
unsigned int soff = bfd_h_get_32 (abfd, sym.e.e.e_offset);
- if ((s->sym.sclass & DBXMASK) == 0 && soff < data->strings_size)
+ if ((s->sym.sclass & DBXMASK) == 0
+ && data->strings != NULL
+ && soff < data->strings_size)
s->sym.name = data->strings + soff;
else
{
@@ -714,6 +765,16 @@ xcoff32_read_symbols (bfd *abfd, struct xcoff_dump *data)
}
}
+ if (s->sym.numaux > data->nsyms - 1 - i)
+ {
+ non_fatal (_("corrupt symbol table: symbol %u claims %u"
+ " aux entries, but only %u remain"),
+ i,
+ (unsigned int) s->sym.numaux,
+ data->nsyms - 1 - i);
+ goto clean;
+ }
+
for (j = 0; j < s->sym.numaux; j++, i++)
{
if (bfd_read (&s[j + 1].aux,
@@ -727,8 +788,10 @@ xcoff32_read_symbols (bfd *abfd, struct xcoff_dump *data)
}
return;
clean:
+ data->nsyms = 0;
free (data->syms);
data->syms = NULL;
+ data->strings_size = 0;
free (data->strings);
data->strings = NULL;
}
new file mode 100644
@@ -0,0 +1,269 @@
+# Expect script for XCOFF dump bounds checks.
+#
+# Copyright (C) 2026 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; see the file COPYING3. If not,
+# see <http://www.gnu.org/licenses/>.
+
+if { [is_remote host] } {
+ return
+}
+
+# Only run on targets where objdump supports aixcoff-rs6000.
+set has_aixcoff 0
+set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS -i"]
+if { [regexp "aixcoff-rs6000" $got] } {
+ set has_aixcoff 1
+}
+
+if { !$has_aixcoff } {
+ unsupported "objdump does not support aixcoff-rs6000"
+ return
+}
+
+set tmpdir tmpdir
+
+proc xcoff_write_hex { hex file } {
+ regsub -all {\s+} $hex "" clean_hex
+ set f [open $file "wb"]
+ fconfigure $f -translation binary
+ puts -nonewline $f [binary format H* $clean_hex]
+ close $f
+}
+
+proc xcoff_mutate_byte { src dst off val } {
+ if { $src ne $dst } {
+ file copy -force $src $dst
+ }
+ set f [open $dst "r+b"]
+ fconfigure $f -translation binary
+ seek $f $off
+ puts -nonewline $f [binary format "c" $val]
+ close $f
+}
+
+# Tcl's "I" format is big-endian, matching XCOFF.
+proc xcoff_mutate_int32 { src dst off val } {
+ if { $src ne $dst } {
+ file copy -force $src $dst
+ }
+ set f [open $dst "r+b"]
+ fconfigure $f -translation binary
+ seek $f $off
+ puts -nonewline $f [binary format "I" $val]
+ close $f
+}
+
+proc xcoff_crashed { out } {
+ foreach pat { "*AddressSanitizer*" "*LeakSanitizer*"
+ "*Segmentation fault*" "*core dumped*" "*SIGABRT*"
+ "*SIGSEGV*" "*free(): invalid size*" "*malloc(): *"
+ "*corrupted size vs. prev_size*" "*assertion fail*"
+ "*Aborted*" } {
+ if { [string match $pat $out] } {
+ return 1
+ }
+ }
+ return 0
+}
+
+proc xcoff_fail { testname status out } {
+ send_log "$testname (status $status)\n$out\n"
+ fail $testname
+}
+
+# Generic test runner. TESTNAME is the test label, OBJ the fixture, OPT
+# the -P option, REQUIRE_RES and FORBID_RES are lists of regexps, and
+# WANT_FAIL is 0 for success, 1 for failure, or -1 for either status.
+proc xcoff_test { testname obj opt require_res {forbid_res {}} {want_fail 0} } {
+ global OBJDUMP OBJDUMPFLAGS binutils_run_status
+ set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS $opt $obj"]
+ set got [string map {"\r" ""} $got]
+ if { [xcoff_crashed $got] } {
+ xcoff_fail $testname $binutils_run_status $got
+ return
+ }
+ if { $want_fail != -1 } {
+ set status_ok [expr { $want_fail ? ($binutils_run_status != 0)
+ : ($binutils_run_status == 0) }]
+ if { !$status_ok } {
+ xcoff_fail $testname $binutils_run_status $got
+ return
+ }
+ }
+ foreach r $require_res {
+ if { ![regexp $r $got] } {
+ xcoff_fail $testname $binutils_run_status $got
+ return
+ }
+ }
+ foreach f $forbid_res {
+ if { [regexp $f $got] } {
+ xcoff_fail $testname $binutils_run_status $got
+ return
+ }
+ }
+ pass $testname
+}
+
+# Copy SRC to NAME and apply byte, int32 or fill mutations.
+proc xcoff_make { src name mutations } {
+ global tmpdir
+ set dst "$tmpdir/$name"
+ file copy -force $src $dst
+ foreach m $mutations {
+ set op [lindex $m 0]
+ set off [lindex $m 1]
+ set val [lindex $m 2]
+ switch -- $op {
+ byte { xcoff_mutate_byte $dst $dst $off $val }
+ int32 { xcoff_mutate_int32 $dst $dst $off $val }
+ fill {
+ set end [lindex $m 2]
+ set val [lindex $m 3]
+ set f [open $dst "r+b"]
+ fconfigure $f -translation binary
+ seek $f $off
+ for {set i $off} {$i < $end} {incr i} {
+ puts -nonewline $f [binary format "c" $val]
+ }
+ close $f
+ }
+ default { error "unknown mutation $op" }
+ }
+ }
+ return $dst
+}
+
+# Write HEX to NAME and return the path.
+proc xcoff_hex { name hex } {
+ global tmpdir
+ set file "$tmpdir/$name"
+ xcoff_write_hex $hex $file
+ return $file
+}
+
+# 120-byte XCOFF32 seed: header 20, .text header 40, .text data 16,
+# symbol table 36 (one C_EXT plus aux), string-table area 8 (4-byte
+# length word plus 4 bytes slack).
+set xcoff32_control_hex {
+ 01df0001000000000000004c00000002000000002e746578740000000000000000000000
+ 000000100000003c00000000000000000000000000000000000000000000000000000000
+ 000000002e66756e63000000000000000001002002010000000000000000000000000000
+ 000000000000000400000000
+}
+
+set ctrl_obj [xcoff_hex "xcoff-ctrl.o" $xcoff32_control_hex]
+
+# Positive control test: unmutated XCOFF32 object dumps cleanly.
+xcoff_test "XCOFF dump control (-P syms)" $ctrl_obj "-P syms" \
+ [list {EXT[^\n]*\.func}]
+
+# 186-byte XCOFF32 object with five symbol entries: .text + aux,
+# .func + aux, and foo.
+set xcoff32_multi_hex {
+ 01df0001000000000000004c00000005000000002e746578740000000000000000000000
+ 000000100000003c00000000000000000000000000000000000000000000000000000000
+ 000000002e74657874000000000000000001000003010000001000000000000000000000
+ 000000002e66756e63000000000000000001002002010000001000000000000002000000
+ 00000000666f6f0000000000000000000001002002000000001462617200000000000000
+ 000000000000
+}
+
+set multi_obj [xcoff_hex "xcoff-multi.o" $xcoff32_multi_hex]
+
+xcoff_test "XCOFF dump control multi-symbol (-P syms)" $multi_obj "-P syms" \
+ [list \
+ {EXT[^\n]*\.func} \
+ {EXT[^\n]*foo}]
+
+# Symbol table negative test 1: Boundary overrun (symbol 0 e_numaux = 2).
+# Declares 2 symbols, symbol 0 claims 2 aux entries -> 1 + 2 = 3 entries
+# needed.
+set over2_obj [xcoff_make $ctrl_obj "xcoff-over-2.o" {{byte 93 2}}]
+
+xcoff_test "XCOFF dump overrun e_numaux=2 (-P syms)" $over2_obj "-P syms" \
+ [list {symbol 0 claims 2 aux entries, but only 1 remain}]
+
+# Symbol table negative test 2: Inflated overrun (symbol 0 e_numaux = 40).
+set over40_obj [xcoff_make $ctrl_obj "xcoff-over-40.o" {{byte 93 40}}]
+
+xcoff_test "XCOFF dump overrun e_numaux=40 (-P syms)" $over40_obj "-P syms" \
+ [list {symbol 0 claims 40 aux entries, but only 1 remain}]
+
+# Symbol table negative test 3: String table length beyond EOF.
+# The control object's string table is 8 bytes at offset 112. Declare a
+# 4 GiB string table without extending the file.
+set str_ovsz_obj [xcoff_make $ctrl_obj \
+ "xcoff-str-ovsz.o" {{int32 112 4294967295}}]
+
+xcoff_test "XCOFF dump string table extends past EOF (-P syms)" \
+ $str_ovsz_obj "-P syms" \
+ [list {strings table extends beyond end of file}]
+
+# f_symptr = 200 with a full 72-byte auxiliary header; BFD can still
+# identify the format, but the symbol table starts past EOF.
+set symptr_eof_hex {
+ 01df000100000000000000c8000000020048000000000000000000000000000000000000
+ 000000000000000000000000000000000000000000000000000000000000000000000000
+ 00000000000000000000000000000000000000002e746578740000000000000000000000
+ 000000100000008400000000000000000000000000000020000000000000000000000000
+ 00000000
+}
+set symptr_eof_obj [xcoff_hex "xcoff-symptr-eof.o" $symptr_eof_hex]
+
+xcoff_test "XCOFF dump symbol table offset beyond EOF (-P syms)" \
+ $symptr_eof_obj "-P syms" \
+ [list {symbol table extends beyond end of file}]
+
+# Symbol table negative test 4: Symbol table extends beyond EOF.
+# Declares f_nsyms = 20 (offset 12..15 = 0x00000014).
+# 20 symbols * 18 bytes = 360 bytes, extending past file size (120 bytes).
+set over_eof_obj [xcoff_make $ctrl_obj "xcoff-over-eof.o" {{byte 15 20}}]
+
+xcoff_test "XCOFF dump symbol table extending past EOF (-P syms)" \
+ $over_eof_obj "-P syms" \
+ [list {symbol table extends beyond end of file}]
+
+# f_symptr = 0xFFFFFF00, f_nsyms = 20: 20 * 18 bytes wraps 32 bits.
+set symptr_wrap_obj [xcoff_make $symptr_eof_obj \
+ "xcoff-symptr-wrap.o" {
+ {byte 8 255} {byte 9 255} {byte 10 255} {byte 11 0} {byte 15 20}
+ }]
+
+xcoff_test "XCOFF dump symbol table pointer wrap (-P syms)" \
+ $symptr_wrap_obj "-P syms" \
+ [list {symbol table size causes integer overflow}]
+
+# 136-byte object whose 24-byte string table ends in non-NUL 'X';
+# the symbol name points at the last byte, so the dumper must append NUL.
+set str_unterm_hex {
+ 01df0001000000000000004c0000000200000000
+ 2e746578740000000000000000000000000000100000003c0000000000000000000000000000
+ 0000
+ 00000000000000000000000000000000
+ 000000000000001700000000000100200201
+ 000000000000000000000000000000000000
+ 000000180000000000000000000000000000000000000058
+}
+set str_unterm_obj [xcoff_hex "xcoff-str-unterm.o" $str_unterm_hex]
+
+xcoff_test "XCOFF dump symbol name at end of string table (-P syms)" \
+ $str_unterm_obj "-P syms" \
+ [list {EXT[^\n]* X\n}]
+
+file delete -force $ctrl_obj $multi_obj $over2_obj $over40_obj $str_ovsz_obj \
+ $symptr_eof_obj $over_eof_obj $symptr_wrap_obj $str_unterm_obj