linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] trace-cmd record: Use copy of PATH for strtok_r() operations
@ 2023-11-28 20:30 Steven Rostedt
  2023-11-28 20:33 ` David Vernet
  0 siblings, 1 reply; 2+ messages in thread
From: Steven Rostedt @ 2023-11-28 20:30 UTC (permalink / raw)
  To: Linux Trace Devel
  Cc: David Vernet, kernel-team, julia.lawall, himadrispandya,
	Mathieu Desnoyers

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

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.

Link: https://lore.kernel.org/all/20231128192435.36507-1-void@manifault.com/

Reported-by: David Vernet <void@manifault.com>
Fixes: edf9424029cc ("trace-cmd: Open code execvp routine to avoid multiple execve syscalls")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 tracecmd/trace-record.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index bced8040..c424a874 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -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));
 	}
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] trace-cmd record: Use copy of PATH for strtok_r() operations
  2023-11-28 20:30 [PATCH] trace-cmd record: Use copy of PATH for strtok_r() operations Steven Rostedt
@ 2023-11-28 20:33 ` David Vernet
  0 siblings, 0 replies; 2+ messages in thread
From: David Vernet @ 2023-11-28 20:33 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Linux Trace Devel, kernel-team, julia.lawall, himadrispandya,
	Mathieu Desnoyers

On Tue, Nov 28, 2023 at 03:30:24PM -0500, Steven Rostedt wrote:
> From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> 
> 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.
> 
> Link: https://lore.kernel.org/all/20231128192435.36507-1-void@manifault.com/
> 
> Reported-by: David Vernet <void@manifault.com>
> Fixes: edf9424029cc ("trace-cmd: Open code execvp routine to avoid multiple execve syscalls")
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Reviewed-by: David Vernet <void@manifault.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-11-28 20:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-28 20:30 [PATCH] trace-cmd record: Use copy of PATH for strtok_r() operations Steven Rostedt
2023-11-28 20:33 ` David Vernet

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).