[binutils-gdb] bfd: elfxx-x86: reject non-x86 target ids in elf_x86_hash_table

Jan Beulich jbeulich@sourceware.org
Fri Nov 6 13:15:52 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3f5a0261d23ca400aaa8f0c27b061b6a4d9bf35f

commit 3f5a0261d23ca400aaa8f0c27b061b6a4d9bf35f
Author: Harshit Kumar <harshit7kr@gmail.com>
Date:   Fri Nov 6 14:13:17 2026 +0100

    bfd: elfxx-x86: reject non-x86 target ids in elf_x86_hash_table
    
    PR ld/34874
    
    A linker script can retarget the output BFD, for example with
    OUTPUT_FORMAT("elf64-bpfle").  The x86 emulation still calls
    bfd_elf_linker_x86_set_options, which takes the target id from the
    output BFD's backend data.  The id and the link hash table then both
    belong to the foreign backend, so the id comparison in
    elf_x86_hash_table passes and struct elf_x86_link_hash_table is the
    wrong layout of that table.  The write to htab->params then overflows
    the heap block (AddressSanitizer, write of size 8, 424 bytes after an
    1848-byte region).
    
    Check the id after is_elf_hash_table, and accept only the x86 ids.
    is_elf_hash_table must stay the first test in the macro: for a
    non-ELF output format the hash table is not an ELF one, so it
    short-circuits and bed->target_id is never read.  A format such as
    "binary" has no ELF backend data at all, so reading the id first
    segfaults.  This order keeps both properties: non-ELF outputs never
    reach the read, and foreign ELF ids are rejected.
    
    The other elf_x86_hash_table callers pass X86_64_ELF_DATA,
    I386_ELF_DATA, or bed->target_id of an x86 output, so they are
    unaffected.  No change to bfd_elf_linker_x86_set_options is needed.
    
    Add ld-x86-64/arch-confusion.exp and its input arch-confusion.s.  The
    foreign ELF case reports the overflow only under AddressSanitizer.
    
    Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>

Diff:
---
 bfd/elfxx-x86.h                           |  12 +++
 ld/testsuite/ld-x86-64/arch-confusion.exp | 152 ++++++++++++++++++++++++++++++
 ld/testsuite/ld-x86-64/arch-confusion.s   |   4 +
 3 files changed, 168 insertions(+)

diff --git a/bfd/elfxx-x86.h b/bfd/elfxx-x86.h
index 06b65fcafc3..afa0de1f431 100644
--- a/bfd/elfxx-x86.h
+++ b/bfd/elfxx-x86.h
@@ -108,8 +108,20 @@
    shared lib.  */
 #define ELIMINATE_COPY_RELOCS 1
 
+/* Return true if ID names an x86 ELF backend, so that struct
+   elf_x86_link_hash_table is the correct layout of the link hash table.
+   ID is the target id of the output backend, which a linker script can
+   retarget with OUTPUT_FORMAT, so it need not name an x86 backend.  */
+
+static inline bool
+elf_x86_target_id_p (enum elf_target_id id)
+{
+  return id == X86_64_ELF_DATA || id == I386_ELF_DATA;
+}
+
 #define elf_x86_hash_table(p, id) \
   (is_elf_hash_table ((p)->hash) \
+   && elf_x86_target_id_p (id) \
    && elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) == (id) \
     ? ((struct elf_x86_link_hash_table *) ((p)->hash)) : NULL)
 
diff --git a/ld/testsuite/ld-x86-64/arch-confusion.exp b/ld/testsuite/ld-x86-64/arch-confusion.exp
new file mode 100644
index 00000000000..7238044ce02
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/arch-confusion.exp
@@ -0,0 +1,152 @@
+# Expect script for x86 ELF output target confusion tests.
+#
+# 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; if not, see <http://www.gnu.org/licenses/>.
+
+# A linker script can retarget the output BFD away from x86 with
+# OUTPUT_FORMAT.  The x86 emulation still calls
+# bfd_elf_linker_x86_set_options.  That function must not mistake the
+# foreign link hash table for a struct elf_x86_link_hash_table.
+
+# The linker scripts below are written to the local tmpdir, which a
+# remote host cannot see.
+if { [is_remote host] } {
+    return
+}
+
+set test "x86-64 output target confusion"
+
+# A different target has neither this test's assembler nor its emulation,
+# so skip silently there, as the rest of ld-x86-64 does.  Once the target
+# matches, both remaining preconditions record a result, so a missing
+# emulation or a failed assembly cannot go unreported.
+if { !([istarget "x86_64-*-elf*"] || [istarget "x86_64-*-linux*"]) } {
+    return
+}
+
+if { ![ld_supports_emul "elf_x86_64"] } {
+    unsupported "$test (elf_x86_64 emulation not supported)"
+    return
+}
+
+# Write a minimal linker script that selects an output format.  ARCH may
+# be empty, in which case the format's own default is used.
+proc arch_confusion_script { path format arch } {
+    set fp [open $path w]
+    puts $fp "OUTPUT_FORMAT(\"$format\")"
+    if { $arch ne "" } {
+	puts $fp "OUTPUT_ARCH($arch)"
+    }
+    puts $fp "SECTIONS"
+    puts $fp "{"
+    puts $fp "  .text : { *(.text) }"
+    puts $fp "}"
+    close $fp
+}
+
+# Link OBJ with -melf_x86_64 and the linker script at SCRIPT, and return
+# the linker diagnostics.  binutils' run_host_cmd returns the output
+# text only, so the exit status is not available here.
+proc arch_confusion_link { script obj out } {
+    global ld
+    catch { file delete -force $out }
+    return [run_host_cmd "$ld" "-melf_x86_64 -T $script -o $out $obj"]
+}
+
+# Foreign ELF output formats to try, as {format architecture}.  Both
+# names have to be valid for the BFD backend, or ld rejects the script
+# before the x86 emulation hook runs and nothing is tested.  A name this
+# build does not know is recognised from the diagnostic and the next
+# candidate is tried.  Each format has an ELF link hash table, and the
+# unpatched tree overflows under AddressSanitizer for each of them.
+set arch_confusion_elf_targets {
+    {elf64-bpfle          bpf}
+    {elf32-littlearc      arc}
+    {elf64-littleriscv    riscv}
+    {elf32-xtensa-le      xtensa}
+    {elf32-microblaze     microblaze}
+    {elf64-tilegx-le      tilegx}
+    {elf32-or1k           or1k}
+    {elf32-bfin           bfin}
+}
+
+if { ![ld_assemble $as "--64 $srcdir/$subdir/arch-confusion.s" \
+	 tmpdir/arch-confusion.o] } {
+    unsupported "$test (could not assemble the input)"
+    return
+}
+
+# A non-ELF output format has no ELF backend data.  The x86 hook must
+# decline to touch it.  Such a link is unaffected and must succeed.
+#
+# This block does not reproduce the original overflow.  What it pins is
+# the ORDER of the tests in elf_x86_hash_table.  is_elf_hash_table must
+# stay first: it short circuits for a non-ELF output, so bed->target_id
+# is never read and the link has no ELF backend data to read from.  Move
+# the id test in front of it and this link segfaults.
+arch_confusion_script tmpdir/arch-confusion-binary.t "binary" ""
+if { [ld_link $ld tmpdir/arch-confusion-binary.out \
+	 "-melf_x86_64 -T tmpdir/arch-confusion-binary.t \
+	  tmpdir/arch-confusion.o"] } {
+    pass "$test (non-ELF output format)"
+} else {
+    fail "$test (link to the binary output format should have succeeded)"
+}
+
+# A foreign ELF output format still gets an ELF link hash table, just
+# not an x86 one.  The link is expected to fail, but only because the
+# input object does not match the foreign architecture.  That diagnostic
+# is printed after the output file has been opened and the x86 hook has
+# run, so requiring it proves the hook returned cleanly.  Any other
+# result, in particular a sanitizer report from the out of bounds store,
+# is a failure.  A candidate this build lacks is logged and skipped.  The
+# test is unsupported only when no candidate is available.
+set found 0
+foreach target $arch_confusion_elf_targets {
+    set format [lindex $target 0]
+    set arch [lindex $target 1]
+
+    arch_confusion_script tmpdir/arch-confusion-elf.t $format $arch
+    set output [arch_confusion_link tmpdir/arch-confusion-elf.t \
+		    tmpdir/arch-confusion.o tmpdir/arch-confusion-elf.out]
+
+    if { [regexp "target $format not found" $output]
+	 || [regexp "cannot represent machine" $output] } {
+	verbose -log "no $format target in this build, skipping it"
+	continue
+    }
+
+    set found 1
+
+    if { [regexp "architecture of input file .* is incompatible with" \
+		 $output] } {
+	pass "$test (foreign ELF output format $format)"
+    } else {
+	fail "$test (foreign ELF output format $format: $output)"
+    }
+    break
+}
+
+if { !$found } {
+    unsupported "$test (foreign ELF output format)"
+}
+
+catch { file delete -force tmpdir/arch-confusion-binary.t }
+catch { file delete -force tmpdir/arch-confusion-binary.out }
+catch { file delete -force tmpdir/arch-confusion-elf.t }
+catch { file delete -force tmpdir/arch-confusion-elf.out }
+catch { file delete -force tmpdir/arch-confusion.o }
diff --git a/ld/testsuite/ld-x86-64/arch-confusion.s b/ld/testsuite/ld-x86-64/arch-confusion.s
new file mode 100644
index 00000000000..69cf1a4449b
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/arch-confusion.s
@@ -0,0 +1,4 @@
+.globl _start
+_start:
+	nop
+	.section	.note.GNU-stack,"",@progbits


More information about the Binutils-cvs mailing list