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