]> granicus.if.org Git - strace/blob - clone.c
strace: terminate itself if interrupted by a signal
[strace] / clone.c
1 /*
2  * Copyright (c) 1999-2000 Wichert Akkerman <wichert@cistron.nl>
3  * Copyright (c) 2002-2005 Roland McGrath <roland@redhat.com>
4  * Copyright (c) 2008 Jan Kratochvil <jan.kratochvil@redhat.com>
5  * Copyright (c) 2009-2013 Denys Vlasenko <dvlasenk@redhat.com>
6  * Copyright (c) 2006-2015 Dmitry V. Levin <ldv@altlinux.org>
7  * Copyright (c) 2014-2018 The strace developers.
8  * All rights reserved.
9  *
10  * SPDX-License-Identifier: LGPL-2.1-or-later
11  */
12
13 #include "defs.h"
14 #include <sched.h>
15 #include <asm/unistd.h>
16
17 #ifndef CSIGNAL
18 # define CSIGNAL 0x000000ff
19 #endif
20
21 #include "xlat/clone_flags.h"
22 #include "xlat/setns_types.h"
23 #include "xlat/unshare_flags.h"
24
25 #if defined IA64
26 # define ARG_FLAGS      0
27 # define ARG_STACK      1
28 # define ARG_STACKSIZE  (tcp->scno == __NR_clone2 ? 2 : -1)
29 # define ARG_PTID       (tcp->scno == __NR_clone2 ? 3 : 2)
30 # define ARG_CTID       (tcp->scno == __NR_clone2 ? 4 : 3)
31 # define ARG_TLS        (tcp->scno == __NR_clone2 ? 5 : 4)
32 #elif defined S390 || defined S390X
33 # define ARG_STACK      0
34 # define ARG_FLAGS      1
35 # define ARG_PTID       2
36 # define ARG_CTID       3
37 # define ARG_TLS        4
38 #elif defined X86_64 || defined X32
39 /* x86 personality processes have the last two arguments flipped. */
40 # define ARG_FLAGS      0
41 # define ARG_STACK      1
42 # define ARG_PTID       2
43 # define ARG_CTID       ((current_personality != 1) ? 3 : 4)
44 # define ARG_TLS        ((current_personality != 1) ? 4 : 3)
45 #elif defined ALPHA || defined TILE || defined OR1K || defined RISCV
46 # define ARG_FLAGS      0
47 # define ARG_STACK      1
48 # define ARG_PTID       2
49 # define ARG_CTID       3
50 # define ARG_TLS        4
51 #else
52 # define ARG_FLAGS      0
53 # define ARG_STACK      1
54 # define ARG_PTID       2
55 # define ARG_TLS        3
56 # define ARG_CTID       4
57 #endif
58
59 static void
60 print_tls_arg(struct tcb *const tcp, const kernel_ulong_t addr)
61 {
62 #ifdef HAVE_STRUCT_USER_DESC
63 # if SUPPORTED_PERSONALITIES > 1
64         if (current_personality == 1)
65 # endif
66         {
67                 print_user_desc(tcp, tcp->u_arg[ARG_TLS], USER_DESC_BOTH);
68         }
69 # if SUPPORTED_PERSONALITIES > 1
70         else
71 # endif
72 #endif /* HAVE_STRUCT_USER_DESC */
73         {
74                 printaddr(tcp->u_arg[ARG_TLS]);
75         }
76 }
77
78 SYS_FUNC(clone)
79 {
80         if (exiting(tcp)) {
81                 const char *sep = "|";
82                 kernel_ulong_t flags = tcp->u_arg[ARG_FLAGS];
83                 tprints("child_stack=");
84                 printaddr(tcp->u_arg[ARG_STACK]);
85                 tprints(", ");
86 #ifdef ARG_STACKSIZE
87                 if (ARG_STACKSIZE != -1)
88                         tprintf("stack_size=%#" PRI_klx ", ",
89                                 tcp->u_arg[ARG_STACKSIZE]);
90 #endif
91                 tprints("flags=");
92                 if (!printflags64(clone_flags, flags & ~CSIGNAL, NULL))
93                         sep = "";
94                 if ((flags & CSIGNAL) != 0) {
95                         tprints(sep);
96                         printsignal(flags & CSIGNAL);
97                 }
98                 if ((flags & (CLONE_PARENT_SETTID|CLONE_CHILD_SETTID
99                               |CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0)
100                         return 0;
101                 if (flags & CLONE_PARENT_SETTID) {
102                         tprints(", parent_tidptr=");
103                         printaddr(tcp->u_arg[ARG_PTID]);
104                 }
105                 if (flags & CLONE_SETTLS) {
106                         tprints(", tls=");
107                         print_tls_arg(tcp, tcp->u_arg[ARG_TLS]);
108                 }
109                 if (flags & (CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) {
110                         tprints(", child_tidptr=");
111                         printaddr(tcp->u_arg[ARG_CTID]);
112                 }
113         }
114         /* TODO on syscall entry:
115          * We can clear CLONE_PTRACE here since it is an ancient hack
116          * to allow us to catch children, and we use another hack for that.
117          * But CLONE_PTRACE can conceivably be used by malicious programs
118          * to subvert us. By clearing this bit, we can defend against it:
119          * in untraced execution, CLONE_PTRACE should have no effect.
120          *
121          * We can also clear CLONE_UNTRACED, since it allows to start
122          * children outside of our control. At the moment
123          * I'm trying to figure out whether there is a *legitimate*
124          * use of this flag which we should respect.
125          */
126         return 0;
127 }
128
129 SYS_FUNC(setns)
130 {
131         printfd(tcp, tcp->u_arg[0]);
132         tprints(", ");
133         printxval(setns_types, tcp->u_arg[1], "CLONE_NEW???");
134
135         return RVAL_DECODED;
136 }
137
138 SYS_FUNC(unshare)
139 {
140         printflags64(unshare_flags, tcp->u_arg[0], "CLONE_???");
141         return RVAL_DECODED;
142 }
143
144 SYS_FUNC(fork)
145 {
146         return RVAL_DECODED;
147 }