https://sourceware.org/bugzilla/show_bug.cgi?id=35080

            Bug ID: 35080
           Summary: Binutils ld: heap out-of-bounds write in
                    _bfd_elf_append_rela when the entry symbol is an
                    absolute symbol from a shared object
           Product: binutils
           Version: 2.48 (HEAD)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: ld
          Assignee: unassigned at sourceware dot org
          Reporter: emily.west286 at gmail dot com
  Target Milestone: ---

Created attachment 17304
  --> https://sourceware.org/bugzilla/attachment.cgi?id=17304&action=edit
Non-minimized reproduction test cases

Binutils ld: heap out-of-bounds write in _bfd_elf_append_rela when the entry
symbol is an absolute symbol from a shared object

Component: ld
Version: binutils-gdb master, reproduced on commits
c79376a262328164cdf81eddfcc5518314b1856a and
62231047e2d7dc828263bd23c191f335ab884969 (2026-12-01)
Target: x86_64-pc-linux-gnu

Summary:
ld -pie writes relocations past the end of .rela.text when an input shared
object defines the entry symbol as an absolute symbol with a weak alias, and a
regular object refers to the alias with PC-relative relocations. Space for the
relocations is counted on the alias, moved to the entry symbol, and then
discarded because the entry symbol is wrongly marked as defined in a regular
object. The relocations are still emitted for the alias.

==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x5210000290e7
WRITE of size 1 at 0x5210000290e7 thread T0
0x5210000290e7 is located 7 bytes after 4064-byte region
[0x521000028100,0x5210000290e0)
SUMMARY: AddressSanitizer: heap-buffer-overflow bfd/libbfd.c:989 in bfd_putl64

Reproducer:
Four inputs are attached (inputs/0012, 0013, 0015, 0016), each with two files.
For 0012:

part0.o: 7408 bytes, ET_DYN, SHA-256
44fbdc0e420be2c5506fb6095065b8f38bc865f4e68e6ec83608ed8e0641a81e
part1.o: 2448 bytes, ET_REL, SHA-256
2fd89d982178fc78f5e63dbe615873ec852ea8c943cf2c7c4ae334b0a909296f

ld -pie -o /tmp/out 0012/part0.o 0012/part1.o

It also reproduces with -pie --noinhibit-exec, which run-all.sh uses. It does
not reproduce without -pie or with -shared.

part0.o defines both symbols at the same value:

   Num:    Value          Size Type    Bind   Vis       Ndx Name
     3: 0000000000000000     5 TLS     GLOBAL PROTECTED ABS _start
     4: 0000000000000000     0 SECTION WEAK   DEFAULT   ABS f

part1.o has R_X86_64_PC16 relocations against f in .text.

With the attached Dockerfile (gcc 14.2.0 environment with optimization -O1,
ASan and UBSan):

docker build -t ld-repro-cl2 .
docker run --rm ld-repro-cl2 0012

All four inputs also reproduce on 62231047e2d7dc828263bd23c191f335ab884969 with
-pie --noinhibit-exec. 

docker build -t ld-repro-cl2-master --build-arg
BINUTILS_COMMIT=62231047e2d7dc828263bd23c191f335ab884969 .

Backtrace:
#0 bfd_putl64                  bfd/libbfd.c:989
#1 bfd_elf64_swap_reloca_out   bfd/elfcode.h:468
#2 _bfd_elf_append_rela        bfd/elflink.c:15682
#3 elf_x86_64_relocate_section bfd/elf64-x86-64.c:4152
#4 elf_link_input_bfd          bfd/elflink.c:11978
#5 _bfd_elf_final_link         bfd/elflink.c:13241
#6 ldwrite                     ld/ldwrite.c:548
#7 main                        ld/ldmain.c:1001

The region in the ASan report is an objalloc chunk from bfd_zalloc. .rela.text
contents are inside it.

Root cause:
In _bfd_elf_fix_symbol_flags, the non_elf branch sets def_regular when the
symbol's section has no owner (line 3192):

c
          if (h->root.u.def.section->owner != NULL
              && (bfd_get_flavour (h->root.u.def.section->owner)
                  == bfd_target_elf_flavour))
            {
              h->ref_regular = 1;
              h->ref_regular_nonweak = 1;
            }
          else
            h->def_regular = 1;

The absolute section has no owner, so an absolute symbol defined only by a
shared object gets def_regular. The branch for symbols that are not non_elf
does check !h->def_dynamic for absolute symbols.

Sequence for 0012:
1. lang_place_undefineds creates _start through insert_undefined
(ldlang.c:4274) before any input is loaded, so it has non_elf = 1.
2. elf_x86_64_scan_relocs counts 32 PC-relative relocations on f
(elf64-x86-64.c:3043).
3. _bfd_elf_fix_symbol_flags (f) sees that f is a weak alias of _start, which
is not yet def_regular, and moves the count to _start (elflink.c:3315).
4. _bfd_elf_fix_symbol_flags (_start) sets def_regular (line 3192).
5. elf_x86_allocate_dynrelocs treats _start as local and discards its 32
PC-relative relocations (elfxx-x86.c:408). Only one slot, for the undefined
symbol tvar, is reserved (line 553).
6. elf_x86_64_relocate_section evaluates f, which is not local, and emits the
relocations. _bfd_elf_append_rela checks the bounds with BFD_ASSERT, which does
not stop the write.

gdb, 0012:
At _bfd_elf_link_hash_copy_indirect: dir = "_start", ind = "f",
ind->dyn_relocs->count = 32, pc_count = 32
Watchpoint on the byte holding def_regular of _start: 25 -> 27 at
elflink.c:3192, with
non_elf = 1, section name = "*ABS*", section owner = 0x0
At elfxx-x86.c:408, h = _start: count = 32, pc_count = 32; after the discard:
count = 0, pc_count = 0
At elfxx-x86.c:553: "tvar", count = 1
At elf64-x86-64.c:4152: h = "f", h->dyn_relocs = 0x0, sreloc->size = 24
At the abort: .rela.text size = 24, reloc_count = 7

The move, the discard and the emission were observed the same way on 0013, 0015
and 0016.

Related crash:
AddressSanitizer: SEGV on unknown address 0x000000000044 (READ)
#0 _bfd_x86_elf_adjust_dynamic_symbol bfd/elfxx-x86.c:3639

SYMBOL_NO_COPYRELOC reads section->owner->flags without checking owner, which
is NULL for the absolute section. All four inputs crash with -e other and with
-e 0 (on 62231047e2, -e other was run and crashes the same way, at
elfxx-x86.c:3642). Fixing only elflink.c exposes this crash on the original
command line, as emulated in gdb on all four inputs, so the patch fixes both.

Impact:
Out-of-bounds heap write in ld when it links a crafted shared object together
with a crafted object file. The data written is Elf64_Rela records, whose
r_addend comes from the input. Every record after the first is outside
.rela.text; the first few land in the same objalloc chunk, so ASan does not
report them. Behavior without sanitizers was not tested, and exploitability was
not assessed. Other ld versions were not tested.

Suggested fix:
abs-entry-def-regular.patch makes two changes.

1. bfd/elflink.c: in the non_elf branch, treat an absolute symbol that is
defined by a dynamic object, and not by a regular one, as a reference.
2. bfd/elfxx-x86.h: check owner for NULL in SYMBOL_NO_COPYRELOC.

Fix verification:
With the patch on c79376a262, none of the four inputs gives an ASan report or
an assertion failure in the seven link modes tested. On 62231047e2, the same
holds for the two modes tested there: -pie --noinhibit-exec, with and without
-e other. UBSan still reports a null pointer offset in
elf_link_add_object_symbols (elflink.c:4802 unpatched, 4807 patched); it also
occurs without the patch and is not addressed by it.

In both modes on 62231047e2, ld exits with status 1. For 0012 it reports:

/src/build/ld/ld-new: copy reloc against protected `_start' is dangerous
/src/build/ld/ld-new: 0012/part1.o: copy relocation against non-copyable
protected symbol `_start' in linker stubs

On c79376a262, the ld testsuite (make check-ld, no sanitizers, default x86_64
target only) has identical results before and after the patch: 3284 expected
passes, 59 expected failures, 3 untested, 58 unsupported, no unexpected
failures. i386 also uses the changed macro; its ELF target was not built
separately. No new testcase is included.

patch.Dockerfile builds ld with the patch applied.

docker build -f patch.Dockerfile -t ld-fix-cl2 .
docker run --rm ld-fix-cl2

Related reports:
These reach the same or nearby code from different causes. Their fixes are all
in c79376a262, where this bug reproduces.

Bug 33457: inputs with a section extending past the end of the file, fixed by
9ca499644a. A bfd_putl64 overflow reported against 2.45, reached by writing GOT
relocations, was closed as a duplicate of it.
Bug 32638 (CVE-2025-1178): write near NULL in elf_append_rela when the GOT
relocation section is missing or empty, fixed by 75086e9de1. Here .rela.text
exists but is too small.
Bug 34444: section contents accessed at an invalid offset for malformed TLS,
GOT and PLT relocations in elf_x86_64_relocate_section, fixed by 471130b39c.
Here the overflow is in the output dynamic relocations.

Attachments:
part0.o and part1.o              reproducers
run-all.sh                       links every input
asan-report.txt                  ASan output for all four inputs, c79376a262
Dockerfile                       builds ld with ASan and UBSan, at c79376a262
by default
abs-entry-def-regular.patch      suggested fix
patch.Dockerfile                 builds ld with the patch applied

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to