From: David Vernet <void@manifault.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: linux-trace-devel@vger.kernel.org,
linux-trace-users@vger.kernel.org, kernel-team@meta.com,
julia.lawall@inria.fr, himadrispandya@gmail.com
Subject: Re: [PATCH] trace-cmd record: Reset PATH variable after strtok search
Date: Tue, 28 Nov 2023 14:08:56 -0600 [thread overview]
Message-ID: <20231128200856.GA431764@maniforge> (raw)
In-Reply-To: <20231128143044.72f0925e@gandalf.local.home>
On Tue, Nov 28, 2023 at 02:30:44PM -0500, Steven Rostedt wrote:
> On Tue, 28 Nov 2023 13:24:35 -0600
> David Vernet <void@manifault.com> wrote:
>
> > 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.
>
> Bah, I had this fixed locally, but never made pushed it up.
>
> >
> > 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));
> > }
>
> The fix I had was this:
>
>
> diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
> index bced8040..1a461631 100644
> --- a/tracecmd/trace-record.c
> +++ b/tracecmd/trace-record.c
> @@ -1698,6 +1698,9 @@ 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);
> +
> for (entry = strtok_r(path, ":", &saveptr);
> entry; entry = strtok_r(NULL, ":", &saveptr)) {
>
> @@ -1712,6 +1715,7 @@ static void execute_program(int argc, char **argv)
> strncpy(buf, argv[0], sizeof(buf));
> }
>
> + free(path);
> tracecmd_enable_tracing();
> if (execve(buf, argv, environ)) {
> fprintf(stderr, "\n********************\n");
>
> Does that work for you?
That would work too, though I don't think strtok_r() is doing anything
useful at that point. IMO it's better to either do the setenv() with
saveptr, or change that strtok_r() to a regular strtok().
>
> Although, I still need to test the result of strdup().
>
> -- Steve
next prev parent reply other threads:[~2023-11-28 20:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=20231128200856.GA431764@maniforge \
--to=void@manifault.com \
--cc=himadrispandya@gmail.com \
--cc=julia.lawall@inria.fr \
--cc=kernel-team@meta.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=linux-trace-users@vger.kernel.org \
--cc=rostedt@goodmis.org \
/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 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).