Skip to contentSkip to conversation

perf tools: Fix perf_evlist__alloc_mmap() failure path

PATCH Applied Wang Nan 4 participants 4 messages May 31, 2016 – Jun 8, 2016 perf events

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

PATCH 1/1 perf tools: Fix perf_evlist__alloc_mmap() failure path

If zalloc fail, setting evlist->mmap[i].fd is unsafe. perf_evlist__alloc_mmap() should fail right after zalloc() fail.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc He Kuang <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/evlist.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/evlist.c +4 -1
index e0f3094..1b918aa 100644
@@ -946,9 +946,12 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
 	if (cpu_map__empty(evlist->cpus))
 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
+	if (!evlist->mmap)
+		return -ENOMEM;
+
 	for (i = 0; i < evlist->nr_mmaps; i++)
 		evlist->mmap[i].fd = -1;
-	return evlist->mmap != NULL ? 0 : -ENOMEM;
+	return 0;
 }
 
 struct mmap_params {
Signature · 1 line
1.8.3.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · On Tue, 31 May 2016 13:06:15 +0000 Wang Nan <[email protected]> wrote:

If zalloc fail, setting evlist->mmap[i].fd is unsafe. perf_evlist__alloc_mmap() should fail right after zalloc() fail.

Good catch!!

Thanks!

Quoted 34 lines
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc He Kuang <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/evlist.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/evlist.c +4 -1
index e0f3094..1b918aa 100644
@@ -946,9 +946,12 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
 	if (cpu_map__empty(evlist->cpus))
 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
+	if (!evlist->mmap)
+		return -ENOMEM;
+
 	for (i = 0; i < evlist->nr_mmaps; i++)
 		evlist->mmap[i].fd = -1;
-	return evlist->mmap != NULL ? 0 : -ENOMEM;
+	return 0;
 }
 
 struct mmap_params {
Signature · 1 line
1.8.3.4
Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer Applied
#2 lore raw

Em Tue, May 31, 2016 at 10:27:43PM +0900, Masami Hiramatsu escreveu:

Quoted 9 lines
Quoted 2 lines · On Tue, 31 May 2016 13:06:15 +0000 Wang Nan <[email protected]> wrote:

If zalloc fail, setting evlist->mmap[i].fd is unsafe. perf_evlist__alloc_mmap() should fail right after zalloc() fail.

Good catch!!

Applied, after adding a:

Fixes d4c6fb36ac2c ("perf evsel: Record fd into perf_mmap")
Quoted 40 lines

Thanks!

Quoted 34 lines
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc He Kuang <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/evlist.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/evlist.c +4 -1
index e0f3094..1b918aa 100644
@@ -946,9 +946,12 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
 	if (cpu_map__empty(evlist->cpus))
 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
+	if (!evlist->mmap)
+		return -ENOMEM;
+
 	for (i = 0; i < evlist->nr_mmaps; i++)
 		evlist->mmap[i].fd = -1;
-	return evlist->mmap != NULL ? 0 : -ENOMEM;
+	return 0;
 }
 
 struct mmap_params {
Signature · 1 line
1.8.3.4
Signature · 1 line
Masami Hiramatsu <[email protected]>
#3 lore raw

[tip:perf/core] perf evlist: Fix alloc_mmap() failure path

In reply to Wang Nan
perf evlist: Fix alloc_mmap() failure path
Commit-ID:  946ae1d41d4b0c77b9f63b4a0393d8a1283a7f9d
Gitweb:     http://git.kernel.org/tip/946ae1d41d4b0c77b9f63b4a0393d8a1283a7f9d
Author:     Wang Nan <[email protected]>
AuthorDate: Tue, 31 May 2016 13:06:15 +0000
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 3 Jun 2016 14:53:46 -0300

perf evlist: Fix alloc_mmap() failure path

If zalloc fail, setting evlist->mmap[i].fd is unsafe and perf_evlist__alloc_mmap() should bail out right after that.

---
 tools/perf/util/evlist.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/evlist.c +4 -1
index e0f3094..1b918aa 100644
@@ -946,9 +946,12 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
 	if (cpu_map__empty(evlist->cpus))
 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
+	if (!evlist->mmap)
+		return -ENOMEM;
+
 	for (i = 0; i < evlist->nr_mmaps; i++)
 		evlist->mmap[i].fd = -1;
-	return evlist->mmap != NULL ? 0 : -ENOMEM;
+	return 0;
 }
 
 struct mmap_params {