linux-trace-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] trace-cmd record: Reset PATH variable after strtok search
@ 2023-11-28 19:24 David Vernet
  2023-11-28 19:30 ` Steven Rostedt
  0 siblings, 1 reply; 7+ messages in thread
From: David Vernet @ 2023-11-28 19:24 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: linux-trace-users, rostedt, kernel-team, julia.lawall,
	himadrispandya

execute_program(), in the trace-cmd record subcommand, searches for a
command in PATH to create an absolute path to pass to execve. The
implementation uses strtok_r, which mutates the underlying string in
place by replacing ':' tokens with NULL bytes. This can and does cause
the PATH that's passed to execve to only contain the first entry to
PATH, which can cause issues such as the following:

[root@maniforge linus]# trace-cmd record -e sched -v -e sched_stat_runtime make clean
/bin/sh: line 1: uname: command not found
/bin/sh: line 1: sed: command not found
/bin/sh: line 1: head: command not found
/bin/sh: line 1: grep: command not found
/bin/sh: line 1: mkdir: command not found
...
/bin/sh: line 1: mkdir: command not found
/bin/sh: line 1: mkdir: command not found
Makefile:681: arch//Makefile: No such file or directory
make: *** No rule to make target 'arch//Makefile'.  Stop.

We should be resetting the PATH variable to the string stored in the
saveptr argument to strtok_r.

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

diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index bced80406816..63af11ecaa80 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -1708,6 +1708,14 @@ static void execute_program(int argc, char **argv)
 				break;
 
 		}
+
+		/*
+		 * reset PATH to saveptr, as strtok_r overwrites the string
+		 * returned by getenv() which backs the PATH environment
+		 * variable.
+		 */
+		if (setenv("PATH", saveptr, 1))
+			die("Failed to reset PATH to %s (%s)", saveptr, strerror(errno));
 	} else {
 		strncpy(buf, argv[0], sizeof(buf));
 	}
-- 
2.42.1


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

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

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-28 19:24 [PATCH] trace-cmd record: Reset PATH variable after strtok search David Vernet
2023-11-28 19:30 ` Steven Rostedt
2023-11-28 20:08   ` David Vernet
2023-11-28 20:13     ` David Vernet
2023-11-28 20:18       ` Steven Rostedt
2023-11-28 20:22         ` Mathieu Desnoyers
2023-11-28 20:28           ` Steven Rostedt

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