| 1 |
/* |
| 2 |
* Copyright (c) 2022 Todd C. Miller <[email protected]> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
* This is an open source non-commercial project. Dear PVS-Studio, please check it. |
| 19 |
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
| 20 |
*/ |
| 21 |
|
| 22 |
/* |
| 23 |
* Test program to exercise seccomp(2) and ptrace(2) intercept code. |
| 24 |
* |
| 25 |
* Usage: test_ptrace [-d 1-3] [command] |
| 26 |
*/ |
| 27 |
|
| 28 |
/* Ignore architecture restrictions and define this unilaterally. */ |
| 29 |
#define HAVE_PTRACE_INTERCEPT |
| 30 |
#include "exec_ptrace.c" |
| 31 |
|
| 32 |
static sig_atomic_t got_sigchld; |
| 33 |
static int debug; |
| 34 |
int sudo_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER; |
| 35 |
|
| 36 |
sudo_dso_public int main(int argc, char *argv[]); |
| 37 |
|
| 38 |
static void |
| 39 |
handler(int signo) |
| 40 |
{ |
| 41 |
if (signo == SIGCHLD) |
| 42 |
got_sigchld = 1; |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
intercept_closure_reset(struct intercept_closure *closure) |
| 47 |
{ |
| 48 |
memset(closure, 0, sizeof(*closure)); |
| 49 |
} |
| 50 |
|
| 51 |
bool |
| 52 |
intercept_check_policy(const char *command, int argc, char **argv, int envc, |
| 53 |
char **envp, const char *runcwd, int *oldcwd, void *v) |
| 54 |
{ |
| 55 |
struct intercept_closure *closure = v; |
| 56 |
struct stat sb1, sb2; |
| 57 |
bool is_denied; |
| 58 |
debug_decl(intercept_check_policy, SUDO_DEBUG_EXEC); |
| 59 |
|
| 60 |
/* Fake policy decisions. */ |
| 61 |
is_denied = stat(command, &sb1) == 0 && stat("/usr/bin/who", &sb2) == 0 && |
| 62 |
sb1.st_ino == sb2.st_ino && sb1.st_dev == sb2.st_dev; |
| 63 |
if (is_denied) { |
| 64 |
sudo_debug_printf(SUDO_DEBUG_DIAG, "denied %s", command); |
| 65 |
closure->state = POLICY_REJECT; |
| 66 |
} else { |
| 67 |
sudo_debug_printf(SUDO_DEBUG_DIAG, "allowed %s", command); |
| 68 |
closure->state = POLICY_TEST; |
| 69 |
} |
| 70 |
*oldcwd = -1; |
| 71 |
|
| 72 |
debug_return_bool(true); |
| 73 |
} |
| 74 |
|
| 75 |
static void |
| 76 |
init_debug_files(struct sudo_conf_debug_file_list *file_list, |
| 77 |
struct sudo_debug_file *file) |
| 78 |
{ |
| 79 |
debug_decl(init_debug_files, SUDO_DEBUG_EXEC); |
| 80 |
|
| 81 |
TAILQ_INIT(file_list); |
| 82 |
switch (debug) { |
| 83 |
case 0: |
| 84 |
debug_return; |
| 85 |
case 1: |
| 86 |
file->debug_flags = (char *)"exec@diag"; |
| 87 |
break; |
| 88 |
case 2: |
| 89 |
file->debug_flags = (char *)"exec@info"; |
| 90 |
break; |
| 91 |
default: |
| 92 |
file->debug_flags = (char *)"exec@debug"; |
| 93 |
break; |
| 94 |
} |
| 95 |
file->debug_file = (char *)"/dev/stderr"; |
| 96 |
TAILQ_INSERT_HEAD(file_list, file, entries); |
| 97 |
|
| 98 |
debug_return; |
| 99 |
} |
| 100 |
|
| 101 |
int |
| 102 |
sudo_sigaction(int signo, struct sigaction *sa, struct sigaction *osa) |
| 103 |
{ |
| 104 |
return sigaction(signo, sa, osa); |
| 105 |
} |
| 106 |
|
| 107 |
int |
| 108 |
main(int argc, char *argv[]) |
| 109 |
{ |
| 110 |
struct sudo_conf_debug_file_list debug_files; |
| 111 |
struct sudo_debug_file debug_file; |
| 112 |
const char *base, *shell = _PATH_SUDO_BSHELL; |
| 113 |
struct intercept_closure closure = { 0 }; |
| 114 |
const char *errstr; |
| 115 |
sigset_t blocked, empty; |
| 116 |
struct sigaction sa; |
| 117 |
pid_t child, my_pid, pid, my_pgrp; |
| 118 |
int ch, status; |
| 119 |
debug_decl_vars(main, SUDO_DEBUG_MAIN); |
| 120 |
|
| 121 |
initprogname(argc > 0 ? argv[0] : "test_ptrace"); |
| 122 |
|
| 123 |
if (!have_seccomp_action("trap")) |
| 124 |
sudo_fatalx("SECCOMP_MODE_FILTER not available in this kernel"); |
| 125 |
|
| 126 |
while ((ch = getopt(argc, argv, "d:")) != -1) { |
| 127 |
switch (ch) { |
| 128 |
case 'd': |
| 129 |
debug = sudo_strtonum(optarg, 1, INT_MAX, &errstr); |
| 130 |
if (errstr != NULL) |
| 131 |
sudo_fatalx(U_("%s: %s"), optarg, U_(errstr)); |
| 132 |
break; |
| 133 |
default: |
| 134 |
fprintf(stderr, "usage: %s [-d 1-3] [command]\n", getprogname()); |
| 135 |
return EXIT_FAILURE; |
| 136 |
} |
| 137 |
} |
| 138 |
argc -= optind; |
| 139 |
argv += optind; |
| 140 |
|
| 141 |
if (argc > 0) |
| 142 |
shell = argv[0]; |
| 143 |
base = strrchr(shell, '/'); |
| 144 |
base = base ? base + 1 : shell; |
| 145 |
|
| 146 |
/* Set debug level based on the debug flag. */ |
| 147 |
init_debug_files(&debug_files, &debug_file); |
| 148 |
sudo_debug_instance = sudo_debug_register(getprogname(), |
| 149 |
NULL, NULL, &debug_files, -1); |
| 150 |
if (sudo_debug_instance == SUDO_DEBUG_INSTANCE_ERROR) |
| 151 |
return EXIT_FAILURE; |
| 152 |
|
| 153 |
/* Block SIGCHLD and SIGUSR during critical section. */ |
| 154 |
sigemptyset(&empty); |
| 155 |
sigemptyset(&blocked); |
| 156 |
sigaddset(&blocked, SIGCHLD); |
| 157 |
sigaddset(&blocked, SIGUSR1); |
| 158 |
sigprocmask(SIG_BLOCK, &blocked, NULL); |
| 159 |
|
| 160 |
/* Signal handler sets a flag for SIGCHLD, nothing for SIGUSR1. */ |
| 161 |
memset(&sa, 0, sizeof(sa)); |
| 162 |
sigemptyset(&sa.sa_mask); |
| 163 |
sa.sa_flags = SA_RESTART; |
| 164 |
sa.sa_handler = handler; |
| 165 |
sigaction(SIGCHLD, &sa, NULL); |
| 166 |
sigaction(SIGUSR1, &sa, NULL); |
| 167 |
|
| 168 |
/* Fork a shell. */ |
| 169 |
my_pid = getpid(); |
| 170 |
my_pgrp = getpgrp(); |
| 171 |
child = fork(); |
| 172 |
switch (child) { |
| 173 |
case -1: |
| 174 |
sudo_fatal("fork"); |
| 175 |
case 0: |
| 176 |
/* child */ |
| 177 |
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) |
| 178 |
sudo_fatal("%s", "unable to set no_new_privs bit"); |
| 179 |
if (!set_exec_filter()) |
| 180 |
_exit(EXIT_FAILURE); |
| 181 |
|
| 182 |
/* Suspend child until tracer seizes control and sends SIGUSR1. */ |
| 183 |
sigsuspend(&empty); |
| 184 |
execl(shell, base, NULL); |
| 185 |
sudo_fatal("execl"); |
| 186 |
default: |
| 187 |
/* Parent attaches to child and allows it to continue. */ |
| 188 |
if (exec_ptrace_seize(child) == -1) |
| 189 |
return EXIT_FAILURE; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
/* Wait for SIGCHLD. */ |
| 194 |
for (;;) { |
| 195 |
sigsuspend(&empty); |
| 196 |
if (!got_sigchld) |
| 197 |
continue; |
| 198 |
got_sigchld = 0; |
| 199 |
|
| 200 |
for (;;) { |
| 201 |
do { |
| 202 |
pid = waitpid(-1, &status, __WALL|WNOHANG); |
| 203 |
} while (pid == -1 && errno == EINTR); |
| 204 |
if (pid <= 0) { |
| 205 |
if (pid == -1 && errno != ECHILD) |
| 206 |
sudo_fatal("waitpid"); |
| 207 |
/* No child to wait for. */ |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
if (WIFEXITED(status)) { |
| 212 |
sudo_debug_printf(SUDO_DEBUG_DIAG, "%d: exited %d", |
| 213 |
(int)pid, WEXITSTATUS(status)); |
| 214 |
if (pid == child) |
| 215 |
return WEXITSTATUS(status); |
| 216 |
} else if (WIFSIGNALED(status)) { |
| 217 |
sudo_debug_printf(SUDO_DEBUG_DIAG, "%d: killed by signal %d", |
| 218 |
(int)pid, WTERMSIG(status)); |
| 219 |
if (pid == child) |
| 220 |
return WTERMSIG(status) | 128; |
| 221 |
} else if (WIFSTOPPED(status)) { |
| 222 |
if (exec_ptrace_stopped(pid, status, &closure)) { |
| 223 |
if (pid == child) { |
| 224 |
sudo_suspend_parent(WSTOPSIG(status), my_pid, |
| 225 |
my_pgrp, child, NULL, NULL); |
| 226 |
if (kill(child, SIGCONT) != 0) |
| 227 |
sudo_warn("kill(%d, SIGCONT)", (int)child); |
| 228 |
} |
| 229 |
} |
| 230 |
} else { |
| 231 |
sudo_fatalx("%d: unknown status 0x%x", (int)pid, status); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |