]> granicus.if.org Git - strace/blob - tests/ptrace.c
tests: remove manual creation of memory mapping holes
[strace] / tests / ptrace.c
1 /*
2  * Check decoding of ptrace syscall.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31 #include <sys/syscall.h>
32
33 #ifdef __NR_rt_sigprocmask
34
35 # include <errno.h>
36 # include <signal.h>
37 # include <stdio.h>
38 # include <string.h>
39 # include <sys/wait.h>
40 # include <unistd.h>
41 # include "ptrace.h"
42 # include <linux/audit.h>
43
44 static long
45 do_ptrace(unsigned long request, unsigned long pid,
46           unsigned long addr, unsigned long data)
47 {
48         return syscall(__NR_ptrace, request, pid, addr, data);
49 }
50
51 static void
52 test_peeksiginfo(unsigned long pid, const unsigned long bad_request)
53 {
54         long rc = do_ptrace(PTRACE_PEEKSIGINFO, pid, 0, bad_request);
55         printf("ptrace(PTRACE_PEEKSIGINFO, %u, NULL, %#lx)"
56                " = %ld %s (%m)\n", (unsigned) pid, bad_request, rc, errno2name());
57
58         struct {
59                 unsigned long long off;
60                 unsigned int flags, nr;
61         } *const psi = tail_alloc(sizeof(*psi));
62
63         psi->off = 0xdeadbeeffacefeed;
64         psi->flags = 1;
65         psi->nr = 42;
66
67         rc = do_ptrace(PTRACE_PEEKSIGINFO,
68                        pid, (unsigned long) psi, bad_request);
69         printf("ptrace(PTRACE_PEEKSIGINFO, %u, {off=%llu"
70                ", flags=PTRACE_PEEKSIGINFO_SHARED, nr=%u}, %#lx)"
71                " = %ld %s (%m)\n",
72                (unsigned) pid, psi->off, psi->nr, bad_request, rc, errno2name());
73
74         pid = fork();
75         if (pid < 0)
76                 perror_msg_and_fail("fork");
77
78         if (!pid) {
79                 sigset_t mask;
80                 sigemptyset(&mask);
81                 sigaddset(&mask, SIGUSR1);
82                 sigaddset(&mask, SIGUSR2);
83                 sigaddset(&mask, SIGALRM);
84
85                 if (sigprocmask(SIG_BLOCK, &mask, NULL))
86                         perror_msg_and_fail("sigprocmask");
87
88                 raise(SIGUSR1);
89                 raise(SIGUSR2);
90                 raise(SIGALRM);
91
92                 if (do_ptrace(PTRACE_TRACEME, 0, 0, 0))
93                         perror_msg_and_fail("child: PTRACE_TRACEME");
94
95                 raise(SIGSTOP);
96                 _exit(0);
97         }
98
99         const unsigned int nsigs = 4;
100         const uid_t uid = geteuid();
101         siginfo_t *sigs = tail_alloc(sizeof(*sigs) * nsigs);
102
103         psi->off = 0;
104         psi->flags = 0;
105         psi->nr = nsigs;
106
107         for (;;) {
108                 int status, tracee, saved;
109
110                 errno = 0;
111                 tracee = wait(&status);
112                 if (tracee <= 0) {
113                         if (errno == EINTR)
114                                 continue;
115                         saved = errno;
116                         kill (pid, SIGKILL);
117                         errno = saved;
118                         perror_msg_and_fail("wait");
119                 }
120                 if (WIFEXITED(status)) {
121                         if (WEXITSTATUS(status) == 0)
122                                 break;
123                         error_msg_and_fail("unexpected exit status %u",
124                                            WEXITSTATUS(status));
125                 }
126                 if (WIFSIGNALED(status))
127                         error_msg_and_fail("unexpected signal %u",
128                                            WTERMSIG(status));
129                 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
130                         kill(pid, SIGKILL);
131                         error_msg_and_fail("unexpected wait status %x",
132                                            status);
133                 }
134
135                 rc = do_ptrace(PTRACE_PEEKSIGINFO, pid,
136                                (unsigned long) psi, (unsigned long) sigs);
137                 if (rc < 0) {
138                         printf("ptrace(PTRACE_PEEKSIGINFO, %u, {off=%llu"
139                                ", flags=0, nr=%u}, %p) = %ld %s (%m)\n",
140                                (unsigned) pid, psi->off, psi->nr, sigs,
141                                rc, errno2name());
142                 } else {
143                         printf("ptrace(PTRACE_PEEKSIGINFO, %u, {off=%llu"
144                                ", flags=0, nr=%u}"
145                                ", [{si_signo=SIGUSR1, si_code=SI_TKILL"
146                                ", si_pid=%u, si_uid=%u}"
147                                ", {si_signo=SIGUSR2, si_code=SI_TKILL"
148                                ", si_pid=%u, si_uid=%u}"
149                                ", {si_signo=SIGALRM, si_code=SI_TKILL"
150                                ", si_pid=%u, si_uid=%u}"
151                                "]) = %ld\n",
152                                (unsigned) pid, psi->off, psi->nr,
153                                (unsigned) pid, (unsigned) uid,
154                                (unsigned) pid, (unsigned) uid,
155                                (unsigned) pid, (unsigned) uid,
156                                rc);
157                 }
158
159                 if (do_ptrace(PTRACE_CONT, pid, 0, 0)) {
160                         saved = errno;
161                         kill (pid, SIGKILL);
162                         errno = saved;
163                         perror_msg_and_fail("ptrace");
164                 }
165                 printf("ptrace(PTRACE_CONT, %ld, NULL, SIG_0) = 0\n", pid);
166         }
167 }
168
169 int
170 main(void)
171 {
172         const unsigned long bad_request =
173                 (unsigned long) 0xdeadbeeffffffeed;
174         const unsigned long bad_data =
175                 (unsigned long) 0xdeadcafefffff00d;
176         const unsigned long pid =
177                 (unsigned long) 0xdefaced00000000 | (unsigned) getpid();
178
179         unsigned int sigset_size;
180
181         for (sigset_size = 1024 / 8; sigset_size; sigset_size >>= 1) {
182                 if (!syscall(__NR_rt_sigprocmask,
183                              SIG_SETMASK, NULL, NULL, sigset_size))
184                         break;
185         }
186         if (!sigset_size)
187                 perror_msg_and_fail("rt_sigprocmask");
188
189         void *const k_set = tail_alloc(sigset_size);
190         siginfo_t *const sip = tail_alloc(sizeof(*sip));
191
192         long rc = do_ptrace(bad_request, pid, 0, 0);
193         printf("ptrace(%#lx /* PTRACE_??? */, %u, NULL, NULL) = %ld %s (%m)\n",
194                bad_request, (unsigned) pid, rc, errno2name());
195
196         rc = do_ptrace(PTRACE_PEEKDATA, pid, bad_request, bad_data);
197 # ifdef IA64
198         printf("ptrace(PTRACE_PEEKDATA, %u, %#lx)"
199                " = %ld %s (%m)\n",
200                (unsigned) pid, bad_request, rc, errno2name());
201 # else
202         printf("ptrace(PTRACE_PEEKDATA, %u, %#lx, %#lx)"
203                " = %ld %s (%m)\n",
204                (unsigned) pid, bad_request, bad_data, rc, errno2name());
205 #endif
206
207         rc = do_ptrace(PTRACE_PEEKTEXT, pid, bad_request, bad_data);
208 # ifdef IA64
209         printf("ptrace(PTRACE_PEEKTEXT, %u, %#lx)"
210                " = %ld %s (%m)\n",
211                (unsigned) pid, bad_request, rc, errno2name());
212 # else
213         printf("ptrace(PTRACE_PEEKTEXT, %u, %#lx, %#lx)"
214                " = %ld %s (%m)\n",
215                (unsigned) pid, bad_request, bad_data, rc, errno2name());
216 #endif
217
218         rc = do_ptrace(PTRACE_PEEKUSER, pid, bad_request, bad_data);
219 # ifdef IA64
220         printf("ptrace(PTRACE_PEEKUSER, %u, %#lx)"
221                " = %ld %s (%m)\n",
222                (unsigned) pid, bad_request, rc, errno2name());
223 # else
224         printf("ptrace(PTRACE_PEEKUSER, %u, %#lx, %#lx)"
225                " = %ld %s (%m)\n",
226                (unsigned) pid, bad_request, bad_data, rc, errno2name());
227 #endif
228
229         rc = do_ptrace(PTRACE_POKEUSER, pid, bad_request, bad_data);
230         printf("ptrace(PTRACE_POKEUSER, %u, %#lx, %#lx)"
231                " = %ld %s (%m)\n",
232                (unsigned) pid, bad_request, bad_data, rc, errno2name());
233
234         rc = do_ptrace(PTRACE_ATTACH, pid, 0, 0);
235         printf("ptrace(PTRACE_ATTACH, %u) = %ld %s (%m)\n",
236                (unsigned) pid, rc, errno2name());
237
238         rc = do_ptrace(PTRACE_INTERRUPT, pid, 0, 0);
239         printf("ptrace(PTRACE_INTERRUPT, %u) = %ld %s (%m)\n",
240                (unsigned) pid, rc, errno2name());
241
242         rc = do_ptrace(PTRACE_KILL, pid, 0, 0);
243         printf("ptrace(PTRACE_KILL, %u) = %ld %s (%m)\n",
244                (unsigned) pid, rc, errno2name());
245
246         rc = do_ptrace(PTRACE_LISTEN, pid, 0, 0);
247         printf("ptrace(PTRACE_LISTEN, %u) = %ld %s (%m)\n",
248                (unsigned) pid, rc, errno2name());
249
250         sigset_t libc_set;
251         sigemptyset(&libc_set);
252         sigaddset(&libc_set, SIGUSR1);
253         memcpy(k_set, &libc_set, sigset_size);
254
255         rc = do_ptrace(PTRACE_SETSIGMASK,
256                        pid, sigset_size, (unsigned long) k_set);
257         printf("ptrace(PTRACE_SETSIGMASK, %u, %u, [USR1])"
258                " = %ld %s (%m)\n",
259                (unsigned) pid, sigset_size, rc, errno2name());
260
261         rc = do_ptrace(PTRACE_GETSIGMASK,
262                        pid, sigset_size, (unsigned long) k_set);
263         printf("ptrace(PTRACE_GETSIGMASK, %u, %u, %p)"
264                " = %ld %s (%m)\n",
265                (unsigned) pid, sigset_size, k_set, rc, errno2name());
266
267         rc = do_ptrace(PTRACE_SECCOMP_GET_FILTER, pid, 42, 0);
268         printf("ptrace(PTRACE_SECCOMP_GET_FILTER, %u, 42, NULL)"
269                " = %ld %s (%m)\n", (unsigned) pid, rc, errno2name());
270
271         rc = do_ptrace(PTRACE_GETEVENTMSG, pid, bad_request, bad_data);
272         printf("ptrace(PTRACE_GETEVENTMSG, %u, %#lx, %#lx)"
273                " = %ld %s (%m)\n",
274                (unsigned) pid, bad_request, bad_data, rc, errno2name());
275
276         memset(sip, -1, sizeof(*sip));
277         sip->si_signo = SIGIO;
278         sip->si_code = 1;
279         sip->si_errno = ENOENT;
280         sip->si_band = -2;
281
282         rc = do_ptrace(PTRACE_SETSIGINFO,
283                        pid, bad_request, (unsigned long) sip);
284         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGIO"
285                ", si_code=POLL_IN, si_errno=ENOENT, si_band=-2})"
286                " = %ld %s (%m)\n",
287                (unsigned) pid, bad_request, rc, errno2name());
288
289         memset(sip, -1, sizeof(*sip));
290         sip->si_signo = SIGTRAP;
291         sip->si_code = 1;
292         sip->si_errno = ENOENT;
293         sip->si_pid = 2;
294         sip->si_uid = 3;
295         sip->si_ptr = (void *) bad_request;
296
297         rc = do_ptrace(PTRACE_SETSIGINFO,
298                        pid, bad_request, (unsigned long) sip);
299         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGTRAP"
300                ", si_code=TRAP_BRKPT, si_errno=ENOENT, si_pid=2, si_uid=3"
301                ", si_value={int=%d, ptr=%p}}) = %ld %s (%m)\n",
302                (unsigned) pid, bad_request, sip->si_int, sip->si_ptr, rc, errno2name());
303
304         memset(sip, -1, sizeof(*sip));
305         sip->si_signo = SIGILL;
306         sip->si_code = 1;
307         sip->si_errno = ENOENT;
308         sip->si_addr = (void *) (unsigned long) 0xfacefeeddeadbeef;
309
310         rc = do_ptrace(PTRACE_SETSIGINFO,
311                        pid, bad_request, (unsigned long) sip);
312         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGILL"
313                ", si_code=ILL_ILLOPC, si_errno=ENOENT, si_addr=%p})"
314                " = %ld %s (%m)\n",
315                (unsigned) pid, bad_request, sip->si_addr, rc, errno2name());
316
317         memset(sip, -1, sizeof(*sip));
318         sip->si_signo = SIGFPE;
319         sip->si_code = 1;
320         sip->si_errno = ENOENT;
321         sip->si_addr = (void *) (unsigned long) 0xfacefeeddeadbeef;
322
323         rc = do_ptrace(PTRACE_SETSIGINFO,
324                        pid, bad_request, (unsigned long) sip);
325         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGFPE"
326                ", si_code=FPE_INTDIV, si_errno=ENOENT, si_addr=%p})"
327                " = %ld %s (%m)\n",
328                (unsigned) pid, bad_request, sip->si_addr, rc, errno2name());
329
330         memset(sip, -1, sizeof(*sip));
331         sip->si_signo = SIGBUS;
332         sip->si_code = 1;
333         sip->si_errno = -2;
334         sip->si_addr = (void *) (unsigned long) 0xfacefeeddeadbeef;
335
336         rc = do_ptrace(PTRACE_SETSIGINFO,
337                        pid, bad_request, (unsigned long) sip);
338         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGBUS"
339                ", si_code=BUS_ADRALN, si_errno=%d, si_addr=%p})"
340                " = %ld %s (%m)\n",
341                (unsigned) pid, bad_request, sip->si_errno, sip->si_addr,
342                rc, errno2name());
343
344         memset(sip, -1, sizeof(*sip));
345         sip->si_signo = SIGPROF;
346         sip->si_code = 0xbadc0ded;
347         sip->si_errno = -2;
348         sip->si_pid = 0;
349         sip->si_uid = 3;
350         sip->si_ptr = 0;
351
352         rc = do_ptrace(PTRACE_SETSIGINFO,
353                        pid, bad_request, (unsigned long) sip);
354         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGPROF"
355                ", si_code=%#x, si_errno=%d, si_pid=0, si_uid=3})"
356                " = %ld %s (%m)\n",
357                (unsigned) pid, bad_request, sip->si_code, sip->si_errno,
358                rc, errno2name());
359
360 #ifdef HAVE_SIGINFO_T_SI_SYSCALL
361         memset(sip, -1, sizeof(*sip));
362         sip->si_signo = SIGSYS;
363         sip->si_code = 1;
364         sip->si_errno = ENOENT;
365         sip->si_call_addr = (void *) (unsigned long) 0xfacefeeddeadbeef;
366         sip->si_syscall = -1U;
367         sip->si_arch = AUDIT_ARCH_X86_64;
368
369         rc = do_ptrace(PTRACE_SETSIGINFO,
370                        pid, bad_request, (unsigned long) sip);
371         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGSYS"
372                ", si_code=SYS_SECCOMP, si_errno=ENOENT, si_call_addr=%p"
373                ", si_syscall=__NR_syscall_%u, si_arch=AUDIT_ARCH_X86_64})"
374                " = %ld %s (%m)\n",
375                (unsigned) pid, bad_request, sip->si_call_addr, sip->si_syscall,
376                rc, errno2name());
377 #endif
378
379 #if defined HAVE_SIGINFO_T_SI_TIMERID && defined HAVE_SIGINFO_T_SI_OVERRUN
380         memset(sip, -1, sizeof(*sip));
381         sip->si_signo = SIGHUP;
382         sip->si_code = SI_TIMER;
383         sip->si_errno = ENOENT;
384         sip->si_timerid = 0xdeadbeef;
385         sip->si_overrun = -1;
386         sip->si_ptr = (void *) (unsigned long) 0xfacefeeddeadbeef;
387
388         rc = do_ptrace(PTRACE_SETSIGINFO,
389                        pid, bad_request, (unsigned long) sip);
390         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGHUP"
391                ", si_code=SI_TIMER, si_errno=ENOENT, si_timerid=%#x"
392                ", si_overrun=%d, si_value={int=%d, ptr=%p}}) = %ld %s (%m)\n",
393                (unsigned) pid, bad_request, sip->si_timerid, sip->si_overrun,
394                sip->si_int, sip->si_ptr, rc, errno2name());
395 #endif
396
397         rc = do_ptrace(PTRACE_GETSIGINFO,
398                        pid, bad_request, (unsigned long) sip);
399         printf("ptrace(PTRACE_GETSIGINFO, %u, %#lx, %p)"
400                " = %ld %s (%m)\n", (unsigned) pid, bad_request, sip, rc, errno2name());
401
402         rc = do_ptrace(PTRACE_CONT, pid, 0, SIGUSR1);
403         printf("ptrace(PTRACE_CONT, %u, NULL, SIGUSR1) = %ld %s (%m)\n",
404                (unsigned) pid, rc, errno2name());
405
406         rc = do_ptrace(PTRACE_DETACH, pid, 0, SIGUSR2);
407         printf("ptrace(PTRACE_DETACH, %u, NULL, SIGUSR2) = %ld %s (%m)\n",
408                (unsigned) pid, rc, errno2name());
409
410         rc = do_ptrace(PTRACE_SYSCALL, pid, 0, SIGUSR1);
411         printf("ptrace(PTRACE_SYSCALL, %u, NULL, SIGUSR1) = %ld %s (%m)\n",
412                (unsigned) pid, rc, errno2name());
413
414 #ifdef PTRACE_SINGLESTEP
415         rc = do_ptrace(PTRACE_SINGLESTEP, pid, 0, SIGUSR2);
416         printf("ptrace(PTRACE_SINGLESTEP, %u, NULL, SIGUSR2) = %ld %s (%m)\n",
417                (unsigned) pid, rc, errno2name());
418 #endif
419
420 #ifdef PTRACE_SINGLEBLOCK
421         rc = do_ptrace(PTRACE_SINGLEBLOCK, pid, 0, SIGUSR1);
422         printf("ptrace(PTRACE_SINGLEBLOCK, %u, NULL, SIGUSR1) = %ld %s (%m)\n",
423                (unsigned) pid, rc, errno2name());
424 #endif
425
426 #ifdef PTRACE_SYSEMU
427         rc = do_ptrace(PTRACE_SYSEMU, pid, 0, SIGUSR2);
428         printf("ptrace(PTRACE_SYSEMU, %u, NULL, SIGUSR2) = %ld %s (%m)\n",
429                (unsigned) pid, rc, errno2name());
430 #endif
431 #ifdef PTRACE_SYSEMU_SINGLESTEP
432         rc = do_ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, SIGUSR1);
433         printf("ptrace(PTRACE_SYSEMU_SINGLESTEP, %u, NULL, SIGUSR1)"
434                " = %ld %s (%m)\n", (unsigned) pid, rc, errno2name());
435 #endif
436
437         rc = do_ptrace(PTRACE_SETOPTIONS,
438                        pid, 0, PTRACE_O_TRACEFORK|PTRACE_O_TRACECLONE);
439         printf("ptrace(PTRACE_SETOPTIONS, %u, NULL"
440                ", PTRACE_O_TRACEFORK|PTRACE_O_TRACECLONE) = %ld %s (%m)\n",
441                (unsigned) pid, rc, errno2name());
442
443         rc = do_ptrace(PTRACE_SEIZE,
444                        pid, bad_request, PTRACE_O_TRACESYSGOOD);
445         printf("ptrace(PTRACE_SEIZE, %u, %#lx"
446                ", PTRACE_O_TRACESYSGOOD) = %ld %s (%m)\n",
447                (unsigned) pid, bad_request, rc, errno2name());
448
449         rc = do_ptrace(PTRACE_SETREGSET, pid, 1, bad_request);
450         printf("ptrace(PTRACE_SETREGSET, %u, NT_PRSTATUS, %#lx)"
451                " = %ld %s (%m)\n",
452                (unsigned) pid, bad_request, rc, errno2name());
453
454         rc = do_ptrace(PTRACE_GETREGSET, pid, 3, bad_request);
455         printf("ptrace(PTRACE_GETREGSET, %u, NT_PRPSINFO, %#lx)"
456                " = %ld %s (%m)\n",
457                (unsigned) pid, bad_request, rc, errno2name());
458
459         test_peeksiginfo(pid, bad_request);
460
461         rc = do_ptrace(PTRACE_TRACEME, 0, 0, 0);
462         if (rc)
463                 printf("ptrace(PTRACE_TRACEME) = %ld %s (%m)\n",
464                        rc, errno2name());
465         else
466                 printf("ptrace(PTRACE_TRACEME) = 0\n");
467
468         puts("+++ exited with 0 +++");
469         return 0;
470 }
471
472
473 #else
474
475 SKIP_MAIN_UNDEFINED("__NR_rt_sigprocmask")
476
477 #endif