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