1 /* $OpenBSD: kern_fork.c,v 1.237 2021/12/05 22:00:42 cheloha Exp $ */ 2 /* $NetBSD: kern_fork.c,v 1.29 1996/02/09 18:59:34 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/filedesc.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/mount.h> 46 #include <sys/proc.h> 47 #include <sys/exec.h> 48 #include <sys/resourcevar.h> 49 #include <sys/signalvar.h> 50 #include <sys/vnode.h> 51 #include <sys/vmmeter.h> 52 #include <sys/acct.h> 53 #include <sys/ktrace.h> 54 #include <sys/sched.h> 55 #include <sys/sysctl.h> 56 #include <sys/pool.h> 57 #include <sys/mman.h> 58 #include <sys/ptrace.h> 59 #include <sys/atomic.h> 60 #include <sys/pledge.h> 61 #include <sys/unistd.h> 62 63 #include <sys/syscallargs.h> 64 65 #include <uvm/uvm.h> 66 #include <machine/tcb.h> 67 68 int nprocesses = 1; /* process 0 */ 69 int nthreads = 1; /* proc 0 */ 70 int randompid; /* when set to 1, pid's go random */ 71 struct forkstat forkstat; 72 73 void fork_return(void *); 74 pid_t alloctid(void); 75 pid_t allocpid(void); 76 int ispidtaken(pid_t); 77 78 void unveil_copy(struct process *parent, struct process *child); 79 80 struct proc *thread_new(struct proc *_parent, vaddr_t _uaddr); 81 struct process *process_new(struct proc *, struct process *, int); 82 int fork_check_maxthread(uid_t _uid); 83 84 void 85 fork_return(void *arg) 86 { 87 struct proc *p = (struct proc *)arg; 88 89 if (p->p_p->ps_flags & PS_TRACED) 90 psignal(p, SIGTRAP); 91 92 child_return(p); 93 } 94 95 int 96 sys_fork(struct proc *p, void *v, register_t *retval) 97 { 98 void (*func)(void *) = child_return; 99 int flags; 100 101 flags = FORK_FORK; 102 if (p->p_p->ps_ptmask & PTRACE_FORK) { 103 flags |= FORK_PTRACE; 104 func = fork_return; 105 } 106 return fork1(p, flags, func, NULL, retval, NULL); 107 } 108 109 int 110 sys_vfork(struct proc *p, void *v, register_t *retval) 111 { 112 return fork1(p, FORK_VFORK|FORK_PPWAIT, child_return, NULL, 113 retval, NULL); 114 } 115 116 int 117 sys___tfork(struct proc *p, void *v, register_t *retval) 118 { 119 struct sys___tfork_args /* { 120 syscallarg(const struct __tfork) *param; 121 syscallarg(size_t) psize; 122 } */ *uap = v; 123 size_t psize = SCARG(uap, psize); 124 struct __tfork param = { 0 }; 125 int error; 126 127 if (psize == 0 || psize > sizeof(param)) 128 return EINVAL; 129 if ((error = copyin(SCARG(uap, param), ¶m, psize))) 130 return error; 131 #ifdef KTRACE 132 if (KTRPOINT(p, KTR_STRUCT)) 133 ktrstruct(p, "tfork", ¶m, sizeof(param)); 134 #endif 135 #ifdef TCB_INVALID 136 if (TCB_INVALID(param.tf_tcb)) 137 return EINVAL; 138 #endif /* TCB_INVALID */ 139 140 /* 141 * kbind(2) can only be used if it is initialized before the 142 * process goes multithreaded. 143 */ 144 if (p->p_p->ps_kbind_addr == 0) 145 p->p_p->ps_kbind_addr = BOGO_PC; 146 147 return thread_fork(p, param.tf_stack, param.tf_tcb, param.tf_tid, 148 retval); 149 } 150 151 /* 152 * Allocate and initialize a thread (proc) structure, given the parent thread. 153 */ 154 struct proc * 155 thread_new(struct proc *parent, vaddr_t uaddr) 156 { 157 struct proc *p; 158 159 p = pool_get(&proc_pool, PR_WAITOK); 160 p->p_stat = SIDL; /* protect against others */ 161 p->p_runpri = 0; 162 p->p_flag = 0; 163 164 /* 165 * Make a proc table entry for the new process. 166 * Start by zeroing the section of proc that is zero-initialized, 167 * then copy the section that is copied directly from the parent. 168 */ 169 memset(&p->p_startzero, 0, 170 (caddr_t)&p->p_endzero - (caddr_t)&p->p_startzero); 171 memcpy(&p->p_startcopy, &parent->p_startcopy, 172 (caddr_t)&p->p_endcopy - (caddr_t)&p->p_startcopy); 173 crhold(p->p_ucred); 174 p->p_addr = (struct user *)uaddr; 175 176 /* 177 * Initialize the timeouts. 178 */ 179 timeout_set(&p->p_sleep_to, endtsleep, p); 180 181 return p; 182 } 183 184 /* 185 * Initialize common bits of a process structure, given the initial thread. 186 */ 187 void 188 process_initialize(struct process *pr, struct proc *p) 189 { 190 /* initialize the thread links */ 191 pr->ps_mainproc = p; 192 TAILQ_INIT(&pr->ps_threads); 193 TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link); 194 pr->ps_refcnt = 1; 195 p->p_p = pr; 196 197 /* give the process the same creds as the initial thread */ 198 pr->ps_ucred = p->p_ucred; 199 crhold(pr->ps_ucred); 200 KASSERT(p->p_ucred->cr_ref >= 2); /* new thread and new process */ 201 202 LIST_INIT(&pr->ps_children); 203 LIST_INIT(&pr->ps_orphans); 204 LIST_INIT(&pr->ps_ftlist); 205 LIST_INIT(&pr->ps_sigiolst); 206 TAILQ_INIT(&pr->ps_tslpqueue); 207 208 rw_init(&pr->ps_lock, "pslock"); 209 mtx_init(&pr->ps_mtx, IPL_MPFLOOR); 210 211 timeout_set_kclock(&pr->ps_realit_to, realitexpire, pr, 212 KCLOCK_UPTIME, 0); 213 timeout_set(&pr->ps_rucheck_to, rucheck, pr); 214 } 215 216 217 /* 218 * Allocate and initialize a new process. 219 */ 220 struct process * 221 process_new(struct proc *p, struct process *parent, int flags) 222 { 223 struct process *pr; 224 225 pr = pool_get(&process_pool, PR_WAITOK); 226 227 /* 228 * Make a process structure for the new process. 229 * Start by zeroing the section of proc that is zero-initialized, 230 * then copy the section that is copied directly from the parent. 231 */ 232 memset(&pr->ps_startzero, 0, 233 (caddr_t)&pr->ps_endzero - (caddr_t)&pr->ps_startzero); 234 memcpy(&pr->ps_startcopy, &parent->ps_startcopy, 235 (caddr_t)&pr->ps_endcopy - (caddr_t)&pr->ps_startcopy); 236 237 process_initialize(pr, p); 238 pr->ps_pid = allocpid(); 239 lim_fork(parent, pr); 240 241 /* post-copy fixups */ 242 pr->ps_pptr = parent; 243 pr->ps_ppid = parent->ps_pid; 244 245 /* bump references to the text vnode (for sysctl) */ 246 pr->ps_textvp = parent->ps_textvp; 247 if (pr->ps_textvp) 248 vref(pr->ps_textvp); 249 250 /* copy unveil if unveil is active */ 251 unveil_copy(parent, pr); 252 253 pr->ps_flags = parent->ps_flags & 254 (PS_SUGID | PS_SUGIDEXEC | PS_PLEDGE | PS_EXECPLEDGE | PS_WXNEEDED); 255 if (parent->ps_session->s_ttyvp != NULL) 256 pr->ps_flags |= parent->ps_flags & PS_CONTROLT; 257 258 /* 259 * Duplicate sub-structures as needed. 260 * Increase reference counts on shared objects. 261 */ 262 if (flags & FORK_SHAREFILES) 263 pr->ps_fd = fdshare(parent); 264 else 265 pr->ps_fd = fdcopy(parent); 266 pr->ps_sigacts = sigactsinit(parent); 267 if (flags & FORK_SHAREVM) 268 pr->ps_vmspace = uvmspace_share(parent); 269 else 270 pr->ps_vmspace = uvmspace_fork(parent); 271 272 if (parent->ps_flags & PS_PROFIL) 273 startprofclock(pr); 274 if (flags & FORK_PTRACE) 275 pr->ps_flags |= parent->ps_flags & PS_TRACED; 276 if (flags & FORK_NOZOMBIE) 277 pr->ps_flags |= PS_NOZOMBIE; 278 if (flags & FORK_SYSTEM) 279 pr->ps_flags |= PS_SYSTEM; 280 281 /* mark as embryo to protect against others */ 282 pr->ps_flags |= PS_EMBRYO; 283 284 /* Force visibility of all of the above changes */ 285 membar_producer(); 286 287 /* it's sufficiently inited to be globally visible */ 288 LIST_INSERT_HEAD(&allprocess, pr, ps_list); 289 290 return pr; 291 } 292 293 /* print the 'table full' message once per 10 seconds */ 294 struct timeval fork_tfmrate = { 10, 0 }; 295 296 int 297 fork_check_maxthread(uid_t uid) 298 { 299 /* 300 * Although process entries are dynamically created, we still keep 301 * a global limit on the maximum number we will create. We reserve 302 * the last 5 processes to root. The variable nprocesses is the 303 * current number of processes, maxprocess is the limit. Similar 304 * rules for threads (struct proc): we reserve the last 5 to root; 305 * the variable nthreads is the current number of procs, maxthread is 306 * the limit. 307 */ 308 if ((nthreads >= maxthread - 5 && uid != 0) || nthreads >= maxthread) { 309 static struct timeval lasttfm; 310 311 if (ratecheck(&lasttfm, &fork_tfmrate)) 312 tablefull("thread"); 313 return EAGAIN; 314 } 315 nthreads++; 316 317 return 0; 318 } 319 320 static inline void 321 fork_thread_start(struct proc *p, struct proc *parent, int flags) 322 { 323 struct cpu_info *ci; 324 int s; 325 326 SCHED_LOCK(s); 327 ci = sched_choosecpu_fork(parent, flags); 328 setrunqueue(ci, p, p->p_usrpri); 329 SCHED_UNLOCK(s); 330 } 331 332 int 333 fork1(struct proc *curp, int flags, void (*func)(void *), void *arg, 334 register_t *retval, struct proc **rnewprocp) 335 { 336 struct process *curpr = curp->p_p; 337 struct process *pr; 338 struct proc *p; 339 uid_t uid = curp->p_ucred->cr_ruid; 340 struct vmspace *vm; 341 int count; 342 vaddr_t uaddr; 343 int error; 344 struct ptrace_state *newptstat = NULL; 345 346 KASSERT((flags & ~(FORK_FORK | FORK_VFORK | FORK_PPWAIT | FORK_PTRACE 347 | FORK_IDLE | FORK_SHAREVM | FORK_SHAREFILES | FORK_NOZOMBIE 348 | FORK_SYSTEM)) == 0); 349 KASSERT(func != NULL); 350 351 if ((error = fork_check_maxthread(uid))) 352 return error; 353 354 if ((nprocesses >= maxprocess - 5 && uid != 0) || 355 nprocesses >= maxprocess) { 356 static struct timeval lasttfm; 357 358 if (ratecheck(&lasttfm, &fork_tfmrate)) 359 tablefull("process"); 360 nthreads--; 361 return EAGAIN; 362 } 363 nprocesses++; 364 365 /* 366 * Increment the count of processes running with this uid. 367 * Don't allow a nonprivileged user to exceed their current limit. 368 */ 369 count = chgproccnt(uid, 1); 370 if (uid != 0 && count > lim_cur(RLIMIT_NPROC)) { 371 (void)chgproccnt(uid, -1); 372 nprocesses--; 373 nthreads--; 374 return EAGAIN; 375 } 376 377 uaddr = uvm_uarea_alloc(); 378 if (uaddr == 0) { 379 (void)chgproccnt(uid, -1); 380 nprocesses--; 381 nthreads--; 382 return (ENOMEM); 383 } 384 385 /* 386 * From now on, we're committed to the fork and cannot fail. 387 */ 388 p = thread_new(curp, uaddr); 389 pr = process_new(p, curpr, flags); 390 391 p->p_fd = pr->ps_fd; 392 p->p_vmspace = pr->ps_vmspace; 393 if (pr->ps_flags & PS_SYSTEM) 394 atomic_setbits_int(&p->p_flag, P_SYSTEM); 395 396 if (flags & FORK_PPWAIT) { 397 atomic_setbits_int(&pr->ps_flags, PS_PPWAIT); 398 atomic_setbits_int(&curpr->ps_flags, PS_ISPWAIT); 399 } 400 401 #ifdef KTRACE 402 /* 403 * Copy traceflag and tracefile if enabled. 404 * If not inherited, these were zeroed above. 405 */ 406 if (curpr->ps_traceflag & KTRFAC_INHERIT) 407 ktrsettrace(pr, curpr->ps_traceflag, curpr->ps_tracevp, 408 curpr->ps_tracecred); 409 #endif 410 411 /* 412 * Finish creating the child thread. cpu_fork() will copy 413 * and update the pcb and make the child ready to run. If 414 * this is a normal user fork, the child will exit directly 415 * to user mode via child_return() on its first time slice 416 * and will not return here. If this is a kernel thread, 417 * the specified entry point will be executed. 418 */ 419 cpu_fork(curp, p, NULL, NULL, func, arg ? arg : p); 420 421 vm = pr->ps_vmspace; 422 423 if (flags & FORK_FORK) { 424 forkstat.cntfork++; 425 forkstat.sizfork += vm->vm_dsize + vm->vm_ssize; 426 } else if (flags & FORK_VFORK) { 427 forkstat.cntvfork++; 428 forkstat.sizvfork += vm->vm_dsize + vm->vm_ssize; 429 } else { 430 forkstat.cntkthread++; 431 } 432 433 if (pr->ps_flags & PS_TRACED && flags & FORK_FORK) 434 newptstat = malloc(sizeof(*newptstat), M_SUBPROC, M_WAITOK); 435 436 p->p_tid = alloctid(); 437 438 LIST_INSERT_HEAD(&allproc, p, p_list); 439 LIST_INSERT_HEAD(TIDHASH(p->p_tid), p, p_hash); 440 LIST_INSERT_HEAD(PIDHASH(pr->ps_pid), pr, ps_hash); 441 LIST_INSERT_AFTER(curpr, pr, ps_pglist); 442 LIST_INSERT_HEAD(&curpr->ps_children, pr, ps_sibling); 443 444 if (pr->ps_flags & PS_TRACED) { 445 pr->ps_oppid = curpr->ps_pid; 446 process_reparent(pr, curpr->ps_pptr); 447 448 /* 449 * Set ptrace status. 450 */ 451 if (newptstat != NULL) { 452 pr->ps_ptstat = newptstat; 453 newptstat = NULL; 454 curpr->ps_ptstat->pe_report_event = PTRACE_FORK; 455 pr->ps_ptstat->pe_report_event = PTRACE_FORK; 456 curpr->ps_ptstat->pe_other_pid = pr->ps_pid; 457 pr->ps_ptstat->pe_other_pid = curpr->ps_pid; 458 } 459 } 460 461 /* 462 * For new processes, set accounting bits and mark as complete. 463 */ 464 nanouptime(&pr->ps_start); 465 pr->ps_acflag = AFORK; 466 atomic_clearbits_int(&pr->ps_flags, PS_EMBRYO); 467 468 if ((flags & FORK_IDLE) == 0) 469 fork_thread_start(p, curp, flags); 470 else 471 p->p_cpu = arg; 472 473 free(newptstat, M_SUBPROC, sizeof(*newptstat)); 474 475 /* 476 * Notify any interested parties about the new process. 477 */ 478 KNOTE(&curpr->ps_klist, NOTE_FORK | pr->ps_pid); 479 480 /* 481 * Update stats now that we know the fork was successful. 482 */ 483 uvmexp.forks++; 484 if (flags & FORK_PPWAIT) 485 uvmexp.forks_ppwait++; 486 if (flags & FORK_SHAREVM) 487 uvmexp.forks_sharevm++; 488 489 /* 490 * Pass a pointer to the new process to the caller. 491 */ 492 if (rnewprocp != NULL) 493 *rnewprocp = p; 494 495 /* 496 * Preserve synchronization semantics of vfork. If waiting for 497 * child to exec or exit, set PS_PPWAIT on child and PS_ISPWAIT 498 * on ourselves, and sleep on our process for the latter flag 499 * to go away. 500 * XXX Need to stop other rthreads in the parent 501 */ 502 if (flags & FORK_PPWAIT) 503 while (curpr->ps_flags & PS_ISPWAIT) 504 tsleep_nsec(curpr, PWAIT, "ppwait", INFSLP); 505 506 /* 507 * If we're tracing the child, alert the parent too. 508 */ 509 if ((flags & FORK_PTRACE) && (curpr->ps_flags & PS_TRACED)) 510 psignal(curp, SIGTRAP); 511 512 /* 513 * Return child pid to parent process 514 */ 515 if (retval != NULL) { 516 retval[0] = pr->ps_pid; 517 retval[1] = 0; 518 } 519 return (0); 520 } 521 522 int 523 thread_fork(struct proc *curp, void *stack, void *tcb, pid_t *tidptr, 524 register_t *retval) 525 { 526 struct process *pr = curp->p_p; 527 struct proc *p; 528 pid_t tid; 529 vaddr_t uaddr; 530 int s, error; 531 532 if (stack == NULL) 533 return EINVAL; 534 535 if ((error = fork_check_maxthread(curp->p_ucred->cr_ruid))) 536 return error; 537 538 uaddr = uvm_uarea_alloc(); 539 if (uaddr == 0) { 540 nthreads--; 541 return ENOMEM; 542 } 543 544 /* 545 * From now on, we're committed to the fork and cannot fail. 546 */ 547 p = thread_new(curp, uaddr); 548 atomic_setbits_int(&p->p_flag, P_THREAD); 549 sigstkinit(&p->p_sigstk); 550 551 /* other links */ 552 p->p_p = pr; 553 pr->ps_refcnt++; 554 555 /* local copies */ 556 p->p_fd = pr->ps_fd; 557 p->p_vmspace = pr->ps_vmspace; 558 559 /* 560 * Finish creating the child thread. cpu_fork() will copy 561 * and update the pcb and make the child ready to run. The 562 * child will exit directly to user mode via child_return() 563 * on its first time slice and will not return here. 564 */ 565 cpu_fork(curp, p, stack, tcb, child_return, p); 566 567 p->p_tid = alloctid(); 568 569 LIST_INSERT_HEAD(&allproc, p, p_list); 570 LIST_INSERT_HEAD(TIDHASH(p->p_tid), p, p_hash); 571 572 SCHED_LOCK(s); 573 TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link); 574 /* 575 * if somebody else wants to take us to single threaded mode, 576 * count ourselves in. 577 */ 578 if (pr->ps_single) { 579 atomic_inc_int(&pr->ps_singlecount); 580 atomic_setbits_int(&p->p_flag, P_SUSPSINGLE); 581 } 582 SCHED_UNLOCK(s); 583 584 /* 585 * Return tid to parent thread and copy it out to userspace 586 */ 587 retval[0] = tid = p->p_tid + THREAD_PID_OFFSET; 588 retval[1] = 0; 589 if (tidptr != NULL) { 590 if (copyout(&tid, tidptr, sizeof(tid))) 591 psignal(curp, SIGSEGV); 592 } 593 594 fork_thread_start(p, curp, 0); 595 596 /* 597 * Update stats now that we know the fork was successful. 598 */ 599 forkstat.cnttfork++; 600 uvmexp.forks++; 601 uvmexp.forks_sharevm++; 602 603 return 0; 604 } 605 606 607 /* Find an unused tid */ 608 pid_t 609 alloctid(void) 610 { 611 pid_t tid; 612 613 do { 614 /* (0 .. TID_MASK+1] */ 615 tid = 1 + (arc4random() & TID_MASK); 616 } while (tfind(tid) != NULL); 617 618 return (tid); 619 } 620 621 /* 622 * Checks for current use of a pid, either as a pid or pgid. 623 */ 624 pid_t oldpids[128]; 625 int 626 ispidtaken(pid_t pid) 627 { 628 uint32_t i; 629 630 for (i = 0; i < nitems(oldpids); i++) 631 if (pid == oldpids[i]) 632 return (1); 633 634 if (prfind(pid) != NULL) 635 return (1); 636 if (pgfind(pid) != NULL) 637 return (1); 638 if (zombiefind(pid) != NULL) 639 return (1); 640 return (0); 641 } 642 643 /* Find an unused pid */ 644 pid_t 645 allocpid(void) 646 { 647 static pid_t lastpid; 648 pid_t pid; 649 650 if (!randompid) { 651 /* only used early on for system processes */ 652 pid = ++lastpid; 653 } else { 654 /* Find an unused pid satisfying lastpid < pid <= PID_MAX */ 655 do { 656 pid = arc4random_uniform(PID_MAX - lastpid) + 1 + 657 lastpid; 658 } while (ispidtaken(pid)); 659 } 660 661 return pid; 662 } 663 664 void 665 freepid(pid_t pid) 666 { 667 static uint32_t idx; 668 669 oldpids[idx++ % nitems(oldpids)] = pid; 670 } 671 672 #if defined(MULTIPROCESSOR) 673 /* 674 * XXX This is a slight hack to get newly-formed processes to 675 * XXX acquire the kernel lock as soon as they run. 676 */ 677 void 678 proc_trampoline_mp(void) 679 { 680 SCHED_ASSERT_LOCKED(); 681 __mp_unlock(&sched_lock); 682 spl0(); 683 SCHED_ASSERT_UNLOCKED(); 684 KERNEL_ASSERT_UNLOCKED(); 685 686 KERNEL_LOCK(); 687 } 688 #endif 689