]> granicus.if.org Git - strace/blob - syscall.c
tests: improve setugid error diagnostics a bit
[strace] / syscall.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *                     Linux for s390 port by D.J. Barrow
8  *                    <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9  * Copyright (c) 1999-2017 The strace developers.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "defs.h"
36 #include "native_defs.h"
37 #include "nsig.h"
38 #include "number_set.h"
39 #include <sys/param.h>
40
41 /* for struct iovec */
42 #include <sys/uio.h>
43
44 /* for __X32_SYSCALL_BIT */
45 #include <asm/unistd.h>
46
47 #include "regs.h"
48 #include "ptrace.h"
49
50 #if defined(SPARC64)
51 # undef PTRACE_GETREGS
52 # define PTRACE_GETREGS PTRACE_GETREGS64
53 # undef PTRACE_SETREGS
54 # define PTRACE_SETREGS PTRACE_SETREGS64
55 #endif
56
57 #ifndef NT_PRSTATUS
58 # define NT_PRSTATUS 1
59 #endif
60
61 #include "syscall.h"
62 #include "xstring.h"
63
64 /* Define these shorthand notations to simplify the syscallent files. */
65 #include "sysent_shorthand_defs.h"
66
67 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
68
69 const struct_sysent sysent0[] = {
70 #include "syscallent.h"
71 };
72
73 #if SUPPORTED_PERSONALITIES > 1
74 # include PERSONALITY1_INCLUDE_FUNCS
75 static const struct_sysent sysent1[] = {
76 # include "syscallent1.h"
77 };
78 #endif
79
80 #if SUPPORTED_PERSONALITIES > 2
81 # include PERSONALITY2_INCLUDE_FUNCS
82 static const struct_sysent sysent2[] = {
83 # include "syscallent2.h"
84 };
85 #endif
86
87 /* Now undef them since short defines cause wicked namespace pollution. */
88 #include "sysent_shorthand_undefs.h"
89
90 /*
91  * `ioctlent[012].h' files are automatically generated by the auxiliary
92  * program `ioctlsort', such that the list is sorted by the `code' field.
93  * This has the side-effect of resolving the _IO.. macros into
94  * plain integers, eliminating the need to include here everything
95  * in "/usr/include".
96  */
97
98 const char *const errnoent0[] = {
99 #include "errnoent.h"
100 };
101 const char *const signalent0[] = {
102 #include "signalent.h"
103 };
104 const struct_ioctlent ioctlent0[] = {
105 #include "ioctlent0.h"
106 };
107
108 #if SUPPORTED_PERSONALITIES > 1
109 static const char *const errnoent1[] = {
110 # include "errnoent1.h"
111 };
112 static const char *const signalent1[] = {
113 # include "signalent1.h"
114 };
115 static const struct_ioctlent ioctlent1[] = {
116 # include "ioctlent1.h"
117 };
118 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
119 static const struct_printers printers0 = {
120 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
121 };
122 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
123 static const struct_printers printers1 = {
124 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
125 };
126 #endif
127
128 #if SUPPORTED_PERSONALITIES > 2
129 static const char *const errnoent2[] = {
130 # include "errnoent2.h"
131 };
132 static const char *const signalent2[] = {
133 # include "signalent2.h"
134 };
135 static const struct_ioctlent ioctlent2[] = {
136 # include "ioctlent2.h"
137 };
138 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
139 static const struct_printers printers2 = {
140 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
141 };
142 #endif
143
144 enum {
145         nsyscalls0 = ARRAY_SIZE(sysent0)
146 #if SUPPORTED_PERSONALITIES > 1
147         , nsyscalls1 = ARRAY_SIZE(sysent1)
148 # if SUPPORTED_PERSONALITIES > 2
149         , nsyscalls2 = ARRAY_SIZE(sysent2)
150 # endif
151 #endif
152 };
153
154 enum {
155         nerrnos0 = ARRAY_SIZE(errnoent0)
156 #if SUPPORTED_PERSONALITIES > 1
157         , nerrnos1 = ARRAY_SIZE(errnoent1)
158 # if SUPPORTED_PERSONALITIES > 2
159         , nerrnos2 = ARRAY_SIZE(errnoent2)
160 # endif
161 #endif
162 };
163
164 enum {
165         nsignals0 = ARRAY_SIZE(signalent0)
166 #if SUPPORTED_PERSONALITIES > 1
167         , nsignals1 = ARRAY_SIZE(signalent1)
168 # if SUPPORTED_PERSONALITIES > 2
169         , nsignals2 = ARRAY_SIZE(signalent2)
170 # endif
171 #endif
172 };
173
174 enum {
175         nioctlents0 = ARRAY_SIZE(ioctlent0)
176 #if SUPPORTED_PERSONALITIES > 1
177         , nioctlents1 = ARRAY_SIZE(ioctlent1)
178 # if SUPPORTED_PERSONALITIES > 2
179         , nioctlents2 = ARRAY_SIZE(ioctlent2)
180 # endif
181 #endif
182 };
183
184 #if SUPPORTED_PERSONALITIES > 1
185 const struct_sysent *sysent = sysent0;
186 const char *const *errnoent = errnoent0;
187 const char *const *signalent = signalent0;
188 const struct_ioctlent *ioctlent = ioctlent0;
189 const struct_printers *printers = &printers0;
190 #endif
191
192 unsigned nsyscalls = nsyscalls0;
193 unsigned nerrnos = nerrnos0;
194 unsigned nsignals = nsignals0;
195 unsigned nioctlents = nioctlents0;
196
197 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
198         nsyscalls0,
199 #if SUPPORTED_PERSONALITIES > 1
200         nsyscalls1,
201 #endif
202 #if SUPPORTED_PERSONALITIES > 2
203         nsyscalls2,
204 #endif
205 };
206 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
207         sysent0,
208 #if SUPPORTED_PERSONALITIES > 1
209         sysent1,
210 #endif
211 #if SUPPORTED_PERSONALITIES > 2
212         sysent2,
213 #endif
214 };
215
216 const char *const personality_names[] =
217 # if defined X86_64
218         {"64 bit", "32 bit", "x32"}
219 # elif defined X32
220         {"x32", "32 bit"}
221 # elif SUPPORTED_PERSONALITIES == 2
222         {"64 bit", "32 bit"}
223 # else
224         {STRINGIFY_VAL(__WORDSIZE) " bit"}
225 # endif
226         ;
227
228 #if SUPPORTED_PERSONALITIES > 1
229
230 unsigned current_personality;
231
232 # ifndef current_wordsize
233 unsigned current_wordsize = PERSONALITY0_WORDSIZE;
234 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
235         PERSONALITY0_WORDSIZE,
236         PERSONALITY1_WORDSIZE,
237 # if SUPPORTED_PERSONALITIES > 2
238         PERSONALITY2_WORDSIZE,
239 # endif
240 };
241 # endif
242
243 # ifndef current_klongsize
244 unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
245 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
246         PERSONALITY0_KLONGSIZE,
247         PERSONALITY1_KLONGSIZE,
248 #  if SUPPORTED_PERSONALITIES > 2
249         PERSONALITY2_KLONGSIZE,
250 #  endif
251 };
252 # endif
253
254 void
255 set_personality(unsigned int personality)
256 {
257         if (personality == current_personality)
258                 return;
259
260         if (personality >= SUPPORTED_PERSONALITIES)
261                 error_msg_and_die("Requested switch to unsupported personality "
262                                   "%u", personality);
263
264         nsyscalls = nsyscall_vec[personality];
265         sysent = sysent_vec[personality];
266
267         switch (personality) {
268         case 0:
269                 errnoent = errnoent0;
270                 nerrnos = nerrnos0;
271                 ioctlent = ioctlent0;
272                 nioctlents = nioctlents0;
273                 signalent = signalent0;
274                 nsignals = nsignals0;
275                 printers = &printers0;
276                 break;
277
278         case 1:
279                 errnoent = errnoent1;
280                 nerrnos = nerrnos1;
281                 ioctlent = ioctlent1;
282                 nioctlents = nioctlents1;
283                 signalent = signalent1;
284                 nsignals = nsignals1;
285                 printers = &printers1;
286                 break;
287
288 # if SUPPORTED_PERSONALITIES > 2
289         case 2:
290                 errnoent = errnoent2;
291                 nerrnos = nerrnos2;
292                 ioctlent = ioctlent2;
293                 nioctlents = nioctlents2;
294                 signalent = signalent2;
295                 nsignals = nsignals2;
296                 printers = &printers2;
297                 break;
298 # endif
299         }
300
301         current_personality = personality;
302 # ifndef current_wordsize
303         current_wordsize = personality_wordsize[personality];
304 # endif
305 # ifndef current_klongsize
306         current_klongsize = personality_klongsize[personality];
307 # endif
308 }
309
310 static void
311 update_personality(struct tcb *tcp, unsigned int personality)
312 {
313         static bool need_mpers_warning[] =
314                 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
315
316         set_personality(personality);
317
318         if (personality == tcp->currpers)
319                 return;
320         tcp->currpers = personality;
321
322         if (!qflag) {
323                 error_msg("[ Process PID=%d runs in %s mode. ]",
324                           tcp->pid, personality_names[personality]);
325         }
326
327         if (need_mpers_warning[personality]) {
328                 error_msg("WARNING: Proper structure decoding for this "
329                           "personality is not supported, please consider "
330                           "building strace with mpers support enabled.");
331                 need_mpers_warning[personality] = false;
332         }
333 }
334 #endif
335
336 #ifdef SYS_socket_subcall
337 static void
338 decode_socket_subcall(struct tcb *tcp)
339 {
340         const int call = tcp->u_arg[0];
341
342         if (call < 1 || call >= SYS_socket_nsubcalls)
343                 return;
344
345         const kernel_ulong_t scno = SYS_socket_subcall + call;
346         const unsigned int nargs = sysent[scno].nargs;
347         uint64_t buf[nargs];
348
349         if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
350                 return;
351
352         tcp->scno = scno;
353         tcp->qual_flg = qual_flags(scno);
354         tcp->s_ent = &sysent[scno];
355
356         unsigned int i;
357         for (i = 0; i < nargs; ++i)
358                 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
359                                 ? ((uint32_t *) (void *) buf)[i] : buf[i];
360 }
361 #endif /* SYS_socket_subcall */
362
363 #ifdef SYS_ipc_subcall
364 static void
365 decode_ipc_subcall(struct tcb *tcp)
366 {
367         unsigned int call = tcp->u_arg[0];
368         const unsigned int version = call >> 16;
369
370         if (version) {
371 # if defined S390 || defined S390X
372                 return;
373 # else
374 #  ifdef SPARC64
375                 if (current_wordsize == 8)
376                         return;
377 #  endif
378                 set_tcb_priv_ulong(tcp, version);
379                 call &= 0xffff;
380 # endif
381         }
382
383         switch (call) {
384                 case  1: case  2: case  3: case  4:
385                 case 11: case 12: case 13: case 14:
386                 case 21: case 22: case 23: case 24:
387                         break;
388                 default:
389                         return;
390         }
391
392         tcp->scno = SYS_ipc_subcall + call;
393         tcp->qual_flg = qual_flags(tcp->scno);
394         tcp->s_ent = &sysent[tcp->scno];
395
396         const unsigned int n = tcp->s_ent->nargs;
397         unsigned int i;
398         for (i = 0; i < n; i++)
399                 tcp->u_arg[i] = tcp->u_arg[i + 1];
400 }
401 #endif /* SYS_ipc_subcall */
402
403 #ifdef SYS_syscall_subcall
404 static void
405 decode_syscall_subcall(struct tcb *tcp)
406 {
407         if (!scno_is_valid(tcp->u_arg[0]))
408                 return;
409         tcp->scno = tcp->u_arg[0];
410         tcp->qual_flg = qual_flags(tcp->scno);
411         tcp->s_ent = &sysent[tcp->scno];
412         memmove(&tcp->u_arg[0], &tcp->u_arg[1],
413                 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
414 # ifdef LINUX_MIPSO32
415         /*
416          * Fetching the last arg of 7-arg syscalls (fadvise64_64
417          * and sync_file_range) requires additional code,
418          * see linux/mips/get_syscall_args.c
419          */
420         if (tcp->s_ent->nargs == MAX_ARGS) {
421                 if (umoven(tcp,
422                            mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
423                            sizeof(tcp->u_arg[0]),
424                            &tcp->u_arg[MAX_ARGS - 1]) < 0)
425                 tcp->u_arg[MAX_ARGS - 1] = 0;
426         }
427 # endif /* LINUX_MIPSO32 */
428 }
429 #endif /* SYS_syscall_subcall */
430
431 static void
432 dumpio(struct tcb *tcp)
433 {
434         if (syserror(tcp))
435                 return;
436
437         int fd = tcp->u_arg[0];
438         if (fd < 0)
439                 return;
440
441         if (is_number_in_set(fd, read_set)) {
442                 switch (tcp->s_ent->sen) {
443                 case SEN_read:
444                 case SEN_pread:
445                 case SEN_recv:
446                 case SEN_recvfrom:
447                 case SEN_mq_timedreceive:
448                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
449                         return;
450                 case SEN_readv:
451                 case SEN_preadv:
452                 case SEN_preadv2:
453                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
454                                      tcp->u_rval);
455                         return;
456                 case SEN_recvmsg:
457                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
458                         return;
459                 case SEN_recvmmsg:
460                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
461                         return;
462                 }
463         }
464         if (is_number_in_set(fd, write_set)) {
465                 switch (tcp->s_ent->sen) {
466                 case SEN_write:
467                 case SEN_pwrite:
468                 case SEN_send:
469                 case SEN_sendto:
470                 case SEN_mq_timedsend:
471                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
472                         break;
473                 case SEN_writev:
474                 case SEN_pwritev:
475                 case SEN_pwritev2:
476                 case SEN_vmsplice:
477                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
478                         break;
479                 case SEN_sendmsg:
480                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
481                         break;
482                 case SEN_sendmmsg:
483                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
484                         break;
485                 }
486         }
487 }
488
489 /*
490  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
491  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
492  */
493 static kernel_ulong_t
494 shuffle_scno(kernel_ulong_t scno)
495 {
496 #ifdef ARM_FIRST_SHUFFLED_SYSCALL       /* So far only 32-bit ARM needs this */
497         if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
498                 return scno;
499
500         /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
501         if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
502                 return 0x000ffff0;
503         if (scno == 0x000ffff0)
504                 return ARM_FIRST_SHUFFLED_SYSCALL;
505
506 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
507         /*
508          * Is it ARM specific syscall?
509          * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
510          * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
511          */
512         if (scno >= 0x000f0000 &&
513             scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
514                 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
515         }
516         if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
517                 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
518         }
519 #endif /* ARM_FIRST_SHUFFLED_SYSCALL */
520
521         return scno;
522 }
523
524 const char *
525 err_name(unsigned long err)
526 {
527         if ((err < nerrnos) && errnoent[err])
528                 return errnoent[err];
529
530         return NULL;
531 }
532
533 static long get_regs(struct tcb *);
534 static int get_syscall_args(struct tcb *);
535 static int get_syscall_result(struct tcb *);
536 static int arch_get_scno(struct tcb *tcp);
537 static int arch_set_scno(struct tcb *, kernel_ulong_t);
538 static void get_error(struct tcb *, const bool);
539 static int arch_set_error(struct tcb *);
540 static int arch_set_success(struct tcb *);
541
542 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
543
544 static struct inject_opts *
545 tcb_inject_opts(struct tcb *tcp)
546 {
547         return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
548                ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
549 }
550
551
552 static long
553 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
554 {
555         if (!tcp->inject_vec[current_personality]) {
556                 tcp->inject_vec[current_personality] =
557                         xcalloc(nsyscalls, sizeof(**inject_vec));
558                 memcpy(tcp->inject_vec[current_personality],
559                        inject_vec[current_personality],
560                        nsyscalls * sizeof(**inject_vec));
561         }
562
563         struct inject_opts *opts = tcb_inject_opts(tcp);
564
565         if (!opts || opts->first == 0)
566                 return 0;
567
568         --opts->first;
569
570         if (opts->first != 0)
571                 return 0;
572
573         opts->first = opts->step;
574
575         if (opts->data.flags & INJECT_F_SIGNAL)
576                 *signo = opts->data.signo;
577         if (opts->data.flags & INJECT_F_RETVAL && !arch_set_scno(tcp, -1))
578                 tcp->flags |= TCB_TAMPERED;
579
580         return 0;
581 }
582
583 static long
584 tamper_with_syscall_exiting(struct tcb *tcp)
585 {
586         struct inject_opts *opts = tcb_inject_opts(tcp);
587
588         if (!opts)
589                 return 0;
590
591         if (opts->data.rval >= 0) {
592                 kernel_long_t u_rval = tcp->u_rval;
593
594                 tcp->u_rval = opts->data.rval;
595                 if (arch_set_success(tcp)) {
596                         tcp->u_rval = u_rval;
597                 } else {
598                         tcp->u_error = 0;
599                 }
600         } else {
601                 unsigned long new_error = -opts->data.rval;
602
603                 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
604                         unsigned long u_error = tcp->u_error;
605
606                         tcp->u_error = new_error;
607                         if (arch_set_error(tcp)) {
608                                 tcp->u_error = u_error;
609                         }
610                 }
611         }
612
613         return 0;
614 }
615
616 /*
617  * Returns:
618  * 0: "ignore this ptrace stop", bail out silently.
619  * 1: ok, decoded; call
620  *    syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
621  * other: error; call syscall_entering_finish(tcp, res), where res is the value
622  *    returned.
623  */
624 int
625 syscall_entering_decode(struct tcb *tcp)
626 {
627         int res = get_scno(tcp);
628         if (res == 0)
629                 return res;
630         int scno_good = res;
631         if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
632                 printleader(tcp);
633                 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
634                 /*
635                  * " <unavailable>" will be added later by the code which
636                  * detects ptrace errors.
637                  */
638                 return res;
639         }
640
641 #if defined SYS_ipc_subcall     \
642  || defined SYS_socket_subcall  \
643  || defined SYS_syscall_subcall
644         for (;;) {
645                 switch (tcp->s_ent->sen) {
646 # ifdef SYS_ipc_subcall
647                 case SEN_ipc:
648                         decode_ipc_subcall(tcp);
649                         break;
650 # endif
651 # ifdef SYS_socket_subcall
652                 case SEN_socketcall:
653                         decode_socket_subcall(tcp);
654                         break;
655 # endif
656 # ifdef SYS_syscall_subcall
657                 case SEN_syscall:
658                         decode_syscall_subcall(tcp);
659                         if (tcp->s_ent->sen != SEN_syscall)
660                                 continue;
661                         break;
662 # endif
663                 }
664                 break;
665         }
666 #endif
667
668         return 1;
669 }
670
671 int
672 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
673 {
674         /* Restrain from fault injection while the trace executes strace code. */
675         if (hide_log(tcp)) {
676                 tcp->qual_flg &= ~QUAL_INJECT;
677         }
678
679         switch (tcp->s_ent->sen) {
680                 case SEN_execve:
681                 case SEN_execveat:
682 #if defined SPARC || defined SPARC64
683                 case SEN_execv:
684 #endif
685                         tcp->flags &= ~TCB_HIDE_LOG;
686                         break;
687         }
688
689         if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
690                 tcp->flags |= TCB_FILTERED;
691                 return 0;
692         }
693
694         tcp->flags &= ~TCB_FILTERED;
695
696         if (hide_log(tcp)) {
697                 return 0;
698         }
699
700         if (inject(tcp))
701                 tamper_with_syscall_entering(tcp, sig);
702
703         if (cflag == CFLAG_ONLY_STATS) {
704                 return 0;
705         }
706
707 #ifdef USE_LIBUNWIND
708         if (stack_trace_enabled) {
709                 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
710                         unwind_capture_stacktrace(tcp);
711         }
712 #endif
713
714         printleader(tcp);
715         tprintf("%s(", tcp->s_ent->sys_name);
716         int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
717         fflush(tcp->outf);
718         return res;
719 }
720
721 void
722 syscall_entering_finish(struct tcb *tcp, int res)
723 {
724         tcp->flags |= TCB_INSYSCALL;
725         tcp->sys_func_rval = res;
726         /* Measure the entrance time as late as possible to avoid errors. */
727         if ((Tflag || cflag) && !filtered(tcp))
728                 gettimeofday(&tcp->etime, NULL);
729 }
730
731 /* Returns:
732  * 0: "bail out".
733  * 1: ok.
734  * -1: error in one of ptrace ops.
735  *
736  * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
737  *    value. Anyway, call syscall_exiting_finish(tcp) then.
738  */
739 int
740 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
741 {
742         /* Measure the exit time as early as possible to avoid errors. */
743         if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
744                 gettimeofday(ptv, NULL);
745
746 #ifdef USE_LIBUNWIND
747         if (stack_trace_enabled) {
748                 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
749                         unwind_cache_invalidate(tcp);
750         }
751 #endif
752
753         if (filtered(tcp) || hide_log(tcp))
754                 return 0;
755
756 #if SUPPORTED_PERSONALITIES > 1
757         update_personality(tcp, tcp->currpers);
758 #endif
759
760         return get_syscall_result(tcp);
761 }
762
763 int
764 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
765 {
766         if (syserror(tcp) && syscall_tampered(tcp))
767                 tamper_with_syscall_exiting(tcp);
768
769         if (cflag) {
770                 count_syscall(tcp, &tv);
771                 if (cflag == CFLAG_ONLY_STATS) {
772                         return 0;
773                 }
774         }
775
776         /* If not in -ff mode, and printing_tcp != tcp,
777          * then the log currently does not end with output
778          * of _our syscall entry_, but with something else.
779          * We need to say which syscall's return is this.
780          *
781          * Forced reprinting via TCB_REPRINT is used only by
782          * "strace -ff -oLOG test/threaded_execve" corner case.
783          * It's the only case when -ff mode needs reprinting.
784          */
785         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
786                 tcp->flags &= ~TCB_REPRINT;
787                 printleader(tcp);
788                 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
789         }
790         printing_tcp = tcp;
791
792         tcp->s_prev_ent = NULL;
793         if (res != 1) {
794                 /* There was error in one of prior ptrace ops */
795                 tprints(") ");
796                 tabto();
797                 tprints("= ? <unavailable>\n");
798                 line_ended();
799                 return res;
800         }
801         tcp->s_prev_ent = tcp->s_ent;
802
803         int sys_res = 0;
804         if (raw(tcp)) {
805                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
806         } else {
807         /* FIXME: not_failing_only (IOW, option -z) is broken:
808          * failure of syscall is known only after syscall return.
809          * Thus we end up with something like this on, say, ENOENT:
810          *     open("does_not_exist", O_RDONLY <unfinished ...>
811          *     {next syscall decode}
812          * whereas the intended result is that open(...) line
813          * is not shown at all.
814          */
815                 if (not_failing_only && tcp->u_error)
816                         return 0;       /* ignore failed syscalls */
817                 if (tcp->sys_func_rval & RVAL_DECODED)
818                         sys_res = tcp->sys_func_rval;
819                 else
820                         sys_res = tcp->s_ent->sys_func(tcp);
821         }
822
823         tprints(") ");
824         tabto();
825         unsigned long u_error = tcp->u_error;
826         kernel_long_t u_rval;
827
828         if (raw(tcp)) {
829                 if (u_error) {
830                         tprintf("= -1 (errno %lu)", u_error);
831                 } else {
832                         tprintf("= %#" PRI_klx, tcp->u_rval);
833                 }
834                 if (syscall_tampered(tcp))
835                         tprints(" (INJECTED)");
836         } else if (!(sys_res & RVAL_NONE) && u_error) {
837                 const char *u_error_str;
838
839                 switch (u_error) {
840                 /* Blocked signals do not interrupt any syscalls.
841                  * In this case syscalls don't return ERESTARTfoo codes.
842                  *
843                  * Deadly signals set to SIG_DFL interrupt syscalls
844                  * and kill the process regardless of which of the codes below
845                  * is returned by the interrupted syscall.
846                  * In some cases, kernel forces a kernel-generated deadly
847                  * signal to be unblocked and set to SIG_DFL (and thus cause
848                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
849                  * or SIGILL. (The alternative is to leave process spinning
850                  * forever on the faulty instruction - not useful).
851                  *
852                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
853                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
854                  * but kernel will always restart them.
855                  */
856                 case ERESTARTSYS:
857                         /* Most common type of signal-interrupted syscall exit code.
858                          * The system call will be restarted with the same arguments
859                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
860                          */
861                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
862                         break;
863                 case ERESTARTNOINTR:
864                         /* Rare. For example, fork() returns this if interrupted.
865                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
866                          */
867                         tprints("= ? ERESTARTNOINTR (To be restarted)");
868                         break;
869                 case ERESTARTNOHAND:
870                         /* pause(), rt_sigsuspend() etc use this code.
871                          * SA_RESTART is ignored (assumed not set):
872                          * syscall won't restart (will return EINTR instead)
873                          * even after signal with SA_RESTART set. However,
874                          * after SIG_IGN or SIG_DFL signal it will restart
875                          * (thus the name "restart only if has no handler").
876                          */
877                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
878                         break;
879                 case ERESTART_RESTARTBLOCK:
880                         /* Syscalls like nanosleep(), poll() which can't be
881                          * restarted with their original arguments use this
882                          * code. Kernel will execute restart_syscall() instead,
883                          * which changes arguments before restarting syscall.
884                          * SA_RESTART is ignored (assumed not set) similarly
885                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
886                          * since restart data is saved in "restart block"
887                          * in task struct, and if signal handler uses a syscall
888                          * which in turn saves another such restart block,
889                          * old data is lost and restart becomes impossible)
890                          */
891                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
892                         break;
893                 default:
894                         u_rval = sys_res & RVAL_PRINT_ERR_VAL ?
895                                  tcp->u_rval : -1;
896                         u_error_str = err_name(u_error);
897                         if (u_error_str)
898                                 tprintf("= %" PRI_kld " %s (%s)",
899                                         u_rval, u_error_str, strerror(u_error));
900                         else
901                                 tprintf("= %" PRI_kld " %lu (%s)",
902                                         u_rval, u_error, strerror(u_error));
903                         break;
904                 }
905                 if (syscall_tampered(tcp))
906                         tprints(" (INJECTED)");
907                 if ((sys_res & RVAL_STR) && tcp->auxstr)
908                         tprintf(" (%s)", tcp->auxstr);
909         } else {
910                 if (sys_res & RVAL_NONE)
911                         tprints("= ?");
912                 else {
913                         switch (sys_res & RVAL_MASK) {
914                         case RVAL_HEX:
915 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
916                                 if (current_wordsize < sizeof(tcp->u_rval)) {
917                                         tprintf("= %#x",
918                                                 (unsigned int) tcp->u_rval);
919                                 } else
920 #endif
921                                 {
922                                         tprintf("= %#" PRI_klx, tcp->u_rval);
923                                 }
924                                 break;
925                         case RVAL_OCTAL:
926                                 tprints("= ");
927                                 print_numeric_long_umask(tcp->u_rval);
928                                 break;
929                         case RVAL_UDECIMAL:
930 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
931                                 if (current_wordsize < sizeof(tcp->u_rval)) {
932                                         tprintf("= %u",
933                                                 (unsigned int) tcp->u_rval);
934                                 } else
935 #endif
936                                 {
937                                         tprintf("= %" PRI_klu, tcp->u_rval);
938                                 }
939                                 break;
940                         case RVAL_DECIMAL:
941                                 tprintf("= %" PRI_kld, tcp->u_rval);
942                                 break;
943                         case RVAL_FD:
944                                 if (show_fd_path) {
945                                         tprints("= ");
946                                         printfd(tcp, tcp->u_rval);
947                                 } else
948                                         tprintf("= %" PRI_kld, tcp->u_rval);
949                                 break;
950                         default:
951                                 error_msg("invalid rval format");
952                                 break;
953                         }
954                 }
955                 if ((sys_res & RVAL_STR) && tcp->auxstr)
956                         tprintf(" (%s)", tcp->auxstr);
957                 if (syscall_tampered(tcp))
958                         tprints(" (INJECTED)");
959         }
960         if (Tflag) {
961                 tv_sub(&tv, &tv, &tcp->etime);
962                 tprintf(" <%ld.%06ld>",
963                         (long) tv.tv_sec, (long) tv.tv_usec);
964         }
965         tprints("\n");
966         dumpio(tcp);
967         line_ended();
968
969 #ifdef USE_LIBUNWIND
970         if (stack_trace_enabled)
971                 unwind_print_stacktrace(tcp);
972 #endif
973         return 0;
974 }
975
976 void
977 syscall_exiting_finish(struct tcb *tcp)
978 {
979         tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
980         tcp->sys_func_rval = 0;
981         free_tcb_priv_data(tcp);
982 }
983
984 bool
985 is_erestart(struct tcb *tcp)
986 {
987         switch (tcp->u_error) {
988                 case ERESTARTSYS:
989                 case ERESTARTNOINTR:
990                 case ERESTARTNOHAND:
991                 case ERESTART_RESTARTBLOCK:
992                         return true;
993                 default:
994                         return false;
995         }
996 }
997
998 static unsigned long saved_u_error;
999
1000 void
1001 temporarily_clear_syserror(struct tcb *tcp)
1002 {
1003         saved_u_error = tcp->u_error;
1004         tcp->u_error = 0;
1005 }
1006
1007 void
1008 restore_cleared_syserror(struct tcb *tcp)
1009 {
1010         tcp->u_error = saved_u_error;
1011 }
1012
1013 #include "arch_regs.c"
1014
1015 #ifdef HAVE_GETRVAL2
1016 # include "arch_getrval2.c"
1017 #endif
1018
1019 void
1020 print_pc(struct tcb *tcp)
1021 {
1022 #if defined ARCH_PC_REG
1023 # define ARCH_GET_PC 0
1024 #elif defined ARCH_PC_PEEK_ADDR
1025         kernel_ulong_t pc;
1026 # define ARCH_PC_REG pc
1027 # define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
1028 #else
1029 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1030 #endif
1031         if (get_regs(tcp) < 0 || ARCH_GET_PC)
1032                 tprints(current_wordsize == 4 ? "[????????] "
1033                                               : "[????????????????] ");
1034         else
1035                 tprintf(current_wordsize == 4
1036                         ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1037                         (kernel_ulong_t) ARCH_PC_REG);
1038 }
1039
1040 #include "getregs_old.h"
1041
1042 #undef ptrace_getregset_or_getregs
1043 #undef ptrace_setregset_or_setregs
1044 #ifdef ARCH_REGS_FOR_GETREGSET
1045
1046 # define ptrace_getregset_or_getregs ptrace_getregset
1047 static long
1048 ptrace_getregset(pid_t pid)
1049 {
1050 # ifdef ARCH_IOVEC_FOR_GETREGSET
1051         /* variable iovec */
1052         ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1053         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1054                       &ARCH_IOVEC_FOR_GETREGSET);
1055 # else
1056         /* constant iovec */
1057         static struct iovec io = {
1058                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1059                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1060         };
1061         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1062
1063 # endif
1064 }
1065
1066 # ifndef HAVE_GETREGS_OLD
1067 #  define ptrace_setregset_or_setregs ptrace_setregset
1068 static int
1069 ptrace_setregset(pid_t pid)
1070 {
1071 #  ifdef ARCH_IOVEC_FOR_GETREGSET
1072         /* variable iovec */
1073         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1074                       &ARCH_IOVEC_FOR_GETREGSET);
1075 #  else
1076         /* constant iovec */
1077         static struct iovec io = {
1078                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1079                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1080         };
1081         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1082 #  endif
1083 }
1084 # endif /* !HAVE_GETREGS_OLD */
1085
1086 #elif defined ARCH_REGS_FOR_GETREGS
1087
1088 # define ptrace_getregset_or_getregs ptrace_getregs
1089 static long
1090 ptrace_getregs(pid_t pid)
1091 {
1092 # if defined SPARC || defined SPARC64
1093         /* SPARC systems have the meaning of data and addr reversed */
1094         return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1095 # else
1096         return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1097 # endif
1098 }
1099
1100 # ifndef HAVE_GETREGS_OLD
1101 #  define ptrace_setregset_or_setregs ptrace_setregs
1102 static int
1103 ptrace_setregs(pid_t pid)
1104 {
1105 #  if defined SPARC || defined SPARC64
1106         /* SPARC systems have the meaning of data and addr reversed */
1107         return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1108 #  else
1109         return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1110 #  endif
1111 }
1112 # endif /* !HAVE_GETREGS_OLD */
1113
1114 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1115
1116 #ifdef ptrace_getregset_or_getregs
1117 static long get_regs_error;
1118 #endif
1119
1120 void
1121 clear_regs(struct tcb *tcp)
1122 {
1123 #ifdef ptrace_getregset_or_getregs
1124         get_regs_error = -1;
1125 #endif
1126 }
1127
1128 static long
1129 get_regs(struct tcb *const tcp)
1130 {
1131 #ifdef ptrace_getregset_or_getregs
1132
1133         if (get_regs_error != -1)
1134                 return get_regs_error;
1135
1136 # ifdef HAVE_GETREGS_OLD
1137         /*
1138          * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1139          * fallback to getregs_old.
1140          */
1141         static int use_getregs_old;
1142         if (use_getregs_old < 0) {
1143                 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1144         } else if (use_getregs_old == 0) {
1145                 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1146                 if (get_regs_error >= 0) {
1147                         use_getregs_old = -1;
1148                         return get_regs_error;
1149                 }
1150                 if (errno == EPERM || errno == ESRCH)
1151                         return get_regs_error;
1152                 use_getregs_old = 1;
1153         }
1154         return get_regs_error = getregs_old(tcp);
1155 # else /* !HAVE_GETREGS_OLD */
1156         /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1157         get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1158
1159 #  if defined ARCH_PERSONALITY_0_IOV_SIZE
1160         if (get_regs_error)
1161                 return get_regs_error;
1162
1163         switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1164         case ARCH_PERSONALITY_0_IOV_SIZE:
1165                 update_personality(tcp, 0);
1166                 break;
1167         case ARCH_PERSONALITY_1_IOV_SIZE:
1168                 update_personality(tcp, 1);
1169                 break;
1170         default: {
1171                 static bool printed = false;
1172
1173                 if (!printed) {
1174                         error_msg("Unsupported regset size returned by "
1175                                   "PTRACE_GETREGSET: %zu",
1176                                   ARCH_IOVEC_FOR_GETREGSET.iov_len);
1177
1178                         printed = true;
1179                 }
1180
1181                 update_personality(tcp, 0);
1182         }
1183         }
1184 #  endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1185
1186         return get_regs_error;
1187
1188 # endif /* !HAVE_GETREGS_OLD */
1189
1190 #else /* !ptrace_getregset_or_getregs */
1191
1192 # warning get_regs is not implemented for this architecture yet
1193         return 0;
1194
1195 #endif /* !ptrace_getregset_or_getregs */
1196 }
1197
1198 #ifdef ptrace_setregset_or_setregs
1199 static int
1200 set_regs(pid_t pid)
1201 {
1202         return ptrace_setregset_or_setregs(pid);
1203 }
1204 #endif /* ptrace_setregset_or_setregs */
1205
1206 struct sysent_buf {
1207         struct tcb *tcp;
1208         struct_sysent ent;
1209         char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1210 };
1211
1212 static void
1213 free_sysent_buf(void *ptr)
1214 {
1215         struct sysent_buf *s = ptr;
1216         s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1217         free(ptr);
1218 }
1219
1220 /*
1221  * Returns:
1222  * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1223  *    out silently" code.
1224  * 1: ok, continue in syscall_entering_decode().
1225  * other: error, syscall_entering_decode() should print error indicator
1226  *    ("????" etc) and return an appropriate code.
1227  */
1228 int
1229 get_scno(struct tcb *tcp)
1230 {
1231         if (get_regs(tcp) < 0)
1232                 return -1;
1233
1234         int rc = arch_get_scno(tcp);
1235         if (rc != 1)
1236                 return rc;
1237
1238         if (scno_is_valid(tcp->scno)) {
1239                 tcp->s_ent = &sysent[tcp->scno];
1240                 tcp->qual_flg = qual_flags(tcp->scno);
1241         } else {
1242                 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1243
1244                 s->tcp = tcp;
1245                 s->ent.nargs = MAX_ARGS;
1246                 s->ent.sen = SEN_printargs;
1247                 s->ent.sys_func = printargs;
1248                 s->ent.sys_name = s->buf;
1249                 xsprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1250
1251                 tcp->s_ent = &s->ent;
1252                 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1253
1254                 set_tcb_priv_data(tcp, s, free_sysent_buf);
1255
1256                 debug_msg("pid %d invalid syscall %" PRI_kld,
1257                           tcp->pid, tcp->scno);
1258         }
1259         return 1;
1260 }
1261
1262 #ifdef ptrace_getregset_or_getregs
1263 # define get_syscall_result_regs get_regs
1264 #else
1265 static int get_syscall_result_regs(struct tcb *);
1266 #endif
1267
1268 /* Returns:
1269  * 1: ok, continue in syscall_exiting_trace().
1270  * -1: error, syscall_exiting_trace() should print error indicator
1271  *    ("????" etc) and bail out.
1272  */
1273 static int
1274 get_syscall_result(struct tcb *tcp)
1275 {
1276         if (get_syscall_result_regs(tcp) < 0)
1277                 return -1;
1278         tcp->u_error = 0;
1279         get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1280
1281         return 1;
1282 }
1283
1284 #include "get_scno.c"
1285 #include "set_scno.c"
1286 #include "get_syscall_args.c"
1287 #ifndef ptrace_getregset_or_getregs
1288 # include "get_syscall_result.c"
1289 #endif
1290 #include "get_error.c"
1291 #include "set_error.c"
1292 #ifdef HAVE_GETREGS_OLD
1293 # include "getregs_old.c"
1294 #endif
1295
1296 const char *
1297 syscall_name(kernel_ulong_t scno)
1298 {
1299 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1300         if (current_personality == X32_PERSONALITY_NUMBER)
1301                 scno &= ~__X32_SYSCALL_BIT;
1302 #endif
1303         return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;
1304 }