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