From patchwork Tue Jun 7 20:04:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaldo Carvalho de Melo X-Patchwork-Id: 69575 Delivered-To: patch@linaro.org Received: by 10.140.106.246 with SMTP id e109csp2153470qgf; Tue, 7 Jun 2016 13:06:42 -0700 (PDT) X-Received: by 10.36.31.137 with SMTP id d131mr7138369itd.87.1465330001703; Tue, 07 Jun 2016 13:06:41 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e1si36521402pfa.186.2016.06.07.13.06.41; Tue, 07 Jun 2016 13:06:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1422743AbcFGUGe (ORCPT + 31 others); Tue, 7 Jun 2016 16:06:34 -0400 Received: from merlin.infradead.org ([205.233.59.134]:56909 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161964AbcFGUFe (ORCPT ); Tue, 7 Jun 2016 16:05:34 -0400 Received: from [187.65.121.17] (helo=jouet.infradead.org) by merlin.infradead.org with esmtpsa (Exim 4.85_2 #1 (Red Hat Linux)) id 1bANFa-0002rN-DK; Tue, 07 Jun 2016 20:05:22 +0000 Received: by jouet.infradead.org (Postfix, from userid 1000) id 234D11438D2; Tue, 7 Jun 2016 17:05:18 -0300 (BRT) From: Arnaldo Carvalho de Melo To: Ingo Molnar Cc: linux-kernel@vger.kernel.org, Wang Nan , Masami Hiramatsu , Zefan Li , pi3orama@163.com, Arnaldo Carvalho de Melo Subject: [PATCH 08/24] perf tools: Fix crash in build_id_cache__kallsyms_path() Date: Tue, 7 Jun 2016 17:04:46 -0300 Message-Id: <1465329902-11069-9-git-send-email-acme@kernel.org> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1465329902-11069-1-git-send-email-acme@kernel.org> References: <1465329902-11069-1-git-send-email-acme@kernel.org> X-SRS-Rewrite: SMTP reverse-path rewritten from by merlin.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wang Nan build_id_cache__kallsyms_path() accepts a string buffer but also allocs a buffer using asnprintf. Unfortunately, the its only user passes it a stack-allocated buffer. Freeing it causes crashes like this: $ perf script *** Error in `/home/wangnan/perf': free(): invalid pointer: 0x00007fffffff9630 *** ======= Backtrace: ========= lib64/libc.so.6(+0x6eeef)[0x7ffff5dbaeef] lib64/libc.so.6(+0x78cae)[0x7ffff5dc4cae] lib64/libc.so.6(+0x79987)[0x7ffff5dc5987] /home/w00229757/perf(build_id_cache__kallsyms_path+0x6b)[0x49681b] /home/w00229757/perf[0x4bdd40] /home/w00229757/perf(dso__load+0xa3a)[0x4c048a] /home/w00229757/perf(map__load+0x6f)[0x4d561f] /home/w00229757/perf(thread__find_addr_map+0x235)[0x49e935] /home/w00229757/perf(machine__resolve+0x7d)[0x49ec6d] /home/w00229757/perf[0x4555a8] /home/w00229757/perf[0x4d9507] /home/w00229757/perf[0x4d9e80] /home/w00229757/perf(ordered_events__flush+0x354)[0x4dd444] /home/w00229757/perf(perf_session__process_events+0x3d0)[0x4dc140] /home/w00229757/perf(cmd_script+0x12b0)[0x4592e0] /home/w00229757/perf[0x4911f1] /home/w00229757/perf(main+0x68f)[0x4352ef] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7ffff5d6dbd5] /home/w00229757/perf[0x435415] ======= Memory map: ======== This patch simplifies build_id_cache__kallsyms_path(), not even considering allocating a string buffer, so never frees anything. Its caller should manage memory allocation. Signed-off-by: Wang Nan Cc: Masami Hiramatsu Cc: Zefan Li Cc: pi3orama@163.com Fixes: 01412261d994 ("perf buildid-cache: Use path/to/bin/buildid/elf instead of path/to/bin/buildid") Link: http://lkml.kernel.org/r/1465271678-7392-1-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/build-id.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) -- 2.5.5 diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index 67f986c8c378..20aef90bf194 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -147,20 +147,17 @@ static int asnprintf(char **strp, size_t size, const char *fmt, ...) char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf, size_t size) { - bool is_alloc = !!bf; bool retry_old = true; - asnprintf(&bf, size, "%s/%s/%s/kallsyms", - buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); + snprintf(bf, size, "%s/%s/%s/kallsyms", + buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); retry: if (!access(bf, F_OK)) return bf; - if (is_alloc) - free(bf); if (retry_old) { /* Try old style kallsyms cache */ - asnprintf(&bf, size, "%s/%s/%s", - buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); + snprintf(bf, size, "%s/%s/%s", + buildid_dir, DSO__NAME_KALLSYMS, sbuild_id); retry_old = false; goto retry; }