diff mbox series

trace-cmd: Limit the size written into the pid mapname

Message ID 20190830125609.2397b21a@gandalf.local.home (mailing list archive)
State Accepted
Commit 3a6ded6c5fad7c152a92980bb4a93c6b4d422fd3
Headers show
Series trace-cmd: Limit the size written into the pid mapname | expand

Commit Message

Steven Rostedt Aug. 30, 2019, 4:56 p.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

Need to tell scanf() the size of the mapname so that we don't risk a buffer
overflow. As STRINGIFY() will make a string from the size, we can't use
"PATH_MAX + 22", but 4096 should be plenty big enough.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 lib/trace-cmd/trace-input.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index 8cceb31c..1db1bffa 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -20,6 +20,9 @@ 
 #include "kbuffer.h"
 #include "list.h"
 
+#define _STRINGIFY(x) #x
+#define STRINGIFY(x) _STRINGIFY(x)
+
 #define MISSING_EVENTS (1 << 31)
 #define MISSING_STORED (1 << 30)
 
@@ -2164,11 +2167,12 @@  static void procmap_free(struct pid_addr_maps *maps)
 	free(maps);
 }
 
-#define STR_PROCMAP_LINE_MAX	(PATH_MAX+22)
+/* Needs to be a constant, and 4K should be good enough */
+#define STR_PROCMAP_LINE_MAX	4096
 static int trace_pid_map_load(struct tracecmd_input *handle, char *buf)
 {
 	struct pid_addr_maps *maps = NULL;
-	char mapname[STR_PROCMAP_LINE_MAX];
+	char mapname[STR_PROCMAP_LINE_MAX+1];
 	char *line;
 	int res;
 	int ret;
@@ -2187,7 +2191,7 @@  static int trace_pid_map_load(struct tracecmd_input *handle, char *buf)
 	if (strlen(buf) > STR_PROCMAP_LINE_MAX)
 		goto out_fail;
 
-	res = sscanf(buf, "%x %x %s", &maps->pid, &maps->nr_lib_maps, mapname);
+	res = sscanf(buf, "%x %x %"STRINGIFY(STR_PROCMAP_LINE_MAX)"s", &maps->pid, &maps->nr_lib_maps, mapname);
 	if (res != 3)
 		goto out_fail;