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