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

            Bug ID: 35086
           Summary: GNU ld indexes a NULL AArch64 local GOT-offset array
           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 17312
  --> https://sourceware.org/bugzilla/attachment.cgi?id=17312&action=edit
Reproducer materials

Product: binutils
Component: ld
Version: current master (716b214ad8db)
Host/target: Use an all-target GNU ld build, or a target build supporting the
emulation exercised by the attachment.

Summary
-------

After elf64_aarch64_check_relocs rejects an AArch64 input partway through
(here an R_AARCH64_ABS32 that cannot be used in a shared object), ld still
runs the final link. elf64_aarch64_relocate_section then handles a TLS-IE
relocation against a local symbol that check_relocs never scanned, so
elf_aarch64_locals (input_bfd) is NULL and symbol_got_offset_ref indexes it at
bfd/elfnn-aarch64.c:5443. A verified
suggested fix is attached.

Reproduction
------------

1. Download and extract
`bfd-aarch64-local-got-offsets-null-pointer-arithmetic-reproducer.tar.gz`.
2. Run the following command with an ld.bfd or all-target `ld-new` binary that
   supports the emulation used by the reproducer:

       LD_BFD=/path/to/ld-new sh repro.sh

The archive contains the fixed input files, independent controls, helper
scripts needed by `repro.sh`, and their checksums. The script uses a temporary
directory and does not modify the source tree.

Observed result
---------------

On current master (716b214ad8db, GNU ld 2.47.50.20261202) built for all
targets with Clang ASan+UBSan, the trigger link aborts with:

    /opt/bfd/ld-new: input.o: relocation R_AARCH64_ABS32 against
`cross_3ee9ad466175_noise0_s2_WO' can not be used when making a shared object
    ...
    /opt/bfd/ld-new: input.o(.text.6[cross_e63cfc4cd612_relay_s1_GO]+0):
R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 used with non-TLS symbol .tbss
    ../../bfd/elfnn-aarch64.c:5443:15: runtime error: applying non-zero offset
320 to null pointer
    ==11==WARNING: invalid path to external symbolizer!
    ==11==WARNING: Failed to use and restart external symbolizer!
        #0 0x55bee4fe2cde  (/opt/bfd/ld-new+0x1d24cde) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #1 0x55bee4fde7e2  (/opt/bfd/ld-new+0x1d207e2) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #2 0x55bee4fcd52f  (/opt/bfd/ld-new+0x1d0f52f) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #3 0x55bee4d1d86d  (/opt/bfd/ld-new+0x1a5f86d) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #4 0x55bee4d12517  (/opt/bfd/ld-new+0x1a54517) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #5 0x55bee474ad37  (/opt/bfd/ld-new+0x148cd37) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #6 0x55bee4743b81  (/opt/bfd/ld-new+0x1485b81) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #7 0x7f674ab57249  (/lib/x86_64-linux-gnu/libc.so.6+0x27249) (BuildId:
93ac61ec5a8eb1396f9fbd350e3169a558528a40)
        #8 0x7f674ab57304  (/lib/x86_64-linux-gnu/libc.so.6+0x27304) (BuildId:
93ac61ec5a8eb1396f9fbd350e3169a558528a40)
        #9 0x55bee4624690  (/opt/bfd/ld-new+0x1366690) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)

    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
../../bfd/elfnn-aarch64.c:5443:15 in 
    Aborted (core dumped)
    run-repro: exit status 134

The trigger also prints two non-fatal "assertion fail elfnn-aarch64.c:5175"
lines before the abort.

Expected result
---------------

ld should stop when it rejects the R_AARCH64_ABS32 relocation and report a
normal link error. It should not continue into the final link, and it should
not compute a pointer from the NULL local-symbol GOT array at
bfd/elfnn-aarch64.c:5443.

Validation
----------

* Current upstream source (PASS): unmodified master 716b214ad8db (GNU ld
2.47.50.20261202), all targets, Clang ASan+UBSan, aborts at
elfnn-aarch64.c:5443 in symbol_got_offset_ref.
* Instrumented build (PASS): Fail-fast UBSan reports non-zero offset 320
applied to NULL while evaluating &l[r_symndx].got_offset.
* Independent control (PASS): Same linker and exact object complete an AArch64
-r partial link with exit 0 and no sanitizer diagnostic; output-mode limitation
is documented.
* Duplicate review (REVIEWED): No exact matching Sourceware report was found in
the review completed on 2026-10-29.

Root cause
----------

_bfd_elf_link_check_relocs stops scanning an input's sections at the first
check_relocs failure, and ld reports that failure but still runs the final
link. Under gdb, elf64_aarch64_check_relocs scans .text, then fails on .data
(the R_AARCH64_ABS32 error), so the section holding the TLS-IE relocation is
never scanned and elfNN_aarch64_allocate_local_symbols is never called for
input.o. elf64_aarch64_relocate_section then runs for every section and
dereferences the missing local-symbol array.

Suggested fix
------------------------

In elfNN_aarch64_relocate_section, stop when a GOT or TLS relocation against
a local symbol finds no local-symbol bookkeeping. The link has already failed
at that point, so this only replaces the crash with a clean error. The patch
is attached as
`bfd-aarch64-local-got-offsets-null-pointer-arithmetic.patch`:

    --- a/bfd/elfnn-aarch64.c
    +++ b/bfd/elfnn-aarch64.c
    @@ -7165,6 +7165,18 @@
           if (bfd_link_relocatable (info))
        continue;

    +      /* check_relocs allocates the local symbol GOT bookkeeping for
    +    every GOT or TLS reloc against a local symbol.  If it is
    +    missing, check_relocs failed on this input before reaching this
    +    reloc and the link has already failed.  */
    +      if (h == NULL
    +     && aarch64_reloc_got_type (bfd_r_type) != GOT_UNKNOWN
    +     && elf_aarch64_locals (input_bfd) == NULL)
    +   {
    +     bfd_set_error (bfd_error_bad_value);
    +     return false;
    +   }
    +
           if (h != NULL)
        name = h->root.root.string;
           else

Verified on master 716b214ad8db with the same Clang ASan+UBSan all-targets
build: the trigger now exits 1 with "final link failed: bad value" and no
sanitizer diagnostic, and the -r control still exits 0. The ld testsuite was
not run. The same guard also removes the analogous NULL local-symbol access
through symbol_tlsdesc_got_offset_ref (elfnn-aarch64.c:5493) for local
TLSDESC relocations.

A broader alternative is for ld to skip the final link when check_relocs
fails, which would cover the same pattern in other backends.

Scope
-----

This is a public robustness or correctness report. The observed behavior does
not establish an invalid write across a security boundary, code execution,
privilege gain, or a vulnerability introduced into otherwise safe generated
output.

Attachments
----------

`bfd-aarch64-local-got-offsets-null-pointer-arithmetic-reproducer.tar.gz`
`bfd-aarch64-local-got-offsets-null-pointer-arithmetic.patch`

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

Reply via email to