From: Ian Rogers <irogers@google.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
Stephane Eranian <eranian@google.com>
Subject: Re: [PATCH] perf sched: Fix memory leaks in __cmd_record
Date: Wed, 24 Aug 2022 07:59:42 -0700 [thread overview]
Message-ID: <CAP-5=fUp5Cf+yfid70OzOAPOm2CJh84RN_NvW9aRhUAX5RPMGg@mail.gmail.com> (raw)
In-Reply-To: <YwYpiuN3VlzVyCiw@kernel.org>
On Wed, Aug 24, 2022 at 6:37 AM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Em Tue, Aug 23, 2022 at 09:38:25PM -0700, Ian Rogers escreveu:
> > An array of strings is passed to cmd_record but not freed. As
> > cmd_record modifies the array, add another array as a copy that can be
> > mutated allowing the original array contents to all be freed.
> >
> > Detected with -fsanitize=address.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> > tools/perf/builtin-sched.c | 21 ++++++++++++++++-----
> > 1 file changed, 16 insertions(+), 5 deletions(-)
> >
> > diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> > index 2f6cd1b8b662..59ba14d2321c 100644
> > --- a/tools/perf/builtin-sched.c
> > +++ b/tools/perf/builtin-sched.c
> > @@ -3355,7 +3355,8 @@ static bool schedstat_events_exposed(void)
> > static int __cmd_record(int argc, const char **argv)
> > {
> > unsigned int rec_argc, i, j;
> > - const char **rec_argv;
> > + char **rec_argv;
> > + const char **rec_argv_copy;
> > const char * const record_args[] = {
> > "record",
> > "-a",
> > @@ -3384,6 +3385,7 @@ static int __cmd_record(int argc, const char **argv)
> > ARRAY_SIZE(schedstat_args) : 0;
> >
> > struct tep_event *waking_event;
> > + int ret;
> >
> > /*
> > * +2 for either "-e", "sched:sched_wakeup" or
> > @@ -3391,14 +3393,15 @@ static int __cmd_record(int argc, const char **argv)
> > */
> > rec_argc = ARRAY_SIZE(record_args) + 2 + schedstat_argc + argc - 1;
> > rec_argv = calloc(rec_argc + 1, sizeof(char *));
> > + rec_argv_copy = calloc(rec_argc + 1, sizeof(char *));
> >
> > - if (rec_argv == NULL)
> > + if (rec_argv == NULL || rec_argv_copy == NULL)
> > return -ENOMEM;
>
> Here you´re leaking rec_argv if rec_argv_copy fails to be allocated, no?
Done in v2.
Thanks,
Ian
> - Arnaldo
>
> >
> > for (i = 0; i < ARRAY_SIZE(record_args); i++)
> > rec_argv[i] = strdup(record_args[i]);
> >
> > - rec_argv[i++] = "-e";
> > + rec_argv[i++] = strdup("-e");
> > waking_event = trace_event__tp_format("sched", "sched_waking");
> > if (!IS_ERR(waking_event))
> > rec_argv[i++] = strdup("sched:sched_waking");
> > @@ -3409,11 +3412,19 @@ static int __cmd_record(int argc, const char **argv)
> > rec_argv[i++] = strdup(schedstat_args[j]);
> >
> > for (j = 1; j < (unsigned int)argc; j++, i++)
> > - rec_argv[i] = argv[j];
> > + rec_argv[i] = strdup(argv[j]);
> >
> > BUG_ON(i != rec_argc);
> >
> > - return cmd_record(i, rec_argv);
> > + memcpy(rec_argv_copy, rec_argv, sizeof(char*) * rec_argc);
> > + ret = cmd_record(rec_argc, rec_argv_copy);
> > +
> > + for (i = 0; i < rec_argc; i++)
> > + free(rec_argv[i]);
> > + free(rec_argv);
> > + free(rec_argv_copy);
> > +
> > + return ret;
> > }
> >
> > int cmd_sched(int argc, const char **argv)
> > --
> > 2.37.2.609.g9ff673ca1a-goog
>
> --
>
> - Arnaldo
prev parent reply other threads:[~2022-08-24 14:59 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-24 4:38 [PATCH] perf sched: Fix memory leaks in __cmd_record Ian Rogers
2022-08-24 13:37 ` Arnaldo Carvalho de Melo
2022-08-24 14:59 ` Ian Rogers [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAP-5=fUp5Cf+yfid70OzOAPOm2CJh84RN_NvW9aRhUAX5RPMGg@mail.gmail.com' \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=eranian@google.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).