Skip to content

Commit 53da12e

Browse files
olsajiriacmel
authored andcommitted
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: Stephane Eranian <eranian@google.com> Suggested-by: Stephane Eranian <eranian@google.com> Signed-off-by: Jiri 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.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent d5ceb62 commit 53da12e

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎tools/perf/util/ordered-events.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static struct ordered_event *alloc_event(struct ordered_events *oe,
101101
struct list_head *cache = &oe->cache;
102102
struct ordered_event *new = NULL;
103103
union perf_event *new_event;
104+
size_t size;
104105

105106
new_event = dup_event(oe, event);
106107
if (!new_event)
@@ -133,17 +134,16 @@ static struct ordered_event *alloc_event(struct ordered_events *oe,
133134
* Removal of ordered event object moves it from events to
134135
* the cache list.
135136
*/
137+
size = sizeof(*oe->buffer) + MAX_SAMPLE_BUFFER * sizeof(*new);
138+
136139
if (!list_empty(cache)) {
137140
new = list_entry(cache->next, struct ordered_event, list);
138141
list_del(&new->list);
139142
} else if (oe->buffer) {
140143
new = &oe->buffer->event[oe->buffer_idx];
141144
if (++oe->buffer_idx == MAX_SAMPLE_BUFFER)
142145
oe->buffer = NULL;
143-
} else if (oe->cur_alloc_size < oe->max_alloc_size) {
144-
size_t size = sizeof(*oe->buffer) +
145-
MAX_SAMPLE_BUFFER * sizeof(*new);
146-
146+
} else if ((oe->cur_alloc_size + size) < oe->max_alloc_size) {
147147
oe->buffer = malloc(size);
148148
if (!oe->buffer) {
149149
free_dup_event(oe, new_event);

0 commit comments

Comments
 (0)