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