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