1 /* $NetBSD: kern_exec.c,v 1.333 2011/12/04 15:12:07 dholland 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.333 2011/12/04 15:12:07 dholland Exp $"); 63 64 #include "opt_exec.h" 65 #include "opt_ktrace.h" 66 #include "opt_modular.h" 67 #include "opt_syscall_debug.h" 68 #include "veriexec.h" 69 #include "opt_pax.h" 70 #include "opt_sa.h" 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/filedesc.h> 75 #include <sys/kernel.h> 76 #include <sys/proc.h> 77 #include <sys/mount.h> 78 #include <sys/malloc.h> 79 #include <sys/kmem.h> 80 #include <sys/namei.h> 81 #include <sys/vnode.h> 82 #include <sys/file.h> 83 #include <sys/acct.h> 84 #include <sys/exec.h> 85 #include <sys/ktrace.h> 86 #include <sys/uidinfo.h> 87 #include <sys/wait.h> 88 #include <sys/mman.h> 89 #include <sys/ras.h> 90 #include <sys/signalvar.h> 91 #include <sys/stat.h> 92 #include <sys/syscall.h> 93 #include <sys/kauth.h> 94 #include <sys/lwpctl.h> 95 #include <sys/pax.h> 96 #include <sys/cpu.h> 97 #include <sys/module.h> 98 #include <sys/sa.h> 99 #include <sys/savar.h> 100 #include <sys/syscallvar.h> 101 #include <sys/syscallargs.h> 102 #if NVERIEXEC > 0 103 #include <sys/verified_exec.h> 104 #endif /* NVERIEXEC > 0 */ 105 #include <sys/sdt.h> 106 #include <sys/cprng.h> 107 108 #include <uvm/uvm_extern.h> 109 110 #include <machine/reg.h> 111 112 #include <compat/common/compat_util.h> 113 114 static int exec_sigcode_map(struct proc *, const struct emul *); 115 116 #ifdef DEBUG_EXEC 117 #define DPRINTF(a) printf a 118 #define COPYPRINTF(s, a, b) printf("%s, %d: copyout%s @%p %zu\n", __func__, \ 119 __LINE__, (s), (a), (b)) 120 #else 121 #define DPRINTF(a) 122 #define COPYPRINTF(s, a, b) 123 #endif /* DEBUG_EXEC */ 124 125 /* 126 * DTrace SDT provider definitions 127 */ 128 SDT_PROBE_DEFINE(proc,,,exec, 129 "char *", NULL, 130 NULL, NULL, NULL, NULL, 131 NULL, NULL, NULL, NULL); 132 SDT_PROBE_DEFINE(proc,,,exec_success, 133 "char *", NULL, 134 NULL, NULL, NULL, NULL, 135 NULL, NULL, NULL, NULL); 136 SDT_PROBE_DEFINE(proc,,,exec_failure, 137 "int", NULL, 138 NULL, NULL, NULL, NULL, 139 NULL, NULL, NULL, NULL); 140 141 /* 142 * Exec function switch: 143 * 144 * Note that each makecmds function is responsible for loading the 145 * exec package with the necessary functions for any exec-type-specific 146 * handling. 147 * 148 * Functions for specific exec types should be defined in their own 149 * header file. 150 */ 151 static const struct execsw **execsw = NULL; 152 static int nexecs; 153 154 u_int exec_maxhdrsz; /* must not be static - used by netbsd32 */ 155 156 /* list of dynamically loaded execsw entries */ 157 static LIST_HEAD(execlist_head, exec_entry) ex_head = 158 LIST_HEAD_INITIALIZER(ex_head); 159 struct exec_entry { 160 LIST_ENTRY(exec_entry) ex_list; 161 SLIST_ENTRY(exec_entry) ex_slist; 162 const struct execsw *ex_sw; 163 }; 164 165 #ifndef __HAVE_SYSCALL_INTERN 166 void syscall(void); 167 #endif 168 169 #ifdef KERN_SA 170 static struct sa_emul saemul_netbsd = { 171 sizeof(ucontext_t), 172 sizeof(struct sa_t), 173 sizeof(struct sa_t *), 174 NULL, 175 NULL, 176 cpu_upcall, 177 (void (*)(struct lwp *, void *))getucontext_sa, 178 sa_ucsp 179 }; 180 #endif /* KERN_SA */ 181 182 /* NetBSD emul struct */ 183 struct emul emul_netbsd = { 184 .e_name = "netbsd", 185 .e_path = NULL, 186 #ifndef __HAVE_MINIMAL_EMUL 187 .e_flags = EMUL_HAS_SYS___syscall, 188 .e_errno = NULL, 189 .e_nosys = SYS_syscall, 190 .e_nsysent = SYS_NSYSENT, 191 #endif 192 .e_sysent = sysent, 193 #ifdef SYSCALL_DEBUG 194 .e_syscallnames = syscallnames, 195 #else 196 .e_syscallnames = NULL, 197 #endif 198 .e_sendsig = sendsig, 199 .e_trapsignal = trapsignal, 200 .e_tracesig = NULL, 201 .e_sigcode = NULL, 202 .e_esigcode = NULL, 203 .e_sigobject = NULL, 204 .e_setregs = setregs, 205 .e_proc_exec = NULL, 206 .e_proc_fork = NULL, 207 .e_proc_exit = NULL, 208 .e_lwp_fork = NULL, 209 .e_lwp_exit = NULL, 210 #ifdef __HAVE_SYSCALL_INTERN 211 .e_syscall_intern = syscall_intern, 212 #else 213 .e_syscall = syscall, 214 #endif 215 .e_sysctlovly = NULL, 216 .e_fault = NULL, 217 .e_vm_default_addr = uvm_default_mapaddr, 218 .e_usertrap = NULL, 219 #ifdef KERN_SA 220 .e_sa = &saemul_netbsd, 221 #else 222 .e_sa = NULL, 223 #endif 224 .e_ucsize = sizeof(ucontext_t), 225 .e_startlwp = startlwp 226 }; 227 228 /* 229 * Exec lock. Used to control access to execsw[] structures. 230 * This must not be static so that netbsd32 can access it, too. 231 */ 232 krwlock_t exec_lock; 233 234 static kmutex_t sigobject_lock; 235 236 static void * 237 exec_pool_alloc(struct pool *pp, int flags) 238 { 239 240 return (void *)uvm_km_alloc(kernel_map, NCARGS, 0, 241 UVM_KMF_PAGEABLE | UVM_KMF_WAITVA); 242 } 243 244 static void 245 exec_pool_free(struct pool *pp, void *addr) 246 { 247 248 uvm_km_free(kernel_map, (vaddr_t)addr, NCARGS, UVM_KMF_PAGEABLE); 249 } 250 251 static struct pool exec_pool; 252 253 static struct pool_allocator exec_palloc = { 254 .pa_alloc = exec_pool_alloc, 255 .pa_free = exec_pool_free, 256 .pa_pagesz = NCARGS 257 }; 258 259 /* 260 * check exec: 261 * given an "executable" described in the exec package's namei info, 262 * see what we can do with it. 263 * 264 * ON ENTRY: 265 * exec package with appropriate namei info 266 * lwp pointer of exec'ing lwp 267 * NO SELF-LOCKED VNODES 268 * 269 * ON EXIT: 270 * error: nothing held, etc. exec header still allocated. 271 * ok: filled exec package, executable's vnode (unlocked). 272 * 273 * EXEC SWITCH ENTRY: 274 * Locked vnode to check, exec package, proc. 275 * 276 * EXEC SWITCH EXIT: 277 * ok: return 0, filled exec package, executable's vnode (unlocked). 278 * error: destructive: 279 * everything deallocated execept exec header. 280 * non-destructive: 281 * error code, executable's vnode (unlocked), 282 * exec header unmodified. 283 */ 284 int 285 /*ARGSUSED*/ 286 check_exec(struct lwp *l, struct exec_package *epp, struct pathbuf *pb) 287 { 288 int error, i; 289 struct vnode *vp; 290 struct nameidata nd; 291 size_t resid; 292 293 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb); 294 295 /* first get the vnode */ 296 if ((error = namei(&nd)) != 0) 297 return error; 298 epp->ep_vp = vp = nd.ni_vp; 299 /* this cannot overflow as both are size PATH_MAX */ 300 strcpy(epp->ep_resolvedname, nd.ni_pnbuf); 301 302 #ifdef DIAGNOSTIC 303 /* paranoia (take this out once namei stuff stabilizes) */ 304 memset(nd.ni_pnbuf, '~', PATH_MAX); 305 #endif 306 307 /* check access and type */ 308 if (vp->v_type != VREG) { 309 error = EACCES; 310 goto bad1; 311 } 312 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0) 313 goto bad1; 314 315 /* get attributes */ 316 if ((error = VOP_GETATTR(vp, epp->ep_vap, l->l_cred)) != 0) 317 goto bad1; 318 319 /* Check mount point */ 320 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 321 error = EACCES; 322 goto bad1; 323 } 324 if (vp->v_mount->mnt_flag & MNT_NOSUID) 325 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 326 327 /* try to open it */ 328 if ((error = VOP_OPEN(vp, FREAD, l->l_cred)) != 0) 329 goto bad1; 330 331 /* unlock vp, since we need it unlocked from here on out. */ 332 VOP_UNLOCK(vp); 333 334 #if NVERIEXEC > 0 335 error = veriexec_verify(l, vp, epp->ep_resolvedname, 336 epp->ep_flags & EXEC_INDIR ? VERIEXEC_INDIRECT : VERIEXEC_DIRECT, 337 NULL); 338 if (error) 339 goto bad2; 340 #endif /* NVERIEXEC > 0 */ 341 342 #ifdef PAX_SEGVGUARD 343 error = pax_segvguard(l, vp, epp->ep_resolvedname, false); 344 if (error) 345 goto bad2; 346 #endif /* PAX_SEGVGUARD */ 347 348 /* now we have the file, get the exec header */ 349 error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0, 350 UIO_SYSSPACE, 0, l->l_cred, &resid, NULL); 351 if (error) 352 goto bad2; 353 epp->ep_hdrvalid = epp->ep_hdrlen - resid; 354 355 /* 356 * Set up default address space limits. Can be overridden 357 * by individual exec packages. 358 * 359 * XXX probably should be all done in the exec packages. 360 */ 361 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 362 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS; 363 /* 364 * set up the vmcmds for creation of the process 365 * address space 366 */ 367 error = ENOEXEC; 368 for (i = 0; i < nexecs; i++) { 369 int newerror; 370 371 epp->ep_esch = execsw[i]; 372 newerror = (*execsw[i]->es_makecmds)(l, epp); 373 374 if (!newerror) { 375 /* Seems ok: check that entry point is not too high */ 376 if (epp->ep_entry > epp->ep_vm_maxaddr) { 377 #ifdef DIAGNOSTIC 378 printf("%s: rejecting %p due to " 379 "too high entry address (> %p)\n", 380 __func__, (void *)epp->ep_entry, 381 (void *)epp->ep_vm_maxaddr); 382 #endif 383 error = ENOEXEC; 384 break; 385 } 386 /* Seems ok: check that entry point is not too low */ 387 if (epp->ep_entry < epp->ep_vm_minaddr) { 388 #ifdef DIAGNOSTIC 389 printf("%s: rejecting %p due to " 390 "too low entry address (< %p)\n", 391 __func__, (void *)epp->ep_entry, 392 (void *)epp->ep_vm_minaddr); 393 #endif 394 error = ENOEXEC; 395 break; 396 } 397 398 /* check limits */ 399 if ((epp->ep_tsize > MAXTSIZ) || 400 (epp->ep_dsize > (u_quad_t)l->l_proc->p_rlimit 401 [RLIMIT_DATA].rlim_cur)) { 402 #ifdef DIAGNOSTIC 403 printf("%s: rejecting due to " 404 "limits (t=%llu > %llu || d=%llu > %llu)\n", 405 __func__, 406 (unsigned long long)epp->ep_tsize, 407 (unsigned long long)MAXTSIZ, 408 (unsigned long long)epp->ep_dsize, 409 (unsigned long long) 410 l->l_proc->p_rlimit[RLIMIT_DATA].rlim_cur); 411 #endif 412 error = ENOMEM; 413 break; 414 } 415 return 0; 416 } 417 418 if (epp->ep_emul_root != NULL) { 419 vrele(epp->ep_emul_root); 420 epp->ep_emul_root = NULL; 421 } 422 if (epp->ep_interp != NULL) { 423 vrele(epp->ep_interp); 424 epp->ep_interp = NULL; 425 } 426 427 /* make sure the first "interesting" error code is saved. */ 428 if (error == ENOEXEC) 429 error = newerror; 430 431 if (epp->ep_flags & EXEC_DESTR) 432 /* Error from "#!" code, tidied up by recursive call */ 433 return error; 434 } 435 436 /* not found, error */ 437 438 /* 439 * free any vmspace-creation commands, 440 * and release their references 441 */ 442 kill_vmcmds(&epp->ep_vmcmds); 443 444 bad2: 445 /* 446 * close and release the vnode, restore the old one, free the 447 * pathname buf, and punt. 448 */ 449 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 450 VOP_CLOSE(vp, FREAD, l->l_cred); 451 vput(vp); 452 return error; 453 454 bad1: 455 /* 456 * free the namei pathname buffer, and put the vnode 457 * (which we don't yet have open). 458 */ 459 vput(vp); /* was still locked */ 460 return error; 461 } 462 463 #ifdef __MACHINE_STACK_GROWS_UP 464 #define STACK_PTHREADSPACE NBPG 465 #else 466 #define STACK_PTHREADSPACE 0 467 #endif 468 469 static int 470 execve_fetch_element(char * const *array, size_t index, char **value) 471 { 472 return copyin(array + index, value, sizeof(*value)); 473 } 474 475 /* 476 * exec system call 477 */ 478 /* ARGSUSED */ 479 int 480 sys_execve(struct lwp *l, const struct sys_execve_args *uap, register_t *retval) 481 { 482 /* { 483 syscallarg(const char *) path; 484 syscallarg(char * const *) argp; 485 syscallarg(char * const *) envp; 486 } */ 487 488 return execve1(l, SCARG(uap, path), SCARG(uap, argp), 489 SCARG(uap, envp), execve_fetch_element); 490 } 491 492 int 493 sys_fexecve(struct lwp *l, const struct sys_fexecve_args *uap, 494 register_t *retval) 495 { 496 /* { 497 syscallarg(int) fd; 498 syscallarg(char * const *) argp; 499 syscallarg(char * const *) envp; 500 } */ 501 502 return ENOSYS; 503 } 504 505 /* 506 * Load modules to try and execute an image that we do not understand. 507 * If no execsw entries are present, we load those likely to be needed 508 * in order to run native images only. Otherwise, we autoload all 509 * possible modules that could let us run the binary. XXX lame 510 */ 511 static void 512 exec_autoload(void) 513 { 514 #ifdef MODULAR 515 static const char * const native[] = { 516 "exec_elf32", 517 "exec_elf64", 518 "exec_script", 519 NULL 520 }; 521 static const char * const compat[] = { 522 "exec_elf32", 523 "exec_elf64", 524 "exec_script", 525 "exec_aout", 526 "exec_coff", 527 "exec_ecoff", 528 "compat_aoutm68k", 529 "compat_freebsd", 530 "compat_ibcs2", 531 "compat_linux", 532 "compat_linux32", 533 "compat_netbsd32", 534 "compat_sunos", 535 "compat_sunos32", 536 "compat_svr4", 537 "compat_svr4_32", 538 "compat_ultrix", 539 NULL 540 }; 541 char const * const *list; 542 int i; 543 544 list = (nexecs == 0 ? native : compat); 545 for (i = 0; list[i] != NULL; i++) { 546 if (module_autoload(list[i], MODULE_CLASS_MISC) != 0) { 547 continue; 548 } 549 yield(); 550 } 551 #endif 552 } 553 554 int 555 execve1(struct lwp *l, const char *path, char * const *args, 556 char * const *envs, execve_fetch_element_t fetch_element) 557 { 558 int error; 559 struct exec_package pack; 560 struct pathbuf *pb; 561 struct vattr attr; 562 struct proc *p; 563 char *argp; 564 char *dp, *sp; 565 long argc, envc; 566 size_t i, len; 567 char *stack; 568 struct ps_strings arginfo; 569 struct ps_strings32 arginfo32; 570 void *aip; 571 struct vmspace *vm; 572 struct exec_fakearg *tmpfap; 573 int szsigcode; 574 struct exec_vmcmd *base_vcp; 575 int oldlwpflags; 576 ksiginfo_t ksi; 577 ksiginfoq_t kq; 578 const char *pathstring; 579 char *resolvedpathbuf; 580 const char *commandname; 581 u_int modgen; 582 size_t ps_strings_sz; 583 584 p = l->l_proc; 585 modgen = 0; 586 587 SDT_PROBE(proc,,,exec, path, 0, 0, 0, 0); 588 589 /* 590 * Check if we have exceeded our number of processes limit. 591 * This is so that we handle the case where a root daemon 592 * forked, ran setuid to become the desired user and is trying 593 * to exec. The obvious place to do the reference counting check 594 * is setuid(), but we don't do the reference counting check there 595 * like other OS's do because then all the programs that use setuid() 596 * must be modified to check the return code of setuid() and exit(). 597 * It is dangerous to make setuid() fail, because it fails open and 598 * the program will continue to run as root. If we make it succeed 599 * and return an error code, again we are not enforcing the limit. 600 * The best place to enforce the limit is here, when the process tries 601 * to execute a new image, because eventually the process will need 602 * to call exec in order to do something useful. 603 */ 604 retry: 605 if ((p->p_flag & PK_SUGID) && kauth_authorize_generic(l->l_cred, 606 KAUTH_GENERIC_ISSUSER, NULL) != 0 && chgproccnt(kauth_cred_getuid( 607 l->l_cred), 0) > p->p_rlimit[RLIMIT_NPROC].rlim_cur) 608 return EAGAIN; 609 610 oldlwpflags = l->l_flag & (LW_SA | LW_SA_UPCALL); 611 if (l->l_flag & LW_SA) { 612 lwp_lock(l); 613 l->l_flag &= ~(LW_SA | LW_SA_UPCALL); 614 lwp_unlock(l); 615 } 616 617 /* 618 * Drain existing references and forbid new ones. The process 619 * should be left alone until we're done here. This is necessary 620 * to avoid race conditions - e.g. in ptrace() - that might allow 621 * a local user to illicitly obtain elevated privileges. 622 */ 623 rw_enter(&p->p_reflock, RW_WRITER); 624 625 base_vcp = NULL; 626 /* 627 * Init the namei data to point the file user's program name. 628 * This is done here rather than in check_exec(), so that it's 629 * possible to override this settings if any of makecmd/probe 630 * functions call check_exec() recursively - for example, 631 * see exec_script_makecmds(). 632 */ 633 error = pathbuf_copyin(path, &pb); 634 if (error) { 635 DPRINTF(("%s: pathbuf_copyin path @%p %d\n", __func__, 636 path, error)); 637 goto clrflg; 638 } 639 pathstring = pathbuf_stringcopy_get(pb); 640 resolvedpathbuf = PNBUF_GET(); 641 #ifdef DIAGNOSTIC 642 strcpy(resolvedpathbuf, "/wrong"); 643 #endif 644 645 /* 646 * initialize the fields of the exec package. 647 */ 648 pack.ep_name = path; 649 pack.ep_kname = pathstring; 650 pack.ep_resolvedname = resolvedpathbuf; 651 pack.ep_hdr = kmem_alloc(exec_maxhdrsz, KM_SLEEP); 652 pack.ep_hdrlen = exec_maxhdrsz; 653 pack.ep_hdrvalid = 0; 654 pack.ep_emul_arg = NULL; 655 pack.ep_vmcmds.evs_cnt = 0; 656 pack.ep_vmcmds.evs_used = 0; 657 pack.ep_vap = &attr; 658 pack.ep_flags = 0; 659 pack.ep_emul_root = NULL; 660 pack.ep_interp = NULL; 661 pack.ep_esch = NULL; 662 pack.ep_pax_flags = 0; 663 664 rw_enter(&exec_lock, RW_READER); 665 666 /* see if we can run it. */ 667 if ((error = check_exec(l, &pack, pb)) != 0) { 668 if (error != ENOENT) { 669 DPRINTF(("%s: check exec failed %d\n", 670 __func__, error)); 671 } 672 goto freehdr; 673 } 674 675 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */ 676 677 /* allocate an argument buffer */ 678 argp = pool_get(&exec_pool, PR_WAITOK); 679 KASSERT(argp != NULL); 680 dp = argp; 681 argc = 0; 682 683 /* copy the fake args list, if there's one, freeing it as we go */ 684 if (pack.ep_flags & EXEC_HASARGL) { 685 tmpfap = pack.ep_fa; 686 while (tmpfap->fa_arg != NULL) { 687 const char *cp; 688 689 cp = tmpfap->fa_arg; 690 while (*cp) 691 *dp++ = *cp++; 692 *dp++ = '\0'; 693 ktrexecarg(tmpfap->fa_arg, cp - tmpfap->fa_arg); 694 695 kmem_free(tmpfap->fa_arg, tmpfap->fa_len); 696 tmpfap++; argc++; 697 } 698 kmem_free(pack.ep_fa, pack.ep_fa_len); 699 pack.ep_flags &= ~EXEC_HASARGL; 700 } 701 702 /* Now get argv & environment */ 703 if (args == NULL) { 704 DPRINTF(("%s: null args\n", __func__)); 705 error = EINVAL; 706 goto bad; 707 } 708 /* 'i' will index the argp/envp element to be retrieved */ 709 i = 0; 710 if (pack.ep_flags & EXEC_SKIPARG) 711 i++; 712 713 while (1) { 714 len = argp + ARG_MAX - dp; 715 if ((error = (*fetch_element)(args, i, &sp)) != 0) { 716 DPRINTF(("%s: fetch_element args %d\n", 717 __func__, error)); 718 goto bad; 719 } 720 if (!sp) 721 break; 722 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 723 DPRINTF(("%s: copyinstr args %d\n", __func__, error)); 724 if (error == ENAMETOOLONG) 725 error = E2BIG; 726 goto bad; 727 } 728 ktrexecarg(dp, len - 1); 729 dp += len; 730 i++; 731 argc++; 732 } 733 734 envc = 0; 735 /* environment need not be there */ 736 if (envs != NULL) { 737 i = 0; 738 while (1) { 739 len = argp + ARG_MAX - dp; 740 if ((error = (*fetch_element)(envs, i, &sp)) != 0) { 741 DPRINTF(("%s: fetch_element env %d\n", 742 __func__, error)); 743 goto bad; 744 } 745 if (!sp) 746 break; 747 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 748 DPRINTF(("%s: copyinstr env %d\n", 749 __func__, error)); 750 if (error == ENAMETOOLONG) 751 error = E2BIG; 752 goto bad; 753 } 754 ktrexecenv(dp, len - 1); 755 dp += len; 756 i++; 757 envc++; 758 } 759 } 760 761 dp = (char *) ALIGN(dp); 762 763 szsigcode = pack.ep_esch->es_emul->e_esigcode - 764 pack.ep_esch->es_emul->e_sigcode; 765 766 #ifdef __MACHINE_STACK_GROWS_UP 767 /* See big comment lower down */ 768 #define RTLD_GAP 32 769 #else 770 #define RTLD_GAP 0 771 #endif 772 773 /* Now check if args & environ fit into new stack */ 774 if (pack.ep_flags & EXEC_32) { 775 aip = &arginfo32; 776 ps_strings_sz = sizeof(struct ps_strings32); 777 len = ((argc + envc + 2 + pack.ep_esch->es_arglen) * 778 sizeof(int) + sizeof(int) + dp + RTLD_GAP + 779 szsigcode + ps_strings_sz + STACK_PTHREADSPACE) 780 - argp; 781 } else { 782 aip = &arginfo; 783 ps_strings_sz = sizeof(struct ps_strings); 784 len = ((argc + envc + 2 + pack.ep_esch->es_arglen) * 785 sizeof(char *) + sizeof(int) + dp + RTLD_GAP + 786 szsigcode + ps_strings_sz + STACK_PTHREADSPACE) 787 - argp; 788 } 789 790 #ifdef PAX_ASLR 791 if (pax_aslr_active(l)) 792 len += (cprng_fast32() % PAGE_SIZE); 793 #endif /* PAX_ASLR */ 794 795 #ifdef STACKALIGN /* arm, etc. */ 796 len = STACKALIGN(len); /* make the stack "safely" aligned */ 797 #else 798 len = ALIGN(len); /* make the stack "safely" aligned */ 799 #endif 800 801 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */ 802 DPRINTF(("%s: stack limit exceeded %zu\n", __func__, len)); 803 error = ENOMEM; 804 goto bad; 805 } 806 807 /* Get rid of other LWPs. */ 808 if (p->p_sa || p->p_nlwps > 1) { 809 mutex_enter(p->p_lock); 810 exit_lwps(l); 811 mutex_exit(p->p_lock); 812 } 813 KDASSERT(p->p_nlwps == 1); 814 815 /* Destroy any lwpctl info. */ 816 if (p->p_lwpctl != NULL) 817 lwp_ctl_exit(); 818 819 #ifdef KERN_SA 820 /* Release any SA state. */ 821 if (p->p_sa) 822 sa_release(p); 823 #endif /* KERN_SA */ 824 825 /* Remove POSIX timers */ 826 timers_free(p, TIMERS_POSIX); 827 828 /* adjust "active stack depth" for process VSZ */ 829 pack.ep_ssize = len; /* maybe should go elsewhere, but... */ 830 831 /* 832 * Do whatever is necessary to prepare the address space 833 * for remapping. Note that this might replace the current 834 * vmspace with another! 835 */ 836 uvmspace_exec(l, pack.ep_vm_minaddr, pack.ep_vm_maxaddr); 837 838 /* record proc's vnode, for use by procfs and others */ 839 if (p->p_textvp) 840 vrele(p->p_textvp); 841 vref(pack.ep_vp); 842 p->p_textvp = pack.ep_vp; 843 844 /* Now map address space */ 845 vm = p->p_vmspace; 846 vm->vm_taddr = (void *)pack.ep_taddr; 847 vm->vm_tsize = btoc(pack.ep_tsize); 848 vm->vm_daddr = (void*)pack.ep_daddr; 849 vm->vm_dsize = btoc(pack.ep_dsize); 850 vm->vm_ssize = btoc(pack.ep_ssize); 851 vm->vm_issize = 0; 852 vm->vm_maxsaddr = (void *)pack.ep_maxsaddr; 853 vm->vm_minsaddr = (void *)pack.ep_minsaddr; 854 855 #ifdef PAX_ASLR 856 pax_aslr_init(l, vm); 857 #endif /* PAX_ASLR */ 858 859 /* create the new process's VM space by running the vmcmds */ 860 #ifdef DIAGNOSTIC 861 if (pack.ep_vmcmds.evs_used == 0) 862 panic("%s: no vmcmds", __func__); 863 #endif 864 865 #ifdef DEBUG_EXEC 866 { 867 size_t j; 868 struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0]; 869 DPRINTF(("vmcmds %u\n", pack.ep_vmcmds.evs_used)); 870 for (j = 0; j < pack.ep_vmcmds.evs_used; j++) { 871 DPRINTF(("vmcmd[%zu] = vmcmd_map_%s %#" 872 PRIxVADDR"/%#"PRIxVSIZE" fd@%#" 873 PRIxVSIZE" prot=0%o flags=%d\n", j, 874 vp[j].ev_proc == vmcmd_map_pagedvn ? 875 "pagedvn" : 876 vp[j].ev_proc == vmcmd_map_readvn ? 877 "readvn" : 878 vp[j].ev_proc == vmcmd_map_zero ? 879 "zero" : "*unknown*", 880 vp[j].ev_addr, vp[j].ev_len, 881 vp[j].ev_offset, vp[j].ev_prot, 882 vp[j].ev_flags)); 883 } 884 } 885 #endif /* DEBUG_EXEC */ 886 887 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) { 888 struct exec_vmcmd *vcp; 889 890 vcp = &pack.ep_vmcmds.evs_cmds[i]; 891 if (vcp->ev_flags & VMCMD_RELATIVE) { 892 #ifdef DIAGNOSTIC 893 if (base_vcp == NULL) 894 panic("%s: relative vmcmd with no base", 895 __func__); 896 if (vcp->ev_flags & VMCMD_BASE) 897 panic("%s: illegal base & relative vmcmd", 898 __func__); 899 #endif 900 vcp->ev_addr += base_vcp->ev_addr; 901 } 902 error = (*vcp->ev_proc)(l, vcp); 903 #ifdef DEBUG_EXEC 904 if (error) { 905 size_t j; 906 struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0]; 907 DPRINTF(("vmcmds %zu/%u, error %d\n", i, 908 pack.ep_vmcmds.evs_used, error)); 909 for (j = 0; j < pack.ep_vmcmds.evs_used; j++) { 910 DPRINTF(("vmcmd[%zu] = vmcmd_map_%s %#" 911 PRIxVADDR"/%#"PRIxVSIZE" fd@%#" 912 PRIxVSIZE" prot=0%o flags=%d\n", j, 913 vp[j].ev_proc == vmcmd_map_pagedvn ? 914 "pagedvn" : 915 vp[j].ev_proc == vmcmd_map_readvn ? 916 "readvn" : 917 vp[j].ev_proc == vmcmd_map_zero ? 918 "zero" : "*unknown*", 919 vp[j].ev_addr, vp[j].ev_len, 920 vp[j].ev_offset, vp[j].ev_prot, 921 vp[j].ev_flags)); 922 if (j == i) 923 DPRINTF((" ^--- failed\n")); 924 } 925 } 926 #endif /* DEBUG_EXEC */ 927 if (vcp->ev_flags & VMCMD_BASE) 928 base_vcp = vcp; 929 } 930 931 /* free the vmspace-creation commands, and release their references */ 932 kill_vmcmds(&pack.ep_vmcmds); 933 934 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 935 VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred); 936 vput(pack.ep_vp); 937 938 /* if an error happened, deallocate and punt */ 939 if (error) { 940 DPRINTF(("%s: vmcmd %zu failed: %d\n", __func__, i - 1, error)); 941 goto exec_abort; 942 } 943 944 /* remember information about the process */ 945 arginfo.ps_nargvstr = argc; 946 arginfo.ps_nenvstr = envc; 947 948 /* set command name & other accounting info */ 949 commandname = strrchr(pack.ep_resolvedname, '/'); 950 if (commandname != NULL) { 951 commandname++; 952 } else { 953 commandname = pack.ep_resolvedname; 954 } 955 i = min(strlen(commandname), MAXCOMLEN); 956 (void)memcpy(p->p_comm, commandname, i); 957 p->p_comm[i] = '\0'; 958 959 dp = PNBUF_GET(); 960 /* 961 * If the path starts with /, we don't need to do any work. 962 * This handles the majority of the cases. 963 * In the future perhaps we could canonicalize it? 964 */ 965 if (pathstring[0] == '/') 966 (void)strlcpy(pack.ep_path = dp, pathstring, MAXPATHLEN); 967 #ifdef notyet 968 /* 969 * Although this works most of the time [since the entry was just 970 * entered in the cache] we don't use it because it theoretically 971 * can fail and it is not the cleanest interface, because there 972 * could be races. When the namei cache is re-written, this can 973 * be changed to use the appropriate function. 974 */ 975 else if (!(error = vnode_to_path(dp, MAXPATHLEN, p->p_textvp, l, p))) 976 pack.ep_path = dp; 977 #endif 978 else { 979 #ifdef notyet 980 printf("Cannot get path for pid %d [%s] (error %d)", 981 (int)p->p_pid, p->p_comm, error); 982 #endif 983 pack.ep_path = NULL; 984 PNBUF_PUT(dp); 985 } 986 987 stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr, 988 STACK_PTHREADSPACE + ps_strings_sz + szsigcode), 989 len - (ps_strings_sz + szsigcode)); 990 991 #ifdef __MACHINE_STACK_GROWS_UP 992 /* 993 * The copyargs call always copies into lower addresses 994 * first, moving towards higher addresses, starting with 995 * the stack pointer that we give. When the stack grows 996 * down, this puts argc/argv/envp very shallow on the 997 * stack, right at the first user stack pointer. 998 * When the stack grows up, the situation is reversed. 999 * 1000 * Normally, this is no big deal. But the ld_elf.so _rtld() 1001 * function expects to be called with a single pointer to 1002 * a region that has a few words it can stash values into, 1003 * followed by argc/argv/envp. When the stack grows down, 1004 * it's easy to decrement the stack pointer a little bit to 1005 * allocate the space for these few words and pass the new 1006 * stack pointer to _rtld. When the stack grows up, however, 1007 * a few words before argc is part of the signal trampoline, XXX 1008 * so we have a problem. 1009 * 1010 * Instead of changing how _rtld works, we take the easy way 1011 * out and steal 32 bytes before we call copyargs. 1012 * This extra space was allowed for when 'len' was calculated. 1013 */ 1014 stack += RTLD_GAP; 1015 #endif /* __MACHINE_STACK_GROWS_UP */ 1016 1017 /* Now copy argc, args & environ to new stack */ 1018 error = (*pack.ep_esch->es_copyargs)(l, &pack, &arginfo, &stack, argp); 1019 if (pack.ep_path) { 1020 PNBUF_PUT(pack.ep_path); 1021 pack.ep_path = NULL; 1022 } 1023 if (error) { 1024 DPRINTF(("%s: copyargs failed %d\n", __func__, error)); 1025 goto exec_abort; 1026 } 1027 /* Move the stack back to original point */ 1028 stack = (char *)STACK_GROW(vm->vm_minsaddr, len); 1029 1030 /* fill process ps_strings info */ 1031 p->p_psstrp = (vaddr_t)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr, 1032 STACK_PTHREADSPACE), ps_strings_sz); 1033 1034 if (pack.ep_flags & EXEC_32) { 1035 arginfo32.ps_argvstr = (vaddr_t)arginfo.ps_argvstr; 1036 arginfo32.ps_nargvstr = arginfo.ps_nargvstr; 1037 arginfo32.ps_envstr = (vaddr_t)arginfo.ps_envstr; 1038 arginfo32.ps_nenvstr = arginfo.ps_nenvstr; 1039 } 1040 1041 /* copy out the process's ps_strings structure */ 1042 if ((error = copyout(aip, (void *)p->p_psstrp, ps_strings_sz)) != 0) { 1043 DPRINTF(("%s: ps_strings copyout %p->%p size %zu failed\n", 1044 __func__, aip, (void *)p->p_psstrp, ps_strings_sz)); 1045 goto exec_abort; 1046 } 1047 1048 cwdexec(p); 1049 fd_closeexec(); /* handle close on exec */ 1050 1051 if (__predict_false(ktrace_on)) 1052 fd_ktrexecfd(); 1053 1054 execsigs(p); /* reset catched signals */ 1055 1056 l->l_ctxlink = NULL; /* reset ucontext link */ 1057 1058 1059 p->p_acflag &= ~AFORK; 1060 mutex_enter(p->p_lock); 1061 p->p_flag |= PK_EXEC; 1062 mutex_exit(p->p_lock); 1063 1064 /* 1065 * Stop profiling. 1066 */ 1067 if ((p->p_stflag & PST_PROFIL) != 0) { 1068 mutex_spin_enter(&p->p_stmutex); 1069 stopprofclock(p); 1070 mutex_spin_exit(&p->p_stmutex); 1071 } 1072 1073 /* 1074 * It's OK to test PL_PPWAIT unlocked here, as other LWPs have 1075 * exited and exec()/exit() are the only places it will be cleared. 1076 */ 1077 if ((p->p_lflag & PL_PPWAIT) != 0) { 1078 mutex_enter(proc_lock); 1079 l->l_lwpctl = NULL; /* was on loan from blocked parent */ 1080 p->p_lflag &= ~PL_PPWAIT; 1081 cv_broadcast(&p->p_pptr->p_waitcv); 1082 mutex_exit(proc_lock); 1083 } 1084 1085 /* 1086 * Deal with set[ug]id. MNT_NOSUID has already been used to disable 1087 * s[ug]id. It's OK to check for PSL_TRACED here as we have blocked 1088 * out additional references on the process for the moment. 1089 */ 1090 if ((p->p_slflag & PSL_TRACED) == 0 && 1091 1092 (((attr.va_mode & S_ISUID) != 0 && 1093 kauth_cred_geteuid(l->l_cred) != attr.va_uid) || 1094 1095 ((attr.va_mode & S_ISGID) != 0 && 1096 kauth_cred_getegid(l->l_cred) != attr.va_gid))) { 1097 /* 1098 * Mark the process as SUGID before we do 1099 * anything that might block. 1100 */ 1101 proc_crmod_enter(); 1102 proc_crmod_leave(NULL, NULL, true); 1103 1104 /* Make sure file descriptors 0..2 are in use. */ 1105 if ((error = fd_checkstd()) != 0) { 1106 DPRINTF(("%s: fdcheckstd failed %d\n", 1107 __func__, error)); 1108 goto exec_abort; 1109 } 1110 1111 /* 1112 * Copy the credential so other references don't see our 1113 * changes. 1114 */ 1115 l->l_cred = kauth_cred_copy(l->l_cred); 1116 #ifdef KTRACE 1117 /* 1118 * If the persistent trace flag isn't set, turn off. 1119 */ 1120 if (p->p_tracep) { 1121 mutex_enter(&ktrace_lock); 1122 if (!(p->p_traceflag & KTRFAC_PERSISTENT)) 1123 ktrderef(p); 1124 mutex_exit(&ktrace_lock); 1125 } 1126 #endif 1127 if (attr.va_mode & S_ISUID) 1128 kauth_cred_seteuid(l->l_cred, attr.va_uid); 1129 if (attr.va_mode & S_ISGID) 1130 kauth_cred_setegid(l->l_cred, attr.va_gid); 1131 } else { 1132 if (kauth_cred_geteuid(l->l_cred) == 1133 kauth_cred_getuid(l->l_cred) && 1134 kauth_cred_getegid(l->l_cred) == 1135 kauth_cred_getgid(l->l_cred)) 1136 p->p_flag &= ~PK_SUGID; 1137 } 1138 1139 /* 1140 * Copy the credential so other references don't see our changes. 1141 * Test to see if this is necessary first, since in the common case 1142 * we won't need a private reference. 1143 */ 1144 if (kauth_cred_geteuid(l->l_cred) != kauth_cred_getsvuid(l->l_cred) || 1145 kauth_cred_getegid(l->l_cred) != kauth_cred_getsvgid(l->l_cred)) { 1146 l->l_cred = kauth_cred_copy(l->l_cred); 1147 kauth_cred_setsvuid(l->l_cred, kauth_cred_geteuid(l->l_cred)); 1148 kauth_cred_setsvgid(l->l_cred, kauth_cred_getegid(l->l_cred)); 1149 } 1150 1151 /* Update the master credentials. */ 1152 if (l->l_cred != p->p_cred) { 1153 kauth_cred_t ocred; 1154 1155 kauth_cred_hold(l->l_cred); 1156 mutex_enter(p->p_lock); 1157 ocred = p->p_cred; 1158 p->p_cred = l->l_cred; 1159 mutex_exit(p->p_lock); 1160 kauth_cred_free(ocred); 1161 } 1162 1163 #if defined(__HAVE_RAS) 1164 /* 1165 * Remove all RASs from the address space. 1166 */ 1167 ras_purgeall(); 1168 #endif 1169 1170 doexechooks(p); 1171 1172 /* setup new registers and do misc. setup. */ 1173 (*pack.ep_esch->es_emul->e_setregs)(l, &pack, (vaddr_t)stack); 1174 if (pack.ep_esch->es_setregs) 1175 (*pack.ep_esch->es_setregs)(l, &pack, (vaddr_t)stack); 1176 1177 /* Provide a consistent LWP private setting */ 1178 (void)lwp_setprivate(l, NULL); 1179 1180 /* Discard all PCU state; need to start fresh */ 1181 pcu_discard_all(l); 1182 1183 /* map the process's signal trampoline code */ 1184 if ((error = exec_sigcode_map(p, pack.ep_esch->es_emul)) != 0) { 1185 DPRINTF(("%s: map sigcode failed %d\n", __func__, error)); 1186 goto exec_abort; 1187 } 1188 1189 pool_put(&exec_pool, argp); 1190 1191 /* notify others that we exec'd */ 1192 KNOTE(&p->p_klist, NOTE_EXEC); 1193 1194 kmem_free(pack.ep_hdr, pack.ep_hdrlen); 1195 1196 SDT_PROBE(proc,,,exec_success, path, 0, 0, 0, 0); 1197 1198 /* The emulation root will usually have been found when we looked 1199 * for the elf interpreter (or similar), if not look now. */ 1200 if (pack.ep_esch->es_emul->e_path != NULL && pack.ep_emul_root == NULL) 1201 emul_find_root(l, &pack); 1202 1203 /* Any old emulation root got removed by fdcloseexec */ 1204 rw_enter(&p->p_cwdi->cwdi_lock, RW_WRITER); 1205 p->p_cwdi->cwdi_edir = pack.ep_emul_root; 1206 rw_exit(&p->p_cwdi->cwdi_lock); 1207 pack.ep_emul_root = NULL; 1208 if (pack.ep_interp != NULL) 1209 vrele(pack.ep_interp); 1210 1211 /* 1212 * Call emulation specific exec hook. This can setup per-process 1213 * p->p_emuldata or do any other per-process stuff an emulation needs. 1214 * 1215 * If we are executing process of different emulation than the 1216 * original forked process, call e_proc_exit() of the old emulation 1217 * first, then e_proc_exec() of new emulation. If the emulation is 1218 * same, the exec hook code should deallocate any old emulation 1219 * resources held previously by this process. 1220 */ 1221 if (p->p_emul && p->p_emul->e_proc_exit 1222 && p->p_emul != pack.ep_esch->es_emul) 1223 (*p->p_emul->e_proc_exit)(p); 1224 1225 /* 1226 * This is now LWP 1. 1227 */ 1228 mutex_enter(p->p_lock); 1229 p->p_nlwpid = 1; 1230 l->l_lid = 1; 1231 mutex_exit(p->p_lock); 1232 1233 /* 1234 * Call exec hook. Emulation code may NOT store reference to anything 1235 * from &pack. 1236 */ 1237 if (pack.ep_esch->es_emul->e_proc_exec) 1238 (*pack.ep_esch->es_emul->e_proc_exec)(p, &pack); 1239 1240 /* update p_emul, the old value is no longer needed */ 1241 p->p_emul = pack.ep_esch->es_emul; 1242 1243 /* ...and the same for p_execsw */ 1244 p->p_execsw = pack.ep_esch; 1245 1246 #ifdef __HAVE_SYSCALL_INTERN 1247 (*p->p_emul->e_syscall_intern)(p); 1248 #endif 1249 ktremul(); 1250 1251 /* Allow new references from the debugger/procfs. */ 1252 rw_exit(&p->p_reflock); 1253 rw_exit(&exec_lock); 1254 1255 mutex_enter(proc_lock); 1256 1257 if ((p->p_slflag & (PSL_TRACED|PSL_SYSCALL)) == PSL_TRACED) { 1258 KSI_INIT_EMPTY(&ksi); 1259 ksi.ksi_signo = SIGTRAP; 1260 ksi.ksi_lid = l->l_lid; 1261 kpsignal(p, &ksi, NULL); 1262 } 1263 1264 if (p->p_sflag & PS_STOPEXEC) { 1265 KERNEL_UNLOCK_ALL(l, &l->l_biglocks); 1266 p->p_pptr->p_nstopchild++; 1267 p->p_pptr->p_waited = 0; 1268 mutex_enter(p->p_lock); 1269 ksiginfo_queue_init(&kq); 1270 sigclearall(p, &contsigmask, &kq); 1271 lwp_lock(l); 1272 l->l_stat = LSSTOP; 1273 p->p_stat = SSTOP; 1274 p->p_nrlwps--; 1275 lwp_unlock(l); 1276 mutex_exit(p->p_lock); 1277 mutex_exit(proc_lock); 1278 lwp_lock(l); 1279 mi_switch(l); 1280 ksiginfo_queue_drain(&kq); 1281 KERNEL_LOCK(l->l_biglocks, l); 1282 } else { 1283 mutex_exit(proc_lock); 1284 } 1285 1286 pathbuf_stringcopy_put(pb, pathstring); 1287 pathbuf_destroy(pb); 1288 PNBUF_PUT(resolvedpathbuf); 1289 DPRINTF(("%s finished\n", __func__)); 1290 return (EJUSTRETURN); 1291 1292 bad: 1293 /* free the vmspace-creation commands, and release their references */ 1294 kill_vmcmds(&pack.ep_vmcmds); 1295 /* kill any opened file descriptor, if necessary */ 1296 if (pack.ep_flags & EXEC_HASFD) { 1297 pack.ep_flags &= ~EXEC_HASFD; 1298 fd_close(pack.ep_fd); 1299 } 1300 /* close and put the exec'd file */ 1301 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 1302 VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred); 1303 vput(pack.ep_vp); 1304 pool_put(&exec_pool, argp); 1305 1306 freehdr: 1307 kmem_free(pack.ep_hdr, pack.ep_hdrlen); 1308 if (pack.ep_emul_root != NULL) 1309 vrele(pack.ep_emul_root); 1310 if (pack.ep_interp != NULL) 1311 vrele(pack.ep_interp); 1312 1313 rw_exit(&exec_lock); 1314 1315 pathbuf_stringcopy_put(pb, pathstring); 1316 pathbuf_destroy(pb); 1317 PNBUF_PUT(resolvedpathbuf); 1318 1319 clrflg: 1320 lwp_lock(l); 1321 l->l_flag |= oldlwpflags; 1322 lwp_unlock(l); 1323 rw_exit(&p->p_reflock); 1324 1325 if (modgen != module_gen && error == ENOEXEC) { 1326 modgen = module_gen; 1327 exec_autoload(); 1328 goto retry; 1329 } 1330 1331 SDT_PROBE(proc,,,exec_failure, error, 0, 0, 0, 0); 1332 return error; 1333 1334 exec_abort: 1335 SDT_PROBE(proc,,,exec_failure, error, 0, 0, 0, 0); 1336 rw_exit(&p->p_reflock); 1337 rw_exit(&exec_lock); 1338 1339 pathbuf_stringcopy_put(pb, pathstring); 1340 pathbuf_destroy(pb); 1341 PNBUF_PUT(resolvedpathbuf); 1342 1343 /* 1344 * the old process doesn't exist anymore. exit gracefully. 1345 * get rid of the (new) address space we have created, if any, get rid 1346 * of our namei data and vnode, and exit noting failure 1347 */ 1348 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 1349 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 1350 if (pack.ep_emul_arg) 1351 free(pack.ep_emul_arg, M_TEMP); 1352 pool_put(&exec_pool, argp); 1353 kmem_free(pack.ep_hdr, pack.ep_hdrlen); 1354 if (pack.ep_emul_root != NULL) 1355 vrele(pack.ep_emul_root); 1356 if (pack.ep_interp != NULL) 1357 vrele(pack.ep_interp); 1358 1359 /* Acquire the sched-state mutex (exit1() will release it). */ 1360 mutex_enter(p->p_lock); 1361 exit1(l, W_EXITCODE(error, SIGABRT)); 1362 1363 /* NOTREACHED */ 1364 return 0; 1365 } 1366 1367 int 1368 copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo, 1369 char **stackp, void *argp) 1370 { 1371 char **cpp, *dp, *sp; 1372 size_t len; 1373 void *nullp; 1374 long argc, envc; 1375 int error; 1376 1377 cpp = (char **)*stackp; 1378 nullp = NULL; 1379 argc = arginfo->ps_nargvstr; 1380 envc = arginfo->ps_nenvstr; 1381 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0) { 1382 COPYPRINTF("", cpp - 1, sizeof(argc)); 1383 return error; 1384 } 1385 1386 dp = (char *) (cpp + argc + envc + 2 + pack->ep_esch->es_arglen); 1387 sp = argp; 1388 1389 /* XXX don't copy them out, remap them! */ 1390 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 1391 1392 for (; --argc >= 0; sp += len, dp += len) { 1393 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0) { 1394 COPYPRINTF("", cpp - 1, sizeof(dp)); 1395 return error; 1396 } 1397 if ((error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) { 1398 COPYPRINTF("str", dp, (size_t)ARG_MAX); 1399 return error; 1400 } 1401 } 1402 1403 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) { 1404 COPYPRINTF("", cpp - 1, sizeof(nullp)); 1405 return error; 1406 } 1407 1408 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 1409 1410 for (; --envc >= 0; sp += len, dp += len) { 1411 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0) { 1412 COPYPRINTF("", cpp - 1, sizeof(dp)); 1413 return error; 1414 } 1415 if ((error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) { 1416 COPYPRINTF("str", dp, (size_t)ARG_MAX); 1417 return error; 1418 } 1419 } 1420 1421 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) { 1422 COPYPRINTF("", cpp - 1, sizeof(nullp)); 1423 return error; 1424 } 1425 1426 *stackp = (char *)cpp; 1427 return 0; 1428 } 1429 1430 1431 /* 1432 * Add execsw[] entries. 1433 */ 1434 int 1435 exec_add(struct execsw *esp, int count) 1436 { 1437 struct exec_entry *it; 1438 int i; 1439 1440 if (count == 0) { 1441 return 0; 1442 } 1443 1444 /* Check for duplicates. */ 1445 rw_enter(&exec_lock, RW_WRITER); 1446 for (i = 0; i < count; i++) { 1447 LIST_FOREACH(it, &ex_head, ex_list) { 1448 /* assume unique (makecmds, probe_func, emulation) */ 1449 if (it->ex_sw->es_makecmds == esp[i].es_makecmds && 1450 it->ex_sw->u.elf_probe_func == 1451 esp[i].u.elf_probe_func && 1452 it->ex_sw->es_emul == esp[i].es_emul) { 1453 rw_exit(&exec_lock); 1454 return EEXIST; 1455 } 1456 } 1457 } 1458 1459 /* Allocate new entries. */ 1460 for (i = 0; i < count; i++) { 1461 it = kmem_alloc(sizeof(*it), KM_SLEEP); 1462 it->ex_sw = &esp[i]; 1463 LIST_INSERT_HEAD(&ex_head, it, ex_list); 1464 } 1465 1466 /* update execsw[] */ 1467 exec_init(0); 1468 rw_exit(&exec_lock); 1469 return 0; 1470 } 1471 1472 /* 1473 * Remove execsw[] entry. 1474 */ 1475 int 1476 exec_remove(struct execsw *esp, int count) 1477 { 1478 struct exec_entry *it, *next; 1479 int i; 1480 const struct proclist_desc *pd; 1481 proc_t *p; 1482 1483 if (count == 0) { 1484 return 0; 1485 } 1486 1487 /* Abort if any are busy. */ 1488 rw_enter(&exec_lock, RW_WRITER); 1489 for (i = 0; i < count; i++) { 1490 mutex_enter(proc_lock); 1491 for (pd = proclists; pd->pd_list != NULL; pd++) { 1492 PROCLIST_FOREACH(p, pd->pd_list) { 1493 if (p->p_execsw == &esp[i]) { 1494 mutex_exit(proc_lock); 1495 rw_exit(&exec_lock); 1496 return EBUSY; 1497 } 1498 } 1499 } 1500 mutex_exit(proc_lock); 1501 } 1502 1503 /* None are busy, so remove them all. */ 1504 for (i = 0; i < count; i++) { 1505 for (it = LIST_FIRST(&ex_head); it != NULL; it = next) { 1506 next = LIST_NEXT(it, ex_list); 1507 if (it->ex_sw == &esp[i]) { 1508 LIST_REMOVE(it, ex_list); 1509 kmem_free(it, sizeof(*it)); 1510 break; 1511 } 1512 } 1513 } 1514 1515 /* update execsw[] */ 1516 exec_init(0); 1517 rw_exit(&exec_lock); 1518 return 0; 1519 } 1520 1521 /* 1522 * Initialize exec structures. If init_boot is true, also does necessary 1523 * one-time initialization (it's called from main() that way). 1524 * Once system is multiuser, this should be called with exec_lock held, 1525 * i.e. via exec_{add|remove}(). 1526 */ 1527 int 1528 exec_init(int init_boot) 1529 { 1530 const struct execsw **sw; 1531 struct exec_entry *ex; 1532 SLIST_HEAD(,exec_entry) first; 1533 SLIST_HEAD(,exec_entry) any; 1534 SLIST_HEAD(,exec_entry) last; 1535 int i, sz; 1536 1537 if (init_boot) { 1538 /* do one-time initializations */ 1539 rw_init(&exec_lock); 1540 mutex_init(&sigobject_lock, MUTEX_DEFAULT, IPL_NONE); 1541 pool_init(&exec_pool, NCARGS, 0, 0, PR_NOALIGN|PR_NOTOUCH, 1542 "execargs", &exec_palloc, IPL_NONE); 1543 pool_sethardlimit(&exec_pool, maxexec, "should not happen", 0); 1544 } else { 1545 KASSERT(rw_write_held(&exec_lock)); 1546 } 1547 1548 /* Sort each entry onto the appropriate queue. */ 1549 SLIST_INIT(&first); 1550 SLIST_INIT(&any); 1551 SLIST_INIT(&last); 1552 sz = 0; 1553 LIST_FOREACH(ex, &ex_head, ex_list) { 1554 switch(ex->ex_sw->es_prio) { 1555 case EXECSW_PRIO_FIRST: 1556 SLIST_INSERT_HEAD(&first, ex, ex_slist); 1557 break; 1558 case EXECSW_PRIO_ANY: 1559 SLIST_INSERT_HEAD(&any, ex, ex_slist); 1560 break; 1561 case EXECSW_PRIO_LAST: 1562 SLIST_INSERT_HEAD(&last, ex, ex_slist); 1563 break; 1564 default: 1565 panic("%s", __func__); 1566 break; 1567 } 1568 sz++; 1569 } 1570 1571 /* 1572 * Create new execsw[]. Ensure we do not try a zero-sized 1573 * allocation. 1574 */ 1575 sw = kmem_alloc(sz * sizeof(struct execsw *) + 1, KM_SLEEP); 1576 i = 0; 1577 SLIST_FOREACH(ex, &first, ex_slist) { 1578 sw[i++] = ex->ex_sw; 1579 } 1580 SLIST_FOREACH(ex, &any, ex_slist) { 1581 sw[i++] = ex->ex_sw; 1582 } 1583 SLIST_FOREACH(ex, &last, ex_slist) { 1584 sw[i++] = ex->ex_sw; 1585 } 1586 1587 /* Replace old execsw[] and free used memory. */ 1588 if (execsw != NULL) { 1589 kmem_free(__UNCONST(execsw), 1590 nexecs * sizeof(struct execsw *) + 1); 1591 } 1592 execsw = sw; 1593 nexecs = sz; 1594 1595 /* Figure out the maximum size of an exec header. */ 1596 exec_maxhdrsz = sizeof(int); 1597 for (i = 0; i < nexecs; i++) { 1598 if (execsw[i]->es_hdrsz > exec_maxhdrsz) 1599 exec_maxhdrsz = execsw[i]->es_hdrsz; 1600 } 1601 1602 return 0; 1603 } 1604 1605 static int 1606 exec_sigcode_map(struct proc *p, const struct emul *e) 1607 { 1608 vaddr_t va; 1609 vsize_t sz; 1610 int error; 1611 struct uvm_object *uobj; 1612 1613 sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode; 1614 1615 if (e->e_sigobject == NULL || sz == 0) { 1616 return 0; 1617 } 1618 1619 /* 1620 * If we don't have a sigobject for this emulation, create one. 1621 * 1622 * sigobject is an anonymous memory object (just like SYSV shared 1623 * memory) that we keep a permanent reference to and that we map 1624 * in all processes that need this sigcode. The creation is simple, 1625 * we create an object, add a permanent reference to it, map it in 1626 * kernel space, copy out the sigcode to it and unmap it. 1627 * We map it with PROT_READ|PROT_EXEC into the process just 1628 * the way sys_mmap() would map it. 1629 */ 1630 1631 uobj = *e->e_sigobject; 1632 if (uobj == NULL) { 1633 mutex_enter(&sigobject_lock); 1634 if ((uobj = *e->e_sigobject) == NULL) { 1635 uobj = uao_create(sz, 0); 1636 (*uobj->pgops->pgo_reference)(uobj); 1637 va = vm_map_min(kernel_map); 1638 if ((error = uvm_map(kernel_map, &va, round_page(sz), 1639 uobj, 0, 0, 1640 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, 1641 UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) { 1642 printf("kernel mapping failed %d\n", error); 1643 (*uobj->pgops->pgo_detach)(uobj); 1644 mutex_exit(&sigobject_lock); 1645 return (error); 1646 } 1647 memcpy((void *)va, e->e_sigcode, sz); 1648 #ifdef PMAP_NEED_PROCWR 1649 pmap_procwr(&proc0, va, sz); 1650 #endif 1651 uvm_unmap(kernel_map, va, va + round_page(sz)); 1652 *e->e_sigobject = uobj; 1653 } 1654 mutex_exit(&sigobject_lock); 1655 } 1656 1657 /* Just a hint to uvm_map where to put it. */ 1658 va = e->e_vm_default_addr(p, (vaddr_t)p->p_vmspace->vm_daddr, 1659 round_page(sz)); 1660 1661 #ifdef __alpha__ 1662 /* 1663 * Tru64 puts /sbin/loader at the end of user virtual memory, 1664 * which causes the above calculation to put the sigcode at 1665 * an invalid address. Put it just below the text instead. 1666 */ 1667 if (va == (vaddr_t)vm_map_max(&p->p_vmspace->vm_map)) { 1668 va = (vaddr_t)p->p_vmspace->vm_taddr - round_page(sz); 1669 } 1670 #endif 1671 1672 (*uobj->pgops->pgo_reference)(uobj); 1673 error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz), 1674 uobj, 0, 0, 1675 UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE, 1676 UVM_ADV_RANDOM, 0)); 1677 if (error) { 1678 DPRINTF(("%s, %d: map %p " 1679 "uvm_map %#"PRIxVSIZE"@%#"PRIxVADDR" failed %d\n", 1680 __func__, __LINE__, &p->p_vmspace->vm_map, round_page(sz), 1681 va, error)); 1682 (*uobj->pgops->pgo_detach)(uobj); 1683 return (error); 1684 } 1685 p->p_sigctx.ps_sigcode = (void *)va; 1686 return (0); 1687 } 1688