Re: [PATCH] perf parse-events: Avoid free on non malloc-ed memory

From: Arnaldo Carvalho de Melo
Date: Wed May 13 2020 - 09:31:35 EST


Em Tue, May 12, 2020 at 04:52:02PM -0700, Ian Rogers escreveu:
> Caught by libfuzzer, there is a segfault with:
> $ perf stat -e i/bs,tsc,L2/o sleep 1
> As a config_term is added that isn't a string.

Yeah, I've just applied a similar patch after looking at your private
report, added the same fixes, used your reproducer,

Thanks,

- Arnaldo

> Fixes: e8dfb81838b1 (perf parse-events: Fix memory leaks found on parse_events)
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> ---
> tools/perf/util/parse-events.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> index e9464b04f149..e37a6a3e6217 100644
> --- a/tools/perf/util/parse-events.c
> +++ b/tools/perf/util/parse-events.c
> @@ -1480,7 +1480,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
>
> list_for_each_entry_safe(pos, tmp, &config_terms, list) {
> list_del_init(&pos->list);
> - zfree(&pos->val.str);
> + if (pos->free_str)
> + zfree(&pos->val.str);
> free(pos);
> }
> return -EINVAL;
> --
> 2.26.2.645.ge9eca65c58-goog
>

--

- Arnaldo