1 /* $NetBSD: kern_exec.c,v 1.146 2001/11/12 15:25:08 lukem 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.146 2001/11/12 15:25:08 lukem 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 /* 158 * Exec lock. Used to control access to execsw[] structures. 159 * This must not be static so that netbsd32 can access it, too. 160 */ 161 struct lock exec_lock; 162 163 #ifdef LKM 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 lockmgr(&exec_lock, LK_SHARED, NULL); 369 370 /* see if we can run it. */ 371 if ((error = check_exec(p, &pack)) != 0) 372 goto freehdr; 373 374 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */ 375 376 /* allocate an argument buffer */ 377 argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS); 378 #ifdef DIAGNOSTIC 379 if (argp == (vaddr_t) 0) 380 panic("execve: argp == NULL"); 381 #endif 382 dp = argp; 383 argc = 0; 384 385 /* copy the fake args list, if there's one, freeing it as we go */ 386 if (pack.ep_flags & EXEC_HASARGL) { 387 tmpfap = pack.ep_fa; 388 while (*tmpfap != NULL) { 389 char *cp; 390 391 cp = *tmpfap; 392 while (*cp) 393 *dp++ = *cp++; 394 dp++; 395 396 FREE(*tmpfap, M_EXEC); 397 tmpfap++; argc++; 398 } 399 FREE(pack.ep_fa, M_EXEC); 400 pack.ep_flags &= ~EXEC_HASARGL; 401 } 402 403 /* Now get argv & environment */ 404 if (!(cpp = SCARG(uap, argp))) { 405 error = EINVAL; 406 goto bad; 407 } 408 409 if (pack.ep_flags & EXEC_SKIPARG) 410 cpp++; 411 412 while (1) { 413 len = argp + ARG_MAX - dp; 414 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0) 415 goto bad; 416 if (!sp) 417 break; 418 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 419 if (error == ENAMETOOLONG) 420 error = E2BIG; 421 goto bad; 422 } 423 dp += len; 424 cpp++; 425 argc++; 426 } 427 428 envc = 0; 429 /* environment need not be there */ 430 if ((cpp = SCARG(uap, envp)) != NULL ) { 431 while (1) { 432 len = argp + ARG_MAX - dp; 433 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0) 434 goto bad; 435 if (!sp) 436 break; 437 if ((error = copyinstr(sp, dp, len, &len)) != 0) { 438 if (error == ENAMETOOLONG) 439 error = E2BIG; 440 goto bad; 441 } 442 dp += len; 443 cpp++; 444 envc++; 445 } 446 } 447 448 dp = (char *) ALIGN(dp); 449 450 szsigcode = pack.ep_es->es_emul->e_esigcode - 451 pack.ep_es->es_emul->e_sigcode; 452 453 /* Now check if args & environ fit into new stack */ 454 if (pack.ep_flags & EXEC_32) 455 len = ((argc + envc + 2 + pack.ep_es->es_arglen) * 456 sizeof(int) + sizeof(int) + dp + STACKGAPLEN + 457 szsigcode + sizeof(struct ps_strings)) - argp; 458 else 459 len = ((argc + envc + 2 + pack.ep_es->es_arglen) * 460 sizeof(char *) + sizeof(int) + dp + STACKGAPLEN + 461 szsigcode + sizeof(struct ps_strings)) - argp; 462 463 len = ALIGN(len); /* make the stack "safely" aligned */ 464 465 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */ 466 error = ENOMEM; 467 goto bad; 468 } 469 470 /* adjust "active stack depth" for process VSZ */ 471 pack.ep_ssize = len; /* maybe should go elsewhere, but... */ 472 473 /* 474 * Do whatever is necessary to prepare the address space 475 * for remapping. Note that this might replace the current 476 * vmspace with another! 477 */ 478 uvmspace_exec(p, pack.ep_vm_minaddr, pack.ep_vm_maxaddr); 479 480 /* Now map address space */ 481 vm = p->p_vmspace; 482 vm->vm_taddr = (char *) pack.ep_taddr; 483 vm->vm_tsize = btoc(pack.ep_tsize); 484 vm->vm_daddr = (char *) pack.ep_daddr; 485 vm->vm_dsize = btoc(pack.ep_dsize); 486 vm->vm_ssize = btoc(pack.ep_ssize); 487 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr; 488 vm->vm_minsaddr = (char *) pack.ep_minsaddr; 489 490 /* create the new process's VM space by running the vmcmds */ 491 #ifdef DIAGNOSTIC 492 if (pack.ep_vmcmds.evs_used == 0) 493 panic("execve: no vmcmds"); 494 #endif 495 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) { 496 struct exec_vmcmd *vcp; 497 498 vcp = &pack.ep_vmcmds.evs_cmds[i]; 499 if (vcp->ev_flags & VMCMD_RELATIVE) { 500 #ifdef DIAGNOSTIC 501 if (base_vcp == NULL) 502 panic("execve: relative vmcmd with no base"); 503 if (vcp->ev_flags & VMCMD_BASE) 504 panic("execve: illegal base & relative vmcmd"); 505 #endif 506 vcp->ev_addr += base_vcp->ev_addr; 507 } 508 error = (*vcp->ev_proc)(p, vcp); 509 #ifdef DEBUG_EXEC 510 if (error) { 511 int j; 512 struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0]; 513 for (j = 0; j <= i; j++) 514 uprintf( 515 "vmcmd[%d] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n", 516 j, vp[j].ev_addr, vp[j].ev_len, 517 vp[j].ev_offset, vp[j].ev_prot, 518 vp[j].ev_flags); 519 } 520 #endif /* DEBUG_EXEC */ 521 if (vcp->ev_flags & VMCMD_BASE) 522 base_vcp = vcp; 523 } 524 525 /* free the vmspace-creation commands, and release their references */ 526 kill_vmcmds(&pack.ep_vmcmds); 527 528 /* if an error happened, deallocate and punt */ 529 if (error) { 530 DPRINTF(("execve: vmcmd %i failed: %d\n", i - 1, error)); 531 goto exec_abort; 532 } 533 534 /* remember information about the process */ 535 arginfo.ps_nargvstr = argc; 536 arginfo.ps_nenvstr = envc; 537 538 stack = (char *) (vm->vm_minsaddr - len); 539 /* Now copy argc, args & environ to new stack */ 540 error = (*pack.ep_es->es_copyargs)(&pack, &arginfo, &stack, argp); 541 if (error) { 542 DPRINTF(("execve: copyargs failed %d\n", error)); 543 goto exec_abort; 544 } 545 /* Move the stack back to original point */ 546 stack = (char *) (vm->vm_minsaddr - len); 547 548 /* fill process ps_strings info */ 549 p->p_psstr = (struct ps_strings *)(vm->vm_minsaddr 550 - sizeof(struct ps_strings)); 551 p->p_psargv = offsetof(struct ps_strings, ps_argvstr); 552 p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr); 553 p->p_psenv = offsetof(struct ps_strings, ps_envstr); 554 p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr); 555 556 /* copy out the process's ps_strings structure */ 557 if ((error = copyout(&arginfo, (char *)p->p_psstr, 558 sizeof(arginfo))) != 0) { 559 DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n", 560 &arginfo, (char *)p->p_psstr, (long)sizeof(arginfo))); 561 goto exec_abort; 562 } 563 564 /* copy out the process's signal trapoline code */ 565 if (szsigcode) { 566 if ((error = copyout((char *)pack.ep_es->es_emul->e_sigcode, 567 p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode, 568 szsigcode)) != 0) { 569 DPRINTF(("execve: sig trampoline copyout failed\n")); 570 goto exec_abort; 571 } 572 #ifdef PMAP_NEED_PROCWR 573 /* This is code. Let the pmap do what is needed. */ 574 pmap_procwr(p, (vaddr_t)p->p_sigctx.ps_sigcode, szsigcode); 575 #endif 576 } 577 578 stopprofclock(p); /* stop profiling */ 579 fdcloseexec(p); /* handle close on exec */ 580 execsigs(p); /* reset catched signals */ 581 p->p_ctxlink = NULL; /* reset ucontext link */ 582 583 /* set command name & other accounting info */ 584 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN); 585 memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len); 586 p->p_comm[len] = 0; 587 p->p_acflag &= ~AFORK; 588 589 /* record proc's vnode, for use by procfs and others */ 590 if (p->p_textvp) 591 vrele(p->p_textvp); 592 VREF(pack.ep_vp); 593 p->p_textvp = pack.ep_vp; 594 595 p->p_flag |= P_EXEC; 596 if (p->p_flag & P_PPWAIT) { 597 p->p_flag &= ~P_PPWAIT; 598 wakeup((caddr_t) p->p_pptr); 599 } 600 601 /* 602 * deal with set[ug]id. 603 * MNT_NOSUID has already been used to disable s[ug]id. 604 */ 605 if ((p->p_flag & P_TRACED) == 0 && 606 607 (((attr.va_mode & S_ISUID) != 0 && 608 p->p_ucred->cr_uid != attr.va_uid) || 609 610 ((attr.va_mode & S_ISGID) != 0 && 611 p->p_ucred->cr_gid != attr.va_gid))) { 612 /* 613 * Mark the process as SUGID before we do 614 * anything that might block. 615 */ 616 p_sugid(p); 617 618 p->p_ucred = crcopy(cred); 619 #ifdef KTRACE 620 /* 621 * If process is being ktraced, turn off - unless 622 * root set it. 623 */ 624 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) 625 ktrderef(p); 626 #endif 627 if (attr.va_mode & S_ISUID) 628 p->p_ucred->cr_uid = attr.va_uid; 629 if (attr.va_mode & S_ISGID) 630 p->p_ucred->cr_gid = attr.va_gid; 631 } else 632 p->p_flag &= ~P_SUGID; 633 p->p_cred->p_svuid = p->p_ucred->cr_uid; 634 p->p_cred->p_svgid = p->p_ucred->cr_gid; 635 636 doexechooks(p); 637 638 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 639 640 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 641 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 642 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 643 vput(pack.ep_vp); 644 645 /* setup new registers and do misc. setup. */ 646 (*pack.ep_es->es_emul->e_setregs)(p, &pack, (u_long) stack); 647 if (pack.ep_es->es_setregs) 648 (*pack.ep_es->es_setregs)(p, &pack, (u_long) stack); 649 650 if (p->p_flag & P_TRACED) 651 psignal(p, SIGTRAP); 652 653 free(pack.ep_hdr, M_EXEC); 654 655 /* 656 * Call emulation specific exec hook. This can setup setup per-process 657 * p->p_emuldata or do any other per-process stuff an emulation needs. 658 * 659 * If we are executing process of different emulation than the 660 * original forked process, call e_proc_exit() of the old emulation 661 * first, then e_proc_exec() of new emulation. If the emulation is 662 * same, the exec hook code should deallocate any old emulation 663 * resources held previously by this process. 664 */ 665 if (p->p_emul && p->p_emul->e_proc_exit 666 && p->p_emul != pack.ep_es->es_emul) 667 (*p->p_emul->e_proc_exit)(p); 668 669 /* 670 * Call exec hook. Emulation code may NOT store reference to anything 671 * from &pack. 672 */ 673 if (pack.ep_es->es_emul->e_proc_exec) 674 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack); 675 676 /* update p_emul, the old value is no longer needed */ 677 p->p_emul = pack.ep_es->es_emul; 678 #ifdef __HAVE_SYSCALL_INTERN 679 (*p->p_emul->e_syscall_intern)(p); 680 #endif 681 #ifdef KTRACE 682 if (KTRPOINT(p, KTR_EMUL)) 683 ktremul(p); 684 #endif 685 686 lockmgr(&exec_lock, LK_RELEASE, NULL); 687 688 return (EJUSTRETURN); 689 690 bad: 691 /* free the vmspace-creation commands, and release their references */ 692 kill_vmcmds(&pack.ep_vmcmds); 693 /* kill any opened file descriptor, if necessary */ 694 if (pack.ep_flags & EXEC_HASFD) { 695 pack.ep_flags &= ~EXEC_HASFD; 696 (void) fdrelease(p, pack.ep_fd); 697 } 698 /* close and put the exec'd file */ 699 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 700 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 701 vput(pack.ep_vp); 702 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 703 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 704 705 freehdr: 706 lockmgr(&exec_lock, LK_RELEASE, NULL); 707 708 free(pack.ep_hdr, M_EXEC); 709 return error; 710 711 exec_abort: 712 lockmgr(&exec_lock, LK_RELEASE, NULL); 713 714 /* 715 * the old process doesn't exist anymore. exit gracefully. 716 * get rid of the (new) address space we have created, if any, get rid 717 * of our namei data and vnode, and exit noting failure 718 */ 719 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS, 720 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS); 721 if (pack.ep_emul_arg) 722 FREE(pack.ep_emul_arg, M_TEMP); 723 PNBUF_PUT(nid.ni_cnd.cn_pnbuf); 724 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY); 725 VOP_CLOSE(pack.ep_vp, FREAD, cred, p); 726 vput(pack.ep_vp); 727 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS); 728 free(pack.ep_hdr, M_EXEC); 729 exit1(p, W_EXITCODE(error, SIGABRT)); 730 731 /* NOTREACHED */ 732 return 0; 733 } 734 735 736 int 737 copyargs(struct exec_package *pack, struct ps_strings *arginfo, 738 char **stackp, void *argp) 739 { 740 char **cpp, *dp, *sp; 741 size_t len; 742 void *nullp; 743 long argc, envc; 744 int error; 745 746 cpp = (char **)*stackp; 747 nullp = NULL; 748 argc = arginfo->ps_nargvstr; 749 envc = arginfo->ps_nenvstr; 750 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0) 751 return error; 752 753 dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen); 754 sp = argp; 755 756 /* XXX don't copy them out, remap them! */ 757 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 758 759 for (; --argc >= 0; sp += len, dp += len) 760 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 || 761 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) 762 return error; 763 764 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) 765 return error; 766 767 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 768 769 for (; --envc >= 0; sp += len, dp += len) 770 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 || 771 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) 772 return error; 773 774 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) 775 return error; 776 777 *stackp = (char *)cpp; 778 return 0; 779 } 780 781 #ifdef LKM 782 /* 783 * Find an emulation of given name in list of emulations. 784 */ 785 static const struct emul * 786 emul_search(const char *name) 787 { 788 struct emul_entry *it; 789 790 LIST_FOREACH(it, &el_head, el_list) { 791 if (strcmp(name, it->el_emul->e_name) == 0) 792 return it->el_emul; 793 } 794 795 return NULL; 796 } 797 798 /* 799 * Add an emulation to list, if it's not there already. 800 */ 801 int 802 emul_register(const struct emul *emul, int ro_entry) 803 { 804 struct emul_entry *ee; 805 int error; 806 807 error = 0; 808 lockmgr(&exec_lock, LK_SHARED, NULL); 809 810 if (emul_search(emul->e_name)) { 811 error = EEXIST; 812 goto out; 813 } 814 815 MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry), 816 M_EXEC, M_WAITOK); 817 ee->el_emul = emul; 818 ee->ro_entry = ro_entry; 819 LIST_INSERT_HEAD(&el_head, ee, el_list); 820 821 out: 822 lockmgr(&exec_lock, LK_RELEASE, NULL); 823 return error; 824 } 825 826 /* 827 * Remove emulation with name 'name' from list of supported emulations. 828 */ 829 int 830 emul_unregister(const char *name) 831 { 832 const struct proclist_desc *pd; 833 struct emul_entry *it; 834 int i, error; 835 struct proc *ptmp; 836 837 error = 0; 838 lockmgr(&exec_lock, LK_SHARED, NULL); 839 840 LIST_FOREACH(it, &el_head, el_list) { 841 if (strcmp(it->el_emul->e_name, name) == 0) 842 break; 843 } 844 845 if (!it) { 846 error = ENOENT; 847 goto out; 848 } 849 850 if (it->ro_entry) { 851 error = EBUSY; 852 goto out; 853 } 854 855 /* test if any execw[] entry is still using this */ 856 for(i=0; i < nexecs; i++) { 857 if (execsw[i]->es_emul == it->el_emul) { 858 error = EBUSY; 859 goto out; 860 } 861 } 862 863 /* 864 * Test if any process is running under this emulation - since 865 * emul_unregister() is running quite sendomly, it's better 866 * to do expensive check here than to use any locking. 867 */ 868 proclist_lock_read(); 869 for (pd = proclists; pd->pd_list != NULL && !error; pd++) { 870 LIST_FOREACH(ptmp, pd->pd_list, p_list) { 871 if (ptmp->p_emul == it->el_emul) { 872 error = EBUSY; 873 break; 874 } 875 } 876 } 877 proclist_unlock_read(); 878 879 if (error) 880 goto out; 881 882 883 /* entry is not used, remove it */ 884 LIST_REMOVE(it, el_list); 885 FREE(it, M_EXEC); 886 887 out: 888 lockmgr(&exec_lock, LK_RELEASE, NULL); 889 return error; 890 } 891 892 /* 893 * Add execsw[] entry. 894 */ 895 int 896 exec_add(struct execsw *esp, const char *e_name) 897 { 898 struct exec_entry *it; 899 int error; 900 901 error = 0; 902 lockmgr(&exec_lock, LK_EXCLUSIVE, NULL); 903 904 if (!esp->es_emul) { 905 esp->es_emul = emul_search(e_name); 906 if (!esp->es_emul) { 907 error = ENOENT; 908 goto out; 909 } 910 } 911 912 LIST_FOREACH(it, &ex_head, ex_list) { 913 /* assume tuple (makecmds, probe_func, emulation) is unique */ 914 if (it->es->es_check == esp->es_check 915 && it->es->u.elf_probe_func == esp->u.elf_probe_func 916 && it->es->es_emul == esp->es_emul) { 917 error = EEXIST; 918 goto out; 919 } 920 } 921 922 /* if we got here, the entry doesn't exist yet */ 923 MALLOC(it, struct exec_entry *, sizeof(struct exec_entry), 924 M_EXEC, M_WAITOK); 925 it->es = esp; 926 LIST_INSERT_HEAD(&ex_head, it, ex_list); 927 928 /* update execsw[] */ 929 exec_init(0); 930 931 out: 932 lockmgr(&exec_lock, LK_RELEASE, NULL); 933 return error; 934 } 935 936 /* 937 * Remove execsw[] entry. 938 */ 939 int 940 exec_remove(const struct execsw *esp) 941 { 942 struct exec_entry *it; 943 int error; 944 945 error = 0; 946 lockmgr(&exec_lock, LK_EXCLUSIVE, NULL); 947 948 LIST_FOREACH(it, &ex_head, ex_list) { 949 /* assume tuple (makecmds, probe_func, emulation) is unique */ 950 if (it->es->es_check == esp->es_check 951 && it->es->u.elf_probe_func == esp->u.elf_probe_func 952 && it->es->es_emul == esp->es_emul) 953 break; 954 } 955 if (!it) { 956 error = ENOENT; 957 goto out; 958 } 959 960 /* remove item from list and free resources */ 961 LIST_REMOVE(it, ex_list); 962 FREE(it, M_EXEC); 963 964 /* update execsw[] */ 965 exec_init(0); 966 967 out: 968 lockmgr(&exec_lock, LK_RELEASE, NULL); 969 return error; 970 } 971 972 static void 973 link_es(struct execsw_entry **listp, const struct execsw *esp) 974 { 975 struct execsw_entry *et, *e1; 976 977 MALLOC(et, struct execsw_entry *, sizeof(struct execsw_entry), 978 M_TEMP, M_WAITOK); 979 et->next = NULL; 980 et->es = esp; 981 if (*listp == NULL) { 982 *listp = et; 983 return; 984 } 985 986 switch(et->es->es_prio) { 987 case EXECSW_PRIO_FIRST: 988 /* put new entry as the first */ 989 et->next = *listp; 990 *listp = et; 991 break; 992 case EXECSW_PRIO_ANY: 993 /* put new entry after all *_FIRST and *_ANY entries */ 994 for(e1 = *listp; e1->next 995 && e1->next->es->es_prio != EXECSW_PRIO_LAST; 996 e1 = e1->next); 997 et->next = e1->next; 998 e1->next = et; 999 break; 1000 case EXECSW_PRIO_LAST: 1001 /* put new entry as the last one */ 1002 for(e1 = *listp; e1->next; e1 = e1->next); 1003 e1->next = et; 1004 break; 1005 default: 1006 #ifdef DIAGNOSTIC 1007 panic("execw[] entry with unknown priority %d found\n", 1008 et->es->es_prio); 1009 #endif 1010 break; 1011 } 1012 } 1013 1014 /* 1015 * Initialize exec structures. If init_boot is true, also does necessary 1016 * one-time initialization (it's called from main() that way). 1017 * Once system is multiuser, this should be called with exec_lock hold, 1018 * i.e. via exec_{add|remove}(). 1019 */ 1020 int 1021 exec_init(int init_boot) 1022 { 1023 const struct execsw **new_es, * const *old_es; 1024 struct execsw_entry *list, *e1; 1025 struct exec_entry *e2; 1026 int i, es_sz; 1027 1028 if (init_boot) { 1029 /* do one-time initializations */ 1030 lockinit(&exec_lock, PWAIT, "execlck", 0, 0); 1031 1032 /* register compiled-in emulations */ 1033 for(i=0; i < nexecs_builtin; i++) { 1034 if (execsw_builtin[i].es_emul) 1035 emul_register(execsw_builtin[i].es_emul, 1); 1036 } 1037 #ifdef DIAGNOSTIC 1038 if (i == 0) 1039 panic("no emulations found in execsw_builtin[]\n"); 1040 #endif 1041 } 1042 1043 /* 1044 * Build execsw[] array from builtin entries and entries added 1045 * at runtime. 1046 */ 1047 list = NULL; 1048 for(i=0; i < nexecs_builtin; i++) 1049 link_es(&list, &execsw_builtin[i]); 1050 1051 /* Add dynamically loaded entries */ 1052 es_sz = nexecs_builtin; 1053 LIST_FOREACH(e2, &ex_head, ex_list) { 1054 link_es(&list, e2->es); 1055 es_sz++; 1056 } 1057 1058 /* 1059 * Now that we have sorted all execw entries, create new execsw[] 1060 * and free no longer needed memory in the process. 1061 */ 1062 new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK); 1063 for(i=0; list; i++) { 1064 new_es[i] = list->es; 1065 e1 = list->next; 1066 FREE(list, M_TEMP); 1067 list = e1; 1068 } 1069 1070 /* 1071 * New execsw[] array built, now replace old execsw[] and free 1072 * used memory. 1073 */ 1074 old_es = execsw; 1075 execsw = new_es; 1076 nexecs = es_sz; 1077 if (old_es) 1078 free((void *)old_es, M_EXEC); 1079 1080 /* 1081 * Figure out the maximum size of an exec header. 1082 */ 1083 exec_maxhdrsz = 0; 1084 for (i = 0; i < nexecs; i++) { 1085 if (execsw[i]->es_hdrsz > exec_maxhdrsz) 1086 exec_maxhdrsz = execsw[i]->es_hdrsz; 1087 } 1088 1089 return 0; 1090 } 1091 #endif 1092 1093 #ifndef LKM 1094 /* 1095 * Simplified exec_init() for kernels without LKMs. Only initialize 1096 * exec_maxhdrsz and execsw[]. 1097 */ 1098 int 1099 exec_init(int init_boot) 1100 { 1101 int i; 1102 1103 #ifdef DIAGNOSTIC 1104 if (!init_boot) 1105 panic("exec_init(): called with init_boot == 0"); 1106 #endif 1107 1108 /* do one-time initializations */ 1109 lockinit(&exec_lock, PWAIT, "execlck", 0, 0); 1110 1111 nexecs = nexecs_builtin; 1112 execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK); 1113 1114 /* 1115 * Fill in execsw[] and figure out the maximum size of an exec header. 1116 */ 1117 exec_maxhdrsz = 0; 1118 for(i=0; i < nexecs; i++) { 1119 execsw[i] = &execsw_builtin[i]; 1120 if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz) 1121 exec_maxhdrsz = execsw_builtin[i].es_hdrsz; 1122 } 1123 1124 return 0; 1125 1126 } 1127 #endif /* !LKM */ 1128