1 /* $OpenBSD: kern_fork.c,v 1.238 2021/12/10 05:34:42 guenther 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 return thread_fork(p, param.tf_stack, param.tf_tcb, param.tf_tid, 141 retval); 142 } 143 144 /* 145 * Allocate and initialize a thread (proc) structure, given the parent thread. 146 */ 147 struct proc * 148 thread_new(struct proc *parent, vaddr_t uaddr) 149 { 150 struct proc *p; 151 152 p = pool_get(&proc_pool, PR_WAITOK); 153 p->p_stat = SIDL; /* protect against others */ 154 p->p_runpri = 0; 155 p->p_flag = 0; 156 157 /* 158 * Make a proc table entry for the new process. 159 * Start by zeroing the section of proc that is zero-initialized, 160 * then copy the section that is copied directly from the parent. 161 */ 162 memset(&p->p_startzero, 0, 163 (caddr_t)&p->p_endzero - (caddr_t)&p->p_startzero); 164 memcpy(&p->p_startcopy, &parent->p_startcopy, 165 (caddr_t)&p->p_endcopy - (caddr_t)&p->p_startcopy); 166 crhold(p->p_ucred); 167 p->p_addr = (struct user *)uaddr; 168 169 /* 170 * Initialize the timeouts. 171 */ 172 timeout_set(&p->p_sleep_to, endtsleep, p); 173 174 return p; 175 } 176 177 /* 178 * Initialize common bits of a process structure, given the initial thread. 179 */ 180 void 181 process_initialize(struct process *pr, struct proc *p) 182 { 183 /* initialize the thread links */ 184 pr->ps_mainproc = p; 185 TAILQ_INIT(&pr->ps_threads); 186 TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link); 187 pr->ps_refcnt = 1; 188 p->p_p = pr; 189 190 /* give the process the same creds as the initial thread */ 191 pr->ps_ucred = p->p_ucred; 192 crhold(pr->ps_ucred); 193 KASSERT(p->p_ucred->cr_ref >= 2); /* new thread and new process */ 194 195 LIST_INIT(&pr->ps_children); 196 LIST_INIT(&pr->ps_orphans); 197 LIST_INIT(&pr->ps_ftlist); 198 LIST_INIT(&pr->ps_sigiolst); 199 TAILQ_INIT(&pr->ps_tslpqueue); 200 201 rw_init(&pr->ps_lock, "pslock"); 202 mtx_init(&pr->ps_mtx, IPL_MPFLOOR); 203 204 timeout_set_kclock(&pr->ps_realit_to, realitexpire, pr, 205 KCLOCK_UPTIME, 0); 206 timeout_set(&pr->ps_rucheck_to, rucheck, pr); 207 } 208 209 210 /* 211 * Allocate and initialize a new process. 212 */ 213 struct process * 214 process_new(struct proc *p, struct process *parent, int flags) 215 { 216 struct process *pr; 217 218 pr = pool_get(&process_pool, PR_WAITOK); 219 220 /* 221 * Make a process structure for the new process. 222 * Start by zeroing the section of proc that is zero-initialized, 223 * then copy the section that is copied directly from the parent. 224 */ 225 memset(&pr->ps_startzero, 0, 226 (caddr_t)&pr->ps_endzero - (caddr_t)&pr->ps_startzero); 227 memcpy(&pr->ps_startcopy, &parent->ps_startcopy, 228 (caddr_t)&pr->ps_endcopy - (caddr_t)&pr->ps_startcopy); 229 230 process_initialize(pr, p); 231 pr->ps_pid = allocpid(); 232 lim_fork(parent, pr); 233 234 /* post-copy fixups */ 235 pr->ps_pptr = parent; 236 pr->ps_ppid = parent->ps_pid; 237 238 /* bump references to the text vnode (for sysctl) */ 239 pr->ps_textvp = parent->ps_textvp; 240 if (pr->ps_textvp) 241 vref(pr->ps_textvp); 242 243 /* copy unveil if unveil is active */ 244 unveil_copy(parent, pr); 245 246 pr->ps_flags = parent->ps_flags & 247 (PS_SUGID | PS_SUGIDEXEC | PS_PLEDGE | PS_EXECPLEDGE | PS_WXNEEDED); 248 if (parent->ps_session->s_ttyvp != NULL) 249 pr->ps_flags |= parent->ps_flags & PS_CONTROLT; 250 251 /* 252 * Duplicate sub-structures as needed. 253 * Increase reference counts on shared objects. 254 */ 255 if (flags & FORK_SHAREFILES) 256 pr->ps_fd = fdshare(parent); 257 else 258 pr->ps_fd = fdcopy(parent); 259 pr->ps_sigacts = sigactsinit(parent); 260 if (flags & FORK_SHAREVM) 261 pr->ps_vmspace = uvmspace_share(parent); 262 else 263 pr->ps_vmspace = uvmspace_fork(parent); 264 265 if (parent->ps_flags & PS_PROFIL) 266 startprofclock(pr); 267 if (flags & FORK_PTRACE) 268 pr->ps_flags |= parent->ps_flags & PS_TRACED; 269 if (flags & FORK_NOZOMBIE) 270 pr->ps_flags |= PS_NOZOMBIE; 271 if (flags & FORK_SYSTEM) 272 pr->ps_flags |= PS_SYSTEM; 273 274 /* mark as embryo to protect against others */ 275 pr->ps_flags |= PS_EMBRYO; 276 277 /* Force visibility of all of the above changes */ 278 membar_producer(); 279 280 /* it's sufficiently inited to be globally visible */ 281 LIST_INSERT_HEAD(&allprocess, pr, ps_list); 282 283 return pr; 284 } 285 286 /* print the 'table full' message once per 10 seconds */ 287 struct timeval fork_tfmrate = { 10, 0 }; 288 289 int 290 fork_check_maxthread(uid_t uid) 291 { 292 /* 293 * Although process entries are dynamically created, we still keep 294 * a global limit on the maximum number we will create. We reserve 295 * the last 5 processes to root. The variable nprocesses is the 296 * current number of processes, maxprocess is the limit. Similar 297 * rules for threads (struct proc): we reserve the last 5 to root; 298 * the variable nthreads is the current number of procs, maxthread is 299 * the limit. 300 */ 301 if ((nthreads >= maxthread - 5 && uid != 0) || nthreads >= maxthread) { 302 static struct timeval lasttfm; 303 304 if (ratecheck(&lasttfm, &fork_tfmrate)) 305 tablefull("thread"); 306 return EAGAIN; 307 } 308 nthreads++; 309 310 return 0; 311 } 312 313 static inline void 314 fork_thread_start(struct proc *p, struct proc *parent, int flags) 315 { 316 struct cpu_info *ci; 317 int s; 318 319 SCHED_LOCK(s); 320 ci = sched_choosecpu_fork(parent, flags); 321 setrunqueue(ci, p, p->p_usrpri); 322 SCHED_UNLOCK(s); 323 } 324 325 int 326 fork1(struct proc *curp, int flags, void (*func)(void *), void *arg, 327 register_t *retval, struct proc **rnewprocp) 328 { 329 struct process *curpr = curp->p_p; 330 struct process *pr; 331 struct proc *p; 332 uid_t uid = curp->p_ucred->cr_ruid; 333 struct vmspace *vm; 334 int count; 335 vaddr_t uaddr; 336 int error; 337 struct ptrace_state *newptstat = NULL; 338 339 KASSERT((flags & ~(FORK_FORK | FORK_VFORK | FORK_PPWAIT | FORK_PTRACE 340 | FORK_IDLE | FORK_SHAREVM | FORK_SHAREFILES | FORK_NOZOMBIE 341 | FORK_SYSTEM)) == 0); 342 KASSERT(func != NULL); 343 344 if ((error = fork_check_maxthread(uid))) 345 return error; 346 347 if ((nprocesses >= maxprocess - 5 && uid != 0) || 348 nprocesses >= maxprocess) { 349 static struct timeval lasttfm; 350 351 if (ratecheck(&lasttfm, &fork_tfmrate)) 352 tablefull("process"); 353 nthreads--; 354 return EAGAIN; 355 } 356 nprocesses++; 357 358 /* 359 * Increment the count of processes running with this uid. 360 * Don't allow a nonprivileged user to exceed their current limit. 361 */ 362 count = chgproccnt(uid, 1); 363 if (uid != 0 && count > lim_cur(RLIMIT_NPROC)) { 364 (void)chgproccnt(uid, -1); 365 nprocesses--; 366 nthreads--; 367 return EAGAIN; 368 } 369 370 uaddr = uvm_uarea_alloc(); 371 if (uaddr == 0) { 372 (void)chgproccnt(uid, -1); 373 nprocesses--; 374 nthreads--; 375 return (ENOMEM); 376 } 377 378 /* 379 * From now on, we're committed to the fork and cannot fail. 380 */ 381 p = thread_new(curp, uaddr); 382 pr = process_new(p, curpr, flags); 383 384 p->p_fd = pr->ps_fd; 385 p->p_vmspace = pr->ps_vmspace; 386 if (pr->ps_flags & PS_SYSTEM) 387 atomic_setbits_int(&p->p_flag, P_SYSTEM); 388 389 if (flags & FORK_PPWAIT) { 390 atomic_setbits_int(&pr->ps_flags, PS_PPWAIT); 391 atomic_setbits_int(&curpr->ps_flags, PS_ISPWAIT); 392 } 393 394 #ifdef KTRACE 395 /* 396 * Copy traceflag and tracefile if enabled. 397 * If not inherited, these were zeroed above. 398 */ 399 if (curpr->ps_traceflag & KTRFAC_INHERIT) 400 ktrsettrace(pr, curpr->ps_traceflag, curpr->ps_tracevp, 401 curpr->ps_tracecred); 402 #endif 403 404 /* 405 * Finish creating the child thread. cpu_fork() will copy 406 * and update the pcb and make the child ready to run. If 407 * this is a normal user fork, the child will exit directly 408 * to user mode via child_return() on its first time slice 409 * and will not return here. If this is a kernel thread, 410 * the specified entry point will be executed. 411 */ 412 cpu_fork(curp, p, NULL, NULL, func, arg ? arg : p); 413 414 vm = pr->ps_vmspace; 415 416 if (flags & FORK_FORK) { 417 forkstat.cntfork++; 418 forkstat.sizfork += vm->vm_dsize + vm->vm_ssize; 419 } else if (flags & FORK_VFORK) { 420 forkstat.cntvfork++; 421 forkstat.sizvfork += vm->vm_dsize + vm->vm_ssize; 422 } else { 423 forkstat.cntkthread++; 424 } 425 426 if (pr->ps_flags & PS_TRACED && flags & FORK_FORK) 427 newptstat = malloc(sizeof(*newptstat), M_SUBPROC, M_WAITOK); 428 429 p->p_tid = alloctid(); 430 431 LIST_INSERT_HEAD(&allproc, p, p_list); 432 LIST_INSERT_HEAD(TIDHASH(p->p_tid), p, p_hash); 433 LIST_INSERT_HEAD(PIDHASH(pr->ps_pid), pr, ps_hash); 434 LIST_INSERT_AFTER(curpr, pr, ps_pglist); 435 LIST_INSERT_HEAD(&curpr->ps_children, pr, ps_sibling); 436 437 if (pr->ps_flags & PS_TRACED) { 438 pr->ps_oppid = curpr->ps_pid; 439 process_reparent(pr, curpr->ps_pptr); 440 441 /* 442 * Set ptrace status. 443 */ 444 if (newptstat != NULL) { 445 pr->ps_ptstat = newptstat; 446 newptstat = NULL; 447 curpr->ps_ptstat->pe_report_event = PTRACE_FORK; 448 pr->ps_ptstat->pe_report_event = PTRACE_FORK; 449 curpr->ps_ptstat->pe_other_pid = pr->ps_pid; 450 pr->ps_ptstat->pe_other_pid = curpr->ps_pid; 451 } 452 } 453 454 /* 455 * For new processes, set accounting bits and mark as complete. 456 */ 457 nanouptime(&pr->ps_start); 458 pr->ps_acflag = AFORK; 459 atomic_clearbits_int(&pr->ps_flags, PS_EMBRYO); 460 461 if ((flags & FORK_IDLE) == 0) 462 fork_thread_start(p, curp, flags); 463 else 464 p->p_cpu = arg; 465 466 free(newptstat, M_SUBPROC, sizeof(*newptstat)); 467 468 /* 469 * Notify any interested parties about the new process. 470 */ 471 KNOTE(&curpr->ps_klist, NOTE_FORK | pr->ps_pid); 472 473 /* 474 * Update stats now that we know the fork was successful. 475 */ 476 uvmexp.forks++; 477 if (flags & FORK_PPWAIT) 478 uvmexp.forks_ppwait++; 479 if (flags & FORK_SHAREVM) 480 uvmexp.forks_sharevm++; 481 482 /* 483 * Pass a pointer to the new process to the caller. 484 */ 485 if (rnewprocp != NULL) 486 *rnewprocp = p; 487 488 /* 489 * Preserve synchronization semantics of vfork. If waiting for 490 * child to exec or exit, set PS_PPWAIT on child and PS_ISPWAIT 491 * on ourselves, and sleep on our process for the latter flag 492 * to go away. 493 * XXX Need to stop other rthreads in the parent 494 */ 495 if (flags & FORK_PPWAIT) 496 while (curpr->ps_flags & PS_ISPWAIT) 497 tsleep_nsec(curpr, PWAIT, "ppwait", INFSLP); 498 499 /* 500 * If we're tracing the child, alert the parent too. 501 */ 502 if ((flags & FORK_PTRACE) && (curpr->ps_flags & PS_TRACED)) 503 psignal(curp, SIGTRAP); 504 505 /* 506 * Return child pid to parent process 507 */ 508 if (retval != NULL) { 509 retval[0] = pr->ps_pid; 510 retval[1] = 0; 511 } 512 return (0); 513 } 514 515 int 516 thread_fork(struct proc *curp, void *stack, void *tcb, pid_t *tidptr, 517 register_t *retval) 518 { 519 struct process *pr = curp->p_p; 520 struct proc *p; 521 pid_t tid; 522 vaddr_t uaddr; 523 int s, error; 524 525 if (stack == NULL) 526 return EINVAL; 527 528 if ((error = fork_check_maxthread(curp->p_ucred->cr_ruid))) 529 return error; 530 531 uaddr = uvm_uarea_alloc(); 532 if (uaddr == 0) { 533 nthreads--; 534 return ENOMEM; 535 } 536 537 /* 538 * From now on, we're committed to the fork and cannot fail. 539 */ 540 p = thread_new(curp, uaddr); 541 atomic_setbits_int(&p->p_flag, P_THREAD); 542 sigstkinit(&p->p_sigstk); 543 544 /* other links */ 545 p->p_p = pr; 546 pr->ps_refcnt++; 547 548 /* local copies */ 549 p->p_fd = pr->ps_fd; 550 p->p_vmspace = pr->ps_vmspace; 551 552 /* 553 * Finish creating the child thread. cpu_fork() will copy 554 * and update the pcb and make the child ready to run. The 555 * child will exit directly to user mode via child_return() 556 * on its first time slice and will not return here. 557 */ 558 cpu_fork(curp, p, stack, tcb, child_return, p); 559 560 p->p_tid = alloctid(); 561 562 LIST_INSERT_HEAD(&allproc, p, p_list); 563 LIST_INSERT_HEAD(TIDHASH(p->p_tid), p, p_hash); 564 565 SCHED_LOCK(s); 566 TAILQ_INSERT_TAIL(&pr->ps_threads, p, p_thr_link); 567 /* 568 * if somebody else wants to take us to single threaded mode, 569 * count ourselves in. 570 */ 571 if (pr->ps_single) { 572 atomic_inc_int(&pr->ps_singlecount); 573 atomic_setbits_int(&p->p_flag, P_SUSPSINGLE); 574 } 575 SCHED_UNLOCK(s); 576 577 /* 578 * Return tid to parent thread and copy it out to userspace 579 */ 580 retval[0] = tid = p->p_tid + THREAD_PID_OFFSET; 581 retval[1] = 0; 582 if (tidptr != NULL) { 583 if (copyout(&tid, tidptr, sizeof(tid))) 584 psignal(curp, SIGSEGV); 585 } 586 587 fork_thread_start(p, curp, 0); 588 589 /* 590 * Update stats now that we know the fork was successful. 591 */ 592 forkstat.cnttfork++; 593 uvmexp.forks++; 594 uvmexp.forks_sharevm++; 595 596 return 0; 597 } 598 599 600 /* Find an unused tid */ 601 pid_t 602 alloctid(void) 603 { 604 pid_t tid; 605 606 do { 607 /* (0 .. TID_MASK+1] */ 608 tid = 1 + (arc4random() & TID_MASK); 609 } while (tfind(tid) != NULL); 610 611 return (tid); 612 } 613 614 /* 615 * Checks for current use of a pid, either as a pid or pgid. 616 */ 617 pid_t oldpids[128]; 618 int 619 ispidtaken(pid_t pid) 620 { 621 uint32_t i; 622 623 for (i = 0; i < nitems(oldpids); i++) 624 if (pid == oldpids[i]) 625 return (1); 626 627 if (prfind(pid) != NULL) 628 return (1); 629 if (pgfind(pid) != NULL) 630 return (1); 631 if (zombiefind(pid) != NULL) 632 return (1); 633 return (0); 634 } 635 636 /* Find an unused pid */ 637 pid_t 638 allocpid(void) 639 { 640 static pid_t lastpid; 641 pid_t pid; 642 643 if (!randompid) { 644 /* only used early on for system processes */ 645 pid = ++lastpid; 646 } else { 647 /* Find an unused pid satisfying lastpid < pid <= PID_MAX */ 648 do { 649 pid = arc4random_uniform(PID_MAX - lastpid) + 1 + 650 lastpid; 651 } while (ispidtaken(pid)); 652 } 653 654 return pid; 655 } 656 657 void 658 freepid(pid_t pid) 659 { 660 static uint32_t idx; 661 662 oldpids[idx++ % nitems(oldpids)] = pid; 663 } 664 665 #if defined(MULTIPROCESSOR) 666 /* 667 * XXX This is a slight hack to get newly-formed processes to 668 * XXX acquire the kernel lock as soon as they run. 669 */ 670 void 671 proc_trampoline_mp(void) 672 { 673 SCHED_ASSERT_LOCKED(); 674 __mp_unlock(&sched_lock); 675 spl0(); 676 SCHED_ASSERT_UNLOCKED(); 677 KERNEL_ASSERT_UNLOCKED(); 678 679 KERNEL_LOCK(); 680 } 681 #endif 682