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