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