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, 0); 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 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 error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 881 sv->sv_stackprot, VM_PROT_ALL, 0); 882 if (error) 883 return (error); 884 885 #ifdef __ia64__ 886 { 887 /* 888 * Allocate backing store. We really need something 889 * similar to vm_map_stack which can allow the backing 890 * store to grow upwards. This will do for now. 891 */ 892 vm_offset_t bsaddr; 893 bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz; 894 error = vm_map_find(map, 0, 0, &bsaddr, 895 regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 896 FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 897 } 898 #endif 899 900 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 901 * VM_STACK case, but they are still used to monitor the size of the 902 * process stack so we can check the stack rlimit. 903 */ 904 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 905 vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 906 907 return (0); 908 } 909 910 /* 911 * Copy out argument and environment strings from the old process 912 * address space into the temporary string buffer. 913 */ 914 int 915 exec_extract_strings(imgp) 916 struct image_params *imgp; 917 { 918 char **argv, **envv; 919 char *argp, *envp; 920 int error; 921 size_t length; 922 923 /* 924 * extract arguments first 925 */ 926 927 argv = imgp->userspace_argv; 928 929 if (argv) { 930 argp = (caddr_t)(intptr_t)fuword(argv); 931 if (argp == (caddr_t)-1) 932 return (EFAULT); 933 if (argp) 934 argv++; 935 if (imgp->argv0) 936 argp = imgp->argv0; 937 if (argp) { 938 do { 939 if (argp == (caddr_t)-1) 940 return (EFAULT); 941 if ((error = copyinstr(argp, imgp->stringp, 942 imgp->stringspace, &length))) { 943 if (error == ENAMETOOLONG) 944 return (E2BIG); 945 return (error); 946 } 947 imgp->stringspace -= length; 948 imgp->stringp += length; 949 imgp->argc++; 950 } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 951 } 952 } 953 954 imgp->endargs = imgp->stringp; 955 956 /* 957 * extract environment strings 958 */ 959 960 envv = imgp->userspace_envv; 961 962 if (envv) { 963 while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 964 if (envp == (caddr_t)-1) 965 return (EFAULT); 966 if ((error = copyinstr(envp, imgp->stringp, 967 imgp->stringspace, &length))) { 968 if (error == ENAMETOOLONG) 969 return (E2BIG); 970 return (error); 971 } 972 imgp->stringspace -= length; 973 imgp->stringp += length; 974 imgp->envc++; 975 } 976 } 977 978 return (0); 979 } 980 981 /* 982 * Copy strings out to the new process address space, constructing 983 * new arg and env vector tables. Return a pointer to the base 984 * so that it can be used as the initial stack pointer. 985 */ 986 register_t * 987 exec_copyout_strings(imgp) 988 struct image_params *imgp; 989 { 990 int argc, envc; 991 char **vectp; 992 char *stringp, *destp; 993 register_t *stack_base; 994 struct ps_strings *arginfo; 995 struct proc *p; 996 int szsigcode; 997 998 /* 999 * Calculate string base and vector table pointers. 1000 * Also deal with signal trampoline code for this exec type. 1001 */ 1002 p = imgp->proc; 1003 szsigcode = 0; 1004 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 1005 if (p->p_sysent->sv_szsigcode != NULL) 1006 szsigcode = *(p->p_sysent->sv_szsigcode); 1007 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 1008 roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 1009 1010 /* 1011 * install sigcode 1012 */ 1013 if (szsigcode) 1014 copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 1015 szsigcode), szsigcode); 1016 1017 /* 1018 * If we have a valid auxargs ptr, prepare some room 1019 * on the stack. 1020 */ 1021 if (imgp->auxargs) { 1022 /* 1023 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 1024 * lower compatibility. 1025 */ 1026 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 1027 (AT_COUNT * 2); 1028 /* 1029 * The '+ 2' is for the null pointers at the end of each of 1030 * the arg and env vector sets,and imgp->auxarg_size is room 1031 * for argument of Runtime loader. 1032 */ 1033 vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 1034 imgp->auxarg_size) * sizeof(char *)); 1035 1036 } else 1037 /* 1038 * The '+ 2' is for the null pointers at the end of each of 1039 * the arg and env vector sets 1040 */ 1041 vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 1042 sizeof(char *)); 1043 1044 /* 1045 * vectp also becomes our initial stack base 1046 */ 1047 stack_base = (register_t *)vectp; 1048 1049 stringp = imgp->stringbase; 1050 argc = imgp->argc; 1051 envc = imgp->envc; 1052 1053 /* 1054 * Copy out strings - arguments and environment. 1055 */ 1056 copyout(stringp, destp, ARG_MAX - imgp->stringspace); 1057 1058 /* 1059 * Fill in "ps_strings" struct for ps, w, etc. 1060 */ 1061 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 1062 suword(&arginfo->ps_nargvstr, argc); 1063 1064 /* 1065 * Fill in argument portion of vector table. 1066 */ 1067 for (; argc > 0; --argc) { 1068 suword(vectp++, (long)(intptr_t)destp); 1069 while (*stringp++ != 0) 1070 destp++; 1071 destp++; 1072 } 1073 1074 /* a null vector table pointer separates the argp's from the envp's */ 1075 suword(vectp++, 0); 1076 1077 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 1078 suword(&arginfo->ps_nenvstr, envc); 1079 1080 /* 1081 * Fill in environment portion of vector table. 1082 */ 1083 for (; envc > 0; --envc) { 1084 suword(vectp++, (long)(intptr_t)destp); 1085 while (*stringp++ != 0) 1086 destp++; 1087 destp++; 1088 } 1089 1090 /* end of vector table is a null pointer */ 1091 suword(vectp, 0); 1092 1093 return (stack_base); 1094 } 1095 1096 /* 1097 * Check permissions of file to execute. 1098 * Called with imgp->vp locked. 1099 * Return 0 for success or error code on failure. 1100 */ 1101 int 1102 exec_check_permissions(imgp) 1103 struct image_params *imgp; 1104 { 1105 struct vnode *vp = imgp->vp; 1106 struct vattr *attr = imgp->attr; 1107 struct thread *td; 1108 int error; 1109 1110 td = curthread; /* XXXKSE */ 1111 1112 /* Get file attributes */ 1113 error = VOP_GETATTR(vp, attr, td->td_ucred, td); 1114 if (error) 1115 return (error); 1116 1117 #ifdef MAC 1118 error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp); 1119 if (error) 1120 return (error); 1121 #endif 1122 1123 /* 1124 * 1) Check if file execution is disabled for the filesystem that this 1125 * file resides on. 1126 * 2) Insure that at least one execute bit is on - otherwise root 1127 * will always succeed, and we don't want to happen unless the 1128 * file really is executable. 1129 * 3) Insure that the file is a regular file. 1130 */ 1131 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 1132 ((attr->va_mode & 0111) == 0) || 1133 (attr->va_type != VREG)) 1134 return (EACCES); 1135 1136 /* 1137 * Zero length files can't be exec'd 1138 */ 1139 if (attr->va_size == 0) 1140 return (ENOEXEC); 1141 1142 /* 1143 * Check for execute permission to file based on current credentials. 1144 */ 1145 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 1146 if (error) 1147 return (error); 1148 1149 /* 1150 * Check number of open-for-writes on the file and deny execution 1151 * if there are any. 1152 */ 1153 if (vp->v_writecount) 1154 return (ETXTBSY); 1155 1156 /* 1157 * Call filesystem specific open routine (which does nothing in the 1158 * general case). 1159 */ 1160 error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 1161 return (error); 1162 } 1163 1164 /* 1165 * Exec handler registration 1166 */ 1167 int 1168 exec_register(execsw_arg) 1169 const struct execsw *execsw_arg; 1170 { 1171 const struct execsw **es, **xs, **newexecsw; 1172 int count = 2; /* New slot and trailing NULL */ 1173 1174 if (execsw) 1175 for (es = execsw; *es; es++) 1176 count++; 1177 newexecsw = malloc(count * sizeof(*es), M_TEMP, 0); 1178 if (newexecsw == NULL) 1179 return (ENOMEM); 1180 xs = newexecsw; 1181 if (execsw) 1182 for (es = execsw; *es; es++) 1183 *xs++ = *es; 1184 *xs++ = execsw_arg; 1185 *xs = NULL; 1186 if (execsw) 1187 free(execsw, M_TEMP); 1188 execsw = newexecsw; 1189 return (0); 1190 } 1191 1192 int 1193 exec_unregister(execsw_arg) 1194 const struct execsw *execsw_arg; 1195 { 1196 const struct execsw **es, **xs, **newexecsw; 1197 int count = 1; 1198 1199 if (execsw == NULL) 1200 panic("unregister with no handlers left?\n"); 1201 1202 for (es = execsw; *es; es++) { 1203 if (*es == execsw_arg) 1204 break; 1205 } 1206 if (*es == NULL) 1207 return (ENOENT); 1208 for (es = execsw; *es; es++) 1209 if (*es != execsw_arg) 1210 count++; 1211 newexecsw = malloc(count * sizeof(*es), M_TEMP, 0); 1212 if (newexecsw == NULL) 1213 return (ENOMEM); 1214 xs = newexecsw; 1215 for (es = execsw; *es; es++) 1216 if (*es != execsw_arg) 1217 *xs++ = *es; 1218 *xs = NULL; 1219 if (execsw) 1220 free(execsw, M_TEMP); 1221 execsw = newexecsw; 1222 return (0); 1223 } 1224 1225 int 1226 at_exec(function) 1227 execlist_fn function; 1228 { 1229 struct execlist *ep; 1230 1231 #ifdef INVARIANTS 1232 /* Be noisy if the programmer has lost track of things */ 1233 if (rm_at_exec(function)) 1234 printf("WARNING: exec callout entry (%p) already present\n", 1235 function); 1236 #endif 1237 ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 1238 if (ep == NULL) 1239 return (ENOMEM); 1240 ep->function = function; 1241 TAILQ_INSERT_TAIL(&exec_list, ep, next); 1242 return (0); 1243 } 1244 1245 /* 1246 * Scan the exec callout list for the given item and remove it. 1247 * Returns the number of items removed (0 or 1) 1248 */ 1249 int 1250 rm_at_exec(function) 1251 execlist_fn function; 1252 { 1253 struct execlist *ep; 1254 1255 TAILQ_FOREACH(ep, &exec_list, next) { 1256 if (ep->function == function) { 1257 TAILQ_REMOVE(&exec_list, ep, next); 1258 free(ep, M_ATEXEC); 1259 return (1); 1260 } 1261 } 1262 return (0); 1263 } 1264