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