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

            Bug ID: 35058
           Summary: ld/pdb.c: uint16_t overflow in calculate_symbols_size
                    and parse_symbols causes infinite loop and
                    heap-buffer-overflow
           Product: binutils
           Version: 2.47
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: ld
          Assignee: unassigned at sourceware dot org
          Reporter: xuehaolin at uniontech dot com
  Target Milestone: ---

Created attachment 17296
  --> https://sourceware.org/bugzilla/attachment.cgi?id=17296&action=edit
triggers infinite loop

In ld/pdb.c, calculate_symbols_size() and parse_symbols() use a local
uint16_t variable to hold the length of CodeView symbol records:

  uint16_t len = bfd_getl16(data) + sizeof(uint16_t);

When bfd_getl16(data) returns 0xFFFE, the addition produces 0x10000,
which truncates to 0 when stored in uint16_t. The loop then does:

  data += len;   // len == 0, pointer does not advance
  size -= len;   // len == 0, size does not decrease

This causes an infinite loop (denial of service) in calculate_symbols_size().

Additionally, in parse_symbols(), after the len==0 stall, subsequent
bfd_getl16() calls read past the end of the heap-allocated buffer,
causing a heap-buffer-overflow (OOB read).

This is a follow-up to commit 147e60cae54 ("Fix potential integer
overflows in ld/pdb.c"), which fixed the same uint16_t truncation
pattern in copy_filechksms() but did not address calculate_symbols_size()
or parse_symbols(). The "if (len > size)" check added by that commit
to parse_symbols() does not help when len wraps to 0, because
(0 > size) is false for any non-negative size. calculate_symbols_size()
received no such check at all.

Relevant source locations (origin/master, commit 9e9d08ed5bc):

  ld/pdb.c:1794  calculate_symbols_size - uint16_t len declaration (no bounds
check)
  ld/pdb.c:1919  calculate_symbols_size - data += len (stalls when len==0)
  ld/pdb.c:1027  parse_symbols - uint16_t len declaration
  ld/pdb.c:1031  parse_symbols - if (len > size) does not catch len==0
  ld/pdb.c:1037  parse_symbols - type = bfd_getl16(...) heap-buffer-overflow
  ld/pdb.c:1776  parse_symbols - data += len (stalls when len==0)

== Steps to reproduce ==

1. Build ld with ASan:

   mkdir build && cd build
   ../configure --target=i386-pc-mingw32 --disable-gdb --disable-gdbserver \
     --disable-sim --disable-gold \
     CFLAGS="-fsanitize=address,undefined -g -O1 -fno-omit-frame-pointer
-Wno-error"
   make all-ld -j$(nproc)

2a. Trigger infinite loop (calculate_symbols_size):

    timeout 15 ./ld/ld-new -m i386pe --pdb=test.pdb -b pe-i386 \
      poc_dos.obj -o /tmp/test.exe
    echo $?   # 124 = timeout (infinite loop confirmed)

2b. Trigger heap-buffer-overflow (parse_symbols):

    ASAN_OPTIONS=detect_leaks=0 ./ld/ld-new -m i386pe --pdb=test.pdb \
      -b pe-i386 poc_dos_parsesym.obj -o /tmp/test.exe

== ASan output (from origin/master 9e9d08ed5bc) ==

=================================================================
==4117127==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x603000000bf5
READ of size 1 at 0x603000000bf5 thread T0
    #0 bfd_getl16       bfd/libbfd.c:752
    #1 parse_symbols    ld/pdb.c:1037
    #2 handle_debugs_section  ld/pdb.c:2252
    #3 populate_module_stream  ld/pdb.c:4053
    #4 create_module_info_substream  ld/pdb.c:4206
    #5 populate_dbi_stream  ld/pdb.c:4764
    #6 create_pdb_file  ld/pdb.c:5457

0x603000000bf5 is located 1 bytes to the right of 20-byte region
allocated by bfd_get_full_section_contents (compress.c:763)

== Expected behavior ==

ld should reject the malformed .debug$S section with an error message
instead of entering an infinite loop or reading out of bounds.

== PoC files ==

Attached: poc_dos.obj (triggers infinite loop), poc_dos_parsesym.obj
(triggers heap-buffer-overflow).

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

Reply via email to