]> granicus.if.org Git - strace/blob - tests/ptrace.c
Update copyright headers
[strace] / tests / ptrace.c
1 /*
2  * Check decoding of ptrace syscall.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2018 The strace developers.
6  * All rights reserved.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "tests.h"
12 #include <asm/unistd.h>
13
14 #include <errno.h>
15 #include "ptrace.h"
16 #include <inttypes.h>
17 #include <signal.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <linux/audit.h>
24
25 static const char *errstr;
26
27 static long
28 do_ptrace(unsigned long request, unsigned long pid,
29           unsigned long addr, unsigned long data)
30 {
31         long rc = syscall(__NR_ptrace, request, pid, addr, data);
32         errstr = sprintrc(rc);
33         return rc;
34 }
35
36 static void
37 test_peeksiginfo(unsigned long pid, const unsigned long bad_request)
38 {
39         do_ptrace(PTRACE_PEEKSIGINFO, pid, 0, bad_request);
40         printf("ptrace(PTRACE_PEEKSIGINFO, %u, NULL, %#lx) = %s\n",
41                (unsigned) pid, bad_request, errstr);
42
43         struct psi {
44                 unsigned long long off;
45                 unsigned int flags, nr;
46         };
47         TAIL_ALLOC_OBJECT_CONST_PTR(struct psi, psi);
48
49         psi->off = 0xdeadbeeffacefeedULL;
50         psi->flags = 1;
51         psi->nr = 42;
52
53         do_ptrace(PTRACE_PEEKSIGINFO, pid, (unsigned long) psi, bad_request);
54         printf("ptrace(PTRACE_PEEKSIGINFO, %u, {off=%llu"
55                ", flags=PTRACE_PEEKSIGINFO_SHARED, nr=%u}, %#lx) = %s\n",
56                (unsigned) pid, psi->off, psi->nr, bad_request, errstr);
57
58         pid = fork();
59         if ((pid_t) pid < 0)
60                 perror_msg_and_fail("fork");
61
62         if (!pid) {
63                 sigset_t mask;
64                 sigemptyset(&mask);
65                 sigaddset(&mask, SIGUSR1);
66                 sigaddset(&mask, SIGUSR2);
67                 sigaddset(&mask, SIGALRM);
68
69                 if (sigprocmask(SIG_BLOCK, &mask, NULL))
70                         perror_msg_and_fail("sigprocmask");
71
72                 raise(SIGUSR1);
73                 raise(SIGUSR2);
74                 raise(SIGALRM);
75
76                 if (do_ptrace(PTRACE_TRACEME, 0, 0, 0))
77                         perror_msg_and_fail("child: PTRACE_TRACEME");
78
79                 raise(SIGSTOP);
80                 _exit(0);
81         }
82
83         const unsigned int nsigs = 4;
84         const uid_t uid = geteuid();
85         siginfo_t *sigs = tail_alloc(sizeof(*sigs) * nsigs);
86
87         psi->off = 0;
88         psi->flags = 0;
89         psi->nr = nsigs;
90
91         for (;;) {
92                 int status, tracee, saved;
93
94                 errno = 0;
95                 tracee = wait(&status);
96                 if (tracee <= 0) {
97                         if (errno == EINTR)
98                                 continue;
99                         saved = errno;
100                         kill(pid, SIGKILL);
101                         errno = saved;
102                         perror_msg_and_fail("wait");
103                 }
104                 if (WIFEXITED(status)) {
105                         if (WEXITSTATUS(status) == 0)
106                                 break;
107                         error_msg_and_fail("unexpected exit status %u",
108                                            WEXITSTATUS(status));
109                 }
110                 if (WIFSIGNALED(status))
111                         error_msg_and_fail("unexpected signal %u",
112                                            WTERMSIG(status));
113                 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
114                         kill(pid, SIGKILL);
115                         error_msg_and_fail("unexpected wait status %x",
116                                            status);
117                 }
118
119                 long rc = do_ptrace(PTRACE_PEEKSIGINFO, pid,
120                                     (unsigned long) psi, (unsigned long) sigs);
121                 if (rc < 0) {
122                         printf("ptrace(PTRACE_PEEKSIGINFO, %u"
123                                ", {off=%llu, flags=0, nr=%u}, %p) = %s\n",
124                                (unsigned) pid, psi->off, psi->nr, sigs,
125                                errstr);
126                 } else {
127                         printf("ptrace(PTRACE_PEEKSIGINFO, %u"
128                                ", {off=%llu, flags=0, nr=%u}"
129                                ", [{si_signo=SIGUSR1, si_code=SI_TKILL"
130                                ", si_pid=%u, si_uid=%u}"
131                                ", {si_signo=SIGUSR2, si_code=SI_TKILL"
132                                ", si_pid=%u, si_uid=%u}"
133                                ", {si_signo=SIGALRM, si_code=SI_TKILL"
134                                ", si_pid=%u, si_uid=%u}"
135                                "]) = %s\n",
136                                (unsigned) pid, psi->off, psi->nr,
137                                (unsigned) pid, (unsigned) uid,
138                                (unsigned) pid, (unsigned) uid,
139                                (unsigned) pid, (unsigned) uid,
140                                errstr);
141                 }
142
143                 if (do_ptrace(PTRACE_CONT, pid, 0, 0)) {
144                         saved = errno;
145                         kill(pid, SIGKILL);
146                         errno = saved;
147                         perror_msg_and_fail("ptrace");
148                 }
149                 printf("ptrace(PTRACE_CONT, %ld, NULL, 0) = 0\n", pid);
150         }
151 }
152
153 int
154 main(void)
155 {
156         const unsigned long bad_request =
157                 (unsigned long) 0xdeadbeeffffffeedULL;
158         const unsigned long bad_data =
159                 (unsigned long) 0xdeadcafefffff00dULL;
160         const unsigned long pid =
161                 (unsigned long) 0xdefaced00000000ULL | (unsigned) getpid();
162
163         TAIL_ALLOC_OBJECT_CONST_PTR(uint64_t, filter_off);
164
165         const unsigned int sigset_size = get_sigset_size();
166
167         void *const k_set = tail_alloc(sigset_size);
168         TAIL_ALLOC_OBJECT_CONST_PTR(siginfo_t, sip);
169
170         do_ptrace(bad_request, pid, 0, 0);
171         printf("ptrace(%#lx /* PTRACE_??? */, %u, NULL, NULL) = %s\n",
172                bad_request, (unsigned) pid, errstr);
173
174         do_ptrace(PTRACE_PEEKDATA, pid, bad_request, bad_data);
175 #ifdef IA64
176         printf("ptrace(PTRACE_PEEKDATA, %u, %#lx) = %s\n",
177                (unsigned) pid, bad_request, errstr);
178 #else
179         printf("ptrace(PTRACE_PEEKDATA, %u, %#lx, %#lx) = %s\n",
180                (unsigned) pid, bad_request, bad_data, errstr);
181 #endif
182
183         do_ptrace(PTRACE_PEEKTEXT, pid, bad_request, bad_data);
184 #ifdef IA64
185         printf("ptrace(PTRACE_PEEKTEXT, %u, %#lx) = %s\n",
186                (unsigned) pid, bad_request, errstr);
187 #else
188         printf("ptrace(PTRACE_PEEKTEXT, %u, %#lx, %#lx) = %s\n",
189                (unsigned) pid, bad_request, bad_data, errstr);
190 #endif
191
192         do_ptrace(PTRACE_PEEKUSER, pid, bad_request, bad_data);
193 #ifdef IA64
194         printf("ptrace(PTRACE_PEEKUSER, %u, %#lx) = %s\n",
195                (unsigned) pid, bad_request, errstr);
196 #else
197         printf("ptrace(PTRACE_PEEKUSER, %u, %#lx, %#lx) = %s\n",
198                (unsigned) pid, bad_request, bad_data, errstr);
199 #endif
200
201         do_ptrace(PTRACE_POKEUSER, pid, bad_request, bad_data);
202         printf("ptrace(PTRACE_POKEUSER, %u, %#lx, %#lx) = %s\n",
203                (unsigned) pid, bad_request, bad_data, errstr);
204
205         do_ptrace(PTRACE_ATTACH, pid, 0, 0);
206         printf("ptrace(PTRACE_ATTACH, %u) = %s\n", (unsigned) pid, errstr);
207
208         do_ptrace(PTRACE_INTERRUPT, pid, 0, 0);
209         printf("ptrace(PTRACE_INTERRUPT, %u) = %s\n", (unsigned) pid, errstr);
210
211         do_ptrace(PTRACE_KILL, pid, 0, 0);
212         printf("ptrace(PTRACE_KILL, %u) = %s\n", (unsigned) pid, errstr);
213
214         do_ptrace(PTRACE_LISTEN, pid, 0, 0);
215         printf("ptrace(PTRACE_LISTEN, %u) = %s\n", (unsigned) pid, errstr);
216
217         sigset_t libc_set;
218         sigemptyset(&libc_set);
219         sigaddset(&libc_set, SIGUSR1);
220         memcpy(k_set, &libc_set, sigset_size);
221
222         do_ptrace(PTRACE_SETSIGMASK, pid, sigset_size, (unsigned long) k_set);
223         printf("ptrace(PTRACE_SETSIGMASK, %u, %u, [USR1]) = %s\n",
224                (unsigned) pid, sigset_size, errstr);
225
226         do_ptrace(PTRACE_GETSIGMASK, pid, sigset_size, (unsigned long) k_set);
227         printf("ptrace(PTRACE_GETSIGMASK, %u, %u, %p) = %s\n",
228                (unsigned) pid, sigset_size, k_set, errstr);
229
230         do_ptrace(PTRACE_SECCOMP_GET_FILTER, pid, 42, 0);
231         printf("ptrace(PTRACE_SECCOMP_GET_FILTER, %u, 42, NULL) = %s\n",
232                (unsigned) pid, errstr);
233
234         do_ptrace(PTRACE_SECCOMP_GET_METADATA, pid, bad_data, 0);
235         printf("ptrace(PTRACE_SECCOMP_GET_METADATA, %u, %lu, NULL) = %s\n",
236                (unsigned) pid, bad_data, errstr);
237
238         do_ptrace(PTRACE_SECCOMP_GET_METADATA, pid, 7,
239                   (unsigned long) filter_off);
240         printf("ptrace(PTRACE_SECCOMP_GET_METADATA, %u, 7, %p) = %s\n",
241                (unsigned) pid, filter_off, errstr);
242
243         *filter_off = 0xfacefeeddeadc0deULL;
244         do_ptrace(PTRACE_SECCOMP_GET_METADATA, pid, bad_data,
245                   (unsigned long) filter_off);
246         printf("ptrace(PTRACE_SECCOMP_GET_METADATA, %u, %lu, "
247                "{filter_off=%" PRIu64 "}) = %s\n",
248                (unsigned) pid, bad_data, *filter_off, errstr);
249
250         do_ptrace(PTRACE_GETEVENTMSG, pid, bad_request, bad_data);
251         printf("ptrace(PTRACE_GETEVENTMSG, %u, %#lx, %#lx) = %s\n",
252                (unsigned) pid, bad_request, bad_data, errstr);
253
254         memset(sip, -1, sizeof(*sip));
255         sip->si_signo = SIGIO;
256         sip->si_code = 1;
257         sip->si_errno = ENOENT;
258         sip->si_band = -2;
259
260         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
261         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGIO"
262                ", si_code=POLL_IN, si_errno=ENOENT, si_band=-2}) = %s\n",
263                (unsigned) pid, bad_request, errstr);
264
265         memset(sip, -1, sizeof(*sip));
266         sip->si_signo = SIGTRAP;
267         sip->si_code = 1;
268         sip->si_errno = ENOENT;
269         sip->si_pid = 2;
270         sip->si_uid = 3;
271         sip->si_ptr = (void *) bad_request;
272
273         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
274         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGTRAP"
275                ", si_code=TRAP_BRKPT, si_errno=ENOENT, si_pid=2, si_uid=3"
276                ", si_value={int=%d, ptr=%p}}) = %s\n",
277                (unsigned) pid, bad_request, sip->si_int, sip->si_ptr,
278                errstr);
279
280         memset(sip, -1, sizeof(*sip));
281         sip->si_signo = SIGILL;
282         sip->si_code = 1;
283         sip->si_errno = ENOENT;
284         sip->si_addr = (void *) (unsigned long) 0xfacefeeddeadbeefULL;
285
286         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
287         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGILL"
288                ", si_code=ILL_ILLOPC, si_errno=ENOENT, si_addr=%p}) = %s\n",
289                (unsigned) pid, bad_request, sip->si_addr, errstr);
290
291         memset(sip, -1, sizeof(*sip));
292         sip->si_signo = SIGFPE;
293         sip->si_code = 1;
294         sip->si_errno = ENOENT;
295         sip->si_addr = (void *) (unsigned long) 0xfacefeeddeadbeefULL;
296
297         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
298         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGFPE"
299                ", si_code=FPE_INTDIV, si_errno=ENOENT, si_addr=%p}) = %s\n",
300                (unsigned) pid, bad_request, sip->si_addr, errstr);
301
302         memset(sip, -1, sizeof(*sip));
303         sip->si_signo = SIGBUS;
304         sip->si_code = 1;
305         sip->si_errno = -2;
306         sip->si_addr = (void *) (unsigned long) 0xfacefeeddeadbeefULL;
307
308         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
309         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGBUS"
310                ", si_code=BUS_ADRALN, si_errno=%d, si_addr=%p}) = %s\n",
311                (unsigned) pid, bad_request, sip->si_errno, sip->si_addr,
312                errstr);
313
314         memset(sip, -1, sizeof(*sip));
315         sip->si_signo = SIGPROF;
316         sip->si_code = 0xbadc0ded;
317         sip->si_errno = -2;
318         sip->si_pid = 0;
319         sip->si_uid = 3;
320         sip->si_ptr = 0;
321
322         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
323         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGPROF"
324                ", si_code=%#x, si_errno=%d, si_pid=0, si_uid=3}) = %s\n",
325                (unsigned) pid, bad_request, sip->si_code, sip->si_errno,
326                errstr);
327
328 #ifdef HAVE_SIGINFO_T_SI_SYSCALL
329         memset(sip, -1, sizeof(*sip));
330         sip->si_signo = SIGSYS;
331         sip->si_code = 1;
332         sip->si_errno = ENOENT;
333         sip->si_call_addr = (void *) (unsigned long) 0xfacefeeddeadbeefULL;
334         sip->si_syscall = -1U;
335         sip->si_arch = AUDIT_ARCH_X86_64;
336
337         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
338         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGSYS"
339                ", si_code=SYS_SECCOMP, si_errno=ENOENT, si_call_addr=%p"
340                ", si_syscall=%u, si_arch=AUDIT_ARCH_X86_64})"
341                " = %s\n",
342                (unsigned) pid, bad_request, sip->si_call_addr, sip->si_syscall,
343                errstr);
344
345         sip->si_errno = 3141592653U;
346         sip->si_call_addr = NULL;
347         sip->si_syscall = __NR_read;
348         sip->si_arch = 0xda7a1057;
349
350         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
351         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGSYS"
352                ", si_code=SYS_SECCOMP, si_errno=%d, si_call_addr=NULL"
353                ", si_syscall=__NR_read, si_arch=%#x /* AUDIT_ARCH_??? */})"
354                " = %s\n",
355                (unsigned) pid, bad_request, sip->si_errno, sip->si_arch,
356                errstr);
357 #endif
358
359 #if defined HAVE_SIGINFO_T_SI_TIMERID && defined HAVE_SIGINFO_T_SI_OVERRUN
360         memset(sip, -1, sizeof(*sip));
361         sip->si_signo = SIGHUP;
362         sip->si_code = SI_TIMER;
363         sip->si_errno = ENOENT;
364         sip->si_timerid = 0xdeadbeef;
365         sip->si_overrun = -1;
366         sip->si_ptr = (void *) (unsigned long) 0xfacefeeddeadbeefULL;
367
368         do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (unsigned long) sip);
369         printf("ptrace(PTRACE_SETSIGINFO, %u, %#lx, {si_signo=SIGHUP"
370                ", si_code=SI_TIMER, si_errno=ENOENT, si_timerid=%#x"
371                ", si_overrun=%d, si_value={int=%d, ptr=%p}}) = %s\n",
372                (unsigned) pid, bad_request, sip->si_timerid, sip->si_overrun,
373                sip->si_int, sip->si_ptr, errstr);
374 #endif
375
376         do_ptrace(PTRACE_GETSIGINFO, pid, bad_request, (unsigned long) sip);
377         printf("ptrace(PTRACE_GETSIGINFO, %u, %#lx, %p)"
378                " = %s\n", (unsigned) pid, bad_request, sip, errstr);
379
380         do_ptrace(PTRACE_CONT, pid, 0, SIGUSR1);
381         printf("ptrace(PTRACE_CONT, %u, NULL, SIGUSR1) = %s\n",
382                (unsigned) pid, errstr);
383
384         do_ptrace(PTRACE_DETACH, pid, 0, SIGUSR2);
385         printf("ptrace(PTRACE_DETACH, %u, NULL, SIGUSR2) = %s\n",
386                (unsigned) pid, errstr);
387
388         do_ptrace(PTRACE_SYSCALL, pid, 0, SIGUSR1);
389         printf("ptrace(PTRACE_SYSCALL, %u, NULL, SIGUSR1) = %s\n",
390                (unsigned) pid, errstr);
391
392 #ifdef PTRACE_SINGLESTEP
393         do_ptrace(PTRACE_SINGLESTEP, pid, 0, SIGUSR2);
394         printf("ptrace(PTRACE_SINGLESTEP, %u, NULL, SIGUSR2) = %s\n",
395                (unsigned) pid, errstr);
396 #endif
397
398 #ifdef PTRACE_SINGLEBLOCK
399         do_ptrace(PTRACE_SINGLEBLOCK, pid, 0, SIGUSR1);
400         printf("ptrace(PTRACE_SINGLEBLOCK, %u, NULL, SIGUSR1) = %s\n",
401                (unsigned) pid, errstr);
402 #endif
403
404 #ifdef PTRACE_SYSEMU
405         do_ptrace(PTRACE_SYSEMU, pid, 0, SIGUSR2);
406         printf("ptrace(PTRACE_SYSEMU, %u, NULL, SIGUSR2) = %s\n",
407                (unsigned) pid, errstr);
408 #endif
409 #ifdef PTRACE_SYSEMU_SINGLESTEP
410         do_ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, SIGUSR1);
411         printf("ptrace(PTRACE_SYSEMU_SINGLESTEP, %u, NULL, SIGUSR1) = %s\n",
412                (unsigned) pid, errstr);
413 #endif
414
415         do_ptrace(PTRACE_SETOPTIONS,
416                   pid, 0, PTRACE_O_TRACEFORK|PTRACE_O_TRACECLONE);
417         printf("ptrace(PTRACE_SETOPTIONS, %u, NULL"
418                ", PTRACE_O_TRACEFORK|PTRACE_O_TRACECLONE) = %s\n",
419                (unsigned) pid, errstr);
420
421         do_ptrace(PTRACE_SEIZE, pid, bad_request, PTRACE_O_TRACESYSGOOD);
422         printf("ptrace(PTRACE_SEIZE, %u, %#lx, PTRACE_O_TRACESYSGOOD) = %s\n",
423                (unsigned) pid, bad_request, errstr);
424
425         do_ptrace(PTRACE_SETREGSET, pid, 1, bad_request);
426         printf("ptrace(PTRACE_SETREGSET, %u, NT_PRSTATUS, %#lx) = %s\n",
427                (unsigned) pid, bad_request, errstr);
428
429         do_ptrace(PTRACE_GETREGSET, pid, 3, bad_request);
430         printf("ptrace(PTRACE_GETREGSET, %u, NT_PRPSINFO, %#lx) = %s\n",
431                (unsigned) pid, bad_request, errstr);
432
433         test_peeksiginfo(pid, bad_request);
434
435         do_ptrace(PTRACE_TRACEME, 0, 0, 0);
436         printf("ptrace(PTRACE_TRACEME) = %s\n", errstr);
437
438         puts("+++ exited with 0 +++");
439         return 0;
440 }