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