Skip to contentSkip to conversation

perf tools: Fix perf_evlist__mmap_read event overflow

PATCH In review Jiri Olsa 3 participants 3 messages Oct 2, 2013 – Oct 15, 2013 perf events

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/1 perf tools: Fix perf_evlist__mmap_read event overflow

The perf_evlist__mmap_read used 'union perf_event' as a placeholder for event crossing the mmap boundary.

This is ok for sample shorter than ~PATH_MAX. However we could grow up to the maximum sample size which is 16 bits max.

I hit this overflow issue when using 'perf top -G dwarf' which produces sample with the size around 8192 bytes. We could configure any valid sample size here using:
'-G dwarf,size'.

Using array with sample max size instead for the event placeholder. Also adding another safe check for the dynamic size of the user stack.

TODO: The 'struct perf_mmap' is quite big now, maybe we could use some lazy allocation for event_copy size.

Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 tools/perf/util/event.h  | 3 +++
 tools/perf/util/evlist.c | 4 ++--
 tools/perf/util/evlist.h | 2 +-
 tools/perf/util/evsel.c  | 3 +++
 4 files changed, 9 insertions(+), 3 deletions(-)
tools/perf/util/event.h +3 -0
index 9f50f88..2b8032a 100644
@@ -75,6 +75,9 @@ struct throttle_event {
 	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
 	 PERF_SAMPLE_IDENTIFIER)
 
+/* perf sample has 16 bits size limit */
+#define PERF_SAMPLE_MAX_SIZE (1 << 16)
+
 struct sample_event {
 	struct perf_event_header        header;
 	u64 array[];
tools/perf/util/evlist.c +2 -2
index f0d71a9..cb9523f 100644
@@ -540,7 +540,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
 			unsigned int offset = old;
 			unsigned int len = min(sizeof(*event), size), cpy;
-			void *dst = &md->event_copy;
+			void *dst = md->event_copy;
 
 			do {
 				cpy = min(md->mask + 1 - (offset & md->mask), len);
@@ -550,7 +550,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 				len -= cpy;
 			} while (len);
 
-			event = &md->event_copy;
+			event = (union perf_event *) md->event_copy;
 		}
 
 		old += size;
tools/perf/util/evlist.h +1 -1
index 871b55a..722618f 100644
@@ -21,7 +21,7 @@ struct perf_mmap {
 	void		 *base;
 	int		 mask;
 	unsigned int	 prev;
-	union perf_event event_copy;
+	char		 event_copy[PERF_SAMPLE_MAX_SIZE];
 };
 
 struct perf_evlist {
tools/perf/util/evsel.c +3 -0
index 0ce9feb..aa20ee2 100644
@@ -1453,6 +1453,9 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
 			array = (void *)array + sz;
 			OVERFLOW_CHECK_u64(array);
 			data->user_stack.size = *array++;
+			if (WARN_ONCE(data->user_stack.size > sz,
+				      "user stack dump failure\n"))
+				return -EFAULT;
 		}
 	}
 
Signature · 1 line
1.7.11.7
Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Jiri Olsa

The perf_evlist__mmap_read used 'union perf_event' as a placeholder for event crossing the mmap boundary.

This is ok for sample shorter than ~PATH_MAX. However we could grow up to the maximum sample size which is 16 bits max.

I hit this overflow issue when using 'perf top -G dwarf' which produces sample with the size around 8192 bytes. We could configure any valid sample size here using:
'-G dwarf,size'.

Quoted 6 lines

Using array with sample max size instead for the event placeholder. Also adding another safe check for the dynamic size of the user stack.

TODO: The 'struct perf_mmap' is quite big now, maybe we could use some lazy allocation for event_copy size.

I was going to suggest just that -- dynamically handle the use of event_copy. The below adds ~60k per mmap. Otherwise...

Quoted 81 lines
Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
  tools/perf/util/event.h  | 3 +++
  tools/perf/util/evlist.c | 4 ++--
  tools/perf/util/evlist.h | 2 +-
  tools/perf/util/evsel.c  | 3 +++
  4 files changed, 9 insertions(+), 3 deletions(-)
tools/perf/util/event.h +3 -0
index 9f50f88..2b8032a 100644
@@ -75,6 +75,9 @@ struct throttle_event {
  	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
  	 PERF_SAMPLE_IDENTIFIER)
 
+/* perf sample has 16 bits size limit */
+#define PERF_SAMPLE_MAX_SIZE (1 << 16)
+
  struct sample_event {
  	struct perf_event_header        header;
  	u64 array[];
tools/perf/util/evlist.c +2 -2
index f0d71a9..cb9523f 100644
@@ -540,7 +540,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
  		if ((old & md->mask) + size != ((old + size) & md->mask)) {
  			unsigned int offset = old;
  			unsigned int len = min(sizeof(*event), size), cpy;
-			void *dst = &md->event_copy;
+			void *dst = md->event_copy;
 
  			do {
  				cpy = min(md->mask + 1 - (offset & md->mask), len);
@@ -550,7 +550,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
  				len -= cpy;
  			} while (len);
 
-			event = &md->event_copy;
+			event = (union perf_event *) md->event_copy;
  		}
 
  		old += size;
tools/perf/util/evlist.h +1 -1
index 871b55a..722618f 100644
@@ -21,7 +21,7 @@ struct perf_mmap {
  	void		 *base;
  	int		 mask;
  	unsigned int	 prev;
-	union perf_event event_copy;
+	char		 event_copy[PERF_SAMPLE_MAX_SIZE];
  };
 
  struct perf_evlist {
tools/perf/util/evsel.c +3 -0
index 0ce9feb..aa20ee2 100644
@@ -1453,6 +1453,9 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
  			array = (void *)array + sz;
  			OVERFLOW_CHECK_u64(array);
  			data->user_stack.size = *array++;
+			if (WARN_ONCE(data->user_stack.size > sz,
+				      "user stack dump failure\n"))
+				return -EFAULT;
  		}
  	}
 
#2 lore raw

[tip:perf/core] perf evlist: Fix perf_evlist__mmap_read event overflow

In reply to Jiri Olsa
perf evlist: Fix perf_evlist__mmap_read event overflow
Commit-ID:  a65cb4b9f8a777a715371c63c0525408048cea3e
Gitweb:     http://git.kernel.org/tip/a65cb4b9f8a777a715371c63c0525408048cea3e
Author:     Jiri Olsa <[email protected]>
AuthorDate: Wed, 2 Oct 2013 15:46:39 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 11 Oct 2013 12:17:40 -0300

perf evlist: Fix perf_evlist__mmap_read event overflow

The perf_evlist__mmap_read used 'union perf_event' as a placeholder for event crossing the mmap boundary.

This is ok for sample shorter than ~PATH_MAX. However we could grow up to the maximum sample size which is 16 bits max.

I hit this overflow issue when using 'perf top -G dwarf' which produces sample with the size around 8192 bytes. We could configure any valid sample size here using: '-G dwarf,size'.

Using array with sample max size instead for the event placeholder. Also adding another safe check for the dynamic size of the user stack.

TODO: The 'struct perf_mmap' is quite big now, maybe we could use some lazy allocation for event_copy size.

---
 tools/perf/util/event.h  | 3 +++
 tools/perf/util/evlist.c | 4 ++--
 tools/perf/util/evlist.h | 2 +-
 tools/perf/util/evsel.c  | 3 +++
 4 files changed, 9 insertions(+), 3 deletions(-)
tools/perf/util/event.h +3 -0
index 9b7d4d3..752709c 100644
@@ -75,6 +75,9 @@ struct throttle_event {
 	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
 	 PERF_SAMPLE_IDENTIFIER)
 
+/* perf sample has 16 bits size limit */
+#define PERF_SAMPLE_MAX_SIZE (1 << 16)
+
 struct sample_event {
 	struct perf_event_header        header;
 	u64 array[];
tools/perf/util/evlist.c +2 -2
index f0d71a9..cb9523f 100644
@@ -540,7 +540,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
 			unsigned int offset = old;
 			unsigned int len = min(sizeof(*event), size), cpy;
-			void *dst = &md->event_copy;
+			void *dst = md->event_copy;
 
 			do {
 				cpy = min(md->mask + 1 - (offset & md->mask), len);
@@ -550,7 +550,7 @@ union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 				len -= cpy;
 			} while (len);
 
-			event = &md->event_copy;
+			event = (union perf_event *) md->event_copy;
 		}
 
 		old += size;
tools/perf/util/evlist.h +1 -1
index 871b55a..722618f 100644
@@ -21,7 +21,7 @@ struct perf_mmap {
 	void		 *base;
 	int		 mask;
 	unsigned int	 prev;
-	union perf_event event_copy;
+	char		 event_copy[PERF_SAMPLE_MAX_SIZE];
 };
 
 struct perf_evlist {
tools/perf/util/evsel.c +3 -0
index abe69af..bfebc1e 100644
@@ -1456,6 +1456,9 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
 			array = (void *)array + sz;
 			OVERFLOW_CHECK_u64(array);
 			data->user_stack.size = *array++;
+			if (WARN_ONCE(data->user_stack.size > sz,
+				      "user stack dump failure\n"))
+				return -EFAULT;
 		}
 	}