All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Linux Trace Devel <linux-trace-devel@vger.kernel.org>
Cc: David Vernet <void@manifault.com>,
	kernel-team@meta.com, julia.lawall@inria.fr,
	himadrispandya@gmail.com,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Subject: [PATCH] trace-cmd record: Use copy of PATH for strtok_r() operations
Date: Tue, 28 Nov 2023 15:30:24 -0500	[thread overview]
Message-ID: <20231128153024.6e0d40e1@gandalf.local.home> (raw)

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


             reply	other threads:[~2023-11-28 20:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-28 20:30 Steven Rostedt [this message]
2023-11-28 20:33 ` [PATCH] trace-cmd record: Use copy of PATH for strtok_r() operations David Vernet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231128153024.6e0d40e1@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=himadrispandya@gmail.com \
    --cc=julia.lawall@inria.fr \
    --cc=kernel-team@meta.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=void@manifault.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.