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: src/sys/kern/kern_exec.c,v 1.107.2.15 2002/07/30 15:40:46 nectar Exp $ 27 * $DragonFly: src/sys/kern/kern_exec.c,v 1.64 2008/10/26 04:29:19 sephe Exp $ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/sysproto.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/kern_syscall.h> 42 #include <sys/wait.h> 43 #include <sys/malloc.h> 44 #include <sys/proc.h> 45 #include <sys/priv.h> 46 #include <sys/ktrace.h> 47 #include <sys/signalvar.h> 48 #include <sys/pioctl.h> 49 #include <sys/nlookup.h> 50 #include <sys/sfbuf.h> 51 #include <sys/sysent.h> 52 #include <sys/shm.h> 53 #include <sys/sysctl.h> 54 #include <sys/vnode.h> 55 #include <sys/vmmeter.h> 56 #include <sys/aio.h> 57 #include <sys/libkern.h> 58 59 #include <vm/vm.h> 60 #include <vm/vm_param.h> 61 #include <sys/lock.h> 62 #include <vm/pmap.h> 63 #include <vm/vm_page.h> 64 #include <vm/vm_map.h> 65 #include <vm/vm_kern.h> 66 #include <vm/vm_extern.h> 67 #include <vm/vm_object.h> 68 #include <vm/vnode_pager.h> 69 #include <vm/vm_pager.h> 70 71 #include <sys/user.h> 72 #include <sys/reg.h> 73 74 #include <sys/thread2.h> 75 76 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 77 MALLOC_DEFINE(M_EXECARGS, "exec-args", "Exec arguments"); 78 79 static register_t *exec_copyout_strings (struct image_params *); 80 81 /* XXX This should be vm_size_t. */ 82 static u_long ps_strings = PS_STRINGS; 83 SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, ""); 84 85 /* XXX This should be vm_size_t. */ 86 static u_long usrstack = USRSTACK; 87 SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, ""); 88 89 u_long ps_arg_cache_limit = PAGE_SIZE / 16; 90 SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 91 &ps_arg_cache_limit, 0, ""); 92 93 int ps_argsopen = 1; 94 SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 95 96 void print_execve_args(struct image_args *args); 97 int debug_execve_args = 0; 98 SYSCTL_INT(_kern, OID_AUTO, debug_execve_args, CTLFLAG_RW, &debug_execve_args, 99 0, ""); 100 101 /* 102 * Exec arguments object cache 103 */ 104 static struct objcache *exec_objcache; 105 106 static 107 void 108 exec_objcache_init(void *arg __unused) 109 { 110 int cluster_limit; 111 112 cluster_limit = 16; /* up to this many objects */ 113 exec_objcache = objcache_create_mbacked( 114 M_EXECARGS, PATH_MAX + ARG_MAX, 115 &cluster_limit, 116 2, /* minimal magazine capacity */ 117 NULL, NULL, NULL); 118 } 119 SYSINIT(exec_objcache, SI_BOOT2_MACHDEP, SI_ORDER_ANY, exec_objcache_init, 0); 120 121 /* 122 * stackgap_random specifies if the stackgap should have a random size added 123 * to it. It must be a power of 2. If non-zero, the stack gap will be 124 * calculated as: ALIGN(karc4random() & (stackgap_random - 1)). 125 */ 126 static int stackgap_random = 1024; 127 static int 128 sysctl_kern_stackgap(SYSCTL_HANDLER_ARGS) 129 { 130 int error, new_val; 131 new_val = stackgap_random; 132 error = sysctl_handle_int(oidp, &new_val, 0, req); 133 if (error != 0 || req->newptr == NULL) 134 return (error); 135 if ((new_val < 0) || (new_val > 16 * PAGE_SIZE) || ! powerof2(new_val)) 136 return (EINVAL); 137 stackgap_random = new_val; 138 139 return(0); 140 } 141 142 SYSCTL_PROC(_kern, OID_AUTO, stackgap_random, CTLFLAG_RW|CTLTYPE_UINT, 143 0, 0, sysctl_kern_stackgap, "IU", "Max random stack gap (power of 2)"); 144 145 void 146 print_execve_args(struct image_args *args) 147 { 148 char *cp; 149 int ndx; 150 151 cp = args->begin_argv; 152 for (ndx = 0; ndx < args->argc; ndx++) { 153 kprintf("\targv[%d]: %s\n", ndx, cp); 154 while (*cp++ != '\0'); 155 } 156 for (ndx = 0; ndx < args->envc; ndx++) { 157 kprintf("\tenvv[%d]: %s\n", ndx, cp); 158 while (*cp++ != '\0'); 159 } 160 } 161 162 /* 163 * Each of the items is a pointer to a `const struct execsw', hence the 164 * double pointer here. 165 */ 166 static const struct execsw **execsw; 167 168 /* 169 * Replace current vmspace with a new binary. 170 * Returns 0 on success, > 0 on recoverable error (use as errno). 171 * Returns -1 on lethal error which demands killing of the current 172 * process! 173 */ 174 int 175 kern_execve(struct nlookupdata *nd, struct image_args *args) 176 { 177 struct thread *td = curthread; 178 struct lwp *lp = td->td_lwp; 179 struct proc *p = td->td_proc; 180 register_t *stack_base; 181 int error, len, i; 182 struct image_params image_params, *imgp; 183 struct vattr attr; 184 int (*img_first) (struct image_params *); 185 186 if (debug_execve_args) { 187 kprintf("%s()\n", __func__); 188 print_execve_args(args); 189 } 190 191 KKASSERT(p); 192 imgp = &image_params; 193 194 /* 195 * NOTE: P_INEXEC is handled by exec_new_vmspace() now. We make 196 * no modifications to the process at all until we get there. 197 * 198 * Note that multiple threads may be trying to exec at the same 199 * time. exec_new_vmspace() handles that too. 200 */ 201 202 /* 203 * Initialize part of the common data 204 */ 205 imgp->proc = p; 206 imgp->args = args; 207 imgp->attr = &attr; 208 imgp->entry_addr = 0; 209 imgp->resident = 0; 210 imgp->vmspace_destroyed = 0; 211 imgp->interpreted = 0; 212 imgp->interpreter_name[0] = 0; 213 imgp->auxargs = NULL; 214 imgp->vp = NULL; 215 imgp->firstpage = NULL; 216 imgp->ps_strings = 0; 217 imgp->image_header = NULL; 218 219 interpret: 220 221 /* 222 * Translate the file name to a vnode. Unlock the cache entry to 223 * improve parallelism for programs exec'd in parallel. 224 */ 225 if ((error = nlookup(nd)) != 0) 226 goto exec_fail; 227 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp); 228 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED); 229 nd->nl_flags &= ~NLC_NCPISLOCKED; 230 cache_unlock(&nd->nl_nch); 231 if (error) 232 goto exec_fail; 233 234 /* 235 * Check file permissions (also 'opens' file) 236 */ 237 error = exec_check_permissions(imgp); 238 if (error) { 239 vn_unlock(imgp->vp); 240 goto exec_fail_dealloc; 241 } 242 243 error = exec_map_first_page(imgp); 244 vn_unlock(imgp->vp); 245 if (error) 246 goto exec_fail_dealloc; 247 248 if (debug_execve_args && imgp->interpreted) { 249 kprintf(" target is interpreted -- recursive pass\n"); 250 kprintf(" interpreter: %s\n", imgp->interpreter_name); 251 print_execve_args(args); 252 } 253 254 /* 255 * If the current process has a special image activator it 256 * wants to try first, call it. For example, emulating shell 257 * scripts differently. 258 */ 259 error = -1; 260 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 261 error = img_first(imgp); 262 263 /* 264 * If the vnode has a registered vmspace, exec the vmspace 265 */ 266 if (error == -1 && imgp->vp->v_resident) { 267 error = exec_resident_imgact(imgp); 268 } 269 270 /* 271 * Loop through the list of image activators, calling each one. 272 * An activator returns -1 if there is no match, 0 on success, 273 * and an error otherwise. 274 */ 275 for (i = 0; error == -1 && execsw[i]; ++i) { 276 if (execsw[i]->ex_imgact == NULL || 277 execsw[i]->ex_imgact == img_first) { 278 continue; 279 } 280 error = (*execsw[i]->ex_imgact)(imgp); 281 } 282 283 if (error) { 284 if (error == -1) 285 error = ENOEXEC; 286 goto exec_fail_dealloc; 287 } 288 289 /* 290 * Special interpreter operation, cleanup and loop up to try to 291 * activate the interpreter. 292 */ 293 if (imgp->interpreted) { 294 exec_unmap_first_page(imgp); 295 nlookup_done(nd); 296 vrele(imgp->vp); 297 imgp->vp = NULL; 298 error = nlookup_init(nd, imgp->interpreter_name, UIO_SYSSPACE, 299 NLC_FOLLOW); 300 if (error) 301 goto exec_fail; 302 goto interpret; 303 } 304 305 /* 306 * Copy out strings (args and env) and initialize stack base 307 */ 308 stack_base = exec_copyout_strings(imgp); 309 p->p_vmspace->vm_minsaddr = (char *)stack_base; 310 311 /* 312 * If custom stack fixup routine present for this process 313 * let it do the stack setup. If we are running a resident 314 * image there is no auxinfo or other image activator context 315 * so don't try to add fixups to the stack. 316 * 317 * Else stuff argument count as first item on stack 318 */ 319 if (p->p_sysent->sv_fixup && imgp->resident == 0) 320 (*p->p_sysent->sv_fixup)(&stack_base, imgp); 321 else 322 suword(--stack_base, imgp->args->argc); 323 324 /* 325 * For security and other reasons, the file descriptor table cannot 326 * be shared after an exec. 327 */ 328 if (p->p_fd->fd_refcnt > 1) { 329 struct filedesc *tmp; 330 331 tmp = fdcopy(p); 332 fdfree(p); 333 p->p_fd = tmp; 334 } 335 336 /* 337 * For security and other reasons, signal handlers cannot 338 * be shared after an exec. The new proces gets a copy of the old 339 * handlers. In execsigs(), the new process will have its signals 340 * reset. 341 */ 342 if (p->p_sigacts->ps_refcnt > 1) { 343 struct sigacts *newsigacts; 344 345 newsigacts = (struct sigacts *)kmalloc(sizeof(*newsigacts), 346 M_SUBPROC, M_WAITOK); 347 bcopy(p->p_sigacts, newsigacts, sizeof(*newsigacts)); 348 p->p_sigacts->ps_refcnt--; 349 p->p_sigacts = newsigacts; 350 p->p_sigacts->ps_refcnt = 1; 351 } 352 353 /* 354 * For security and other reasons virtual kernels cannot be 355 * inherited by an exec. This also allows a virtual kernel 356 * to fork/exec unrelated applications. 357 */ 358 if (p->p_vkernel) 359 vkernel_exit(p); 360 361 /* Stop profiling */ 362 stopprofclock(p); 363 364 /* close files on exec */ 365 fdcloseexec(p); 366 367 /* reset caught signals */ 368 execsigs(p); 369 370 /* name this process - nameiexec(p, ndp) */ 371 len = min(nd->nl_nch.ncp->nc_nlen, MAXCOMLEN); 372 bcopy(nd->nl_nch.ncp->nc_name, p->p_comm, len); 373 p->p_comm[len] = 0; 374 bcopy(p->p_comm, lp->lwp_thread->td_comm, MAXCOMLEN+1); 375 376 /* 377 * mark as execed, wakeup the process that vforked (if any) and tell 378 * it that it now has its own resources back 379 */ 380 p->p_flag |= P_EXEC; 381 if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 382 p->p_flag &= ~P_PPWAIT; 383 wakeup((caddr_t)p->p_pptr); 384 } 385 386 /* 387 * Implement image setuid/setgid. 388 * 389 * Don't honor setuid/setgid if the filesystem prohibits it or if 390 * the process is being traced. 391 */ 392 if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) || 393 ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) && 394 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 395 (p->p_flag & P_TRACED) == 0) { 396 /* 397 * Turn off syscall tracing for set-id programs, except for 398 * root. Record any set-id flags first to make sure that 399 * we do not regain any tracing during a possible block. 400 */ 401 setsugid(); 402 if (p->p_tracenode && priv_check(td, PRIV_ROOT) != 0) { 403 ktrdestroy(&p->p_tracenode); 404 p->p_traceflag = 0; 405 } 406 /* Close any file descriptors 0..2 that reference procfs */ 407 setugidsafety(p); 408 /* Make sure file descriptors 0..2 are in use. */ 409 error = fdcheckstd(p); 410 if (error != 0) 411 goto exec_fail_dealloc; 412 /* 413 * Set the new credentials. 414 */ 415 cratom(&p->p_ucred); 416 if (attr.va_mode & VSUID) 417 change_euid(attr.va_uid); 418 if (attr.va_mode & VSGID) 419 p->p_ucred->cr_gid = attr.va_gid; 420 421 /* 422 * Clear local varsym variables 423 */ 424 varsymset_clean(&p->p_varsymset); 425 } else { 426 if (p->p_ucred->cr_uid == p->p_ucred->cr_ruid && 427 p->p_ucred->cr_gid == p->p_ucred->cr_rgid) 428 p->p_flag &= ~P_SUGID; 429 } 430 431 /* 432 * Implement correct POSIX saved-id behavior. 433 */ 434 if (p->p_ucred->cr_svuid != p->p_ucred->cr_uid || 435 p->p_ucred->cr_svgid != p->p_ucred->cr_gid) { 436 cratom(&p->p_ucred); 437 p->p_ucred->cr_svuid = p->p_ucred->cr_uid; 438 p->p_ucred->cr_svgid = p->p_ucred->cr_gid; 439 } 440 441 /* 442 * Store the vp for use in procfs 443 */ 444 if (p->p_textvp) /* release old reference */ 445 vrele(p->p_textvp); 446 p->p_textvp = imgp->vp; 447 vref(p->p_textvp); 448 449 /* 450 * Notify others that we exec'd, and clear the P_INEXEC flag 451 * as we're now a bona fide freshly-execed process. 452 */ 453 KNOTE(&p->p_klist, NOTE_EXEC); 454 p->p_flag &= ~P_INEXEC; 455 456 /* 457 * If tracing the process, trap to debugger so breakpoints 458 * can be set before the program executes. 459 */ 460 STOPEVENT(p, S_EXEC, 0); 461 462 if (p->p_flag & P_TRACED) 463 ksignal(p, SIGTRAP); 464 465 /* clear "fork but no exec" flag, as we _are_ execing */ 466 p->p_acflag &= ~AFORK; 467 468 /* Set values passed into the program in registers. */ 469 exec_setregs(imgp->entry_addr, (u_long)(uintptr_t)stack_base, 470 imgp->ps_strings); 471 472 /* Free any previous argument cache */ 473 if (p->p_args && --p->p_args->ar_ref == 0) 474 FREE(p->p_args, M_PARGS); 475 p->p_args = NULL; 476 477 /* Cache arguments if they fit inside our allowance */ 478 i = imgp->args->begin_envv - imgp->args->begin_argv; 479 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 480 MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, 481 M_PARGS, M_WAITOK); 482 p->p_args->ar_ref = 1; 483 p->p_args->ar_length = i; 484 bcopy(imgp->args->begin_argv, p->p_args->ar_args, i); 485 } 486 487 exec_fail_dealloc: 488 489 /* 490 * free various allocated resources 491 */ 492 if (imgp->firstpage) 493 exec_unmap_first_page(imgp); 494 495 if (imgp->vp) { 496 vrele(imgp->vp); 497 imgp->vp = NULL; 498 } 499 500 if (error == 0) { 501 ++mycpu->gd_cnt.v_exec; 502 return (0); 503 } 504 505 exec_fail: 506 /* 507 * we're done here, clear P_INEXEC if we were the ones that 508 * set it. Otherwise if vmspace_destroyed is still set we 509 * raced another thread and that thread is responsible for 510 * clearing it. 511 */ 512 if (imgp->vmspace_destroyed & 2) 513 p->p_flag &= ~P_INEXEC; 514 if (imgp->vmspace_destroyed) { 515 /* 516 * Sorry, no more process anymore. exit gracefully. 517 * However we can't die right here, because our 518 * caller might have to clean up, so indicate a 519 * lethal error by returning -1. 520 */ 521 return(-1); 522 } else { 523 return(error); 524 } 525 } 526 527 /* 528 * execve() system call. 529 */ 530 int 531 sys_execve(struct execve_args *uap) 532 { 533 struct nlookupdata nd; 534 struct image_args args; 535 int error; 536 537 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW); 538 if (error == 0) { 539 error = exec_copyin_args(&args, uap->fname, PATH_USERSPACE, 540 uap->argv, uap->envv); 541 } 542 if (error == 0) 543 error = kern_execve(&nd, &args); 544 nlookup_done(&nd); 545 exec_free_args(&args); 546 547 if (error < 0) { 548 /* We hit a lethal error condition. Let's die now. */ 549 exit1(W_EXITCODE(0, SIGABRT)); 550 /* NOTREACHED */ 551 } 552 553 /* 554 * The syscall result is returned in registers to the new program. 555 * Linux will register %edx as an atexit function and we must be 556 * sure to set it to 0. XXX 557 */ 558 if (error == 0) 559 uap->sysmsg_result64 = 0; 560 561 return (error); 562 } 563 564 int 565 exec_map_first_page(struct image_params *imgp) 566 { 567 int rv, i; 568 int initial_pagein; 569 vm_page_t ma[VM_INITIAL_PAGEIN]; 570 vm_page_t m; 571 vm_object_t object; 572 573 if (imgp->firstpage) 574 exec_unmap_first_page(imgp); 575 576 /* 577 * The file has to be mappable. 578 */ 579 if ((object = imgp->vp->v_object) == NULL) 580 return (EIO); 581 582 /* 583 * We shouldn't need protection for vm_page_grab() but we certainly 584 * need it for the lookup loop below (lookup/busy race), since 585 * an interrupt can unbusy and free the page before our busy check. 586 */ 587 crit_enter(); 588 m = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 589 590 if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 591 ma[0] = m; 592 initial_pagein = VM_INITIAL_PAGEIN; 593 if (initial_pagein > object->size) 594 initial_pagein = object->size; 595 for (i = 1; i < initial_pagein; i++) { 596 if ((m = vm_page_lookup(object, i)) != NULL) { 597 if ((m->flags & PG_BUSY) || m->busy) 598 break; 599 if (m->valid) 600 break; 601 vm_page_busy(m); 602 } else { 603 m = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 604 if (m == NULL) 605 break; 606 } 607 ma[i] = m; 608 } 609 initial_pagein = i; 610 611 /* 612 * get_pages unbusies all the requested pages except the 613 * primary page (at index 0 in this case). The primary 614 * page may have been wired during the pagein (e.g. by 615 * the buffer cache) so vnode_pager_freepage() must be 616 * used to properly release it. 617 */ 618 rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 619 m = vm_page_lookup(object, 0); 620 621 if (rv != VM_PAGER_OK || m == NULL || m->valid == 0) { 622 if (m) { 623 vm_page_protect(m, VM_PROT_NONE); 624 vnode_pager_freepage(m); 625 } 626 crit_exit(); 627 return EIO; 628 } 629 } 630 vm_page_hold(m); 631 vm_page_wakeup(m); /* unbusy the page */ 632 crit_exit(); 633 634 imgp->firstpage = sf_buf_alloc(m, SFB_CPUPRIVATE); 635 imgp->image_header = (void *)sf_buf_kva(imgp->firstpage); 636 637 return 0; 638 } 639 640 void 641 exec_unmap_first_page(struct image_params *imgp) 642 { 643 vm_page_t m; 644 645 crit_enter(); 646 if (imgp->firstpage != NULL) { 647 m = sf_buf_page(imgp->firstpage); 648 sf_buf_free(imgp->firstpage); 649 imgp->firstpage = NULL; 650 imgp->image_header = NULL; 651 vm_page_unhold(m); 652 } 653 crit_exit(); 654 } 655 656 /* 657 * Destroy old address space, and allocate a new stack 658 * The new stack is only SGROWSIZ large because it is grown 659 * automatically in trap.c. 660 * 661 * This is the point of no return. 662 */ 663 int 664 exec_new_vmspace(struct image_params *imgp, struct vmspace *vmcopy) 665 { 666 struct vmspace *vmspace = imgp->proc->p_vmspace; 667 vm_offset_t stack_addr = USRSTACK - maxssiz; 668 struct proc *p; 669 vm_map_t map; 670 int error; 671 672 /* 673 * Indicate that we cannot gracefully error out any more, kill 674 * any other threads present, and set P_INEXEC to indicate that 675 * we are now messing with the process structure proper. 676 * 677 * If killalllwps() races return an error which coupled with 678 * vmspace_destroyed will cause us to exit. This is what we 679 * want since another thread is patiently waiting for us to exit 680 * in that case. 681 */ 682 p = curproc; 683 imgp->vmspace_destroyed = 1; 684 685 if (curthread->td_proc->p_nthreads > 1) { 686 error = killalllwps(1); 687 if (error) 688 return (error); 689 } 690 imgp->vmspace_destroyed |= 2; /* we are responsible for P_INEXEC */ 691 p->p_flag |= P_INEXEC; 692 693 /* 694 * Prevent a pending AIO from modifying the new address space. 695 */ 696 aio_proc_rundown(imgp->proc); 697 698 /* 699 * Blow away entire process VM, if address space not shared, 700 * otherwise, create a new VM space so that other threads are 701 * not disrupted. If we are execing a resident vmspace we 702 * create a duplicate of it and remap the stack. 703 * 704 * The exitingcnt test is not strictly necessary but has been 705 * included for code sanity (to make the code more deterministic). 706 */ 707 map = &vmspace->vm_map; 708 if (vmcopy) { 709 vmspace_exec(imgp->proc, vmcopy); 710 vmspace = imgp->proc->p_vmspace; 711 pmap_remove_pages(vmspace_pmap(vmspace), stack_addr, USRSTACK); 712 map = &vmspace->vm_map; 713 } else if (vmspace->vm_sysref.refcnt == 1 && 714 vmspace->vm_exitingcnt == 0) { 715 shmexit(vmspace); 716 if (vmspace->vm_upcalls) 717 upc_release(vmspace, ONLY_LWP_IN_PROC(imgp->proc)); 718 pmap_remove_pages(vmspace_pmap(vmspace), 719 0, VM_MAX_USER_ADDRESS); 720 vm_map_remove(map, 0, VM_MAX_USER_ADDRESS); 721 } else { 722 vmspace_exec(imgp->proc, NULL); 723 vmspace = imgp->proc->p_vmspace; 724 map = &vmspace->vm_map; 725 } 726 727 /* Allocate a new stack */ 728 error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz, 729 VM_PROT_ALL, VM_PROT_ALL, 0); 730 if (error) 731 return (error); 732 733 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 734 * VM_STACK case, but they are still used to monitor the size of the 735 * process stack so we can check the stack rlimit. 736 */ 737 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 738 vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz; 739 740 return(0); 741 } 742 743 /* 744 * Copy out argument and environment strings from the old process 745 * address space into the temporary string buffer. 746 */ 747 int 748 exec_copyin_args(struct image_args *args, char *fname, 749 enum exec_path_segflg segflg, char **argv, char **envv) 750 { 751 char *argp, *envp; 752 int error = 0; 753 size_t length; 754 755 bzero(args, sizeof(*args)); 756 757 args->buf = objcache_get(exec_objcache, M_WAITOK); 758 if (args->buf == NULL) 759 return (ENOMEM); 760 args->begin_argv = args->buf; 761 args->endp = args->begin_argv; 762 args->space = ARG_MAX; 763 764 args->fname = args->buf + ARG_MAX; 765 766 /* 767 * Copy the file name. 768 */ 769 if (segflg == PATH_SYSSPACE) { 770 error = copystr(fname, args->fname, PATH_MAX, &length); 771 } else if (segflg == PATH_USERSPACE) { 772 error = copyinstr(fname, args->fname, PATH_MAX, &length); 773 } 774 775 /* 776 * Extract argument strings. argv may not be NULL. The argv 777 * array is terminated by a NULL entry. We special-case the 778 * situation where argv[0] is NULL by passing { filename, NULL } 779 * to the new program to guarentee that the interpreter knows what 780 * file to open in case we exec an interpreted file. Note that 781 * a NULL argv[0] terminates the argv[] array. 782 * 783 * XXX the special-casing of argv[0] is historical and needs to be 784 * revisited. 785 */ 786 if (argv == NULL) 787 error = EFAULT; 788 if (error == 0) { 789 while ((argp = (caddr_t)(intptr_t)fuword(argv++)) != NULL) { 790 if (argp == (caddr_t)-1) { 791 error = EFAULT; 792 break; 793 } 794 error = copyinstr(argp, args->endp, 795 args->space, &length); 796 if (error) { 797 if (error == ENAMETOOLONG) 798 error = E2BIG; 799 break; 800 } 801 args->space -= length; 802 args->endp += length; 803 args->argc++; 804 } 805 if (args->argc == 0 && error == 0) { 806 length = strlen(args->fname) + 1; 807 if (length > args->space) { 808 error = E2BIG; 809 } else { 810 bcopy(args->fname, args->endp, length); 811 args->space -= length; 812 args->endp += length; 813 args->argc++; 814 } 815 } 816 } 817 818 args->begin_envv = args->endp; 819 820 /* 821 * extract environment strings. envv may be NULL. 822 */ 823 if (envv && error == 0) { 824 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 825 if (envp == (caddr_t) -1) { 826 error = EFAULT; 827 break; 828 } 829 error = copyinstr(envp, args->endp, args->space, 830 &length); 831 if (error) { 832 if (error == ENAMETOOLONG) 833 error = E2BIG; 834 break; 835 } 836 args->space -= length; 837 args->endp += length; 838 args->envc++; 839 } 840 } 841 return (error); 842 } 843 844 void 845 exec_free_args(struct image_args *args) 846 { 847 if (args->buf) { 848 objcache_put(exec_objcache, args->buf); 849 args->buf = NULL; 850 } 851 } 852 853 /* 854 * Copy strings out to the new process address space, constructing 855 * new arg and env vector tables. Return a pointer to the base 856 * so that it can be used as the initial stack pointer. 857 */ 858 register_t * 859 exec_copyout_strings(struct image_params *imgp) 860 { 861 int argc, envc, sgap; 862 char **vectp; 863 char *stringp, *destp; 864 register_t *stack_base; 865 struct ps_strings *arginfo; 866 int szsigcode; 867 868 /* 869 * Calculate string base and vector table pointers. 870 * Also deal with signal trampoline code for this exec type. 871 */ 872 arginfo = (struct ps_strings *)PS_STRINGS; 873 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 874 if (stackgap_random != 0) 875 sgap = ALIGN(karc4random() & (stackgap_random - 1)); 876 else 877 sgap = 0; 878 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - sgap - 879 roundup((ARG_MAX - imgp->args->space), sizeof(char *)); 880 881 /* 882 * install sigcode 883 */ 884 if (szsigcode) 885 copyout(imgp->proc->p_sysent->sv_sigcode, 886 ((caddr_t)arginfo - szsigcode), szsigcode); 887 888 /* 889 * If we have a valid auxargs ptr, prepare some room 890 * on the stack. 891 * 892 * The '+ 2' is for the null pointers at the end of each of the 893 * arg and env vector sets, and 'AT_COUNT*2' is room for the 894 * ELF Auxargs data. 895 */ 896 if (imgp->auxargs) { 897 vectp = (char **)(destp - (imgp->args->argc + 898 imgp->args->envc + 2 + AT_COUNT * 2) * sizeof(char*)); 899 } else { 900 vectp = (char **)(destp - (imgp->args->argc + 901 imgp->args->envc + 2) * sizeof(char*)); 902 } 903 904 /* 905 * NOTE: don't bother aligning the stack here for GCC 2.x, it will 906 * be done in crt1.o. Note that GCC 3.x aligns the stack in main. 907 */ 908 909 /* 910 * vectp also becomes our initial stack base 911 */ 912 stack_base = (register_t *)vectp; 913 914 stringp = imgp->args->begin_argv; 915 argc = imgp->args->argc; 916 envc = imgp->args->envc; 917 918 /* 919 * Copy out strings - arguments and environment. 920 */ 921 copyout(stringp, destp, ARG_MAX - imgp->args->space); 922 923 /* 924 * Fill in "ps_strings" struct for ps, w, etc. 925 */ 926 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 927 suword(&arginfo->ps_nargvstr, argc); 928 929 /* 930 * Fill in argument portion of vector table. 931 */ 932 for (; argc > 0; --argc) { 933 suword(vectp++, (long)(intptr_t)destp); 934 while (*stringp++ != 0) 935 destp++; 936 destp++; 937 } 938 939 /* a null vector table pointer separates the argp's from the envp's */ 940 suword(vectp++, 0); 941 942 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 943 suword(&arginfo->ps_nenvstr, envc); 944 945 /* 946 * Fill in environment portion of vector table. 947 */ 948 for (; envc > 0; --envc) { 949 suword(vectp++, (long)(intptr_t)destp); 950 while (*stringp++ != 0) 951 destp++; 952 destp++; 953 } 954 955 /* end of vector table is a null pointer */ 956 suword(vectp, 0); 957 958 return (stack_base); 959 } 960 961 /* 962 * Check permissions of file to execute. 963 * Return 0 for success or error code on failure. 964 */ 965 int 966 exec_check_permissions(struct image_params *imgp) 967 { 968 struct proc *p = imgp->proc; 969 struct vnode *vp = imgp->vp; 970 struct vattr *attr = imgp->attr; 971 int error; 972 973 /* Get file attributes */ 974 error = VOP_GETATTR(vp, attr); 975 if (error) 976 return (error); 977 978 /* 979 * 1) Check if file execution is disabled for the filesystem that this 980 * file resides on. 981 * 2) Insure that at least one execute bit is on - otherwise root 982 * will always succeed, and we don't want to happen unless the 983 * file really is executable. 984 * 3) Insure that the file is a regular file. 985 */ 986 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 987 ((attr->va_mode & 0111) == 0) || 988 (attr->va_type != VREG)) { 989 return (EACCES); 990 } 991 992 /* 993 * Zero length files can't be exec'd 994 */ 995 if (attr->va_size == 0) 996 return (ENOEXEC); 997 998 /* 999 * Check for execute permission to file based on current credentials. 1000 */ 1001 error = VOP_ACCESS(vp, VEXEC, p->p_ucred); 1002 if (error) 1003 return (error); 1004 1005 /* 1006 * Check number of open-for-writes on the file and deny execution 1007 * if there are any. 1008 */ 1009 if (vp->v_writecount) 1010 return (ETXTBSY); 1011 1012 /* 1013 * Call filesystem specific open routine, which allows us to read, 1014 * write, and mmap the file. Without the VOP_OPEN we can only 1015 * stat the file. 1016 */ 1017 error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL); 1018 if (error) 1019 return (error); 1020 1021 return (0); 1022 } 1023 1024 /* 1025 * Exec handler registration 1026 */ 1027 int 1028 exec_register(const struct execsw *execsw_arg) 1029 { 1030 const struct execsw **es, **xs, **newexecsw; 1031 int count = 2; /* New slot and trailing NULL */ 1032 1033 if (execsw) 1034 for (es = execsw; *es; es++) 1035 count++; 1036 newexecsw = kmalloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1037 xs = newexecsw; 1038 if (execsw) 1039 for (es = execsw; *es; es++) 1040 *xs++ = *es; 1041 *xs++ = execsw_arg; 1042 *xs = NULL; 1043 if (execsw) 1044 kfree(execsw, M_TEMP); 1045 execsw = newexecsw; 1046 return 0; 1047 } 1048 1049 int 1050 exec_unregister(const struct execsw *execsw_arg) 1051 { 1052 const struct execsw **es, **xs, **newexecsw; 1053 int count = 1; 1054 1055 if (execsw == NULL) 1056 panic("unregister with no handlers left?"); 1057 1058 for (es = execsw; *es; es++) { 1059 if (*es == execsw_arg) 1060 break; 1061 } 1062 if (*es == NULL) 1063 return ENOENT; 1064 for (es = execsw; *es; es++) 1065 if (*es != execsw_arg) 1066 count++; 1067 newexecsw = kmalloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1068 xs = newexecsw; 1069 for (es = execsw; *es; es++) 1070 if (*es != execsw_arg) 1071 *xs++ = *es; 1072 *xs = NULL; 1073 if (execsw) 1074 kfree(execsw, M_TEMP); 1075 execsw = newexecsw; 1076 return 0; 1077 } 1078