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