Skip to contentSkip to conversation

perf: Prevent potential null dereference

RFC In review Cyrill Gorcunov 4 participants 7 messages Dec 2, 2010 – Dec 6, 2010 perf events

lore
Share & tools
lore
#0 lore raw

RFC 1/1 perf: Prevent potential null dereference

In case if there is no memory we might hit null
dereference on accessing calloc'ed data.

CC Arnaldo Carvalho de Melo <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Ingo Molnar <[email protected]>
CC Frederic Weisbecker <[email protected]>

---

It seems exit right here is more convenient than passing error handling level up (which would have to exit anyway), thought if handling it "upper" is preferred -- just say a word.

 tools/perf/builtin-record.c |    4 ++++
 1 file changed, 4 insertions(+)
tools/perf/builtin-record.c +4 -0
Index: linux-2.6.git/tools/perf/builtin-record.c
=====================================================================
@@ -524,6 +524,10 @@ static void comm__construct(int argc, co
 		return;
 
 	comm = calloc(1, size);
+	if (!comm) {
+		pr_err("Not enough memory to construct internal command line.\n");
+		exit(-1);
+	}
 
 	tmp = comm;
 	for (i = 0; i < argc; i++) {
#1 lore raw
Quoted 22 lines · Cyrill Gorcunov

In case if there is no memory we might hit null
dereference on accessing calloc'ed data.

CC Arnaldo Carvalho de Melo <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Ingo Molnar <[email protected]>
CC Frederic Weisbecker <[email protected]>

---

It seems exit right here is more convenient than passing error handling level up (which would have to exit anyway), thought if handling it "upper" is preferred -- just say a word.

 tools/perf/builtin-record.c |    4 ++++
 1 file changed, 4 insertions(+)

Index: linux-2.6.git/tools/perf/builtin-record.c
=====================================================================

tools/perf/builtin-record.c static void comm__construct(int argc, co · line 533

Quoted 10 lines
tools/perf/builtin-record.c +4 -0
@@ -524,6 +524,10 @@ static void comm__construct(int argc, co
 		return;
 
 	comm = calloc(1, size);
+	if (!comm) {
+		pr_err("Not enough memory to construct internal command line.\n");
+		exit(-1);
+	}
 
 	tmp = comm;
 	for (i = 0; i < argc; i++) {

Good.

As a nit, not that it matters that much because we are very close to the starting code anyway, but it would be better to propagate the error to the callers.

Thanks.

#2 lore raw

Em Thu, Dec 02, 2010 at 11:41:08PM +0100, Frederic Weisbecker escreveu:

Quoted 31 lines · Cyrill Gorcunov
Quoted 30 lines

In case if there is no memory we might hit null
dereference on accessing calloc'ed data.

CC Arnaldo Carvalho de Melo <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Ingo Molnar <[email protected]>
CC Frederic Weisbecker <[email protected]>

---

It seems exit right here is more convenient than passing error handling level up (which would have to exit anyway), thought if handling it "upper" is preferred -- just say a word.

 tools/perf/builtin-record.c |    4 ++++
 1 file changed, 4 insertions(+)

Index: linux-2.6.git/tools/perf/builtin-record.c
=====================================================================

tools/perf/builtin-record.c +4 -0
@@ -524,6 +524,10 @@ static void comm__construct(int argc, co
 		return;
 
 	comm = calloc(1, size);
+	if (!comm) {
+		pr_err("Not enough memory to construct internal command line.\n");
+		exit(-1);
+	}
 
Quoted 8 lines

tools/perf/builtin-record.c static void comm__construct(int argc, co · line 533

Quoted 2 lines
tools/perf/builtin-record.c +0 -0
 	tmp = comm;
 	for (i = 0; i < argc; i++) {

Good.

As a nit, not that it matters that much because we are very close to the starting code anyway, but it would be better to propagate the error to the callers.

I'm of the opinion that main() should be where exit() is allowed, and even there... return would be better. 8-)

- Arnaldo

#3 lore raw

On Thu, Dec 02, 2010 at 08:46:10PM -0200, Arnaldo Carvalho de Melo wrote:
...

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

It seems exit right here is more convenient than passing error handling level up (which would have to exit anyway), thought if handling it "upper" is preferred -- just say a word.

Good.

Quoted 8 lines
Quoted 2 lines

As a nit, not that it matters that much because we are very close to the starting code anyway, but it would be better to propagate the error to the callers.

I'm of the opinion that main() should be where exit() is allowed, and even there... return would be better. 8-)

- Arnaldo

ok, i'll update tomorrow (almost off).

Cyrill

#4 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 11 lines · Arnaldo Carvalho de Melo

Em Thu, Dec 02, 2010 at 11:41:08PM +0100, Frederic Weisbecker escreveu:

Quoted 10 lines · Cyrill Gorcunov
Quoted 9 lines

In case if there is no memory we might hit null dereference on accessing calloc'ed data.

CC Arnaldo Carvalho de Melo <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Ingo Molnar <[email protected]>
CC Frederic Weisbecker <[email protected]>

---

...

Quoted 11 lines
Quoted 5 lines

Good.

As a nit, not that it matters that much because we are very close to the starting code anyway, but it would be better to propagate the error to the callers.

I'm of the opinion that main() should be where exit() is allowed, and even there... return would be better. 8-)

- Arnaldo

ok, sorry for delay, it seems the following would be liked more than first version ;)

Cyrill
---
[PATCH] perf: Prevent potential null dereference v2

In case if there is no memory we might hit null dereference on accessing calloc'ed data.

v2: propagate error to a caller

CC Arnaldo Carvalho de Melo <[email protected]>,
CC Peter Zijlstra <[email protected]>
CC Ingo Molnar <[email protected]>
CC Frederic Weisbecker <[email protected]>

---

NB it's unclear for me why don't we yield any message on too long command line, but anyway even then it should not be messed with this particular patch.

Arnaldo, I'll check builtin-kmem.c next time i be able to, though if there anyone would like to beat me on this -- feel free ;)

 tools/perf/builtin-record.c |   14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
tools/perf/builtin-record.c +11 -3
Index: linux-2.6.git/tools/perf/builtin-record.c
=====================================================================
@@ -507,7 +507,7 @@ static void mmap_read_all(void)
 		write_output(&finished_round_event, sizeof(finished_round_event));
 }
 
-static void comm__construct(int argc, const char **argv)
+static int comm__construct(int argc, const char **argv)
 {
 	char *comm, *tmp;
 	size_t size;
@@ -521,9 +521,13 @@ static void comm__construct(int argc, co
 	}
 
 	if ((long)size < 0)
-		return;
+		return 0;
 
 	comm = calloc(1, size);
+	if (!comm) {
+		pr_err("Not enough memory to construct internal command line.\n");
+		return -ENOMEM;
+	}
 
 	tmp = comm;
 	for (i = 0; i < argc; i++) {
@@ -533,6 +537,7 @@ static void comm__construct(int argc, co
 	}
 
 	session->command_line = comm;
+	return 0;
 }
 
 static int __cmd_record(int argc, const char **argv)
@@ -597,7 +602,10 @@ static int __cmd_record(int argc, const 
 	if (!no_buildid)
 		perf_header__set_feat(&session->header, HEADER_BUILD_ID);
 
-	comm__construct(argc, argv);
+	err = comm__construct(argc, argv);
+	if (err < 0)
+		goto out_delete_session;
+
 
 	if (!file_new) {
 		err = perf_header__read(session, output);
#5 lore raw

Em Mon, Dec 06, 2010 at 01:13:21AM +0300, Cyrill Gorcunov escreveu:

Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 11 lines

Em Thu, Dec 02, 2010 at 11:41:08PM +0100, Frederic Weisbecker escreveu:

Quoted 10 lines · Cyrill Gorcunov
Quoted 9 lines

In case if there is no memory we might hit null dereference on accessing calloc'ed data.

CC Arnaldo Carvalho de Melo <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Ingo Molnar <[email protected]>
CC Frederic Weisbecker <[email protected]>

---

...

Quoted 6 lines
Quoted 5 lines

Good.

As a nit, not that it matters that much because we are very close to the starting code anyway, but it would be better to propagate the error to the callers.

Quoted 9 lines
Quoted 5 lines

I'm of the opinion that main() should be where exit() is allowed, and even there... return would be better. 8-)

- Arnaldo

ok, sorry for delay, it seems the following would be liked more than first version ;)

Hey, what tree is this agains? I guess tip/master, right? If so, I still didn't got there :-\

- Arnaldo

#6 lore raw

On Mon, Dec 06, 2010 at 12:49:55PM -0200, Arnaldo Carvalho de Melo wrote:
...

Quoted 3 lines

Hey, what tree is this agains? I guess tip/master, right? If so, I still didn't got there :-\

yup, it was -tip/master

 | commit 93edb4532cbdbc174a64ec4ab347a345159bc93e
 | Merge: 913b7a9 11e8896
 | Author: Ingo Molnar <[email protected]>
 | Date:   Sat Dec 4 11:28:13 2010 +0100
 |
 |   Merge branch 'linus'
Quoted 3 lines

- Arnaldo

Cyrill