Bug 21159

Summary: readelf of-by-one -- seems to be related to PR 21135
Product: binutils Reporter: Thuan Pham <thuanpv>
Component: binutilsAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: nickc
Priority: P2    
Version: 2.29   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Attachments: Bug triggering input

Description Thuan Pham 2017-02-14 10:41:12 UTC
Created attachment 9826 [details]
Bug triggering input

Dear all,

This bug was found with AFLGo, a directed version of AFL/AFLFast. Thanks also to Marcel Böhme. 

This bug was found on Ubuntu 14.04 64-bit & binutils was checked out from main repository at git://sourceware.org/git/binutils-gdb.git. Its commit is 61697d017e114d7667fbb340fb73f8184d48ee5a (Tue Feb 14 00:00:30 2017) 

binutils was built with ASAN using gcc-6.2 and clang-3.4. The configure command was:

CC=clang CFLAGS="-DFORTIFY_SOURCE=2 -fstack-protector-all -fsanitize=undefined,address -fno-omit-frame-pointer -g -Wno-error" ../configure --disable-shared --disable-gdb --disable-libdecnumber --disable-readline --disable-sim

To reproduce:
Download the attached file - bug_3
readelf -zR8 bug_3



Valgrind says:
==43329== Invalid read of size 1
==43329==    at 0x41E760: dump_section_as_bytes (readelf.c:12894)
==43329==    by 0x42355B: process_section_contents (readelf.c:13201)
==43329==    by 0x42355B: process_object (readelf.c:16899)
==43329==    by 0x402111: process_file (readelf.c:17273)
==43329==    by 0x402111: main (readelf.c:17344)
==43329==  Address 0x52036ce is 0 bytes after a block of size 78 alloc'd
==43329==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==43329==    by 0x40574E: get_data (readelf.c:393)
==43329==    by 0x41E5AB: dump_section_as_bytes (readelf.c:12785)
==43329==    by 0x42355B: process_section_contents (readelf.c:13201)
==43329==    by 0x42355B: process_object (readelf.c:16899)
==43329==    by 0x402111: process_file (readelf.c:17273)
==43329==    by 0x402111: main (readelf.c:17344)
==43329== 
==43329== Invalid read of size 1
==43329==    at 0x41E7C8: dump_section_as_bytes (readelf.c:12905)
==43329==    by 0x42355B: process_section_contents (readelf.c:13201)
==43329==    by 0x42355B: process_object (readelf.c:16899)
==43329==    by 0x402111: process_file (readelf.c:17273)
==43329==    by 0x402111: main (readelf.c:17344)
==43329==  Address 0x52036ce is 0 bytes after a block of size 78 alloc'd
==43329==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==43329==    by 0x40574E: get_data (readelf.c:393)
==43329==    by 0x41E5AB: dump_section_as_bytes (readelf.c:12785)
==43329==    by 0x42355B: process_section_contents (readelf.c:13201)
==43329==    by 0x42355B: process_object (readelf.c:16899)
==43329==    by 0x402111: process_file (readelf.c:17273)
==43329==    by 0x402111: main (readelf.c:17344)

ASAN says:
==48173==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60700000d88e at pc 0x56090c bp 0x7ffc4f4c1d50 sp 0x7ffc4f4c1d48
READ of size 1 at 0x60700000d88e thread T0
    #0 0x56090b in dump_section_as_bytes /home/ubuntu/thesis/subjects/binutils-gdb-nick/build-asan/binutils/../../binutils/readelf.c:12894
    #1 0x4e1661 in process_section_contents /home/ubuntu/thesis/subjects/binutils-gdb-nick/build-asan/binutils/../../binutils/readelf.c:13201
    #2 0x48d720 in process_object /home/ubuntu/thesis/subjects/binutils-gdb-nick/build-asan/binutils/../../binutils/readelf.c:16899
    #3 0x488475 in process_file /home/ubuntu/thesis/subjects/binutils-gdb-nick/build-asan/binutils/../../binutils/readelf.c:17273
    #4 0x4856d3 in main /home/ubuntu/thesis/subjects/binutils-gdb-nick/build-asan/binutils/../../binutils/readelf.c:17344
    #5 0x7ff239209f44 (/lib/x86_64-linux-gnu/libc.so.6+0x21f44)
    #6 0x47ddfc in _start (/home/ubuntu/thesis/subjects/binutils-gdb-nick/build-asan/binutils/readelf+0x47ddfc)
Comment 1 cvs-commit@gcc.gnu.org 2017-02-14 15:11:54 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

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

commit bc303e5d6c2dd33086478f80fd1d3096d4e1bc01
Author: Nick Clifton <nickc@redhat.com>
Date:   Tue Feb 14 15:10:34 2017 +0000

    Fix invalid memory access displayiing contents of sections.
    
    	PR binutils/21159
    	* readelf.c (dump_section_as_strings): Reset the start address if
    	no decompression is perfromed.
    	(dump_section_as_bytes): Likewise.
Comment 2 Nick Clifton 2017-02-14 15:16:13 UTC
Hi Thuan,

  Thanks for the bug report.  You are right, this problem is fallout from the fix for 21135.  I have applied a patch to fix the problem, and I think that this time it should work. :-)

Cheers
  Nick