Bug 25362

Summary: memory leak in nm, bfd_malloc
Product: binutils Reporter: Heqing HUANG <featherrain26>
Component: binutilsAssignee: Alan Modra <amodra>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 2.32   
Target Milestone: 2.34   
Host: Target:
Build: Last reconfirmed: 2020-01-13 00:00:00
Project(s) to access: ssh public key:
Attachments: POC file

Description Heqing HUANG 2020-01-10 14:34:58 UTC
Created attachment 12183 [details]
POC file

Hi, there.

There is a memory leak in file binutils/nm.c, get_data function.

Here is the reproducing environment and procedure:

Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.6 LTS
Release:	16.04
Codename:	xenial
gcc:            5.4.0

compilation:
CFLAGS="-fsanitize=address,undefined" ./configure

run:
./nm-new -C -a -l --synthetic poc

Here is the error message:

=================================================================
==19825==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4294967344 byte(s) in 1 object(s) allocated from:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x44075f in bfd_malloc (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x44075f)
    #2 0x4b3d9b in _bfd_x86_elf_get_synthetic_symtab (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x4b3d9b)
    #3 0x649790 in elf_i386_get_synthetic_symtab (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x649790)
    #4 0x40a137 in display_rel_file (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x40a137)
    #5 0x40b1df in display_file (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x40b1df)
    #6 0x40db2a in main (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x40db2a)
    #7 0x7ffff5baf82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Direct leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x7fbefb in xmalloc (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x7fbefb)
    #2 0x409f2e in display_rel_file (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x409f2e)
    #3 0x40b1df in display_file (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x40b1df)
    #4 0x40db2a in main (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x40db2a)
    #5 0x7ffff5baf82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

Direct leak of 56 byte(s) in 2 object(s) allocated from:
    #0 0x7ffff6f02602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)
    #1 0x44075f in bfd_malloc (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x44075f)
    #2 0x647acb in elf_i386_get_synthetic_symtab (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x647acb)
    #3 0x40a137 in display_rel_file (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x40a137)
    #4 0x40b1df in display_file (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x40b1df)
    #5 0x40db2a in main (/playground/playground/binutils-2.32-r/binutils-2.32/binutils/nm-new+0x40db2a)
    #6 0x7ffff5baf82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: 4294967464 byte(s) leaked in 4 allocation(s).

Regards,
Comment 1 Alan Modra 2020-01-13 09:16:52 UTC
*** Bug 25363 has been marked as a duplicate of this bug. ***
Comment 2 Sourceware Commits 2020-01-13 12:16:38 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

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

commit 805f38bc551de820bcd7b31d3c5731ae27cf853a
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Jan 13 19:36:13 2020 +1030

    PR25362, memory leak in nm
    
    	PR 25362
    	* nm.c (display_rel_file): Free dyn_syms.
Comment 3 Alan Modra 2020-01-13 12:21:55 UTC
Fixed.