Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1416561

[PATCH 08/24] perf tools: Fix crash in build_id_cache__kallsyms_path()

From Arnaldo Carvalho de Melo <acme@kernel.org>
Newsgroups linux.kernel
Subject [PATCH 08/24] perf tools: Fix crash in build_id_cache__kallsyms_path()
Date 2016-06-07 22:10 +0200
Message-ID <rHvu3-7bR-53@gated-at.bofh.it> (permalink)
References <rHvu1-7bR-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Wang Nan <wangnan0@huawei.com>

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 <wangnan0@huawei.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
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 <acme@redhat.com>
---
 tools/perf/util/build-id.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

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;
 	}
-- 
2.5.5

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[GIT PULL 00/24] perf/core improvements and fixes Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:10 +0200
  [PATCH 08/24] perf tools: Fix crash in build_id_cache__kallsyms_path() Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:10 +0200
  [PATCH 02/24] tools lib bpf: Rename bpf_map__get_name() to bpf_map__name() Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:10 +0200
  [PATCH 20/24] perf unwind: Check the target platform before assigning unwind methods Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:10 +0200
  [PATCH 17/24] perf unwind: Rename unwind-libunwind.c to unwind-libunwind-local.c Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:10 +0200
  [PATCH 19/24] perf tools: Export normalize_arch() function Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 22/24] perf unwind: Introduce flag to separate local/remote unwind compilation Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 10/24] perf config: Use new perf_config_set__init() to initialize config set Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 06/24] tools lib bpf: Make bpf_program__get_private() use IS_ERR() Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 09/24] perf config: Constructor should free its allocated memory when failing Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 15/24] perf unwind: Don't mix LIBUNWIND_LIBS into LIBUNWIND_LDFLAGS Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 11/24] perf unwind: Use LIBUNWIND_DIR for remote libunwind feature check Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 12/24] perf unwind: Decouple thread->address_space on libunwind Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 13/24] perf unwind: Introduce 'struct unwind_libunwind_ops' for local unwind Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  [PATCH 01/24] tools lib bpf: Use IS_ERR() reporting macros with bpf_map__get_private() Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-07 22:20 +0200
  Re: [GIT PULL 00/24] perf/core improvements and fixes Taeung Song <treeze.taeung@gmail.com> - 2016-06-08 01:20 +0200
    Re: [GIT PULL 00/24] perf/core improvements and fixes Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-06-08 15:20 +0200
  Re: [GIT PULL 00/24] perf/core improvements and fixes Ingo Molnar <mingo@kernel.org> - 2016-06-08 09:50 +0200

csiph-web