Skip to contentSkip to conversation

trace-cmd record: Use copy of PATH for strtok_r() operations

PATCH In review Steven Rostedt 2 participants 2 messages Nov 28, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 trace-cmd record: Use copy of PATH for strtok_r() operations

From: "Steven Rostedt (Google)" <[email protected]>

As strtok_r() modifies the string it is parsing, using the environment variable PATH to find the paths for execution causes it to be truncated when reused by exec. Instead, make a copy of the PATH environment variable to use to parse the paths.

I had this fixed in my repo for some time and never pushed it out, but it was eventually reported by others.

Fixes edf9424029cc ("trace-cmd: Open code execvp routine to avoid multiple execve syscalls")
---
 tracecmd/trace-record.c | 6 ++++++
 1 file changed, 6 insertions(+)
tracecmd/trace-record.c +6 -0
index bced8040..c424a874 100644
@@ -1698,6 +1698,11 @@ static void execute_program(int argc, char **argv)
 		if (!path)
 			die("can't search for '%s' if $PATH is NULL", argv[0]);
 
+		/* Do not modify the actual environment variable */
+		path = strdup(path);
+		if (!path)
+			die("Failed to allocate PATH");
+
 		for (entry = strtok_r(path, ":", &saveptr);
 		     entry; entry = strtok_r(NULL, ":", &saveptr)) {
 
@@ -1708,6 +1713,7 @@ static void execute_program(int argc, char **argv)
 				break;
 
 		}
+		free(path);
 	} else {
 		strncpy(buf, argv[0], sizeof(buf));
 	}
Signature · 1 line
2.42.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

As strtok_r() modifies the string it is parsing, using the environment variable PATH to find the paths for execution causes it to be truncated when reused by exec. Instead, make a copy of the PATH environment variable to use to parse the paths.

Quoted 8 lines

I had this fixed in my repo for some time and never pushed it out, but it was eventually reported by others.

Fixes edf9424029cc ("trace-cmd: Open code execvp routine to avoid multiple execve syscalls")