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