Commit Graph

67 Commits

Author SHA1 Message Date
Todd C. Miller
8db1eb0c8b Update the cwd for log_subcmds too.
Fixes a problem for intercept_method=trace when running a relative
command from a different directory than what sudo ws started from.
GitHub issue #194
2022-11-07 12:14:15 -07:00
Todd C. Miller
7944494196 Fix compilation error on Linux/mips. 2022-10-26 16:35:30 -06:00
Todd C. Miller
a6472710e6 Zero out register struct before calling ptrace_getregs().
Quiets a spurious valgrind warning.
2022-08-30 07:35:43 -06:00
Todd C. Miller
817f63b05d Realloc the buffer used to store argv and envp as needed.
We now store the vector immediately after the string table.
It is possible for argv and its contents to be invalidated
by realloc() when reading envp so we store the pointers as
offsets until we are done allocating.
2022-08-29 12:47:42 -06:00
Todd C. Miller
0d2f1c4a2b ptrace_verify_post_exec: use /proc/PID/cmdline and /proc/PID/environ
There is no reason to read these directly from the tracee
when we rely on /proc being mounted to access /proc/PID/exe.
2022-08-28 18:59:41 -06:00
Todd C. Miller
234c56b87f Protect ptrace_readv_string() with #ifdef HAVE_PROCESS_VM_READV 2022-08-28 08:27:30 -06:00
Todd C. Miller
14356c1940 Use process_vm_readv(2) and process_vm_writev(2) if available.
This is faster than reading/writing from/to the remote process one
word at a time using PTRACE_PEEKDATA and PTRACE_POKEDATA.
2022-08-25 12:37:19 -06:00
Todd C. Miller
3d995f7e58 ptrace_getregs: make compat check more generic
No need to use different checks for mips and non-mips, the compiler
will optimize away the superfluous check.
2022-08-22 11:46:46 -06:00
Todd C. Miller
8eb619ac2b ptrace_getregs: try to determine compat mode if caller doesn't know.
In ptrace_verify_post_exec(), we don't know whether the executable
that is now running is a native or compat binary.  In most cases
ptrace_getregs() will be able to figure it out for us.
2022-08-19 11:34:40 -06:00
Todd C. Miller
b2d8909c19 ptrace_intercept_execve: fail syscall rather than killing process on error.
If the execve(2) args are bogus pointers, we should just return an
error instead of killing the process.  For consistency with the
kernel, convert EIO from ptrace(2) to EFAULT.
Also convert some ptrace(2) warnings to debug printfs so sudo is less chatty.
2022-08-19 11:05:10 -06:00
Todd C. Miller
9f552471bc Treat argv and closure->run_argv of different sizes as a mismatch.
If argv and closure->run_argv match up to the point where we hit a
NULL but one of them has additional entries, we still need to rewrite
argv.
2022-08-18 15:15:06 -06:00
Todd C. Miller
aef4a61886 Handle the case where argc is 0 when allocating space for argv.
We need to pass the pathname to the policy plugin in argv[0] so we
must be sure to allocate space for it even if argc is 0.
2022-08-18 15:12:21 -06:00
Todd C. Miller
23fa22975c Handle sysconf(_SC_ARG_MAX) failure, Coverity CID 276504. 2022-08-18 10:32:19 -06:00
Todd C. Miller
3ce19efca9 Add intercept_verify sudoers option to control execve(2) argument checking. 2022-07-29 15:22:27 -06:00
Todd C. Miller
b80b012de0 Use PTRACE_EVENT_EXEC to stop execution before return from execve(2).
We can now verify that the arguments match what we accepted before
the command actually runs.  If there is a mismatch, the process is
killed.  Shell scripts must be handled specially since the path
executed will be the interpreter, not the script name.
Linux allows interpreters to be nested up to 4 deep.
2022-07-29 15:22:26 -06:00
Todd C. Miller
25513b4f37 The length returned by ptrace_read_string() include the NUL.
We were wasting a extra byte in the string table for each entry.
2022-07-27 14:11:13 -06:00
Todd C. Miller
e5652fc65a Linux execve(2) allows argv or envp to be NULL.
Add checks to make sure we don't deference a NULL pointer.
2022-07-14 09:29:40 -06:00
Todd C. Miller
f6e4d2765a Add explicit include of unistd.h for getopt(3) and related variables. 2022-07-05 11:35:25 -06:00
Todd C. Miller
f5ac1317c4 Make sudo pass -Wmissing-prototypes 2022-06-27 12:48:03 -06:00
Todd C. Miller
f432209304 Include inttypes.h if stdint.h is not present.
Bug #1035
2022-06-27 07:20:37 -06:00
Todd C. Miller
33547702f3 readlink(2) does NUL-terminate the buffer, do it manually.
Fixes a bug where the current working directory could include garbage
in intercept mode using ptrace(2).
2022-06-21 20:12:58 -06:00
Todd C. Miller
898ca50545 Eliminate some dead stores that clang-analyzer complains about. 2022-06-03 10:01:11 -06:00
Todd C. Miller
63e6973ad1 ptrace_read_vec: don't try to free memory on the error path
This is leftover from when ptrace_read_string() allocated its own memory.
2022-06-03 10:00:08 -06:00
Todd C. Miller
b6ddf3db6d Consolidate some translatable strings. 2022-05-26 09:37:58 -06:00
Todd C. Miller
d6be44db00 Add support for running o32 and n32 binaries on mips64. 2022-05-24 13:43:51 -06:00
Todd C. Miller
bc6a0e1a16 Enable ptrace support for MIPS but only for log_subcmds.
It is not possible to change the syscall return value on MIPS
so we cannot support full intercept mode.
Another complication on MIPS is that if a system call is invoked
via syscall(__NR_###), v0 holds __NR_O32_Linux and the real syscall
is in the first arg (a0) and other args are shifted by one.
2022-05-24 13:43:50 -06:00
Todd C. Miller
f053f174bc Add intercept_type sudoers option to set intercept/log_subcmds mechanism. 2022-05-24 13:39:28 -06:00
Todd C. Miller
a52e3776f0 Fix suspending a sudo-run shell in ptrace intercept mode with no pty.
When ptracing a process, we receive the signal-delivery-stop signal
before the group-stop signal.  If sudo is running the command in
the same terminal, we need to wait until the stop signal is actually
delivered to the command before we can suspend sudo itself.  If we
suspend sudo before receiving the group-stop, the command will be
restarted with PTRACE_LISTEN too late and will miss the SIGCONT
from sudo.
2022-05-17 14:26:03 -06:00
Todd C. Miller
161a972600 The set_sc_arg3, get_sc_arg3 and set_sc_arg4 functions are not used.
Use ifdef notyet to disable for now since they may be used in the
future.
2022-05-17 08:25:59 -06:00
Todd C. Miller
3238c43f92 ptrace_write_string: the terminating NUL fix was reverted by mistake. 2022-05-14 13:05:00 -06:00
Todd C. Miller
b99debf92f Add arm-specific code to set the system call number.
Fixes rejection of commands due to policy on arm when in intercept mode.
2022-05-12 10:23:27 -06:00
Todd C. Miller
ea2bf7f1f2 ptrace_intercept_execve: read back the updated syscall args in test mode.
This makes it easier to detect problems with the syscall rewrite code
when testing with test_ptrace.
2022-05-12 08:32:31 -06:00
Todd C. Miller
4ecada04a4 Repair ptrace_write_vec() for compat binaries. 2022-05-12 09:21:02 -06:00
Todd C. Miller
f94e5d40db Fix compilation error on systems with no compat arch.
Currently only affects i386.
2022-05-11 20:07:59 -06:00
Todd C. Miller
64aba8f88e Move code to write a string vector to ptrace_write_vec(). 2022-05-11 20:07:59 -06:00
Todd C. Miller
55f5e389ba Fix tracing compat binaries on big endian systems.
We need to swap the order of the two 32-bit addresses for big-endian.
2022-05-11 20:07:59 -06:00
Todd C. Miller
31e8506c82 Enable ptrace intercept on powerpc.
Tested on ppc64 and ppc64le.
2022-05-11 20:07:59 -06:00
Todd C. Miller
040e75a07b Add test_ptrace program to test ptrace-based intercept support. 2022-05-11 20:07:55 -06:00
Todd C. Miller
532e8218b2 Use unsigned long for addresses so we don't have to worry about sign extension. 2022-05-11 08:04:58 -06:00
Todd C. Miller
6cd461d9f8 ptrace_write_string: make sure we always write the terminating NUL.
We can't check *str for NUL since it may not have been written yet.
2022-05-10 11:23:44 -06:00
Todd C. Miller
8fe1238679 Fix compilation error when SECCOMP_AUDIT_ARCH_COMPAT is not defined. 2022-05-10 08:17:00 -06:00
Todd C. Miller
a40d62b3d5 It is now safe to make WORDALIGN use compat (not native) aligment.
We allocate space for an extra pointer between argv and the string
table for compat binaries so there is no need to align address to
sizeof(long).
2022-05-09 11:03:30 -06:00
Todd C. Miller
f82aa334cc Use the entire word in ptrace_get_vec_len() and ptrace_read_vec().
For compat binaries, use the upper 32-bits as the next word instead
of calling ptrace(2) to get it.  This reduces the number of ptrace(2)
calls when reading argv and envp for compat binaries.
2022-05-09 10:09:30 -06:00
Todd C. Miller
c9a481506d We don't need to align strings in the string table.
We align the start of the string table to a word boundary to help
prevent overlap when writing the pointers.  However, the actual
strings themselves don't need to be aligned.
2022-05-07 18:42:32 -06:00
Todd C. Miller
206cd10ed5 Avoid potentially overwriting string table when writing argv.
In compat mode, if argc is odd, writing the last pointer of argv will
overlap with the address of argv[0], so leave an extra word in between.
Also remove incorrect comments about PTRACE_PEEKDATA unaligned access.
2022-05-06 19:46:22 -06:00
Todd C. Miller
0a2975367e ptrace_intercept_execve: rewrite path to exec if changed by the policy 2022-05-05 19:07:12 -06:00
Todd C. Miller
a15ca9c85b Use native word size for padding and when reading/writing strings.
If we try to use the compat word size we can end up in a situation
where a subsequent PTRACE_POKEDATA overwrites part of what we've
already written since it always writes in sizeof(long) units.
2022-05-06 10:46:06 -06:00
Todd C. Miller
339746730c ptrace_intercept_execve: plug memory leak of get_execve_info() buffer 2022-05-05 19:06:19 -06:00
Todd C. Miller
35ea534b3e Move register definitions to exec_ptrace.h 2022-05-05 13:37:26 -06:00
Todd C. Miller
d3a1bf4216 Add support for intercepting 32-bit binaries on 64-bit systems.
We need to define the ptrace register struct ourselves for the
32-bit system since there is no good way to get it from the system
headers.  Currently only implemented for x86_64 and aarch64.
2022-05-05 09:29:05 -06:00