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