Skip to contentSkip to conversation

perf: Add NULL pointer check for cpu_map iteration and NULL assignment for all_cpus.

PATCH Applied [email protected] 4 participants 5 messages Mar 8, 2020 – May 8, 2020 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: Add NULL pointer check for cpu_map iteration and NULL assignment for all_cpus.

From: He Zhe <[email protected]>

NULL pointer may be passed to perf_cpu_map__cpu and then cause crash, such as the one commit cb71f7d43ece ("libperf: Setup initial evlist::all_cpus value") fix.

---
 tools/perf/lib/cpumap.c | 2 +-
 tools/perf/lib/evlist.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
tools/perf/lib/cpumap.c +1 -1
index f93f4e7..ca02150 100644
@@ -247,7 +247,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 
 int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx)
 {
-	if (idx < cpus->nr)
+	if (cpus && idx < cpus->nr)
 		return cpus->map[idx];
 
 	return -1;
tools/perf/lib/evlist.c +1 -0
index 5b9f2ca..f87a239 100644
@@ -127,6 +127,7 @@ void perf_evlist__exit(struct perf_evlist *evlist)
 	perf_cpu_map__put(evlist->cpus);
 	perf_thread_map__put(evlist->threads);
 	evlist->cpus = NULL;
+	evlist->all_cpus = NULL;
 	evlist->threads = NULL;
 	fdarray__exit(&evlist->pollfd);
 }
Signature · 1 line
2.7.4
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · On Sun, Mar 08, 2020 at 06:59:17PM +0800, [email protected] wrote:

From: He Zhe <[email protected]>

NULL pointer may be passed to perf_cpu_map__cpu and then cause crash, such as the one commit cb71f7d43ece ("libperf: Setup initial evlist::all_cpus value") fix.

thanks,
jirka

Quoted 33 lines
---
 tools/perf/lib/cpumap.c | 2 +-
 tools/perf/lib/evlist.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
tools/perf/lib/cpumap.c +1 -1
index f93f4e7..ca02150 100644
@@ -247,7 +247,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 
 int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx)
 {
-	if (idx < cpus->nr)
+	if (cpus && idx < cpus->nr)
 		return cpus->map[idx];
 
 	return -1;
tools/perf/lib/evlist.c +1 -0
index 5b9f2ca..f87a239 100644
@@ -127,6 +127,7 @@ void perf_evlist__exit(struct perf_evlist *evlist)
 	perf_cpu_map__put(evlist->cpus);
 	perf_thread_map__put(evlist->threads);
 	evlist->cpus = NULL;
+	evlist->all_cpus = NULL;
 	evlist->threads = NULL;
 	fdarray__exit(&evlist->pollfd);
 }
Signature · 1 line
2.7.4
#2 lore raw

Em Wed, Mar 18, 2020 at 11:32:24AM +0100, Jiri Olsa escreveu:

Quoted 7 lines · On Sun, Mar 08, 2020 at 06:59:17PM +0800, [email protected] wrote:
Quoted 6 lines

From: He Zhe <[email protected]>

NULL pointer may be passed to perf_cpu_map__cpu and then cause crash, such as the one commit cb71f7d43ece ("libperf: Setup initial evlist::all_cpus value") fix.

Quoted 11 lines
Quoted 1 line

thanks,
jirka

Quoted 4 lines
---
 tools/perf/lib/cpumap.c | 2 +-
 tools/perf/lib/evlist.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

this is in tools/lib/perf/ for some time already, I'll do the changes there, thanks,

- Arnaldo

Quoted 29 lines
Quoted 29 lines
tools/perf/lib/cpumap.c +1 -1
index f93f4e7..ca02150 100644
@@ -247,7 +247,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 
 int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx)
 {
-	if (idx < cpus->nr)
+	if (cpus && idx < cpus->nr)
 		return cpus->map[idx];
 
 	return -1;
tools/perf/lib/evlist.c +1 -0
index 5b9f2ca..f87a239 100644
@@ -127,6 +127,7 @@ void perf_evlist__exit(struct perf_evlist *evlist)
 	perf_cpu_map__put(evlist->cpus);
 	perf_thread_map__put(evlist->threads);
 	evlist->cpus = NULL;
+	evlist->all_cpus = NULL;
 	evlist->threads = NULL;
 	fdarray__exit(&evlist->pollfd);
 }
Signature · 1 line
2.7.4
#3 lore raw

Em Wed, Mar 18, 2020 at 10:34:35AM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 15 lines

Em Wed, Mar 18, 2020 at 11:32:24AM +0100, Jiri Olsa escreveu:

Quoted 14 lines · On Sun, Mar 08, 2020 at 06:59:17PM +0800, [email protected] wrote:
Quoted 7 lines

From: He Zhe <[email protected]>

NULL pointer may be passed to perf_cpu_map__cpu and then cause crash, such as the one commit cb71f7d43ece ("libperf: Setup initial evlist::all_cpus value") fix.

thanks,
jirka

Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
---
 tools/perf/lib/cpumap.c | 2 +-
 tools/perf/lib/evlist.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

this is in tools/lib/perf/ for some time already, I'll do the changes there, thanks,

Fixed up from tools/perf/lib to tools/lib/perf and applied.

Quoted 31 lines

- Arnaldo

Quoted 29 lines
Quoted 29 lines
tools/perf/lib/cpumap.c +1 -1
index f93f4e7..ca02150 100644
@@ -247,7 +247,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 
 int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx)
 {
-	if (idx < cpus->nr)
+	if (cpus && idx < cpus->nr)
 		return cpus->map[idx];
 
 	return -1;
tools/perf/lib/evlist.c +1 -0
index 5b9f2ca..f87a239 100644
@@ -127,6 +127,7 @@ void perf_evlist__exit(struct perf_evlist *evlist)
 	perf_cpu_map__put(evlist->cpus);
 	perf_thread_map__put(evlist->threads);
 	evlist->cpus = NULL;
+	evlist->all_cpus = NULL;
 	evlist->threads = NULL;
 	fdarray__exit(&evlist->pollfd);
 }
Signature · 1 line
2.7.4
merge bot Applied
#4 lore raw

[tip: perf/core] libperf: Add NULL pointer check for cpu_map iteration and NULL assignment for all_cpus.

In reply to [email protected]
The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     44d041b7b2c11b6739501fd3763cc6fed62cf0ed
Gitweb:        https://git.kernel.org/tip/44d041b7b2c11b6739501fd3763cc6fed62cf0ed
Author:        He Zhe <[email protected]>
AuthorDate:    Sun, 08 Mar 2020 18:59:17 +08:00
Committer:     Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Tue, 05 May 2020 16:35:29 -03:00

libperf: Add NULL pointer check for cpu_map iteration and NULL assignment for all_cpus.

A NULL pointer may be passed to perf_cpu_map__cpu and then cause a crash, such as the one commit cb71f7d43ece ("libperf: Setup initial evlist::all_cpus value") fix.

---
 tools/lib/perf/cpumap.c | 2 +-
 tools/lib/perf/evlist.c | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
tools/lib/perf/cpumap.c +1 -1
index f93f4e7..ca02150 100644
@@ -247,7 +247,7 @@ out:
 
 int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx)
 {
-	if (idx < cpus->nr)
+	if (cpus && idx < cpus->nr)
 		return cpus->map[idx];
 
 	return -1;
tools/lib/perf/evlist.c +1 -0
index def5505..c481b62 100644
@@ -125,6 +125,7 @@ void perf_evlist__exit(struct perf_evlist *evlist)
 	perf_cpu_map__put(evlist->cpus);
 	perf_thread_map__put(evlist->threads);
 	evlist->cpus = NULL;
+	evlist->all_cpus = NULL;
 	evlist->threads = NULL;
 	fdarray__exit(&evlist->pollfd);
 }