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