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