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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "defs.h"
34 #include <sched.h>
35 #include <asm/unistd.h>
36 
37 #ifndef CSIGNAL
38 # define CSIGNAL 0x000000ff
39 #endif
40 
41 #include "xlat/clone_flags.h"
42 #include "xlat/setns_types.h"
43 #include "xlat/unshare_flags.h"
44 
45 #if defined IA64
46 # define ARG_FLAGS	0
47 # define ARG_STACK	1
48 # define ARG_STACKSIZE	(tcp->scno == __NR_clone2 ? 2 : -1)
49 # define ARG_PTID	(tcp->scno == __NR_clone2 ? 3 : 2)
50 # define ARG_CTID	(tcp->scno == __NR_clone2 ? 4 : 3)
51 # define ARG_TLS	(tcp->scno == __NR_clone2 ? 5 : 4)
52 #elif defined S390 || defined S390X
53 # define ARG_STACK	0
54 # define ARG_FLAGS	1
55 # define ARG_PTID	2
56 # define ARG_CTID	3
57 # define ARG_TLS	4
58 #elif defined X86_64 || defined X32
59 /* x86 personality processes have the last two arguments flipped. */
60 # define ARG_FLAGS	0
61 # define ARG_STACK	1
62 # define ARG_PTID	2
63 # define ARG_CTID	((current_personality != 1) ? 3 : 4)
64 # define ARG_TLS	((current_personality != 1) ? 4 : 3)
65 #elif defined ALPHA || defined TILE || defined OR1K || defined RISCV
66 # define ARG_FLAGS	0
67 # define ARG_STACK	1
68 # define ARG_PTID	2
69 # define ARG_CTID	3
70 # define ARG_TLS	4
71 #else
72 # define ARG_FLAGS	0
73 # define ARG_STACK	1
74 # define ARG_PTID	2
75 # define ARG_TLS	3
76 # define ARG_CTID	4
77 #endif
78 
79 static void
print_tls_arg(struct tcb * const tcp,const kernel_ulong_t addr)80 print_tls_arg(struct tcb *const tcp, const kernel_ulong_t addr)
81 {
82 #ifdef HAVE_STRUCT_USER_DESC
83 # if SUPPORTED_PERSONALITIES > 1
84 	if (current_personality == 1)
85 # endif
86 	{
87 		print_user_desc(tcp, tcp->u_arg[ARG_TLS], USER_DESC_BOTH);
88 	}
89 # if SUPPORTED_PERSONALITIES > 1
90 	else
91 # endif
92 #endif /* HAVE_STRUCT_USER_DESC */
93 	{
94 		printaddr(tcp->u_arg[ARG_TLS]);
95 	}
96 }
97 
SYS_FUNC(clone)98 SYS_FUNC(clone)
99 {
100 	if (exiting(tcp)) {
101 		const char *sep = "|";
102 		kernel_ulong_t flags = tcp->u_arg[ARG_FLAGS];
103 		tprints("child_stack=");
104 		printaddr(tcp->u_arg[ARG_STACK]);
105 		tprints(", ");
106 #ifdef ARG_STACKSIZE
107 		if (ARG_STACKSIZE != -1)
108 			tprintf("stack_size=%#" PRI_klx ", ",
109 				tcp->u_arg[ARG_STACKSIZE]);
110 #endif
111 		tprints("flags=");
112 		if (!printflags64(clone_flags, flags & ~CSIGNAL, NULL))
113 			sep = "";
114 		if ((flags & CSIGNAL) != 0)
115 			tprintf("%s%s", sep, signame(flags & CSIGNAL));
116 		if ((flags & (CLONE_PARENT_SETTID|CLONE_CHILD_SETTID
117 			      |CLONE_CHILD_CLEARTID|CLONE_SETTLS)) == 0)
118 			return 0;
119 		if (flags & CLONE_PARENT_SETTID) {
120 			tprints(", parent_tidptr=");
121 			printaddr(tcp->u_arg[ARG_PTID]);
122 		}
123 		if (flags & CLONE_SETTLS) {
124 			tprints(", tls=");
125 			print_tls_arg(tcp, tcp->u_arg[ARG_TLS]);
126 		}
127 		if (flags & (CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) {
128 			tprints(", child_tidptr=");
129 			printaddr(tcp->u_arg[ARG_CTID]);
130 		}
131 	}
132 	/* TODO on syscall entry:
133 	 * We can clear CLONE_PTRACE here since it is an ancient hack
134 	 * to allow us to catch children, and we use another hack for that.
135 	 * But CLONE_PTRACE can conceivably be used by malicious programs
136 	 * to subvert us. By clearing this bit, we can defend against it:
137 	 * in untraced execution, CLONE_PTRACE should have no effect.
138 	 *
139 	 * We can also clear CLONE_UNTRACED, since it allows to start
140 	 * children outside of our control. At the moment
141 	 * I'm trying to figure out whether there is a *legitimate*
142 	 * use of this flag which we should respect.
143 	 */
144 	return 0;
145 }
146 
SYS_FUNC(setns)147 SYS_FUNC(setns)
148 {
149 	printfd(tcp, tcp->u_arg[0]);
150 	tprints(", ");
151 	printxval(setns_types, tcp->u_arg[1], "CLONE_NEW???");
152 
153 	return RVAL_DECODED;
154 }
155 
SYS_FUNC(unshare)156 SYS_FUNC(unshare)
157 {
158 	printflags64(unshare_flags, tcp->u_arg[0], "CLONE_???");
159 	return RVAL_DECODED;
160 }
161 
SYS_FUNC(fork)162 SYS_FUNC(fork)
163 {
164 	return RVAL_DECODED;
165 }
166