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