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