GNU Linux-libre 4.9.328-gnu1
[releases.git] / tools / testing / selftests / x86 / ptrace_syscall.c
1 #define _GNU_SOURCE
2
3 #include <sys/ptrace.h>
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <sys/syscall.h>
7 #include <sys/user.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <err.h>
13 #include <string.h>
14 #include <asm/ptrace-abi.h>
15 #include <sys/auxv.h>
16
17 /* Bitness-agnostic defines for user_regs_struct fields. */
18 #ifdef __x86_64__
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
24 # define user_arg4              r8
25 # define user_arg5              r9
26 # define user_ip                rip
27 # define user_ax                rax
28 #else
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
36 # define user_ip                eip
37 # define user_ax                eax
38 #endif
39
40 static int nerrs = 0;
41
42 struct syscall_args32 {
43         uint32_t nr, arg0, arg1, arg2, arg3, arg4, arg5;
44 };
45
46 #ifdef __i386__
47 extern void sys32_helper(struct syscall_args32 *, void *);
48 extern void int80_and_ret(void);
49 #endif
50
51 /*
52  * Helper to invoke int80 with controlled regs and capture the final regs.
53  */
54 static void do_full_int80(struct syscall_args32 *args)
55 {
56 #ifdef __x86_64__
57         register unsigned long bp asm("bp") = args->arg5;
58         asm volatile ("int $0x80"
59                       : "+a" (args->nr),
60                         "+b" (args->arg0), "+c" (args->arg1), "+d" (args->arg2),
61                         "+S" (args->arg3), "+D" (args->arg4), "+r" (bp)
62                         : : "r8", "r9", "r10", "r11");
63         args->arg5 = bp;
64 #else
65         sys32_helper(args, int80_and_ret);
66 #endif
67 }
68
69 #ifdef __i386__
70 static void (*vsyscall32)(void);
71
72 /*
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 :)
76  */
77 static void do_full_vsyscall32(struct syscall_args32 *args)
78 {
79         sys32_helper(args, vsyscall32);
80 }
81 #endif
82
83 static siginfo_t wait_trap(pid_t chld)
84 {
85         siginfo_t si;
86         if (waitid(P_PID, chld, &si, WEXITED|WSTOPPED) != 0)
87                 err(1, "waitid");
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);
92         return si;
93 }
94
95 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
96                        int flags)
97 {
98         struct sigaction sa;
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))
104                 err(1, "sigaction");
105 }
106
107 static void setsigign(int sig, int flags)
108 {
109         struct sigaction sa;
110         memset(&sa, 0, sizeof(sa));
111         sa.sa_sigaction = (void *)SIG_IGN;
112         sa.sa_flags = flags;
113         sigemptyset(&sa.sa_mask);
114         if (sigaction(sig, &sa, 0))
115                 err(1, "sigaction");
116 }
117
118 static void clearhandler(int sig)
119 {
120         struct sigaction sa;
121         memset(&sa, 0, sizeof(sa));
122         sa.sa_handler = SIG_DFL;
123         sigemptyset(&sa.sa_mask);
124         if (sigaction(sig, &sa, 0))
125                 err(1, "sigaction");
126 }
127
128 #ifdef __x86_64__
129 # define REG_BP REG_RBP
130 #else
131 # define REG_BP REG_EBP
132 #endif
133
134 static void empty_handler(int sig, siginfo_t *si, void *ctx_void)
135 {
136 }
137
138 static void test_sys32_regs(void (*do_syscall)(struct syscall_args32 *))
139 {
140         struct syscall_args32 args = {
141                 .nr = 224,      /* gettid */
142                 .arg0 = 10, .arg1 = 11, .arg2 = 12,
143                 .arg3 = 13, .arg4 = 14, .arg5 = 15,
144         };
145
146         do_syscall(&args);
147
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");
152                 nerrs++;
153         } else {
154                 printf("[OK]\tgetpid() preserves regs\n");
155         }
156
157         sethandler(SIGUSR1, empty_handler, 0);
158
159         args.nr = 37;   /* kill */
160         args.arg0 = getpid();
161         args.arg1 = SIGUSR1;
162         do_syscall(&args);
163         if (args.nr != 0 ||
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");
167                 nerrs++;
168         } else {
169                 printf("[OK]\tkill(getpid(), SIGUSR1) preserves regs\n");
170         }
171         clearhandler(SIGUSR1);
172 }
173
174 static void test_ptrace_syscall_restart(void)
175 {
176         printf("[RUN]\tptrace-induced syscall restart\n");
177         pid_t chld = fork();
178         if (chld < 0)
179                 err(1, "fork");
180
181         if (chld == 0) {
182                 if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
183                         err(1, "PTRACE_TRACEME");
184
185                 pid_t pid = getpid(), tid = syscall(SYS_gettid);
186
187                 printf("\tChild will make one syscall\n");
188                 syscall(SYS_tgkill, pid, tid, SIGSTOP);
189
190                 syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
191                 _exit(0);
192         }
193
194         int status;
195
196         /* Wait for SIGSTOP. */
197         if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
198                 err(1, "waitpid");
199
200         struct user_regs_struct regs;
201
202         printf("[RUN]\tSYSEMU\n");
203         if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
204                 err(1, "PTRACE_SYSEMU");
205         wait_trap(chld);
206
207         if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
208                 err(1, "PTRACE_GETREGS");
209
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);
215                 nerrs++;
216         } else {
217                 printf("[OK]\tInitial nr and args are correct\n");
218         }
219
220         printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
221                (unsigned long)regs.user_ip);
222
223         /*
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
227          * work as expected.
228          */
229         regs.user_ax = regs.user_syscall_nr;
230         regs.user_ip -= 2;
231         if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
232                 err(1, "PTRACE_SETREGS");
233
234         if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
235                 err(1, "PTRACE_SYSEMU");
236         wait_trap(chld);
237
238         if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
239                 err(1, "PTRACE_GETREGS");
240
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);
246                 nerrs++;
247         } else {
248                 printf("[OK]\tRestarted nr and args are correct\n");
249         }
250
251         printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
252                (unsigned long)regs.user_ip);
253
254         regs.user_ax = SYS_getpid;
255         regs.user_arg0 = 20;
256         regs.user_arg1 = 21;
257         regs.user_arg2 = 22;
258         regs.user_arg3 = 23;
259         regs.user_arg4 = 24;
260         regs.user_arg5 = 25;
261         regs.user_ip -= 2;
262
263         if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
264                 err(1, "PTRACE_SETREGS");
265
266         if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
267                 err(1, "PTRACE_SYSEMU");
268         wait_trap(chld);
269
270         if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
271                 err(1, "PTRACE_GETREGS");
272
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);
277                 nerrs++;
278         } else {
279                 printf("[OK]\tReplacement nr and args are correct\n");
280         }
281
282         if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
283                 err(1, "PTRACE_CONT");
284         if (waitpid(chld, &status, 0) != chld)
285                 err(1, "waitpid");
286         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
287                 printf("[FAIL]\tChild failed\n");
288                 nerrs++;
289         } else {
290                 printf("[OK]\tChild exited cleanly\n");
291         }
292 }
293
294 static void test_restart_under_ptrace(void)
295 {
296         printf("[RUN]\tkernel syscall restart under ptrace\n");
297         pid_t chld = fork();
298         if (chld < 0)
299                 err(1, "fork");
300
301         if (chld == 0) {
302                 if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
303                         err(1, "PTRACE_TRACEME");
304
305                 pid_t pid = getpid(), tid = syscall(SYS_gettid);
306
307                 printf("\tChild will take a nap until signaled\n");
308                 setsigign(SIGUSR1, SA_RESTART);
309                 syscall(SYS_tgkill, pid, tid, SIGSTOP);
310
311                 syscall(SYS_pause, 0, 0, 0, 0, 0, 0);
312                 _exit(0);
313         }
314
315         int status;
316
317         /* Wait for SIGSTOP. */
318         if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
319                 err(1, "waitpid");
320
321         struct user_regs_struct regs;
322
323         printf("[RUN]\tSYSCALL\n");
324         if (ptrace(PTRACE_SYSCALL, chld, 0, 0) != 0)
325                 err(1, "PTRACE_SYSCALL");
326         wait_trap(chld);
327
328         /* We should be stopped at pause(2) entry. */
329
330         if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
331                 err(1, "PTRACE_GETREGS");
332
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);
338                 nerrs++;
339         } else {
340                 printf("[OK]\tInitial nr and args are correct\n");
341         }
342
343         /* Interrupt it. */
344         kill(chld, SIGUSR1);
345
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");
350         wait_trap(chld);
351
352         if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
353                 err(1, "PTRACE_GETREGS");
354
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);
360                 nerrs++;
361         } else {
362                 printf("[OK]\tArgs after SIGUSR1 are correct (ax = %ld)\n",
363                        (long)regs.user_ax);
364         }
365
366         /* Poke the regs back in.  This must not break anything. */
367         if (ptrace(PTRACE_SETREGS, chld, 0, &regs) != 0)
368                 err(1, "PTRACE_SETREGS");
369
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)
374                 err(1, "waitpid");
375         if (!WIFSTOPPED(status)) {
376                 printf("[FAIL]\tChild was stopped for SIGUSR1 (status = 0x%x)\n", status);
377                 nerrs++;
378         } else {
379                 printf("[OK]\tChild got SIGUSR1\n");
380         }
381
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");
386         wait_trap(chld);
387
388         /* We should be stopped at pause(2) entry. */
389
390         if (ptrace(PTRACE_GETREGS, chld, 0, &regs) != 0)
391                 err(1, "PTRACE_GETREGS");
392
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);
398                 nerrs++;
399         } else {
400                 printf("[OK]\tpause(2) restarted correctly\n");
401         }
402
403         /* Kill it. */
404         kill(chld, SIGKILL);
405         if (waitpid(chld, &status, 0) != chld)
406                 err(1, "waitpid");
407 }
408
409 int main()
410 {
411         printf("[RUN]\tCheck int80 return regs\n");
412         test_sys32_regs(do_full_int80);
413
414 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16)
415         vsyscall32 = (void *)getauxval(AT_SYSINFO);
416         if (vsyscall32) {
417                 printf("[RUN]\tCheck AT_SYSINFO return regs\n");
418                 test_sys32_regs(do_full_vsyscall32);
419         } else {
420                 printf("[SKIP]\tAT_SYSINFO is not available\n");
421         }
422 #endif
423
424         test_ptrace_syscall_restart();
425
426         test_restart_under_ptrace();
427
428         return 0;
429 }