1 // SPDX-License-Identifier: GPL-2.0
4 #include <sys/ptrace.h>
7 #include <sys/syscall.h>
15 #include <asm/ptrace-abi.h>
18 /* Bitness-agnostic defines for user_regs_struct fields. */
20 # define user_syscall_nr orig_rax
21 # define user_arg0 rdi
22 # define user_arg1 rsi
23 # define user_arg2 rdx
24 # define user_arg3 r10
30 # define user_syscall_nr orig_eax
31 # define user_arg0 ebx
32 # define user_arg1 ecx
33 # define user_arg2 edx
34 # define user_arg3 esi
35 # define user_arg4 edi
36 # define user_arg5 ebp
43 struct syscall_args32 {
44 uint32_t nr, arg0, arg1, arg2, arg3, arg4, arg5;
48 extern void sys32_helper(struct syscall_args32 *, void *);
49 extern void int80_and_ret(void);
53 * Helper to invoke int80 with controlled regs and capture the final regs.
55 static void do_full_int80(struct syscall_args32 *args)
58 register unsigned long bp asm("bp") = args->arg5;
59 asm volatile ("int $0x80"
61 "+b" (args->arg0), "+c" (args->arg1), "+d" (args->arg2),
62 "+S" (args->arg3), "+D" (args->arg4), "+r" (bp)
63 : : "r8", "r9", "r10", "r11");
66 sys32_helper(args, int80_and_ret);
71 static void (*vsyscall32)(void);
74 * Nasty helper to invoke AT_SYSINFO (i.e. __kernel_vsyscall) with
75 * controlled regs and capture the final regs. This is so nasty that it
76 * crashes my copy of gdb :)
78 static void do_full_vsyscall32(struct syscall_args32 *args)
80 sys32_helper(args, vsyscall32);
84 static siginfo_t wait_trap(pid_t chld)
87 if (waitid(P_PID, chld, &si, WEXITED|WSTOPPED) != 0)
89 if (si.si_pid != chld)
90 errx(1, "got unexpected pid in event\n");
91 if (si.si_code != CLD_TRAPPED)
92 errx(1, "got unexpected event type %d\n", si.si_code);
96 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
100 memset(&sa, 0, sizeof(sa));
101 sa.sa_sigaction = handler;
102 sa.sa_flags = SA_SIGINFO | flags;
103 sigemptyset(&sa.sa_mask);
104 if (sigaction(sig, &sa, 0))
108 static void setsigign(int sig, int flags)
111 memset(&sa, 0, sizeof(sa));
112 sa.sa_sigaction = (void *)SIG_IGN;
114 sigemptyset(&sa.sa_mask);
115 if (sigaction(sig, &sa, 0))
119 static void clearhandler(int sig)
122 memset(&sa, 0, sizeof(sa));
123 sa.sa_handler = SIG_DFL;
124 sigemptyset(&sa.sa_mask);
125 if (sigaction(sig, &sa, 0))
130 # define REG_BP REG_RBP
132 # define REG_BP REG_EBP
135 static void empty_handler(int sig, siginfo_t *si, void *ctx_void)
139 static void test_sys32_regs(void (*do_syscall)(struct syscall_args32 *))
141 struct syscall_args32 args = {
142 .nr = 224, /* gettid */
143 .arg0 = 10, .arg1 = 11, .arg2 = 12,
144 .arg3 = 13, .arg4 = 14, .arg5 = 15,
149 if (args.nr != getpid() ||
150 args.arg0 != 10 || args.arg1 != 11 || args.arg2 != 12 ||
151 args.arg3 != 13 || args.arg4 != 14 || args.arg5 != 15) {
152 printf("[FAIL]\tgetpid() failed to preserve regs\n");
155 printf("[OK]\tgetpid() preserves regs\n");
158 sethandler(SIGUSR1, empty_handler, 0);
160 args.nr = 37; /* kill */
161 args.arg0 = getpid();
165 args.arg0 != getpid() || args.arg1 != SIGUSR1 || args.arg2 != 12 ||
166 args.arg3 != 13 || args.arg4 != 14 || args.arg5 != 15) {
167 printf("[FAIL]\tkill(getpid(), SIGUSR1) failed to preserve regs\n");
170 printf("[OK]\tkill(getpid(), SIGUSR1) preserves regs\n");
172 clearhandler(SIGUSR1);
175 static void test_ptrace_syscall_restart(void)
177 printf("[RUN]\tptrace-induced syscall restart\n");
183 if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
184 err(1, "PTRACE_TRACEME");
186 printf("\tChild will make one syscall\n");
189 syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
195 /* Wait for SIGSTOP. */
196 if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
199 struct user_regs_struct regs;
201 printf("[RUN]\tSYSEMU\n");
202 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
203 err(1, "PTRACE_SYSEMU");
206 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
207 err(1, "PTRACE_GETREGS");
209 if (regs.user_syscall_nr != SYS_gettid ||
210 regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
211 regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
212 regs.user_arg4 != 14 || regs.user_arg5 != 15) {
213 printf("[FAIL]\tInitial args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
216 printf("[OK]\tInitial nr and args are correct\n");
219 printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
220 (unsigned long)regs.user_ip);
223 * This does exactly what it appears to do if syscall is int80 or
224 * SYSCALL64. For SYSCALL32 or SYSENTER, though, this is highly
225 * magical. It needs to work so that ptrace and syscall restart
228 regs.user_ax = regs.user_syscall_nr;
230 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
231 err(1, "PTRACE_SETREGS");
233 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
234 err(1, "PTRACE_SYSEMU");
237 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
238 err(1, "PTRACE_GETREGS");
240 if (regs.user_syscall_nr != SYS_gettid ||
241 regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
242 regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
243 regs.user_arg4 != 14 || regs.user_arg5 != 15) {
244 printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
247 printf("[OK]\tRestarted nr and args are correct\n");
250 printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
251 (unsigned long)regs.user_ip);
253 regs.user_ax = SYS_getpid;
262 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
263 err(1, "PTRACE_SETREGS");
265 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
266 err(1, "PTRACE_SYSEMU");
269 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
270 err(1, "PTRACE_GETREGS");
272 if (regs.user_syscall_nr != SYS_getpid ||
273 regs.user_arg0 != 20 || regs.user_arg1 != 21 || regs.user_arg2 != 22 ||
274 regs.user_arg3 != 23 || regs.user_arg4 != 24 || regs.user_arg5 != 25) {
275 printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
278 printf("[OK]\tReplacement nr and args are correct\n");
281 if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
282 err(1, "PTRACE_CONT");
283 if (waitpid(chld, &status, 0) != chld)
285 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
286 printf("[FAIL]\tChild failed\n");
289 printf("[OK]\tChild exited cleanly\n");
293 static void test_restart_under_ptrace(void)
295 printf("[RUN]\tkernel syscall restart under ptrace\n");
301 if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
302 err(1, "PTRACE_TRACEME");
304 printf("\tChild will take a nap until signaled\n");
305 setsigign(SIGUSR1, SA_RESTART);
308 syscall(SYS_pause, 0, 0, 0, 0, 0, 0);
314 /* Wait for SIGSTOP. */
315 if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
318 struct user_regs_struct regs;
320 printf("[RUN]\tSYSCALL\n");
321 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
322 err(1, "PTRACE_SYSCALL");
325 /* We should be stopped at pause(2) entry. */
327 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
328 err(1, "PTRACE_GETREGS");
330 if (regs.user_syscall_nr != SYS_pause ||
331 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
332 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
333 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
334 printf("[FAIL]\tInitial args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
337 printf("[OK]\tInitial nr and args are correct\n");
343 /* Advance. We should be stopped at exit. */
344 printf("[RUN]\tSYSCALL\n");
345 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
346 err(1, "PTRACE_SYSCALL");
349 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
350 err(1, "PTRACE_GETREGS");
352 if (regs.user_syscall_nr != SYS_pause ||
353 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
354 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
355 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
356 printf("[FAIL]\tArgs after SIGUSR1 are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
359 printf("[OK]\tArgs after SIGUSR1 are correct (ax = %ld)\n",
363 /* Poke the regs back in. This must not break anything. */
364 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
365 err(1, "PTRACE_SETREGS");
367 /* Catch the (ignored) SIGUSR1. */
368 if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
369 err(1, "PTRACE_CONT");
370 if (waitpid(chld, &status, 0) != chld)
372 if (!WIFSTOPPED(status)) {
373 printf("[FAIL]\tChild was stopped for SIGUSR1 (status = 0x%x)\n", status);
376 printf("[OK]\tChild got SIGUSR1\n");
379 /* The next event should be pause(2) again. */
380 printf("[RUN]\tStep again\n");
381 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
382 err(1, "PTRACE_SYSCALL");
385 /* We should be stopped at pause(2) entry. */
387 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
388 err(1, "PTRACE_GETREGS");
390 if (regs.user_syscall_nr != SYS_pause ||
391 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
392 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
393 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
394 printf("[FAIL]\tpause did not restart (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n", (unsigned long)regs.user_syscall_nr, (unsigned long)regs.user_arg0, (unsigned long)regs.user_arg1, (unsigned long)regs.user_arg2, (unsigned long)regs.user_arg3, (unsigned long)regs.user_arg4, (unsigned long)regs.user_arg5);
397 printf("[OK]\tpause(2) restarted correctly\n");
402 if (waitpid(chld, &status, 0) != chld)
408 printf("[RUN]\tCheck int80 return regs\n");
409 test_sys32_regs(do_full_int80);
411 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
412 vsyscall32 = (void *)getauxval(AT_SYSINFO);
413 printf("[RUN]\tCheck AT_SYSINFO return regs\n");
414 test_sys32_regs(do_full_vsyscall32);
417 test_ptrace_syscall_restart();
419 test_restart_under_ptrace();