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-2023 The strace developers.
10 * All rights reserved.
11 *
12 * SPDX-License-Identifier: LGPL-2.1-or-later
13 */
14
15 #include "defs.h"
16 #include "get_personality.h"
17 #include "mmap_notify.h"
18 #include "native_defs.h"
19 #include "ptrace.h"
20 #include "ptrace_syscall_info.h"
21 #include "nsig.h"
22 #include "number_set.h"
23 #include "delay.h"
24 #include "poke.h"
25 #include "retval.h"
26 #include <limits.h>
27 #include <fcntl.h>
28
29 /* for struct iovec */
30 #include <sys/uio.h>
31
32 /* for PR_SET_NAME */
33 #include <linux/prctl.h>
34
35 /* for __X32_SYSCALL_BIT */
36 #include "scno.h"
37
38 #include "regs.h"
39
40 #if defined(SPARC64)
41 # undef PTRACE_GETREGS
42 # define PTRACE_GETREGS PTRACE_GETREGS64
43 # undef PTRACE_SETREGS
44 # define PTRACE_SETREGS PTRACE_SETREGS64
45 #endif
46
47 #include "sen.h"
48 #include "xstring.h"
49 #include "syscallent_base_nr.h"
50
51 /* Define these shorthand notations to simplify the syscallent files. */
52 #include "sysent_shorthand_defs.h"
53
54 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
55
56 const struct_sysent sysent0[] = {
57 #include "syscallent.h"
58 };
59
60 #if SUPPORTED_PERSONALITIES > 1
61 # include PERSONALITY1_INCLUDE_FUNCS
62 static const struct_sysent sysent1[] = {
63 # include "syscallent1.h"
64 };
65 #endif
66
67 #if SUPPORTED_PERSONALITIES > 2
68 # include PERSONALITY2_INCLUDE_FUNCS
69 static const struct_sysent sysent2[] = {
70 # include "syscallent2.h"
71 };
72 #endif
73
74 /* Now undef them since short defines cause wicked namespace pollution. */
75 #include "sysent_shorthand_undefs.h"
76
77 const char *const errnoent[] = {
78 #include "errnoent.h"
79 };
80 const char *const signalent[] = {
81 #include "signalent.h"
82
83 };
84 /*
85 * `ioctlent[012].h' files are automatically generated by the auxiliary
86 * program `ioctlsort', such that the list is sorted by the `code' field.
87 * This has the side-effect of resolving the _IO.. macros into
88 * plain integers, eliminating the need to include here everything
89 * in "/usr/include".
90 */
91
92 const struct_ioctlent ioctlent0[] = {
93 #include "ioctlent0.h"
94 };
95
96 #if SUPPORTED_PERSONALITIES > 1
97 static const struct_ioctlent ioctlent1[] = {
98 # include "ioctlent1.h"
99 };
100 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
101 static const struct_printers printers0 = {
102 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
103 };
104 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
105 static const struct_printers printers1 = {
106 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
107 };
108 #endif
109
110 #if SUPPORTED_PERSONALITIES > 2
111 static const struct_ioctlent ioctlent2[] = {
112 # include "ioctlent2.h"
113 };
114 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
115 static const struct_printers printers2 = {
116 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
117 };
118 #endif
119
120 enum {
121 nsyscalls0 = ARRAY_SIZE(sysent0)
122 #if SUPPORTED_PERSONALITIES > 1
123 , nsyscalls1 = ARRAY_SIZE(sysent1)
124 # if SUPPORTED_PERSONALITIES > 2
125 , nsyscalls2 = ARRAY_SIZE(sysent2)
126 # endif
127 #endif
128 };
129
130 enum {
131 nioctlents0 = ARRAY_SIZE(ioctlent0)
132 #if SUPPORTED_PERSONALITIES > 1
133 , nioctlents1 = ARRAY_SIZE(ioctlent1)
134 # if SUPPORTED_PERSONALITIES > 2
135 , nioctlents2 = ARRAY_SIZE(ioctlent2)
136 # endif
137 #endif
138 };
139
140 #if SUPPORTED_PERSONALITIES > 1
141 const struct_sysent *sysent = sysent0;
142 const struct_ioctlent *ioctlent = ioctlent0;
143 const struct_printers *printers = &printers0;
144 #endif
145
146 const unsigned int nerrnos = ARRAY_SIZE(errnoent);
147 const unsigned int nsignals = ARRAY_SIZE(signalent);
148 unsigned nsyscalls = nsyscalls0;
149 unsigned nioctlents = nioctlents0;
150
151 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
152 nsyscalls0,
153 #if SUPPORTED_PERSONALITIES > 1
154 nsyscalls1,
155 #endif
156 #if SUPPORTED_PERSONALITIES > 2
157 nsyscalls2,
158 #endif
159 };
160 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
161 sysent0,
162 #if SUPPORTED_PERSONALITIES > 1
163 sysent1,
164 #endif
165 #if SUPPORTED_PERSONALITIES > 2
166 sysent2,
167 #endif
168 };
169
170 const char *const personality_names[] = PERSONALITY_NAMES;
171 static_assert(ARRAY_SIZE(personality_names) == SUPPORTED_PERSONALITIES,
172 "ARRAY_SIZE(personality_names) != SUPPORTED_PERSONALITIES");
173
174 #if SUPPORTED_PERSONALITIES > 1
175
176 unsigned current_personality;
177
178 # ifndef current_wordsize
179 unsigned current_wordsize = PERSONALITY0_WORDSIZE;
180 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
181 PERSONALITY0_WORDSIZE,
182 PERSONALITY1_WORDSIZE,
183 # if SUPPORTED_PERSONALITIES > 2
184 PERSONALITY2_WORDSIZE,
185 # endif
186 };
187 # endif
188
189 # ifndef current_klongsize
190 unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
191 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
192 PERSONALITY0_KLONGSIZE,
193 PERSONALITY1_KLONGSIZE,
194 # if SUPPORTED_PERSONALITIES > 2
195 PERSONALITY2_KLONGSIZE,
196 # endif
197 };
198 # endif
199
200 void
201 set_personality(unsigned int personality)
202 {
203 if (personality == current_personality)
204 return;
205
206 if (personality >= SUPPORTED_PERSONALITIES)
207 error_msg_and_die("Requested switch to unsupported personality "
208 "%u", personality);
209
210 nsyscalls = nsyscall_vec[personality];
211 sysent = sysent_vec[personality];
212
213 switch (personality) {
214 case 0:
215 ioctlent = ioctlent0;
216 nioctlents = nioctlents0;
217 printers = &printers0;
218 break;
219
220 case 1:
221 ioctlent = ioctlent1;
222 nioctlents = nioctlents1;
223 printers = &printers1;
224 break;
225
226 # if SUPPORTED_PERSONALITIES > 2
227 case 2:
228 ioctlent = ioctlent2;
229 nioctlents = nioctlents2;
230 printers = &printers2;
231 break;
232 # endif
233 }
234
235 current_personality = personality;
236 # ifndef current_wordsize
237 current_wordsize = personality_wordsize[personality];
238 # endif
239 # ifndef current_klongsize
240 current_klongsize = personality_klongsize[personality];
241 # endif
242 }
243
244 static void
245 update_personality(struct tcb *tcp, unsigned int personality)
246 {
247 static bool need_mpers_warning[] =
248 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
249
250 set_personality(personality);
251
252 if (personality == tcp->currpers)
253 return;
254 tcp->currpers = personality;
255
256 if (!is_number_in_set(QUIET_PERSONALITY, quiet_set)) {
257 printleader(tcp);
258 tprintf_string("[ Process PID=%d runs in %s mode. ]\n",
259 tcp->pid, personality_names[personality]);
260 line_ended();
261 }
262
263 if (need_mpers_warning[personality]) {
264 error_msg("WARNING: Proper structure decoding for this "
265 "personality is not supported, please consider "
266 "building strace with mpers support enabled.");
267 need_mpers_warning[personality] = false;
268 }
269
270 # if defined(ENABLE_STACKTRACE) && !defined(USE_LIBUNWIND)
271 if (stack_trace_enabled) {
272 unwind_tcb_fin(tcp);
273 unwind_tcb_init(tcp);
274 }
275 # endif
276 }
277 #endif /* SUPPORTED_PERSONALITIES > 1 */
278
279 #ifdef SYS_socket_subcall
280 static void
281 decode_socket_subcall(struct tcb *tcp)
282 {
283 const int call = tcp->u_arg[0];
284
285 if (call < 1 || call >= SYS_socket_nsubcalls)
286 return;
287
288 const kernel_ulong_t scno = SYS_socket_subcall + call;
289 const unsigned int nargs = sysent[scno].nargs;
290 uint64_t buf[nargs];
291
292 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
293 return;
294
295 tcp->scno = scno;
296 tcp->qual_flg = qual_flags(scno);
297 tcp->s_ent = &sysent[scno];
298
299 for (unsigned int i = 0; i < nargs; ++i)
300 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
301 ? ((uint32_t *) (void *) buf)[i] : buf[i];
302 }
303 #endif /* SYS_socket_subcall */
304
305 #ifdef SYS_ipc_subcall
306 static void
307 decode_ipc_subcall(struct tcb *tcp)
308 {
309 unsigned int call = tcp->u_arg[0];
310 const unsigned int version = call >> 16;
311
312 if (version) {
313 # if defined S390 || defined S390X
314 return;
315 # else
316 # ifdef SPARC64
317 if (current_wordsize == 8)
318 return;
319 # endif
320 set_tcb_priv_ulong(tcp, version);
321 call &= 0xffff;
322 # endif
323 }
324
325 switch (call) {
326 case 1: case 2: case 3: case 4:
327 case 11: case 12: case 13: case 14:
328 case 21: case 22: case 23: case 24:
329 break;
330 default:
331 return;
332 }
333
334 tcp->scno = SYS_ipc_subcall + call;
335 tcp->qual_flg = qual_flags(tcp->scno);
336 tcp->s_ent = &sysent[tcp->scno];
337
338 const unsigned int n = n_args(tcp);
339 for (unsigned int i = 0; i < n; ++i)
340 tcp->u_arg[i] = tcp->u_arg[i + 1];
341 }
342 #endif /* SYS_ipc_subcall */
343
344 #ifdef SYS_syscall_subcall
345 /* The implementation is architecture specific. */
346 static void decode_syscall_subcall(struct tcb *);
347 #endif /* SYS_syscall_subcall */
348
349 static void
350 dumpio(struct tcb *tcp)
351 {
352 int fd = tcp->u_arg[0];
353 if (fd < 0)
354 return;
355
356 if (is_number_in_set(fd, write_set)) {
357 switch (tcp_sysent(tcp)->sen) {
358 case SEN_write:
359 case SEN_pwrite:
360 case SEN_send:
361 case SEN_sendto:
362 case SEN_mq_timedsend_time32:
363 case SEN_mq_timedsend_time64:
364 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
365 break;
366 case SEN_writev:
367 case SEN_pwritev:
368 case SEN_pwritev2:
369 case SEN_vmsplice:
370 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
371 break;
372 case SEN_sendmsg:
373 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
374 break;
375 case SEN_sendmmsg:
376 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
377 break;
378 }
379 }
380
381 if (syserror(tcp))
382 return;
383
384 if (is_number_in_set(fd, read_set)) {
385 switch (tcp_sysent(tcp)->sen) {
386 case SEN_read:
387 case SEN_pread:
388 case SEN_recv:
389 case SEN_recvfrom:
390 case SEN_mq_timedreceive_time32:
391 case SEN_mq_timedreceive_time64:
392 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
393 return;
394 case SEN_readv:
395 case SEN_preadv:
396 case SEN_preadv2:
397 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
398 tcp->u_rval);
399 return;
400 case SEN_recvmsg:
401 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
402 return;
403 case SEN_recvmmsg:
404 case SEN_recvmmsg_time32:
405 case SEN_recvmmsg_time64:
406 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
407 return;
408 }
409 }
410 }
411
412 static const char *
413 err_name(uint64_t err)
414 {
415 return err < nerrnos ? errnoent[err] : NULL;
416 }
417
418 void
419 print_err(int64_t err, bool negated)
420 {
421 const char *str = err_name(negated ? -err : err);
422
423 if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
424 tprintf_string(negated ? "%" PRId64 : "%" PRIu64, err);
425 if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
426 return;
427 (xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV
428 ? tprintf_string : tprintf_comment)("%s%s",
429 negated ? "-" : "", str);
430 }
431
432 static void
433 print_err_ret(kernel_ulong_t ret, unsigned long u_error)
434 {
435 const char *u_error_str = err_name(u_error);
436
437 tprints_sysret_next("retval");
438 PRINT_VAL_D(ret);
439
440 if (u_error_str) {
441 tprints_sysret_next("error");
442 tprints_string(u_error_str);
443 tprints_sysret_string("strerror", strerror(u_error));
444 } else {
445 tprints_sysret_next("strerror");
446 tprintf_string("(errno %lu)", u_error);
447 }
448 }
449
450 static long get_regs(struct tcb *);
451 static int get_syscall_args(struct tcb *);
452 static int get_syscall_result(struct tcb *);
453 static void get_error(struct tcb *, bool);
454 static void set_error(struct tcb *, unsigned long);
455 static void set_success(struct tcb *, kernel_long_t);
456 static int arch_get_scno(struct tcb *);
457 static int arch_check_scno(struct tcb *);
458 static int arch_set_scno(struct tcb *, kernel_ulong_t);
459 static int arch_get_syscall_args(struct tcb *);
460 static void arch_get_error(struct tcb *, bool);
461 static int arch_set_error(struct tcb *);
462 static int arch_set_success(struct tcb *);
463 #if MAX_ARGS > 6
464 static void arch_get_syscall_args_extra(struct tcb *, unsigned int);
465 #endif
466
467 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
468
469 static struct inject_opts *
470 tcb_inject_opts(struct tcb *tcp)
471 {
472 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
473 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
474 }
475
476
477 static long
478 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
479 {
480 if (!tcp->inject_vec[current_personality]) {
481 tcp->inject_vec[current_personality] =
482 xarraydup(inject_vec[current_personality],
483 nsyscalls, sizeof(**inject_vec));
484 }
485
486 struct inject_opts *opts = tcb_inject_opts(tcp);
487
488 if (!opts || opts->first == 0 || opts->last == 0)
489 return 0;
490
491 if (opts->last != INJECT_LAST_INF)
492 --opts->last;
493
494 --opts->first;
495
496 if (opts->first != 0)
497 return 0;
498
499 opts->first = opts->step;
500
501 if (!recovering(tcp)) {
502 if (opts->data.flags & INJECT_F_SIGNAL)
503 *signo = opts->data.signo;
504 if (opts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL)) {
505 kernel_long_t scno =
506 (opts->data.flags & INJECT_F_SYSCALL)
507 ? (kernel_long_t) shuffle_scno(opts->data.scno)
508 : -1;
509
510 if (!arch_set_scno(tcp, scno)) {
511 tcp->flags |= TCB_TAMPERED;
512 if (scno != -1)
513 tcp->flags |= TCB_TAMPERED_NO_FAIL;
514 #if ARCH_NEEDS_SET_ERROR_FOR_SCNO_TAMPERING
515 /*
516 * So far it's just a workaround for hppa,
517 * but let's pretend it could be used elsewhere.
518 */
519 else {
520 kernel_long_t rval =
521 (opts->data.flags & INJECT_F_RETVAL) ?
522 ENOSYS : retval_get(opts->data.rval_idx);
523
524 tcp->u_error = 0; /* force reset */
525 set_error(tcp, rval);
526 }
527 #endif
528 }
529 }
530 if (opts->data.flags & INJECT_F_POKE_ENTER)
531 poke_tcb(tcp, opts->data.poke_idx, true);
532 if (opts->data.flags & INJECT_F_POKE_EXIT)
533 tcp->flags |= TCB_INJECT_POKE_EXIT;
534 if (opts->data.flags & INJECT_F_DELAY_ENTER)
535 delay_tcb(tcp, opts->data.delay_idx, true);
536 if (opts->data.flags & INJECT_F_DELAY_EXIT)
537 tcp->flags |= TCB_INJECT_DELAY_EXIT;
538 }
539
540 return 0;
541 }
542
543 static long
544 tamper_with_syscall_exiting(struct tcb *tcp)
545 {
546 struct inject_opts *opts = tcb_inject_opts(tcp);
547 if (!opts)
548 return 0;
549
550 if (inject_poke_exit(tcp))
551 poke_tcb(tcp, opts->data.poke_idx, false);
552
553 if (inject_delay_exit(tcp))
554 delay_tcb(tcp, opts->data.delay_idx, false);
555
556 if (!syscall_tampered(tcp))
557 return 0;
558
559 if (!syserror(tcp) ^ !!syscall_tampered_nofail(tcp)) {
560 error_msg("Failed to tamper with process %d: unexpectedly got"
561 " %serror (return value %#" PRI_klx ", error %lu)",
562 tcp->pid, syscall_tampered_nofail(tcp) ? "" : "no ",
563 tcp->u_rval, tcp->u_error);
564
565 return 1;
566 }
567
568 if (opts->data.flags & INJECT_F_RETVAL)
569 set_success(tcp, retval_get(opts->data.rval_idx));
570 else
571 set_error(tcp, retval_get(opts->data.rval_idx));
572
573 return 0;
574 }
575
576 /*
577 * Returns:
578 * 0: "ignore this ptrace stop", bail out silently.
579 * 1: ok, decoded; call
580 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
581 * other: error; call syscall_entering_finish(tcp, res), where res is the value
582 * returned.
583 */
584 int
585 syscall_entering_decode(struct tcb *tcp)
586 {
587 int res = get_scno(tcp);
588 if (res == 0)
589 return res;
590 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
591 printleader(tcp);
592 tprints_arg_begin(tcp_sysent(tcp)->sys_name);
593 /*
594 * " <unavailable>" will be added later by the code which
595 * detects ptrace errors.
596 */
597 return res;
598 }
599
600 #ifdef SYS_syscall_subcall
601 if (tcp_sysent(tcp)->sen == SEN_syscall)
602 decode_syscall_subcall(tcp);
603 #endif
604 #if defined SYS_ipc_subcall \
605 || defined SYS_socket_subcall
606 switch (tcp_sysent(tcp)->sen) {
607 # ifdef SYS_ipc_subcall
608 case SEN_ipc:
609 decode_ipc_subcall(tcp);
610 break;
611 # endif
612 # ifdef SYS_socket_subcall
613 case SEN_socketcall:
614 decode_socket_subcall(tcp);
615 break;
616 # endif
617 }
618 #endif
619
620 return 1;
621 }
622
623 int
624 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
625 {
626 if (hide_log(tcp)) {
627 /*
628 * Restrain from fault injection
629 * while the tracee executes strace code.
630 */
631 tcp->qual_flg &= ~QUAL_INJECT;
632
633 switch (tcp_sysent(tcp)->sen) {
634 case SEN_execve:
635 case SEN_execveat:
636 case SEN_execv:
637 /*
638 * First exec* syscall makes the log visible.
639 */
640 tcp->flags &= ~TCB_HIDE_LOG;
641 /*
642 * Check whether this exec* syscall succeeds.
643 */
644 tcp->flags |= TCB_CHECK_EXEC_SYSCALL;
645 break;
646 }
647 }
648
649 if (hide_log(tcp) || !traced(tcp)
650 || ((tracing_paths || tracing_fds) && !pathtrace_match(tcp))) {
651 tcp->flags |= TCB_FILTERED;
652 return 0;
653 }
654
655 tcp->flags &= ~TCB_FILTERED;
656
657 if (inject(tcp))
658 tamper_with_syscall_entering(tcp, sig);
659
660 if (cflag == CFLAG_ONLY_STATS) {
661 return 0;
662 }
663
664 #ifdef ENABLE_STACKTRACE
665 if (stack_trace_enabled &&
666 !check_exec_syscall(tcp) &&
667 tcp_sysent(tcp)->sys_flags & STACKTRACE_CAPTURE_ON_ENTER) {
668 unwind_tcb_capture(tcp);
669 }
670 #endif
671
672 if (!is_complete_set(status_set, NUMBER_OF_STATUSES))
673 strace_open_memstream(tcp);
674
675 printleader(tcp);
676 tprints_arg_begin(tcp_sysent(tcp)->sys_name);
677 int res = raw(tcp) ? printargs(tcp) : tcp_sysent(tcp)->sys_func(tcp);
678 fflush(tcp->outf);
679 return res;
680 }
681
682 void
683 syscall_entering_finish(struct tcb *tcp, int res)
684 {
685 tcp->flags |= TCB_INSYSCALL;
686 tcp->sys_func_rval = res;
687
688 /* Measure the entrance time as late as possible to avoid errors. */
689 if ((Tflag || cflag) && !filtered(tcp))
690 clock_gettime(CLOCK_MONOTONIC, &tcp->etime);
691
692 /* Start tracking system time */
693 if (cflag) {
694 if (debug_flag) {
695 struct timespec dt;
696
697 ts_sub(&dt, &tcp->stime, &tcp->ltime);
698
699 if (ts_nz(&dt))
700 debug_func_msg("pid %d: %.9f seconds of system "
701 "time spent since the last "
702 "syscall exit",
703 tcp->pid, ts_float(&dt));
704 }
705
706 tcp->ltime = tcp->stime;
707 }
708 }
709
710 /* Returns:
711 * 0: "bail out".
712 * 1: ok.
713 * -1: error in one of ptrace ops.
714 *
715 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
716 * value. Anyway, call syscall_exiting_finish(tcp) then.
717 */
718 int
719 syscall_exiting_decode(struct tcb *tcp, struct timespec *pts)
720 {
721 /* Measure the exit time as early as possible to avoid errors. */
722 if ((Tflag || cflag) && !filtered(tcp))
723 clock_gettime(CLOCK_MONOTONIC, pts);
724
725 if (tcp_sysent(tcp)->sys_flags & MEMORY_MAPPING_CHANGE)
726 mmap_notify_report(tcp);
727
728 if ((tcp_sysent(tcp)->sys_flags & COMM_CHANGE) && !syserror(tcp) &&
729 (tcp_sysent(tcp)->sen != SEN_prctl || tcp->u_arg[0] == PR_SET_NAME))
730 maybe_load_task_comm(tcp);
731
732 if (filtered(tcp))
733 return 0;
734
735 if (check_exec_syscall(tcp)) {
736 /* The check failed, hide the log. */
737 tcp->flags |= TCB_HIDE_LOG;
738 }
739
740 #if SUPPORTED_PERSONALITIES > 1
741 update_personality(tcp, tcp->currpers);
742 #endif
743
744 return get_syscall_result(tcp);
745 }
746
747 void
748 print_syscall_resume(struct tcb *tcp)
749 {
750 /* If not in -ff mode, and printing_tcp != tcp,
751 * then the log currently does not end with output
752 * of _our syscall entry_, but with something else.
753 * We need to say which syscall's return is this.
754 *
755 * Forced reprinting via TCB_REPRINT is used only by
756 * "strace -ff -oLOG test/threaded_execve" corner case.
757 * It's the only case when -ff mode needs reprinting.
758 */
759 if ((!output_separately && printing_tcp != tcp && !tcp->staged_output_data)
760 || (tcp->flags & TCB_REPRINT)) {
761 tcp->flags &= ~TCB_REPRINT;
762 printleader(tcp);
763 tprintf_string("<... %s resumed>", tcp_sysent(tcp)->sys_name);
764 }
765 printing_tcp = tcp;
766 }
767
768 static void
769 print_injected_note(struct tcb *tcp)
770 {
771 if (syscall_tampered(tcp) && syscall_tampered_poked(tcp))
772 tprints_sysret_string("inject", "INJECTED: args, retval");
773 else if (syscall_tampered_poked(tcp))
774 tprints_sysret_string("inject", "INJECTED: args");
775 else if (syscall_tampered(tcp))
776 tprints_sysret_string("inject", "INJECTED");
777 if (syscall_tampered_delayed(tcp))
778 tprints_sysret_string("delay", "DELAYED");
779 }
780
781 static void
782 print_erestart(const char *err_short, const char *err_long)
783 {
784 tprints_sysret_next("retval");
785 tprint_sysret_pseudo_rval();
786 tprints_sysret_next("error");
787 tprints_string(err_short);
788 tprints_sysret_string("strerror", err_long);
789 }
790
791 int
792 syscall_exiting_trace(struct tcb *tcp, struct timespec *ts, int res)
793 {
794 if (syscall_tampered(tcp) ||
795 inject_delay_exit(tcp) ||
796 inject_poke_exit(tcp))
797 tamper_with_syscall_exiting(tcp);
798
799 if (cflag != CFLAG_ONLY_STATS)
800 print_syscall_resume(tcp);
801
802 const struct_sysent *prev_ent =
803 tcp_sysent(tcp)->sen == SEN_restart_syscall
804 ? tcp->s_prev_ent : tcp->s_ent;
805 tcp->s_prev_ent = NULL;
806 if (res != 1) {
807 /* There was an error in one of prior ptrace ops. */
808 bool status_filtering =
809 !is_complete_set(status_set, NUMBER_OF_STATUSES);
810 bool publish = status_filtering
811 ? is_number_in_set(STATUS_UNAVAILABLE, status_set)
812 : true;
813 if (cflag && publish)
814 count_syscall(tcp, ts);
815 if (cflag != CFLAG_ONLY_STATS) {
816 tprint_arg_end();
817 tprint_space();
818 tabto();
819 tprint_sysret_begin();
820 tprints_sysret_next("retval");
821 tprint_sysret_pseudo_rval();
822 tprints_sysret_next("return");
823 tprints_string("<unavailable>");
824 tprint_sysret_end();
825 tprint_newline();
826 if (status_filtering)
827 strace_close_memstream(tcp, publish);
828 line_ended();
829 }
830 return res;
831 }
832 tcp->s_prev_ent = prev_ent;
833
834 int sys_res = 0;
835 if (cflag != CFLAG_ONLY_STATS) {
836 if (raw(tcp)) {
837 /* sys_res = printargs(tcp); - but it's nop on sysexit */
838 } else {
839 if (tcp->sys_func_rval & RVAL_DECODED)
840 sys_res = tcp->sys_func_rval;
841 else
842 sys_res = tcp_sysent(tcp)->sys_func(tcp);
843 }
844 }
845
846 if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
847 bool publish = syserror(tcp)
848 && is_number_in_set(STATUS_FAILED, status_set);
849 publish |= !syserror(tcp)
850 && is_number_in_set(STATUS_SUCCESSFUL, status_set);
851 if (cflag != CFLAG_ONLY_STATS)
852 strace_close_memstream(tcp, publish);
853 if (!publish) {
854 if (cflag != CFLAG_ONLY_STATS)
855 line_ended();
856 return 0;
857 }
858 }
859
860 if (syscall_limit != -1)
861 syscall_limit--;
862
863 if (cflag) {
864 count_syscall(tcp, ts);
865 if (cflag == CFLAG_ONLY_STATS)
866 return 0;
867 }
868
869 tprint_arg_end();
870 tprint_space();
871 tabto();
872 tprint_sysret_begin();
873
874 if (raw(tcp)) {
875 if (tcp->u_error) {
876 print_err_ret(tcp->u_rval, tcp->u_error);
877 } else {
878 tprints_sysret_next("retval");
879 PRINT_VAL_X(tcp->u_rval);
880 }
881 } else if (!(sys_res & RVAL_NONE) && tcp->u_error) {
882 switch (tcp->u_error) {
883 /* Blocked signals do not interrupt any syscalls.
884 * In this case syscalls don't return ERESTARTfoo codes.
885 *
886 * Deadly signals set to SIG_DFL interrupt syscalls
887 * and kill the process regardless of which of the codes below
888 * is returned by the interrupted syscall.
889 * In some cases, kernel forces a kernel-generated deadly
890 * signal to be unblocked and set to SIG_DFL (and thus cause
891 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
892 * or SIGILL. (The alternative is to leave process spinning
893 * forever on the faulty instruction - not useful).
894 *
895 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
896 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
897 * but kernel will always restart them.
898 */
899 case ERESTARTSYS:
900 /* Most common type of signal-interrupted syscall exit code.
901 * The system call will be restarted with the same arguments
902 * if SA_RESTART is set; otherwise, it will fail with EINTR.
903 */
904 print_erestart("ERESTARTSYS", "To be restarted if SA_RESTART is set");
905 break;
906 case ERESTARTNOINTR:
907 /* Rare. For example, fork() returns this if interrupted.
908 * SA_RESTART is ignored (assumed set): the restart is unconditional.
909 */
910 print_erestart("ERESTARTNOINTR", "To be restarted");
911 break;
912 case ERESTARTNOHAND:
913 /* pause(), rt_sigsuspend() etc use this code.
914 * SA_RESTART is ignored (assumed not set):
915 * syscall won't restart (will return EINTR instead)
916 * even after signal with SA_RESTART set. However,
917 * after SIG_IGN or SIG_DFL signal it will restart
918 * (thus the name "restart only if has no handler").
919 */
920 print_erestart("ERESTARTNOHAND", "To be restarted if no handler");
921 break;
922 case ERESTART_RESTARTBLOCK:
923 /* Syscalls like nanosleep(), poll() which can't be
924 * restarted with their original arguments use this
925 * code. Kernel will execute restart_syscall() instead,
926 * which changes arguments before restarting syscall.
927 * SA_RESTART is ignored (assumed not set) similarly
928 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
929 * since restart data is saved in "restart block"
930 * in task struct, and if signal handler uses a syscall
931 * which in turn saves another such restart block,
932 * old data is lost and restart becomes impossible)
933 */
934 print_erestart("ERESTART_RESTARTBLOCK", "Interrupted by signal");
935 break;
936 default:
937 print_err_ret(tcp->u_rval, tcp->u_error);
938 break;
939 }
940 } else {
941 tprints_sysret_next("retval");
942 if (sys_res & RVAL_NONE)
943 tprint_sysret_pseudo_rval();
944 else {
945 switch (sys_res & RVAL_MASK) {
946 case RVAL_HEX:
947 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
948 if (current_klongsize < sizeof(tcp->u_rval)) {
949 PRINT_VAL_X((unsigned int) tcp->u_rval);
950 } else
951 #endif
952 {
953 PRINT_VAL_X(tcp->u_rval);
954 }
955 break;
956 case RVAL_OCTAL: {
957 unsigned long long mode =
958 zero_extend_signed_to_ull(tcp->u_rval);
959 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
960 if (current_klongsize < sizeof(tcp->u_rval))
961 mode = (unsigned int) mode;
962 #endif
963 print_numeric_ll_umode_t(mode);
964 break;
965 }
966 case RVAL_UDECIMAL:
967 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
968 if (current_klongsize < sizeof(tcp->u_rval)) {
969 PRINT_VAL_U((unsigned int) tcp->u_rval);
970 } else
971 #endif
972 {
973 PRINT_VAL_U(tcp->u_rval);
974 }
975 break;
976 case RVAL_FD:
977 /*
978 * printfd accepts int as fd and it makes
979 * little sense to pass negative fds to it.
980 */
981 if ((current_klongsize < sizeof(tcp->u_rval)) ||
982 ((kernel_ulong_t) tcp->u_rval <= INT_MAX)) {
983 printfd(tcp, tcp->u_rval);
984 } else {
985 PRINT_VAL_D(tcp->u_rval);
986 }
987 break;
988 case RVAL_TID:
989 case RVAL_SID:
990 case RVAL_TGID:
991 case RVAL_PGID: {
992 #define _(_t) [RVAL_##_t - RVAL_TID] = PT_##_t
993 static const enum pid_type types[] = {
994 _(TID), _(SID), _(TGID), _(PGID),
995 };
996 #undef _
997
998 printpid(tcp, tcp->u_rval,
999 types[(sys_res & RVAL_MASK) - RVAL_TID]);
1000 break;
1001 }
1002 default:
1003 tprint_sysret_pseudo_rval();
1004 error_msg("invalid rval format");
1005 break;
1006 }
1007 }
1008 }
1009 if ((sys_res & RVAL_STR) && tcp->auxstr)
1010 tprints_sysret_string("retstr", tcp->auxstr);
1011 print_injected_note(tcp);
1012 if (Tflag) {
1013 tprints_sysret_next("time");
1014 tprint_associated_info_begin();
1015 ts_sub(ts, ts, &tcp->etime);
1016 PRINT_VAL_D(ts->tv_sec);
1017 if (Tflag_width) {
1018 tprintf_string(".%0*ld", Tflag_width,
1019 (long) ts->tv_nsec / Tflag_scale);
1020 }
1021 tprint_associated_info_end();
1022 }
1023 tprint_sysret_end();
1024 tprint_newline();
1025 dumpio(tcp);
1026 line_ended();
1027
1028 #ifdef ENABLE_STACKTRACE
1029 if (stack_trace_enabled)
1030 unwind_tcb_print(tcp);
1031 #endif
1032 return 0;
1033 }
1034
1035 void
1036 syscall_exiting_finish(struct tcb *tcp)
1037 {
1038 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED | TCB_INJECT_DELAY_EXIT |
1039 TCB_INJECT_POKE_EXIT | TCB_TAMPERED_DELAYED | TCB_TAMPERED_POKED);
1040 tcp->sys_func_rval = 0;
1041 free_tcb_priv_data(tcp);
1042
1043 #ifdef ENABLE_SECONTEXT
1044 tcp->last_dirfd = AT_FDCWD;
1045 #endif
1046
1047 if (cflag)
1048 tcp->ltime = tcp->stime;
1049 }
1050
1051 bool
1052 is_erestart(struct tcb *tcp)
1053 {
1054 switch (tcp->u_error) {
1055 case ERESTARTSYS:
1056 case ERESTARTNOINTR:
1057 case ERESTARTNOHAND:
1058 case ERESTART_RESTARTBLOCK:
1059 return true;
1060 default:
1061 return false;
1062 }
1063 }
1064
1065 static unsigned long saved_u_error;
1066
1067 void
1068 temporarily_clear_syserror(struct tcb *tcp)
1069 {
1070 saved_u_error = tcp->u_error;
1071 tcp->u_error = 0;
1072 }
1073
1074 void
1075 restore_cleared_syserror(struct tcb *tcp)
1076 {
1077 tcp->u_error = saved_u_error;
1078 }
1079
1080 static struct_ptrace_syscall_info ptrace_sci;
1081
1082 static bool
1083 ptrace_syscall_info_is_valid(void)
1084 {
1085 return ptrace_get_syscall_info_supported &&
1086 ptrace_sci.op <= PTRACE_SYSCALL_INFO_SECCOMP;
1087 }
1088
1089 #define XLAT_MACROS_ONLY
1090 #include "xlat/nt_descriptor_types.h"
1091 #undef XLAT_MACROS_ONLY
1092
1093 #define ARCH_MIGHT_USE_SET_REGS 1
1094
1095 #include "arch_regs.c"
1096
1097 #if HAVE_ARCH_GETRVAL2
1098 # include "arch_getrval2.c"
1099 #endif
1100
1101 #include "getregs_old.h"
1102 #ifdef HAVE_GETREGS_OLD
1103 /* Either getregs_old() or set_regs() */
1104 # undef ARCH_MIGHT_USE_SET_REGS
1105 # define ARCH_MIGHT_USE_SET_REGS 0
1106 #endif
1107
1108 #undef ptrace_getregset_or_getregs
1109 #undef ptrace_setregset_or_setregs
1110 #ifdef ARCH_REGS_FOR_GETREGSET
1111
1112 # define ptrace_getregset_or_getregs ptrace_getregset
1113 static long
1114 ptrace_getregset(pid_t pid)
1115 {
1116 # ifdef ARCH_IOVEC_FOR_GETREGSET
1117 /* variable iovec */
1118 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1119 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1120 &ARCH_IOVEC_FOR_GETREGSET);
1121 # else
1122 /* constant iovec */
1123 static struct iovec io = {
1124 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1125 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1126 };
1127 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1128
1129 # endif
1130 }
1131
1132 # if ARCH_MIGHT_USE_SET_REGS
1133 # define ptrace_setregset_or_setregs ptrace_setregset
1134 static int
1135 ptrace_setregset(pid_t pid)
1136 {
1137 # ifdef ARCH_IOVEC_FOR_GETREGSET
1138 /* variable iovec */
1139 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1140 &ARCH_IOVEC_FOR_GETREGSET);
1141 # else
1142 /* constant iovec */
1143 static struct iovec io = {
1144 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1145 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1146 };
1147 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1148 # endif
1149 }
1150 # endif /* ARCH_MIGHT_USE_SET_REGS */
1151
1152 #elif defined ARCH_REGS_FOR_GETREGS
1153
1154 # define ptrace_getregset_or_getregs ptrace_getregs
1155 static long
1156 ptrace_getregs(pid_t pid)
1157 {
1158 # if defined SPARC || defined SPARC64
1159 /* SPARC systems have the meaning of data and addr reversed */
1160 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1161 # else
1162 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1163 # endif
1164 }
1165
1166 # if ARCH_MIGHT_USE_SET_REGS
1167 # define ptrace_setregset_or_setregs ptrace_setregs
1168 static int
1169 ptrace_setregs(pid_t pid)
1170 {
1171 # if defined SPARC || defined SPARC64
1172 /* SPARC systems have the meaning of data and addr reversed */
1173 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1174 # else
1175 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1176 # endif
1177 }
1178 # endif /* ARCH_MIGHT_USE_SET_REGS */
1179
1180 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1181
1182 static long get_regs_error = -1;
1183
1184 void
1185 clear_regs(struct tcb *tcp)
1186 {
1187 ptrace_sci.op = 0xff;
1188 get_regs_error = -1;
1189 }
1190
1191 static long
1192 get_regs(struct tcb *const tcp)
1193 {
1194 #ifdef ptrace_getregset_or_getregs
1195
1196 if (get_regs_error != -1)
1197 return get_regs_error;
1198
1199 # ifdef HAVE_GETREGS_OLD
1200 /*
1201 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1202 * fallback to getregs_old.
1203 */
1204 static int use_getregs_old;
1205 if (use_getregs_old < 0) {
1206 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1207 } else if (use_getregs_old == 0) {
1208 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1209 if (get_regs_error >= 0) {
1210 use_getregs_old = -1;
1211 return get_regs_error;
1212 }
1213 if (errno == EPERM || errno == ESRCH)
1214 return get_regs_error;
1215 use_getregs_old = 1;
1216 }
1217 return get_regs_error = getregs_old(tcp);
1218 # else /* !HAVE_GETREGS_OLD */
1219 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1220 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1221
1222 # if defined ARCH_PERSONALITY_0_IOV_SIZE
1223 if (get_regs_error)
1224 return get_regs_error;
1225
1226 switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1227 case ARCH_PERSONALITY_0_IOV_SIZE:
1228 update_personality(tcp, 0);
1229 break;
1230 case ARCH_PERSONALITY_1_IOV_SIZE:
1231 update_personality(tcp, 1);
1232 break;
1233 default: {
1234 static bool printed = false;
1235
1236 if (!printed) {
1237 error_msg("Unsupported regset size returned by "
1238 "PTRACE_GETREGSET: %zu",
1239 ARCH_IOVEC_FOR_GETREGSET.iov_len);
1240
1241 printed = true;
1242 }
1243
1244 update_personality(tcp, 0);
1245 }
1246 }
1247 # endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1248
1249 return get_regs_error;
1250
1251 # endif /* !HAVE_GETREGS_OLD */
1252
1253 #else /* !ptrace_getregset_or_getregs */
1254
1255 # warning get_regs is not implemented for this architecture yet
1256 return 0;
1257
1258 #endif /* !ptrace_getregset_or_getregs */
1259 }
1260
1261 #ifdef ptrace_setregset_or_setregs
1262 static int
1263 set_regs(pid_t pid)
1264 {
1265 return ptrace_setregset_or_setregs(pid);
1266 }
1267 #endif /* ptrace_setregset_or_setregs */
1268
1269 struct sysent_buf {
1270 struct tcb *tcp;
1271 struct_sysent ent;
1272 char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
1273 };
1274
1275 static void
1276 free_sysent_buf(void *ptr)
1277 {
1278 struct sysent_buf *s = ptr;
1279 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1280 free(ptr);
1281 }
1282
1283 static bool
1284 strace_get_syscall_info(struct tcb *tcp)
1285 {
1286 /*
1287 * ptrace_get_syscall_info_supported should have been checked
1288 * by the caller.
1289 */
1290 if (ptrace_sci.op == 0xff) {
1291 const size_t size = sizeof(ptrace_sci);
1292 if (ptrace(PTRACE_GET_SYSCALL_INFO, tcp->pid,
1293 (void *) size, &ptrace_sci) < 0) {
1294 get_regs_error = -2;
1295 return false;
1296 }
1297 #if SUPPORTED_PERSONALITIES > 1
1298 int newpers = get_personality_from_syscall_info(&ptrace_sci);
1299 if (newpers >= 0)
1300 update_personality(tcp, newpers);
1301 #endif
1302 }
1303
1304 if (entering(tcp)) {
1305 if (ptrace_sci.op == PTRACE_SYSCALL_INFO_EXIT) {
1306 error_msg("pid %d: entering"
1307 ", ptrace_syscall_info.op == %u",
1308 tcp->pid, ptrace_sci.op);
1309 /* TODO: handle this. */
1310 }
1311 } else {
1312 if (ptrace_sci.op == PTRACE_SYSCALL_INFO_ENTRY) {
1313 error_msg("pid %d: exiting"
1314 ", ptrace_syscall_info.op == %u",
1315 tcp->pid, ptrace_sci.op);
1316 /* TODO: handle this. */
1317 }
1318 }
1319
1320 return true;
1321 }
1322
1323 bool
1324 get_instruction_pointer(struct tcb *tcp, kernel_ulong_t *ip)
1325 {
1326 if (get_regs_error < -1)
1327 return false;
1328
1329 if (ptrace_get_syscall_info_supported) {
1330 if (!strace_get_syscall_info(tcp))
1331 return false;
1332 *ip = (kernel_ulong_t) ptrace_sci.instruction_pointer;
1333 return true;
1334 }
1335
1336 #if defined ARCH_PC_REG
1337 if (get_regs(tcp) < 0)
1338 return false;
1339 *ip = (kernel_ulong_t) ARCH_PC_REG;
1340 return true;
1341 #elif defined ARCH_PC_PEEK_ADDR
1342 if (upeek(tcp, ARCH_PC_PEEK_ADDR, ip) < 0)
1343 return false;
1344 return true;
1345 #else
1346 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1347 #endif
1348 }
1349
1350 bool
1351 get_stack_pointer(struct tcb *tcp, kernel_ulong_t *sp)
1352 {
1353 if (get_regs_error < -1)
1354 return false;
1355
1356 if (ptrace_get_syscall_info_supported) {
1357 if (!strace_get_syscall_info(tcp))
1358 return false;
1359 *sp = (kernel_ulong_t) ptrace_sci.stack_pointer;
1360 return true;
1361 }
1362
1363 #if defined ARCH_SP_REG
1364 if (get_regs(tcp) < 0)
1365 return false;
1366 *sp = (kernel_ulong_t) ARCH_SP_REG;
1367 return true;
1368 #elif defined ARCH_SP_PEEK_ADDR
1369 if (upeek(tcp, ARCH_SP_PEEK_ADDR, sp) < 0)
1370 return false;
1371 return true;
1372 #else
1373 return false;
1374 #endif
1375 }
1376
1377 static int
1378 get_syscall_regs(struct tcb *tcp)
1379 {
1380 if (get_regs_error != -1)
1381 return get_regs_error;
1382
1383 if (ptrace_get_syscall_info_supported)
1384 return strace_get_syscall_info(tcp) ? 0 : get_regs_error;
1385
1386 return get_regs(tcp);
1387 }
1388
1389 const struct_sysent stub_sysent = {
1390 .nargs = MAX_ARGS,
1391 .sys_flags = MEMORY_MAPPING_CHANGE,
1392 .sen = SEN_printargs,
1393 .sys_func = printargs,
1394 .sys_name = "???",
1395 };
1396
1397 /*
1398 * Returns:
1399 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1400 * out silently" code.
1401 * 1: ok, continue in syscall_entering_decode().
1402 * other: error, syscall_entering_decode() should print error indicator
1403 * ("???" etc) and return an appropriate code.
1404 */
1405 int
1406 get_scno(struct tcb *tcp)
1407 {
1408 tcp->scno = -1;
1409 tcp->s_ent = NULL;
1410 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1411
1412 if (get_syscall_regs(tcp) < 0)
1413 return -1;
1414
1415 if (ptrace_syscall_info_is_valid()) {
1416 /* Apply arch-specific workarounds. */
1417 int rc = arch_check_scno(tcp);
1418 if (rc != 1)
1419 return rc;
1420 tcp->scno = ptrace_sci.entry.nr;
1421 } else {
1422 int rc = arch_get_scno(tcp);
1423 if (rc != 1)
1424 return rc;
1425 }
1426
1427 tcp->true_scno = tcp->scno;
1428 tcp->scno = shuffle_scno(tcp->scno);
1429
1430 if (scno_is_valid(tcp->scno)) {
1431 tcp->s_ent = &sysent[tcp->scno];
1432 tcp->qual_flg = qual_flags(tcp->scno);
1433 } else {
1434 struct sysent_buf *s = xzalloc(sizeof(*s));
1435
1436 s->tcp = tcp;
1437 s->ent = stub_sysent;
1438 s->ent.sys_name = s->buf;
1439 xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
1440
1441 tcp->s_ent = &s->ent;
1442
1443 set_tcb_priv_data(tcp, s, free_sysent_buf);
1444
1445 debug_msg("pid %d invalid syscall %#" PRI_klx,
1446 tcp->pid, shuffle_scno(tcp->scno));
1447 }
1448
1449 /*
1450 * We refrain from argument decoding during recovering
1451 * as tracee memory mappings has changed and the registers
1452 * are very likely pointing to garbage already.
1453 */
1454 if (recovering(tcp))
1455 tcp->qual_flg |= QUAL_RAW;
1456
1457 return 1;
1458 }
1459
1460 static int
1461 get_syscall_args(struct tcb *tcp)
1462 {
1463 if (ptrace_syscall_info_is_valid()) {
1464 const unsigned int n =
1465 MIN(ARRAY_SIZE(tcp->u_arg),
1466 ARRAY_SIZE(ptrace_sci.entry.args));
1467 for (unsigned int i = 0; i < n; ++i)
1468 tcp->u_arg[i] = ptrace_sci.entry.args[i];
1469 #if SUPPORTED_PERSONALITIES > 1
1470 if (tcp_sysent(tcp)->sys_flags & COMPAT_SYSCALL_TYPES) {
1471 for (unsigned int i = 0; i < n; ++i)
1472 tcp->u_arg[i] = (uint32_t) tcp->u_arg[i];
1473 }
1474 #endif
1475 /*
1476 * So far it's just a workaround for mips o32,
1477 * but let's pretend it could be used elsewhere.
1478 */
1479 #if MAX_ARGS > 6
1480 arch_get_syscall_args_extra(tcp, n);
1481 #endif
1482 return 1;
1483 }
1484 return arch_get_syscall_args(tcp);
1485 }
1486
1487 #ifdef ptrace_getregset_or_getregs
1488 # define get_syscall_result_regs get_syscall_regs
1489 #else
1490 static int get_syscall_result_regs(struct tcb *);
1491 #endif
1492
1493 /* Returns:
1494 * 1: ok, continue in syscall_exiting_trace().
1495 * -1: error, syscall_exiting_trace() should print error indicator
1496 * ("???" etc) and bail out.
1497 */
1498 static int
1499 get_syscall_result(struct tcb *tcp)
1500 {
1501 if (get_syscall_result_regs(tcp) < 0)
1502 return -1;
1503 get_error(tcp,
1504 (!(tcp_sysent(tcp)->sys_flags & SYSCALL_NEVER_FAILS)
1505 || syscall_tampered(tcp))
1506 && !syscall_tampered_nofail(tcp));
1507
1508 return 1;
1509 }
1510
1511 static void
1512 get_error(struct tcb *tcp, const bool check_errno)
1513 {
1514 if (ptrace_syscall_info_is_valid()) {
1515 if (ptrace_sci.exit.is_error) {
1516 tcp->u_rval = -1;
1517 tcp->u_error = -ptrace_sci.exit.rval;
1518 } else {
1519 tcp->u_error = 0;
1520 tcp->u_rval = ptrace_sci.exit.rval;
1521 }
1522 } else {
1523 tcp->u_error = 0;
1524 arch_get_error(tcp, check_errno);
1525 }
1526 }
1527
1528 static void
1529 set_error(struct tcb *tcp, unsigned long new_error)
1530 {
1531 const unsigned long old_error = tcp->u_error;
1532
1533 if (new_error == old_error || new_error > MAX_ERRNO_VALUE)
1534 return;
1535
1536 #ifdef ptrace_setregset_or_setregs
1537 /* if we are going to invoke set_regs, call get_regs first */
1538 if (get_regs(tcp) < 0)
1539 return;
1540 #endif
1541
1542 tcp->u_error = new_error;
1543 if (arch_set_error(tcp)) {
1544 tcp->u_error = old_error;
1545 /* arch_set_error does not update u_rval */
1546 } else {
1547 if (ptrace_syscall_info_is_valid())
1548 tcp->u_rval = -1;
1549 else
1550 get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
1551 SYSCALL_NEVER_FAILS));
1552 }
1553 }
1554
1555 static void
1556 set_success(struct tcb *tcp, kernel_long_t new_rval)
1557 {
1558 const kernel_long_t old_rval = tcp->u_rval;
1559
1560 #ifdef ptrace_setregset_or_setregs
1561 /* if we are going to invoke set_regs, call get_regs first */
1562 if (get_regs(tcp) < 0)
1563 return;
1564 #endif
1565
1566 tcp->u_rval = new_rval;
1567 if (arch_set_success(tcp)) {
1568 tcp->u_rval = old_rval;
1569 /* arch_set_success does not update u_error */
1570 } else {
1571 if (ptrace_syscall_info_is_valid())
1572 tcp->u_error = 0;
1573 else
1574 get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
1575 SYSCALL_NEVER_FAILS));
1576 }
1577 }
1578
1579 #include "get_scno.c"
1580 #include "check_scno.c"
1581 #include "set_scno.c"
1582 #include "get_syscall_args.c"
1583 #ifndef ptrace_getregset_or_getregs
1584 # include "get_syscall_result.c"
1585 #endif
1586 #include "get_error.c"
1587 #include "set_error.c"
1588 #ifdef HAVE_GETREGS_OLD
1589 # include "getregs_old.c"
1590 #endif
1591 #include "shuffle_scno.c"