Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf annotate: Avoid TUI crash when navigating in the annotation of recursive functions

In 'perf report', entering a recursive function from inside of itself
(either directly of indirectly through some other function) results in
calling symbol__annotate2 multiple() times, and freeing the whole
disassembly when exiting from the innermost instance.

The first issue causes the function's disassembly to be duplicated, and
the latter a heap use-after-free (and crash) when trying to access the
disassembly again.

I reproduced the bug on perf 5.11.22 (Ubuntu 20.04.3 LTS) and 5.16.rc8
with the following testcase (compile with gcc recursive.c -o recursive).
To reproduce:

- perf record ./recursive
- perf report
- enter fibonacci and annotate it
- move the cursor on one of the "callq fibonacci" instructions and press enter
- at this point there will be two copies of the function in the disassembly
- go back by pressing q, and perf will crash

#include <stdio.h>

int fibonacci(int n)
{
if(n <= 2) return 1;
return fibonacci(n-1) + fibonacci(n-2);
}

int main()
{
printf("%d\n", fibonacci(40));
}

This patch addresses the issue by annotating a function and freeing the
associated memory on exit only if no annotation is already present, so
that a recursive function is only annotated on entry.

Signed-off-by: Dario Petrillo <dario.pk1@gmail.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: stable@kernel.org
Link: http://lore.kernel.org/lkml/20220109234441.325106-1-dario.pk1@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Dario Petrillo and committed by
Arnaldo Carvalho de Melo
d5962fb7 befee377

+14 -9
+14 -9
tools/perf/ui/browsers/annotate.c
··· 966 966 .opts = opts, 967 967 }; 968 968 int ret = -1, err; 969 + int not_annotated = list_empty(&notes->src->source); 969 970 970 971 if (sym == NULL) 971 972 return -1; ··· 974 973 if (ms->map->dso->annotate_warned) 975 974 return -1; 976 975 977 - err = symbol__annotate2(ms, evsel, opts, &browser.arch); 978 - if (err) { 979 - char msg[BUFSIZ]; 980 - ms->map->dso->annotate_warned = true; 981 - symbol__strerror_disassemble(ms, err, msg, sizeof(msg)); 982 - ui__error("Couldn't annotate %s:\n%s", sym->name, msg); 983 - goto out_free_offsets; 976 + if (not_annotated) { 977 + err = symbol__annotate2(ms, evsel, opts, &browser.arch); 978 + if (err) { 979 + char msg[BUFSIZ]; 980 + ms->map->dso->annotate_warned = true; 981 + symbol__strerror_disassemble(ms, err, msg, sizeof(msg)); 982 + ui__error("Couldn't annotate %s:\n%s", sym->name, msg); 983 + goto out_free_offsets; 984 + } 984 985 } 985 986 986 987 ui_helpline__push("Press ESC to exit"); ··· 997 994 998 995 ret = annotate_browser__run(&browser, evsel, hbt); 999 996 1000 - annotated_source__purge(notes->src); 997 + if(not_annotated) 998 + annotated_source__purge(notes->src); 1001 999 1002 1000 out_free_offsets: 1003 - zfree(&notes->offsets); 1001 + if(not_annotated) 1002 + zfree(&notes->offsets); 1004 1003 return ret; 1005 1004 }