Commit Graph

1652 Commits

Author SHA1 Message Date
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
kernelmethod
bd25b85a66 Add an apparmor_profile sudo setting
Define a new sudo setting, `apparmor_profile`, that can be used to pass
in an AppArmor profile that should be used to confine commands. If
apparmor_profile is specified, sudo will execute the command using the
new `apparmor_execve` function, which confines the command under the
provided profile before exec'ing it.
2022-05-23 13:41:42 -06:00
Todd C. Miller
b203753013 Pass the WUNTRACED flag to waitpid() even if __WALL is present.
Otherwise, we won't get the wait status of a suspended command
that is not being traced.
2022-05-19 19:48:24 -06:00
Todd C. Miller
0ea431e392 Move code to suspend sudo when no pty is in use to separate file.
Use this in test_ptrace.c to be able to suspend just like sudo does.
2022-05-18 07:29:55 -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
0bfbef0169 Use __x86_64__ preprocessor symbol, not __amd64__
Also clarify a comment about MIPS ptrace.
2022-05-16 16:04:24 -06:00
Todd C. Miller
7329a4a864 ptrace support has been tested on Debian/s390x.
It should also work on s390 but this has not been tested.
I have not added a compat mode to trace 31-bit binaries on s390x
due to the lack of a test system.
2022-05-16 13:22:31 -06:00
Todd C. Miller
2eb8ff17be Define sudo_pt_regs instead of user_pt_regs and include the struct keyword.
On s390, the struct is typedef'd without a name.
2022-05-16 12:39:59 -06:00
Todd C. Miller
74c6353a84 ptrace support has been tested on Debian/riscv64. 2022-05-16 09:08:45 -06:00
Todd C. Miller
55bd6272c0 Tracing 32-bit arm binaries from a 64-bit sudo works. 2022-05-14 14:41:18 -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
cb95b75ffa ptrace-based intercept has now been tested on 32-bit arm 2022-05-14 13:03:19 -06:00
Todd C. Miller
bb3c4ba07c Don't use PTRACE_SET_SYSCALL for 32-bit arm binaries running on aarch64.
Use PTRACE_SETREGSET with NT_ARM_SYSTEM_CALL instead just like we
would for a 64-bit binary.  Newer Linux headers don't define
PTRACE_SET_SYSCALL for aarch64.
2022-05-13 18:15:28 -06:00
Todd C. Miller
6a196190cf Replace verbose flag with debug flag.
This is more accurate since it actually uses the debug subsystem.
2022-05-13 07:55:05 -06:00
Todd C. Miller
3226c2df40 Initial cut at MIPS support, untested.
Mips is a bit different in that most Linux distros appear to use
the n32 ABI on 64-bit CPUs.  We don't currently support tracing a
64-bit binary from a 32-bit sudo.  We could suport tracing o32
ABI binaries in compat mode, though.
2022-05-13 07:07:32 -06:00
Todd C. Miller
f4cc99c063 Add have_seccomp_action("trap") call to check for SECCOMP_MODE_FILTER. 2022-05-12 15:46:53 -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
0f104d2ff1 Fix a crash when not run in verbose mode. 2022-05-12 08:37:48 -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
Todd C. Miller
de678ba775 Add setters and getters for ptrace(2) register access.
This will be used when running 32-bit binaries from a 64-bit sudo.
2022-05-05 09:17:58 -06:00
Todd C. Miller
f53053a66f exec_ptrace_handled: don't return early if ptrace_intercept_execve() fails.
We need to continue the traced process even if there is a fatal
error.  Otherwise, sudo will appear to hang as the running process
is left in PTRACE_EVENT stop.
2022-05-05 09:15:50 -06:00
Todd C. Miller
bbbb6e2ace Don't use PTRACE_GETREGS, it is too complicated when runing compat binaries.
Unlike PTRACE_GETREGSET, PTRACE_GETREGS requires that we manually
map registers from 64-bit to 32-bit layouts when running, e.g. a
32-bit binary from a 64-bit sudo process.
2022-05-05 08:53:51 -06:00
Todd C. Miller
4ab6a87b96 Initialize intercept_allow_setid to true if we use ptrace(2) and seccomp(2). 2022-05-04 13:32:28 -06:00
Todd C. Miller
e84fdd99fd If the process is already being traced, just resume it and clear flags.
This makes it possible to run sudo in ptrace intercept mode from within
a shell (or other process) that is already being traced by sudo.
2022-05-03 13:34:40 -06:00
Todd C. Miller
cc52ab770c exec_ptrace_handled: fix delivery of non-stop signals.
We need to deliver signals to the tracee as long as it is not
a group stop.  Fixes a hang while tracing another sudo process.
2022-05-03 12:54:10 -06:00
Todd C. Miller
4cac34b86d Make SIGCHLD handler more consistent with the pty version.
No real change other than a few debug statements.
2022-05-03 09:38:28 -06:00
Todd C. Miller
3ee8bcefb0 Kill the command if intercept_setup() or ptrace_seize() fail. 2022-05-03 09:25:58 -06:00
Todd C. Miller
1d17415b69 Add support for intercepting x32 binaries on Linux x64_64. 2022-05-02 14:36:34 -06:00
Todd C. Miller
307b4f69b8 Fix typos 2022-04-29 19:03:20 -06:00
Todd C. Miller
cdc35afff3 Short-circuit the policy check if the command doesn't exist.
Otherwise, both sudo and the shell will report the error.
2022-04-29 13:22:51 -06:00