Skip to content

Commit d72e5cf

Browse files
captain5050acmel
authored andcommitted
perf sched: Fix memory leaks in __cmd_record detected with -fsanitize=address
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> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Link: https://lore.kernel.org/r/20220824145733.409005-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent e89eaa6 commit d72e5cf

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

tools/perf/builtin-sched.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,7 +3355,8 @@ static bool schedstat_events_exposed(void)
33553355
static int __cmd_record(int argc, const char **argv)
33563356
{
33573357
unsigned int rec_argc, i, j;
3358-
const char **rec_argv;
3358+
char **rec_argv;
3359+
const char **rec_argv_copy;
33593360
const char * const record_args[] = {
33603361
"record",
33613362
"-a",
@@ -3384,21 +3385,26 @@ static int __cmd_record(int argc, const char **argv)
33843385
ARRAY_SIZE(schedstat_args) : 0;
33853386

33863387
struct tep_event *waking_event;
3388+
int ret;
33873389

33883390
/*
33893391
* +2 for either "-e", "sched:sched_wakeup" or
33903392
* "-e", "sched:sched_waking"
33913393
*/
33923394
rec_argc = ARRAY_SIZE(record_args) + 2 + schedstat_argc + argc - 1;
33933395
rec_argv = calloc(rec_argc + 1, sizeof(char *));
3394-
33953396
if (rec_argv == NULL)
33963397
return -ENOMEM;
3398+
rec_argv_copy = calloc(rec_argc + 1, sizeof(char *));
3399+
if (rec_argv_copy == NULL) {
3400+
free(rec_argv);
3401+
return -ENOMEM;
3402+
}
33973403

33983404
for (i = 0; i < ARRAY_SIZE(record_args); i++)
33993405
rec_argv[i] = strdup(record_args[i]);
34003406

3401-
rec_argv[i++] = "-e";
3407+
rec_argv[i++] = strdup("-e");
34023408
waking_event = trace_event__tp_format("sched", "sched_waking");
34033409
if (!IS_ERR(waking_event))
34043410
rec_argv[i++] = strdup("sched:sched_waking");
@@ -3409,11 +3415,19 @@ static int __cmd_record(int argc, const char **argv)
34093415
rec_argv[i++] = strdup(schedstat_args[j]);
34103416

34113417
for (j = 1; j < (unsigned int)argc; j++, i++)
3412-
rec_argv[i] = argv[j];
3418+
rec_argv[i] = strdup(argv[j]);
34133419

34143420
BUG_ON(i != rec_argc);
34153421

3416-
return cmd_record(i, rec_argv);
3422+
memcpy(rec_argv_copy, rec_argv, sizeof(char *) * rec_argc);
3423+
ret = cmd_record(rec_argc, rec_argv_copy);
3424+
3425+
for (i = 0; i < rec_argc; i++)
3426+
free(rec_argv[i]);
3427+
free(rec_argv);
3428+
free(rec_argv_copy);
3429+
3430+
return ret;
34173431
}
34183432

34193433
int cmd_sched(int argc, const char **argv)

0 commit comments

Comments
 (0)