Skip to contentSkip to conversation

perf: prevent overflow in size calculation

PATCH Superseded Cody Schafer 2 participants 7 messages Jul 20, 2012 – Jul 25, 2012 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: prevent overflow in size calculation

A large enough symbol size causes an overflow in the size parameter to the histogram allocation, leading to a segfault in symbol__inc_addr_samples later on when this histogram is accessed.

In the case of being called via perf-report, this returns back and gracefully ignores the sample, eventually ignoring the chained return value of perf_session_deliver_event in flush_sample_queue.

---
 tools/perf/util/annotate.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/annotate.c +6 -1
index 8069dfb..6f78f20 100644
@@ -426,8 +426,13 @@ int symbol__alloc_hist(struct symbol *sym)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	const size_t size = symbol__size(sym);
-	size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+	size_t sizeof_sym_hist;
 
+	/* Check for overflow when calculating sizeof_sym_hist */
+	if (size > (SIZE_MAX / sizeof(u64)))
+		return -1;
+
+	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
 	notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
 	if (notes->src == NULL)
 		return -1;
Signature · 1 line
1.7.9.5
maintainer
#1 lore raw

Hi, Cody

Quoted 18 lines · Cody Schafer

A large enough symbol size causes an overflow in the size parameter to the histogram allocation, leading to a segfault in symbol__inc_addr_samples later on when this histogram is accessed.

In the case of being called via perf-report, this returns back and gracefully ignores the sample, eventually ignoring the chained return value of perf_session_deliver_event in flush_sample_queue.

---
 tools/perf/util/annotate.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/annotate.c +0 -0
index 8069dfb..6f78f20 100644

tools/perf/util/annotate.c int symbol__alloc_hist(struct symbol *sym) · line 433

Quoted 9 lines
tools/perf/util/annotate.c +4 -1
@@ -426,8 +426,13 @@ int symbol__alloc_hist(struct symbol *sym)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	const size_t size = symbol__size(sym);
-	size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+	size_t sizeof_sym_hist;
 
+	/* Check for overflow when calculating sizeof_sym_hist */
+	if (size > (SIZE_MAX / sizeof(u64)))
+		return -1;

How does it guarantee that the end result which used in zalloc below would not overflow?

Thanks,
Namhyung

Quoted 5 lines
tools/perf/util/annotate.c +2 -0
+
+	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
 	notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
 	if (notes->src == NULL)
 		return -1;
#2 lore raw

PATCH v2 1/1 perf: prevent overflow in size calculation

A large enough symbol size causes an overflow in the size parameter to the histogram allocation, leading to a segfault in symbol__inc_addr_samples later on when this histogram is accessed.

In the case of being called via perf-report, this returns back and gracefully ignores the sample, eventually ignoring the chained return value of perf_session_deliver_event in flush_sample_queue.

---
 tools/perf/util/annotate.c |   13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
tools/perf/util/annotate.c +12 -1
index 8069dfb..73a1919 100644
@@ -426,7 +426,18 @@ int symbol__alloc_hist(struct symbol *sym)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	const size_t size = symbol__size(sym);
-	size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+	size_t sizeof_sym_hist;
+
+	/* Check for overflow when calculating sizeof_sym_hist */
+	if (size > (SIZE_MAX / sizeof(u64) - sizeof(struct sym_hist)))
+		return -1;
+
+	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+
+	/* Check for overflow in zalloc argument */
+	if (sizeof_sym_hist > (SIZE_MAX / symbol_conf.nr_events
+				- sizeof(*notes->src)))
+		return -1;
 
 	notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
 	if (notes->src == NULL)
Signature · 1 line
1.7.9.5
#6 lore raw
In reply to Namhyung Kim
Quoted 7 lines

tools/perf/util/annotate.c int symbol__alloc_hist(struct symbol *sym) · line 433

Quoted 3 lines
tools/perf/util/annotate.c +3 -0
+	/* Check for overflow when calculating sizeof_sym_hist */
+	if (size > (SIZE_MAX / sizeof(u64)))
+		return -1;

How does it guarantee that the end result which used in zalloc below would not overflow?

Quoted 5 lines

tools/perf/util/annotate.c int symbol__alloc_hist(struct symbol *sym) · line 438

Quoted 5 lines
tools/perf/util/annotate.c +2 -0
+
+	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
  	notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  	if (notes->src == NULL)
  		return -1;

Whoops. Thanks for pointing that out.
I've sent a fixed up patch (the check for sizeof_sym_hist wasn't even complete)

--
Cody

#3 lore raw
In reply to Cody Schafer
Quoted 10 lines
  	struct annotation *notes = symbol__annotation(sym);
  	const size_t size = symbol__size(sym);
tools/perf/util/annotate.c +5 -1
-	size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+	size_t sizeof_sym_hist;
+
+	/* Check for overflow when calculating sizeof_sym_hist */
+	if (size > (SIZE_MAX / sizeof(u64) - sizeof(struct sym_hist)))
+		return -1;

tools/perf/util/annotate.c int symbol__alloc_hist(struct symbol *sym) · line 443

Quoted 10 lines
tools/perf/util/annotate.c +7 -0
+
+	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+
+	/* Check for overflow in zalloc argument */
+	if (sizeof_sym_hist > (SIZE_MAX / symbol_conf.nr_events
+				- sizeof(*notes->src)))
+		return -1;
 
  	notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  	if (notes->src == NULL)

Actually, I don't think this is correct either (subtraction seems to occur in the wrong spot).

#4 lore raw

PATCH v3 1/1 perf: prevent overflow in size calculation

A large enough symbol size causes an overflow in the size parameter to the histogram allocation, leading to a segfault in symbol__inc_addr_samples later on when this histogram is accessed.

In the case of being called via perf-report, this returns back and gracefully ignores the sample, eventually ignoring the chained return value of perf_session_deliver_event in flush_sample_queue.

---
 tools/perf/util/annotate.c |   13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
tools/perf/util/annotate.c +12 -1
index 8069dfb..d88693e 100644
@@ -426,7 +426,18 @@ int symbol__alloc_hist(struct symbol *sym)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	const size_t size = symbol__size(sym);
-	size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+	size_t sizeof_sym_hist;
+
+	/* Check for overflow when calculating sizeof_sym_hist */
+	if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
+		return -1;
+
+	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+
+	/* Check for overflow in zalloc argument */
+	if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
+				/ symbol_conf.nr_events)
+		return -1;
 
 	notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
 	if (notes->src == NULL)
Signature · 1 line
1.7.9.5
#5 lore raw

[tip:perf/core] perf annotate: Prevent overflow in size calculation

Commit-ID:  8696329b7bcf32e69ad12d5975ad1497936d43ec
Gitweb:     http://git.kernel.org/tip/8696329b7bcf32e69ad12d5975ad1497936d43ec
Author:     Cody Schafer <[email protected]>
AuthorDate: Thu, 19 Jul 2012 20:05:25 -0700
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 25 Jul 2012 13:06:42 -0300

perf annotate: Prevent overflow in size calculation

A large enough symbol size causes an overflow in the size parameter to the histogram allocation, leading to a segfault in symbol__inc_addr_samples later on when this histogram is accessed.

In the case of being called via perf-report, this returns back and gracefully ignores the sample, eventually ignoring the chained return value of perf_session_deliver_event in flush_sample_queue.

---
 tools/perf/util/annotate.c |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)
tools/perf/util/annotate.c +12 -1
index 7d3641f..3a282c0 100644
@@ -426,7 +426,18 @@ int symbol__alloc_hist(struct symbol *sym)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	const size_t size = symbol__size(sym);
-	size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+	size_t sizeof_sym_hist;
+
+	/* Check for overflow when calculating sizeof_sym_hist */
+	if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
+		return -1;
+
+	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
+
+	/* Check for overflow in zalloc argument */
+	if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
+				/ symbol_conf.nr_events)
+		return -1;
 
 	notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
 	if (notes->src == NULL)