kern_exec.c revision 1.376 1 /* $NetBSD: kern_exec.c,v 1.376 2014/02/17 19:29:46 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*-
30 * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
31 * Copyright (C) 1992 Wolfgang Solfrank.
32 * Copyright (C) 1992 TooLs GmbH.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by TooLs GmbH.
46 * 4. The name of TooLs GmbH may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.376 2014/02/17 19:29:46 maxv Exp $");
63
64 #include "opt_exec.h"
65 #include "opt_execfmt.h"
66 #include "opt_ktrace.h"
67 #include "opt_modular.h"
68 #include "opt_syscall_debug.h"
69 #include "veriexec.h"
70 #include "opt_pax.h"
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/filedesc.h>
75 #include <sys/kernel.h>
76 #include <sys/proc.h>
77 #include <sys/mount.h>
78 #include <sys/malloc.h>
79 #include <sys/kmem.h>
80 #include <sys/namei.h>
81 #include <sys/vnode.h>
82 #include <sys/file.h>
83 #include <sys/acct.h>
84 #include <sys/atomic.h>
85 #include <sys/exec.h>
86 #include <sys/ktrace.h>
87 #include <sys/uidinfo.h>
88 #include <sys/wait.h>
89 #include <sys/mman.h>
90 #include <sys/ras.h>
91 #include <sys/signalvar.h>
92 #include <sys/stat.h>
93 #include <sys/syscall.h>
94 #include <sys/kauth.h>
95 #include <sys/lwpctl.h>
96 #include <sys/pax.h>
97 #include <sys/cpu.h>
98 #include <sys/module.h>
99 #include <sys/syscallvar.h>
100 #include <sys/syscallargs.h>
101 #if NVERIEXEC > 0
102 #include <sys/verified_exec.h>
103 #endif /* NVERIEXEC > 0 */
104 #include <sys/sdt.h>
105 #include <sys/spawn.h>
106 #include <sys/prot.h>
107 #include <sys/cprng.h>
108
109 #include <uvm/uvm_extern.h>
110
111 #include <machine/reg.h>
112
113 #include <compat/common/compat_util.h>
114
115 #ifndef MD_TOPDOWN_INIT
116 #ifdef __USE_TOPDOWN_VM
117 #define MD_TOPDOWN_INIT(epp) (epp)->ep_flags |= EXEC_TOPDOWN_VM
118 #else
119 #define MD_TOPDOWN_INIT(epp)
120 #endif
121 #endif
122
123 static int exec_sigcode_map(struct proc *, const struct emul *);
124
125 #ifdef DEBUG_EXEC
126 #define DPRINTF(a) printf a
127 #define COPYPRINTF(s, a, b) printf("%s, %d: copyout%s @%p %zu\n", __func__, \
128 __LINE__, (s), (a), (b))
129 #else
130 #define DPRINTF(a)
131 #define COPYPRINTF(s, a, b)
132 #endif /* DEBUG_EXEC */
133
134 /*
135 * DTrace SDT provider definitions
136 */
137 SDT_PROBE_DEFINE(proc,,,exec,exec,
138 "char *", NULL,
139 NULL, NULL, NULL, NULL,
140 NULL, NULL, NULL, NULL);
141 SDT_PROBE_DEFINE(proc,,,exec_success,exec-success,
142 "char *", NULL,
143 NULL, NULL, NULL, NULL,
144 NULL, NULL, NULL, NULL);
145 SDT_PROBE_DEFINE(proc,,,exec_failure,exec-failure,
146 "int", NULL,
147 NULL, NULL, NULL, NULL,
148 NULL, NULL, NULL, NULL);
149
150 /*
151 * Exec function switch:
152 *
153 * Note that each makecmds function is responsible for loading the
154 * exec package with the necessary functions for any exec-type-specific
155 * handling.
156 *
157 * Functions for specific exec types should be defined in their own
158 * header file.
159 */
160 static const struct execsw **execsw = NULL;
161 static int nexecs;
162
163 u_int exec_maxhdrsz; /* must not be static - used by netbsd32 */
164
165 /* list of dynamically loaded execsw entries */
166 static LIST_HEAD(execlist_head, exec_entry) ex_head =
167 LIST_HEAD_INITIALIZER(ex_head);
168 struct exec_entry {
169 LIST_ENTRY(exec_entry) ex_list;
170 SLIST_ENTRY(exec_entry) ex_slist;
171 const struct execsw *ex_sw;
172 };
173
174 #ifndef __HAVE_SYSCALL_INTERN
175 void syscall(void);
176 #endif
177
178 /* NetBSD emul struct */
179 struct emul emul_netbsd = {
180 .e_name = "netbsd",
181 #ifdef EMUL_NATIVEROOT
182 .e_path = EMUL_NATIVEROOT,
183 #else
184 .e_path = NULL,
185 #endif
186 #ifndef __HAVE_MINIMAL_EMUL
187 .e_flags = EMUL_HAS_SYS___syscall,
188 .e_errno = NULL,
189 .e_nosys = SYS_syscall,
190 .e_nsysent = SYS_NSYSENT,
191 #endif
192 .e_sysent = sysent,
193 #ifdef SYSCALL_DEBUG
194 .e_syscallnames = syscallnames,
195 #else
196 .e_syscallnames = NULL,
197 #endif
198 .e_sendsig = sendsig,
199 .e_trapsignal = trapsignal,
200 .e_tracesig = NULL,
201 .e_sigcode = NULL,
202 .e_esigcode = NULL,
203 .e_sigobject = NULL,
204 .e_setregs = setregs,
205 .e_proc_exec = NULL,
206 .e_proc_fork = NULL,
207 .e_proc_exit = NULL,
208 .e_lwp_fork = NULL,
209 .e_lwp_exit = NULL,
210 #ifdef __HAVE_SYSCALL_INTERN
211 .e_syscall_intern = syscall_intern,
212 #else
213 .e_syscall = syscall,
214 #endif
215 .e_sysctlovly = NULL,
216 .e_fault = NULL,
217 .e_vm_default_addr = uvm_default_mapaddr,
218 .e_usertrap = NULL,
219 .e_ucsize = sizeof(ucontext_t),
220 .e_startlwp = startlwp
221 };
222
223 /*
224 * Exec lock. Used to control access to execsw[] structures.
225 * This must not be static so that netbsd32 can access it, too.
226 */
227 krwlock_t exec_lock;
228
229 static kmutex_t sigobject_lock;
230
231 /*
232 * Data used between a loadvm and execve part of an "exec" operation
233 */
234 struct execve_data {
235 struct exec_package ed_pack;
236 struct pathbuf *ed_pathbuf;
237 struct vattr ed_attr;
238 struct ps_strings ed_arginfo;
239 char *ed_argp;
240 const char *ed_pathstring;
241 char *ed_resolvedpathbuf;
242 size_t ed_ps_strings_sz;
243 int ed_szsigcode;
244 long ed_argc;
245 long ed_envc;
246 };
247
248 /*
249 * data passed from parent lwp to child during a posix_spawn()
250 */
251 struct spawn_exec_data {
252 struct execve_data sed_exec;
253 struct posix_spawn_file_actions
254 *sed_actions;
255 struct posix_spawnattr *sed_attrs;
256 struct proc *sed_parent;
257 kcondvar_t sed_cv_child_ready;
258 kmutex_t sed_mtx_child;
259 int sed_error;
260 volatile uint32_t sed_refcnt;
261 };
262
263 static void *
264 exec_pool_alloc(struct pool *pp, int flags)
265 {
266
267 return (void *)uvm_km_alloc(kernel_map, NCARGS, 0,
268 UVM_KMF_PAGEABLE | UVM_KMF_WAITVA);
269 }
270
271 static void
272 exec_pool_free(struct pool *pp, void *addr)
273 {
274
275 uvm_km_free(kernel_map, (vaddr_t)addr, NCARGS, UVM_KMF_PAGEABLE);
276 }
277
278 static struct pool exec_pool;
279
280 static struct pool_allocator exec_palloc = {
281 .pa_alloc = exec_pool_alloc,
282 .pa_free = exec_pool_free,
283 .pa_pagesz = NCARGS
284 };
285
286 /*
287 * check exec:
288 * given an "executable" described in the exec package's namei info,
289 * see what we can do with it.
290 *
291 * ON ENTRY:
292 * exec package with appropriate namei info
293 * lwp pointer of exec'ing lwp
294 * NO SELF-LOCKED VNODES
295 *
296 * ON EXIT:
297 * error: nothing held, etc. exec header still allocated.
298 * ok: filled exec package, executable's vnode (unlocked).
299 *
300 * EXEC SWITCH ENTRY:
301 * Locked vnode to check, exec package, proc.
302 *
303 * EXEC SWITCH EXIT:
304 * ok: return 0, filled exec package, executable's vnode (unlocked).
305 * error: destructive:
306 * everything deallocated execept exec header.
307 * non-destructive:
308 * error code, executable's vnode (unlocked),
309 * exec header unmodified.
310 */
311 int
312 /*ARGSUSED*/
313 check_exec(struct lwp *l, struct exec_package *epp, struct pathbuf *pb)
314 {
315 int error, i;
316 struct vnode *vp;
317 struct nameidata nd;
318 size_t resid;
319
320 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
321
322 /* first get the vnode */
323 if ((error = namei(&nd)) != 0)
324 return error;
325 epp->ep_vp = vp = nd.ni_vp;
326 /* normally this can't fail */
327 if ((error = copystr(nd.ni_pnbuf, epp->ep_resolvedname, PATH_MAX, NULL)))
328 goto bad1;
329
330 #ifdef DIAGNOSTIC
331 /* paranoia (take this out once namei stuff stabilizes) */
332 memset(nd.ni_pnbuf, '~', PATH_MAX);
333 #endif
334
335 /* check access and type */
336 if (vp->v_type != VREG) {
337 error = EACCES;
338 goto bad1;
339 }
340 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
341 goto bad1;
342
343 /* get attributes */
344 if ((error = VOP_GETATTR(vp, epp->ep_vap, l->l_cred)) != 0)
345 goto bad1;
346
347 /* Check mount point */
348 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
349 error = EACCES;
350 goto bad1;
351 }
352 if (vp->v_mount->mnt_flag & MNT_NOSUID)
353 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
354
355 /* try to open it */
356 if ((error = VOP_OPEN(vp, FREAD, l->l_cred)) != 0)
357 goto bad1;
358
359 /* unlock vp, since we need it unlocked from here on out. */
360 VOP_UNLOCK(vp);
361
362 #if NVERIEXEC > 0
363 error = veriexec_verify(l, vp, epp->ep_resolvedname,
364 epp->ep_flags & EXEC_INDIR ? VERIEXEC_INDIRECT : VERIEXEC_DIRECT,
365 NULL);
366 if (error)
367 goto bad2;
368 #endif /* NVERIEXEC > 0 */
369
370 #ifdef PAX_SEGVGUARD
371 error = pax_segvguard(l, vp, epp->ep_resolvedname, false);
372 if (error)
373 goto bad2;
374 #endif /* PAX_SEGVGUARD */
375
376 /* now we have the file, get the exec header */
377 error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
378 UIO_SYSSPACE, 0, l->l_cred, &resid, NULL);
379 if (error)
380 goto bad2;
381 epp->ep_hdrvalid = epp->ep_hdrlen - resid;
382
383 /*
384 * Set up default address space limits. Can be overridden
385 * by individual exec packages.
386 *
387 * XXX probably should be all done in the exec packages.
388 */
389 epp->ep_vm_minaddr = VM_MIN_ADDRESS;
390 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
391 /*
392 * set up the vmcmds for creation of the process
393 * address space
394 */
395 error = ENOEXEC;
396 for (i = 0; i < nexecs; i++) {
397 int newerror;
398
399 epp->ep_esch = execsw[i];
400 newerror = (*execsw[i]->es_makecmds)(l, epp);
401
402 if (!newerror) {
403 /* Seems ok: check that entry point is not too high */
404 if (epp->ep_entry > epp->ep_vm_maxaddr) {
405 #ifdef DIAGNOSTIC
406 printf("%s: rejecting %p due to "
407 "too high entry address (> %p)\n",
408 __func__, (void *)epp->ep_entry,
409 (void *)epp->ep_vm_maxaddr);
410 #endif
411 error = ENOEXEC;
412 break;
413 }
414 /* Seems ok: check that entry point is not too low */
415 if (epp->ep_entry < epp->ep_vm_minaddr) {
416 #ifdef DIAGNOSTIC
417 printf("%s: rejecting %p due to "
418 "too low entry address (< %p)\n",
419 __func__, (void *)epp->ep_entry,
420 (void *)epp->ep_vm_minaddr);
421 #endif
422 error = ENOEXEC;
423 break;
424 }
425
426 /* check limits */
427 if ((epp->ep_tsize > MAXTSIZ) ||
428 (epp->ep_dsize > (u_quad_t)l->l_proc->p_rlimit
429 [RLIMIT_DATA].rlim_cur)) {
430 #ifdef DIAGNOSTIC
431 printf("%s: rejecting due to "
432 "limits (t=%llu > %llu || d=%llu > %llu)\n",
433 __func__,
434 (unsigned long long)epp->ep_tsize,
435 (unsigned long long)MAXTSIZ,
436 (unsigned long long)epp->ep_dsize,
437 (unsigned long long)
438 l->l_proc->p_rlimit[RLIMIT_DATA].rlim_cur);
439 #endif
440 error = ENOMEM;
441 break;
442 }
443 return 0;
444 }
445
446 if (epp->ep_emul_root != NULL) {
447 vrele(epp->ep_emul_root);
448 epp->ep_emul_root = NULL;
449 }
450 if (epp->ep_interp != NULL) {
451 vrele(epp->ep_interp);
452 epp->ep_interp = NULL;
453 }
454
455 /* make sure the first "interesting" error code is saved. */
456 if (error == ENOEXEC)
457 error = newerror;
458
459 if (epp->ep_flags & EXEC_DESTR)
460 /* Error from "#!" code, tidied up by recursive call */
461 return error;
462 }
463
464 /* not found, error */
465
466 /*
467 * free any vmspace-creation commands,
468 * and release their references
469 */
470 kill_vmcmds(&epp->ep_vmcmds);
471
472 bad2:
473 /*
474 * close and release the vnode, restore the old one, free the
475 * pathname buf, and punt.
476 */
477 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
478 VOP_CLOSE(vp, FREAD, l->l_cred);
479 vput(vp);
480 return error;
481
482 bad1:
483 /*
484 * free the namei pathname buffer, and put the vnode
485 * (which we don't yet have open).
486 */
487 vput(vp); /* was still locked */
488 return error;
489 }
490
491 #ifdef __MACHINE_STACK_GROWS_UP
492 #define STACK_PTHREADSPACE NBPG
493 #else
494 #define STACK_PTHREADSPACE 0
495 #endif
496
497 static int
498 execve_fetch_element(char * const *array, size_t index, char **value)
499 {
500 return copyin(array + index, value, sizeof(*value));
501 }
502
503 /*
504 * exec system call
505 */
506 int
507 sys_execve(struct lwp *l, const struct sys_execve_args *uap, register_t *retval)
508 {
509 /* {
510 syscallarg(const char *) path;
511 syscallarg(char * const *) argp;
512 syscallarg(char * const *) envp;
513 } */
514
515 return execve1(l, SCARG(uap, path), SCARG(uap, argp),
516 SCARG(uap, envp), execve_fetch_element);
517 }
518
519 int
520 sys_fexecve(struct lwp *l, const struct sys_fexecve_args *uap,
521 register_t *retval)
522 {
523 /* {
524 syscallarg(int) fd;
525 syscallarg(char * const *) argp;
526 syscallarg(char * const *) envp;
527 } */
528
529 return ENOSYS;
530 }
531
532 /*
533 * Load modules to try and execute an image that we do not understand.
534 * If no execsw entries are present, we load those likely to be needed
535 * in order to run native images only. Otherwise, we autoload all
536 * possible modules that could let us run the binary. XXX lame
537 */
538 static void
539 exec_autoload(void)
540 {
541 #ifdef MODULAR
542 static const char * const native[] = {
543 "exec_elf32",
544 "exec_elf64",
545 "exec_script",
546 NULL
547 };
548 static const char * const compat[] = {
549 "exec_elf32",
550 "exec_elf64",
551 "exec_script",
552 "exec_aout",
553 "exec_coff",
554 "exec_ecoff",
555 "compat_aoutm68k",
556 "compat_freebsd",
557 "compat_ibcs2",
558 "compat_linux",
559 "compat_linux32",
560 "compat_netbsd32",
561 "compat_sunos",
562 "compat_sunos32",
563 "compat_svr4",
564 "compat_svr4_32",
565 "compat_ultrix",
566 NULL
567 };
568 char const * const *list;
569 int i;
570
571 list = (nexecs == 0 ? native : compat);
572 for (i = 0; list[i] != NULL; i++) {
573 if (module_autoload(list[i], MODULE_CLASS_EXEC) != 0) {
574 continue;
575 }
576 yield();
577 }
578 #endif
579 }
580
581 static int
582 execve_loadvm(struct lwp *l, const char *path, char * const *args,
583 char * const *envs, execve_fetch_element_t fetch_element,
584 struct execve_data * restrict data)
585 {
586 int error;
587 struct proc *p;
588 char *dp, *sp;
589 size_t i, len;
590 struct exec_fakearg *tmpfap;
591 u_int modgen;
592
593 KASSERT(data != NULL);
594
595 p = l->l_proc;
596 modgen = 0;
597
598 SDT_PROBE(proc,,,exec, path, 0, 0, 0, 0);
599
600 /*
601 * Check if we have exceeded our number of processes limit.
602 * This is so that we handle the case where a root daemon
603 * forked, ran setuid to become the desired user and is trying
604 * to exec. The obvious place to do the reference counting check
605 * is setuid(), but we don't do the reference counting check there
606 * like other OS's do because then all the programs that use setuid()
607 * must be modified to check the return code of setuid() and exit().
608 * It is dangerous to make setuid() fail, because it fails open and
609 * the program will continue to run as root. If we make it succeed
610 * and return an error code, again we are not enforcing the limit.
611 * The best place to enforce the limit is here, when the process tries
612 * to execute a new image, because eventually the process will need
613 * to call exec in order to do something useful.
614 */
615 retry:
616 if (p->p_flag & PK_SUGID) {
617 if (kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
618 p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_BYPASS),
619 &p->p_rlimit[RLIMIT_NPROC],
620 KAUTH_ARG(RLIMIT_NPROC)) != 0 &&
621 chgproccnt(kauth_cred_getuid(l->l_cred), 0) >
622 p->p_rlimit[RLIMIT_NPROC].rlim_cur)
623 return EAGAIN;
624 }
625
626 /*
627 * Drain existing references and forbid new ones. The process
628 * should be left alone until we're done here. This is necessary
629 * to avoid race conditions - e.g. in ptrace() - that might allow
630 * a local user to illicitly obtain elevated privileges.
631 */
632 rw_enter(&p->p_reflock, RW_WRITER);
633
634 /*
635 * Init the namei data to point the file user's program name.
636 * This is done here rather than in check_exec(), so that it's
637 * possible to override this settings if any of makecmd/probe
638 * functions call check_exec() recursively - for example,
639 * see exec_script_makecmds().
640 */
641 error = pathbuf_copyin(path, &data->ed_pathbuf);
642 if (error) {
643 DPRINTF(("%s: pathbuf_copyin path @%p %d\n", __func__,
644 path, error));
645 goto clrflg;
646 }
647 data->ed_pathstring = pathbuf_stringcopy_get(data->ed_pathbuf);
648 data->ed_resolvedpathbuf = PNBUF_GET();
649
650 /*
651 * initialize the fields of the exec package.
652 */
653 data->ed_pack.ep_name = path;
654 data->ed_pack.ep_kname = data->ed_pathstring;
655 data->ed_pack.ep_resolvedname = data->ed_resolvedpathbuf;
656 data->ed_pack.ep_hdr = kmem_alloc(exec_maxhdrsz, KM_SLEEP);
657 data->ed_pack.ep_hdrlen = exec_maxhdrsz;
658 data->ed_pack.ep_hdrvalid = 0;
659 data->ed_pack.ep_emul_arg = NULL;
660 data->ed_pack.ep_emul_arg_free = NULL;
661 data->ed_pack.ep_vmcmds.evs_cnt = 0;
662 data->ed_pack.ep_vmcmds.evs_used = 0;
663 data->ed_pack.ep_vap = &data->ed_attr;
664 data->ed_pack.ep_flags = 0;
665 MD_TOPDOWN_INIT(&data->ed_pack);
666 data->ed_pack.ep_emul_root = NULL;
667 data->ed_pack.ep_interp = NULL;
668 data->ed_pack.ep_esch = NULL;
669 data->ed_pack.ep_pax_flags = 0;
670 memset(data->ed_pack.ep_machine_arch, 0,
671 sizeof(data->ed_pack.ep_machine_arch));
672
673 rw_enter(&exec_lock, RW_READER);
674
675 /* see if we can run it. */
676 if ((error = check_exec(l, &data->ed_pack, data->ed_pathbuf)) != 0) {
677 if (error != ENOENT) {
678 DPRINTF(("%s: check exec failed %d\n",
679 __func__, error));
680 }
681 goto freehdr;
682 }
683
684 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
685
686 /* allocate an argument buffer */
687 data->ed_argp = pool_get(&exec_pool, PR_WAITOK);
688 KASSERT(data->ed_argp != NULL);
689 dp = data->ed_argp;
690 data->ed_argc = 0;
691
692 /* copy the fake args list, if there's one, freeing it as we go */
693 if (data->ed_pack.ep_flags & EXEC_HASARGL) {
694 tmpfap = data->ed_pack.ep_fa;
695 while (tmpfap->fa_arg != NULL) {
696 const char *cp;
697
698 cp = tmpfap->fa_arg;
699 while (*cp)
700 *dp++ = *cp++;
701 *dp++ = '\0';
702 ktrexecarg(tmpfap->fa_arg, cp - tmpfap->fa_arg);
703
704 kmem_free(tmpfap->fa_arg, tmpfap->fa_len);
705 tmpfap++; data->ed_argc++;
706 }
707 kmem_free(data->ed_pack.ep_fa, data->ed_pack.ep_fa_len);
708 data->ed_pack.ep_flags &= ~EXEC_HASARGL;
709 }
710
711 /* Now get argv & environment */
712 if (args == NULL) {
713 DPRINTF(("%s: null args\n", __func__));
714 error = EINVAL;
715 goto bad;
716 }
717 /* 'i' will index the argp/envp element to be retrieved */
718 i = 0;
719 if (data->ed_pack.ep_flags & EXEC_SKIPARG)
720 i++;
721
722 while (1) {
723 len = data->ed_argp + ARG_MAX - dp;
724 if ((error = (*fetch_element)(args, i, &sp)) != 0) {
725 DPRINTF(("%s: fetch_element args %d\n",
726 __func__, error));
727 goto bad;
728 }
729 if (!sp)
730 break;
731 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
732 DPRINTF(("%s: copyinstr args %d\n", __func__, error));
733 if (error == ENAMETOOLONG)
734 error = E2BIG;
735 goto bad;
736 }
737 ktrexecarg(dp, len - 1);
738 dp += len;
739 i++;
740 data->ed_argc++;
741 }
742
743 data->ed_envc = 0;
744 /* environment need not be there */
745 if (envs != NULL) {
746 i = 0;
747 while (1) {
748 len = data->ed_argp + ARG_MAX - dp;
749 if ((error = (*fetch_element)(envs, i, &sp)) != 0) {
750 DPRINTF(("%s: fetch_element env %d\n",
751 __func__, error));
752 goto bad;
753 }
754 if (!sp)
755 break;
756 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
757 DPRINTF(("%s: copyinstr env %d\n",
758 __func__, error));
759 if (error == ENAMETOOLONG)
760 error = E2BIG;
761 goto bad;
762 }
763
764 ktrexecenv(dp, len - 1);
765 dp += len;
766 i++;
767 data->ed_envc++;
768 }
769 }
770
771 dp = (char *) ALIGN(dp);
772
773 data->ed_szsigcode = data->ed_pack.ep_esch->es_emul->e_esigcode -
774 data->ed_pack.ep_esch->es_emul->e_sigcode;
775
776 #ifdef __MACHINE_STACK_GROWS_UP
777 /* See big comment lower down */
778 #define RTLD_GAP 32
779 #else
780 #define RTLD_GAP 0
781 #endif
782
783 /* Now check if args & environ fit into new stack */
784 if (data->ed_pack.ep_flags & EXEC_32) {
785 data->ed_ps_strings_sz = sizeof(struct ps_strings32);
786 len = ((data->ed_argc + data->ed_envc + 2 +
787 data->ed_pack.ep_esch->es_arglen) *
788 sizeof(int) + sizeof(int) + dp + RTLD_GAP +
789 data->ed_szsigcode + data->ed_ps_strings_sz + STACK_PTHREADSPACE)
790 - data->ed_argp;
791 } else {
792 data->ed_ps_strings_sz = sizeof(struct ps_strings);
793 len = ((data->ed_argc + data->ed_envc + 2 +
794 data->ed_pack.ep_esch->es_arglen) *
795 sizeof(char *) + sizeof(int) + dp + RTLD_GAP +
796 data->ed_szsigcode + data->ed_ps_strings_sz + STACK_PTHREADSPACE)
797 - data->ed_argp;
798 }
799
800 #ifdef PAX_ASLR
801 if (pax_aslr_active(l))
802 len += (cprng_fast32() % PAGE_SIZE);
803 #endif /* PAX_ASLR */
804
805 /* make the stack "safely" aligned */
806 len = STACK_LEN_ALIGN(len, STACK_ALIGNBYTES);
807
808 if (len > data->ed_pack.ep_ssize) {
809 /* in effect, compare to initial limit */
810 DPRINTF(("%s: stack limit exceeded %zu\n", __func__, len));
811 goto bad;
812 }
813 /* adjust "active stack depth" for process VSZ */
814 data->ed_pack.ep_ssize = len;
815
816 return 0;
817
818 bad:
819 /* free the vmspace-creation commands, and release their references */
820 kill_vmcmds(&data->ed_pack.ep_vmcmds);
821 /* kill any opened file descriptor, if necessary */
822 if (data->ed_pack.ep_flags & EXEC_HASFD) {
823 data->ed_pack.ep_flags &= ~EXEC_HASFD;
824 fd_close(data->ed_pack.ep_fd);
825 }
826 /* close and put the exec'd file */
827 vn_lock(data->ed_pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
828 VOP_CLOSE(data->ed_pack.ep_vp, FREAD, l->l_cred);
829 vput(data->ed_pack.ep_vp);
830 pool_put(&exec_pool, data->ed_argp);
831
832 freehdr:
833 kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen);
834 if (data->ed_pack.ep_emul_root != NULL)
835 vrele(data->ed_pack.ep_emul_root);
836 if (data->ed_pack.ep_interp != NULL)
837 vrele(data->ed_pack.ep_interp);
838
839 rw_exit(&exec_lock);
840
841 pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring);
842 pathbuf_destroy(data->ed_pathbuf);
843 PNBUF_PUT(data->ed_resolvedpathbuf);
844
845 clrflg:
846 rw_exit(&p->p_reflock);
847
848 if (modgen != module_gen && error == ENOEXEC) {
849 modgen = module_gen;
850 exec_autoload();
851 goto retry;
852 }
853
854 SDT_PROBE(proc,,,exec_failure, error, 0, 0, 0, 0);
855 return error;
856 }
857
858 static void
859 execve_free_data(struct execve_data *data)
860 {
861
862 /* free the vmspace-creation commands, and release their references */
863 kill_vmcmds(&data->ed_pack.ep_vmcmds);
864 /* kill any opened file descriptor, if necessary */
865 if (data->ed_pack.ep_flags & EXEC_HASFD) {
866 data->ed_pack.ep_flags &= ~EXEC_HASFD;
867 fd_close(data->ed_pack.ep_fd);
868 }
869
870 /* close and put the exec'd file */
871 vn_lock(data->ed_pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
872 VOP_CLOSE(data->ed_pack.ep_vp, FREAD, curlwp->l_cred);
873 vput(data->ed_pack.ep_vp);
874 pool_put(&exec_pool, data->ed_argp);
875
876 kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen);
877 if (data->ed_pack.ep_emul_root != NULL)
878 vrele(data->ed_pack.ep_emul_root);
879 if (data->ed_pack.ep_interp != NULL)
880 vrele(data->ed_pack.ep_interp);
881
882 pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring);
883 pathbuf_destroy(data->ed_pathbuf);
884 PNBUF_PUT(data->ed_resolvedpathbuf);
885 }
886
887 static int
888 execve_runproc(struct lwp *l, struct execve_data * restrict data,
889 bool no_local_exec_lock, bool is_spawn)
890 {
891 int error = 0;
892 struct proc *p;
893 size_t i;
894 char *stack, *dp;
895 const char *commandname;
896 struct ps_strings32 arginfo32;
897 struct exec_vmcmd *base_vcp;
898 void *aip;
899 struct vmspace *vm;
900 ksiginfo_t ksi;
901 ksiginfoq_t kq;
902
903 /*
904 * In case of a posix_spawn operation, the child doing the exec
905 * might not hold the reader lock on exec_lock, but the parent
906 * will do this instead.
907 */
908 KASSERT(no_local_exec_lock || rw_lock_held(&exec_lock));
909 KASSERT(data != NULL);
910 if (data == NULL)
911 return EINVAL;
912
913 p = l->l_proc;
914 if (no_local_exec_lock)
915 KASSERT(is_spawn);
916
917 base_vcp = NULL;
918
919 if (data->ed_pack.ep_flags & EXEC_32)
920 aip = &arginfo32;
921 else
922 aip = &data->ed_arginfo;
923
924 /* Get rid of other LWPs. */
925 if (p->p_nlwps > 1) {
926 mutex_enter(p->p_lock);
927 exit_lwps(l);
928 mutex_exit(p->p_lock);
929 }
930 KDASSERT(p->p_nlwps == 1);
931
932 /* Destroy any lwpctl info. */
933 if (p->p_lwpctl != NULL)
934 lwp_ctl_exit();
935
936 /* Remove POSIX timers */
937 timers_free(p, TIMERS_POSIX);
938
939 /*
940 * Do whatever is necessary to prepare the address space
941 * for remapping. Note that this might replace the current
942 * vmspace with another!
943 */
944 if (is_spawn)
945 uvmspace_spawn(l, data->ed_pack.ep_vm_minaddr,
946 data->ed_pack.ep_vm_maxaddr,
947 data->ed_pack.ep_flags & EXEC_TOPDOWN_VM);
948 else
949 uvmspace_exec(l, data->ed_pack.ep_vm_minaddr,
950 data->ed_pack.ep_vm_maxaddr,
951 data->ed_pack.ep_flags & EXEC_TOPDOWN_VM);
952
953 /* record proc's vnode, for use by procfs and others */
954 if (p->p_textvp)
955 vrele(p->p_textvp);
956 vref(data->ed_pack.ep_vp);
957 p->p_textvp = data->ed_pack.ep_vp;
958
959 /* Now map address space */
960 vm = p->p_vmspace;
961 vm->vm_taddr = (void *)data->ed_pack.ep_taddr;
962 vm->vm_tsize = btoc(data->ed_pack.ep_tsize);
963 vm->vm_daddr = (void*)data->ed_pack.ep_daddr;
964 vm->vm_dsize = btoc(data->ed_pack.ep_dsize);
965 vm->vm_ssize = btoc(data->ed_pack.ep_ssize);
966 vm->vm_issize = 0;
967 vm->vm_maxsaddr = (void *)data->ed_pack.ep_maxsaddr;
968 vm->vm_minsaddr = (void *)data->ed_pack.ep_minsaddr;
969
970 #ifdef PAX_ASLR
971 pax_aslr_init(l, vm);
972 #endif /* PAX_ASLR */
973
974 /* create the new process's VM space by running the vmcmds */
975 #ifdef DIAGNOSTIC
976 if (data->ed_pack.ep_vmcmds.evs_used == 0)
977 panic("%s: no vmcmds", __func__);
978 #endif
979
980 #ifdef DEBUG_EXEC
981 {
982 size_t j;
983 struct exec_vmcmd *vp = &data->ed_pack.ep_vmcmds.evs_cmds[0];
984 DPRINTF(("vmcmds %u\n", data->ed_pack.ep_vmcmds.evs_used));
985 for (j = 0; j < data->ed_pack.ep_vmcmds.evs_used; j++) {
986 DPRINTF(("vmcmd[%zu] = vmcmd_map_%s %#"
987 PRIxVADDR"/%#"PRIxVSIZE" fd@%#"
988 PRIxVSIZE" prot=0%o flags=%d\n", j,
989 vp[j].ev_proc == vmcmd_map_pagedvn ?
990 "pagedvn" :
991 vp[j].ev_proc == vmcmd_map_readvn ?
992 "readvn" :
993 vp[j].ev_proc == vmcmd_map_zero ?
994 "zero" : "*unknown*",
995 vp[j].ev_addr, vp[j].ev_len,
996 vp[j].ev_offset, vp[j].ev_prot,
997 vp[j].ev_flags));
998 }
999 }
1000 #endif /* DEBUG_EXEC */
1001
1002 for (i = 0; i < data->ed_pack.ep_vmcmds.evs_used && !error; i++) {
1003 struct exec_vmcmd *vcp;
1004
1005 vcp = &data->ed_pack.ep_vmcmds.evs_cmds[i];
1006 if (vcp->ev_flags & VMCMD_RELATIVE) {
1007 #ifdef DIAGNOSTIC
1008 if (base_vcp == NULL)
1009 panic("%s: relative vmcmd with no base",
1010 __func__);
1011 if (vcp->ev_flags & VMCMD_BASE)
1012 panic("%s: illegal base & relative vmcmd",
1013 __func__);
1014 #endif
1015 vcp->ev_addr += base_vcp->ev_addr;
1016 }
1017 error = (*vcp->ev_proc)(l, vcp);
1018 #ifdef DEBUG_EXEC
1019 if (error) {
1020 size_t j;
1021 struct exec_vmcmd *vp =
1022 &data->ed_pack.ep_vmcmds.evs_cmds[0];
1023 DPRINTF(("vmcmds %zu/%u, error %d\n", i,
1024 data->ed_pack.ep_vmcmds.evs_used, error));
1025 for (j = 0; j < data->ed_pack.ep_vmcmds.evs_used; j++) {
1026 DPRINTF(("vmcmd[%zu] = vmcmd_map_%s %#"
1027 PRIxVADDR"/%#"PRIxVSIZE" fd@%#"
1028 PRIxVSIZE" prot=0%o flags=%d\n", j,
1029 vp[j].ev_proc == vmcmd_map_pagedvn ?
1030 "pagedvn" :
1031 vp[j].ev_proc == vmcmd_map_readvn ?
1032 "readvn" :
1033 vp[j].ev_proc == vmcmd_map_zero ?
1034 "zero" : "*unknown*",
1035 vp[j].ev_addr, vp[j].ev_len,
1036 vp[j].ev_offset, vp[j].ev_prot,
1037 vp[j].ev_flags));
1038 if (j == i)
1039 DPRINTF((" ^--- failed\n"));
1040 }
1041 }
1042 #endif /* DEBUG_EXEC */
1043 if (vcp->ev_flags & VMCMD_BASE)
1044 base_vcp = vcp;
1045 }
1046
1047 /* free the vmspace-creation commands, and release their references */
1048 kill_vmcmds(&data->ed_pack.ep_vmcmds);
1049
1050 vn_lock(data->ed_pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
1051 VOP_CLOSE(data->ed_pack.ep_vp, FREAD, l->l_cred);
1052 vput(data->ed_pack.ep_vp);
1053
1054 /* if an error happened, deallocate and punt */
1055 if (error) {
1056 DPRINTF(("%s: vmcmd %zu failed: %d\n", __func__, i - 1, error));
1057 goto exec_abort;
1058 }
1059
1060 /* remember information about the process */
1061 data->ed_arginfo.ps_nargvstr = data->ed_argc;
1062 data->ed_arginfo.ps_nenvstr = data->ed_envc;
1063
1064 /* set command name & other accounting info */
1065 commandname = strrchr(data->ed_pack.ep_resolvedname, '/');
1066 if (commandname != NULL) {
1067 commandname++;
1068 } else {
1069 commandname = data->ed_pack.ep_resolvedname;
1070 }
1071 i = min(strlen(commandname), MAXCOMLEN);
1072 (void)memcpy(p->p_comm, commandname, i);
1073 p->p_comm[i] = '\0';
1074
1075 dp = PNBUF_GET();
1076 /*
1077 * If the path starts with /, we don't need to do any work.
1078 * This handles the majority of the cases.
1079 * In the future perhaps we could canonicalize it?
1080 */
1081 if (data->ed_pathstring[0] == '/')
1082 (void)strlcpy(data->ed_pack.ep_path = dp, data->ed_pathstring,
1083 MAXPATHLEN);
1084 #ifdef notyet
1085 /*
1086 * Although this works most of the time [since the entry was just
1087 * entered in the cache] we don't use it because it will fail for
1088 * entries that are not placed in the cache because their name is
1089 * longer than NCHNAMLEN and it is not the cleanest interface,
1090 * because there could be races. When the namei cache is re-written,
1091 * this can be changed to use the appropriate function.
1092 */
1093 else if (!(error = vnode_to_path(dp, MAXPATHLEN, p->p_textvp, l, p)))
1094 data->ed_pack.ep_path = dp;
1095 #endif
1096 else {
1097 #ifdef notyet
1098 printf("Cannot get path for pid %d [%s] (error %d)\n",
1099 (int)p->p_pid, p->p_comm, error);
1100 #endif
1101 data->ed_pack.ep_path = NULL;
1102 PNBUF_PUT(dp);
1103 }
1104
1105 stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
1106 STACK_PTHREADSPACE + data->ed_ps_strings_sz + data->ed_szsigcode),
1107 data->ed_pack.ep_ssize - (data->ed_ps_strings_sz + data->ed_szsigcode));
1108
1109 #ifdef __MACHINE_STACK_GROWS_UP
1110 /*
1111 * The copyargs call always copies into lower addresses
1112 * first, moving towards higher addresses, starting with
1113 * the stack pointer that we give. When the stack grows
1114 * down, this puts argc/argv/envp very shallow on the
1115 * stack, right at the first user stack pointer.
1116 * When the stack grows up, the situation is reversed.
1117 *
1118 * Normally, this is no big deal. But the ld_elf.so _rtld()
1119 * function expects to be called with a single pointer to
1120 * a region that has a few words it can stash values into,
1121 * followed by argc/argv/envp. When the stack grows down,
1122 * it's easy to decrement the stack pointer a little bit to
1123 * allocate the space for these few words and pass the new
1124 * stack pointer to _rtld. When the stack grows up, however,
1125 * a few words before argc is part of the signal trampoline, XXX
1126 * so we have a problem.
1127 *
1128 * Instead of changing how _rtld works, we take the easy way
1129 * out and steal 32 bytes before we call copyargs.
1130 * This extra space was allowed for when 'pack.ep_ssize' was calculated.
1131 */
1132 stack += RTLD_GAP;
1133 #endif /* __MACHINE_STACK_GROWS_UP */
1134
1135 /* Now copy argc, args & environ to new stack */
1136 error = (*data->ed_pack.ep_esch->es_copyargs)(l, &data->ed_pack,
1137 &data->ed_arginfo, &stack, data->ed_argp);
1138
1139 if (data->ed_pack.ep_path) {
1140 PNBUF_PUT(data->ed_pack.ep_path);
1141 data->ed_pack.ep_path = NULL;
1142 }
1143 if (error) {
1144 DPRINTF(("%s: copyargs failed %d\n", __func__, error));
1145 goto exec_abort;
1146 }
1147 /* Move the stack back to original point */
1148 stack = (char *)STACK_GROW(vm->vm_minsaddr, data->ed_pack.ep_ssize);
1149
1150 /* fill process ps_strings info */
1151 p->p_psstrp = (vaddr_t)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
1152 STACK_PTHREADSPACE), data->ed_ps_strings_sz);
1153
1154 if (data->ed_pack.ep_flags & EXEC_32) {
1155 arginfo32.ps_argvstr = (vaddr_t)data->ed_arginfo.ps_argvstr;
1156 arginfo32.ps_nargvstr = data->ed_arginfo.ps_nargvstr;
1157 arginfo32.ps_envstr = (vaddr_t)data->ed_arginfo.ps_envstr;
1158 arginfo32.ps_nenvstr = data->ed_arginfo.ps_nenvstr;
1159 }
1160
1161 /* copy out the process's ps_strings structure */
1162 if ((error = copyout(aip, (void *)p->p_psstrp, data->ed_ps_strings_sz))
1163 != 0) {
1164 DPRINTF(("%s: ps_strings copyout %p->%p size %zu failed\n",
1165 __func__, aip, (void *)p->p_psstrp, data->ed_ps_strings_sz));
1166 goto exec_abort;
1167 }
1168
1169 cwdexec(p);
1170 fd_closeexec(); /* handle close on exec */
1171
1172 if (__predict_false(ktrace_on))
1173 fd_ktrexecfd();
1174
1175 execsigs(p); /* reset catched signals */
1176
1177 l->l_ctxlink = NULL; /* reset ucontext link */
1178
1179
1180 p->p_acflag &= ~AFORK;
1181 mutex_enter(p->p_lock);
1182 p->p_flag |= PK_EXEC;
1183 mutex_exit(p->p_lock);
1184
1185 /*
1186 * Stop profiling.
1187 */
1188 if ((p->p_stflag & PST_PROFIL) != 0) {
1189 mutex_spin_enter(&p->p_stmutex);
1190 stopprofclock(p);
1191 mutex_spin_exit(&p->p_stmutex);
1192 }
1193
1194 /*
1195 * It's OK to test PL_PPWAIT unlocked here, as other LWPs have
1196 * exited and exec()/exit() are the only places it will be cleared.
1197 */
1198 if ((p->p_lflag & PL_PPWAIT) != 0) {
1199 #if 0
1200 lwp_t *lp;
1201
1202 mutex_enter(proc_lock);
1203 lp = p->p_vforklwp;
1204 p->p_vforklwp = NULL;
1205
1206 l->l_lwpctl = NULL; /* was on loan from blocked parent */
1207 p->p_lflag &= ~PL_PPWAIT;
1208
1209 lp->l_pflag &= ~LP_VFORKWAIT; /* XXX */
1210 cv_broadcast(&lp->l_waitcv);
1211 mutex_exit(proc_lock);
1212 #else
1213 mutex_enter(proc_lock);
1214 l->l_lwpctl = NULL; /* was on loan from blocked parent */
1215 p->p_lflag &= ~PL_PPWAIT;
1216 cv_broadcast(&p->p_pptr->p_waitcv);
1217 mutex_exit(proc_lock);
1218 #endif
1219 }
1220
1221 /*
1222 * Deal with set[ug]id. MNT_NOSUID has already been used to disable
1223 * s[ug]id. It's OK to check for PSL_TRACED here as we have blocked
1224 * out additional references on the process for the moment.
1225 */
1226 if ((p->p_slflag & PSL_TRACED) == 0 &&
1227
1228 (((data->ed_attr.va_mode & S_ISUID) != 0 &&
1229 kauth_cred_geteuid(l->l_cred) != data->ed_attr.va_uid) ||
1230
1231 ((data->ed_attr.va_mode & S_ISGID) != 0 &&
1232 kauth_cred_getegid(l->l_cred) != data->ed_attr.va_gid))) {
1233 /*
1234 * Mark the process as SUGID before we do
1235 * anything that might block.
1236 */
1237 proc_crmod_enter();
1238 proc_crmod_leave(NULL, NULL, true);
1239
1240 /* Make sure file descriptors 0..2 are in use. */
1241 if ((error = fd_checkstd()) != 0) {
1242 DPRINTF(("%s: fdcheckstd failed %d\n",
1243 __func__, error));
1244 goto exec_abort;
1245 }
1246
1247 /*
1248 * Copy the credential so other references don't see our
1249 * changes.
1250 */
1251 l->l_cred = kauth_cred_copy(l->l_cred);
1252 #ifdef KTRACE
1253 /*
1254 * If the persistent trace flag isn't set, turn off.
1255 */
1256 if (p->p_tracep) {
1257 mutex_enter(&ktrace_lock);
1258 if (!(p->p_traceflag & KTRFAC_PERSISTENT))
1259 ktrderef(p);
1260 mutex_exit(&ktrace_lock);
1261 }
1262 #endif
1263 if (data->ed_attr.va_mode & S_ISUID)
1264 kauth_cred_seteuid(l->l_cred, data->ed_attr.va_uid);
1265 if (data->ed_attr.va_mode & S_ISGID)
1266 kauth_cred_setegid(l->l_cred, data->ed_attr.va_gid);
1267 } else {
1268 if (kauth_cred_geteuid(l->l_cred) ==
1269 kauth_cred_getuid(l->l_cred) &&
1270 kauth_cred_getegid(l->l_cred) ==
1271 kauth_cred_getgid(l->l_cred))
1272 p->p_flag &= ~PK_SUGID;
1273 }
1274
1275 /*
1276 * Copy the credential so other references don't see our changes.
1277 * Test to see if this is necessary first, since in the common case
1278 * we won't need a private reference.
1279 */
1280 if (kauth_cred_geteuid(l->l_cred) != kauth_cred_getsvuid(l->l_cred) ||
1281 kauth_cred_getegid(l->l_cred) != kauth_cred_getsvgid(l->l_cred)) {
1282 l->l_cred = kauth_cred_copy(l->l_cred);
1283 kauth_cred_setsvuid(l->l_cred, kauth_cred_geteuid(l->l_cred));
1284 kauth_cred_setsvgid(l->l_cred, kauth_cred_getegid(l->l_cred));
1285 }
1286
1287 /* Update the master credentials. */
1288 if (l->l_cred != p->p_cred) {
1289 kauth_cred_t ocred;
1290
1291 kauth_cred_hold(l->l_cred);
1292 mutex_enter(p->p_lock);
1293 ocred = p->p_cred;
1294 p->p_cred = l->l_cred;
1295 mutex_exit(p->p_lock);
1296 kauth_cred_free(ocred);
1297 }
1298
1299 #if defined(__HAVE_RAS)
1300 /*
1301 * Remove all RASs from the address space.
1302 */
1303 ras_purgeall();
1304 #endif
1305
1306 doexechooks(p);
1307
1308 /* setup new registers and do misc. setup. */
1309 (*data->ed_pack.ep_esch->es_emul->e_setregs)(l, &data->ed_pack,
1310 (vaddr_t)stack);
1311 if (data->ed_pack.ep_esch->es_setregs)
1312 (*data->ed_pack.ep_esch->es_setregs)(l, &data->ed_pack,
1313 (vaddr_t)stack);
1314
1315 /* Provide a consistent LWP private setting */
1316 (void)lwp_setprivate(l, NULL);
1317
1318 /* Discard all PCU state; need to start fresh */
1319 pcu_discard_all(l);
1320
1321 /* map the process's signal trampoline code */
1322 if ((error = exec_sigcode_map(p, data->ed_pack.ep_esch->es_emul)) != 0) {
1323 DPRINTF(("%s: map sigcode failed %d\n", __func__, error));
1324 goto exec_abort;
1325 }
1326
1327 pool_put(&exec_pool, data->ed_argp);
1328
1329 /* notify others that we exec'd */
1330 KNOTE(&p->p_klist, NOTE_EXEC);
1331
1332 kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen);
1333
1334 SDT_PROBE(proc,,,exec_success, data->ed_pack.ep_name, 0, 0, 0, 0);
1335
1336 /* The emulation root will usually have been found when we looked
1337 * for the elf interpreter (or similar), if not look now. */
1338 if (data->ed_pack.ep_esch->es_emul->e_path != NULL &&
1339 data->ed_pack.ep_emul_root == NULL)
1340 emul_find_root(l, &data->ed_pack);
1341
1342 /* Any old emulation root got removed by fdcloseexec */
1343 rw_enter(&p->p_cwdi->cwdi_lock, RW_WRITER);
1344 p->p_cwdi->cwdi_edir = data->ed_pack.ep_emul_root;
1345 rw_exit(&p->p_cwdi->cwdi_lock);
1346 data->ed_pack.ep_emul_root = NULL;
1347 if (data->ed_pack.ep_interp != NULL)
1348 vrele(data->ed_pack.ep_interp);
1349
1350 /*
1351 * Call emulation specific exec hook. This can setup per-process
1352 * p->p_emuldata or do any other per-process stuff an emulation needs.
1353 *
1354 * If we are executing process of different emulation than the
1355 * original forked process, call e_proc_exit() of the old emulation
1356 * first, then e_proc_exec() of new emulation. If the emulation is
1357 * same, the exec hook code should deallocate any old emulation
1358 * resources held previously by this process.
1359 */
1360 if (p->p_emul && p->p_emul->e_proc_exit
1361 && p->p_emul != data->ed_pack.ep_esch->es_emul)
1362 (*p->p_emul->e_proc_exit)(p);
1363
1364 /*
1365 * This is now LWP 1.
1366 */
1367 mutex_enter(p->p_lock);
1368 p->p_nlwpid = 1;
1369 l->l_lid = 1;
1370 mutex_exit(p->p_lock);
1371
1372 /*
1373 * Call exec hook. Emulation code may NOT store reference to anything
1374 * from &pack.
1375 */
1376 if (data->ed_pack.ep_esch->es_emul->e_proc_exec)
1377 (*data->ed_pack.ep_esch->es_emul->e_proc_exec)(p, &data->ed_pack);
1378
1379 /* update p_emul, the old value is no longer needed */
1380 p->p_emul = data->ed_pack.ep_esch->es_emul;
1381
1382 /* ...and the same for p_execsw */
1383 p->p_execsw = data->ed_pack.ep_esch;
1384
1385 #ifdef __HAVE_SYSCALL_INTERN
1386 (*p->p_emul->e_syscall_intern)(p);
1387 #endif
1388 ktremul();
1389
1390 /* Allow new references from the debugger/procfs. */
1391 rw_exit(&p->p_reflock);
1392 if (!no_local_exec_lock)
1393 rw_exit(&exec_lock);
1394
1395 mutex_enter(proc_lock);
1396
1397 if ((p->p_slflag & (PSL_TRACED|PSL_SYSCALL)) == PSL_TRACED) {
1398 KSI_INIT_EMPTY(&ksi);
1399 ksi.ksi_signo = SIGTRAP;
1400 ksi.ksi_lid = l->l_lid;
1401 kpsignal(p, &ksi, NULL);
1402 }
1403
1404 if (p->p_sflag & PS_STOPEXEC) {
1405 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
1406 p->p_pptr->p_nstopchild++;
1407 p->p_pptr->p_waited = 0;
1408 mutex_enter(p->p_lock);
1409 ksiginfo_queue_init(&kq);
1410 sigclearall(p, &contsigmask, &kq);
1411 lwp_lock(l);
1412 l->l_stat = LSSTOP;
1413 p->p_stat = SSTOP;
1414 p->p_nrlwps--;
1415 lwp_unlock(l);
1416 mutex_exit(p->p_lock);
1417 mutex_exit(proc_lock);
1418 lwp_lock(l);
1419 mi_switch(l);
1420 ksiginfo_queue_drain(&kq);
1421 KERNEL_LOCK(l->l_biglocks, l);
1422 } else {
1423 mutex_exit(proc_lock);
1424 }
1425
1426 pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring);
1427 pathbuf_destroy(data->ed_pathbuf);
1428 PNBUF_PUT(data->ed_resolvedpathbuf);
1429 DPRINTF(("%s finished\n", __func__));
1430 return EJUSTRETURN;
1431
1432 exec_abort:
1433 SDT_PROBE(proc,,,exec_failure, error, 0, 0, 0, 0);
1434 rw_exit(&p->p_reflock);
1435 if (!no_local_exec_lock)
1436 rw_exit(&exec_lock);
1437
1438 pathbuf_stringcopy_put(data->ed_pathbuf, data->ed_pathstring);
1439 pathbuf_destroy(data->ed_pathbuf);
1440 PNBUF_PUT(data->ed_resolvedpathbuf);
1441
1442 /*
1443 * the old process doesn't exist anymore. exit gracefully.
1444 * get rid of the (new) address space we have created, if any, get rid
1445 * of our namei data and vnode, and exit noting failure
1446 */
1447 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
1448 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
1449
1450 exec_free_emul_arg(&data->ed_pack);
1451 pool_put(&exec_pool, data->ed_argp);
1452 kmem_free(data->ed_pack.ep_hdr, data->ed_pack.ep_hdrlen);
1453 if (data->ed_pack.ep_emul_root != NULL)
1454 vrele(data->ed_pack.ep_emul_root);
1455 if (data->ed_pack.ep_interp != NULL)
1456 vrele(data->ed_pack.ep_interp);
1457
1458 /* Acquire the sched-state mutex (exit1() will release it). */
1459 if (!is_spawn) {
1460 mutex_enter(p->p_lock);
1461 exit1(l, W_EXITCODE(error, SIGABRT));
1462 }
1463
1464 return error;
1465 }
1466
1467 int
1468 execve1(struct lwp *l, const char *path, char * const *args,
1469 char * const *envs, execve_fetch_element_t fetch_element)
1470 {
1471 struct execve_data data;
1472 int error;
1473
1474 error = execve_loadvm(l, path, args, envs, fetch_element, &data);
1475 if (error)
1476 return error;
1477 error = execve_runproc(l, &data, false, false);
1478 return error;
1479 }
1480
1481 int
1482 copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo,
1483 char **stackp, void *argp)
1484 {
1485 char **cpp, *dp, *sp;
1486 size_t len;
1487 void *nullp;
1488 long argc, envc;
1489 int error;
1490
1491 cpp = (char **)*stackp;
1492 nullp = NULL;
1493 argc = arginfo->ps_nargvstr;
1494 envc = arginfo->ps_nenvstr;
1495 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0) {
1496 COPYPRINTF("", cpp - 1, sizeof(argc));
1497 return error;
1498 }
1499
1500 dp = (char *) (cpp + argc + envc + 2 + pack->ep_esch->es_arglen);
1501 sp = argp;
1502
1503 /* XXX don't copy them out, remap them! */
1504 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
1505
1506 for (; --argc >= 0; sp += len, dp += len) {
1507 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0) {
1508 COPYPRINTF("", cpp - 1, sizeof(dp));
1509 return error;
1510 }
1511 if ((error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) {
1512 COPYPRINTF("str", dp, (size_t)ARG_MAX);
1513 return error;
1514 }
1515 }
1516
1517 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) {
1518 COPYPRINTF("", cpp - 1, sizeof(nullp));
1519 return error;
1520 }
1521
1522 arginfo->ps_envstr = cpp; /* remember location of envp for later */
1523
1524 for (; --envc >= 0; sp += len, dp += len) {
1525 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0) {
1526 COPYPRINTF("", cpp - 1, sizeof(dp));
1527 return error;
1528 }
1529 if ((error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0) {
1530 COPYPRINTF("str", dp, (size_t)ARG_MAX);
1531 return error;
1532 }
1533
1534 }
1535
1536 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0) {
1537 COPYPRINTF("", cpp - 1, sizeof(nullp));
1538 return error;
1539 }
1540
1541 *stackp = (char *)cpp;
1542 return 0;
1543 }
1544
1545
1546 /*
1547 * Add execsw[] entries.
1548 */
1549 int
1550 exec_add(struct execsw *esp, int count)
1551 {
1552 struct exec_entry *it;
1553 int i;
1554
1555 if (count == 0) {
1556 return 0;
1557 }
1558
1559 /* Check for duplicates. */
1560 rw_enter(&exec_lock, RW_WRITER);
1561 for (i = 0; i < count; i++) {
1562 LIST_FOREACH(it, &ex_head, ex_list) {
1563 /* assume unique (makecmds, probe_func, emulation) */
1564 if (it->ex_sw->es_makecmds == esp[i].es_makecmds &&
1565 it->ex_sw->u.elf_probe_func ==
1566 esp[i].u.elf_probe_func &&
1567 it->ex_sw->es_emul == esp[i].es_emul) {
1568 rw_exit(&exec_lock);
1569 return EEXIST;
1570 }
1571 }
1572 }
1573
1574 /* Allocate new entries. */
1575 for (i = 0; i < count; i++) {
1576 it = kmem_alloc(sizeof(*it), KM_SLEEP);
1577 it->ex_sw = &esp[i];
1578 LIST_INSERT_HEAD(&ex_head, it, ex_list);
1579 }
1580
1581 /* update execsw[] */
1582 exec_init(0);
1583 rw_exit(&exec_lock);
1584 return 0;
1585 }
1586
1587 /*
1588 * Remove execsw[] entry.
1589 */
1590 int
1591 exec_remove(struct execsw *esp, int count)
1592 {
1593 struct exec_entry *it, *next;
1594 int i;
1595 const struct proclist_desc *pd;
1596 proc_t *p;
1597
1598 if (count == 0) {
1599 return 0;
1600 }
1601
1602 /* Abort if any are busy. */
1603 rw_enter(&exec_lock, RW_WRITER);
1604 for (i = 0; i < count; i++) {
1605 mutex_enter(proc_lock);
1606 for (pd = proclists; pd->pd_list != NULL; pd++) {
1607 PROCLIST_FOREACH(p, pd->pd_list) {
1608 if (p->p_execsw == &esp[i]) {
1609 mutex_exit(proc_lock);
1610 rw_exit(&exec_lock);
1611 return EBUSY;
1612 }
1613 }
1614 }
1615 mutex_exit(proc_lock);
1616 }
1617
1618 /* None are busy, so remove them all. */
1619 for (i = 0; i < count; i++) {
1620 for (it = LIST_FIRST(&ex_head); it != NULL; it = next) {
1621 next = LIST_NEXT(it, ex_list);
1622 if (it->ex_sw == &esp[i]) {
1623 LIST_REMOVE(it, ex_list);
1624 kmem_free(it, sizeof(*it));
1625 break;
1626 }
1627 }
1628 }
1629
1630 /* update execsw[] */
1631 exec_init(0);
1632 rw_exit(&exec_lock);
1633 return 0;
1634 }
1635
1636 /*
1637 * Initialize exec structures. If init_boot is true, also does necessary
1638 * one-time initialization (it's called from main() that way).
1639 * Once system is multiuser, this should be called with exec_lock held,
1640 * i.e. via exec_{add|remove}().
1641 */
1642 int
1643 exec_init(int init_boot)
1644 {
1645 const struct execsw **sw;
1646 struct exec_entry *ex;
1647 SLIST_HEAD(,exec_entry) first;
1648 SLIST_HEAD(,exec_entry) any;
1649 SLIST_HEAD(,exec_entry) last;
1650 int i, sz;
1651
1652 if (init_boot) {
1653 /* do one-time initializations */
1654 rw_init(&exec_lock);
1655 mutex_init(&sigobject_lock, MUTEX_DEFAULT, IPL_NONE);
1656 pool_init(&exec_pool, NCARGS, 0, 0, PR_NOALIGN|PR_NOTOUCH,
1657 "execargs", &exec_palloc, IPL_NONE);
1658 pool_sethardlimit(&exec_pool, maxexec, "should not happen", 0);
1659 } else {
1660 KASSERT(rw_write_held(&exec_lock));
1661 }
1662
1663 /* Sort each entry onto the appropriate queue. */
1664 SLIST_INIT(&first);
1665 SLIST_INIT(&any);
1666 SLIST_INIT(&last);
1667 sz = 0;
1668 LIST_FOREACH(ex, &ex_head, ex_list) {
1669 switch(ex->ex_sw->es_prio) {
1670 case EXECSW_PRIO_FIRST:
1671 SLIST_INSERT_HEAD(&first, ex, ex_slist);
1672 break;
1673 case EXECSW_PRIO_ANY:
1674 SLIST_INSERT_HEAD(&any, ex, ex_slist);
1675 break;
1676 case EXECSW_PRIO_LAST:
1677 SLIST_INSERT_HEAD(&last, ex, ex_slist);
1678 break;
1679 default:
1680 panic("%s", __func__);
1681 break;
1682 }
1683 sz++;
1684 }
1685
1686 /*
1687 * Create new execsw[]. Ensure we do not try a zero-sized
1688 * allocation.
1689 */
1690 sw = kmem_alloc(sz * sizeof(struct execsw *) + 1, KM_SLEEP);
1691 i = 0;
1692 SLIST_FOREACH(ex, &first, ex_slist) {
1693 sw[i++] = ex->ex_sw;
1694 }
1695 SLIST_FOREACH(ex, &any, ex_slist) {
1696 sw[i++] = ex->ex_sw;
1697 }
1698 SLIST_FOREACH(ex, &last, ex_slist) {
1699 sw[i++] = ex->ex_sw;
1700 }
1701
1702 /* Replace old execsw[] and free used memory. */
1703 if (execsw != NULL) {
1704 kmem_free(__UNCONST(execsw),
1705 nexecs * sizeof(struct execsw *) + 1);
1706 }
1707 execsw = sw;
1708 nexecs = sz;
1709
1710 /* Figure out the maximum size of an exec header. */
1711 exec_maxhdrsz = sizeof(int);
1712 for (i = 0; i < nexecs; i++) {
1713 if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1714 exec_maxhdrsz = execsw[i]->es_hdrsz;
1715 }
1716
1717 return 0;
1718 }
1719
1720 static int
1721 exec_sigcode_map(struct proc *p, const struct emul *e)
1722 {
1723 vaddr_t va;
1724 vsize_t sz;
1725 int error;
1726 struct uvm_object *uobj;
1727
1728 sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
1729
1730 if (e->e_sigobject == NULL || sz == 0) {
1731 return 0;
1732 }
1733
1734 /*
1735 * If we don't have a sigobject for this emulation, create one.
1736 *
1737 * sigobject is an anonymous memory object (just like SYSV shared
1738 * memory) that we keep a permanent reference to and that we map
1739 * in all processes that need this sigcode. The creation is simple,
1740 * we create an object, add a permanent reference to it, map it in
1741 * kernel space, copy out the sigcode to it and unmap it.
1742 * We map it with PROT_READ|PROT_EXEC into the process just
1743 * the way sys_mmap() would map it.
1744 */
1745
1746 uobj = *e->e_sigobject;
1747 if (uobj == NULL) {
1748 mutex_enter(&sigobject_lock);
1749 if ((uobj = *e->e_sigobject) == NULL) {
1750 uobj = uao_create(sz, 0);
1751 (*uobj->pgops->pgo_reference)(uobj);
1752 va = vm_map_min(kernel_map);
1753 if ((error = uvm_map(kernel_map, &va, round_page(sz),
1754 uobj, 0, 0,
1755 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
1756 UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) {
1757 printf("kernel mapping failed %d\n", error);
1758 (*uobj->pgops->pgo_detach)(uobj);
1759 mutex_exit(&sigobject_lock);
1760 return error;
1761 }
1762 memcpy((void *)va, e->e_sigcode, sz);
1763 #ifdef PMAP_NEED_PROCWR
1764 pmap_procwr(&proc0, va, sz);
1765 #endif
1766 uvm_unmap(kernel_map, va, va + round_page(sz));
1767 *e->e_sigobject = uobj;
1768 }
1769 mutex_exit(&sigobject_lock);
1770 }
1771
1772 /* Just a hint to uvm_map where to put it. */
1773 va = e->e_vm_default_addr(p, (vaddr_t)p->p_vmspace->vm_daddr,
1774 round_page(sz));
1775
1776 #ifdef __alpha__
1777 /*
1778 * Tru64 puts /sbin/loader at the end of user virtual memory,
1779 * which causes the above calculation to put the sigcode at
1780 * an invalid address. Put it just below the text instead.
1781 */
1782 if (va == (vaddr_t)vm_map_max(&p->p_vmspace->vm_map)) {
1783 va = (vaddr_t)p->p_vmspace->vm_taddr - round_page(sz);
1784 }
1785 #endif
1786
1787 (*uobj->pgops->pgo_reference)(uobj);
1788 error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz),
1789 uobj, 0, 0,
1790 UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE,
1791 UVM_ADV_RANDOM, 0));
1792 if (error) {
1793 DPRINTF(("%s, %d: map %p "
1794 "uvm_map %#"PRIxVSIZE"@%#"PRIxVADDR" failed %d\n",
1795 __func__, __LINE__, &p->p_vmspace->vm_map, round_page(sz),
1796 va, error));
1797 (*uobj->pgops->pgo_detach)(uobj);
1798 return error;
1799 }
1800 p->p_sigctx.ps_sigcode = (void *)va;
1801 return 0;
1802 }
1803
1804 /*
1805 * Release a refcount on spawn_exec_data and destroy memory, if this
1806 * was the last one.
1807 */
1808 static void
1809 spawn_exec_data_release(struct spawn_exec_data *data)
1810 {
1811 if (atomic_dec_32_nv(&data->sed_refcnt) != 0)
1812 return;
1813
1814 cv_destroy(&data->sed_cv_child_ready);
1815 mutex_destroy(&data->sed_mtx_child);
1816
1817 if (data->sed_actions)
1818 posix_spawn_fa_free(data->sed_actions,
1819 data->sed_actions->len);
1820 if (data->sed_attrs)
1821 kmem_free(data->sed_attrs,
1822 sizeof(*data->sed_attrs));
1823 kmem_free(data, sizeof(*data));
1824 }
1825
1826 /*
1827 * A child lwp of a posix_spawn operation starts here and ends up in
1828 * cpu_spawn_return, dealing with all filedescriptor and scheduler
1829 * manipulations in between.
1830 * The parent waits for the child, as it is not clear whether the child
1831 * will be able to acquire its own exec_lock. If it can, the parent can
1832 * be released early and continue running in parallel. If not (or if the
1833 * magic debug flag is passed in the scheduler attribute struct), the
1834 * child rides on the parent's exec lock until it is ready to return to
1835 * to userland - and only then releases the parent. This method loses
1836 * concurrency, but improves error reporting.
1837 */
1838 static void
1839 spawn_return(void *arg)
1840 {
1841 struct spawn_exec_data *spawn_data = arg;
1842 struct lwp *l = curlwp;
1843 int error, newfd;
1844 size_t i;
1845 const struct posix_spawn_file_actions_entry *fae;
1846 pid_t ppid;
1847 register_t retval;
1848 bool have_reflock;
1849 bool parent_is_waiting = true;
1850
1851 /*
1852 * Check if we can release parent early.
1853 * We either need to have no sed_attrs, or sed_attrs does not
1854 * have POSIX_SPAWN_RETURNERROR or one of the flags, that require
1855 * safe access to the parent proc (passed in sed_parent).
1856 * We then try to get the exec_lock, and only if that works, we can
1857 * release the parent here already.
1858 */
1859 ppid = spawn_data->sed_parent->p_pid;
1860 if ((!spawn_data->sed_attrs
1861 || (spawn_data->sed_attrs->sa_flags
1862 & (POSIX_SPAWN_RETURNERROR|POSIX_SPAWN_SETPGROUP)) == 0)
1863 && rw_tryenter(&exec_lock, RW_READER)) {
1864 parent_is_waiting = false;
1865 mutex_enter(&spawn_data->sed_mtx_child);
1866 cv_signal(&spawn_data->sed_cv_child_ready);
1867 mutex_exit(&spawn_data->sed_mtx_child);
1868 }
1869
1870 /* don't allow debugger access yet */
1871 rw_enter(&l->l_proc->p_reflock, RW_WRITER);
1872 have_reflock = true;
1873
1874 error = 0;
1875 /* handle posix_spawn_file_actions */
1876 if (spawn_data->sed_actions != NULL) {
1877 for (i = 0; i < spawn_data->sed_actions->len; i++) {
1878 fae = &spawn_data->sed_actions->fae[i];
1879 switch (fae->fae_action) {
1880 case FAE_OPEN:
1881 if (fd_getfile(fae->fae_fildes) != NULL) {
1882 error = fd_close(fae->fae_fildes);
1883 if (error)
1884 break;
1885 }
1886 error = fd_open(fae->fae_path, fae->fae_oflag,
1887 fae->fae_mode, &newfd);
1888 if (error)
1889 break;
1890 if (newfd != fae->fae_fildes) {
1891 error = dodup(l, newfd,
1892 fae->fae_fildes, 0, &retval);
1893 if (fd_getfile(newfd) != NULL)
1894 fd_close(newfd);
1895 }
1896 break;
1897 case FAE_DUP2:
1898 error = dodup(l, fae->fae_fildes,
1899 fae->fae_newfildes, 0, &retval);
1900 break;
1901 case FAE_CLOSE:
1902 if (fd_getfile(fae->fae_fildes) == NULL) {
1903 error = EBADF;
1904 break;
1905 }
1906 error = fd_close(fae->fae_fildes);
1907 break;
1908 }
1909 if (error)
1910 goto report_error;
1911 }
1912 }
1913
1914 /* handle posix_spawnattr */
1915 if (spawn_data->sed_attrs != NULL) {
1916 int ostat;
1917 struct sigaction sigact;
1918 sigact._sa_u._sa_handler = SIG_DFL;
1919 sigact.sa_flags = 0;
1920
1921 /*
1922 * set state to SSTOP so that this proc can be found by pid.
1923 * see proc_enterprp, do_sched_setparam below
1924 */
1925 ostat = l->l_proc->p_stat;
1926 l->l_proc->p_stat = SSTOP;
1927
1928 /* Set process group */
1929 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETPGROUP) {
1930 pid_t mypid = l->l_proc->p_pid,
1931 pgrp = spawn_data->sed_attrs->sa_pgroup;
1932
1933 if (pgrp == 0)
1934 pgrp = mypid;
1935
1936 error = proc_enterpgrp(spawn_data->sed_parent,
1937 mypid, pgrp, false);
1938 if (error)
1939 goto report_error;
1940 }
1941
1942 /* Set scheduler policy */
1943 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSCHEDULER)
1944 error = do_sched_setparam(l->l_proc->p_pid, 0,
1945 spawn_data->sed_attrs->sa_schedpolicy,
1946 &spawn_data->sed_attrs->sa_schedparam);
1947 else if (spawn_data->sed_attrs->sa_flags
1948 & POSIX_SPAWN_SETSCHEDPARAM) {
1949 error = do_sched_setparam(ppid, 0,
1950 SCHED_NONE, &spawn_data->sed_attrs->sa_schedparam);
1951 }
1952 if (error)
1953 goto report_error;
1954
1955 /* Reset user ID's */
1956 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_RESETIDS) {
1957 error = do_setresuid(l, -1,
1958 kauth_cred_getgid(l->l_cred), -1,
1959 ID_E_EQ_R | ID_E_EQ_S);
1960 if (error)
1961 goto report_error;
1962 error = do_setresuid(l, -1,
1963 kauth_cred_getuid(l->l_cred), -1,
1964 ID_E_EQ_R | ID_E_EQ_S);
1965 if (error)
1966 goto report_error;
1967 }
1968
1969 /* Set signal masks/defaults */
1970 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSIGMASK) {
1971 mutex_enter(l->l_proc->p_lock);
1972 error = sigprocmask1(l, SIG_SETMASK,
1973 &spawn_data->sed_attrs->sa_sigmask, NULL);
1974 mutex_exit(l->l_proc->p_lock);
1975 if (error)
1976 goto report_error;
1977 }
1978
1979 if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSIGDEF) {
1980 /*
1981 * The following sigaction call is using a sigaction
1982 * version 0 trampoline which is in the compatibility
1983 * code only. This is not a problem because for SIG_DFL
1984 * and SIG_IGN, the trampolines are now ignored. If they
1985 * were not, this would be a problem because we are
1986 * holding the exec_lock, and the compat code needs
1987 * to do the same in order to replace the trampoline
1988 * code of the process.
1989 */
1990 for (i = 1; i <= NSIG; i++) {
1991 if (sigismember(
1992 &spawn_data->sed_attrs->sa_sigdefault, i))
1993 sigaction1(l, i, &sigact, NULL, NULL,
1994 0);
1995 }
1996 }
1997 l->l_proc->p_stat = ostat;
1998 }
1999
2000 /* now do the real exec */
2001 error = execve_runproc(l, &spawn_data->sed_exec, parent_is_waiting,
2002 true);
2003 have_reflock = false;
2004 if (error == EJUSTRETURN)
2005 error = 0;
2006 else if (error)
2007 goto report_error;
2008
2009 if (parent_is_waiting) {
2010 mutex_enter(&spawn_data->sed_mtx_child);
2011 cv_signal(&spawn_data->sed_cv_child_ready);
2012 mutex_exit(&spawn_data->sed_mtx_child);
2013 }
2014
2015 /* release our refcount on the data */
2016 spawn_exec_data_release(spawn_data);
2017
2018 /* and finally: leave to userland for the first time */
2019 cpu_spawn_return(l);
2020
2021 /* NOTREACHED */
2022 return;
2023
2024 report_error:
2025 if (have_reflock) {
2026 /*
2027 * We have not passed through execve_runproc(),
2028 * which would have released the p_reflock and also
2029 * taken ownership of the sed_exec part of spawn_data,
2030 * so release/free both here.
2031 */
2032 rw_exit(&l->l_proc->p_reflock);
2033 execve_free_data(&spawn_data->sed_exec);
2034 }
2035
2036 if (parent_is_waiting) {
2037 /* pass error to parent */
2038 mutex_enter(&spawn_data->sed_mtx_child);
2039 spawn_data->sed_error = error;
2040 cv_signal(&spawn_data->sed_cv_child_ready);
2041 mutex_exit(&spawn_data->sed_mtx_child);
2042 } else {
2043 rw_exit(&exec_lock);
2044 }
2045
2046 /* release our refcount on the data */
2047 spawn_exec_data_release(spawn_data);
2048
2049 /* done, exit */
2050 mutex_enter(l->l_proc->p_lock);
2051 /*
2052 * Posix explicitly asks for an exit code of 127 if we report
2053 * errors from the child process - so, unfortunately, there
2054 * is no way to report a more exact error code.
2055 * A NetBSD specific workaround is POSIX_SPAWN_RETURNERROR as
2056 * flag bit in the attrp argument to posix_spawn(2), see above.
2057 */
2058 exit1(l, W_EXITCODE(127, 0));
2059 }
2060
2061 void
2062 posix_spawn_fa_free(struct posix_spawn_file_actions *fa, size_t len)
2063 {
2064
2065 for (size_t i = 0; i < len; i++) {
2066 struct posix_spawn_file_actions_entry *fae = &fa->fae[i];
2067 if (fae->fae_action != FAE_OPEN)
2068 continue;
2069 kmem_free(fae->fae_path, strlen(fae->fae_path) + 1);
2070 }
2071 if (fa->len > 0)
2072 kmem_free(fa->fae, sizeof(*fa->fae) * fa->len);
2073 kmem_free(fa, sizeof(*fa));
2074 }
2075
2076 static int
2077 posix_spawn_fa_alloc(struct posix_spawn_file_actions **fap,
2078 const struct posix_spawn_file_actions *ufa, rlim_t lim)
2079 {
2080 struct posix_spawn_file_actions *fa;
2081 struct posix_spawn_file_actions_entry *fae;
2082 char *pbuf = NULL;
2083 int error;
2084 size_t i = 0;
2085
2086 fa = kmem_alloc(sizeof(*fa), KM_SLEEP);
2087 error = copyin(ufa, fa, sizeof(*fa));
2088 if (error || fa->len == 0) {
2089 kmem_free(fa, sizeof(*fa));
2090 return error; /* 0 if not an error, and len == 0 */
2091 }
2092
2093 if (fa->len > lim) {
2094 kmem_free(fa, sizeof(*fa));
2095 return EINVAL;
2096 }
2097
2098 fa->size = fa->len;
2099 size_t fal = fa->len * sizeof(*fae);
2100 fae = fa->fae;
2101 fa->fae = kmem_alloc(fal, KM_SLEEP);
2102 error = copyin(fae, fa->fae, fal);
2103 if (error)
2104 goto out;
2105
2106 pbuf = PNBUF_GET();
2107 for (; i < fa->len; i++) {
2108 fae = &fa->fae[i];
2109 if (fae->fae_action != FAE_OPEN)
2110 continue;
2111 error = copyinstr(fae->fae_path, pbuf, MAXPATHLEN, &fal);
2112 if (error)
2113 goto out;
2114 fae->fae_path = kmem_alloc(fal, KM_SLEEP);
2115 memcpy(fae->fae_path, pbuf, fal);
2116 }
2117 PNBUF_PUT(pbuf);
2118
2119 *fap = fa;
2120 return 0;
2121 out:
2122 if (pbuf)
2123 PNBUF_PUT(pbuf);
2124 posix_spawn_fa_free(fa, i);
2125 return error;
2126 }
2127
2128 int
2129 check_posix_spawn(struct lwp *l1)
2130 {
2131 int error, tnprocs, count;
2132 uid_t uid;
2133 struct proc *p1;
2134
2135 p1 = l1->l_proc;
2136 uid = kauth_cred_getuid(l1->l_cred);
2137 tnprocs = atomic_inc_uint_nv(&nprocs);
2138
2139 /*
2140 * Although process entries are dynamically created, we still keep
2141 * a global limit on the maximum number we will create.
2142 */
2143 if (__predict_false(tnprocs >= maxproc))
2144 error = -1;
2145 else
2146 error = kauth_authorize_process(l1->l_cred,
2147 KAUTH_PROCESS_FORK, p1, KAUTH_ARG(tnprocs), NULL, NULL);
2148
2149 if (error) {
2150 atomic_dec_uint(&nprocs);
2151 return EAGAIN;
2152 }
2153
2154 /*
2155 * Enforce limits.
2156 */
2157 count = chgproccnt(uid, 1);
2158 if (kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_RLIMIT,
2159 p1, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_BYPASS),
2160 &p1->p_rlimit[RLIMIT_NPROC], KAUTH_ARG(RLIMIT_NPROC)) != 0 &&
2161 __predict_false(count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)) {
2162 (void)chgproccnt(uid, -1);
2163 atomic_dec_uint(&nprocs);
2164 return EAGAIN;
2165 }
2166
2167 return 0;
2168 }
2169
2170 int
2171 do_posix_spawn(struct lwp *l1, pid_t *pid_res, bool *child_ok, const char *path,
2172 struct posix_spawn_file_actions *fa,
2173 struct posix_spawnattr *sa,
2174 char *const *argv, char *const *envp,
2175 execve_fetch_element_t fetch)
2176 {
2177
2178 struct proc *p1, *p2;
2179 struct lwp *l2;
2180 int error;
2181 struct spawn_exec_data *spawn_data;
2182 vaddr_t uaddr;
2183 pid_t pid;
2184 bool have_exec_lock = false;
2185
2186 p1 = l1->l_proc;
2187
2188 /* Allocate and init spawn_data */
2189 spawn_data = kmem_zalloc(sizeof(*spawn_data), KM_SLEEP);
2190 spawn_data->sed_refcnt = 1; /* only parent so far */
2191 cv_init(&spawn_data->sed_cv_child_ready, "pspawn");
2192 mutex_init(&spawn_data->sed_mtx_child, MUTEX_DEFAULT, IPL_NONE);
2193 mutex_enter(&spawn_data->sed_mtx_child);
2194
2195 /*
2196 * Do the first part of the exec now, collect state
2197 * in spawn_data.
2198 */
2199 error = execve_loadvm(l1, path, argv,
2200 envp, fetch, &spawn_data->sed_exec);
2201 if (error == EJUSTRETURN)
2202 error = 0;
2203 else if (error)
2204 goto error_exit;
2205
2206 have_exec_lock = true;
2207
2208 /*
2209 * Allocate virtual address space for the U-area now, while it
2210 * is still easy to abort the fork operation if we're out of
2211 * kernel virtual address space.
2212 */
2213 uaddr = uvm_uarea_alloc();
2214 if (__predict_false(uaddr == 0)) {
2215 error = ENOMEM;
2216 goto error_exit;
2217 }
2218
2219 /*
2220 * Allocate new proc. Borrow proc0 vmspace for it, we will
2221 * replace it with its own before returning to userland
2222 * in the child.
2223 * This is a point of no return, we will have to go through
2224 * the child proc to properly clean it up past this point.
2225 */
2226 p2 = proc_alloc();
2227 pid = p2->p_pid;
2228
2229 /*
2230 * Make a proc table entry for the new process.
2231 * Start by zeroing the section of proc that is zero-initialized,
2232 * then copy the section that is copied directly from the parent.
2233 */
2234 memset(&p2->p_startzero, 0,
2235 (unsigned) ((char *)&p2->p_endzero - (char *)&p2->p_startzero));
2236 memcpy(&p2->p_startcopy, &p1->p_startcopy,
2237 (unsigned) ((char *)&p2->p_endcopy - (char *)&p2->p_startcopy));
2238 p2->p_vmspace = proc0.p_vmspace;
2239
2240 TAILQ_INIT(&p2->p_sigpend.sp_info);
2241
2242 LIST_INIT(&p2->p_lwps);
2243 LIST_INIT(&p2->p_sigwaiters);
2244
2245 /*
2246 * Duplicate sub-structures as needed.
2247 * Increase reference counts on shared objects.
2248 * Inherit flags we want to keep. The flags related to SIGCHLD
2249 * handling are important in order to keep a consistent behaviour
2250 * for the child after the fork. If we are a 32-bit process, the
2251 * child will be too.
2252 */
2253 p2->p_flag =
2254 p1->p_flag & (PK_SUGID | PK_NOCLDWAIT | PK_CLDSIGIGN | PK_32);
2255 p2->p_emul = p1->p_emul;
2256 p2->p_execsw = p1->p_execsw;
2257
2258 mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
2259 mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
2260 rw_init(&p2->p_reflock);
2261 cv_init(&p2->p_waitcv, "wait");
2262 cv_init(&p2->p_lwpcv, "lwpwait");
2263
2264 p2->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
2265
2266 kauth_proc_fork(p1, p2);
2267
2268 p2->p_raslist = NULL;
2269 p2->p_fd = fd_copy();
2270
2271 /* XXX racy */
2272 p2->p_mqueue_cnt = p1->p_mqueue_cnt;
2273
2274 p2->p_cwdi = cwdinit();
2275
2276 /*
2277 * Note: p_limit (rlimit stuff) is copy-on-write, so normally
2278 * we just need increase pl_refcnt.
2279 */
2280 if (!p1->p_limit->pl_writeable) {
2281 lim_addref(p1->p_limit);
2282 p2->p_limit = p1->p_limit;
2283 } else {
2284 p2->p_limit = lim_copy(p1->p_limit);
2285 }
2286
2287 p2->p_lflag = 0;
2288 p2->p_sflag = 0;
2289 p2->p_slflag = 0;
2290 p2->p_pptr = p1;
2291 p2->p_ppid = p1->p_pid;
2292 LIST_INIT(&p2->p_children);
2293
2294 p2->p_aio = NULL;
2295
2296 #ifdef KTRACE
2297 /*
2298 * Copy traceflag and tracefile if enabled.
2299 * If not inherited, these were zeroed above.
2300 */
2301 if (p1->p_traceflag & KTRFAC_INHERIT) {
2302 mutex_enter(&ktrace_lock);
2303 p2->p_traceflag = p1->p_traceflag;
2304 if ((p2->p_tracep = p1->p_tracep) != NULL)
2305 ktradref(p2);
2306 mutex_exit(&ktrace_lock);
2307 }
2308 #endif
2309
2310 /*
2311 * Create signal actions for the child process.
2312 */
2313 p2->p_sigacts = sigactsinit(p1, 0);
2314 mutex_enter(p1->p_lock);
2315 p2->p_sflag |=
2316 (p1->p_sflag & (PS_STOPFORK | PS_STOPEXEC | PS_NOCLDSTOP));
2317 sched_proc_fork(p1, p2);
2318 mutex_exit(p1->p_lock);
2319
2320 p2->p_stflag = p1->p_stflag;
2321
2322 /*
2323 * p_stats.
2324 * Copy parts of p_stats, and zero out the rest.
2325 */
2326 p2->p_stats = pstatscopy(p1->p_stats);
2327
2328 /* copy over machdep flags to the new proc */
2329 cpu_proc_fork(p1, p2);
2330
2331 /*
2332 * Prepare remaining parts of spawn data
2333 */
2334 spawn_data->sed_actions = fa;
2335 spawn_data->sed_attrs = sa;
2336
2337 spawn_data->sed_parent = p1;
2338
2339 /* create LWP */
2340 lwp_create(l1, p2, uaddr, 0, NULL, 0, spawn_return, spawn_data,
2341 &l2, l1->l_class);
2342 l2->l_ctxlink = NULL; /* reset ucontext link */
2343
2344 /*
2345 * Copy the credential so other references don't see our changes.
2346 * Test to see if this is necessary first, since in the common case
2347 * we won't need a private reference.
2348 */
2349 if (kauth_cred_geteuid(l2->l_cred) != kauth_cred_getsvuid(l2->l_cred) ||
2350 kauth_cred_getegid(l2->l_cred) != kauth_cred_getsvgid(l2->l_cred)) {
2351 l2->l_cred = kauth_cred_copy(l2->l_cred);
2352 kauth_cred_setsvuid(l2->l_cred, kauth_cred_geteuid(l2->l_cred));
2353 kauth_cred_setsvgid(l2->l_cred, kauth_cred_getegid(l2->l_cred));
2354 }
2355
2356 /* Update the master credentials. */
2357 if (l2->l_cred != p2->p_cred) {
2358 kauth_cred_t ocred;
2359
2360 kauth_cred_hold(l2->l_cred);
2361 mutex_enter(p2->p_lock);
2362 ocred = p2->p_cred;
2363 p2->p_cred = l2->l_cred;
2364 mutex_exit(p2->p_lock);
2365 kauth_cred_free(ocred);
2366 }
2367
2368 *child_ok = true;
2369 spawn_data->sed_refcnt = 2; /* child gets it as well */
2370 #if 0
2371 l2->l_nopreempt = 1; /* start it non-preemptable */
2372 #endif
2373
2374 /*
2375 * It's now safe for the scheduler and other processes to see the
2376 * child process.
2377 */
2378 mutex_enter(proc_lock);
2379
2380 if (p1->p_session->s_ttyvp != NULL && p1->p_lflag & PL_CONTROLT)
2381 p2->p_lflag |= PL_CONTROLT;
2382
2383 LIST_INSERT_HEAD(&p1->p_children, p2, p_sibling);
2384 p2->p_exitsig = SIGCHLD; /* signal for parent on exit */
2385
2386 LIST_INSERT_AFTER(p1, p2, p_pglist);
2387 LIST_INSERT_HEAD(&allproc, p2, p_list);
2388
2389 p2->p_trace_enabled = trace_is_enabled(p2);
2390 #ifdef __HAVE_SYSCALL_INTERN
2391 (*p2->p_emul->e_syscall_intern)(p2);
2392 #endif
2393
2394 /*
2395 * Make child runnable, set start time, and add to run queue except
2396 * if the parent requested the child to start in SSTOP state.
2397 */
2398 mutex_enter(p2->p_lock);
2399
2400 getmicrotime(&p2->p_stats->p_start);
2401
2402 lwp_lock(l2);
2403 KASSERT(p2->p_nrlwps == 1);
2404 p2->p_nrlwps = 1;
2405 p2->p_stat = SACTIVE;
2406 l2->l_stat = LSRUN;
2407 sched_enqueue(l2, false);
2408 lwp_unlock(l2);
2409
2410 mutex_exit(p2->p_lock);
2411 mutex_exit(proc_lock);
2412
2413 cv_wait(&spawn_data->sed_cv_child_ready, &spawn_data->sed_mtx_child);
2414 error = spawn_data->sed_error;
2415 mutex_exit(&spawn_data->sed_mtx_child);
2416 spawn_exec_data_release(spawn_data);
2417
2418 rw_exit(&p1->p_reflock);
2419 rw_exit(&exec_lock);
2420 have_exec_lock = false;
2421
2422 *pid_res = pid;
2423 return error;
2424
2425 error_exit:
2426 if (have_exec_lock) {
2427 execve_free_data(&spawn_data->sed_exec);
2428 rw_exit(&p1->p_reflock);
2429 rw_exit(&exec_lock);
2430 }
2431 mutex_exit(&spawn_data->sed_mtx_child);
2432 spawn_exec_data_release(spawn_data);
2433
2434 return error;
2435 }
2436
2437 int
2438 sys_posix_spawn(struct lwp *l1, const struct sys_posix_spawn_args *uap,
2439 register_t *retval)
2440 {
2441 /* {
2442 syscallarg(pid_t *) pid;
2443 syscallarg(const char *) path;
2444 syscallarg(const struct posix_spawn_file_actions *) file_actions;
2445 syscallarg(const struct posix_spawnattr *) attrp;
2446 syscallarg(char *const *) argv;
2447 syscallarg(char *const *) envp;
2448 } */
2449
2450 int error;
2451 struct posix_spawn_file_actions *fa = NULL;
2452 struct posix_spawnattr *sa = NULL;
2453 pid_t pid;
2454 bool child_ok = false;
2455 rlim_t max_fileactions;
2456 proc_t *p = l1->l_proc;
2457
2458 error = check_posix_spawn(l1);
2459 if (error) {
2460 *retval = error;
2461 return 0;
2462 }
2463
2464 /* copy in file_actions struct */
2465 if (SCARG(uap, file_actions) != NULL) {
2466 max_fileactions = 2 * min(p->p_rlimit[RLIMIT_NOFILE].rlim_cur,
2467 maxfiles);
2468 error = posix_spawn_fa_alloc(&fa, SCARG(uap, file_actions),
2469 max_fileactions);
2470 if (error)
2471 goto error_exit;
2472 }
2473
2474 /* copyin posix_spawnattr struct */
2475 if (SCARG(uap, attrp) != NULL) {
2476 sa = kmem_alloc(sizeof(*sa), KM_SLEEP);
2477 error = copyin(SCARG(uap, attrp), sa, sizeof(*sa));
2478 if (error)
2479 goto error_exit;
2480 }
2481
2482 /*
2483 * Do the spawn
2484 */
2485 error = do_posix_spawn(l1, &pid, &child_ok, SCARG(uap, path), fa, sa,
2486 SCARG(uap, argv), SCARG(uap, envp), execve_fetch_element);
2487 if (error)
2488 goto error_exit;
2489
2490 if (error == 0 && SCARG(uap, pid) != NULL)
2491 error = copyout(&pid, SCARG(uap, pid), sizeof(pid));
2492
2493 *retval = error;
2494 return 0;
2495
2496 error_exit:
2497 if (!child_ok) {
2498 (void)chgproccnt(kauth_cred_getuid(l1->l_cred), -1);
2499 atomic_dec_uint(&nprocs);
2500
2501 if (sa)
2502 kmem_free(sa, sizeof(*sa));
2503 if (fa)
2504 posix_spawn_fa_free(fa, fa->len);
2505 }
2506
2507 *retval = error;
2508 return 0;
2509 }
2510
2511 void
2512 exec_free_emul_arg(struct exec_package *epp)
2513 {
2514 if (epp->ep_emul_arg_free != NULL) {
2515 KASSERT(epp->ep_emul_arg != NULL);
2516 (*epp->ep_emul_arg_free)(epp->ep_emul_arg);
2517 epp->ep_emul_arg_free = NULL;
2518 epp->ep_emul_arg = NULL;
2519 } else {
2520 KASSERT(epp->ep_emul_arg == NULL);
2521 }
2522 }
2523