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