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 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 mtx_lock(&vm_mtx); 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 mtx_unlock(&vm_mtx); 446 return EIO; 447 } 448 } 449 450 vm_page_wire(ma[0]); 451 vm_page_wakeup(ma[0]); 452 453 pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 454 imgp->firstpage = ma[0]; 455 456 mtx_unlock(&vm_mtx); 457 return 0; 458 } 459 460 void 461 exec_unmap_first_page(imgp) 462 struct image_params *imgp; 463 { 464 465 if (imgp->firstpage) { 466 mtx_lock(&vm_mtx); 467 pmap_kremove((vm_offset_t) imgp->image_header); 468 vm_page_unwire(imgp->firstpage, 1); 469 mtx_unlock(&vm_mtx); 470 imgp->firstpage = NULL; 471 } 472 } 473 474 /* 475 * Destroy old address space, and allocate a new stack 476 * The new stack is only SGROWSIZ large because it is grown 477 * automatically in trap.c. 478 */ 479 int 480 exec_new_vmspace(imgp) 481 struct image_params *imgp; 482 { 483 int error; 484 struct vmspace *vmspace = imgp->proc->p_vmspace; 485 caddr_t stack_addr = (caddr_t) (USRSTACK - MAXSSIZ); 486 vm_map_t map = &vmspace->vm_map; 487 488 mtx_assert(&vm_mtx, MA_OWNED); 489 imgp->vmspace_destroyed = 1; 490 491 /* 492 * Blow away entire process VM, if address space not shared, 493 * otherwise, create a new VM space so that other threads are 494 * not disrupted 495 */ 496 if (vmspace->vm_refcnt == 1) { 497 if (vmspace->vm_shm) 498 shmexit(imgp->proc); 499 pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); 500 vm_map_remove(map, 0, VM_MAXUSER_ADDRESS); 501 } else { 502 vmspace_exec(imgp->proc); 503 vmspace = imgp->proc->p_vmspace; 504 map = &vmspace->vm_map; 505 } 506 507 /* Allocate a new stack */ 508 error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr, 509 (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0); 510 if (error) 511 return (error); 512 513 #ifdef __ia64__ 514 { 515 /* 516 * Allocate backing store. We really need something 517 * similar to vm_map_stack which can allow the backing 518 * store to grow upwards. This will do for now. 519 */ 520 vm_offset_t bsaddr; 521 bsaddr = USRSTACK - 2*MAXSSIZ; 522 error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr, 523 4*PAGE_SIZE, 0, 524 VM_PROT_ALL, VM_PROT_ALL, 0); 525 imgp->proc->p_md.md_bspstore = bsaddr; 526 } 527 #endif 528 529 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 530 * VM_STACK case, but they are still used to monitor the size of the 531 * process stack so we can check the stack rlimit. 532 */ 533 vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 534 vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 535 536 return(0); 537 } 538 539 /* 540 * Copy out argument and environment strings from the old process 541 * address space into the temporary string buffer. 542 */ 543 int 544 exec_extract_strings(imgp) 545 struct image_params *imgp; 546 { 547 char **argv, **envv; 548 char *argp, *envp; 549 int error; 550 size_t length; 551 552 /* 553 * extract arguments first 554 */ 555 556 argv = imgp->uap->argv; 557 558 if (argv) { 559 argp = (caddr_t) (intptr_t) fuword(argv); 560 if (argp == (caddr_t) -1) 561 return (EFAULT); 562 if (argp) 563 argv++; 564 if (imgp->argv0) 565 argp = imgp->argv0; 566 if (argp) { 567 do { 568 if (argp == (caddr_t) -1) 569 return (EFAULT); 570 if ((error = copyinstr(argp, imgp->stringp, 571 imgp->stringspace, &length))) { 572 if (error == ENAMETOOLONG) 573 return(E2BIG); 574 return (error); 575 } 576 imgp->stringspace -= length; 577 imgp->stringp += length; 578 imgp->argc++; 579 } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 580 } 581 } 582 583 imgp->endargs = imgp->stringp; 584 585 /* 586 * extract environment strings 587 */ 588 589 envv = imgp->uap->envv; 590 591 if (envv) { 592 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 593 if (envp == (caddr_t) -1) 594 return (EFAULT); 595 if ((error = copyinstr(envp, imgp->stringp, 596 imgp->stringspace, &length))) { 597 if (error == ENAMETOOLONG) 598 return(E2BIG); 599 return (error); 600 } 601 imgp->stringspace -= length; 602 imgp->stringp += length; 603 imgp->envc++; 604 } 605 } 606 607 return (0); 608 } 609 610 /* 611 * Copy strings out to the new process address space, constructing 612 * new arg and env vector tables. Return a pointer to the base 613 * so that it can be used as the initial stack pointer. 614 */ 615 register_t * 616 exec_copyout_strings(imgp) 617 struct image_params *imgp; 618 { 619 int argc, envc; 620 char **vectp; 621 char *stringp, *destp; 622 register_t *stack_base; 623 struct ps_strings *arginfo; 624 int szsigcode; 625 626 /* 627 * Calculate string base and vector table pointers. 628 * Also deal with signal trampoline code for this exec type. 629 */ 630 arginfo = (struct ps_strings *)PS_STRINGS; 631 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 632 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 633 roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 634 635 /* 636 * install sigcode 637 */ 638 if (szsigcode) 639 copyout(imgp->proc->p_sysent->sv_sigcode, 640 ((caddr_t)arginfo - szsigcode), szsigcode); 641 642 /* 643 * If we have a valid auxargs ptr, prepare some room 644 * on the stack. 645 */ 646 if (imgp->auxargs) { 647 /* 648 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 649 * lower compatibility. 650 */ 651 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size 652 : (AT_COUNT * 2); 653 /* 654 * The '+ 2' is for the null pointers at the end of each of 655 * the arg and env vector sets,and imgp->auxarg_size is room 656 * for argument of Runtime loader. 657 */ 658 vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 + 659 imgp->auxarg_size) * sizeof(char *)); 660 661 } else 662 /* 663 * The '+ 2' is for the null pointers at the end of each of 664 * the arg and env vector sets 665 */ 666 vectp = (char **) 667 (destp - (imgp->argc + imgp->envc + 2) * sizeof(char *)); 668 669 /* 670 * vectp also becomes our initial stack base 671 */ 672 stack_base = (register_t *)vectp; 673 674 stringp = imgp->stringbase; 675 argc = imgp->argc; 676 envc = imgp->envc; 677 678 /* 679 * Copy out strings - arguments and environment. 680 */ 681 copyout(stringp, destp, ARG_MAX - imgp->stringspace); 682 683 /* 684 * Fill in "ps_strings" struct for ps, w, etc. 685 */ 686 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 687 suword(&arginfo->ps_nargvstr, argc); 688 689 /* 690 * Fill in argument portion of vector table. 691 */ 692 for (; argc > 0; --argc) { 693 suword(vectp++, (long)(intptr_t)destp); 694 while (*stringp++ != 0) 695 destp++; 696 destp++; 697 } 698 699 /* a null vector table pointer separates the argp's from the envp's */ 700 suword(vectp++, 0); 701 702 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 703 suword(&arginfo->ps_nenvstr, envc); 704 705 /* 706 * Fill in environment portion of vector table. 707 */ 708 for (; envc > 0; --envc) { 709 suword(vectp++, (long)(intptr_t)destp); 710 while (*stringp++ != 0) 711 destp++; 712 destp++; 713 } 714 715 /* end of vector table is a null pointer */ 716 suword(vectp, 0); 717 718 return (stack_base); 719 } 720 721 /* 722 * Check permissions of file to execute. 723 * Called with imgp->vp locked. 724 * Return 0 for success or error code on failure. 725 */ 726 int 727 exec_check_permissions(imgp) 728 struct image_params *imgp; 729 { 730 struct proc *p = imgp->proc; 731 struct vnode *vp = imgp->vp; 732 struct vattr *attr = imgp->attr; 733 int error; 734 735 /* Get file attributes */ 736 error = VOP_GETATTR(vp, attr, p->p_ucred, p); 737 if (error) 738 return (error); 739 740 /* 741 * 1) Check if file execution is disabled for the filesystem that this 742 * file resides on. 743 * 2) Insure that at least one execute bit is on - otherwise root 744 * will always succeed, and we don't want to happen unless the 745 * file really is executable. 746 * 3) Insure that the file is a regular file. 747 */ 748 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 749 ((attr->va_mode & 0111) == 0) || 750 (attr->va_type != VREG)) { 751 return (EACCES); 752 } 753 754 /* 755 * Zero length files can't be exec'd 756 */ 757 if (attr->va_size == 0) 758 return (ENOEXEC); 759 760 /* 761 * Check for execute permission to file based on current credentials. 762 */ 763 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 764 if (error) 765 return (error); 766 767 /* 768 * Check number of open-for-writes on the file and deny execution 769 * if there are any. 770 */ 771 if (vp->v_writecount) 772 return (ETXTBSY); 773 774 /* 775 * Call filesystem specific open routine (which does nothing in the 776 * general case). 777 */ 778 error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 779 if (error) 780 return (error); 781 782 return (0); 783 } 784 785 /* 786 * Exec handler registration 787 */ 788 int 789 exec_register(execsw_arg) 790 const struct execsw *execsw_arg; 791 { 792 const struct execsw **es, **xs, **newexecsw; 793 int count = 2; /* New slot and trailing NULL */ 794 795 if (execsw) 796 for (es = execsw; *es; es++) 797 count++; 798 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 799 if (newexecsw == NULL) 800 return ENOMEM; 801 xs = newexecsw; 802 if (execsw) 803 for (es = execsw; *es; es++) 804 *xs++ = *es; 805 *xs++ = execsw_arg; 806 *xs = NULL; 807 if (execsw) 808 free(execsw, M_TEMP); 809 execsw = newexecsw; 810 return 0; 811 } 812 813 int 814 exec_unregister(execsw_arg) 815 const struct execsw *execsw_arg; 816 { 817 const struct execsw **es, **xs, **newexecsw; 818 int count = 1; 819 820 if (execsw == NULL) 821 panic("unregister with no handlers left?\n"); 822 823 for (es = execsw; *es; es++) { 824 if (*es == execsw_arg) 825 break; 826 } 827 if (*es == NULL) 828 return ENOENT; 829 for (es = execsw; *es; es++) 830 if (*es != execsw_arg) 831 count++; 832 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 833 if (newexecsw == NULL) 834 return ENOMEM; 835 xs = newexecsw; 836 for (es = execsw; *es; es++) 837 if (*es != execsw_arg) 838 *xs++ = *es; 839 *xs = NULL; 840 if (execsw) 841 free(execsw, M_TEMP); 842 execsw = newexecsw; 843 return 0; 844 } 845