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