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 pid_t pid = getpid(), tid = syscall(SYS_gettid);
187 printf("\tChild will make one syscall\n");
188 syscall(SYS_tgkill, pid, tid, SIGSTOP);
190 syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
196 /* Wait for SIGSTOP. */
197 if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
200 struct user_regs_struct regs;
202 printf("[RUN]\tSYSEMU\n");
203 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
204 err(1, "PTRACE_SYSEMU");
207 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
208 err(1, "PTRACE_GETREGS");
210 if (regs.user_syscall_nr != SYS_gettid ||
211 regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
212 regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
213 regs.user_arg4 != 14 || regs.user_arg5 != 15) {
214 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);
217 printf("[OK]\tInitial nr and args are correct\n");
220 printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
221 (unsigned long)regs.user_ip);
224 * This does exactly what it appears to do if syscall is int80 or
225 * SYSCALL64. For SYSCALL32 or SYSENTER, though, this is highly
226 * magical. It needs to work so that ptrace and syscall restart
229 regs.user_ax = regs.user_syscall_nr;
231 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
232 err(1, "PTRACE_SETREGS");
234 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
235 err(1, "PTRACE_SYSEMU");
238 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
239 err(1, "PTRACE_GETREGS");
241 if (regs.user_syscall_nr != SYS_gettid ||
242 regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
243 regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
244 regs.user_arg4 != 14 || regs.user_arg5 != 15) {
245 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);
248 printf("[OK]\tRestarted nr and args are correct\n");
251 printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
252 (unsigned long)regs.user_ip);
254 regs.user_ax = SYS_getpid;
263 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
264 err(1, "PTRACE_SETREGS");
266 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
267 err(1, "PTRACE_SYSEMU");
270 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
271 err(1, "PTRACE_GETREGS");
273 if (regs.user_syscall_nr != SYS_getpid ||
274 regs.user_arg0 != 20 || regs.user_arg1 != 21 || regs.user_arg2 != 22 ||
275 regs.user_arg3 != 23 || regs.user_arg4 != 24 || regs.user_arg5 != 25) {
276 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);
279 printf("[OK]\tReplacement nr and args are correct\n");
282 if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
283 err(1, "PTRACE_CONT");
284 if (waitpid(chld, &status, 0) != chld)
286 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
287 printf("[FAIL]\tChild failed\n");
290 printf("[OK]\tChild exited cleanly\n");
294 static void test_restart_under_ptrace(void)
296 printf("[RUN]\tkernel syscall restart under ptrace\n");
302 if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
303 err(1, "PTRACE_TRACEME");
305 pid_t pid = getpid(), tid = syscall(SYS_gettid);
307 printf("\tChild will take a nap until signaled\n");
308 setsigign(SIGUSR1, SA_RESTART);
309 syscall(SYS_tgkill, pid, tid, SIGSTOP);
311 syscall(SYS_pause, 0, 0, 0, 0, 0, 0);
317 /* Wait for SIGSTOP. */
318 if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
321 struct user_regs_struct regs;
323 printf("[RUN]\tSYSCALL\n");
324 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
325 err(1, "PTRACE_SYSCALL");
328 /* We should be stopped at pause(2) entry. */
330 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
331 err(1, "PTRACE_GETREGS");
333 if (regs.user_syscall_nr != SYS_pause ||
334 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
335 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
336 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
337 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);
340 printf("[OK]\tInitial nr and args are correct\n");
346 /* Advance. We should be stopped at exit. */
347 printf("[RUN]\tSYSCALL\n");
348 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
349 err(1, "PTRACE_SYSCALL");
352 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
353 err(1, "PTRACE_GETREGS");
355 if (regs.user_syscall_nr != SYS_pause ||
356 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
357 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
358 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
359 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);
362 printf("[OK]\tArgs after SIGUSR1 are correct (ax = %ld)\n",
366 /* Poke the regs back in. This must not break anything. */
367 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
368 err(1, "PTRACE_SETREGS");
370 /* Catch the (ignored) SIGUSR1. */
371 if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
372 err(1, "PTRACE_CONT");
373 if (waitpid(chld, &status, 0) != chld)
375 if (!WIFSTOPPED(status)) {
376 printf("[FAIL]\tChild was stopped for SIGUSR1 (status = 0x%x)\n", status);
379 printf("[OK]\tChild got SIGUSR1\n");
382 /* The next event should be pause(2) again. */
383 printf("[RUN]\tStep again\n");
384 if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
385 err(1, "PTRACE_SYSCALL");
388 /* We should be stopped at pause(2) entry. */
390 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
391 err(1, "PTRACE_GETREGS");
393 if (regs.user_syscall_nr != SYS_pause ||
394 regs.user_arg0 != 0 || regs.user_arg1 != 0 ||
395 regs.user_arg2 != 0 || regs.user_arg3 != 0 ||
396 regs.user_arg4 != 0 || regs.user_arg5 != 0) {
397 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);
400 printf("[OK]\tpause(2) restarted correctly\n");
405 if (waitpid(chld, &status, 0) != chld)
411 printf("[RUN]\tCheck int80 return regs\n");
412 test_sys32_regs(do_full_int80);
414 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
415 vsyscall32 = (void *)getauxval(AT_SYSINFO);
417 printf("[RUN]\tCheck AT_SYSINFO return regs\n");
418 test_sys32_regs(do_full_vsyscall32);
420 printf("[SKIP]\tAT_SYSINFO is not available\n");
424 test_ptrace_syscall_restart();
426 test_restart_under_ptrace();