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