1 /* $NetBSD: kern_exec.c,v 1.368 2013/12/24 14:47:04 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /*- 30 * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou 31 * Copyright (C) 1992 Wolfgang Solfrank. 32 * Copyright (C) 1992 TooLs GmbH. 33 * All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgement: 45 * This product includes software developed by TooLs GmbH. 46 * 4. The name of TooLs GmbH may not be used to endorse or promote products 47 * derived from this software without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 52 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61 #include <sys/cdefs.h> 62 __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.368 2013/12/24 14:47:04 christos Exp $"); 63 64 #include "opt_exec.h" 65 #include "opt_execfmt.h" 66 #include "opt_ktrace.h" 67 #include "opt_modular.h" 68 #include "opt_syscall_debug.h" 69 #include "veriexec.h" 70 #include "opt_pax.h" 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/filedesc.h> 75 #include <sys/kernel.h> 76 #include <sys/proc.h> 77 #include <sys/mount.h> 78 #include <sys/malloc.h> 79 #include <sys/kmem.h> 80 #include <sys/namei.h> 81 #include <sys/vnode.h> 82 #include <sys/file.h> 83 #include <sys/acct.h> 84 #include <sys/atomic.h> 85 #include <sys/exec.h> 86 #include <sys/ktrace.h> 87 #include <sys/uidinfo.h> 88 #include <sys/wait.h> 89 #include <sys/mman.h> 90 #include <sys/ras.h> 91 #include <sys/signalvar.h> 92 #include <sys/stat.h> 93 #include <sys/syscall.h> 94 #include <sys/kauth.h> 95 #include <sys/lwpctl.h> 96 #include <sys/pax.h> 97 #include <sys/cpu.h> 98 #include <sys/module.h> 99 #include <sys/syscallvar.h> 100 #include <sys/syscallargs.h> 101 #if NVERIEXEC > 0 102 #include <sys/verified_exec.h> 103 #endif /* NVERIEXEC > 0 */ 104 #include <sys/sdt.h> 105 #include <sys/spawn.h> 106 #include <sys/prot.h> 107 #include <sys/cprng.h> 108 109 #include <uvm/uvm_extern.h> 110 111 #include <machine/reg.h> 112 113 #include <compat/common/compat_util.h> 114 115 #ifndef MD_TOPDOWN_INIT 116 #ifdef __USING_TOPDOWN_VM 117 #define MD_TOPDOWN_INIT(epp) (epp)->ep_flags |= EXEC_TOPDOWN_VM 118 #else 119 #define MD_TOPDOWN_INIT(epp) 120 #endif 121 #endif 122 123 static int exec_sigcode_map(struct proc *, const struct emul *); 124 125 #ifdef DEBUG_EXEC 126 #define DPRINTF(a) printf a 127 #define COPYPRINTF(s, a, b) printf("%s, %d: copyout%s @%p %zu\n", __func__, \ 128 __LINE__, (s), (a), (b)) 129 #else 130 #define DPRINTF(a) 131 #define COPYPRINTF(s, a, b) 132 #endif /* DEBUG_EXEC */ 133 134 /* 135 * DTrace SDT provider definitions 136 */ 137 SDT_PROBE_DEFINE(proc,,,exec,exec, 138 "char *", NULL, 139 NULL, NULL, NULL, NULL, 140 NULL, NULL, NULL, NULL); 141 SDT_PROBE_DEFINE(proc,,,exec_success,exec-success, 142 "char *", NULL, 143 NULL, NULL, NULL, NULL, 144 NULL, NULL, NULL, NULL); 145 SDT_PROBE_DEFINE(proc,,,exec_failure,exec-failure, 146 "int", NULL, 147 NULL, NULL, NULL, NULL, 148 NULL, NULL, NULL, NULL); 149 150 /* 151 * Exec function switch: 152 * 153 * Note that each makecmds function is responsible for loading the 154 * exec package with the necessary functions for any exec-type-specific 155 * handling. 156 * 157 * Functions for specific exec types should be defined in their own 158 * header file. 159 */ 160 static const struct execsw **execsw = NULL; 161 static int nexecs; 162 163 u_int exec_maxhdrsz; /* must not be static - used by netbsd32 */ 164 165 /* list of dynamically loaded execsw entries */ 166 static LIST_HEAD(execlist_head, exec_entry) ex_head = 167 LIST_HEAD_INITIALIZER(ex_head); 168 struct exec_entry { 169 LIST_ENTRY(exec_entry) ex_list; 170 SLIST_ENTRY(exec_entry) ex_slist; 171 const struct execsw *ex_sw; 172 }; 173 174 #ifndef __HAVE_SYSCALL_INTERN 175 void syscall(void); 176 #endif 177 178 /* NetBSD emul struct */ 179 struct emul emul_netbsd = { 180 .e_name = "netbsd", 181 .e_path = NULL, 182 #ifndef __HAVE_MINIMAL_EMUL 183 .e_flags = EMUL_HAS_SYS___syscall, 184 .e_errno = NULL, 185 .e_nosys = SYS_syscall, 186 .e_nsysent = SYS_NSYSENT, 187 #endif 188 .e_sysent = sysent, 189 #ifdef SYSCALL_DEBUG 190 .e_syscallnames = syscallnames, 191 #else 192 .e_syscallnames = NULL, 193 #endif 194 .e_sendsig = sendsig, 195 .e_trapsignal = trapsignal, 196 .e_tracesig = NULL, 197 .e_sigcode = NULL, 198 .e_esigcode = NULL, 199 .e_sigobject = NULL, 200 .e_setregs = setregs, 201 .e_proc_exec = NULL, 202 .e_proc_fork = NULL, 203 .e_proc_exit = NULL, 204 .e_lwp_fork = NULL, 205 .e_lwp_exit = NULL, 206 #ifdef __HAVE_SYSCALL_INTERN 207 .e_syscall_intern = syscall_intern, 208 #else 209 .e_syscall = syscall, 210 #endif 211 .e_sysctlovly = NULL, 212 .e_fault = NULL, 213 .e_vm_default_addr = uvm_default_mapaddr, 214 .e_usertrap = NULL, 215 .e_ucsize = sizeof(ucontext_t), 216 .e_startlwp = startlwp 217 }; 218 219 /* 220 * Exec lock. Used to control access to execsw[] structures. 221 * This must not be static so that netbsd32 can access it, too. 222 */ 223 krwlock_t exec_lock; 224 225 static kmutex_t sigobject_lock; 226 227 /* 228 * Data used between a loadvm and execve part of an "exec" operation 229 */ 230 struct execve_data { 231 struct exec_package ed_pack; 232 struct pathbuf *ed_pathbuf; 233 struct vattr ed_attr; 234 struct ps_strings ed_arginfo; 235 char *ed_argp; 236 const char *ed_pathstring; 237 char *ed_resolvedpathbuf; 238 size_t ed_ps_strings_sz; 239 int ed_szsigcode; 240 long ed_argc; 241 long ed_envc; 242 }; 243 244 /* 245 * data passed from parent lwp to child during a posix_spawn() 246 */ 247 struct spawn_exec_data { 248 struct execve_data sed_exec; 249 struct posix_spawn_file_actions 250 *sed_actions; 251 struct posix_spawnattr *sed_attrs; 252 struct proc *sed_parent; 253 kcondvar_t sed_cv_child_ready; 254 kmutex_t sed_mtx_child; 255 int sed_error; 256 volatile uint32_t sed_refcnt; 257 }; 258 259 static void * 260 exec_pool_alloc(struct pool *pp, int flags) 261 { 262 263 return (void *)uvm_km_alloc(kernel_map, NCARGS, 0, 264 UVM_KMF_PAGEABLE | UVM_KMF_WAITVA); 265 } 266 267 static void 268 exec_pool_free(struct pool *pp, void *addr) 269 { 270 271 uvm_km_free(kernel_map, (vaddr_t)addr, NCARGS, UVM_KMF_PAGEABLE); 272 } 273 274 static struct pool exec_pool; 275 276 static struct pool_allocator exec_palloc = { 277 .pa_alloc = exec_pool_alloc, 278 .pa_free = exec_pool_free, 279 .pa_pagesz = NCARGS 280 }; 281 282 /* 283 * check exec: 284 * given an "executable" described in the exec package's namei info, 285 * see what we can do with it. 286 * 287 * ON ENTRY: 288 * exec package with appropriate namei info 289 * lwp pointer of exec'ing lwp 290 * NO SELF-LOCKED VNODES 291 * 292 * ON EXIT: 293 * error: nothing held, etc. exec header still allocated. 294 * ok: filled exec package, executable's vnode (unlocked). 295 * 296 * EXEC SWITCH ENTRY: 297 * Locked vnode to check, exec package, proc. 298 * 299 * EXEC SWITCH EXIT: 300 * ok: return 0, filled exec package, executable's vnode (unlocked). 301 * error: destructive: 302 * everything deallocated execept exec header. 303 * non-destructive: 304 * error code, executable's vnode (unlocked), 305 * exec header unmodified. 306 */ 307 int 308 /*ARGSUSED*/ 309 check_exec(struct lwp *l, struct exec_package *epp, struct pathbuf *pb) 310 { 311 int error, i; 312 struct vnode *vp; 313 struct nameidata nd; 314 size_t resid; 315 316 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb); 317 318 /* first get the vnode */ 319 if ((error = namei(&nd)) != 0) 320 return error; 321 epp->ep_vp = vp = nd.ni_vp; 322 /* normally this can't fail */ 323 if ((error = copystr(nd.ni_pnbuf, epp->ep_resolvedname, PATH_MAX, NULL))) 324 goto bad1; 325 326 #ifdef DIAGNOSTIC 327 /* paranoia (take this out once namei stuff stabilizes) */ 328 memset(nd.ni_pnbuf, '~', PATH_MAX); 329 #endif 330 331 /* check access and type */ 332 if (vp->v_type != VREG) { 333 error = EACCES; 334 goto bad1; 335 } 336 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0) 337 goto bad1; 338 339 /* get attributes */ 340 if ((error = VOP_GETATTR(vp, epp->ep_vap, l->l_cred)) != 0) 341 goto bad1; 342 343 /* Check mount point */ 344 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 345 error = EACCES; 346 goto bad1; 347 } 348 if (vp->v_mount->mnt_flag & MNT_NOSUID) 349 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 350 351 /* try to open it */ 352 if ((error = VOP_OPEN(vp, FREAD, l->l_cred)) != 0) 353 goto bad1; 354 355 /* unlock vp, since we need it unlocked from here on out. */ 356 VOP_UNLOCK(vp); 357 358 #if NVERIEXEC > 0 359 error = veriexec_verify(l, vp, epp->ep_resolvedname, 360 epp->ep_flags & EXEC_INDIR ? VERIEXEC_INDIRECT : VERIEXEC_DIRECT, 361 NULL); 362 if (error) 363 goto bad2; 364 #endif /* NVERIEXEC > 0 */ 365 366 #ifdef PAX_SEGVGUARD 367 error = pax_segvguard(l, vp, epp->ep_resolvedname, false); 368 if (error) 369 goto bad2; 370 #endif /* PAX_SEGVGUARD */ 371 372 /* now we have the file, get the exec header */ 373 error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0, 374 UIO_SYSSPACE, 0, l->l_cred, &resid, NULL); 375 if (error) 376 goto bad2; 377 epp->ep_hdrvalid = epp->ep_hdrlen - resid; 378 379 /* 380 * Set up default address space limits. Can be overridden 381 * by individual exec packages. 382 * 383 * XXX probably should be all done in the exec packages. 384 */ 385 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 386 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS; 387 /* 388 * set up the vmcmds for creation of the process 389 * address space 390 */ 391 error = ENOEXEC; 392 for (i = 0; i < nexecs; i++) { 393 int newerror; 394 395 epp->ep_esch = execsw[i]; 396 newerror = (*execsw[i]->es_makecmds)(l, epp); 397 398 if (!newerror) { 399 /* Seems ok: check that entry point is not too high */ 400 if (epp->ep_entry > epp->ep_vm_maxaddr) { 401 #ifdef DIAGNOSTIC 402 printf("%s: rejecting %p due to " 403 "too high entry address (> %p)\n", 404 __func__, (void *)epp->ep_entry, 405 (void *)epp->ep_vm_maxaddr); 406 #endif 407 error = ENOEXEC; 408 break; 409 } 410 /* Seems ok: check that entry point is not too low */ 411 if (epp->ep_entry < epp->ep_vm_minaddr) { 412 #ifdef DIAGNOSTIC 413 printf("%s: rejecting %p due to " 414 "too low entry address (< %p)\n", 415 __func__, (void *)epp->ep_entry, 416 (void *)epp->ep_vm_minaddr); 417 #endif 418 error = ENOEXEC; 419 break; 420 } 421 422 /* check limits */ 423 if ((epp->ep_tsize > MAXTSIZ) || 424 (epp->ep_dsize > (u_quad_t)l->l_proc->p_rlimit 425 [RLIMIT_DATA].rlim_cur)) { 426 #ifdef DIAGNOSTIC 427 printf("%s: rejecting due to " 428 "limits (t=%llu > %llu || d=%llu > %llu)\n", 429 __func__, 430 (unsigned long long)epp->ep_tsize, 431 (unsigned long long)MAXTSIZ, 432 (unsigned long long)epp->ep_dsize, 433 (unsigned long long) 434 l->l_proc->p_rlimit[RLIMIT_DATA].rlim_cur); 435 #endif 436 error = ENOMEM; 437 break; 438 } 439 return 0; 440 } 441 442 if (epp->ep_emul_root != NULL) { 443 vrele(epp->ep_emul_root); 444 epp->ep_emul_root = NULL; 445 } 446 if (epp->ep_interp != NULL) { 447 vrele(epp->ep_interp); 448 epp->ep_interp = NULL; 449 } 450 451 /* make sure the first "interesting" error code is saved. */ 452 if (error == ENOEXEC) 453 error = newerror; 454 455 if (epp->ep_flags & EXEC_DESTR) 456 /* Error from "#!" code, tidied up by recursive call */ 457 return error; 458 } 459 460 /* not found, error */ 461 462 /* 463 * free any vmspace-creation commands, 464 * and release their references 465 */ 466 kill_vmcmds(&epp->ep_vmcmds); 467 468 bad2: 469 /* 470 * close and release the vnode, restore the old one, free the 471 * pathname buf, and punt. 472 */ 473 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 474 VOP_CLOSE(vp, FREAD, l->l_cred); 475 vput(vp); 476 return error; 477 478 bad1: 479 /* 480 * free the namei pathname buffer, and put the vnode 481 * (which we don't yet have open). 482 */ 483 vput(vp); /* was still locked */ 484 return error; 485 } 486 487 #ifdef __MACHINE_STACK_GROWS_UP 488 #define STACK_PTHREADSPACE NBPG 489 #else 490 #define STACK_PTHREADSPACE 0 491 #endif 492 493 static int 494 execve_fetch_element(char * const *array, size_t index, char **value) 495 { 496 return copyin(array + index, value, sizeof(*value)); 497 } 498 499 /* 500 * exec system call 501 */ 502 int 503 sys_execve(struct lwp *l, const struct sys_execve_args *uap, register_t *retval) 504 { 505 /* { 506 syscallarg(const char *) path; 507 syscallarg(char * const *) argp; 508 syscallarg(char * const *) envp; 509 } */ 510 511 return execve1(l, SCARG(uap, path), SCARG(uap, argp), 512 SCARG(uap, envp), execve_fetch_element); 513 } 514 515 int 516 sys_fexecve(struct lwp *l, const struct sys_fexecve_args *uap, 517 register_t *retval) 518 { 519 /* { 520 syscallarg(int) fd; 521 syscallarg(char * const *) argp; 522 syscallarg(char * const *) envp; 523 } */ 524 525 return ENOSYS; 526 } 527 528 /* 529 * Load modules to try and execute an image that we do not understand. 530 * If no execsw entries are present, we load those likely to be needed 531 * in order to run native images only. Otherwise, we autoload all 532 * possible modules that could let us run the binary. XXX lame 533 */ 534 static void 535 exec_autoload(void) 536 { 537 #ifdef MODULAR 538 static const char * const native[] = { 539 "exec_elf32", 540 "exec_elf64", 541 "exec_script", 542 NULL 543 }; 544 static const char * const compat[] = { 545 "exec_elf32", 546 "exec_elf64", 547 "exec_script", 548 "exec_aout", 549 "exec_coff", 550 "exec_ecoff", 551 "compat_aoutm68k", 552 "compat_freebsd", 553 "compat_ibcs2", 554 "compat_linux", 555 "compat_linux32", 556 "compat_netbsd32", 557 "compat_sunos", 558 "compat_sunos32", 559 "compat_svr4", 560 "compat_svr4_32", 561 "compat_ultrix", 562 NULL 563 }; 564 char const * const *list; 565 int i; 566 567 list = (nexecs == 0 ? native : compat); 568 for (i = 0; list[i] != NULL; i++) { 569 if (module_autoload(list[i], MODULE_CLASS_EXEC) != 0) { 570 continue; 571 } 572 yield(); 573 } 574 #endif 575 } 576 577 static int 578 execve_loadvm(struct lwp *l, const char *path, char * const *args, 579 char * const *envs, execve_fetch_element_t fetch_element, 580 struct execve_data * restrict data) 581 { 582 int error; 583 struct proc *p; 584 char *dp, *sp; 585 size_t i, len; 586 struct exec_fakearg *tmpfap; 587 u_int modgen; 588 589 KASSERT(data != NULL); 590 591 p = l->l_proc; 592 modgen = 0; 593 594 SDT_PROBE(proc,,,exec, path, 0, 0, 0, 0); 595 596 /* 597 * Check if we have exceeded our number of processes limit. 598 * This is so that we handle the case where a root daemon 599 * forked, ran setuid to become the desired user and is trying 600 * to exec. The obvious place to do the reference counting check 601 * is setuid(), but we don't do the reference counting check there 602 * like other OS's do because then all the programs that use setuid() 603 * must be modified to check the return code of setuid() and exit(). 604 * It is dangerous to make setuid() fail, because it fails open and 605 * the program will continue to run as root. If we make it succeed 606 * and return an error code, again we are not enforcing the limit. 607 * The best place to enforce the limit is here, when the process tries 608 * to execute a new image, because eventually the process will need 609 * to call exec in order to do something useful. 610 */ 611 retry: 612 if (p->p_flag & PK_SUGID) { 613 if (kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT, 614 p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_BYPASS), 615 &p->p_rlimit[RLIMIT_NPROC], 616 KAUTH_ARG(RLIMIT_NPROC)) != 0 && 617 chgproccnt(kauth_cred_getuid(l->l_cred), 0) > 618 p->p_rlimit[RLIMIT_NPROC].rlim_cur) 619 return EAGAIN; 620 } 621 622 /* 623 * Drain existing references and forbid new ones. The process 624 * should be left alone until we're done here. This is necessary 625 * to avoid race conditions - e.g. in ptrace() - that might allow 626 * a local user to illicitly obtain elevated privileges. 627 */ 628 rw_enter(&p->p_reflock, RW_WRITER); 629 630 /* 631 * Init the namei data to point the file user's program name. 632 * This is done here rather than in check_exec(), so that it's 633 * possible to override this settings if any of makecmd/probe 634 * functions call check_exec() recursively - for example, 635 * see exec_script_makecmds(). 636 */ 637 error = pathbuf_copyin(path, &data->ed_pathbuf); 638 if (error) { 639 DPRINTF(("%s: pathbuf_copyin path @%p %d\n", __func__, 640 path, error)); 641 goto clrflg; 642 } 643 data->ed_pathstring = pathbuf_stringcopy_get(data->ed_pathbuf); 644 data->ed_resolvedpathbuf = PNBUF_GET(); 645 646 /* 647 * initialize the fields of the exec package. 648 */ 649 data->ed_pack.ep_name = path; 650 data->ed_pack.ep_kname = data->ed_pathstring; 651 data->ed_pack.ep_resolvedname = data->ed_resolvedpathbuf; 652 data->ed_pack.ep_hdr = kmem_alloc(exec_maxhdrsz, KM_SLEEP); 653 data->ed_pack.ep_hdrlen = exec_maxhdrsz; 654 data->ed_pack.ep_hdrvalid = 0; 655 data->ed_pack.ep_emul_arg = NULL; 656 data->ed_pack.ep_emul_arg_free = NULL; 657 data->ed_pack.ep_vmcmds.evs_cnt = 0; 658 data->ed_pack.ep_vmcmds.evs_used = 0; 659 data->ed_pack.ep_vap = &data->ed_attr; 660 data->ed_pack.ep_flags = 0; 661 MD_TOPDOWN_INIT(&data->ed_pack); 662 data->ed_pack.ep_emul_root = NULL; 663 data->ed_pack.ep_interp = NULL; 664 data->ed_pack.ep_esch = NULL; 665 data->ed_pack.ep_pax_flags = 0; 666 memset(data->ed_pack.ep_machine_arch, 0, 667 sizeof(data->ed_pack.ep_machine_arch)); 668 669 rw_enter(&exec_lock, RW_READER); 670 671 /* see if we can run it. */ 672 if ((error = check_exec(l, &data->ed_pack, data->ed_pathbuf)) != 0) { 673 if (error != ENOENT) { 674 DPRINTF(("%s: check exec failed %d\n", 675 __func__, error)); 676 } 677 goto freehdr; 678 } 679 680 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */ 681 682 /* allocate an argument buffer */ 683 data->ed_argp = pool_get(&exec_pool, PR_WAITOK); 684 KASSERT(data->ed_argp != NULL); 685 dp = data->ed_argp; 686 data->ed_argc = 0; 687 688 /* copy the fake args list, if there's one, freeing it as we go */ 689 if (data->ed_pack.ep_flags & EXEC_HASARGL) { 690 tmpfap = data->ed_pack.ep_fa; 691 while (tmpfap->fa_arg != NULL) { 692 const char *cp; 693 694 cp = tmpfap->fa_arg; 695 while (*cp) 696 *dp++ = *cp++; 697 *dp++ = '\0'; 698 ktrexecarg(tmpfap->fa_arg, cp - tmpfap->fa_arg); 699 700 kmem_free(tmpfap->fa_arg, tmpfap->fa_len); 701 tmpfap++; data->ed_argc++; 702 } 703 kmem_free(data->ed_pack.ep_fa, data->ed_pack.ep_fa_len); 704 data->ed_pack.ep_flags &= ~EXEC_HASARGL; 705 } 706 707 /* Now get argv & environment */ 708 if (args == NULL) { 709 DPRINTF(("%s: null args\n", __func__)); 710 error = EINVAL; 711 goto bad; 712 } 713 /* 'i' will index the argp/envp element to be retrieved */ 714 i = 0; 715 if (data->ed_pack.ep_flags & EXEC_SKIPARG) 716 i++; 717 718 while (1) { 719 len = data->ed_argp + ARG_MAX - dp; 720 if ((error = (*fetch_element)(args, i, &sp)) != 0) { 721 DPRINTF(("%s: fetch_element args %d\n", 722 __func__, error)); 723 goto bad; 724 } 725 if (!sp) 726 break; 727 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 728 DPRINTF(("%s: copyinstr args %d\n", __func__, error)); 729 if (error == ENAMETOOLONG) 730 error = E2BIG; 731 goto bad; 732 } 733 ktrexecarg(dp, len - 1); 734 dp += len; 735 i++; 736 data->ed_argc++; 737 } 738 739 data->ed_envc = 0; 740 /* environment need not be there */ 741 if (envs != NULL) { 742 i = 0; 743 while (1) { 744 len = data->ed_argp + ARG_MAX - dp; 745 if ((error = (*fetch_element)(envs, i, &sp)) != 0) { 746 DPRINTF(("%s: fetch_element env %d\n", 747 __func__, error)); 748 goto bad; 749 } 750 if (!sp) 751 break; 752 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 753 DPRINTF(("%s: copyinstr env %d\n", 754 __func__, error)); 755 if (error == ENAMETOOLONG) 756 error = E2BIG; 757 goto bad; 758 } 759 760 ktrexecenv(dp, len - 1); 761 dp += len; 762 i++; 763 data->ed_envc++; 764 } 765 } 766 767 dp = (char *) ALIGN(dp); 768 769 data->ed_szsigcode = data->ed_pack.ep_esch->es_emul->e_esigcode - 770 data->ed_pack.ep_esch->es_emul->e_sigcode; 771 772 #ifdef __MACHINE_STACK_GROWS_UP 773 /* See big comment lower down */ 774 #define RTLD_GAP 32 775 #else 776 #define RTLD_GAP 0 777 #endif 778 779 /* Now check if args & environ fit into new stack */ 780 if (data->ed_pack.ep_flags & EXEC_32) { 781 data->ed_ps_strings_sz = sizeof(struct ps_strings32); 782 len = ((data->ed_argc + data->ed_envc + 2 + 783 data->ed_pack.ep_esch->es_arglen) * 784 sizeof(int) + sizeof(int) + dp + RTLD_GAP + 785 data->ed_szsigcode + data->ed_ps_strings_sz + STACK_PTHREADSPACE) 786 - data->ed_argp; 787 } else { 788 data->ed_ps_strings_sz = sizeof(struct ps_strings); 789 len = ((data->ed_argc + data->ed_envc + 2 + 790 data->ed_pack.ep_esch->es_arglen) * 791 sizeof(char *) + sizeof(int) + dp + RTLD_GAP + 792 data->ed_szsigcode + data->ed_ps_strings_sz + STACK_PTHREADSPACE) 793 - data->ed_argp; 794 } 795 796 #ifdef PAX_ASLR 797 if (pax_aslr_active(l)) 798 len += (cprng_fast32() % PAGE_SIZE); 799 #endif /* PAX_ASLR */ 800 801 /* make the stack "safely" aligned */ 802 len = STACK_LEN_ALIGN(len, STACK_ALIGNBYTES); 803 804 if (len > data->ed_pack.ep_ssize) { 805 /* in effect, compare to initial limit */ 806 DPRINTF(("%s: stack limit exceeded %zu\n", __func__, len)); 807 goto bad; 808 } 809 /* adjust "active stack depth" for process VSZ */ 810 data->ed_pack.ep_ssize = len; 811 812 return 0; 813 814 bad: 815 /* free the vmspace-creation commands, and release their references */ 816 kill_vmcmds(&data->ed_pack.ep_vmcmds); 817 /* kill any opened file descriptor, if necessary */ 818 if (data->ed_pack.ep_flags & EXEC_HASFD) { 819 data->ed_pack.ep_flags &= ~EXEC_HASFD; 820 fd_close(data->ed_pack.ep_fd); 821 } 822 /* close and put the exec'd file */ 823 vn_lock(data->ed_pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 824 VOP_CLOSE(data->ed_pack.ep_vp, FREAD, l->l_cred); 825 vput(data->ed_pack.ep_vp); 826 pool_put(&exec_pool, data->ed_argp); 827 828 freehdr: 829 kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen); 830 if (data->ed_pack.ep_emul_root != NULL) 831 vrele(data->ed_pack.ep_emul_root); 832 if (data->ed_pack.ep_interp != NULL) 833 vrele(data->ed_pack.ep_interp); 834 835 rw_exit(&exec_lock); 836 837 pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring); 838 pathbuf_destroy(data->ed_pathbuf); 839 PNBUF_PUT(data->ed_resolvedpathbuf); 840 841 clrflg: 842 rw_exit(&p->p_reflock); 843 844 if (modgen != module_gen && error == ENOEXEC) { 845 modgen = module_gen; 846 exec_autoload(); 847 goto retry; 848 } 849 850 SDT_PROBE(proc,,,exec_failure, error, 0, 0, 0, 0); 851 return error; 852 } 853 854 static void 855 execve_free_data(struct execve_data *data) 856 { 857 858 /* free the vmspace-creation commands, and release their references */ 859 kill_vmcmds(&data->ed_pack.ep_vmcmds); 860 /* kill any opened file descriptor, if necessary */ 861 if (data->ed_pack.ep_flags & EXEC_HASFD) { 862 data->ed_pack.ep_flags &= ~EXEC_HASFD; 863 fd_close(data->ed_pack.ep_fd); 864 } 865 866 /* close and put the exec'd file */ 867 vn_lock(data->ed_pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 868 VOP_CLOSE(data->ed_pack.ep_vp, FREAD, curlwp->l_cred); 869 vput(data->ed_pack.ep_vp); 870 pool_put(&exec_pool, data->ed_argp); 871 872 kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen); 873 if (data->ed_pack.ep_emul_root != NULL) 874 vrele(data->ed_pack.ep_emul_root); 875 if (data->ed_pack.ep_interp != NULL) 876 vrele(data->ed_pack.ep_interp); 877 878 pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring); 879 pathbuf_destroy(data->ed_pathbuf); 880 PNBUF_PUT(data->ed_resolvedpathbuf); 881 } 882 883 static int 884 execve_runproc(struct lwp *l, struct execve_data * restrict data, 885 bool no_local_exec_lock, bool is_spawn) 886 { 887 int error = 0; 888 struct proc *p; 889 size_t i; 890 char *stack, *dp; 891 const char *commandname; 892 struct ps_strings32 arginfo32; 893 struct exec_vmcmd *base_vcp; 894 void *aip; 895 struct vmspace *vm; 896 ksiginfo_t ksi; 897 ksiginfoq_t kq; 898 899 /* 900 * In case of a posix_spawn operation, the child doing the exec 901 * might not hold the reader lock on exec_lock, but the parent 902 * will do this instead. 903 */ 904 KASSERT(no_local_exec_lock || rw_lock_held(&exec_lock)); 905 KASSERT(data != NULL); 906 if (data == NULL) 907 return (EINVAL); 908 909 p = l->l_proc; 910 if (no_local_exec_lock) 911 KASSERT(is_spawn); 912 913 base_vcp = NULL; 914 915 if (data->ed_pack.ep_flags & EXEC_32) 916 aip = &arginfo32; 917 else 918 aip = &data->ed_arginfo; 919 920 /* Get rid of other LWPs. */ 921 if (p->p_nlwps > 1) { 922 mutex_enter(p->p_lock); 923 exit_lwps(l); 924 mutex_exit(p->p_lock); 925 } 926 KDASSERT(p->p_nlwps == 1); 927 928 /* Destroy any lwpctl info. */ 929 if (p->p_lwpctl != NULL) 930 lwp_ctl_exit(); 931 932 /* Remove POSIX timers */ 933 timers_free(p, TIMERS_POSIX); 934 935 /* 936 * Do whatever is necessary to prepare the address space 937 * for remapping. Note that this might replace the current 938 * vmspace with another! 939 */ 940 if (is_spawn) 941 uvmspace_spawn(l, data->ed_pack.ep_vm_minaddr, 942 data->ed_pack.ep_vm_maxaddr, 943 data->ed_pack.ep_flags & EXEC_TOPDOWN_VM); 944 else 945 uvmspace_exec(l, data->ed_pack.ep_vm_minaddr, 946 data->ed_pack.ep_vm_maxaddr, 947 data->ed_pack.ep_flags & EXEC_TOPDOWN_VM); 948 949 /* record proc's vnode, for use by procfs and others */ 950 if (p->p_textvp) 951 vrele(p->p_textvp); 952 vref(data->ed_pack.ep_vp); 953 p->p_textvp = data->ed_pack.ep_vp; 954 955 /* Now map address space */ 956 vm = p->p_vmspace; 957 vm->vm_taddr = (void *)data->ed_pack.ep_taddr; 958 vm->vm_tsize = btoc(data->ed_pack.ep_tsize); 959 vm->vm_daddr = (void*)data->ed_pack.ep_daddr; 960 vm->vm_dsize = btoc(data->ed_pack.ep_dsize); 961 vm->vm_ssize = btoc(data->ed_pack.ep_ssize); 962 vm->vm_issize = 0; 963 vm->vm_maxsaddr = (void *)data->ed_pack.ep_maxsaddr; 964 vm->vm_minsaddr = (void *)data->ed_pack.ep_minsaddr; 965 966 #ifdef PAX_ASLR 967 pax_aslr_init(l, vm); 968 #endif /* PAX_ASLR */ 969 970 /* create the new process's VM space by running the vmcmds */ 971 #ifdef DIAGNOSTIC 972 if (data->ed_pack.ep_vmcmds.evs_used == 0) 973 panic("%s: no vmcmds", __func__); 974 #endif 975 976 #ifdef DEBUG_EXEC 977 { 978 size_t j; 979 struct exec_vmcmd *vp = &data->ed_pack.ep_vmcmds.evs_cmds[0]; 980 DPRINTF(("vmcmds %u\n", data->ed_pack.ep_vmcmds.evs_used)); 981 for (j = 0; j < data->ed_pack.ep_vmcmds.evs_used; j++) { 982 DPRINTF(("vmcmd[%zu] = vmcmd_map_%s %#" 983 PRIxVADDR"/%#"PRIxVSIZE" fd@%#" 984 PRIxVSIZE" prot=0%o flags=%d\n", j, 985 vp[j].ev_proc == vmcmd_map_pagedvn ? 986 "pagedvn" : 987 vp[j].ev_proc == vmcmd_map_readvn ? 988 "readvn" : 989 vp[j].ev_proc == vmcmd_map_zero ? 990 "zero" : "*unknown*", 991 vp[j].ev_addr, vp[j].ev_len, 992 vp[j].ev_offset, vp[j].ev_prot, 993 vp[j].ev_flags)); 994 } 995 } 996 #endif /* DEBUG_EXEC */ 997 998 for (i = 0; i < data->ed_pack.ep_vmcmds.evs_used && !error; i++) { 999 struct exec_vmcmd *vcp; 1000 1001 vcp = &data->ed_pack.ep_vmcmds.evs_cmds[i]; 1002 if (vcp->ev_flags & VMCMD_RELATIVE) { 1003 #ifdef DIAGNOSTIC 1004 if (base_vcp == NULL) 1005 panic("%s: relative vmcmd with no base", 1006 __func__); 1007 if (vcp->ev_flags & VMCMD_BASE) 1008 panic("%s: illegal base & relative vmcmd", 1009 __func__); 1010 #endif 1011 vcp->ev_addr += base_vcp->ev_addr; 1012 } 1013 error = (*vcp->ev_proc)(l, vcp); 1014 #ifdef DEBUG_EXEC 1015 if (error) { 1016 size_t j; 1017 struct exec_vmcmd *vp = 1018 &data->ed_pack.ep_vmcmds.evs_cmds[0]; 1019 DPRINTF(("vmcmds %zu/%u, error %d\n", i, 1020 data->ed_pack.ep_vmcmds.evs_used, error)); 1021 for (j = 0; j < data->ed_pack.ep_vmcmds.evs_used; j++) { 1022 DPRINTF(("vmcmd[%zu] = vmcmd_map_%s %#" 1023 PRIxVADDR"/%#"PRIxVSIZE" fd@%#" 1024 PRIxVSIZE" prot=0%o flags=%d\n", j, 1025 vp[j].ev_proc == vmcmd_map_pagedvn ? 1026 "pagedvn" : 1027 vp[j].ev_proc == vmcmd_map_readvn ? 1028 "readvn" : 1029 vp[j].ev_proc == vmcmd_map_zero ? 1030 "zero" : "*unknown*", 1031 vp[j].ev_addr, vp[j].ev_len, 1032 vp[j].ev_offset, vp[j].ev_prot, 1033 vp[j].ev_flags)); 1034 if (j == i) 1035 DPRINTF((" ^--- failed\n")); 1036 } 1037 } 1038 #endif /* DEBUG_EXEC */ 1039 if (vcp->ev_flags & VMCMD_BASE) 1040 base_vcp = vcp; 1041 } 1042 1043 /* free the vmspace-creation commands, and release their references */ 1044 kill_vmcmds(&data->ed_pack.ep_vmcmds); 1045 1046 vn_lock(data->ed_pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 1047 VOP_CLOSE(data->ed_pack.ep_vp, FREAD, l->l_cred); 1048 vput(data->ed_pack.ep_vp); 1049 1050 /* if an error happened, deallocate and punt */ 1051 if (error) { 1052 DPRINTF(("%s: vmcmd %zu failed: %d\n", __func__, i - 1, error)); 1053 goto exec_abort; 1054 } 1055 1056 /* remember information about the process */ 1057 data->ed_arginfo.ps_nargvstr = data->ed_argc; 1058 data->ed_arginfo.ps_nenvstr = data->ed_envc; 1059 1060 /* set command name & other accounting info */ 1061 commandname = strrchr(data->ed_pack.ep_resolvedname, '/'); 1062 if (commandname != NULL) { 1063 commandname++; 1064 } else { 1065 commandname = data->ed_pack.ep_resolvedname; 1066 } 1067 i = min(strlen(commandname), MAXCOMLEN); 1068 (void)memcpy(p->p_comm, commandname, i); 1069 p->p_comm[i] = '\0'; 1070 1071 dp = PNBUF_GET(); 1072 /* 1073 * If the path starts with /, we don't need to do any work. 1074 * This handles the majority of the cases. 1075 * In the future perhaps we could canonicalize it? 1076 */ 1077 if (data->ed_pathstring[0] == '/') 1078 (void)strlcpy(data->ed_pack.ep_path = dp, data->ed_pathstring, 1079 MAXPATHLEN); 1080 #ifdef notyet 1081 /* 1082 * Although this works most of the time [since the entry was just 1083 * entered in the cache] we don't use it because it will fail for 1084 * entries that are not placed in the cache because their name is 1085 * longer than NCHNAMLEN and it is not the cleanest interface, 1086 * because there could be races. When the namei cache is re-written, 1087 * this can be changed to use the appropriate function. 1088 */ 1089 else if (!(error = vnode_to_path(dp, MAXPATHLEN, p->p_textvp, l, p))) 1090 data->ed_pack.ep_path = dp; 1091 #endif 1092 else { 1093 #ifdef notyet 1094 printf("Cannot get path for pid %d [%s] (error %d)\n", 1095 (int)p->p_pid, p->p_comm, error); 1096 #endif 1097 data->ed_pack.ep_path = NULL; 1098 PNBUF_PUT(dp); 1099 } 1100 1101 stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr, 1102 STACK_PTHREADSPACE + data->ed_ps_strings_sz + data->ed_szsigcode), 1103 data->ed_pack.ep_ssize - (data->ed_ps_strings_sz + data->ed_szsigcode)); 1104 1105 #ifdef __MACHINE_STACK_GROWS_UP 1106 /* 1107 * The copyargs call always copies into lower addresses 1108 * first, moving towards higher addresses, starting with 1109 * the stack pointer that we give. When the stack grows 1110 * down, this puts argc/argv/envp very shallow on the 1111 * stack, right at the first user stack pointer. 1112 * When the stack grows up, the situation is reversed. 1113 * 1114 * Normally, this is no big deal. But the ld_elf.so _rtld() 1115 * function expects to be called with a single pointer to 1116 * a region that has a few words it can stash values into, 1117 * followed by argc/argv/envp. When the stack grows down, 1118 * it's easy to decrement the stack pointer a little bit to 1119 * allocate the space for these few words and pass the new 1120 * stack pointer to _rtld. When the stack grows up, however, 1121 * a few words before argc is part of the signal trampoline, XXX 1122 * so we have a problem. 1123 * 1124 * Instead of changing how _rtld works, we take the easy way 1125 * out and steal 32 bytes before we call copyargs. 1126 * This extra space was allowed for when 'pack.ep_ssize' was calculated. 1127 */ 1128 stack += RTLD_GAP; 1129 #endif /* __MACHINE_STACK_GROWS_UP */ 1130 1131 /* Now copy argc, args & environ to new stack */ 1132 error = (*data->ed_pack.ep_esch->es_copyargs)(l, &data->ed_pack, 1133 &data->ed_arginfo, &stack, data->ed_argp); 1134 1135 if (data->ed_pack.ep_path) { 1136 PNBUF_PUT(data->ed_pack.ep_path); 1137 data->ed_pack.ep_path = NULL; 1138 } 1139 if (error) { 1140 DPRINTF(("%s: copyargs failed %d\n", __func__, error)); 1141 goto exec_abort; 1142 } 1143 /* Move the stack back to original point */ 1144 stack = (char *)STACK_GROW(vm->vm_minsaddr, data->ed_pack.ep_ssize); 1145 1146 /* fill process ps_strings info */ 1147 p->p_psstrp = (vaddr_t)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr, 1148 STACK_PTHREADSPACE), data->ed_ps_strings_sz); 1149 1150 if (data->ed_pack.ep_flags & EXEC_32) { 1151 arginfo32.ps_argvstr = (vaddr_t)data->ed_arginfo.ps_argvstr; 1152 arginfo32.ps_nargvstr = data->ed_arginfo.ps_nargvstr; 1153 arginfo32.ps_envstr = (vaddr_t)data->ed_arginfo.ps_envstr; 1154 arginfo32.ps_nenvstr = data->ed_arginfo.ps_nenvstr; 1155 } 1156 1157 /* copy out the process's ps_strings structure */ 1158 if ((error = copyout(aip, (void *)p->p_psstrp, data->ed_ps_strings_sz)) 1159 != 0) { 1160 DPRINTF(("%s: ps_strings copyout %p->%p size %zu failed\n", 1161 __func__, aip, (void *)p->p_psstrp, data->ed_ps_strings_sz)); 1162 goto exec_abort; 1163 } 1164 1165 cwdexec(p); 1166 fd_closeexec(); /* handle close on exec */ 1167 1168 if (__predict_false(ktrace_on)) 1169 fd_ktrexecfd(); 1170 1171 execsigs(p); /* reset catched signals */ 1172 1173 l->l_ctxlink = NULL; /* reset ucontext link */ 1174 1175 1176 p->p_acflag &= ~AFORK; 1177 mutex_enter(p->p_lock); 1178 p->p_flag |= PK_EXEC; 1179 mutex_exit(p->p_lock); 1180 1181 /* 1182 * Stop profiling. 1183 */ 1184 if ((p->p_stflag & PST_PROFIL) != 0) { 1185 mutex_spin_enter(&p->p_stmutex); 1186 stopprofclock(p); 1187 mutex_spin_exit(&p->p_stmutex); 1188 } 1189 1190 /* 1191 * It's OK to test PL_PPWAIT unlocked here, as other LWPs have 1192 * exited and exec()/exit() are the only places it will be cleared. 1193 */ 1194 if ((p->p_lflag & PL_PPWAIT) != 0) { 1195 #if 0 1196 lwp_t *lp; 1197 1198 mutex_enter(proc_lock); 1199 lp = p->p_vforklwp; 1200 p->p_vforklwp = NULL; 1201 1202 l->l_lwpctl = NULL; /* was on loan from blocked parent */ 1203 p->p_lflag &= ~PL_PPWAIT; 1204 1205 lp->l_pflag &= ~LP_VFORKWAIT; /* XXX */ 1206 cv_broadcast(&lp->l_waitcv); 1207 mutex_exit(proc_lock); 1208 #else 1209 mutex_enter(proc_lock); 1210 l->l_lwpctl = NULL; /* was on loan from blocked parent */ 1211 p->p_lflag &= ~PL_PPWAIT; 1212 cv_broadcast(&p->p_pptr->p_waitcv); 1213 mutex_exit(proc_lock); 1214 #endif 1215 } 1216 1217 /* 1218 * Deal with set[ug]id. MNT_NOSUID has already been used to disable 1219 * s[ug]id. It's OK to check for PSL_TRACED here as we have blocked 1220 * out additional references on the process for the moment. 1221 */ 1222 if ((p->p_slflag & PSL_TRACED) == 0 && 1223 1224 (((data->ed_attr.va_mode & S_ISUID) != 0 && 1225 kauth_cred_geteuid(l->l_cred) != data->ed_attr.va_uid) || 1226 1227 ((data->ed_attr.va_mode & S_ISGID) != 0 && 1228 kauth_cred_getegid(l->l_cred) != data->ed_attr.va_gid))) { 1229 /* 1230 * Mark the process as SUGID before we do 1231 * anything that might block. 1232 */ 1233 proc_crmod_enter(); 1234 proc_crmod_leave(NULL, NULL, true); 1235 1236 /* Make sure file descriptors 0..2 are in use. */ 1237 if ((error = fd_checkstd()) != 0) { 1238 DPRINTF(("%s: fdcheckstd failed %d\n", 1239 __func__, error)); 1240 goto exec_abort; 1241 } 1242 1243 /* 1244 * Copy the credential so other references don't see our 1245 * changes. 1246 */ 1247 l->l_cred = kauth_cred_copy(l->l_cred); 1248 #ifdef KTRACE 1249 /* 1250 * If the persistent trace flag isn't set, turn off. 1251 */ 1252 if (p->p_tracep) { 1253 mutex_enter(&ktrace_lock); 1254 if (!(p->p_traceflag & KTRFAC_PERSISTENT)) 1255 ktrderef(p); 1256 mutex_exit(&ktrace_lock); 1257 } 1258 #endif 1259 if (data->ed_attr.va_mode & S_ISUID) 1260 kauth_cred_seteuid(l->l_cred, data->ed_attr.va_uid); 1261 if (data->ed_attr.va_mode & S_ISGID) 1262 kauth_cred_setegid(l->l_cred, data->ed_attr.va_gid); 1263 } else { 1264 if (kauth_cred_geteuid(l->l_cred) == 1265 kauth_cred_getuid(l->l_cred) && 1266 kauth_cred_getegid(l->l_cred) == 1267 kauth_cred_getgid(l->l_cred)) 1268 p->p_flag &= ~PK_SUGID; 1269 } 1270 1271 /* 1272 * Copy the credential so other references don't see our changes. 1273 * Test to see if this is necessary first, since in the common case 1274 * we won't need a private reference. 1275 */ 1276 if (kauth_cred_geteuid(l->l_cred) != kauth_cred_getsvuid(l->l_cred) || 1277 kauth_cred_getegid(l->l_cred) != kauth_cred_getsvgid(l->l_cred)) { 1278 l->l_cred = kauth_cred_copy(l->l_cred); 1279 kauth_cred_setsvuid(l->l_cred, kauth_cred_geteuid(l->l_cred)); 1280 kauth_cred_setsvgid(l->l_cred, kauth_cred_getegid(l->l_cred)); 1281 } 1282 1283 /* Update the master credentials. */ 1284 if (l->l_cred != p->p_cred) { 1285 kauth_cred_t ocred; 1286 1287 kauth_cred_hold(l->l_cred); 1288 mutex_enter(p->p_lock); 1289 ocred = p->p_cred; 1290 p->p_cred = l->l_cred; 1291 mutex_exit(p->p_lock); 1292 kauth_cred_free(ocred); 1293 } 1294 1295 #if defined(__HAVE_RAS) 1296 /* 1297 * Remove all RASs from the address space. 1298 */ 1299 ras_purgeall(); 1300 #endif 1301 1302 doexechooks(p); 1303 1304 /* setup new registers and do misc. setup. */ 1305 (*data->ed_pack.ep_esch->es_emul->e_setregs)(l, &data->ed_pack, 1306 (vaddr_t)stack); 1307 if (data->ed_pack.ep_esch->es_setregs) 1308 (*data->ed_pack.ep_esch->es_setregs)(l, &data->ed_pack, 1309 (vaddr_t)stack); 1310 1311 /* Provide a consistent LWP private setting */ 1312 (void)lwp_setprivate(l, NULL); 1313 1314 /* Discard all PCU state; need to start fresh */ 1315 pcu_discard_all(l); 1316 1317 /* map the process's signal trampoline code */ 1318 if ((error = exec_sigcode_map(p, data->ed_pack.ep_esch->es_emul)) != 0) { 1319 DPRINTF(("%s: map sigcode failed %d\n", __func__, error)); 1320 goto exec_abort; 1321 } 1322 1323 pool_put(&exec_pool, data->ed_argp); 1324 1325 /* notify others that we exec'd */ 1326 KNOTE(&p->p_klist, NOTE_EXEC); 1327 1328 kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen); 1329 1330 SDT_PROBE(proc,,,exec_success, data->ed_pack.ep_name, 0, 0, 0, 0); 1331 1332 /* The emulation root will usually have been found when we looked 1333 * for the elf interpreter (or similar), if not look now. */ 1334 if (data->ed_pack.ep_esch->es_emul->e_path != NULL && 1335 data->ed_pack.ep_emul_root == NULL) 1336 emul_find_root(l, &data->ed_pack); 1337 1338 /* Any old emulation root got removed by fdcloseexec */ 1339 rw_enter(&p->p_cwdi->cwdi_lock, RW_WRITER); 1340 p->p_cwdi->cwdi_edir = data->ed_pack.ep_emul_root; 1341 rw_exit(&p->p_cwdi->cwdi_lock); 1342 data->ed_pack.ep_emul_root = NULL; 1343 if (data->ed_pack.ep_interp != NULL) 1344 vrele(data->ed_pack.ep_interp); 1345 1346 /* 1347 * Call emulation specific exec hook. This can setup per-process 1348 * p->p_emuldata or do any other per-process stuff an emulation needs. 1349 * 1350 * If we are executing process of different emulation than the 1351 * original forked process, call e_proc_exit() of the old emulation 1352 * first, then e_proc_exec() of new emulation. If the emulation is 1353 * same, the exec hook code should deallocate any old emulation 1354 * resources held previously by this process. 1355 */ 1356 if (p->p_emul && p->p_emul->e_proc_exit 1357 && p->p_emul != data->ed_pack.ep_esch->es_emul) 1358 (*p->p_emul->e_proc_exit)(p); 1359 1360 /* 1361 * This is now LWP 1. 1362 */ 1363 mutex_enter(p->p_lock); 1364 p->p_nlwpid = 1; 1365 l->l_lid = 1; 1366 mutex_exit(p->p_lock); 1367 1368 /* 1369 * Call exec hook. Emulation code may NOT store reference to anything 1370 * from &pack. 1371 */ 1372 if (data->ed_pack.ep_esch->es_emul->e_proc_exec) 1373 (*data->ed_pack.ep_esch->es_emul->e_proc_exec)(p, &data->ed_pack); 1374 1375 /* update p_emul, the old value is no longer needed */ 1376 p->p_emul = data->ed_pack.ep_esch->es_emul; 1377 1378 /* ...and the same for p_execsw */ 1379 p->p_execsw = data->ed_pack.ep_esch; 1380 1381 #ifdef __HAVE_SYSCALL_INTERN 1382 (*p->p_emul->e_syscall_intern)(p); 1383 #endif 1384 ktremul(); 1385 1386 /* Allow new references from the debugger/procfs. */ 1387 rw_exit(&p->p_reflock); 1388 if (!no_local_exec_lock) 1389 rw_exit(&exec_lock); 1390 1391 mutex_enter(proc_lock); 1392 1393 if ((p->p_slflag & (PSL_TRACED|PSL_SYSCALL)) == PSL_TRACED) { 1394 KSI_INIT_EMPTY(&ksi); 1395 ksi.ksi_signo = SIGTRAP; 1396 ksi.ksi_lid = l->l_lid; 1397 kpsignal(p, &ksi, NULL); 1398 } 1399 1400 if (p->p_sflag & PS_STOPEXEC) { 1401 KERNEL_UNLOCK_ALL(l, &l->l_biglocks); 1402 p->p_pptr->p_nstopchild++; 1403 p->p_pptr->p_waited = 0; 1404 mutex_enter(p->p_lock); 1405 ksiginfo_queue_init(&kq); 1406 sigclearall(p, &contsigmask, &kq); 1407 lwp_lock(l); 1408 l->l_stat = LSSTOP; 1409 p->p_stat = SSTOP; 1410 p->p_nrlwps--; 1411 lwp_unlock(l); 1412 mutex_exit(p->p_lock); 1413 mutex_exit(proc_lock); 1414 lwp_lock(l); 1415 mi_switch(l); 1416 ksiginfo_queue_drain(&kq); 1417 KERNEL_LOCK(l->l_biglocks, l); 1418 } else { 1419 mutex_exit(proc_lock); 1420 } 1421 1422 pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring); 1423 pathbuf_destroy(data->ed_pathbuf); 1424 PNBUF_PUT(data->ed_resolvedpathbuf); 1425 DPRINTF(("%s finished\n", __func__)); 1426 return (EJUSTRETURN); 1427 1428 exec_abort: 1429 SDT_PROBE(proc,,,exec_failure, error, 0, 0, 0, 0); 1430 rw_exit(&p->p_reflock); 1431 if (!no_local_exec_lock) 1432 rw_exit(&exec_lock); 1433 1434 pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring); 1435 pathbuf_destroy(data->ed_pathbuf); 1436 PNBUF_PUT(data->ed_resolvedpathbuf); 1437 1438 /* 1439 * the old process doesn't exist anymore. exit gracefully. 1440 * get rid of the (new) address space we have created, if any, get rid 1441 * of our namei data and vnode, and exit noting failure 1442 */ 1443 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 1444 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 1445 1446 exec_free_emul_arg(&data->ed_pack); 1447 pool_put(&exec_pool, data->ed_argp); 1448 kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen); 1449 if (data->ed_pack.ep_emul_root != NULL) 1450 vrele(data->ed_pack.ep_emul_root); 1451 if (data->ed_pack.ep_interp != NULL) 1452 vrele(data->ed_pack.ep_interp); 1453 1454 /* Acquire the sched-state mutex (exit1() will release it). */ 1455 if (!is_spawn) { 1456 mutex_enter(p->p_lock); 1457 exit1(l, W_EXITCODE(error, SIGABRT)); 1458 } 1459 1460 return error; 1461 } 1462 1463 int 1464 execve1(struct lwp *l, const char *path, char * const *args, 1465 char * const *envs, execve_fetch_element_t fetch_element) 1466 { 1467 struct execve_data data; 1468 int error; 1469 1470 error = execve_loadvm(l, path, args, envs, fetch_element, &data); 1471 if (error) 1472 return error; 1473 error = execve_runproc(l, &data, false, false); 1474 return error; 1475 } 1476 1477 int 1478 copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo, 1479 char **stackp, void *argp) 1480 { 1481 char **cpp, *dp, *sp; 1482 size_t len; 1483 void *nullp; 1484 long argc, envc; 1485 int error; 1486 1487 cpp = (char **)*stackp; 1488 nullp = NULL; 1489 argc = arginfo->ps_nargvstr; 1490 envc = arginfo->ps_nenvstr; 1491 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0) { 1492 COPYPRINTF("", cpp - 1, sizeof(argc)); 1493 return error; 1494 } 1495 1496 dp = (char *) (cpp + argc + envc + 2 + pack->ep_esch->es_arglen); 1497 sp = argp; 1498 1499 /* XXX don't copy them out, remap them! */ 1500 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 1501 1502 for (; --argc >= 0; sp += len, dp += len) { 1503 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0) { 1504 COPYPRINTF("", cpp - 1, sizeof(dp)); 1505 return error; 1506 } 1507 if ((error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) { 1508 COPYPRINTF("str", dp, (size_t)ARG_MAX); 1509 return error; 1510 } 1511 } 1512 1513 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) { 1514 COPYPRINTF("", cpp - 1, sizeof(nullp)); 1515 return error; 1516 } 1517 1518 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 1519 1520 for (; --envc >= 0; sp += len, dp += len) { 1521 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0) { 1522 COPYPRINTF("", cpp - 1, sizeof(dp)); 1523 return error; 1524 } 1525 if ((error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) { 1526 COPYPRINTF("str", dp, (size_t)ARG_MAX); 1527 return error; 1528 } 1529 1530 } 1531 1532 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) { 1533 COPYPRINTF("", cpp - 1, sizeof(nullp)); 1534 return error; 1535 } 1536 1537 *stackp = (char *)cpp; 1538 return 0; 1539 } 1540 1541 1542 /* 1543 * Add execsw[] entries. 1544 */ 1545 int 1546 exec_add(struct execsw *esp, int count) 1547 { 1548 struct exec_entry *it; 1549 int i; 1550 1551 if (count == 0) { 1552 return 0; 1553 } 1554 1555 /* Check for duplicates. */ 1556 rw_enter(&exec_lock, RW_WRITER); 1557 for (i = 0; i < count; i++) { 1558 LIST_FOREACH(it, &ex_head, ex_list) { 1559 /* assume unique (makecmds, probe_func, emulation) */ 1560 if (it->ex_sw->es_makecmds == esp[i].es_makecmds && 1561 it->ex_sw->u.elf_probe_func == 1562 esp[i].u.elf_probe_func && 1563 it->ex_sw->es_emul == esp[i].es_emul) { 1564 rw_exit(&exec_lock); 1565 return EEXIST; 1566 } 1567 } 1568 } 1569 1570 /* Allocate new entries. */ 1571 for (i = 0; i < count; i++) { 1572 it = kmem_alloc(sizeof(*it), KM_SLEEP); 1573 it->ex_sw = &esp[i]; 1574 LIST_INSERT_HEAD(&ex_head, it, ex_list); 1575 } 1576 1577 /* update execsw[] */ 1578 exec_init(0); 1579 rw_exit(&exec_lock); 1580 return 0; 1581 } 1582 1583 /* 1584 * Remove execsw[] entry. 1585 */ 1586 int 1587 exec_remove(struct execsw *esp, int count) 1588 { 1589 struct exec_entry *it, *next; 1590 int i; 1591 const struct proclist_desc *pd; 1592 proc_t *p; 1593 1594 if (count == 0) { 1595 return 0; 1596 } 1597 1598 /* Abort if any are busy. */ 1599 rw_enter(&exec_lock, RW_WRITER); 1600 for (i = 0; i < count; i++) { 1601 mutex_enter(proc_lock); 1602 for (pd = proclists; pd->pd_list != NULL; pd++) { 1603 PROCLIST_FOREACH(p, pd->pd_list) { 1604 if (p->p_execsw == &esp[i]) { 1605 mutex_exit(proc_lock); 1606 rw_exit(&exec_lock); 1607 return EBUSY; 1608 } 1609 } 1610 } 1611 mutex_exit(proc_lock); 1612 } 1613 1614 /* None are busy, so remove them all. */ 1615 for (i = 0; i < count; i++) { 1616 for (it = LIST_FIRST(&ex_head); it != NULL; it = next) { 1617 next = LIST_NEXT(it, ex_list); 1618 if (it->ex_sw == &esp[i]) { 1619 LIST_REMOVE(it, ex_list); 1620 kmem_free(it, sizeof(*it)); 1621 break; 1622 } 1623 } 1624 } 1625 1626 /* update execsw[] */ 1627 exec_init(0); 1628 rw_exit(&exec_lock); 1629 return 0; 1630 } 1631 1632 /* 1633 * Initialize exec structures. If init_boot is true, also does necessary 1634 * one-time initialization (it's called from main() that way). 1635 * Once system is multiuser, this should be called with exec_lock held, 1636 * i.e. via exec_{add|remove}(). 1637 */ 1638 int 1639 exec_init(int init_boot) 1640 { 1641 const struct execsw **sw; 1642 struct exec_entry *ex; 1643 SLIST_HEAD(,exec_entry) first; 1644 SLIST_HEAD(,exec_entry) any; 1645 SLIST_HEAD(,exec_entry) last; 1646 int i, sz; 1647 1648 if (init_boot) { 1649 /* do one-time initializations */ 1650 rw_init(&exec_lock); 1651 mutex_init(&sigobject_lock, MUTEX_DEFAULT, IPL_NONE); 1652 pool_init(&exec_pool, NCARGS, 0, 0, PR_NOALIGN|PR_NOTOUCH, 1653 "execargs", &exec_palloc, IPL_NONE); 1654 pool_sethardlimit(&exec_pool, maxexec, "should not happen", 0); 1655 } else { 1656 KASSERT(rw_write_held(&exec_lock)); 1657 } 1658 1659 /* Sort each entry onto the appropriate queue. */ 1660 SLIST_INIT(&first); 1661 SLIST_INIT(&any); 1662 SLIST_INIT(&last); 1663 sz = 0; 1664 LIST_FOREACH(ex, &ex_head, ex_list) { 1665 switch(ex->ex_sw->es_prio) { 1666 case EXECSW_PRIO_FIRST: 1667 SLIST_INSERT_HEAD(&first, ex, ex_slist); 1668 break; 1669 case EXECSW_PRIO_ANY: 1670 SLIST_INSERT_HEAD(&any, ex, ex_slist); 1671 break; 1672 case EXECSW_PRIO_LAST: 1673 SLIST_INSERT_HEAD(&last, ex, ex_slist); 1674 break; 1675 default: 1676 panic("%s", __func__); 1677 break; 1678 } 1679 sz++; 1680 } 1681 1682 /* 1683 * Create new execsw[]. Ensure we do not try a zero-sized 1684 * allocation. 1685 */ 1686 sw = kmem_alloc(sz * sizeof(struct execsw *) + 1, KM_SLEEP); 1687 i = 0; 1688 SLIST_FOREACH(ex, &first, ex_slist) { 1689 sw[i++] = ex->ex_sw; 1690 } 1691 SLIST_FOREACH(ex, &any, ex_slist) { 1692 sw[i++] = ex->ex_sw; 1693 } 1694 SLIST_FOREACH(ex, &last, ex_slist) { 1695 sw[i++] = ex->ex_sw; 1696 } 1697 1698 /* Replace old execsw[] and free used memory. */ 1699 if (execsw != NULL) { 1700 kmem_free(__UNCONST(execsw), 1701 nexecs * sizeof(struct execsw *) + 1); 1702 } 1703 execsw = sw; 1704 nexecs = sz; 1705 1706 /* Figure out the maximum size of an exec header. */ 1707 exec_maxhdrsz = sizeof(int); 1708 for (i = 0; i < nexecs; i++) { 1709 if (execsw[i]->es_hdrsz > exec_maxhdrsz) 1710 exec_maxhdrsz = execsw[i]->es_hdrsz; 1711 } 1712 1713 return 0; 1714 } 1715 1716 static int 1717 exec_sigcode_map(struct proc *p, const struct emul *e) 1718 { 1719 vaddr_t va; 1720 vsize_t sz; 1721 int error; 1722 struct uvm_object *uobj; 1723 1724 sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode; 1725 1726 if (e->e_sigobject == NULL || sz == 0) { 1727 return 0; 1728 } 1729 1730 /* 1731 * If we don't have a sigobject for this emulation, create one. 1732 * 1733 * sigobject is an anonymous memory object (just like SYSV shared 1734 * memory) that we keep a permanent reference to and that we map 1735 * in all processes that need this sigcode. The creation is simple, 1736 * we create an object, add a permanent reference to it, map it in 1737 * kernel space, copy out the sigcode to it and unmap it. 1738 * We map it with PROT_READ|PROT_EXEC into the process just 1739 * the way sys_mmap() would map it. 1740 */ 1741 1742 uobj = *e->e_sigobject; 1743 if (uobj == NULL) { 1744 mutex_enter(&sigobject_lock); 1745 if ((uobj = *e->e_sigobject) == NULL) { 1746 uobj = uao_create(sz, 0); 1747 (*uobj->pgops->pgo_reference)(uobj); 1748 va = vm_map_min(kernel_map); 1749 if ((error = uvm_map(kernel_map, &va, round_page(sz), 1750 uobj, 0, 0, 1751 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, 1752 UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) { 1753 printf("kernel mapping failed %d\n", error); 1754 (*uobj->pgops->pgo_detach)(uobj); 1755 mutex_exit(&sigobject_lock); 1756 return (error); 1757 } 1758 memcpy((void *)va, e->e_sigcode, sz); 1759 #ifdef PMAP_NEED_PROCWR 1760 pmap_procwr(&proc0, va, sz); 1761 #endif 1762 uvm_unmap(kernel_map, va, va + round_page(sz)); 1763 *e->e_sigobject = uobj; 1764 } 1765 mutex_exit(&sigobject_lock); 1766 } 1767 1768 /* Just a hint to uvm_map where to put it. */ 1769 va = e->e_vm_default_addr(p, (vaddr_t)p->p_vmspace->vm_daddr, 1770 round_page(sz)); 1771 1772 #ifdef __alpha__ 1773 /* 1774 * Tru64 puts /sbin/loader at the end of user virtual memory, 1775 * which causes the above calculation to put the sigcode at 1776 * an invalid address. Put it just below the text instead. 1777 */ 1778 if (va == (vaddr_t)vm_map_max(&p->p_vmspace->vm_map)) { 1779 va = (vaddr_t)p->p_vmspace->vm_taddr - round_page(sz); 1780 } 1781 #endif 1782 1783 (*uobj->pgops->pgo_reference)(uobj); 1784 error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz), 1785 uobj, 0, 0, 1786 UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE, 1787 UVM_ADV_RANDOM, 0)); 1788 if (error) { 1789 DPRINTF(("%s, %d: map %p " 1790 "uvm_map %#"PRIxVSIZE"@%#"PRIxVADDR" failed %d\n", 1791 __func__, __LINE__, &p->p_vmspace->vm_map, round_page(sz), 1792 va, error)); 1793 (*uobj->pgops->pgo_detach)(uobj); 1794 return (error); 1795 } 1796 p->p_sigctx.ps_sigcode = (void *)va; 1797 return (0); 1798 } 1799 1800 /* 1801 * Release a refcount on spawn_exec_data and destroy memory, if this 1802 * was the last one. 1803 */ 1804 static void 1805 spawn_exec_data_release(struct spawn_exec_data *data) 1806 { 1807 if (atomic_dec_32_nv(&data->sed_refcnt) != 0) 1808 return; 1809 1810 cv_destroy(&data->sed_cv_child_ready); 1811 mutex_destroy(&data->sed_mtx_child); 1812 1813 if (data->sed_actions) 1814 posix_spawn_fa_free(data->sed_actions, 1815 data->sed_actions->len); 1816 if (data->sed_attrs) 1817 kmem_free(data->sed_attrs, 1818 sizeof(*data->sed_attrs)); 1819 kmem_free(data, sizeof(*data)); 1820 } 1821 1822 /* 1823 * A child lwp of a posix_spawn operation starts here and ends up in 1824 * cpu_spawn_return, dealing with all filedescriptor and scheduler 1825 * manipulations in between. 1826 * The parent waits for the child, as it is not clear wether the child 1827 * will be able to aquire its own exec_lock. If it can, the parent can 1828 * be released early and continue running in parallel. If not (or if the 1829 * magic debug flag is passed in the scheduler attribute struct), the 1830 * child rides on the parent's exec lock untill it is ready to return to 1831 * to userland - and only then releases the parent. This method loses 1832 * concurrency, but improves error reporting. 1833 */ 1834 static void 1835 spawn_return(void *arg) 1836 { 1837 struct spawn_exec_data *spawn_data = arg; 1838 struct lwp *l = curlwp; 1839 int error, newfd; 1840 size_t i; 1841 const struct posix_spawn_file_actions_entry *fae; 1842 pid_t ppid; 1843 register_t retval; 1844 bool have_reflock; 1845 bool parent_is_waiting = true; 1846 1847 /* 1848 * Check if we can release parent early. 1849 * We either need to have no sed_attrs, or sed_attrs does not 1850 * have POSIX_SPAWN_RETURNERROR or one of the flags, that require 1851 * safe access to the parent proc (passed in sed_parent). 1852 * We then try to get the exec_lock, and only if that works, we can 1853 * release the parent here already. 1854 */ 1855 ppid = spawn_data->sed_parent->p_pid; 1856 if ((!spawn_data->sed_attrs 1857 || (spawn_data->sed_attrs->sa_flags 1858 & (POSIX_SPAWN_RETURNERROR|POSIX_SPAWN_SETPGROUP)) == 0) 1859 && rw_tryenter(&exec_lock, RW_READER)) { 1860 parent_is_waiting = false; 1861 mutex_enter(&spawn_data->sed_mtx_child); 1862 cv_signal(&spawn_data->sed_cv_child_ready); 1863 mutex_exit(&spawn_data->sed_mtx_child); 1864 } 1865 1866 /* don't allow debugger access yet */ 1867 rw_enter(&l->l_proc->p_reflock, RW_WRITER); 1868 have_reflock = true; 1869 1870 error = 0; 1871 /* handle posix_spawn_file_actions */ 1872 if (spawn_data->sed_actions != NULL) { 1873 for (i = 0; i < spawn_data->sed_actions->len; i++) { 1874 fae = &spawn_data->sed_actions->fae[i]; 1875 switch (fae->fae_action) { 1876 case FAE_OPEN: 1877 if (fd_getfile(fae->fae_fildes) != NULL) { 1878 error = fd_close(fae->fae_fildes); 1879 if (error) 1880 break; 1881 } 1882 error = fd_open(fae->fae_path, fae->fae_oflag, 1883 fae->fae_mode, &newfd); 1884 if (error) 1885 break; 1886 if (newfd != fae->fae_fildes) { 1887 error = dodup(l, newfd, 1888 fae->fae_fildes, 0, &retval); 1889 if (fd_getfile(newfd) != NULL) 1890 fd_close(newfd); 1891 } 1892 break; 1893 case FAE_DUP2: 1894 error = dodup(l, fae->fae_fildes, 1895 fae->fae_newfildes, 0, &retval); 1896 break; 1897 case FAE_CLOSE: 1898 if (fd_getfile(fae->fae_fildes) == NULL) { 1899 error = EBADF; 1900 break; 1901 } 1902 error = fd_close(fae->fae_fildes); 1903 break; 1904 } 1905 if (error) 1906 goto report_error; 1907 } 1908 } 1909 1910 /* handle posix_spawnattr */ 1911 if (spawn_data->sed_attrs != NULL) { 1912 int ostat; 1913 struct sigaction sigact; 1914 sigact._sa_u._sa_handler = SIG_DFL; 1915 sigact.sa_flags = 0; 1916 1917 /* 1918 * set state to SSTOP so that this proc can be found by pid. 1919 * see proc_enterprp, do_sched_setparam below 1920 */ 1921 ostat = l->l_proc->p_stat; 1922 l->l_proc->p_stat = SSTOP; 1923 1924 /* Set process group */ 1925 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETPGROUP) { 1926 pid_t mypid = l->l_proc->p_pid, 1927 pgrp = spawn_data->sed_attrs->sa_pgroup; 1928 1929 if (pgrp == 0) 1930 pgrp = mypid; 1931 1932 error = proc_enterpgrp(spawn_data->sed_parent, 1933 mypid, pgrp, false); 1934 if (error) 1935 goto report_error; 1936 } 1937 1938 /* Set scheduler policy */ 1939 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSCHEDULER) 1940 error = do_sched_setparam(l->l_proc->p_pid, 0, 1941 spawn_data->sed_attrs->sa_schedpolicy, 1942 &spawn_data->sed_attrs->sa_schedparam); 1943 else if (spawn_data->sed_attrs->sa_flags 1944 & POSIX_SPAWN_SETSCHEDPARAM) { 1945 error = do_sched_setparam(ppid, 0, 1946 SCHED_NONE, &spawn_data->sed_attrs->sa_schedparam); 1947 } 1948 if (error) 1949 goto report_error; 1950 1951 /* Reset user ID's */ 1952 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_RESETIDS) { 1953 error = do_setresuid(l, -1, 1954 kauth_cred_getgid(l->l_cred), -1, 1955 ID_E_EQ_R | ID_E_EQ_S); 1956 if (error) 1957 goto report_error; 1958 error = do_setresuid(l, -1, 1959 kauth_cred_getuid(l->l_cred), -1, 1960 ID_E_EQ_R | ID_E_EQ_S); 1961 if (error) 1962 goto report_error; 1963 } 1964 1965 /* Set signal masks/defaults */ 1966 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSIGMASK) { 1967 mutex_enter(l->l_proc->p_lock); 1968 error = sigprocmask1(l, SIG_SETMASK, 1969 &spawn_data->sed_attrs->sa_sigmask, NULL); 1970 mutex_exit(l->l_proc->p_lock); 1971 if (error) 1972 goto report_error; 1973 } 1974 1975 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSIGDEF) { 1976 for (i = 1; i <= NSIG; i++) { 1977 if (sigismember( 1978 &spawn_data->sed_attrs->sa_sigdefault, i)) 1979 sigaction1(l, i, &sigact, NULL, NULL, 1980 0); 1981 } 1982 } 1983 l->l_proc->p_stat = ostat; 1984 } 1985 1986 /* now do the real exec */ 1987 error = execve_runproc(l, &spawn_data->sed_exec, parent_is_waiting, 1988 true); 1989 have_reflock = false; 1990 if (error == EJUSTRETURN) 1991 error = 0; 1992 else if (error) 1993 goto report_error; 1994 1995 if (parent_is_waiting) { 1996 mutex_enter(&spawn_data->sed_mtx_child); 1997 cv_signal(&spawn_data->sed_cv_child_ready); 1998 mutex_exit(&spawn_data->sed_mtx_child); 1999 } 2000 2001 /* release our refcount on the data */ 2002 spawn_exec_data_release(spawn_data); 2003 2004 /* and finaly: leave to userland for the first time */ 2005 cpu_spawn_return(l); 2006 2007 /* NOTREACHED */ 2008 return; 2009 2010 report_error: 2011 if (have_reflock) { 2012 /* 2013 * We have not passed through execve_runproc(), 2014 * which would have released the p_reflock and also 2015 * taken ownership of the sed_exec part of spawn_data, 2016 * so release/free both here. 2017 */ 2018 rw_exit(&l->l_proc->p_reflock); 2019 execve_free_data(&spawn_data->sed_exec); 2020 } 2021 2022 if (parent_is_waiting) { 2023 /* pass error to parent */ 2024 mutex_enter(&spawn_data->sed_mtx_child); 2025 spawn_data->sed_error = error; 2026 cv_signal(&spawn_data->sed_cv_child_ready); 2027 mutex_exit(&spawn_data->sed_mtx_child); 2028 } else { 2029 rw_exit(&exec_lock); 2030 } 2031 2032 /* release our refcount on the data */ 2033 spawn_exec_data_release(spawn_data); 2034 2035 /* done, exit */ 2036 mutex_enter(l->l_proc->p_lock); 2037 /* 2038 * Posix explicitly asks for an exit code of 127 if we report 2039 * errors from the child process - so, unfortunately, there 2040 * is no way to report a more exact error code. 2041 * A NetBSD specific workaround is POSIX_SPAWN_RETURNERROR as 2042 * flag bit in the attrp argument to posix_spawn(2), see above. 2043 */ 2044 exit1(l, W_EXITCODE(127, 0)); 2045 } 2046 2047 void 2048 posix_spawn_fa_free(struct posix_spawn_file_actions *fa, size_t len) 2049 { 2050 2051 for (size_t i = 0; i < len; i++) { 2052 struct posix_spawn_file_actions_entry *fae = &fa->fae[i]; 2053 if (fae->fae_action != FAE_OPEN) 2054 continue; 2055 kmem_free(fae->fae_path, strlen(fae->fae_path) + 1); 2056 } 2057 if (fa->len > 0) 2058 kmem_free(fa->fae, sizeof(*fa->fae) * fa->len); 2059 kmem_free(fa, sizeof(*fa)); 2060 } 2061 2062 static int 2063 posix_spawn_fa_alloc(struct posix_spawn_file_actions **fap, 2064 const struct posix_spawn_file_actions *ufa) 2065 { 2066 struct posix_spawn_file_actions *fa; 2067 struct posix_spawn_file_actions_entry *fae; 2068 char *pbuf = NULL; 2069 int error; 2070 size_t i = 0; 2071 2072 fa = kmem_alloc(sizeof(*fa), KM_SLEEP); 2073 error = copyin(ufa, fa, sizeof(*fa)); 2074 if (error) { 2075 fa->fae = NULL; 2076 fa->len = 0; 2077 goto out; 2078 } 2079 2080 if (fa->len == 0) { 2081 kmem_free(fa, sizeof(*fa)); 2082 return 0; 2083 } 2084 2085 fa->size = fa->len; 2086 size_t fal = fa->len * sizeof(*fae); 2087 fae = fa->fae; 2088 fa->fae = kmem_alloc(fal, KM_SLEEP); 2089 error = copyin(fae, fa->fae, fal); 2090 if (error) 2091 goto out; 2092 2093 pbuf = PNBUF_GET(); 2094 for (; i < fa->len; i++) { 2095 fae = &fa->fae[i]; 2096 if (fae->fae_action != FAE_OPEN) 2097 continue; 2098 error = copyinstr(fae->fae_path, pbuf, MAXPATHLEN, &fal); 2099 if (error) 2100 goto out; 2101 fae->fae_path = kmem_alloc(fal, KM_SLEEP); 2102 memcpy(fae->fae_path, pbuf, fal); 2103 } 2104 PNBUF_PUT(pbuf); 2105 2106 *fap = fa; 2107 return 0; 2108 out: 2109 if (pbuf) 2110 PNBUF_PUT(pbuf); 2111 posix_spawn_fa_free(fa, i); 2112 return error; 2113 } 2114 2115 int 2116 check_posix_spawn(struct lwp *l1) 2117 { 2118 int error, tnprocs, count; 2119 uid_t uid; 2120 struct proc *p1; 2121 2122 p1 = l1->l_proc; 2123 uid = kauth_cred_getuid(l1->l_cred); 2124 tnprocs = atomic_inc_uint_nv(&nprocs); 2125 2126 /* 2127 * Although process entries are dynamically created, we still keep 2128 * a global limit on the maximum number we will create. 2129 */ 2130 if (__predict_false(tnprocs >= maxproc)) 2131 error = -1; 2132 else 2133 error = kauth_authorize_process(l1->l_cred, 2134 KAUTH_PROCESS_FORK, p1, KAUTH_ARG(tnprocs), NULL, NULL); 2135 2136 if (error) { 2137 atomic_dec_uint(&nprocs); 2138 return EAGAIN; 2139 } 2140 2141 /* 2142 * Enforce limits. 2143 */ 2144 count = chgproccnt(uid, 1); 2145 if (kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_RLIMIT, 2146 p1, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_BYPASS), 2147 &p1->p_rlimit[RLIMIT_NPROC], KAUTH_ARG(RLIMIT_NPROC)) != 0 && 2148 __predict_false(count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)) { 2149 (void)chgproccnt(uid, -1); 2150 atomic_dec_uint(&nprocs); 2151 return EAGAIN; 2152 } 2153 2154 return 0; 2155 } 2156 2157 int 2158 do_posix_spawn(struct lwp *l1, pid_t *pid_res, bool *child_ok, const char *path, 2159 struct posix_spawn_file_actions *fa, 2160 struct posix_spawnattr *sa, 2161 char *const *argv, char *const *envp, 2162 execve_fetch_element_t fetch) 2163 { 2164 2165 struct proc *p1, *p2; 2166 struct lwp *l2; 2167 int error; 2168 struct spawn_exec_data *spawn_data; 2169 vaddr_t uaddr; 2170 pid_t pid; 2171 bool have_exec_lock = false; 2172 2173 p1 = l1->l_proc; 2174 2175 /* Allocate and init spawn_data */ 2176 spawn_data = kmem_zalloc(sizeof(*spawn_data), KM_SLEEP); 2177 spawn_data->sed_refcnt = 1; /* only parent so far */ 2178 cv_init(&spawn_data->sed_cv_child_ready, "pspawn"); 2179 mutex_init(&spawn_data->sed_mtx_child, MUTEX_DEFAULT, IPL_NONE); 2180 mutex_enter(&spawn_data->sed_mtx_child); 2181 2182 /* 2183 * Do the first part of the exec now, collect state 2184 * in spawn_data. 2185 */ 2186 error = execve_loadvm(l1, path, argv, 2187 envp, fetch, &spawn_data->sed_exec); 2188 if (error == EJUSTRETURN) 2189 error = 0; 2190 else if (error) 2191 goto error_exit; 2192 2193 have_exec_lock = true; 2194 2195 /* 2196 * Allocate virtual address space for the U-area now, while it 2197 * is still easy to abort the fork operation if we're out of 2198 * kernel virtual address space. 2199 */ 2200 uaddr = uvm_uarea_alloc(); 2201 if (__predict_false(uaddr == 0)) { 2202 error = ENOMEM; 2203 goto error_exit; 2204 } 2205 2206 /* 2207 * Allocate new proc. Borrow proc0 vmspace for it, we will 2208 * replace it with its own before returning to userland 2209 * in the child. 2210 * This is a point of no return, we will have to go through 2211 * the child proc to properly clean it up past this point. 2212 */ 2213 p2 = proc_alloc(); 2214 pid = p2->p_pid; 2215 2216 /* 2217 * Make a proc table entry for the new process. 2218 * Start by zeroing the section of proc that is zero-initialized, 2219 * then copy the section that is copied directly from the parent. 2220 */ 2221 memset(&p2->p_startzero, 0, 2222 (unsigned) ((char *)&p2->p_endzero - (char *)&p2->p_startzero)); 2223 memcpy(&p2->p_startcopy, &p1->p_startcopy, 2224 (unsigned) ((char *)&p2->p_endcopy - (char *)&p2->p_startcopy)); 2225 p2->p_vmspace = proc0.p_vmspace; 2226 2227 TAILQ_INIT(&p2->p_sigpend.sp_info); 2228 2229 LIST_INIT(&p2->p_lwps); 2230 LIST_INIT(&p2->p_sigwaiters); 2231 2232 /* 2233 * Duplicate sub-structures as needed. 2234 * Increase reference counts on shared objects. 2235 * Inherit flags we want to keep. The flags related to SIGCHLD 2236 * handling are important in order to keep a consistent behaviour 2237 * for the child after the fork. If we are a 32-bit process, the 2238 * child will be too. 2239 */ 2240 p2->p_flag = 2241 p1->p_flag & (PK_SUGID | PK_NOCLDWAIT | PK_CLDSIGIGN | PK_32); 2242 p2->p_emul = p1->p_emul; 2243 p2->p_execsw = p1->p_execsw; 2244 2245 mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH); 2246 mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE); 2247 rw_init(&p2->p_reflock); 2248 cv_init(&p2->p_waitcv, "wait"); 2249 cv_init(&p2->p_lwpcv, "lwpwait"); 2250 2251 p2->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 2252 2253 kauth_proc_fork(p1, p2); 2254 2255 p2->p_raslist = NULL; 2256 p2->p_fd = fd_copy(); 2257 2258 /* XXX racy */ 2259 p2->p_mqueue_cnt = p1->p_mqueue_cnt; 2260 2261 p2->p_cwdi = cwdinit(); 2262 2263 /* 2264 * Note: p_limit (rlimit stuff) is copy-on-write, so normally 2265 * we just need increase pl_refcnt. 2266 */ 2267 if (!p1->p_limit->pl_writeable) { 2268 lim_addref(p1->p_limit); 2269 p2->p_limit = p1->p_limit; 2270 } else { 2271 p2->p_limit = lim_copy(p1->p_limit); 2272 } 2273 2274 p2->p_lflag = 0; 2275 p2->p_sflag = 0; 2276 p2->p_slflag = 0; 2277 p2->p_pptr = p1; 2278 p2->p_ppid = p1->p_pid; 2279 LIST_INIT(&p2->p_children); 2280 2281 p2->p_aio = NULL; 2282 2283 #ifdef KTRACE 2284 /* 2285 * Copy traceflag and tracefile if enabled. 2286 * If not inherited, these were zeroed above. 2287 */ 2288 if (p1->p_traceflag & KTRFAC_INHERIT) { 2289 mutex_enter(&ktrace_lock); 2290 p2->p_traceflag = p1->p_traceflag; 2291 if ((p2->p_tracep = p1->p_tracep) != NULL) 2292 ktradref(p2); 2293 mutex_exit(&ktrace_lock); 2294 } 2295 #endif 2296 2297 /* 2298 * Create signal actions for the child process. 2299 */ 2300 p2->p_sigacts = sigactsinit(p1, 0); 2301 mutex_enter(p1->p_lock); 2302 p2->p_sflag |= 2303 (p1->p_sflag & (PS_STOPFORK | PS_STOPEXEC | PS_NOCLDSTOP)); 2304 sched_proc_fork(p1, p2); 2305 mutex_exit(p1->p_lock); 2306 2307 p2->p_stflag = p1->p_stflag; 2308 2309 /* 2310 * p_stats. 2311 * Copy parts of p_stats, and zero out the rest. 2312 */ 2313 p2->p_stats = pstatscopy(p1->p_stats); 2314 2315 /* copy over machdep flags to the new proc */ 2316 cpu_proc_fork(p1, p2); 2317 2318 /* 2319 * Prepare remaining parts of spawn data 2320 */ 2321 spawn_data->sed_actions = fa; 2322 spawn_data->sed_attrs = sa; 2323 2324 spawn_data->sed_parent = p1; 2325 2326 /* create LWP */ 2327 lwp_create(l1, p2, uaddr, 0, NULL, 0, spawn_return, spawn_data, 2328 &l2, l1->l_class); 2329 l2->l_ctxlink = NULL; /* reset ucontext link */ 2330 2331 /* 2332 * Copy the credential so other references don't see our changes. 2333 * Test to see if this is necessary first, since in the common case 2334 * we won't need a private reference. 2335 */ 2336 if (kauth_cred_geteuid(l2->l_cred) != kauth_cred_getsvuid(l2->l_cred) || 2337 kauth_cred_getegid(l2->l_cred) != kauth_cred_getsvgid(l2->l_cred)) { 2338 l2->l_cred = kauth_cred_copy(l2->l_cred); 2339 kauth_cred_setsvuid(l2->l_cred, kauth_cred_geteuid(l2->l_cred)); 2340 kauth_cred_setsvgid(l2->l_cred, kauth_cred_getegid(l2->l_cred)); 2341 } 2342 2343 /* Update the master credentials. */ 2344 if (l2->l_cred != p2->p_cred) { 2345 kauth_cred_t ocred; 2346 2347 kauth_cred_hold(l2->l_cred); 2348 mutex_enter(p2->p_lock); 2349 ocred = p2->p_cred; 2350 p2->p_cred = l2->l_cred; 2351 mutex_exit(p2->p_lock); 2352 kauth_cred_free(ocred); 2353 } 2354 2355 *child_ok = true; 2356 spawn_data->sed_refcnt = 2; /* child gets it as well */ 2357 #if 0 2358 l2->l_nopreempt = 1; /* start it non-preemptable */ 2359 #endif 2360 2361 /* 2362 * It's now safe for the scheduler and other processes to see the 2363 * child process. 2364 */ 2365 mutex_enter(proc_lock); 2366 2367 if (p1->p_session->s_ttyvp != NULL && p1->p_lflag & PL_CONTROLT) 2368 p2->p_lflag |= PL_CONTROLT; 2369 2370 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling); 2371 p2->p_exitsig = SIGCHLD; /* signal for parent on exit */ 2372 2373 LIST_INSERT_AFTER(p1, p2, p_pglist); 2374 LIST_INSERT_HEAD(&allproc, p2, p_list); 2375 2376 p2->p_trace_enabled = trace_is_enabled(p2); 2377 #ifdef __HAVE_SYSCALL_INTERN 2378 (*p2->p_emul->e_syscall_intern)(p2); 2379 #endif 2380 2381 /* 2382 * Make child runnable, set start time, and add to run queue except 2383 * if the parent requested the child to start in SSTOP state. 2384 */ 2385 mutex_enter(p2->p_lock); 2386 2387 getmicrotime(&p2->p_stats->p_start); 2388 2389 lwp_lock(l2); 2390 KASSERT(p2->p_nrlwps == 1); 2391 p2->p_nrlwps = 1; 2392 p2->p_stat = SACTIVE; 2393 l2->l_stat = LSRUN; 2394 sched_enqueue(l2, false); 2395 lwp_unlock(l2); 2396 2397 mutex_exit(p2->p_lock); 2398 mutex_exit(proc_lock); 2399 2400 cv_wait(&spawn_data->sed_cv_child_ready, &spawn_data->sed_mtx_child); 2401 error = spawn_data->sed_error; 2402 mutex_exit(&spawn_data->sed_mtx_child); 2403 spawn_exec_data_release(spawn_data); 2404 2405 rw_exit(&p1->p_reflock); 2406 rw_exit(&exec_lock); 2407 have_exec_lock = false; 2408 2409 *pid_res = pid; 2410 return error; 2411 2412 error_exit: 2413 if (have_exec_lock) { 2414 execve_free_data(&spawn_data->sed_exec); 2415 rw_exit(&p1->p_reflock); 2416 rw_exit(&exec_lock); 2417 } 2418 mutex_exit(&spawn_data->sed_mtx_child); 2419 spawn_exec_data_release(spawn_data); 2420 2421 return error; 2422 } 2423 2424 int 2425 sys_posix_spawn(struct lwp *l1, const struct sys_posix_spawn_args *uap, 2426 register_t *retval) 2427 { 2428 /* { 2429 syscallarg(pid_t *) pid; 2430 syscallarg(const char *) path; 2431 syscallarg(const struct posix_spawn_file_actions *) file_actions; 2432 syscallarg(const struct posix_spawnattr *) attrp; 2433 syscallarg(char *const *) argv; 2434 syscallarg(char *const *) envp; 2435 } */ 2436 2437 int error; 2438 struct posix_spawn_file_actions *fa = NULL; 2439 struct posix_spawnattr *sa = NULL; 2440 pid_t pid; 2441 bool child_ok = false; 2442 2443 error = check_posix_spawn(l1); 2444 if (error) { 2445 *retval = error; 2446 return 0; 2447 } 2448 2449 /* copy in file_actions struct */ 2450 if (SCARG(uap, file_actions) != NULL) { 2451 error = posix_spawn_fa_alloc(&fa, SCARG(uap, file_actions)); 2452 if (error) 2453 goto error_exit; 2454 } 2455 2456 /* copyin posix_spawnattr struct */ 2457 if (SCARG(uap, attrp) != NULL) { 2458 sa = kmem_alloc(sizeof(*sa), KM_SLEEP); 2459 error = copyin(SCARG(uap, attrp), sa, sizeof(*sa)); 2460 if (error) 2461 goto error_exit; 2462 } 2463 2464 /* 2465 * Do the spawn 2466 */ 2467 error = do_posix_spawn(l1, &pid, &child_ok, SCARG(uap, path), fa, sa, 2468 SCARG(uap, argv), SCARG(uap, envp), execve_fetch_element); 2469 if (error) 2470 goto error_exit; 2471 2472 if (error == 0 && SCARG(uap, pid) != NULL) 2473 error = copyout(&pid, SCARG(uap, pid), sizeof(pid)); 2474 2475 *retval = error; 2476 return 0; 2477 2478 error_exit: 2479 if (!child_ok) { 2480 (void)chgproccnt(kauth_cred_getuid(l1->l_cred), -1); 2481 atomic_dec_uint(&nprocs); 2482 2483 if (sa) 2484 kmem_free(sa, sizeof(*sa)); 2485 if (fa) 2486 posix_spawn_fa_free(fa, fa->len); 2487 } 2488 2489 *retval = error; 2490 return 0; 2491 } 2492 2493 void 2494 exec_free_emul_arg(struct exec_package *epp) 2495 { 2496 if (epp->ep_emul_arg_free != NULL) { 2497 KASSERT(epp->ep_emul_arg != NULL); 2498 (*epp->ep_emul_arg_free)(epp->ep_emul_arg); 2499 epp->ep_emul_arg_free = NULL; 2500 epp->ep_emul_arg = NULL; 2501 } else { 2502 KASSERT(epp->ep_emul_arg == NULL); 2503 } 2504 } 2505