xref: /freebsd/sys/compat/linux/linux_fork.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004 Tim J. Robbins
5  * Copyright (c) 2002 Doug Rabson
6  * Copyright (c) 2000 Marcel Moolenaar
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/ktr.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/ptrace.h>
38 #include <sys/racct.h>
39 #include <sys/sched.h>
40 #include <sys/syscallsubr.h>
41 #include <sys/sx.h>
42 #include <sys/umtxvar.h>
43 #include <sys/unistd.h>
44 
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_map.h>
48 
49 #ifdef COMPAT_LINUX32
50 #include <machine/../linux32/linux.h>
51 #include <machine/../linux32/linux32_proto.h>
52 #else
53 #include <machine/../linux/linux.h>
54 #include <machine/../linux/linux_proto.h>
55 #endif
56 #include <compat/linux/linux.h>
57 #include <compat/linux/linux_emul.h>
58 #include <compat/linux/linux_fork.h>
59 #include <compat/linux/linux_futex.h>
60 #include <compat/linux/linux_mib.h>
61 #include <compat/linux/linux_misc.h>
62 #include <compat/linux/linux_util.h>
63 
64 #ifdef LINUX_LEGACY_SYSCALLS
65 int
66 linux_fork(struct thread *td, struct linux_fork_args *args)
67 {
68 	struct fork_req fr;
69 	int error;
70 	struct proc *p2;
71 	struct thread *td2;
72 
73 	bzero(&fr, sizeof(fr));
74 	fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
75 	fr.fr_procp = &p2;
76 	if ((error = fork1(td, &fr)) != 0)
77 		return (error);
78 
79 	td2 = FIRST_THREAD_IN_PROC(p2);
80 
81 	linux_proc_init(td, td2, false);
82 
83 	td->td_retval[0] = p2->p_pid;
84 
85 	/*
86 	 * Make this runnable after we are finished with it.
87 	 */
88 	thread_lock(td2);
89 	TD_SET_CAN_RUN(td2);
90 	sched_add(td2, SRQ_BORING);
91 
92 	return (0);
93 }
94 
95 int
96 linux_vfork(struct thread *td, struct linux_vfork_args *args)
97 {
98 	struct fork_req fr;
99 	int error;
100 	struct proc *p2;
101 	struct thread *td2;
102 
103 	bzero(&fr, sizeof(fr));
104 	fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
105 	fr.fr_procp = &p2;
106 	if ((error = fork1(td, &fr)) != 0)
107 		return (error);
108 
109 	td2 = FIRST_THREAD_IN_PROC(p2);
110 
111 	linux_proc_init(td, td2, false);
112 
113 	td->td_retval[0] = p2->p_pid;
114 
115 	/*
116 	 * Make this runnable after we are finished with it.
117 	 */
118 	thread_lock(td2);
119 	TD_SET_CAN_RUN(td2);
120 	sched_add(td2, SRQ_BORING);
121 
122 	return (0);
123 }
124 #endif
125 
126 static int
127 linux_clone_proc(struct thread *td, struct l_clone_args *args)
128 {
129 	struct fork_req fr;
130 	int error, ff, f2;
131 	struct proc *p2;
132 	struct thread *td2;
133 	int exit_signal;
134 	struct linux_emuldata *em;
135 
136 	f2 = 0;
137 	ff = RFPROC | RFSTOPPED;
138 	if (LINUX_SIG_VALID(args->exit_signal)) {
139 		exit_signal = linux_to_bsd_signal(args->exit_signal);
140 	} else if (args->exit_signal != 0)
141 		return (EINVAL);
142 	else
143 		exit_signal = 0;
144 
145 	if (args->flags & LINUX_CLONE_VM)
146 		ff |= RFMEM;
147 	if (args->flags & LINUX_CLONE_SIGHAND)
148 		ff |= RFSIGSHARE;
149 	if ((args->flags & LINUX_CLONE_CLEAR_SIGHAND) != 0)
150 		f2 |= FR2_DROPSIG_CAUGHT;
151 	if (args->flags & LINUX_CLONE_FILES) {
152 		if (!(args->flags & LINUX_CLONE_FS))
153 			f2 |= FR2_SHARE_PATHS;
154 	} else {
155 		ff |= RFFDG;
156 		if (args->flags & LINUX_CLONE_FS)
157 			f2 |= FR2_SHARE_PATHS;
158 	}
159 
160 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
161 		if (args->parent_tid == NULL)
162 			return (EINVAL);
163 
164 	if (args->flags & LINUX_CLONE_VFORK)
165 		ff |= RFPPWAIT;
166 
167 	bzero(&fr, sizeof(fr));
168 	fr.fr_flags = ff;
169 	fr.fr_flags2 = f2;
170 	fr.fr_procp = &p2;
171 	error = fork1(td, &fr);
172 	if (error)
173 		return (error);
174 
175 	td2 = FIRST_THREAD_IN_PROC(p2);
176 
177 	/* create the emuldata */
178 	linux_proc_init(td, td2, false);
179 
180 	em = em_find(td2);
181 	KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
182 
183 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
184 		em->child_set_tid = args->child_tid;
185 	else
186 		em->child_set_tid = NULL;
187 
188 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
189 		em->child_clear_tid = args->child_tid;
190 	else
191 		em->child_clear_tid = NULL;
192 
193 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
194 		error = copyout(&p2->p_pid, args->parent_tid,
195 		    sizeof(p2->p_pid));
196 		if (error)
197 			linux_msg(td, "copyout p_pid failed!");
198 	}
199 
200 	PROC_LOCK(p2);
201 	p2->p_sigparent = exit_signal;
202 	PROC_UNLOCK(p2);
203 	/*
204 	 * In a case of stack = NULL, we are supposed to COW calling process
205 	 * stack. This is what normal fork() does, so we just keep tf_rsp arg
206 	 * intact.
207 	 */
208 	linux_set_upcall(td2, args->stack);
209 
210 	if (args->flags & LINUX_CLONE_SETTLS)
211 		linux_set_cloned_tls(td2, PTRIN(args->tls));
212 
213 	/*
214 	 * If CLONE_PARENT is set, then the parent of the new process will be
215 	 * the same as that of the calling process.
216 	 */
217 	if (args->flags & LINUX_CLONE_PARENT) {
218 		sx_xlock(&proctree_lock);
219 		PROC_LOCK(p2);
220 		proc_reparent(p2, td->td_proc->p_pptr, true);
221 		PROC_UNLOCK(p2);
222 		sx_xunlock(&proctree_lock);
223 	}
224 
225 	/*
226 	 * Make this runnable after we are finished with it.
227 	 */
228 	thread_lock(td2);
229 	TD_SET_CAN_RUN(td2);
230 	sched_add(td2, SRQ_BORING);
231 
232 	td->td_retval[0] = p2->p_pid;
233 
234 	return (0);
235 }
236 
237 static int
238 linux_clone_thread(struct thread *td, struct l_clone_args *args)
239 {
240 	struct linux_emuldata *em;
241 	struct thread *newtd;
242 	struct proc *p;
243 	int error;
244 
245 	LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
246 	    td->td_tid, (unsigned)args->flags,
247 	    args->parent_tid, args->child_tid);
248 
249 	if ((args->flags & LINUX_CLONE_PARENT) != 0)
250 		return (EINVAL);
251 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
252 		if (args->parent_tid == NULL)
253 			return (EINVAL);
254 
255 	/* Threads should be created with own stack */
256 	if (PTRIN(args->stack) == NULL)
257 		return (EINVAL);
258 
259 	p = td->td_proc;
260 
261 #ifdef RACCT
262 	if (racct_enable) {
263 		PROC_LOCK(p);
264 		error = racct_add(p, RACCT_NTHR, 1);
265 		PROC_UNLOCK(p);
266 		if (error != 0)
267 			return (EPROCLIM);
268 	}
269 #endif
270 
271 	/* Initialize our td */
272 	error = kern_thr_alloc(p, 0, &newtd);
273 	if (error)
274 		goto fail;
275 
276 	bzero(&newtd->td_startzero,
277 	    __rangeof(struct thread, td_startzero, td_endzero));
278 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
279 	    __rangeof(struct thread, td_startcopy, td_endcopy));
280 
281 	newtd->td_proc = p;
282 	thread_cow_get(newtd, td);
283 
284 	cpu_copy_thread(newtd, td);
285 
286 	/* create the emuldata */
287 	linux_proc_init(td, newtd, true);
288 
289 	em = em_find(newtd);
290 	KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
291 
292 	if (args->flags & LINUX_CLONE_SETTLS)
293 		linux_set_cloned_tls(newtd, PTRIN(args->tls));
294 
295 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
296 		em->child_set_tid = args->child_tid;
297 	else
298 		em->child_set_tid = NULL;
299 
300 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
301 		em->child_clear_tid = args->child_tid;
302 	else
303 		em->child_clear_tid = NULL;
304 
305 	cpu_thread_clean(newtd);
306 
307 	linux_set_upcall(newtd, args->stack);
308 
309 	PROC_LOCK(p);
310 	p->p_flag |= P_HADTHREADS;
311 	thread_link(newtd, p);
312 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
313 
314 	thread_lock(td);
315 	/* let the scheduler know about these things. */
316 	sched_fork_thread(td, newtd);
317 	thread_unlock(td);
318 	if (P_SHOULDSTOP(p))
319 		ast_sched(newtd, TDA_SUSPEND);
320 
321 	if (p->p_ptevents & PTRACE_LWP)
322 		newtd->td_dbgflags |= TDB_BORN;
323 	PROC_UNLOCK(p);
324 
325 	tidhash_add(newtd);
326 
327 	LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
328 	    td->td_tid, newtd->td_tid);
329 
330 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
331 		error = copyout(&newtd->td_tid, args->parent_tid,
332 		    sizeof(newtd->td_tid));
333 		if (error)
334 			linux_msg(td, "clone_thread: copyout td_tid failed!");
335 	}
336 
337 	/*
338 	 * Make this runnable after we are finished with it.
339 	 */
340 	thread_lock(newtd);
341 	TD_SET_CAN_RUN(newtd);
342 	sched_add(newtd, SRQ_BORING);
343 
344 	td->td_retval[0] = newtd->td_tid;
345 
346 	return (0);
347 
348 fail:
349 #ifdef RACCT
350 	if (racct_enable) {
351 		PROC_LOCK(p);
352 		racct_sub(p, RACCT_NTHR, 1);
353 		PROC_UNLOCK(p);
354 	}
355 #endif
356 	return (error);
357 }
358 
359 int
360 linux_clone(struct thread *td, struct linux_clone_args *args)
361 {
362 	struct l_clone_args ca = {
363 		.flags = (lower_32_bits(args->flags) & ~LINUX_CSIGNAL),
364 		.child_tid = args->child_tidptr,
365 		.parent_tid = args->parent_tidptr,
366 		.exit_signal = (lower_32_bits(args->flags) & LINUX_CSIGNAL),
367 		.stack = args->stack,
368 		.tls = args->tls,
369 	};
370 
371 	if (args->flags & LINUX_CLONE_THREAD)
372 		return (linux_clone_thread(td, &ca));
373 	else
374 		return (linux_clone_proc(td, &ca));
375 }
376 
377 
378 static int
379 linux_clone3_args_valid(struct l_user_clone_args *uca)
380 {
381 
382 	/* Verify that no unknown flags are passed along. */
383 	if ((uca->flags & ~(LINUX_CLONE_LEGACY_FLAGS |
384 	    LINUX_CLONE_CLEAR_SIGHAND | LINUX_CLONE_INTO_CGROUP)) != 0)
385 		return (EINVAL);
386 	if ((uca->flags & (LINUX_CLONE_DETACHED | LINUX_CSIGNAL)) != 0)
387 		return (EINVAL);
388 
389 	if ((uca->flags & (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND)) ==
390 	    (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND))
391 		return (EINVAL);
392 	if ((uca->flags & (LINUX_CLONE_THREAD | LINUX_CLONE_PARENT)) != 0 &&
393 	    uca->exit_signal != 0)
394 		return (EINVAL);
395 
396 	/* We don't support set_tid, only validate input. */
397 	if (uca->set_tid_size > LINUX_MAX_PID_NS_LEVEL)
398 		return (EINVAL);
399 	if (uca->set_tid == 0 && uca->set_tid_size > 0)
400 		return (EINVAL);
401 	if (uca->set_tid != 0 && uca->set_tid_size == 0)
402 		return (EINVAL);
403 
404 	if (uca->stack == 0 && uca->stack_size > 0)
405 		return (EINVAL);
406 	if (uca->stack != 0 && uca->stack_size == 0)
407 		return (EINVAL);
408 
409 	/* Verify that higher 32bits of exit_signal are unset. */
410 	if ((uca->exit_signal & ~(uint64_t)LINUX_CSIGNAL) != 0)
411 		return (EINVAL);
412 
413 	/* Verify that no unsupported flags are passed along. */
414 	if ((uca->flags & LINUX_CLONE_NEWTIME) != 0) {
415 		LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_NEWTIME");
416 		return (ENOSYS);
417 	}
418 	if ((uca->flags & LINUX_CLONE_INTO_CGROUP) != 0) {
419 		LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_INTO_CGROUP");
420 		return (ENOSYS);
421 	}
422 	if (uca->set_tid != 0 || uca->set_tid_size != 0) {
423 		LINUX_RATELIMIT_MSG("unsupported clone3 set_tid");
424 		return (ENOSYS);
425 	}
426 
427 	return (0);
428 }
429 
430 int
431 linux_clone3(struct thread *td, struct linux_clone3_args *args)
432 {
433 	struct l_user_clone_args *uca;
434 	struct l_clone_args *ca;
435 	size_t size;
436 	int error;
437 
438 	if (args->usize > PAGE_SIZE)
439 		return (E2BIG);
440 	if (args->usize < LINUX_CLONE_ARGS_SIZE_VER0)
441 		return (EINVAL);
442 
443 	/*
444 	 * usize can be less than size of struct clone_args, to avoid using
445 	 * of uninitialized data of struct clone_args, allocate at least
446 	 * sizeof(struct clone_args) storage and zero it.
447 	 */
448 	size = max(args->usize, sizeof(*uca));
449 	uca = malloc(size, M_LINUX, M_WAITOK | M_ZERO);
450 	error = copyin(args->uargs, uca, args->usize);
451 	if (error != 0)
452 		goto out;
453 	error = linux_clone3_args_valid(uca);
454 	if (error != 0)
455 		goto out;
456 	ca = malloc(sizeof(*ca), M_LINUX, M_WAITOK | M_ZERO);
457 	ca->flags = uca->flags;
458 	ca->child_tid = PTRIN(uca->child_tid);
459 	ca->parent_tid = PTRIN(uca->parent_tid);
460 	ca->exit_signal = uca->exit_signal;
461 	ca->stack = uca->stack + uca->stack_size;
462 	ca->stack_size = uca->stack_size;
463 	ca->tls = uca->tls;
464 
465 	if ((ca->flags & LINUX_CLONE_THREAD) != 0)
466 		error = linux_clone_thread(td, ca);
467 	else
468 		error = linux_clone_proc(td, ca);
469 	free(ca, M_LINUX);
470 out:
471 	free(uca, M_LINUX);
472 	return (error);
473 }
474 
475 int
476 linux_exit(struct thread *td, struct linux_exit_args *args)
477 {
478 	struct linux_emuldata *em __diagused;
479 
480 	em = em_find(td);
481 	KASSERT(em != NULL, ("exit: emuldata not found.\n"));
482 
483 	LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
484 
485 	linux_thread_detach(td);
486 
487 	/*
488 	 * XXX. When the last two threads of a process
489 	 * exit via pthread_exit() try thr_exit() first.
490 	 */
491 	kern_thr_exit(td);
492 	exit1(td, args->rval, 0);
493 		/* NOTREACHED */
494 }
495 
496 int
497 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
498 {
499 	struct linux_emuldata *em;
500 
501 	em = em_find(td);
502 	KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
503 
504 	em->child_clear_tid = args->tidptr;
505 
506 	td->td_retval[0] = em->em_tid;
507 
508 	LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
509 	    em->em_tid, args->tidptr, td->td_retval[0]);
510 
511 	return (0);
512 }
513 
514 void
515 linux_thread_detach(struct thread *td)
516 {
517 	struct linux_emuldata *em;
518 	int *child_clear_tid;
519 	int error;
520 
521 	em = em_find(td);
522 	KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
523 
524 	LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
525 
526 	release_futexes(td, em);
527 
528 	child_clear_tid = em->child_clear_tid;
529 
530 	if (child_clear_tid != NULL) {
531 		LINUX_CTR2(thread_detach, "thread(%d) %p",
532 		    em->em_tid, child_clear_tid);
533 
534 		error = suword32(child_clear_tid, 0);
535 		if (error != 0)
536 			return;
537 
538 		error = futex_wake(td, child_clear_tid, 1, false);
539 		/*
540 		 * this cannot happen at the moment and if this happens it
541 		 * probably means there is a user space bug
542 		 */
543 		if (error != 0)
544 			linux_msg(td, "futex stuff in thread_detach failed.");
545 	}
546 
547 	/*
548 	 * Do not rely on the robust list which is maintained by userspace,
549 	 * cleanup remaining pi (if any) after release_futexes anyway.
550 	 */
551 	umtx_thread_exit(td);
552 }
553