Bug 22058 - Heap out of bounds read in _bfd_elf_attr_strdup()
Summary: Heap out of bounds read in _bfd_elf_attr_strdup()
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.29
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-09-01 07:22 UTC by Kamil Frankowicz
Modified: 2017-09-01 09:01 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
POC to trigger heap out of bounds read (objdump) (165 bytes, application/octet-stream)
2017-09-01 07:22 UTC, Kamil Frankowicz
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kamil Frankowicz 2017-09-01 07:22:56 UTC
Created attachment 10383 [details]
POC to trigger heap out of bounds read (objdump)

After some fuzz testing I found a crashing test case.

Version: 2.29

Command: objdump -x -Wl -R -SD objdump_hoobr_bfd_elf_attr_strdup

ASAN:

==29788==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x619000004090 at pc 0x000000424b5f bp 0x7fff480b0c50 sp 0x7fff480b03f8
READ of size 1 at 0x619000004090 thread T0
    #0 0x424b5e in __interceptor_strlen /home/llvm/clang-3.9/final/llvm.src/projects/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:227:5
    #1 0x794012 in _bfd_elf_attr_strdup XYZ/binutils-2.29/bfd/elf-attrs.c:310:9
    #2 0x794012 in bfd_elf_add_obj_attr_string XYZ/binutils-2.29/bfd/elf-attrs.c:323
    #3 0x796201 in _bfd_elf_parse_attributes XYZ/binutils-2.29/bfd/elf-attrs.c:533:6
    #4 0x6e3766 in bfd_section_from_shdr XYZ/binutils-2.29/bfd/elf.c:2448:4
    #5 0x803733 in bfd_elf32_object_p XYZ/binutils-2.29/bfd/./elfcode.h:805:7
    #6 0x65bf6c in bfd_check_format_matches XYZ/binutils-2.29/bfd/format.c:311:14
    #7 0x4e8bb5 in display_object_bfd XYZ/binutils-2.29/binutils/./objdump.c:3601:7
    #8 0x4e8bb5 in display_any_bfd XYZ/binutils-2.29/binutils/./objdump.c:3692
    #9 0x4e7d5a in display_file XYZ/binutils-2.29/binutils/./objdump.c:3713:3
    #10 0x4e7d5a in main XYZ/binutils-2.29/binutils/./objdump.c:4015
    #11 0x7ff780a6f82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #12 0x419d98 in _start (XYZ/binutils-2.29/binutils/objdump+0x419d98)

0x619000004090 is located 0 bytes to the right of 1040-byte region [0x619000003c80,0x619000004090)
allocated by thread T0 here:
    #0 0x4b85ac in malloc /home/llvm/clang-3.9/final/llvm.src/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:64:3
    #1 0x6618b3 in bfd_malloc XYZ/binutils-2.29/bfd/libbfd.c:193:9
    #2 0x6e3766 in bfd_section_from_shdr XYZ/binutils-2.29/bfd/elf.c:2448:4

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/llvm/clang-3.9/final/llvm.src/projects/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:227:5 in __interceptor_strlen
Shadow bytes around the buggy address:
  0x0c327fff87c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c327fff87d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c327fff87e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c327fff87f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c327fff8800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c327fff8810: 00 00[fa]fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8820: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8830: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8840: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8850: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c327fff8860: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Heap right redzone:      fb
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack partial redzone:   f4
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==29788==ABORTING
Comment 1 cvs-commit@gcc.gnu.org 2017-09-01 08:58:58 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

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

commit 2a143b99fc4a5094a9cf128f3184d8e6818c8229
Author: Nick Clifton <nickc@redhat.com>
Date:   Fri Sep 1 09:57:44 2017 +0100

    Fix buffer overrun when parsing an ELF attribute string that is not NUL terminated.
    
    	PR 22058
    	* elf-attrs.c (_bfd_elf_parse_attributes): Ensure that the
    	attribute buffer is NUL terminated.
Comment 2 cvs-commit@gcc.gnu.org 2017-09-01 09:00:29 UTC
The binutils-2_29-branch branch has been updated by Nick Clifton <nickc@sourceware.org>:

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

commit aceaeff8140af6ba50469e8b63d664cc91e5485b
Author: Nick Clifton <nickc@redhat.com>
Date:   Fri Sep 1 09:59:17 2017 +0100

    Fix buffer overrun when parsing an ELF attribute string that is not NUL terminated.
    
    	PR 22058
    	* elf-attrs.c (_bfd_elf_parse_attributes): Ensure that the
    	attribute buffer is NUL terminated.
Comment 3 Nick Clifton 2017-09-01 09:01:51 UTC
Hi Kamil,

  Thanks for reporting this bug.  This was another case of buffer overrun
  due to an unterminated string.  I have checked in a small patch to the
  mainline and 2.29 branch in order to fix the problem.

Cheers
  Nick