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 pid_t pid
= getpid(), tid
= syscall(SYS_gettid
);
188 printf("\tChild will make one syscall\n");
189 syscall(SYS_tgkill
, pid
, tid
, SIGSTOP
);
191 syscall(SYS_gettid
, 10, 11, 12, 13, 14, 15);
197 /* Wait for SIGSTOP. */
198 if (waitpid(chld
, &status
, 0) != chld
|| !WIFSTOPPED(status
))
201 struct user_regs_struct regs
;
203 printf("[RUN]\tSYSEMU\n");
204 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
205 err(1, "PTRACE_SYSEMU");
208 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
209 err(1, "PTRACE_GETREGS");
211 if (regs
.user_syscall_nr
!= SYS_gettid
||
212 regs
.user_arg0
!= 10 || regs
.user_arg1
!= 11 ||
213 regs
.user_arg2
!= 12 || regs
.user_arg3
!= 13 ||
214 regs
.user_arg4
!= 14 || regs
.user_arg5
!= 15) {
215 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
);
218 printf("[OK]\tInitial nr and args are correct\n");
221 printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
222 (unsigned long)regs
.user_ip
);
225 * This does exactly what it appears to do if syscall is int80 or
226 * SYSCALL64. For SYSCALL32 or SYSENTER, though, this is highly
227 * magical. It needs to work so that ptrace and syscall restart
230 regs
.user_ax
= regs
.user_syscall_nr
;
232 if (ptrace(PTRACE_SETREGS
, chld
, 0, ®s
) != 0)
233 err(1, "PTRACE_SETREGS");
235 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
236 err(1, "PTRACE_SYSEMU");
239 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
240 err(1, "PTRACE_GETREGS");
242 if (regs
.user_syscall_nr
!= SYS_gettid
||
243 regs
.user_arg0
!= 10 || regs
.user_arg1
!= 11 ||
244 regs
.user_arg2
!= 12 || regs
.user_arg3
!= 13 ||
245 regs
.user_arg4
!= 14 || regs
.user_arg5
!= 15) {
246 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
);
249 printf("[OK]\tRestarted nr and args are correct\n");
252 printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
253 (unsigned long)regs
.user_ip
);
255 regs
.user_ax
= SYS_getpid
;
264 if (ptrace(PTRACE_SETREGS
, chld
, 0, ®s
) != 0)
265 err(1, "PTRACE_SETREGS");
267 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
268 err(1, "PTRACE_SYSEMU");
271 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
272 err(1, "PTRACE_GETREGS");
274 if (regs
.user_syscall_nr
!= SYS_getpid
||
275 regs
.user_arg0
!= 20 || regs
.user_arg1
!= 21 || regs
.user_arg2
!= 22 ||
276 regs
.user_arg3
!= 23 || regs
.user_arg4
!= 24 || regs
.user_arg5
!= 25) {
277 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
);
280 printf("[OK]\tReplacement nr and args are correct\n");
283 if (ptrace(PTRACE_CONT
, chld
, 0, 0) != 0)
284 err(1, "PTRACE_CONT");
285 if (waitpid(chld
, &status
, 0) != chld
)
287 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
288 printf("[FAIL]\tChild failed\n");
291 printf("[OK]\tChild exited cleanly\n");
295 static void test_restart_under_ptrace(void)
297 printf("[RUN]\tkernel syscall restart under ptrace\n");
303 if (ptrace(PTRACE_TRACEME
, 0, 0, 0) != 0)
304 err(1, "PTRACE_TRACEME");
306 pid_t pid
= getpid(), tid
= syscall(SYS_gettid
);
308 printf("\tChild will take a nap until signaled\n");
309 setsigign(SIGUSR1
, SA_RESTART
);
310 syscall(SYS_tgkill
, pid
, tid
, SIGSTOP
);
312 syscall(SYS_pause
, 0, 0, 0, 0, 0, 0);
318 /* Wait for SIGSTOP. */
319 if (waitpid(chld
, &status
, 0) != chld
|| !WIFSTOPPED(status
))
322 struct user_regs_struct regs
;
324 printf("[RUN]\tSYSCALL\n");
325 if (ptrace(PTRACE_SYSCALL
, chld
, 0, 0) != 0)
326 err(1, "PTRACE_SYSCALL");
329 /* We should be stopped at pause(2) entry. */
331 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
332 err(1, "PTRACE_GETREGS");
334 if (regs
.user_syscall_nr
!= SYS_pause
||
335 regs
.user_arg0
!= 0 || regs
.user_arg1
!= 0 ||
336 regs
.user_arg2
!= 0 || regs
.user_arg3
!= 0 ||
337 regs
.user_arg4
!= 0 || regs
.user_arg5
!= 0) {
338 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
);
341 printf("[OK]\tInitial nr and args are correct\n");
347 /* Advance. We should be stopped at exit. */
348 printf("[RUN]\tSYSCALL\n");
349 if (ptrace(PTRACE_SYSCALL
, chld
, 0, 0) != 0)
350 err(1, "PTRACE_SYSCALL");
353 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
354 err(1, "PTRACE_GETREGS");
356 if (regs
.user_syscall_nr
!= SYS_pause
||
357 regs
.user_arg0
!= 0 || regs
.user_arg1
!= 0 ||
358 regs
.user_arg2
!= 0 || regs
.user_arg3
!= 0 ||
359 regs
.user_arg4
!= 0 || regs
.user_arg5
!= 0) {
360 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
);
363 printf("[OK]\tArgs after SIGUSR1 are correct (ax = %ld)\n",
367 /* Poke the regs back in. This must not break anything. */
368 if (ptrace(PTRACE_SETREGS
, chld
, 0, ®s
) != 0)
369 err(1, "PTRACE_SETREGS");
371 /* Catch the (ignored) SIGUSR1. */
372 if (ptrace(PTRACE_CONT
, chld
, 0, 0) != 0)
373 err(1, "PTRACE_CONT");
374 if (waitpid(chld
, &status
, 0) != chld
)
376 if (!WIFSTOPPED(status
)) {
377 printf("[FAIL]\tChild was stopped for SIGUSR1 (status = 0x%x)\n", status
);
380 printf("[OK]\tChild got SIGUSR1\n");
383 /* The next event should be pause(2) again. */
384 printf("[RUN]\tStep again\n");
385 if (ptrace(PTRACE_SYSCALL
, chld
, 0, 0) != 0)
386 err(1, "PTRACE_SYSCALL");
389 /* We should be stopped at pause(2) entry. */
391 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
392 err(1, "PTRACE_GETREGS");
394 if (regs
.user_syscall_nr
!= SYS_pause
||
395 regs
.user_arg0
!= 0 || regs
.user_arg1
!= 0 ||
396 regs
.user_arg2
!= 0 || regs
.user_arg3
!= 0 ||
397 regs
.user_arg4
!= 0 || regs
.user_arg5
!= 0) {
398 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
);
401 printf("[OK]\tpause(2) restarted correctly\n");
406 if (waitpid(chld
, &status
, 0) != chld
)
412 printf("[RUN]\tCheck int80 return regs\n");
413 test_sys32_regs(do_full_int80
);
415 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
416 vsyscall32
= (void *)getauxval(AT_SYSINFO
);
417 printf("[RUN]\tCheck AT_SYSINFO return regs\n");
418 test_sys32_regs(do_full_vsyscall32
);
421 test_ptrace_syscall_restart();
423 test_restart_under_ptrace();