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