Skip to contentSkip to conversation

perf tools: Fix possible double free on error

PATCH In review He Kuang 3 participants 4 messages Mar 4, 2015 – Mar 14, 2015 perf events

lore
Share & tools
lore

He Kuang

author
#0 lore raw

PATCH 1/1 perf tools: Fix possible double free on error

A double free occurred when get source file path failed. If lr->path failed to assign a new value, it will be freed as the old path and then be freed again during line_range__clear(), and causes this:

  $ perf probe -L do_execve -k vmlinux
  *** Error in `/usr/bin/perf': double free or corruption (fasttop):
      0x0000000000a9ac50 ***
  ======= Backtrace: =========
  ../lib64/libc.so.6(+0x6eeef)[0x7ffff5e44eef]
  ../lib64/libc.so.6(+0x78cae)[0x7ffff5e4ecae]
  ../lib64/libc.so.6(+0x79987)[0x7ffff5e4f987]
  ../bin/perf[0x4ab41f]
  ...

This patch fix this problem.

---
 tools/perf/util/probe-event.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/perf/util/probe-event.c +5 -1
index 919937e..7df30bd 100644
@@ -650,7 +650,11 @@ static int __show_line_range(struct line_range *lr, const char *module)
 	/* Convert source file path */
 	tmp = lr->path;
 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
-	free(tmp);	/* Free old path */
+
+	/* Free old path when new path is assigned */
+	if (tmp != lr->path)
+		free(tmp);
+
 	if (ret < 0) {
 		pr_warning("Failed to find source file path.\n");
 		return ret;
Signature · 1 line
2.2.0.33.gc18b867
Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · He Kuang

A double free occurred when get source file path failed. If lr->path failed to assign a new value, it will be freed as the old path and then be freed again during line_range__clear(), and causes this:

  $ perf probe -L do_execve -k vmlinux
  *** Error in `/usr/bin/perf': double free or corruption (fasttop):
      0x0000000000a9ac50 ***
  ======= Backtrace: =========
  ../lib64/libc.so.6(+0x6eeef)[0x7ffff5e44eef]
  ../lib64/libc.so.6(+0x78cae)[0x7ffff5e4ecae]
  ../lib64/libc.so.6(+0x79987)[0x7ffff5e4f987]
  ../bin/perf[0x4ab41f]
  ...
Quoted 1 line

This patch fix this problem.

Good catch!

Thank you,

Quoted 24 lines
---
 tools/perf/util/probe-event.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/perf/util/probe-event.c +5 -1
index 919937e..7df30bd 100644
@@ -650,7 +650,11 @@ static int __show_line_range(struct line_range *lr, const char *module)
 	/* Convert source file path */
 	tmp = lr->path;
 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
-	free(tmp);	/* Free old path */
+
+	/* Free old path when new path is assigned */
+	if (tmp != lr->path)
+		free(tmp);
+
 	if (ret < 0) {
 		pr_warning("Failed to find source file path.\n");
 		return ret;
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
author
#2 lore raw

Hi,

It seems this patch not appeared in any repo, so ping..

Quoted 46 lines · Masami Hiramatsu
Quoted 14 lines · He Kuang

A double free occurred when get source file path failed. If lr->path failed to assign a new value, it will be freed as the old path and then be freed again during line_range__clear(), and causes this:

   $ perf probe -L do_execve -k vmlinux
   *** Error in `/usr/bin/perf': double free or corruption (fasttop):
       0x0000000000a9ac50 ***
   ======= Backtrace: =========
   ../lib64/libc.so.6(+0x6eeef)[0x7ffff5e44eef]
   ../lib64/libc.so.6(+0x78cae)[0x7ffff5e4ecae]
   ../lib64/libc.so.6(+0x79987)[0x7ffff5e4f987]
   ../bin/perf[0x4ab41f]
   ...
Quoted 1 line

This patch fix this problem.

Good catch!

Thank you,

Quoted 23 lines
---
  tools/perf/util/probe-event.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
tools/perf/util/probe-event.c +5 -1
index 919937e..7df30bd 100644
@@ -650,7 +650,11 @@ static int __show_line_range(struct line_range *lr, const char *module)
  	/* Convert source file path */
  	tmp = lr->path;
  	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
-	free(tmp);	/* Free old path */
+
+	/* Free old path when new path is assigned */
+	if (tmp != lr->path)
+		free(tmp);
+
  	if (ret < 0) {
  		pr_warning("Failed to find source file path.\n");
  		return ret;
#3 lore raw

[tip:perf/core] perf probe: Fix possible double free on error

In reply to He Kuang
perf probe: Fix possible double free on error
Commit-ID:  a78604defffbc1da1497a8c8b48275b723eb5946
Gitweb:     http://git.kernel.org/tip/a78604defffbc1da1497a8c8b48275b723eb5946
Author:     He Kuang <[email protected]>
AuthorDate: Wed, 4 Mar 2015 18:01:42 +0800
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 12 Mar 2015 12:39:58 -0300

perf probe: Fix possible double free on error

A double free occurred when get source file path failed. If lr->path failed to assign a new value, it will be freed as the old path and then be freed again during line_range__clear(), and causes this:

  $ perf probe -L do_execve -k vmlinux
  *** Error in `/usr/bin/perf': double free or corruption (fasttop):
      0x0000000000a9ac50 ***
  ======= Backtrace: =========
  ../lib64/libc.so.6(+0x6eeef)[0x7ffff5e44eef]
  ../lib64/libc.so.6(+0x78cae)[0x7ffff5e4ecae]
  ../lib64/libc.so.6(+0x79987)[0x7ffff5e4f987]
  ../bin/perf[0x4ab41f]
  ...

This patch fix this problem.

---
 tools/perf/util/probe-event.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/perf/util/probe-event.c +5 -1
index 8af8e7f..e2bf620 100644
@@ -790,7 +790,11 @@ static int __show_line_range(struct line_range *lr, const char *module,
 	/* Convert source file path */
 	tmp = lr->path;
 	ret = get_real_path(tmp, lr->comp_dir, &lr->path);
-	free(tmp);	/* Free old path */
+
+	/* Free old path when new path is assigned */
+	if (tmp != lr->path)
+		free(tmp);
+
 	if (ret < 0) {
 		pr_warning("Failed to find source file path.\n");
 		return ret;