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