Skip to contentSkip to conversation

perf sched: Fix memory leaks in __cmd_record

PATCH Superseded Ian Rogers 2 participants 3 messages Aug 24, 2022 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf sched: Fix memory leaks in __cmd_record

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.

---
 tools/perf/builtin-sched.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
tools/perf/builtin-sched.c +16 -5
index 2f6cd1b8b662..59ba14d2321c 100644
@@ -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;
 
 	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)
Signature · 1 line
2.37.2.609.g9ff673ca1a-goog
#1 lore raw

Em Tue, Aug 23, 2022 at 09:38:25PM -0700, Ian Rogers escreveu:

Quoted 34 lines

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.

---
 tools/perf/builtin-sched.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
tools/perf/builtin-sched.c +3 -1
index 2f6cd1b8b662..59ba14d2321c 100644
@@ -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

tools/perf/builtin-sched.c static int __cmd_record(int argc, const char **argv) · line 3399

Quoted 8 lines
tools/perf/builtin-sched.c +2 -1
@@ -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?

- Arnaldo

Quoted 33 lines
tools/perf/builtin-sched.c +11 -3
 	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)
Signature · 1 line
2.37.2.609.g9ff673ca1a-goog
Signature · 1 line
- Arnaldo
author reviewer
#2 lore raw
Quoted 41 lines · Arnaldo Carvalho de Melo

Em Tue, Aug 23, 2022 at 09:38:25PM -0700, Ian Rogers escreveu:

Quoted 39 lines

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.

---
 tools/perf/builtin-sched.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
tools/perf/builtin-sched.c +4 -1
index 2f6cd1b8b662..59ba14d2321c 100644
@@ -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 *));
 
Quoted 5 lines

tools/perf/builtin-sched.c static int __cmd_record(int argc, const char **argv) · line 3399

Quoted 3 lines
tools/perf/builtin-sched.c +1 -1
-     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

Quoted 39 lines

- Arnaldo

Quoted 32 lines
tools/perf/builtin-sched.c +11 -4
      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)
--
Quoted 1 line

2.37.2.609.g9ff673ca1a-goog

--

- Arnaldo