From 56441d9f9ae2def351594dc19a5df5f3fc929bff Mon Sep 17 00:00:00 2001 From: John Reiser Date: Tue, 4 Apr 2023 11:27:53 -0700 Subject: Introduce --argv0 option This option is useful for tracing multi-call executables which interpret argv[0], such as busybox or kmod. * NEWS: Mention this. * doc/strace.1.in: Document this. * src/strace.c (usage): Likewise. (argv0): New variable. (startup_child): Use it. (GETOPT_ARGV0): New enum constant. (longopts, init): Use it. Co-authored-by: Dmitry V. Levin Resolves: https://github.com/strace/strace/pull/248 --- NEWS | 1 + doc/strace.1.in | 6 ++++++ src/strace.c | 13 +++++++++++++ 3 files changed, 20 insertions(+) diff --git a/NEWS b/NEWS index bcb3c1a94..e1d3f12c8 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ Noteworthy changes in release ?.? (????-??-??) that operate on the specified set of file descriptors. * Implemented --syscall-limit option to automatically detach tracees after capturing the specified number of syscalls. + * Implemented --argv0 option to set argv[0] of the command being executed. * Implemented decoding of PR_GET_MDWE and PR_SET_MDWE operations of prctl syscall. * Implemented decoding of IP_LOCAL_PORT_RANGE socket option. diff --git a/doc/strace.1.in b/doc/strace.1.in index 986e97eee..4d5730ed6 100644 --- a/doc/strace.1.in +++ b/doc/strace.1.in @@ -353,6 +353,12 @@ This option is only useful when running as root and enables the correct execution of setuid and/or setgid binaries. Unless this option is used setuid and setgid programs are executed without effective privileges. +.TP +.BR "\-\-argv0" = \fIname\fR +Set argv[0] of the command being executed to +.IR name . +Useful for tracing multi-call executables which interpret argv[0], +such as busybox or kmod. .SS Tracing .TP 12 .BI "\-b " syscall diff --git a/src/strace.c b/src/strace.c index 265c0c6dc..85d69485f 100644 --- a/src/strace.c +++ b/src/strace.c @@ -186,6 +186,8 @@ static size_t tcb_wait_tab_size; char *program_invocation_name; #endif +char *argv0; /* override argv[0] on execve */ + unsigned os_release; /* generated from uname()'s u.release */ static void detach(struct tcb *tcp); @@ -303,6 +305,7 @@ Startup:\n\ trace process with process id PID, may be repeated\n\ -u USERNAME, --user=USERNAME\n\ run command as USERNAME handling setuid and/or setgid\n\ + --argv0=NAME set PROG argv[0] to NAME\n\ \n\ Tracing:\n\ -b execve, --detach-on=execve\n\ @@ -1675,6 +1678,8 @@ startup_child(char **argv, char **env) params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid; params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid; params_for_tracee.argv = argv; + if (argv0) + params_for_tracee.argv[0] = argv0; params_for_tracee.env = env; /* * On NOMMU, can be safely freed only after execve in tracee. @@ -2266,6 +2271,7 @@ init(int argc, char *argv[]) GETOPT_SYSCALL_LIMIT, GETOPT_TS, GETOPT_TIPS, + GETOPT_ARGV0, GETOPT_QUAL_TRACE, GETOPT_QUAL_TRACE_FD, @@ -2327,6 +2333,7 @@ init(int argc, char *argv[]) { "failing-only", no_argument, 0, 'Z' }, { "seccomp-bpf", no_argument, 0, GETOPT_SECCOMP }, { "tips", optional_argument, 0, GETOPT_TIPS }, + { "argv0", required_argument, 0, GETOPT_ARGV0 }, { "trace", required_argument, 0, GETOPT_QUAL_TRACE }, { "trace-fds", required_argument, 0, GETOPT_QUAL_TRACE_FD }, @@ -2564,6 +2571,9 @@ init(int argc, char *argv[]) if (parse_tips_arg(optarg ?: "")) error_opt_arg(c, lopt, optarg); break; + case GETOPT_ARGV0: + argv0 = optarg; + break; case GETOPT_QUAL_SECONTEXT: qualify_secontext(optarg ? optarg : secontext_qual); break; @@ -2634,6 +2644,9 @@ init(int argc, char *argv[]) error_msg_and_help("must have PROG [ARGS] or -p PID"); } + if (!argc && argv0) + error_msg_and_help("PROG [ARGS] must be specified with --argv0"); + if (daemonized_tracer_long) { if (daemonized_tracer) { error_msg_and_die("-D and --daemonize cannot" -- cgit v1.2.1