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