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