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