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