[4/6] binutils: od-xcoff: fix traceback leak and .text lookup

Message ID 20261101105139.573422-5-harshit7kr@gmail.com
State New
Headers
Series binutils: od-xcoff: malformed input hardening |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Harshit Kumar Nov. 1, 2026, 10:51 a.m. UTC
  dump_xcoff32_traceback allocated the .text section buffer before looking
for the STYP_TEXT section header.  If no STYP_TEXT section existed, the
function returned early without freeing the buffer (AddressSanitizer /
LeakSanitizer: leak).  Check for the section before allocating.

The section lookup previously checked s_flags == STYP_TEXT, which
missed sections that combine STYP_TEXT with another flag bit, and took
the first matching section regardless of whether its name was .text.
The buffer was fetched by name from .text; if the two didn't match,
the dumper matched symbols against one section and dumped tags from
another.  Select the section by name and test s_flags with a bitmask,
returning silently if no section named .text carrying STYP_TEXT is
present (a section carrying STYP_TEXT whose name is not .text is now
silently ignored, matching the buffer lookup).  In
xcoff32_read_sections, the eight-byte on-disk name is copied into a
nine-byte struct field and the ninth byte is set to zero, so strcmp on
data->sects[i].name is always NUL-terminated.  Check
bfd_malloc_and_get_section and report failures with non_fatal.  The
symbol-walking loop has no early exits; record that invariant in a
comment so the single free (text) on the return path stays obvious.

Add a LeakSanitizer regression test using an object whose .text section
does not carry STYP_TEXT, plus tests for an extra STYP_TEXT flag bit,
section selection by name, and a zero-size .text section.  The leak test
uses the build's LSan configuration rather than overriding
ASAN_OPTIONS; it is discriminating only in an LSan-enabled build, and
other builds treat it as a functional control.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
 binutils/od-xcoff.c                           | 25 ++++++---
 .../binutils-all/xcoff-dump-bounds.exp        | 55 ++++++++++++++++++-
 2 files changed, 72 insertions(+), 8 deletions(-)
  

Patch

diff --git a/binutils/od-xcoff.c b/binutils/od-xcoff.c
index c8c5801b..7b52675e 100644
--- a/binutils/od-xcoff.c
+++ b/binutils/od-xcoff.c
@@ -1612,30 +1612,41 @@  dump_xcoff32_traceback (bfd *abfd, struct xcoff_dump *data)
   unsigned int text_vma;
   asection *text_sec;
   bfd_size_type text_size;
+  bfd_byte *text_data;
   char *text;
 
   if (data->syms == NULL || data->sects == NULL)
     return;
 
-  /* Read text section.  */
+  /* Locate the .text section and its section header.  The buffer and
+     the symbol section index must both refer to the same section.  */
   text_sec = bfd_get_section_by_name (abfd, ".text");
   if (text_sec == NULL)
     return;
-  text_vma = bfd_section_vma (text_sec);
-
-  text_size = bfd_section_size (text_sec);
-  text = (char *) xmalloc (text_size);
-  bfd_get_section_contents (abfd, text_sec, text, 0, text_size);
 
+  /* xcoff32_read_sections makes name a NUL-terminated copy of the
+     eight-byte XCOFF section name.  */
   for (i = 0; i < data->nscns; i++)
-    if (data->sects[i].flags == STYP_TEXT)
+    if (strcmp (data->sects[i].name, ".text") == 0)
       {
+	if ((data->sects[i].flags & STYP_TEXT) == 0)
+	  return;
         scnum_text = i + 1;
         break;
       }
   if (scnum_text == (unsigned int)-1)
     return;
 
+  text_vma = bfd_section_vma (text_sec);
+  text_size = bfd_section_size (text_sec);
+  if (!bfd_malloc_and_get_section (abfd, text_sec, &text_data))
+    {
+      non_fatal (_("cannot read .text section contents"));
+      return;
+    }
+  text = (char *) text_data;
+
+  /* No early exits below: every path from here reaches free (text).  */
   for (i = 0; i < data->nsyms; i++)
     {
       union xcoff32_symbol *s = &data->syms[i];
diff --git a/binutils/testsuite/binutils-all/xcoff-dump-bounds.exp b/binutils/testsuite/binutils-all/xcoff-dump-bounds.exp
index ff36cbf4..78158a29 100644
--- a/binutils/testsuite/binutils-all/xcoff-dump-bounds.exp
+++ b/binutils/testsuite/binutils-all/xcoff-dump-bounds.exp
@@ -535,6 +535,58 @@  xcoff_test "XCOFF dump loader import string overrun (-P loader)" \
 	$loader_overrun_strtab_obj "-P loader" \
 	[list {truncated import file list}]
 
+# The control .text lacks STYP_TEXT.  With LSan enabled, the old
+# early-return leak is reported; other builds treat this as a
+# functional control.  Use the build's LSan configuration rather than
+# overriding ASAN_OPTIONS.
+xcoff_test "XCOFF dump traceback STYP_TEXT early return memory leak" \
+	$ctrl_obj "-P traceback" \
+	{} \
+	[list {LeakSanitizer}]
+
+# s_flags = STYP_TEXT | STYP_DATA; the dumper must test the bit, not
+# compare the whole word.
+set tb_mask_obj [xcoff_make $ctrl_obj \
+	"xcoff-tb-mask.o" {{int32 56 96} {byte 104 2} {byte 105 0}}]
+
+xcoff_test "XCOFF dump traceback STYP_TEXT with extra flag (-P traceback)" \
+	$tb_mask_obj "-P traceback" \
+	[list \
+		{tags at 00000004} \
+		{end of tags at 0000000c}]
+
+# 172-byte object: .text has no STYP_TEXT, .data has it, and the
+# symbol points at .data.  The dumper must use .text for both the
+# buffer and the section index, so it prints no tags.
+set tb_sel_hex {
+    01df0002000000000000008400000002
+    000000002e7465787400000000000000
+    00000000000000100000006400000000
+    0000000000000000000000002e646174
+    61000000000000000000000000000010
+    00000074000000000000000000000000
+    00000020000000000000000000000000
+    00000000000000000000000000000000
+    000000002e66756e6300000000000000
+    00020020020100000000000000000000
+    020000000000000000000004
+}
+set tb_sel_obj [xcoff_hex "xcoff-tb-sel.o" $tb_sel_hex]
+
+xcoff_test "XCOFF dump traceback section selection by name (-P traceback)" \
+	$tb_sel_obj "-P traceback" \
+	{} \
+	[list {tags at}]
+
+# .text has STYP_TEXT but size 0; the scanner must report no tags and
+# free the (possibly NULL) buffer.
+set tb_zero_obj [xcoff_make $ctrl_obj \
+	"xcoff-tb-zero.o" {{int32 56 32} {byte 104 2} {byte 105 0} {int32 36 0}}]
+
+xcoff_test "XCOFF dump traceback zero-size .text (-P traceback)" \
+	$tb_zero_obj "-P traceback" \
+	[list {no tags found}]
+
 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 \
     $debug_off_obj $debug_exact_obj $aout_large_obj $aout_short_obj \
@@ -543,4 +595,5 @@  file delete -force $ctrl_obj $multi_obj $over2_obj $over40_obj $str_ovsz_obj \
     $loader_obj $loader_ovsym_obj $loader_ovrel_obj $loader_ovstoff_obj \
     $loader_empty_st_obj $loader_zero_imp_obj $loader_symoff_obj \
     $loader_unterm_obj $loader_trunc_imp_obj $loader_ovistlen_obj \
-    $loader_unterm_istlen_obj $loader_overrun_strtab_obj
+    $loader_unterm_istlen_obj $loader_overrun_strtab_obj \
+    $tb_mask_obj $tb_sel_obj $tb_zero_obj