Skip to contentSkip to conversation

perf evsel: fix NULL pointer deference when evsel->counts is NULL

PATCH In review Colin King 3 participants 4 messages Jan 19, 2013 – Jan 23, 2013 perf events

lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 perf evsel: fix NULL pointer deference when evsel->counts is NULL

From: Colin Ian King <[email protected]>

__perf_evsel__read_on_cpu() only bails out with -ENOMEM if evsel->counts is NULL and perf_evsel__alloc_counts() has returned an error. If perf_evsel__alloc_counts() does not return an error we get an NULL pointer deference on evsel->counts->cpu[cpu] if evsel->counts is NULL.

---
 tools/perf/util/evsel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/evsel.c +1 -1
index 1b16dd1..93acd06 100644
@@ -640,7 +640,7 @@ int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
 	if (FD(evsel, cpu, thread) < 0)
 		return -EINVAL;
 
-	if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
+	if (evsel->counts == NULL || perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
 		return -ENOMEM;
 
 	if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
Signature · 1 line
1.8.0
maintainer
#1 lore raw

Hi Colin,

Quoted 7 lines · Colin King

From: Colin Ian King <[email protected]>

__perf_evsel__read_on_cpu() only bails out with -ENOMEM if evsel->counts is NULL and perf_evsel__alloc_counts() has returned an error. If perf_evsel__alloc_counts() does not return an error we get an NULL pointer deference on evsel->counts->cpu[cpu] if evsel->counts is NULL.

perf_evsel__alloc_counts() should allocate evsel->counts when it sees evsel->counts is NULL and return negative error code if the allocation fails.

So I don't see any problem in current code. With your code, it won't try to allocate if ->counts is NULL but overwrite existing ->counts?

Thanks,
Namhyung

Quoted 19 lines
---
 tools/perf/util/evsel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/evsel.c +1 -1
index 1b16dd1..93acd06 100644
@@ -640,7 +640,7 @@ int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
 	if (FD(evsel, cpu, thread) < 0)
 		return -EINVAL;
 
-	if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
+	if (evsel->counts == NULL || perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
 		return -ENOMEM;
 
 	if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
#2 lore raw

Em Mon, Jan 21, 2013 at 01:53:21PM +0900, Namhyung Kim escreveu:

Quoted 9 lines · Colin King
Quoted 7 lines

From: Colin Ian King <[email protected]>

__perf_evsel__read_on_cpu() only bails out with -ENOMEM if evsel->counts is NULL and perf_evsel__alloc_counts() has returned an error. If perf_evsel__alloc_counts() does not return an error we get an NULL pointer deference on evsel->counts->cpu[cpu] if evsel->counts is NULL.

Quoted 6 lines

perf_evsel__alloc_counts() should allocate evsel->counts when it sees evsel->counts is NULL and return negative error code if the allocation fails.

So I don't see any problem in current code. With your code, it won't try to allocate if ->counts is NULL but overwrite existing ->counts?

Right, the patch introduces a problem in code that works perfectly :-)

- Arnaldo

#3 lore raw
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Mon, Jan 21, 2013 at 01:53:21PM +0900, Namhyung Kim escreveu:

Quoted 12 lines · Colin King
Quoted 7 lines

From: Colin Ian King <[email protected]>

__perf_evsel__read_on_cpu() only bails out with -ENOMEM if evsel->counts is NULL and perf_evsel__alloc_counts() has returned an error. If perf_evsel__alloc_counts() does not return an error we get an NULL pointer deference on evsel->counts->cpu[cpu] if evsel->counts is NULL.

perf_evsel__alloc_counts() should allocate evsel->counts when it sees evsel->counts is NULL and return negative error code if the allocation fails.

Quoted 7 lines
Quoted 2 lines

So I don't see any problem in current code. With your code, it won't try to allocate if ->counts is NULL but overwrite existing ->counts?

Right, the patch introduces a problem in code that works perfectly :-)

- Arnaldo

Apologies for the noise.

Colin