Commit 53da12e0 authored by Jiri Olsa's avatar Jiri Olsa Committed by Arnaldo Carvalho de Melo

perf ordered_events: Prevent crossing max_alloc_size

Stephane reported a possible issue in the ordered events code, which
could lead to allocating more memory than guarded by max_alloc_size.

He also suggested the fix to properly check that the new size is below
the max_alloc_size limit.
Reported-by: default avatarStephane Eranian <eranian@google.com>
Suggested-by: default avatarStephane Eranian <eranian@google.com>
Signed-off-by: default avatarJiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20180907102455.7030-2-jolsa@kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent d5ceb62b
...@@ -101,6 +101,7 @@ static struct ordered_event *alloc_event(struct ordered_events *oe, ...@@ -101,6 +101,7 @@ static struct ordered_event *alloc_event(struct ordered_events *oe,
struct list_head *cache = &oe->cache; struct list_head *cache = &oe->cache;
struct ordered_event *new = NULL; struct ordered_event *new = NULL;
union perf_event *new_event; union perf_event *new_event;
size_t size;
new_event = dup_event(oe, event); new_event = dup_event(oe, event);
if (!new_event) if (!new_event)
...@@ -133,6 +134,8 @@ static struct ordered_event *alloc_event(struct ordered_events *oe, ...@@ -133,6 +134,8 @@ static struct ordered_event *alloc_event(struct ordered_events *oe,
* Removal of ordered event object moves it from events to * Removal of ordered event object moves it from events to
* the cache list. * the cache list.
*/ */
size = sizeof(*oe->buffer) + MAX_SAMPLE_BUFFER * sizeof(*new);
if (!list_empty(cache)) { if (!list_empty(cache)) {
new = list_entry(cache->next, struct ordered_event, list); new = list_entry(cache->next, struct ordered_event, list);
list_del(&new->list); list_del(&new->list);
...@@ -140,10 +143,7 @@ static struct ordered_event *alloc_event(struct ordered_events *oe, ...@@ -140,10 +143,7 @@ static struct ordered_event *alloc_event(struct ordered_events *oe,
new = &oe->buffer->event[oe->buffer_idx]; new = &oe->buffer->event[oe->buffer_idx];
if (++oe->buffer_idx == MAX_SAMPLE_BUFFER) if (++oe->buffer_idx == MAX_SAMPLE_BUFFER)
oe->buffer = NULL; oe->buffer = NULL;
} else if (oe->cur_alloc_size < oe->max_alloc_size) { } else if ((oe->cur_alloc_size + size) < oe->max_alloc_size) {
size_t size = sizeof(*oe->buffer) +
MAX_SAMPLE_BUFFER * sizeof(*new);
oe->buffer = malloc(size); oe->buffer = malloc(size);
if (!oe->buffer) { if (!oe->buffer) {
free_dup_event(oe, new_event); free_dup_event(oe, new_event);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment