1 /* 2 * Copyright (c) 1993, David Greenman 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/lock.h> 32 #include <sys/mutex.h> 33 #include <sys/sysproto.h> 34 #include <sys/signalvar.h> 35 #include <sys/kernel.h> 36 #include <sys/mount.h> 37 #include <sys/filedesc.h> 38 #include <sys/fcntl.h> 39 #include <sys/acct.h> 40 #include <sys/exec.h> 41 #include <sys/imgact.h> 42 #include <sys/imgact_elf.h> 43 #include <sys/wait.h> 44 #include <sys/proc.h> 45 #include <sys/pioctl.h> 46 #include <sys/malloc.h> 47 #include <sys/namei.h> 48 #include <sys/sysent.h> 49 #include <sys/shm.h> 50 #include <sys/sysctl.h> 51 #include <sys/vnode.h> 52 53 #include <vm/vm.h> 54 #include <vm/vm_param.h> 55 #include <vm/pmap.h> 56 #include <vm/vm_page.h> 57 #include <vm/vm_map.h> 58 #include <vm/vm_kern.h> 59 #include <vm/vm_extern.h> 60 #include <vm/vm_object.h> 61 #include <vm/vm_pager.h> 62 63 #include <machine/reg.h> 64 65 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 66 67 static register_t *exec_copyout_strings __P((struct image_params *)); 68 69 /* XXX This should be vm_size_t. */ 70 static u_long ps_strings = PS_STRINGS; 71 SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, ""); 72 73 /* XXX This should be vm_size_t. */ 74 static u_long usrstack = USRSTACK; 75 SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, ""); 76 77 u_long ps_arg_cache_limit = PAGE_SIZE / 16; 78 SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 79 &ps_arg_cache_limit, 0, ""); 80 81 int ps_argsopen = 1; 82 SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 83 84 /* 85 * Each of the items is a pointer to a `const struct execsw', hence the 86 * double pointer here. 87 */ 88 static const struct execsw **execsw; 89 90 #ifndef _SYS_SYSPROTO_H_ 91 struct execve_args { 92 char *fname; 93 char **argv; 94 char **envv; 95 }; 96 #endif 97 98 /* 99 * execve() system call. 100 */ 101 int 102 execve(p, uap) 103 struct proc *p; 104 register struct execve_args *uap; 105 { 106 struct nameidata nd, *ndp; 107 register_t *stack_base; 108 int error, len, i; 109 struct image_params image_params, *imgp; 110 struct vattr attr; 111 int (*img_first) __P((struct image_params *)); 112 113 imgp = &image_params; 114 115 /* 116 * Initialize part of the common data 117 */ 118 imgp->proc = p; 119 imgp->uap = uap; 120 imgp->attr = &attr; 121 imgp->argc = imgp->envc = 0; 122 imgp->argv0 = NULL; 123 imgp->entry_addr = 0; 124 imgp->vmspace_destroyed = 0; 125 imgp->interpreted = 0; 126 imgp->interpreter_name[0] = '\0'; 127 imgp->auxargs = NULL; 128 imgp->vp = NULL; 129 imgp->firstpage = NULL; 130 imgp->ps_strings = 0; 131 imgp->auxarg_size = 0; 132 133 /* 134 * Allocate temporary demand zeroed space for argument and 135 * environment strings 136 */ 137 imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 138 if (imgp->stringbase == NULL) { 139 error = ENOMEM; 140 goto exec_fail; 141 } 142 imgp->stringp = imgp->stringbase; 143 imgp->stringspace = ARG_MAX; 144 imgp->image_header = imgp->stringbase + ARG_MAX; 145 146 /* 147 * Translate the file name. namei() returns a vnode pointer 148 * in ni_vp amoung other things. 149 */ 150 ndp = &nd; 151 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 152 UIO_USERSPACE, uap->fname, p); 153 154 interpret: 155 156 error = namei(ndp); 157 if (error) { 158 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 159 ARG_MAX + PAGE_SIZE); 160 goto exec_fail; 161 } 162 163 imgp->vp = ndp->ni_vp; 164 imgp->fname = uap->fname; 165 166 /* 167 * Check file permissions (also 'opens' file) 168 */ 169 error = exec_check_permissions(imgp); 170 if (error) { 171 VOP_UNLOCK(imgp->vp, 0, p); 172 goto exec_fail_dealloc; 173 } 174 175 error = exec_map_first_page(imgp); 176 VOP_UNLOCK(imgp->vp, 0, p); 177 if (error) 178 goto exec_fail_dealloc; 179 180 /* 181 * If the current process has a special image activator it 182 * wants to try first, call it. For example, emulating shell 183 * scripts differently. 184 */ 185 error = -1; 186 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 187 error = img_first(imgp); 188 189 /* 190 * Loop through the list of image activators, calling each one. 191 * An activator returns -1 if there is no match, 0 on success, 192 * and an error otherwise. 193 */ 194 for (i = 0; error == -1 && execsw[i]; ++i) { 195 if (execsw[i]->ex_imgact == NULL || 196 execsw[i]->ex_imgact == img_first) { 197 continue; 198 } 199 error = (*execsw[i]->ex_imgact)(imgp); 200 } 201 202 if (error) { 203 if (error == -1) 204 error = ENOEXEC; 205 goto exec_fail_dealloc; 206 } 207 208 /* 209 * Special interpreter operation, cleanup and loop up to try to 210 * activate the interpreter. 211 */ 212 if (imgp->interpreted) { 213 exec_unmap_first_page(imgp); 214 /* free name buffer and old vnode */ 215 NDFREE(ndp, NDF_ONLY_PNBUF); 216 vrele(ndp->ni_vp); 217 /* set new name to that of the interpreter */ 218 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 219 UIO_SYSSPACE, imgp->interpreter_name, p); 220 goto interpret; 221 } 222 223 /* 224 * Copy out strings (args and env) and initialize stack base 225 */ 226 stack_base = exec_copyout_strings(imgp); 227 p->p_vmspace->vm_minsaddr = (char *)stack_base; 228 229 /* 230 * If custom stack fixup routine present for this process 231 * let it do the stack setup. 232 * Else stuff argument count as first item on stack 233 */ 234 if (p->p_sysent->sv_fixup) 235 (*p->p_sysent->sv_fixup)(&stack_base, imgp); 236 else 237 suword(--stack_base, imgp->argc); 238 239 /* 240 * For security and other reasons, the file descriptor table cannot 241 * be shared after an exec. 242 */ 243 if (p->p_fd->fd_refcnt > 1) { 244 struct filedesc *tmp; 245 246 tmp = fdcopy(p); 247 fdfree(p); 248 p->p_fd = tmp; 249 } 250 251 /* Stop profiling */ 252 stopprofclock(p); 253 254 /* close files on exec */ 255 fdcloseexec(p); 256 257 /* reset caught signals */ 258 execsigs(p); 259 260 /* name this process - nameiexec(p, ndp) */ 261 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 262 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 263 p->p_comm[len] = 0; 264 265 /* 266 * mark as execed, wakeup the process that vforked (if any) and tell 267 * it that it now has its own resources back 268 */ 269 PROC_LOCK(p); 270 p->p_flag |= P_EXEC; 271 if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 272 p->p_flag &= ~P_PPWAIT; 273 wakeup((caddr_t)p->p_pptr); 274 } 275 276 /* 277 * Implement image setuid/setgid. 278 * 279 * Don't honor setuid/setgid if the filesystem prohibits it or if 280 * the process is being traced. 281 */ 282 if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) || 283 ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) && 284 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 285 (p->p_flag & P_TRACED) == 0) { 286 PROC_UNLOCK(p); 287 /* 288 * Turn off syscall tracing for set-id programs, except for 289 * root. 290 */ 291 if (p->p_tracep && suser(p)) { 292 p->p_traceflag = 0; 293 vrele(p->p_tracep); 294 p->p_tracep = NULL; 295 } 296 /* 297 * Set the new credentials. 298 */ 299 p->p_ucred = crcopy(p->p_ucred); 300 if (attr.va_mode & VSUID) 301 change_euid(p, attr.va_uid); 302 if (attr.va_mode & VSGID) 303 p->p_ucred->cr_gid = attr.va_gid; 304 setsugid(p); 305 setugidsafety(p); 306 } else { 307 if (p->p_ucred->cr_uid == p->p_cred->p_ruid && 308 p->p_ucred->cr_gid == p->p_cred->p_rgid) 309 p->p_flag &= ~P_SUGID; 310 PROC_UNLOCK(p); 311 } 312 313 /* 314 * Implement correct POSIX saved-id behavior. 315 */ 316 p->p_cred->p_svuid = p->p_ucred->cr_uid; 317 p->p_cred->p_svgid = p->p_ucred->cr_gid; 318 319 /* 320 * Store the vp for use in procfs 321 */ 322 if (p->p_textvp) /* release old reference */ 323 vrele(p->p_textvp); 324 VREF(ndp->ni_vp); 325 p->p_textvp = ndp->ni_vp; 326 327 /* 328 * notify others that we exec'd 329 */ 330 PROC_LOCK(p); 331 KNOTE(&p->p_klist, NOTE_EXEC); 332 333 /* 334 * If tracing the process, trap to debugger so breakpoints 335 * can be set before the program executes. 336 */ 337 _STOPEVENT(p, S_EXEC, 0); 338 339 if (p->p_flag & P_TRACED) 340 psignal(p, SIGTRAP); 341 342 /* clear "fork but no exec" flag, as we _are_ execing */ 343 p->p_acflag &= ~AFORK; 344 345 /* Set values passed into the program in registers. */ 346 setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base, 347 imgp->ps_strings); 348 349 /* Free any previous argument cache */ 350 if (p->p_args && --p->p_args->ar_ref == 0) 351 FREE(p->p_args, M_PARGS); 352 p->p_args = NULL; 353 354 /* Cache arguments if they fit inside our allowance */ 355 i = imgp->endargs - imgp->stringbase; 356 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 357 PROC_UNLOCK(p); 358 MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, 359 M_PARGS, M_WAITOK); 360 KASSERT(p->p_args != NULL, ("malloc of p_args failed")); 361 PROC_LOCK(p); 362 p->p_args->ar_ref = 1; 363 p->p_args->ar_length = i; 364 bcopy(imgp->stringbase, p->p_args->ar_args, i); 365 } 366 PROC_UNLOCK(p); 367 368 exec_fail_dealloc: 369 370 /* 371 * free various allocated resources 372 */ 373 if (imgp->firstpage) 374 exec_unmap_first_page(imgp); 375 376 if (imgp->stringbase != NULL) 377 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 378 ARG_MAX + PAGE_SIZE); 379 380 if (imgp->vp) { 381 NDFREE(ndp, NDF_ONLY_PNBUF); 382 vrele(imgp->vp); 383 } 384 385 if (error == 0) 386 return (0); 387 388 exec_fail: 389 if (imgp->vmspace_destroyed) { 390 /* sorry, no more process anymore. exit gracefully */ 391 exit1(p, W_EXITCODE(0, SIGABRT)); 392 /* NOT REACHED */ 393 return(0); 394 } else { 395 return(error); 396 } 397 } 398 399 int 400 exec_map_first_page(imgp) 401 struct image_params *imgp; 402 { 403 int s, rv, i; 404 int initial_pagein; 405 vm_page_t ma[VM_INITIAL_PAGEIN]; 406 vm_object_t object; 407 408 409 if (imgp->firstpage) { 410 exec_unmap_first_page(imgp); 411 } 412 413 VOP_GETVOBJECT(imgp->vp, &object); 414 s = splvm(); 415 416 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 417 418 if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 419 initial_pagein = VM_INITIAL_PAGEIN; 420 if (initial_pagein > object->size) 421 initial_pagein = object->size; 422 for (i = 1; i < initial_pagein; i++) { 423 if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 424 if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 425 break; 426 if (ma[i]->valid) 427 break; 428 vm_page_busy(ma[i]); 429 } else { 430 ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 431 if (ma[i] == NULL) 432 break; 433 } 434 } 435 initial_pagein = i; 436 437 rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 438 ma[0] = vm_page_lookup(object, 0); 439 440 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 441 if (ma[0]) { 442 vm_page_protect(ma[0], VM_PROT_NONE); 443 vm_page_free(ma[0]); 444 } 445 splx(s); 446 return EIO; 447 } 448 } 449 450 vm_page_wire(ma[0]); 451 vm_page_wakeup(ma[0]); 452 splx(s); 453 454 pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 455 imgp->firstpage = ma[0]; 456 457 return 0; 458 } 459 460 void 461 exec_unmap_first_page(imgp) 462 struct image_params *imgp; 463 { 464 if (imgp->firstpage) { 465 pmap_kremove((vm_offset_t) imgp->image_header); 466 vm_page_unwire(imgp->firstpage, 1); 467 imgp->firstpage = NULL; 468 } 469 } 470 471 /* 472 * Destroy old address space, and allocate a new stack 473 * The new stack is only SGROWSIZ large because it is grown 474 * automatically in trap.c. 475 */ 476 int 477 exec_new_vmspace(imgp) 478 struct image_params *imgp; 479 { 480 int error; 481 struct vmspace *vmspace = imgp->proc->p_vmspace; 482 caddr_t stack_addr = (caddr_t) (USRSTACK - MAXSSIZ); 483 vm_map_t map = &vmspace->vm_map; 484 485 imgp->vmspace_destroyed = 1; 486 487 /* 488 * Blow away entire process VM, if address space not shared, 489 * otherwise, create a new VM space so that other threads are 490 * not disrupted 491 */ 492 if (vmspace->vm_refcnt == 1) { 493 if (vmspace->vm_shm) 494 shmexit(imgp->proc); 495 pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); 496 vm_map_remove(map, 0, VM_MAXUSER_ADDRESS); 497 } else { 498 vmspace_exec(imgp->proc); 499 vmspace = imgp->proc->p_vmspace; 500 map = &vmspace->vm_map; 501 } 502 503 /* Allocate a new stack */ 504 error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr, 505 (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0); 506 if (error) 507 return (error); 508 509 #ifdef __ia64__ 510 { 511 /* 512 * Allocate backing store. We really need something 513 * similar to vm_map_stack which can allow the backing 514 * store to grow upwards. This will do for now. 515 */ 516 vm_offset_t bsaddr; 517 bsaddr = USRSTACK - 2*MAXSSIZ; 518 error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr, 519 4*PAGE_SIZE, 0, 520 VM_PROT_ALL, VM_PROT_ALL, 0); 521 imgp->proc->p_md.md_bspstore = bsaddr; 522 } 523 #endif 524 525 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 526 * VM_STACK case, but they are still used to monitor the size of the 527 * process stack so we can check the stack rlimit. 528 */ 529 vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 530 vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 531 532 return(0); 533 } 534 535 /* 536 * Copy out argument and environment strings from the old process 537 * address space into the temporary string buffer. 538 */ 539 int 540 exec_extract_strings(imgp) 541 struct image_params *imgp; 542 { 543 char **argv, **envv; 544 char *argp, *envp; 545 int error; 546 size_t length; 547 548 /* 549 * extract arguments first 550 */ 551 552 argv = imgp->uap->argv; 553 554 if (argv) { 555 argp = (caddr_t) (intptr_t) fuword(argv); 556 if (argp == (caddr_t) -1) 557 return (EFAULT); 558 if (argp) 559 argv++; 560 if (imgp->argv0) 561 argp = imgp->argv0; 562 if (argp) { 563 do { 564 if (argp == (caddr_t) -1) 565 return (EFAULT); 566 if ((error = copyinstr(argp, imgp->stringp, 567 imgp->stringspace, &length))) { 568 if (error == ENAMETOOLONG) 569 return(E2BIG); 570 return (error); 571 } 572 imgp->stringspace -= length; 573 imgp->stringp += length; 574 imgp->argc++; 575 } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 576 } 577 } 578 579 imgp->endargs = imgp->stringp; 580 581 /* 582 * extract environment strings 583 */ 584 585 envv = imgp->uap->envv; 586 587 if (envv) { 588 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 589 if (envp == (caddr_t) -1) 590 return (EFAULT); 591 if ((error = copyinstr(envp, imgp->stringp, 592 imgp->stringspace, &length))) { 593 if (error == ENAMETOOLONG) 594 return(E2BIG); 595 return (error); 596 } 597 imgp->stringspace -= length; 598 imgp->stringp += length; 599 imgp->envc++; 600 } 601 } 602 603 return (0); 604 } 605 606 /* 607 * Copy strings out to the new process address space, constructing 608 * new arg and env vector tables. Return a pointer to the base 609 * so that it can be used as the initial stack pointer. 610 */ 611 register_t * 612 exec_copyout_strings(imgp) 613 struct image_params *imgp; 614 { 615 int argc, envc; 616 char **vectp; 617 char *stringp, *destp; 618 register_t *stack_base; 619 struct ps_strings *arginfo; 620 int szsigcode; 621 622 /* 623 * Calculate string base and vector table pointers. 624 * Also deal with signal trampoline code for this exec type. 625 */ 626 arginfo = (struct ps_strings *)PS_STRINGS; 627 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 628 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 629 roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 630 631 /* 632 * install sigcode 633 */ 634 if (szsigcode) 635 copyout(imgp->proc->p_sysent->sv_sigcode, 636 ((caddr_t)arginfo - szsigcode), szsigcode); 637 638 /* 639 * If we have a valid auxargs ptr, prepare some room 640 * on the stack. 641 */ 642 if (imgp->auxargs) { 643 /* 644 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 645 * lower compatibility. 646 */ 647 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 648 : (AT_COUNT * 2); 649 /* 650 * The '+ 2' is for the null pointers at the end of each of 651 * the arg and env vector sets,and imgp->auxarg_size is room 652 * for argument of Runtime loader. 653 */ 654 vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 + 655 imgp->auxarg_size) * sizeof(char *)); 656 657 } else 658 /* 659 * The '+ 2' is for the null pointers at the end of each of 660 * the arg and env vector sets 661 */ 662 vectp = (char **) 663 (destp - (imgp->argc + imgp->envc + 2) * sizeof(char *)); 664 665 /* 666 * vectp also becomes our initial stack base 667 */ 668 stack_base = (register_t *)vectp; 669 670 stringp = imgp->stringbase; 671 argc = imgp->argc; 672 envc = imgp->envc; 673 674 /* 675 * Copy out strings - arguments and environment. 676 */ 677 copyout(stringp, destp, ARG_MAX - imgp->stringspace); 678 679 /* 680 * Fill in "ps_strings" struct for ps, w, etc. 681 */ 682 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 683 suword(&arginfo->ps_nargvstr, argc); 684 685 /* 686 * Fill in argument portion of vector table. 687 */ 688 for (; argc > 0; --argc) { 689 suword(vectp++, (long)(intptr_t)destp); 690 while (*stringp++ != 0) 691 destp++; 692 destp++; 693 } 694 695 /* a null vector table pointer separates the argp's from the envp's */ 696 suword(vectp++, 0); 697 698 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 699 suword(&arginfo->ps_nenvstr, envc); 700 701 /* 702 * Fill in environment portion of vector table. 703 */ 704 for (; envc > 0; --envc) { 705 suword(vectp++, (long)(intptr_t)destp); 706 while (*stringp++ != 0) 707 destp++; 708 destp++; 709 } 710 711 /* end of vector table is a null pointer */ 712 suword(vectp, 0); 713 714 return (stack_base); 715 } 716 717 /* 718 * Check permissions of file to execute. 719 * Called with imgp->vp locked. 720 * Return 0 for success or error code on failure. 721 */ 722 int 723 exec_check_permissions(imgp) 724 struct image_params *imgp; 725 { 726 struct proc *p = imgp->proc; 727 struct vnode *vp = imgp->vp; 728 struct vattr *attr = imgp->attr; 729 int error; 730 731 /* Get file attributes */ 732 error = VOP_GETATTR(vp, attr, p->p_ucred, p); 733 if (error) 734 return (error); 735 736 /* 737 * 1) Check if file execution is disabled for the filesystem that this 738 * file resides on. 739 * 2) Insure that at least one execute bit is on - otherwise root 740 * will always succeed, and we don't want to happen unless the 741 * file really is executable. 742 * 3) Insure that the file is a regular file. 743 */ 744 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 745 ((attr->va_mode & 0111) == 0) || 746 (attr->va_type != VREG)) { 747 return (EACCES); 748 } 749 750 /* 751 * Zero length files can't be exec'd 752 */ 753 if (attr->va_size == 0) 754 return (ENOEXEC); 755 756 /* 757 * Check for execute permission to file based on current credentials. 758 */ 759 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 760 if (error) 761 return (error); 762 763 /* 764 * Check number of open-for-writes on the file and deny execution 765 * if there are any. 766 */ 767 if (vp->v_writecount) 768 return (ETXTBSY); 769 770 /* 771 * Call filesystem specific open routine (which does nothing in the 772 * general case). 773 */ 774 error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 775 if (error) 776 return (error); 777 778 return (0); 779 } 780 781 /* 782 * Exec handler registration 783 */ 784 int 785 exec_register(execsw_arg) 786 const struct execsw *execsw_arg; 787 { 788 const struct execsw **es, **xs, **newexecsw; 789 int count = 2; /* New slot and trailing NULL */ 790 791 if (execsw) 792 for (es = execsw; *es; es++) 793 count++; 794 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 795 if (newexecsw == NULL) 796 return ENOMEM; 797 xs = newexecsw; 798 if (execsw) 799 for (es = execsw; *es; es++) 800 *xs++ = *es; 801 *xs++ = execsw_arg; 802 *xs = NULL; 803 if (execsw) 804 free(execsw, M_TEMP); 805 execsw = newexecsw; 806 return 0; 807 } 808 809 int 810 exec_unregister(execsw_arg) 811 const struct execsw *execsw_arg; 812 { 813 const struct execsw **es, **xs, **newexecsw; 814 int count = 1; 815 816 if (execsw == NULL) 817 panic("unregister with no handlers left?\n"); 818 819 for (es = execsw; *es; es++) { 820 if (*es == execsw_arg) 821 break; 822 } 823 if (*es == NULL) 824 return ENOENT; 825 for (es = execsw; *es; es++) 826 if (*es != execsw_arg) 827 count++; 828 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 829 if (newexecsw == NULL) 830 return ENOMEM; 831 xs = newexecsw; 832 for (es = execsw; *es; es++) 833 if (*es != execsw_arg) 834 *xs++ = *es; 835 *xs = NULL; 836 if (execsw) 837 free(execsw, M_TEMP); 838 execsw = newexecsw; 839 return 0; 840 } 841