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/sysproto.h> 32 #include <sys/signalvar.h> 33 #include <sys/kernel.h> 34 #include <sys/mount.h> 35 #include <sys/mutex.h> 36 #include <sys/filedesc.h> 37 #include <sys/fcntl.h> 38 #include <sys/acct.h> 39 #include <sys/exec.h> 40 #include <sys/imgact.h> 41 #include <sys/imgact_elf.h> 42 #include <sys/wait.h> 43 #include <sys/proc.h> 44 #include <sys/pioctl.h> 45 #include <sys/malloc.h> 46 #include <sys/namei.h> 47 #include <sys/sysent.h> 48 #include <sys/shm.h> 49 #include <sys/sysctl.h> 50 #include <sys/vnode.h> 51 52 #include <vm/vm.h> 53 #include <vm/vm_param.h> 54 #include <sys/lock.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 p->p_flag |= P_EXEC; 270 PROCTREE_LOCK(PT_SHARED); 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 PROCTREE_LOCK(PT_RELEASE); 276 277 /* 278 * Implement image setuid/setgid. 279 * 280 * Don't honor setuid/setgid if the filesystem prohibits it or if 281 * the process is being traced. 282 */ 283 if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) || 284 ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) && 285 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 286 (p->p_flag & P_TRACED) == 0) { 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 } 311 312 /* 313 * Implement correct POSIX saved-id behavior. 314 */ 315 p->p_cred->p_svuid = p->p_ucred->cr_uid; 316 p->p_cred->p_svgid = p->p_ucred->cr_gid; 317 318 /* 319 * Store the vp for use in procfs 320 */ 321 if (p->p_textvp) /* release old reference */ 322 vrele(p->p_textvp); 323 VREF(ndp->ni_vp); 324 p->p_textvp = ndp->ni_vp; 325 326 /* 327 * notify others that we exec'd 328 */ 329 KNOTE(&p->p_klist, NOTE_EXEC); 330 331 /* 332 * If tracing the process, trap to debugger so breakpoints 333 * can be set before the program executes. 334 */ 335 STOPEVENT(p, S_EXEC, 0); 336 337 if (p->p_flag & P_TRACED) 338 psignal(p, SIGTRAP); 339 340 /* clear "fork but no exec" flag, as we _are_ execing */ 341 p->p_acflag &= ~AFORK; 342 343 /* Set values passed into the program in registers. */ 344 setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base, 345 imgp->ps_strings); 346 347 /* Free any previous argument cache */ 348 if (p->p_args && --p->p_args->ar_ref == 0) 349 FREE(p->p_args, M_PARGS); 350 p->p_args = NULL; 351 352 /* Cache arguments if they fit inside our allowance */ 353 i = imgp->endargs - imgp->stringbase; 354 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 355 MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, 356 M_PARGS, M_WAITOK); 357 p->p_args->ar_ref = 1; 358 p->p_args->ar_length = i; 359 bcopy(imgp->stringbase, p->p_args->ar_args, i); 360 } 361 362 exec_fail_dealloc: 363 364 /* 365 * free various allocated resources 366 */ 367 if (imgp->firstpage) 368 exec_unmap_first_page(imgp); 369 370 if (imgp->stringbase != NULL) 371 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 372 ARG_MAX + PAGE_SIZE); 373 374 if (imgp->vp) { 375 NDFREE(ndp, NDF_ONLY_PNBUF); 376 vrele(imgp->vp); 377 } 378 379 if (error == 0) 380 return (0); 381 382 exec_fail: 383 if (imgp->vmspace_destroyed) { 384 /* sorry, no more process anymore. exit gracefully */ 385 exit1(p, W_EXITCODE(0, SIGABRT)); 386 /* NOT REACHED */ 387 return(0); 388 } else { 389 return(error); 390 } 391 } 392 393 int 394 exec_map_first_page(imgp) 395 struct image_params *imgp; 396 { 397 int s, rv, i; 398 int initial_pagein; 399 vm_page_t ma[VM_INITIAL_PAGEIN]; 400 vm_object_t object; 401 402 403 if (imgp->firstpage) { 404 exec_unmap_first_page(imgp); 405 } 406 407 VOP_GETVOBJECT(imgp->vp, &object); 408 s = splvm(); 409 410 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 411 412 if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 413 initial_pagein = VM_INITIAL_PAGEIN; 414 if (initial_pagein > object->size) 415 initial_pagein = object->size; 416 for (i = 1; i < initial_pagein; i++) { 417 if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 418 if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 419 break; 420 if (ma[i]->valid) 421 break; 422 vm_page_busy(ma[i]); 423 } else { 424 ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 425 if (ma[i] == NULL) 426 break; 427 } 428 } 429 initial_pagein = i; 430 431 rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 432 ma[0] = vm_page_lookup(object, 0); 433 434 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 435 if (ma[0]) { 436 vm_page_protect(ma[0], VM_PROT_NONE); 437 vm_page_free(ma[0]); 438 } 439 splx(s); 440 return EIO; 441 } 442 } 443 444 vm_page_wire(ma[0]); 445 vm_page_wakeup(ma[0]); 446 splx(s); 447 448 pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 449 imgp->firstpage = ma[0]; 450 451 return 0; 452 } 453 454 void 455 exec_unmap_first_page(imgp) 456 struct image_params *imgp; 457 { 458 if (imgp->firstpage) { 459 pmap_kremove((vm_offset_t) imgp->image_header); 460 vm_page_unwire(imgp->firstpage, 1); 461 imgp->firstpage = NULL; 462 } 463 } 464 465 /* 466 * Destroy old address space, and allocate a new stack 467 * The new stack is only SGROWSIZ large because it is grown 468 * automatically in trap.c. 469 */ 470 int 471 exec_new_vmspace(imgp) 472 struct image_params *imgp; 473 { 474 int error; 475 struct vmspace *vmspace = imgp->proc->p_vmspace; 476 caddr_t stack_addr = (caddr_t) (USRSTACK - MAXSSIZ); 477 vm_map_t map = &vmspace->vm_map; 478 479 imgp->vmspace_destroyed = 1; 480 481 /* 482 * Blow away entire process VM, if address space not shared, 483 * otherwise, create a new VM space so that other threads are 484 * not disrupted 485 */ 486 if (vmspace->vm_refcnt == 1) { 487 if (vmspace->vm_shm) 488 shmexit(imgp->proc); 489 pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); 490 vm_map_remove(map, 0, VM_MAXUSER_ADDRESS); 491 } else { 492 vmspace_exec(imgp->proc); 493 vmspace = imgp->proc->p_vmspace; 494 map = &vmspace->vm_map; 495 } 496 497 /* Allocate a new stack */ 498 error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr, 499 (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0); 500 if (error) 501 return (error); 502 503 #ifdef __ia64__ 504 { 505 /* 506 * Allocate backing store. We really need something 507 * similar to vm_map_stack which can allow the backing 508 * store to grow upwards. This will do for now. 509 */ 510 vm_offset_t bsaddr; 511 bsaddr = USRSTACK - 2*MAXSSIZ; 512 error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr, 513 4*PAGE_SIZE, 0, 514 VM_PROT_ALL, VM_PROT_ALL, 0); 515 imgp->proc->p_md.md_bspstore = bsaddr; 516 } 517 #endif 518 519 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 520 * VM_STACK case, but they are still used to monitor the size of the 521 * process stack so we can check the stack rlimit. 522 */ 523 vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 524 vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 525 526 return(0); 527 } 528 529 /* 530 * Copy out argument and environment strings from the old process 531 * address space into the temporary string buffer. 532 */ 533 int 534 exec_extract_strings(imgp) 535 struct image_params *imgp; 536 { 537 char **argv, **envv; 538 char *argp, *envp; 539 int error; 540 size_t length; 541 542 /* 543 * extract arguments first 544 */ 545 546 argv = imgp->uap->argv; 547 548 if (argv) { 549 argp = (caddr_t) (intptr_t) fuword(argv); 550 if (argp == (caddr_t) -1) 551 return (EFAULT); 552 if (argp) 553 argv++; 554 if (imgp->argv0) 555 argp = imgp->argv0; 556 if (argp) { 557 do { 558 if (argp == (caddr_t) -1) 559 return (EFAULT); 560 if ((error = copyinstr(argp, imgp->stringp, 561 imgp->stringspace, &length))) { 562 if (error == ENAMETOOLONG) 563 return(E2BIG); 564 return (error); 565 } 566 imgp->stringspace -= length; 567 imgp->stringp += length; 568 imgp->argc++; 569 } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 570 } 571 } 572 573 imgp->endargs = imgp->stringp; 574 575 /* 576 * extract environment strings 577 */ 578 579 envv = imgp->uap->envv; 580 581 if (envv) { 582 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 583 if (envp == (caddr_t) -1) 584 return (EFAULT); 585 if ((error = copyinstr(envp, imgp->stringp, 586 imgp->stringspace, &length))) { 587 if (error == ENAMETOOLONG) 588 return(E2BIG); 589 return (error); 590 } 591 imgp->stringspace -= length; 592 imgp->stringp += length; 593 imgp->envc++; 594 } 595 } 596 597 return (0); 598 } 599 600 /* 601 * Copy strings out to the new process address space, constructing 602 * new arg and env vector tables. Return a pointer to the base 603 * so that it can be used as the initial stack pointer. 604 */ 605 register_t * 606 exec_copyout_strings(imgp) 607 struct image_params *imgp; 608 { 609 int argc, envc; 610 char **vectp; 611 char *stringp, *destp; 612 register_t *stack_base; 613 struct ps_strings *arginfo; 614 int szsigcode; 615 616 /* 617 * Calculate string base and vector table pointers. 618 * Also deal with signal trampoline code for this exec type. 619 */ 620 arginfo = (struct ps_strings *)PS_STRINGS; 621 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 622 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 623 roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 624 625 /* 626 * install sigcode 627 */ 628 if (szsigcode) 629 copyout(imgp->proc->p_sysent->sv_sigcode, 630 ((caddr_t)arginfo - szsigcode), szsigcode); 631 632 /* 633 * If we have a valid auxargs ptr, prepare some room 634 * on the stack. 635 */ 636 if (imgp->auxargs) { 637 /* 638 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 639 * lower compatibility. 640 */ 641 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 642 : (AT_COUNT * 2); 643 /* 644 * The '+ 2' is for the null pointers at the end of each of 645 * the arg and env vector sets,and imgp->auxarg_size is room 646 * for argument of Runtime loader. 647 */ 648 vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 + 649 imgp->auxarg_size) * sizeof(char *)); 650 651 } else 652 /* 653 * The '+ 2' is for the null pointers at the end of each of 654 * the arg and env vector sets 655 */ 656 vectp = (char **) 657 (destp - (imgp->argc + imgp->envc + 2) * sizeof(char *)); 658 659 /* 660 * vectp also becomes our initial stack base 661 */ 662 stack_base = (register_t *)vectp; 663 664 stringp = imgp->stringbase; 665 argc = imgp->argc; 666 envc = imgp->envc; 667 668 /* 669 * Copy out strings - arguments and environment. 670 */ 671 copyout(stringp, destp, ARG_MAX - imgp->stringspace); 672 673 /* 674 * Fill in "ps_strings" struct for ps, w, etc. 675 */ 676 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 677 suword(&arginfo->ps_nargvstr, argc); 678 679 /* 680 * Fill in argument portion of vector table. 681 */ 682 for (; argc > 0; --argc) { 683 suword(vectp++, (long)(intptr_t)destp); 684 while (*stringp++ != 0) 685 destp++; 686 destp++; 687 } 688 689 /* a null vector table pointer seperates the argp's from the envp's */ 690 suword(vectp++, 0); 691 692 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 693 suword(&arginfo->ps_nenvstr, envc); 694 695 /* 696 * Fill in environment portion of vector table. 697 */ 698 for (; envc > 0; --envc) { 699 suword(vectp++, (long)(intptr_t)destp); 700 while (*stringp++ != 0) 701 destp++; 702 destp++; 703 } 704 705 /* end of vector table is a null pointer */ 706 suword(vectp, 0); 707 708 return (stack_base); 709 } 710 711 /* 712 * Check permissions of file to execute. 713 * Called with imgp->vp locked. 714 * Return 0 for success or error code on failure. 715 */ 716 int 717 exec_check_permissions(imgp) 718 struct image_params *imgp; 719 { 720 struct proc *p = imgp->proc; 721 struct vnode *vp = imgp->vp; 722 struct vattr *attr = imgp->attr; 723 int error; 724 725 /* Get file attributes */ 726 error = VOP_GETATTR(vp, attr, p->p_ucred, p); 727 if (error) 728 return (error); 729 730 /* 731 * 1) Check if file execution is disabled for the filesystem that this 732 * file resides on. 733 * 2) Insure that at least one execute bit is on - otherwise root 734 * will always succeed, and we don't want to happen unless the 735 * file really is executable. 736 * 3) Insure that the file is a regular file. 737 */ 738 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 739 ((attr->va_mode & 0111) == 0) || 740 (attr->va_type != VREG)) { 741 return (EACCES); 742 } 743 744 /* 745 * Zero length files can't be exec'd 746 */ 747 if (attr->va_size == 0) 748 return (ENOEXEC); 749 750 /* 751 * Check for execute permission to file based on current credentials. 752 */ 753 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 754 if (error) 755 return (error); 756 757 /* 758 * Check number of open-for-writes on the file and deny execution 759 * if there are any. 760 */ 761 if (vp->v_writecount) 762 return (ETXTBSY); 763 764 /* 765 * Call filesystem specific open routine (which does nothing in the 766 * general case). 767 */ 768 error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 769 if (error) 770 return (error); 771 772 return (0); 773 } 774 775 /* 776 * Exec handler registration 777 */ 778 int 779 exec_register(execsw_arg) 780 const struct execsw *execsw_arg; 781 { 782 const struct execsw **es, **xs, **newexecsw; 783 int count = 2; /* New slot and trailing NULL */ 784 785 if (execsw) 786 for (es = execsw; *es; es++) 787 count++; 788 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 789 if (newexecsw == NULL) 790 return ENOMEM; 791 xs = newexecsw; 792 if (execsw) 793 for (es = execsw; *es; es++) 794 *xs++ = *es; 795 *xs++ = execsw_arg; 796 *xs = NULL; 797 if (execsw) 798 free(execsw, M_TEMP); 799 execsw = newexecsw; 800 return 0; 801 } 802 803 int 804 exec_unregister(execsw_arg) 805 const struct execsw *execsw_arg; 806 { 807 const struct execsw **es, **xs, **newexecsw; 808 int count = 1; 809 810 if (execsw == NULL) 811 panic("unregister with no handlers left?\n"); 812 813 for (es = execsw; *es; es++) { 814 if (*es == execsw_arg) 815 break; 816 } 817 if (*es == NULL) 818 return ENOENT; 819 for (es = execsw; *es; es++) 820 if (*es != execsw_arg) 821 count++; 822 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 823 if (newexecsw == NULL) 824 return ENOMEM; 825 xs = newexecsw; 826 for (es = execsw; *es; es++) 827 if (*es != execsw_arg) 828 *xs++ = *es; 829 *xs = NULL; 830 if (execsw) 831 free(execsw, M_TEMP); 832 execsw = newexecsw; 833 return 0; 834 } 835