Blob Blame History Raw
commit 80c570537e380c1b8e48754c0ddbce2abcde2d00
Author: Jan Kratochvil <jan.kratochvil@redhat.com>
Date:   Thu Feb 26 14:08:01 2015 +0100

    SEGV in ppc64_elf_get_synthetic_symtab reading a separate debug file
    
    The attached patch fixes the SEGV and lets GDB successfully
    load all kernel modules installed by default on RHEL 7.
    
    Valgrind on F-21 x86_64 host has shown me more clear what is the problem:
    
    Reading symbols from /home/jkratoch/t/cordic.ko...Reading symbols from
    /home/jkratoch/t/cordic.ko.debug...=================================================================
    ==22763==ERROR: AddressSanitizer: heap-use-after-free on address 0x6120000461c8 at pc 0x150cdbd bp 0x7fffffffc7e0 sp 0x7fffffffc7d0
    READ of size 8 at 0x6120000461c8 thread T0
        #0 0x150cdbc in ppc64_elf_get_synthetic_symtab /home/jkratoch/redhat/gdb-test-asan/bfd/elf64-ppc.c:3282
        #1 0x8c5274 in elf_read_minimal_symbols /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1205
        #2 0x8c55e7 in elf_symfile_read /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1268
    [...]
    0x6120000461c8 is located 264 bytes inside of 288-byte region [0x6120000460c0,0x6120000461e0)
    freed by thread T0 here:
        #0 0x7ffff715454f in __interceptor_free (/lib64/libasan.so.1+0x5754f)
        #1 0xde9cde in xfree common/common-utils.c:98
        #2 0x9a04f7 in do_my_cleanups common/cleanups.c:155
        #3 0x9a05d3 in do_cleanups common/cleanups.c:177
        #4 0x8c538a in elf_read_minimal_symbols /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1229
        #5 0x8c55e7 in elf_symfile_read /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1268
    [...]
    previously allocated by thread T0 here:
        #0 0x7ffff71547c7 in malloc (/lib64/libasan.so.1+0x577c7)
        #1 0xde9b95 in xmalloc common/common-utils.c:41
        #2 0x8c4da2 in elf_read_minimal_symbols /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1147
        #3 0x8c55e7 in elf_symfile_read /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1268
    [...]
    SUMMARY: AddressSanitizer: heap-use-after-free /home/jkratoch/redhat/gdb-test-asan/bfd/elf64-ppc.c:3282 ppc64_elf_get_synthetic_symtab
    [...]
    ==22763==ABORTING
    
    A similar case a few lines later I have fixed in 2010 by:
            https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=3f1eff0a2c7f0e7078f011f55b8e7f710aae0cc2
    
    My testcase does not always reproduce it but at least a bit:
     * GDB without ppc64 target (even as a secondary one) is reported as "untested"
     * ASAN-built GDB with ppc64 target always crashes (and PASSes with this fix)
     * unpatched non-ASAN-built GDB with ppc64 target crashes from commandline
     * unpatched non-ASAN-built GDB with ppc64 target PASSes from runtest (?)
    
    gdb/ChangeLog
    2015-02-26  Jan Kratochvil  <jan.kratochvil@redhat.com>
    
    	* elfread.c (elf_read_minimal_symbols): Use bfd_alloc for
    	bfd_canonicalize_symtab.
    
    gdb/testsuite/ChangeLog
    2015-02-26  Jan Kratochvil  <jan.kratochvil@redhat.com>
    
    	* gdb.arch/cordic.ko.bz2: New file.
    	* gdb.arch/cordic.ko.debug.bz2: New file.
    	* gdb.arch/ppc64-symtab-cordic.exp: New file.

Index: gdb-7.6.1/gdb/elfread.c
===================================================================
--- gdb-7.6.1.orig/gdb/elfread.c
+++ gdb-7.6.1/gdb/elfread.c
@@ -2308,8 +2308,10 @@ elf_symfile_read (struct objfile *objfil
 
   if (storage_needed > 0)
     {
-      symbol_table = (asymbol **) xmalloc (storage_needed);
-      make_cleanup (xfree, symbol_table);
+      /* Memory gets permanently referenced from ABFD after
+	 bfd_canonicalize_symtab so it must not get freed before ABFD gets.  */
+
+      symbol_table = bfd_alloc (abfd, storage_needed);
       symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
 
       if (symcount < 0)
Index: gdb-7.6.1/gdb/testsuite/gdb.arch/ppc64-symtab-cordic.exp
===================================================================
--- /dev/null
+++ gdb-7.6.1/gdb/testsuite/gdb.arch/ppc64-symtab-cordic.exp
@@ -0,0 +1,51 @@
+# Copyright 2015 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+
+set kobz2uufile ${srcdir}/${subdir}/cordic.ko.bz2.uu
+set kofile ${objdir}/${subdir}/cordic.ko
+set kodebugbz2uufile ${srcdir}/${subdir}/cordic.ko.debug.bz2.uu
+set kodebugfile ${objdir}/${subdir}/cordic.ko.debug
+
+if {[catch "system \"uudecode -o - ${kobz2uufile} | bzip2 -dc >${kofile}\""] != 0} {
+    untested "failed bzip2 for ${kobz2uufile}"
+    return -1
+}
+if {[catch "system \"uudecode -o - ${kodebugbz2uufile} | bzip2 -dc >${kodebugfile}\""] != 0} {
+    untested "failed bzip2 for ${kodebugbz2uufile}"
+    return -1
+}
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+
+# This test won't work properly if system debuginfo is installed.
+# Test message is suppressed by "" as otherwise we could print PASS+UNTESTED
+# result to gdb.sum making a false feeling the issue has been tested.
+gdb_test_no_output "set debug-file-directory" ""
+
+gdb_load ${kofile}
+
+set test "show architecture"
+gdb_test_multiple $test $test {
+    -re "\r\nThe target architecture is set automatically \\(currently powerpc:common64\\)\r\n$gdb_prompt $" {
+	pass $test
+    }
+    -re "\r\nThe target architecture is set automatically \\(currently .*\\)\r\n$gdb_prompt $" {
+	untested "powerpc:common64 is not supported"
+    }
+}
Index: gdb-7.6.1/gdb/testsuite/gdb.arch/cordic.ko.bz2.uu
===================================================================
--- /dev/null
+++ gdb-7.6.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+end
Index: gdb-7.6.1/gdb/testsuite/gdb.arch/cordic.ko.debug.bz2.uu
===================================================================
--- /dev/null
+++ gdb-7.6.1/gdb/testsuite/gdb.arch/cordic.ko.debug.bz2.uu
@@ -0,0 +1,24 @@
+begin 664 cordic.ko.debug.bz2.uu
+M0EIH.3%!62936;;3U;4``^=______^==:W]63^U^1+_O_V"H4V#`)$A@F@90
+M"D$!"""2P`,!Q0-*1AHB-4])ZGI-#3U`T:8C0``-``-`:-#0`:#T@!IH-&@-
+M!HA$\D](])E)Z(V@)A#30TT,1@`C-`)@(!H8"8!,--!!@``````````$P```
+M`$8``F``2)$FF@FDVIZGJ>BFF1A-/-)HVJ'J8`#U3,H:#"-D@'H:@8CT&("]
+M???6F=%1'FNGCPP"I4HL6ZE@2>=E(>L$@&5?>9ZJAI%`&%I2P2MAW226F58=
+M!Z[0:D$@C,S$%$*-\ZZF[U##1Q9M=6V$MAXS#$,A5@8IA,-^8#:S#*JAEJUA
+M.ZJWN+EH.Z>`5$02]B/^B\^*GMP%%$,#,BH!`%2&""YD(Z$``3/9]@Y;'/LR
+M6_C%]&YT&1HR5[H(&VQ`9U[/&/_IX]&CA3,4BW\%WK:>,KF?AA?;I0M)HJHA
+M))0E$!$-PFTDHB!1$`@;;*T0".1!";2&QM!L,.,TDA*LQ:G*A?T5HS.EQQ-^
+MXX9F.PV"$H]V,TTRF$T%@NHPIQ2[]T6@8*!#(2$CYW1@8Y(S,WQ[.UM[GKY.
+M"Y^V=].A<UFI,NGU92)29WV1.9)H:[UE)0D)4,"\PS?KC;T(3=0U#]C`"0J4
+MLR"24H($(R60S'.$.0^K9F@,AZ&K`@I92T5.3:4H@=-7S'#/C,1QSD%>INN_
+MT#7-^3M?A9*J-8-<-'1M[-.*2H<Y0M)B'-Y(3XMHL#)[--X-<BGOA\YO#T,(
+M,AEB@'L$[^WOL#7!:ID4FH5S_!;7O`M!KRV?T3%):X7H+A!38."$$X!D+S@4
+MP3/-VM<9?0%-"Z.(MVYY(FE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