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