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