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 clearhandler(int sig
)
109 memset(&sa
, 0, sizeof(sa
));
110 sa
.sa_handler
= SIG_DFL
;
111 sigemptyset(&sa
.sa_mask
);
112 if (sigaction(sig
, &sa
, 0))
117 # define REG_BP REG_RBP
119 # define REG_BP REG_EBP
122 static void empty_handler(int sig
, siginfo_t
*si
, void *ctx_void
)
126 static void test_sys32_regs(void (*do_syscall
)(struct syscall_args32
*))
128 struct syscall_args32 args
= {
129 .nr
= 224, /* gettid */
130 .arg0
= 10, .arg1
= 11, .arg2
= 12,
131 .arg3
= 13, .arg4
= 14, .arg5
= 15,
136 if (args
.nr
!= getpid() ||
137 args
.arg0
!= 10 || args
.arg1
!= 11 || args
.arg2
!= 12 ||
138 args
.arg3
!= 13 || args
.arg4
!= 14 || args
.arg5
!= 15) {
139 printf("[FAIL]\tgetpid() failed to preseve regs\n");
142 printf("[OK]\tgetpid() preserves regs\n");
145 sethandler(SIGUSR1
, empty_handler
, 0);
147 args
.nr
= 37; /* kill */
148 args
.arg0
= getpid();
152 args
.arg0
!= getpid() || args
.arg1
!= SIGUSR1
|| args
.arg2
!= 12 ||
153 args
.arg3
!= 13 || args
.arg4
!= 14 || args
.arg5
!= 15) {
154 printf("[FAIL]\tkill(getpid(), SIGUSR1) failed to preseve regs\n");
157 printf("[OK]\tkill(getpid(), SIGUSR1) preserves regs\n");
159 clearhandler(SIGUSR1
);
162 static void test_ptrace_syscall_restart(void)
164 printf("[RUN]\tptrace-induced syscall restart\n");
170 if (ptrace(PTRACE_TRACEME
, 0, 0, 0) != 0)
171 err(1, "PTRACE_TRACEME");
173 printf("\tChild will make one syscall\n");
176 syscall(SYS_gettid
, 10, 11, 12, 13, 14, 15);
182 /* Wait for SIGSTOP. */
183 if (waitpid(chld
, &status
, 0) != chld
|| !WIFSTOPPED(status
))
186 struct user_regs_struct regs
;
188 printf("[RUN]\tSYSEMU\n");
189 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
190 err(1, "PTRACE_SYSCALL");
193 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
194 err(1, "PTRACE_GETREGS");
196 if (regs
.user_syscall_nr
!= SYS_gettid
||
197 regs
.user_arg0
!= 10 || regs
.user_arg1
!= 11 ||
198 regs
.user_arg2
!= 12 || regs
.user_arg3
!= 13 ||
199 regs
.user_arg4
!= 14 || regs
.user_arg5
!= 15) {
200 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
);
203 printf("[OK]\tInitial nr and args are correct\n");
206 printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
207 (unsigned long)regs
.user_ip
);
210 * This does exactly what it appears to do if syscall is int80 or
211 * SYSCALL64. For SYSCALL32 or SYSENTER, though, this is highly
212 * magical. It needs to work so that ptrace and syscall restart
215 regs
.user_ax
= regs
.user_syscall_nr
;
217 if (ptrace(PTRACE_SETREGS
, chld
, 0, ®s
) != 0)
218 err(1, "PTRACE_SETREGS");
220 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
221 err(1, "PTRACE_SYSCALL");
224 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
225 err(1, "PTRACE_GETREGS");
227 if (regs
.user_syscall_nr
!= SYS_gettid
||
228 regs
.user_arg0
!= 10 || regs
.user_arg1
!= 11 ||
229 regs
.user_arg2
!= 12 || regs
.user_arg3
!= 13 ||
230 regs
.user_arg4
!= 14 || regs
.user_arg5
!= 15) {
231 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
);
234 printf("[OK]\tRestarted nr and args are correct\n");
237 printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
238 (unsigned long)regs
.user_ip
);
240 regs
.user_ax
= SYS_getpid
;
249 if (ptrace(PTRACE_SETREGS
, chld
, 0, ®s
) != 0)
250 err(1, "PTRACE_SETREGS");
252 if (ptrace(PTRACE_SYSEMU
, chld
, 0, 0) != 0)
253 err(1, "PTRACE_SYSCALL");
256 if (ptrace(PTRACE_GETREGS
, chld
, 0, ®s
) != 0)
257 err(1, "PTRACE_GETREGS");
259 if (regs
.user_syscall_nr
!= SYS_getpid
||
260 regs
.user_arg0
!= 20 || regs
.user_arg1
!= 21 || regs
.user_arg2
!= 22 ||
261 regs
.user_arg3
!= 23 || regs
.user_arg4
!= 24 || regs
.user_arg5
!= 25) {
262 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
);
265 printf("[OK]\tReplacement nr and args are correct\n");
268 if (ptrace(PTRACE_CONT
, chld
, 0, 0) != 0)
269 err(1, "PTRACE_CONT");
270 if (waitpid(chld
, &status
, 0) != chld
)
272 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
273 printf("[FAIL]\tChild failed\n");
276 printf("[OK]\tChild exited cleanly\n");
282 printf("[RUN]\tCheck int80 return regs\n");
283 test_sys32_regs(do_full_int80
);
285 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
286 vsyscall32
= (void *)getauxval(AT_SYSINFO
);
287 printf("[RUN]\tCheck AT_SYSINFO return regs\n");
288 test_sys32_regs(do_full_vsyscall32
);
291 test_ptrace_syscall_restart();