3 #include <sys/ptrace.h>
6 #include <sys/syscall.h>
14 #include <asm/ptrace-abi.h>
17 /* Bitness-agnostic defines for user_regs_struct fields. */
19 # define user_syscall_nr orig_rax
20 # define user_arg0 rdi
21 # define user_arg1 rsi
22 # define user_arg2 rdx
23 # define user_arg3 r10
29 # define user_syscall_nr orig_eax
30 # define user_arg0 ebx
31 # define user_arg1 ecx
32 # define user_arg2 edx
33 # define user_arg3 esi
34 # define user_arg4 edi
35 # define user_arg5 ebp
42 struct syscall_args32 {
43 uint32_t nr, arg0, arg1, arg2, arg3, arg4, arg5;
47 extern void sys32_helper(struct syscall_args32 *, void *);
48 extern void int80_and_ret(void);
52 * Helper to invoke int80 with controlled regs and capture the final regs.
54 static void do_full_int80(struct syscall_args32 *args)
57 register unsigned long bp asm("bp") = args->arg5;
58 asm volatile ("int $0x80"
60 "+b" (args->arg0), "+c" (args->arg1), "+d" (args->arg2),
61 "+S" (args->arg3), "+D" (args->arg4), "+r" (bp)
62 : : "r8", "r9", "r10", "r11");
65 sys32_helper(args, int80_and_ret);
70 static void (*vsyscall32)(void);
73 * Nasty helper to invoke AT_SYSINFO (i.e. __kernel_vsyscall) with
74 * controlled regs and capture the final regs. This is so nasty that it
75 * crashes my copy of gdb :)
77 static void do_full_vsyscall32(struct syscall_args32 *args)
79 sys32_helper(args, vsyscall32);
83 static siginfo_t wait_trap(pid_t chld)
86 if (waitid(P_PID, chld, &si, WEXITED|WSTOPPED) != 0)
88 if (si.si_pid != chld)
89 errx(1, "got unexpected pid in event\n");
90 if (si.si_code != CLD_TRAPPED)
91 errx(1, "got unexpected event type %d\n", si.si_code);
95 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
99 memset(&sa, 0, sizeof(sa));
100 sa.sa_sigaction = handler;
101 sa.sa_flags = SA_SIGINFO | flags;
102 sigemptyset(&sa.sa_mask);
103 if (sigaction(sig, &sa, 0))
107 static void setsigign(int sig, int flags)
110 memset(&sa, 0, sizeof(sa));
111 sa.sa_sigaction = (void *)SIG_IGN;
113 sigemptyset(&sa.sa_mask);
114 if (sigaction(sig, &sa, 0))
118 static void clearhandler(int sig)
121 memset(&sa, 0, sizeof(sa));
122 sa.sa_handler = SIG_DFL;
123 sigemptyset(&sa.sa_mask);
124 if (sigaction(sig, &sa, 0))
129 # define REG_BP REG_RBP
131 # define REG_BP REG_EBP
134 static void empty_handler(int sig, siginfo_t *si, void *ctx_void)
138 static void test_sys32_regs(void (*do_syscall)(struct syscall_args32 *))
140 struct syscall_args32 args = {
141 .nr = 224, /* gettid */
142 .arg0 = 10, .arg1 = 11, .arg2 = 12,
143 .arg3 = 13, .arg4 = 14, .arg5 = 15,
148 if (args.nr != getpid() ||
149 args.arg0 != 10 || args.arg1 != 11 || args.arg2 != 12 ||
150 args.arg3 != 13 || args.arg4 != 14 || args.arg5 != 15) {
151 printf("[FAIL]\tgetpid() failed to preserve regs\n");
154 printf("[OK]\tgetpid() preserves regs\n");
157 sethandler(SIGUSR1, empty_handler, 0);
159 args.nr = 37; /* kill */
160 args.arg0 = getpid();
164 args.arg0 != getpid() || args.arg1 != SIGUSR1 || args.arg2 != 12 ||
165 args.arg3 != 13 || args.arg4 != 14 || args.arg5 != 15) {
166 printf("[FAIL]\tkill(getpid(), SIGUSR1) failed to preserve regs\n");
169 printf("[OK]\tkill(getpid(), SIGUSR1) preserves regs\n");
171 clearhandler(SIGUSR1);
174 static void test_ptrace_syscall_restart(void)
176 printf("[RUN]\tptrace-induced syscall restart\n");
182 if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
183 err(1, "PTRACE_TRACEME");
185 printf("\tChild will make one syscall\n");
188 syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
194 /* Wait for SIGSTOP. */
195 if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
198 struct user_regs_struct regs;
200 printf("[RUN]\tSYSEMU\n");
201 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
202 err(1, "PTRACE_SYSEMU");
205 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
206 err(1, "PTRACE_GETREGS");
208 if (regs.user_syscall_nr != SYS_gettid ||
209 regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
210 regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
211 regs.user_arg4 != 14 || regs.user_arg5 != 15) {
212 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);
215 printf("[OK]\tInitial nr and args are correct\n");
218 printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
219 (unsigned long)regs.user_ip);
222 * This does exactly what it appears to do if syscall is int80 or
223 * SYSCALL64. For SYSCALL32 or SYSENTER, though, this is highly
224 * magical. It needs to work so that ptrace and syscall restart
227 regs.user_ax = regs.user_syscall_nr;
229 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
230 err(1, "PTRACE_SETREGS");
232 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
233 err(1, "PTRACE_SYSEMU");
236 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
237 err(1, "PTRACE_GETREGS");
239 if (regs.user_syscall_nr != SYS_gettid ||
240 regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
241 regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
242 regs.user_arg4 != 14 || regs.user_arg5 != 15) {
243 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);
246 printf("[OK]\tRestarted nr and args are correct\n");
249 printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
250 (unsigned long)regs.user_ip);
252 regs.user_ax = SYS_getpid;
261 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
262 err(1, "PTRACE_SETREGS");
264 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
265 err(1, "PTRACE_SYSEMU");
268 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
269 err(1, "PTRACE_GETREGS");
271 if (regs.user_syscall_nr != SYS_getpid ||
272 regs.user_arg0 != 20 || regs.user_arg1 != 21 || regs.user_arg2 != 22 ||
273 regs.user_arg3 != 23 || regs.user_arg4 != 24 || regs.user_arg5 != 25) {
274 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);
277 printf("[OK]\tReplacement nr and args are correct\n");
280 if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
281 err(1, "PTRACE_CONT");
282 if (waitpid(chld, &status, 0) != chld)
284 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
285 printf("[FAIL]\tChild failed\n");
288 printf("[OK]\tChild exited cleanly\n");
292 static void test_restart_under_ptrace(void)
294 printf("[RUN]\tkernel syscall restart under ptrace\n");
300 if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
301 err(1, "PTRACE_TRACEME");
303 printf("\tChild will take a nap until signaled\n");
304 setsigign(SIGUSR1, SA_RESTART);
307 syscall(SYS_pause, 0, 0, 0, 0, 0, 0);
313 /* Wait for SIGSTOP. */
314 if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
317 struct user_regs_struct regs;
319 printf("[RUN]\tSYSCALL\n");
320 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
321 err(1, "PTRACE_SYSCALL");
324 /* We should be stopped at pause(2) entry. */
326 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
327 err(1, "PTRACE_GETREGS");
329 if (regs.user_syscall_nr != SYS_pause ||
330 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
331 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
332 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
333 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);
336 printf("[OK]\tInitial nr and args are correct\n");
342 /* Advance. We should be stopped at exit. */
343 printf("[RUN]\tSYSCALL\n");
344 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
345 err(1, "PTRACE_SYSCALL");
348 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
349 err(1, "PTRACE_GETREGS");
351 if (regs.user_syscall_nr != SYS_pause ||
352 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
353 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
354 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
355 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);
358 printf("[OK]\tArgs after SIGUSR1 are correct (ax = %ld)\n",
362 /* Poke the regs back in. This must not break anything. */
363 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
364 err(1, "PTRACE_SETREGS");
366 /* Catch the (ignored) SIGUSR1. */
367 if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
368 err(1, "PTRACE_CONT");
369 if (waitpid(chld, &status, 0) != chld)
371 if (!WIFSTOPPED(status)) {
372 printf("[FAIL]\tChild was stopped for SIGUSR1 (status = 0x%x)\n", status);
375 printf("[OK]\tChild got SIGUSR1\n");
378 /* The next event should be pause(2) again. */
379 printf("[RUN]\tStep again\n");
380 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
381 err(1, "PTRACE_SYSCALL");
384 /* We should be stopped at pause(2) entry. */
386 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
387 err(1, "PTRACE_GETREGS");
389 if (regs.user_syscall_nr != SYS_pause ||
390 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
391 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
392 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
393 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);
396 printf("[OK]\tpause(2) restarted correctly\n");
401 if (waitpid(chld, &status, 0) != chld)
407 printf("[RUN]\tCheck int80 return regs\n");
408 test_sys32_regs(do_full_int80);
410 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
411 vsyscall32 = (void *)getauxval(AT_SYSINFO);
412 printf("[RUN]\tCheck AT_SYSINFO return regs\n");
413 test_sys32_regs(do_full_vsyscall32);
416 test_ptrace_syscall_restart();
418 test_restart_under_ptrace();