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
));
64 sys32_helper(args
, int80_and_ret
);
69 static void (*vsyscall32
)(void);
72 * Nasty helper to invoke AT_SYSINFO (i.e. __kernel_vsyscall) with
73 * controlled regs and capture the final regs. This is so nasty that it
74 * crashes my copy of gdb :)
76 static void do_full_vsyscall32(struct syscall_args32
*args
)
78 sys32_helper(args
, vsyscall32
);
82 static siginfo_t
wait_trap(pid_t chld
)
85 if (waitid(P_PID
, chld
, &si
, WEXITED
|WSTOPPED
) != 0)
87 if (si
.si_pid
!= chld
)
88 errx(1, "got unexpected pid in event\n");
89 if (si
.si_code
!= CLD_TRAPPED
)
90 errx(1, "got unexpected event type %d\n", si
.si_code
);
94 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
98 memset(&sa
, 0, sizeof(sa
));
99 sa
.sa_sigaction
= handler
;
100 sa
.sa_flags
= SA_SIGINFO
| flags
;
101 sigemptyset(&sa
.sa_mask
);
102 if (sigaction(sig
, &sa
, 0))
106 static void setsigign(int sig
, int flags
)
109 memset(&sa
, 0, sizeof(sa
));
110 sa
.sa_sigaction
= (void *)SIG_IGN
;
112 sigemptyset(&sa
.sa_mask
);
113 if (sigaction(sig
, &sa
, 0))
117 static void clearhandler(int sig
)
120 memset(&sa
, 0, sizeof(sa
));
121 sa
.sa_handler
= SIG_DFL
;
122 sigemptyset(&sa
.sa_mask
);
123 if (sigaction(sig
, &sa
, 0))
128 # define REG_BP REG_RBP
130 # define REG_BP REG_EBP
133 static void empty_handler(int sig
, siginfo_t
*si
, void *ctx_void
)
137 static void test_sys32_regs(void (*do_syscall
)(struct syscall_args32
*))
139 struct syscall_args32 args
= {
140 .nr
= 224, /* gettid */
141 .arg0
= 10, .arg1
= 11, .arg2
= 12,
142 .arg3
= 13, .arg4
= 14, .arg5
= 15,
147 if (args
.nr
!= getpid() ||
148 args
.arg0
!= 10 || args
.arg1
!= 11 || args
.arg2
!= 12 ||
149 args
.arg3
!= 13 || args
.arg4
!= 14 || args
.arg5
!= 15) {
150 printf("[FAIL]\tgetpid() failed to preseve regs\n");
153 printf("[OK]\tgetpid() preserves regs\n");
156 sethandler(SIGUSR1
, empty_handler
, 0);
158 args
.nr
= 37; /* kill */
159 args
.arg0
= getpid();
163 args
.arg0
!= getpid() || args
.arg1
!= SIGUSR1
|| args
.arg2
!= 12 ||
164 args
.arg3
!= 13 || args
.arg4
!= 14 || args
.arg5
!= 15) {
165 printf("[FAIL]\tkill(getpid(), SIGUSR1) failed to preseve regs\n");
168 printf("[OK]\tkill(getpid(), SIGUSR1) preserves regs\n");
170 clearhandler(SIGUSR1
);
173 static void test_ptrace_syscall_restart(void)
175 printf("[RUN]\tptrace-induced syscall restart\n");
181 if (ptrace(PTRACE_TRACEME
, 0, 0, 0) != 0)
182 err(1, "PTRACE_TRACEME");
184 printf("\tChild will make one syscall\n");
187 syscall(SYS_gettid
, 10, 11, 12, 13, 14, 15);
193 /* Wait for SIGSTOP. */
194 if (waitpid(chld
, &status
, 0) != chld
|| !WIFSTOPPED(status
))
197 struct user_regs_struct regs
;
199 printf("[RUN]\tSYSEMU\n");
200 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
201 err(1, "PTRACE_SYSEMU");
204 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
205 err(1, "PTRACE_GETREGS");
207 if (regs
.user_syscall_nr
!= SYS_gettid
||
208 regs
.user_arg0
!= 10 || regs
.user_arg1
!= 11 ||
209 regs
.user_arg2
!= 12 || regs
.user_arg3
!= 13 ||
210 regs
.user_arg4
!= 14 || regs
.user_arg5
!= 15) {
211 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
);
214 printf("[OK]\tInitial nr and args are correct\n");
217 printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
218 (unsigned long)regs
.user_ip
);
221 * This does exactly what it appears to do if syscall is int80 or
222 * SYSCALL64. For SYSCALL32 or SYSENTER, though, this is highly
223 * magical. It needs to work so that ptrace and syscall restart
226 regs
.user_ax
= regs
.user_syscall_nr
;
228 if (ptrace(PTRACE_SETREGS
, chld
, 0, ®s
) != 0)
229 err(1, "PTRACE_SETREGS");
231 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
232 err(1, "PTRACE_SYSEMU");
235 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
236 err(1, "PTRACE_GETREGS");
238 if (regs
.user_syscall_nr
!= SYS_gettid
||
239 regs
.user_arg0
!= 10 || regs
.user_arg1
!= 11 ||
240 regs
.user_arg2
!= 12 || regs
.user_arg3
!= 13 ||
241 regs
.user_arg4
!= 14 || regs
.user_arg5
!= 15) {
242 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
);
245 printf("[OK]\tRestarted nr and args are correct\n");
248 printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
249 (unsigned long)regs
.user_ip
);
251 regs
.user_ax
= SYS_getpid
;
260 if (ptrace(PTRACE_SETREGS
, chld
, 0, ®s
) != 0)
261 err(1, "PTRACE_SETREGS");
263 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
264 err(1, "PTRACE_SYSEMU");
267 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
268 err(1, "PTRACE_GETREGS");
270 if (regs
.user_syscall_nr
!= SYS_getpid
||
271 regs
.user_arg0
!= 20 || regs
.user_arg1
!= 21 || regs
.user_arg2
!= 22 ||
272 regs
.user_arg3
!= 23 || regs
.user_arg4
!= 24 || regs
.user_arg5
!= 25) {
273 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
);
276 printf("[OK]\tReplacement nr and args are correct\n");
279 if (ptrace(PTRACE_CONT
, chld
, 0, 0) != 0)
280 err(1, "PTRACE_CONT");
281 if (waitpid(chld
, &status
, 0) != chld
)
283 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
284 printf("[FAIL]\tChild failed\n");
287 printf("[OK]\tChild exited cleanly\n");
291 static void test_restart_under_ptrace(void)
293 printf("[RUN]\tkernel syscall restart under ptrace\n");
299 if (ptrace(PTRACE_TRACEME
, 0, 0, 0) != 0)
300 err(1, "PTRACE_TRACEME");
302 printf("\tChild will take a nap until signaled\n");
303 setsigign(SIGUSR1
, SA_RESTART
);
306 syscall(SYS_pause
, 0, 0, 0, 0, 0, 0);
312 /* Wait for SIGSTOP. */
313 if (waitpid(chld
, &status
, 0) != chld
|| !WIFSTOPPED(status
))
316 struct user_regs_struct regs
;
318 printf("[RUN]\tSYSCALL\n");
319 if (ptrace(PTRACE_SYSCALL
, chld
, 0, 0) != 0)
320 err(1, "PTRACE_SYSCALL");
323 /* We should be stopped at pause(2) entry. */
325 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
326 err(1, "PTRACE_GETREGS");
328 if (regs
.user_syscall_nr
!= SYS_pause
||
329 regs
.user_arg0
!= 0 || regs
.user_arg1
!= 0 ||
330 regs
.user_arg2
!= 0 || regs
.user_arg3
!= 0 ||
331 regs
.user_arg4
!= 0 || regs
.user_arg5
!= 0) {
332 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
);
335 printf("[OK]\tInitial nr and args are correct\n");
341 /* Advance. We should be stopped at exit. */
342 printf("[RUN]\tSYSCALL\n");
343 if (ptrace(PTRACE_SYSCALL
, chld
, 0, 0) != 0)
344 err(1, "PTRACE_SYSCALL");
347 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
348 err(1, "PTRACE_GETREGS");
350 if (regs
.user_syscall_nr
!= SYS_pause
||
351 regs
.user_arg0
!= 0 || regs
.user_arg1
!= 0 ||
352 regs
.user_arg2
!= 0 || regs
.user_arg3
!= 0 ||
353 regs
.user_arg4
!= 0 || regs
.user_arg5
!= 0) {
354 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
);
357 printf("[OK]\tArgs after SIGUSR1 are correct (ax = %ld)\n",
361 /* Poke the regs back in. This must not break anything. */
362 if (ptrace(PTRACE_SETREGS
, chld
, 0, ®s
) != 0)
363 err(1, "PTRACE_SETREGS");
365 /* Catch the (ignored) SIGUSR1. */
366 if (ptrace(PTRACE_CONT
, chld
, 0, 0) != 0)
367 err(1, "PTRACE_CONT");
368 if (waitpid(chld
, &status
, 0) != chld
)
370 if (!WIFSTOPPED(status
)) {
371 printf("[FAIL]\tChild was stopped for SIGUSR1 (status = 0x%x)\n", status
);
374 printf("[OK]\tChild got SIGUSR1\n");
377 /* The next event should be pause(2) again. */
378 printf("[RUN]\tStep again\n");
379 if (ptrace(PTRACE_SYSCALL
, chld
, 0, 0) != 0)
380 err(1, "PTRACE_SYSCALL");
383 /* We should be stopped at pause(2) entry. */
385 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
386 err(1, "PTRACE_GETREGS");
388 if (regs
.user_syscall_nr
!= SYS_pause
||
389 regs
.user_arg0
!= 0 || regs
.user_arg1
!= 0 ||
390 regs
.user_arg2
!= 0 || regs
.user_arg3
!= 0 ||
391 regs
.user_arg4
!= 0 || regs
.user_arg5
!= 0) {
392 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
);
395 printf("[OK]\tpause(2) restarted correctly\n");
400 if (waitpid(chld
, &status
, 0) != chld
)
406 printf("[RUN]\tCheck int80 return regs\n");
407 test_sys32_regs(do_full_int80
);
409 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
410 vsyscall32
= (void *)getauxval(AT_SYSINFO
);
411 printf("[RUN]\tCheck AT_SYSINFO return regs\n");
412 test_sys32_regs(do_full_vsyscall32
);
415 test_ptrace_syscall_restart();
417 test_restart_under_ptrace();