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

            Bug ID: 35098
           Summary: GNU ld compact EH terminator dereferences a missing
                    section
           Product: binutils
           Version: 2.48 (HEAD)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: gprofng
          Assignee: unassigned at sourceware dot org
          Reporter: emily.west286 at gmail dot com
  Target Milestone: ---

Created attachment 17330
  --> https://sourceware.org/bugzilla/attachment.cgi?id=17330&action=edit
Reproduction materials

I'm reporting this bug on behalf of my team: Ananta Srikar Puranam, Emily West,
Dr. Swarn Priya, Dr. Peng Gao and Dr. Basavesh Ammanaghatta Shivakumar.

Product: binutils
Component: ld
Version: current master (716b214ad8db)
Host/target: MIPS ELF (elf32ltsmip)

Summary
-------

If a link discards every .eh_frame_entry section, _bfd_elf_end_eh_frame_parsing
(bfd/elf-eh-frame.c) skips its early empty check and then loops over
array_count - 1. With array_count == 0 that wraps to UINT_MAX, so the loop
passes a NULL entry to add_eh_frame_hdr_terminator, which reads sec->sec_info
at line 1108.

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

1. Extract `bfd-compact-eh-frame-terminator-null-section-reproducer.tar.gz`.
2. Run:

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

   Without a local build, use the `bfd-repro` image:

       docker run --rm -v "$PWD:/queue:ro" bfd-repro \
        
/queue/attachments/bfd-compact-eh-frame-terminator-null-section-reproducer.tar.gz

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

Master 716b214ad8db, Clang ASan+UBSan. Trigger:

    ../../bfd/elf-eh-frame.c:1108:23: runtime error: member access within null
pointer of type 'asection' (aka 'struct bfd_section')
    ==20==WARNING: invalid path to external symbolizer!
    ==20==WARNING: Failed to use and restart external symbolizer!
        #0 0x5606e0cfb4c6  (/opt/bfd/ld-new+0x1a9a4c6) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #1 0x5606e0cfab9a  (/opt/bfd/ld-new+0x1a99b9a) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #2 0x5606e0cd9f68  (/opt/bfd/ld-new+0x1a78f68) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #3 0x5606e0a42ccf  (/opt/bfd/ld-new+0x17e1ccf) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #4 0x5606e06f8f71  (/opt/bfd/ld-new+0x1497f71) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #5 0x5606e06c4439  (/opt/bfd/ld-new+0x1463439) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #6 0x5606e06e6a1a  (/opt/bfd/ld-new+0x1485a1a) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)
        #7 0x7fac8fb98249  (/lib/x86_64-linux-gnu/libc.so.6+0x27249) (BuildId:
93ac61ec5a8eb1396f9fbd350e3169a558528a40)
        #8 0x7fac8fb98304  (/lib/x86_64-linux-gnu/libc.so.6+0x27304) (BuildId:
93ac61ec5a8eb1396f9fbd350e3169a558528a40)
        #9 0x5606e05c7690  (/opt/bfd/ld-new+0x1366690) (BuildId:
cdf54a78f712fddf693b6a6d40dd2b8a40f19f55)

    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
../../bfd/elf-eh-frame.c:1108:23 in
    timeout: the monitored command dumped core
    Aborted

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

ld should accept the input with a warning, or reject it with a normal
diagnostic. It should not crash or read a NULL section.

Validation
----------

* Master 716b214ad8db, Clang ASan+UBSan: trigger aborts at elf-eh-frame.c:1108;
control exits 0.
* With the suggested patch: trigger exits 0 with an entry symbol warning.
* Related: #34445, #34446, and #34792

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

Two options:

1. Accept with a warning: return early when no entries remain after discarding.
2. Reject with an error: report the malformed table and fail the link.

The suggested patch accepts with a warning.
Patch test image: `patch.Dockerfile`, built with
`--build-arg PATCH=bfd-compact-eh-frame-terminator-null-section.patch`.

Scope
-----

No invalid write across a security boundary is established.

Attachment
----------

- `bfd-compact-eh-frame-terminator-null-section-reproducer.tar.gz`
- `bfd-compact-eh-frame-terminator-null-section.patch`
- `patch.Dockerfile`

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

Reply via email to