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