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