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