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