Re: [PATCH 1/2] perf script: Initialize buffer for regs_map()

From: Namhyung Kim
Date: Fri Jun 23 2023 - 14:03:22 EST


On Thu, Jun 22, 2023 at 5:16 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
>
> On Thu, Jun 22, 2023 at 4:54 PM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
> >
> > The buffer is used to save register mapping in a sample. Normally
> > perf samples don't have any register so the string should be empty.
> > But it missed to initialize the buffer when the size is 0. And it's
> > passed to PyUnicode_FromString() with a garbage data.
> >
> > So it returns NULL due to invalid input (instead of an empty unicode
> > string object) which causes a segfault like below:
> >
> > Thread 2.1 "perf" received signal SIGSEGV, Segmentation fault.
> > [Switching to Thread 0x7ffff7c83780 (LWP 193775)]
> > 0x00007ffff6dbca2e in PyDict_SetItem () from /lib/x86_64-linux-gnu/libpython3.11.so.1.0
> > (gdb) bt
> > #0 0x00007ffff6dbca2e in PyDict_SetItem () from /lib/x86_64-linux-gnu/libpython3.11.so.1.0
> > #1 0x00007ffff6dbf848 in PyDict_SetItemString () from /lib/x86_64-linux-gnu/libpython3.11.so.1.0
> > #2 0x000055555575824d in pydict_set_item_string_decref (val=0x0, key=0x5555557f96e3 "iregs", dict=0x7ffff5f7f780)
> > at util/scripting-engines/trace-event-python.c:145
> > #3 set_regs_in_dict (evsel=0x555555efc370, sample=0x7fffffffb870, dict=0x7ffff5f7f780)
> > at util/scripting-engines/trace-event-python.c:776
> > #4 get_perf_sample_dict (sample=sample@entry=0x7fffffffb870, evsel=evsel@entry=0x555555efc370, al=al@entry=0x7fffffffb2e0,
> > addr_al=addr_al@entry=0x0, callchain=callchain@entry=0x7ffff63ef440) at util/scripting-engines/trace-event-python.c:923
> > #5 0x0000555555758ec1 in python_process_tracepoint (sample=0x7fffffffb870, evsel=0x555555efc370, al=0x7fffffffb2e0, addr_al=0x0)
> > at util/scripting-engines/trace-event-python.c:1044
> > #6 0x00005555555c5db8 in process_sample_event (tool=<optimized out>, event=<optimized out>, sample=<optimized out>,
> > evsel=0x555555efc370, machine=0x555555ef4d68) at builtin-script.c:2421
> > #7 0x00005555556b7793 in perf_session__deliver_event (session=0x555555ef4b60, event=0x7ffff62ff7d0, tool=0x7fffffffc150,
> > file_offset=30672, file_path=0x555555efb8a0 "perf.data") at util/session.c:1639
> > #8 0x00005555556bc864 in do_flush (show_progress=true, oe=0x555555efb700) at util/ordered-events.c:245
> > #9 __ordered_events__flush (oe=oe@entry=0x555555efb700, how=how@entry=OE_FLUSH__FINAL, timestamp=timestamp@entry=0)
> > at util/ordered-events.c:324
> > #10 0x00005555556bd06e in ordered_events__flush (oe=oe@entry=0x555555efb700, how=how@entry=OE_FLUSH__FINAL)
> > at util/ordered-events.c:342
> > #11 0x00005555556b9d63 in __perf_session__process_events (session=0x555555ef4b60) at util/session.c:2465
> > #12 perf_session__process_events (session=0x555555ef4b60) at util/session.c:2627
> > #13 0x00005555555cb1d0 in __cmd_script (script=0x7fffffffc150) at builtin-script.c:2839
> > #14 cmd_script (argc=<optimized out>, argv=<optimized out>) at builtin-script.c:4365
> > #15 0x0000555555650811 in run_builtin (p=p@entry=0x555555ed8948 <commands+456>, argc=argc@entry=4, argv=argv@entry=0x7fffffffe240)
> > at perf.c:323
> > #16 0x0000555555597eb3 in handle_internal_command (argv=0x7fffffffe240, argc=4) at perf.c:377
> > #17 run_argv (argv=<synthetic pointer>, argcp=<synthetic pointer>) at perf.c:421
> > #18 main (argc=4, argv=0x7fffffffe240) at perf.c:537
> >
> > Fixes: 51cfe7a3e87e ("perf python: Avoid 2 leak sanitizer issues")
> > Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
>
> Acked-by: Ian Rogers <irogers@xxxxxxxxxx>

Applied both to perf-tools-next, thanks!