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