kern_exec.c revision 1.251 1 /* $NetBSD: kern_exec.c,v 1.251 2007/10/24 14:50:40 ad Exp $ */
2
3 /*-
4 * Copyright (C) 1993, 1994, 1996 Christopher G. Demetriou
5 * Copyright (C) 1992 Wolfgang Solfrank.
6 * Copyright (C) 1992 TooLs GmbH.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.251 2007/10/24 14:50:40 ad Exp $");
37
38 #include "opt_ktrace.h"
39 #include "opt_syscall_debug.h"
40 #include "opt_compat_netbsd.h"
41 #include "veriexec.h"
42 #include "opt_pax.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/malloc.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/file.h>
54 #include <sys/acct.h>
55 #include <sys/exec.h>
56 #include <sys/ktrace.h>
57 #include <sys/resourcevar.h>
58 #include <sys/wait.h>
59 #include <sys/mman.h>
60 #include <sys/ras.h>
61 #include <sys/signalvar.h>
62 #include <sys/stat.h>
63 #include <sys/syscall.h>
64 #include <sys/kauth.h>
65
66 #include <sys/syscallargs.h>
67 #if NVERIEXEC > 0
68 #include <sys/verified_exec.h>
69 #endif /* NVERIEXEC > 0 */
70
71 #ifdef SYSTRACE
72 #include <sys/systrace.h>
73 #endif /* SYSTRACE */
74
75 #ifdef PAX_SEGVGUARD
76 #include <sys/pax.h>
77 #endif /* PAX_SEGVGUARD */
78
79 #include <uvm/uvm_extern.h>
80
81 #include <sys/cpu.h>
82 #include <machine/reg.h>
83
84 #include <compat/common/compat_util.h>
85
86 static int exec_sigcode_map(struct proc *, const struct emul *);
87
88 #ifdef DEBUG_EXEC
89 #define DPRINTF(a) uprintf a
90 #else
91 #define DPRINTF(a)
92 #endif /* DEBUG_EXEC */
93
94 MALLOC_DEFINE(M_EXEC, "exec", "argument lists & other mem used by exec");
95
96 /*
97 * Exec function switch:
98 *
99 * Note that each makecmds function is responsible for loading the
100 * exec package with the necessary functions for any exec-type-specific
101 * handling.
102 *
103 * Functions for specific exec types should be defined in their own
104 * header file.
105 */
106 extern const struct execsw execsw_builtin[];
107 extern int nexecs_builtin;
108 static const struct execsw **execsw = NULL;
109 static int nexecs;
110
111 u_int exec_maxhdrsz; /* must not be static - netbsd32 needs it */
112
113 #ifdef LKM
114 /* list of supported emulations */
115 static
116 LIST_HEAD(emlist_head, emul_entry) el_head = LIST_HEAD_INITIALIZER(el_head);
117 struct emul_entry {
118 LIST_ENTRY(emul_entry) el_list;
119 const struct emul *el_emul;
120 int ro_entry;
121 };
122
123 /* list of dynamically loaded execsw entries */
124 static
125 LIST_HEAD(execlist_head, exec_entry) ex_head = LIST_HEAD_INITIALIZER(ex_head);
126 struct exec_entry {
127 LIST_ENTRY(exec_entry) ex_list;
128 const struct execsw *es;
129 };
130
131 /* structure used for building execw[] */
132 struct execsw_entry {
133 struct execsw_entry *next;
134 const struct execsw *es;
135 };
136 #endif /* LKM */
137
138 #ifdef SYSCALL_DEBUG
139 extern const char * const syscallnames[];
140 #endif
141
142 #ifdef COMPAT_16
143 extern char sigcode[], esigcode[];
144 struct uvm_object *emul_netbsd_object;
145 #endif
146
147 #ifndef __HAVE_SYSCALL_INTERN
148 void syscall(void);
149 #endif
150
151 /* NetBSD emul struct */
152 const struct emul emul_netbsd = {
153 "netbsd",
154 NULL, /* emulation path */
155 #ifndef __HAVE_MINIMAL_EMUL
156 EMUL_HAS_SYS___syscall,
157 NULL,
158 SYS_syscall,
159 SYS_NSYSENT,
160 #endif
161 sysent,
162 #ifdef SYSCALL_DEBUG
163 syscallnames,
164 #else
165 NULL,
166 #endif
167 sendsig,
168 trapsignal,
169 NULL,
170 #ifdef COMPAT_16
171 sigcode,
172 esigcode,
173 &emul_netbsd_object,
174 #else
175 NULL,
176 NULL,
177 NULL,
178 #endif
179 setregs,
180 NULL,
181 NULL,
182 NULL,
183 NULL,
184 NULL,
185 #ifdef __HAVE_SYSCALL_INTERN
186 syscall_intern,
187 #else
188 syscall,
189 #endif
190 NULL,
191 NULL,
192
193 uvm_default_mapaddr,
194 NULL,
195 sizeof(ucontext_t),
196 startlwp,
197 };
198
199 #ifdef LKM
200 /*
201 * Exec lock. Used to control access to execsw[] structures.
202 * This must not be static so that netbsd32 can access it, too.
203 */
204 krwlock_t exec_lock;
205
206 static void link_es(struct execsw_entry **, const struct execsw *);
207 #endif /* LKM */
208
209 /*
210 * check exec:
211 * given an "executable" described in the exec package's namei info,
212 * see what we can do with it.
213 *
214 * ON ENTRY:
215 * exec package with appropriate namei info
216 * lwp pointer of exec'ing lwp
217 * NO SELF-LOCKED VNODES
218 *
219 * ON EXIT:
220 * error: nothing held, etc. exec header still allocated.
221 * ok: filled exec package, executable's vnode (unlocked).
222 *
223 * EXEC SWITCH ENTRY:
224 * Locked vnode to check, exec package, proc.
225 *
226 * EXEC SWITCH EXIT:
227 * ok: return 0, filled exec package, executable's vnode (unlocked).
228 * error: destructive:
229 * everything deallocated execept exec header.
230 * non-destructive:
231 * error code, executable's vnode (unlocked),
232 * exec header unmodified.
233 */
234 int
235 /*ARGSUSED*/
236 check_exec(struct lwp *l, struct exec_package *epp)
237 {
238 int error, i;
239 struct vnode *vp;
240 struct nameidata *ndp;
241 size_t resid;
242
243 ndp = epp->ep_ndp;
244 ndp->ni_cnd.cn_nameiop = LOOKUP;
245 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME | TRYEMULROOT;
246 /* first get the vnode */
247 if ((error = namei(ndp)) != 0)
248 return error;
249 epp->ep_vp = vp = ndp->ni_vp;
250
251 /* check access and type */
252 if (vp->v_type != VREG) {
253 error = EACCES;
254 goto bad1;
255 }
256 if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred, l)) != 0)
257 goto bad1;
258
259 /* get attributes */
260 if ((error = VOP_GETATTR(vp, epp->ep_vap, l->l_cred, l)) != 0)
261 goto bad1;
262
263 /* Check mount point */
264 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
265 error = EACCES;
266 goto bad1;
267 }
268 if (vp->v_mount->mnt_flag & MNT_NOSUID)
269 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
270
271 /* try to open it */
272 if ((error = VOP_OPEN(vp, FREAD, l->l_cred, l)) != 0)
273 goto bad1;
274
275 /* unlock vp, since we need it unlocked from here on out. */
276 VOP_UNLOCK(vp, 0);
277
278 #if NVERIEXEC > 0
279 error = veriexec_verify(l, vp, ndp->ni_cnd.cn_pnbuf,
280 epp->ep_flags & EXEC_INDIR ? VERIEXEC_INDIRECT : VERIEXEC_DIRECT,
281 NULL);
282 if (error)
283 goto bad2;
284 #endif /* NVERIEXEC > 0 */
285
286 #ifdef PAX_SEGVGUARD
287 error = pax_segvguard(l, vp, ndp->ni_cnd.cn_pnbuf, false);
288 if (error)
289 goto bad2;
290 #endif /* PAX_SEGVGUARD */
291
292 /* now we have the file, get the exec header */
293 error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
294 UIO_SYSSPACE, 0, l->l_cred, &resid, NULL);
295 if (error)
296 goto bad2;
297 epp->ep_hdrvalid = epp->ep_hdrlen - resid;
298
299 /*
300 * Set up default address space limits. Can be overridden
301 * by individual exec packages.
302 *
303 * XXX probably should be all done in the exec packages.
304 */
305 epp->ep_vm_minaddr = VM_MIN_ADDRESS;
306 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
307 /*
308 * set up the vmcmds for creation of the process
309 * address space
310 */
311 error = ENOEXEC;
312 for (i = 0; i < nexecs; i++) {
313 int newerror;
314
315 epp->ep_esch = execsw[i];
316 newerror = (*execsw[i]->es_makecmds)(l, epp);
317
318 if (!newerror) {
319 /* Seems ok: check that entry point is sane */
320 if (epp->ep_entry > VM_MAXUSER_ADDRESS) {
321 error = ENOEXEC;
322 break;
323 }
324
325 /* check limits */
326 if ((epp->ep_tsize > MAXTSIZ) ||
327 (epp->ep_dsize > (u_quad_t)l->l_proc->p_rlimit
328 [RLIMIT_DATA].rlim_cur)) {
329 error = ENOMEM;
330 break;
331 }
332 return 0;
333 }
334
335 if (epp->ep_emul_root != NULL) {
336 vrele(epp->ep_emul_root);
337 epp->ep_emul_root = NULL;
338 }
339 if (epp->ep_interp != NULL) {
340 vrele(epp->ep_interp);
341 epp->ep_interp = NULL;
342 }
343
344 /* make sure the first "interesting" error code is saved. */
345 if (error == ENOEXEC)
346 error = newerror;
347
348 if (epp->ep_flags & EXEC_DESTR)
349 /* Error from "#!" code, tidied up by recursive call */
350 return error;
351 }
352
353 /* not found, error */
354
355 /*
356 * free any vmspace-creation commands,
357 * and release their references
358 */
359 kill_vmcmds(&epp->ep_vmcmds);
360
361 bad2:
362 /*
363 * close and release the vnode, restore the old one, free the
364 * pathname buf, and punt.
365 */
366 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
367 VOP_CLOSE(vp, FREAD, l->l_cred, l);
368 vput(vp);
369 PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
370 return error;
371
372 bad1:
373 /*
374 * free the namei pathname buffer, and put the vnode
375 * (which we don't yet have open).
376 */
377 vput(vp); /* was still locked */
378 PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
379 return error;
380 }
381
382 #ifdef __MACHINE_STACK_GROWS_UP
383 #define STACK_PTHREADSPACE NBPG
384 #else
385 #define STACK_PTHREADSPACE 0
386 #endif
387
388 static int
389 execve_fetch_element(char * const *array, size_t index, char **value)
390 {
391 return copyin(array + index, value, sizeof(*value));
392 }
393
394 /*
395 * exec system call
396 */
397 /* ARGSUSED */
398 int
399 sys_execve(struct lwp *l, void *v, register_t *retval)
400 {
401 struct sys_execve_args /* {
402 syscallarg(const char *) path;
403 syscallarg(char * const *) argp;
404 syscallarg(char * const *) envp;
405 } */ *uap = v;
406
407 return execve1(l, SCARG(uap, path), SCARG(uap, argp),
408 SCARG(uap, envp), execve_fetch_element);
409 }
410
411 int
412 execve1(struct lwp *l, const char *path, char * const *args,
413 char * const *envs, execve_fetch_element_t fetch_element)
414 {
415 int error;
416 struct exec_package pack;
417 struct nameidata nid;
418 struct vattr attr;
419 struct proc *p;
420 char *argp;
421 char *dp, *sp;
422 long argc, envc;
423 size_t i, len;
424 char *stack;
425 struct ps_strings arginfo;
426 struct ps_strings *aip = &arginfo;
427 struct vmspace *vm;
428 char **tmpfap;
429 int szsigcode;
430 struct exec_vmcmd *base_vcp;
431 ksiginfo_t ksi;
432 ksiginfoq_t kq;
433 #ifdef SYSTRACE
434 int wassugid = ISSET(p->p_flag, PK_SUGID);
435 char pathbuf[MAXPATHLEN];
436 size_t pathbuflen;
437 #endif /* SYSTRACE */
438
439 p = l->l_proc;
440
441 /*
442 * Drain existing references and forbid new ones. The process
443 * should be left alone until we're done here. This is necessary
444 * to avoid race conditions - e.g. in ptrace() - that might allow
445 * a local user to illicitly obtain elevated privileges.
446 */
447 mutex_enter(&p->p_mutex);
448 proc_drainrefs(p);
449 mutex_exit(&p->p_mutex);
450
451 base_vcp = NULL;
452 /*
453 * Init the namei data to point the file user's program name.
454 * This is done here rather than in check_exec(), so that it's
455 * possible to override this settings if any of makecmd/probe
456 * functions call check_exec() recursively - for example,
457 * see exec_script_makecmds().
458 */
459 #ifdef SYSTRACE
460 if (ISSET(p->p_flag, PK_SYSTRACE))
461 systrace_execve0(p);
462
463 error = copyinstr(path, pathbuf, sizeof(pathbuf), &pathbuflen);
464 if (error) {
465 DPRINTF(("execve: copyinstr path %d", error));
466 goto clrflg;
467 }
468
469 NDINIT(&nid, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_SYSSPACE, pathbuf, l);
470 #else
471 NDINIT(&nid, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, path, l);
472 #endif /* SYSTRACE */
473
474 /*
475 * initialize the fields of the exec package.
476 */
477 #ifdef SYSTRACE
478 pack.ep_name = pathbuf;
479 #else
480 pack.ep_name = path;
481 #endif /* SYSTRACE */
482 pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
483 pack.ep_hdrlen = exec_maxhdrsz;
484 pack.ep_hdrvalid = 0;
485 pack.ep_ndp = &nid;
486 pack.ep_emul_arg = NULL;
487 pack.ep_vmcmds.evs_cnt = 0;
488 pack.ep_vmcmds.evs_used = 0;
489 pack.ep_vap = &attr;
490 pack.ep_flags = 0;
491 pack.ep_emul_root = NULL;
492 pack.ep_interp = NULL;
493 pack.ep_esch = NULL;
494
495 #ifdef LKM
496 rw_enter(&exec_lock, RW_READER);
497 #endif
498
499 /* see if we can run it. */
500 if ((error = check_exec(l, &pack)) != 0) {
501 DPRINTF(("execve: check exec failed %d\n", error));
502 goto freehdr;
503 }
504
505 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
506
507 /* allocate an argument buffer */
508 argp = (char *) uvm_km_alloc(exec_map, NCARGS, 0,
509 UVM_KMF_PAGEABLE|UVM_KMF_WAITVA);
510 #ifdef DIAGNOSTIC
511 if (argp == NULL)
512 panic("execve: argp == NULL");
513 #endif
514 dp = argp;
515 argc = 0;
516
517 /* copy the fake args list, if there's one, freeing it as we go */
518 if (pack.ep_flags & EXEC_HASARGL) {
519 tmpfap = pack.ep_fa;
520 while (*tmpfap != NULL) {
521 char *cp;
522
523 cp = *tmpfap;
524 while (*cp)
525 *dp++ = *cp++;
526 dp++;
527
528 FREE(*tmpfap, M_EXEC);
529 tmpfap++; argc++;
530 }
531 FREE(pack.ep_fa, M_EXEC);
532 pack.ep_flags &= ~EXEC_HASARGL;
533 }
534
535 /* Now get argv & environment */
536 if (args == NULL) {
537 DPRINTF(("execve: null args\n"));
538 error = EINVAL;
539 goto bad;
540 }
541 /* 'i' will index the argp/envp element to be retrieved */
542 i = 0;
543 if (pack.ep_flags & EXEC_SKIPARG)
544 i++;
545
546 while (1) {
547 len = argp + ARG_MAX - dp;
548 if ((error = (*fetch_element)(args, i, &sp)) != 0) {
549 DPRINTF(("execve: fetch_element args %d\n", error));
550 goto bad;
551 }
552 if (!sp)
553 break;
554 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
555 DPRINTF(("execve: copyinstr args %d\n", error));
556 if (error == ENAMETOOLONG)
557 error = E2BIG;
558 goto bad;
559 }
560 ktrexecarg(dp, len - 1);
561 dp += len;
562 i++;
563 argc++;
564 }
565
566 envc = 0;
567 /* environment need not be there */
568 if (envs != NULL) {
569 i = 0;
570 while (1) {
571 len = argp + ARG_MAX - dp;
572 if ((error = (*fetch_element)(envs, i, &sp)) != 0) {
573 DPRINTF(("execve: fetch_element env %d\n", error));
574 goto bad;
575 }
576 if (!sp)
577 break;
578 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
579 DPRINTF(("execve: copyinstr env %d\n", error));
580 if (error == ENAMETOOLONG)
581 error = E2BIG;
582 goto bad;
583 }
584 ktrexecenv(dp, len - 1);
585 dp += len;
586 i++;
587 envc++;
588 }
589 }
590
591 dp = (char *) ALIGN(dp);
592
593 szsigcode = pack.ep_esch->es_emul->e_esigcode -
594 pack.ep_esch->es_emul->e_sigcode;
595
596 /* Now check if args & environ fit into new stack */
597 if (pack.ep_flags & EXEC_32)
598 len = ((argc + envc + 2 + pack.ep_esch->es_arglen) *
599 sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
600 szsigcode + sizeof(struct ps_strings) + STACK_PTHREADSPACE)
601 - argp;
602 else
603 len = ((argc + envc + 2 + pack.ep_esch->es_arglen) *
604 sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
605 szsigcode + sizeof(struct ps_strings) + STACK_PTHREADSPACE)
606 - argp;
607
608 #ifdef STACKLALIGN /* arm, etc. */
609 len = STACKALIGN(len); /* make the stack "safely" aligned */
610 #else
611 len = ALIGN(len); /* make the stack "safely" aligned */
612 #endif
613
614 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
615 DPRINTF(("execve: stack limit exceeded %zu\n", len));
616 error = ENOMEM;
617 goto bad;
618 }
619
620 /* Get rid of other LWPs. */
621 if (p->p_nlwps > 1) {
622 mutex_enter(&p->p_smutex);
623 exit_lwps(l);
624 mutex_exit(&p->p_smutex);
625 }
626 KDASSERT(p->p_nlwps == 1);
627
628 /* This is now LWP 1 */
629 l->l_lid = 1;
630 p->p_nlwpid = 1;
631
632 /* Remove POSIX timers */
633 timers_free(p, TIMERS_POSIX);
634
635 /* adjust "active stack depth" for process VSZ */
636 pack.ep_ssize = len; /* maybe should go elsewhere, but... */
637
638 /*
639 * Do whatever is necessary to prepare the address space
640 * for remapping. Note that this might replace the current
641 * vmspace with another!
642 */
643 uvmspace_exec(l, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
644
645 /* record proc's vnode, for use by procfs and others */
646 if (p->p_textvp)
647 vrele(p->p_textvp);
648 VREF(pack.ep_vp);
649 p->p_textvp = pack.ep_vp;
650
651 /* Now map address space */
652 vm = p->p_vmspace;
653 vm->vm_taddr = (void *)pack.ep_taddr;
654 vm->vm_tsize = btoc(pack.ep_tsize);
655 vm->vm_daddr = (void*)pack.ep_daddr;
656 vm->vm_dsize = btoc(pack.ep_dsize);
657 vm->vm_ssize = btoc(pack.ep_ssize);
658 vm->vm_maxsaddr = (void *)pack.ep_maxsaddr;
659 vm->vm_minsaddr = (void *)pack.ep_minsaddr;
660
661 /* create the new process's VM space by running the vmcmds */
662 #ifdef DIAGNOSTIC
663 if (pack.ep_vmcmds.evs_used == 0)
664 panic("execve: no vmcmds");
665 #endif
666 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
667 struct exec_vmcmd *vcp;
668
669 vcp = &pack.ep_vmcmds.evs_cmds[i];
670 if (vcp->ev_flags & VMCMD_RELATIVE) {
671 #ifdef DIAGNOSTIC
672 if (base_vcp == NULL)
673 panic("execve: relative vmcmd with no base");
674 if (vcp->ev_flags & VMCMD_BASE)
675 panic("execve: illegal base & relative vmcmd");
676 #endif
677 vcp->ev_addr += base_vcp->ev_addr;
678 }
679 error = (*vcp->ev_proc)(l, vcp);
680 #ifdef DEBUG_EXEC
681 if (error) {
682 size_t j;
683 struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0];
684 for (j = 0; j <= i; j++)
685 uprintf(
686 "vmcmd[%zu] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n",
687 j, vp[j].ev_addr, vp[j].ev_len,
688 vp[j].ev_offset, vp[j].ev_prot,
689 vp[j].ev_flags);
690 }
691 #endif /* DEBUG_EXEC */
692 if (vcp->ev_flags & VMCMD_BASE)
693 base_vcp = vcp;
694 }
695
696 /* free the vmspace-creation commands, and release their references */
697 kill_vmcmds(&pack.ep_vmcmds);
698
699 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
700 VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred, l);
701 vput(pack.ep_vp);
702
703 /* if an error happened, deallocate and punt */
704 if (error) {
705 DPRINTF(("execve: vmcmd %zu failed: %d\n", i - 1, error));
706 goto exec_abort;
707 }
708
709 /* remember information about the process */
710 arginfo.ps_nargvstr = argc;
711 arginfo.ps_nenvstr = envc;
712
713 stack = (char *)STACK_ALLOC(STACK_GROW(vm->vm_minsaddr,
714 STACK_PTHREADSPACE + sizeof(struct ps_strings) + szsigcode),
715 len - (sizeof(struct ps_strings) + szsigcode));
716 #ifdef __MACHINE_STACK_GROWS_UP
717 /*
718 * The copyargs call always copies into lower addresses
719 * first, moving towards higher addresses, starting with
720 * the stack pointer that we give. When the stack grows
721 * down, this puts argc/argv/envp very shallow on the
722 * stack, right at the first user stack pointer, and puts
723 * STACKGAPLEN very deep in the stack. When the stack
724 * grows up, the situation is reversed.
725 *
726 * Normally, this is no big deal. But the ld_elf.so _rtld()
727 * function expects to be called with a single pointer to
728 * a region that has a few words it can stash values into,
729 * followed by argc/argv/envp. When the stack grows down,
730 * it's easy to decrement the stack pointer a little bit to
731 * allocate the space for these few words and pass the new
732 * stack pointer to _rtld. When the stack grows up, however,
733 * a few words before argc is part of the signal trampoline, XXX
734 * so we have a problem.
735 *
736 * Instead of changing how _rtld works, we take the easy way
737 * out and steal 32 bytes before we call copyargs. This
738 * space is effectively stolen from STACKGAPLEN.
739 */
740 stack += 32;
741 #endif /* __MACHINE_STACK_GROWS_UP */
742
743 /* Now copy argc, args & environ to new stack */
744 error = (*pack.ep_esch->es_copyargs)(l, &pack, &arginfo, &stack, argp);
745 if (error) {
746 DPRINTF(("execve: copyargs failed %d\n", error));
747 goto exec_abort;
748 }
749 /* Move the stack back to original point */
750 stack = (char *)STACK_GROW(vm->vm_minsaddr, len);
751
752 /* fill process ps_strings info */
753 p->p_psstr = (struct ps_strings *)
754 STACK_ALLOC(STACK_GROW(vm->vm_minsaddr, STACK_PTHREADSPACE),
755 sizeof(struct ps_strings));
756 p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
757 p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
758 p->p_psenv = offsetof(struct ps_strings, ps_envstr);
759 p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
760
761 /* copy out the process's ps_strings structure */
762 if ((error = copyout(aip, (char *)p->p_psstr,
763 sizeof(arginfo))) != 0) {
764 DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n",
765 aip, (char *)p->p_psstr, (long)sizeof(arginfo)));
766 goto exec_abort;
767 }
768
769 fdcloseexec(l); /* handle close on exec */
770 execsigs(p); /* reset catched signals */
771
772 l->l_ctxlink = NULL; /* reset ucontext link */
773
774 /* set command name & other accounting info */
775 i = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
776 memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, i);
777 p->p_comm[i] = '\0';
778 p->p_acflag &= ~AFORK;
779
780 p->p_flag |= PK_EXEC;
781
782 /*
783 * Stop profiling.
784 */
785 if ((p->p_stflag & PST_PROFIL) != 0) {
786 mutex_spin_enter(&p->p_stmutex);
787 stopprofclock(p);
788 mutex_spin_exit(&p->p_stmutex);
789 }
790
791 /*
792 * It's OK to test PS_PPWAIT unlocked here, as other LWPs have
793 * exited and exec()/exit() are the only places it will be cleared.
794 */
795 if ((p->p_sflag & PS_PPWAIT) != 0) {
796 mutex_enter(&proclist_lock);
797 mutex_enter(&p->p_smutex);
798 p->p_sflag &= ~PS_PPWAIT;
799 cv_broadcast(&p->p_pptr->p_waitcv);
800 mutex_exit(&p->p_smutex);
801 mutex_exit(&proclist_lock);
802 }
803
804 /*
805 * Deal with set[ug]id. MNT_NOSUID has already been used to disable
806 * s[ug]id. It's OK to check for PSL_TRACED here as we have blocked
807 * out additional references on the process for the moment.
808 */
809 if ((p->p_slflag & PSL_TRACED) == 0 &&
810
811 (((attr.va_mode & S_ISUID) != 0 &&
812 kauth_cred_geteuid(l->l_cred) != attr.va_uid) ||
813
814 ((attr.va_mode & S_ISGID) != 0 &&
815 kauth_cred_getegid(l->l_cred) != attr.va_gid))) {
816 /*
817 * Mark the process as SUGID before we do
818 * anything that might block.
819 */
820 proc_crmod_enter();
821 proc_crmod_leave(NULL, NULL, true);
822
823 /* Make sure file descriptors 0..2 are in use. */
824 if ((error = fdcheckstd(l)) != 0) {
825 DPRINTF(("execve: fdcheckstd failed %d\n", error));
826 goto exec_abort;
827 }
828
829 /*
830 * Copy the credential so other references don't see our
831 * changes.
832 */
833 l->l_cred = kauth_cred_copy(l->l_cred);
834 #ifdef KTRACE
835 /*
836 * If process is being ktraced, turn off - unless
837 * root set it.
838 */
839 if (p->p_tracep) {
840 mutex_enter(&ktrace_lock);
841 if (!(p->p_traceflag & KTRFAC_ROOT))
842 ktrderef(p);
843 mutex_exit(&ktrace_lock);
844 }
845 #endif
846 if (attr.va_mode & S_ISUID)
847 kauth_cred_seteuid(l->l_cred, attr.va_uid);
848 if (attr.va_mode & S_ISGID)
849 kauth_cred_setegid(l->l_cred, attr.va_gid);
850 } else {
851 if (kauth_cred_geteuid(l->l_cred) ==
852 kauth_cred_getuid(l->l_cred) &&
853 kauth_cred_getegid(l->l_cred) ==
854 kauth_cred_getgid(l->l_cred))
855 p->p_flag &= ~PK_SUGID;
856 }
857
858 /*
859 * Copy the credential so other references don't see our changes.
860 * Test to see if this is necessary first, since in the common case
861 * we won't need a private reference.
862 */
863 if (kauth_cred_geteuid(l->l_cred) != kauth_cred_getsvuid(l->l_cred) ||
864 kauth_cred_getegid(l->l_cred) != kauth_cred_getsvgid(l->l_cred)) {
865 l->l_cred = kauth_cred_copy(l->l_cred);
866 kauth_cred_setsvuid(l->l_cred, kauth_cred_geteuid(l->l_cred));
867 kauth_cred_setsvgid(l->l_cred, kauth_cred_getegid(l->l_cred));
868 }
869
870 /* Update the master credentials. */
871 if (l->l_cred != p->p_cred) {
872 kauth_cred_t ocred;
873
874 kauth_cred_hold(l->l_cred);
875 mutex_enter(&p->p_mutex);
876 ocred = p->p_cred;
877 p->p_cred = l->l_cred;
878 mutex_exit(&p->p_mutex);
879 kauth_cred_free(ocred);
880 }
881
882 #if defined(__HAVE_RAS)
883 /*
884 * Remove all RASs from the address space.
885 */
886 ras_purgeall();
887 #endif
888
889 doexechooks(p);
890
891 uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
892
893 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
894
895 /* notify others that we exec'd */
896 KNOTE(&p->p_klist, NOTE_EXEC);
897
898 /* setup new registers and do misc. setup. */
899 (*pack.ep_esch->es_emul->e_setregs)(l, &pack, (u_long) stack);
900 if (pack.ep_esch->es_setregs)
901 (*pack.ep_esch->es_setregs)(l, &pack, (u_long) stack);
902
903 /* map the process's signal trampoline code */
904 if (exec_sigcode_map(p, pack.ep_esch->es_emul)) {
905 DPRINTF(("execve: map sigcode failed %d\n", error));
906 goto exec_abort;
907 }
908
909 free(pack.ep_hdr, M_EXEC);
910
911 /* The emulation root will usually have been found when we looked
912 * for the elf interpreter (or similar), if not look now. */
913 if (pack.ep_esch->es_emul->e_path != NULL && pack.ep_emul_root == NULL)
914 emul_find_root(l, &pack);
915
916 /* Any old emulation root got removed by fdcloseexec */
917 p->p_cwdi->cwdi_edir = pack.ep_emul_root;
918 pack.ep_emul_root = NULL;
919 if (pack.ep_interp != NULL)
920 vrele(pack.ep_interp);
921
922 /*
923 * Call emulation specific exec hook. This can setup per-process
924 * p->p_emuldata or do any other per-process stuff an emulation needs.
925 *
926 * If we are executing process of different emulation than the
927 * original forked process, call e_proc_exit() of the old emulation
928 * first, then e_proc_exec() of new emulation. If the emulation is
929 * same, the exec hook code should deallocate any old emulation
930 * resources held previously by this process.
931 */
932 if (p->p_emul && p->p_emul->e_proc_exit
933 && p->p_emul != pack.ep_esch->es_emul)
934 (*p->p_emul->e_proc_exit)(p);
935
936 /*
937 * Call exec hook. Emulation code may NOT store reference to anything
938 * from &pack.
939 */
940 if (pack.ep_esch->es_emul->e_proc_exec)
941 (*pack.ep_esch->es_emul->e_proc_exec)(p, &pack);
942
943 /* update p_emul, the old value is no longer needed */
944 p->p_emul = pack.ep_esch->es_emul;
945
946 /* ...and the same for p_execsw */
947 p->p_execsw = pack.ep_esch;
948
949 #ifdef __HAVE_SYSCALL_INTERN
950 (*p->p_emul->e_syscall_intern)(p);
951 #endif
952 ktremul();
953
954 #ifdef LKM
955 rw_exit(&exec_lock);
956 #endif
957
958 mutex_enter(&proclist_mutex);
959
960 if ((p->p_slflag & (PSL_TRACED|PSL_SYSCALL)) == PSL_TRACED) {
961 KSI_INIT_EMPTY(&ksi);
962 ksi.ksi_signo = SIGTRAP;
963 ksi.ksi_lid = l->l_lid;
964 kpsignal(p, &ksi, NULL);
965 }
966
967 if (p->p_sflag & PS_STOPEXEC) {
968 KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
969 p->p_pptr->p_nstopchild++;
970 p->p_pptr->p_waited = 0;
971 mutex_enter(&p->p_smutex);
972 ksiginfo_queue_init(&kq);
973 sigclearall(p, &contsigmask, &kq);
974 lwp_lock(l);
975 p->p_refcnt = 1;
976 l->l_stat = LSSTOP;
977 p->p_stat = SSTOP;
978 p->p_nrlwps--;
979 mutex_exit(&p->p_smutex);
980 mutex_exit(&proclist_mutex);
981 mi_switch(l);
982 ksiginfo_queue_drain(&kq);
983 KERNEL_LOCK(l->l_biglocks, l);
984 } else {
985 mutex_exit(&proclist_mutex);
986
987 /* Unlock the process. */
988 mb_write();
989 p->p_refcnt = 1;
990 }
991
992 #ifdef SYSTRACE
993 /* XXXSMP */
994 if (ISSET(p->p_flag, PK_SYSTRACE) &&
995 wassugid && !ISSET(p->p_flag, PK_SUGID))
996 systrace_execve1(pathbuf, p);
997 #endif /* SYSTRACE */
998
999 return (EJUSTRETURN);
1000
1001 bad:
1002 /* free the vmspace-creation commands, and release their references */
1003 kill_vmcmds(&pack.ep_vmcmds);
1004 /* kill any opened file descriptor, if necessary */
1005 if (pack.ep_flags & EXEC_HASFD) {
1006 pack.ep_flags &= ~EXEC_HASFD;
1007 (void) fdrelease(l, pack.ep_fd);
1008 }
1009 /* close and put the exec'd file */
1010 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
1011 VOP_CLOSE(pack.ep_vp, FREAD, l->l_cred, l);
1012 vput(pack.ep_vp);
1013 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
1014 uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
1015
1016 freehdr:
1017 free(pack.ep_hdr, M_EXEC);
1018 if (pack.ep_emul_root != NULL)
1019 vrele(pack.ep_emul_root);
1020 if (pack.ep_interp != NULL)
1021 vrele(pack.ep_interp);
1022
1023 #ifdef SYSTRACE
1024 clrflg:
1025 #endif /* SYSTRACE */
1026 /* Unlock the process. */
1027 mb_write();
1028 p->p_refcnt = 1;
1029
1030 #ifdef LKM
1031 rw_exit(&exec_lock);
1032 #endif
1033
1034 return error;
1035
1036 exec_abort:
1037 #ifdef LKM
1038 rw_exit(&exec_lock);
1039 #endif
1040
1041 /*
1042 * the old process doesn't exist anymore. exit gracefully.
1043 * get rid of the (new) address space we have created, if any, get rid
1044 * of our namei data and vnode, and exit noting failure
1045 */
1046 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
1047 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
1048 if (pack.ep_emul_arg)
1049 FREE(pack.ep_emul_arg, M_TEMP);
1050 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
1051 uvm_km_free(exec_map, (vaddr_t) argp, NCARGS, UVM_KMF_PAGEABLE);
1052 free(pack.ep_hdr, M_EXEC);
1053 if (pack.ep_emul_root != NULL)
1054 vrele(pack.ep_emul_root);
1055 if (pack.ep_interp != NULL)
1056 vrele(pack.ep_interp);
1057
1058 /*
1059 * Acquire the sched-state mutex (exit1() will release it). Since
1060 * this is a failed exec and we are exiting, keep the process locked
1061 * (p->p_refcnt == 0) through exit1().
1062 */
1063 mutex_enter(&p->p_smutex);
1064 exit1(l, W_EXITCODE(error, SIGABRT));
1065
1066 /* NOTREACHED */
1067 return 0;
1068 }
1069
1070
1071 int
1072 copyargs(struct lwp *l, struct exec_package *pack, struct ps_strings *arginfo,
1073 char **stackp, void *argp)
1074 {
1075 char **cpp, *dp, *sp;
1076 size_t len;
1077 void *nullp;
1078 long argc, envc;
1079 int error;
1080
1081 cpp = (char **)*stackp;
1082 nullp = NULL;
1083 argc = arginfo->ps_nargvstr;
1084 envc = arginfo->ps_nenvstr;
1085 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
1086 return error;
1087
1088 dp = (char *) (cpp + argc + envc + 2 + pack->ep_esch->es_arglen);
1089 sp = argp;
1090
1091 /* XXX don't copy them out, remap them! */
1092 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
1093
1094 for (; --argc >= 0; sp += len, dp += len)
1095 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
1096 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
1097 return error;
1098
1099 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
1100 return error;
1101
1102 arginfo->ps_envstr = cpp; /* remember location of envp for later */
1103
1104 for (; --envc >= 0; sp += len, dp += len)
1105 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
1106 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
1107 return error;
1108
1109 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
1110 return error;
1111
1112 *stackp = (char *)cpp;
1113 return 0;
1114 }
1115
1116 #ifdef LKM
1117 /*
1118 * Find an emulation of given name in list of emulations.
1119 * Needs to be called with the exec_lock held.
1120 */
1121 const struct emul *
1122 emul_search(const char *name)
1123 {
1124 struct emul_entry *it;
1125
1126 LIST_FOREACH(it, &el_head, el_list) {
1127 if (strcmp(name, it->el_emul->e_name) == 0)
1128 return it->el_emul;
1129 }
1130
1131 return NULL;
1132 }
1133
1134 /*
1135 * Add an emulation to list, if it's not there already.
1136 */
1137 int
1138 emul_register(const struct emul *emul, int ro_entry)
1139 {
1140 struct emul_entry *ee;
1141 int error;
1142
1143 error = 0;
1144 rw_enter(&exec_lock, RW_WRITER);
1145
1146 if (emul_search(emul->e_name)) {
1147 error = EEXIST;
1148 goto out;
1149 }
1150
1151 MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry),
1152 M_EXEC, M_WAITOK);
1153 ee->el_emul = emul;
1154 ee->ro_entry = ro_entry;
1155 LIST_INSERT_HEAD(&el_head, ee, el_list);
1156
1157 out:
1158 rw_exit(&exec_lock);
1159 return error;
1160 }
1161
1162 /*
1163 * Remove emulation with name 'name' from list of supported emulations.
1164 */
1165 int
1166 emul_unregister(const char *name)
1167 {
1168 const struct proclist_desc *pd;
1169 struct emul_entry *it;
1170 int i, error;
1171 struct proc *ptmp;
1172
1173 error = 0;
1174 rw_enter(&exec_lock, RW_WRITER);
1175
1176 LIST_FOREACH(it, &el_head, el_list) {
1177 if (strcmp(it->el_emul->e_name, name) == 0)
1178 break;
1179 }
1180
1181 if (!it) {
1182 error = ENOENT;
1183 goto out;
1184 }
1185
1186 if (it->ro_entry) {
1187 error = EBUSY;
1188 goto out;
1189 }
1190
1191 /* test if any execw[] entry is still using this */
1192 for(i=0; i < nexecs; i++) {
1193 if (execsw[i]->es_emul == it->el_emul) {
1194 error = EBUSY;
1195 goto out;
1196 }
1197 }
1198
1199 /*
1200 * Test if any process is running under this emulation - since
1201 * emul_unregister() is running quite sendomly, it's better
1202 * to do expensive check here than to use any locking.
1203 */
1204 mutex_enter(&proclist_lock);
1205 for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
1206 PROCLIST_FOREACH(ptmp, pd->pd_list) {
1207 if (ptmp->p_emul == it->el_emul) {
1208 error = EBUSY;
1209 break;
1210 }
1211 }
1212 }
1213 mutex_exit(&proclist_lock);
1214
1215 if (error)
1216 goto out;
1217
1218
1219 /* entry is not used, remove it */
1220 LIST_REMOVE(it, el_list);
1221 FREE(it, M_EXEC);
1222
1223 out:
1224 rw_exit(&exec_lock);
1225 return error;
1226 }
1227
1228 /*
1229 * Add execsw[] entry.
1230 */
1231 int
1232 exec_add(struct execsw *esp, const char *e_name)
1233 {
1234 struct exec_entry *it;
1235 int error;
1236
1237 error = 0;
1238 rw_enter(&exec_lock, RW_WRITER);
1239
1240 if (!esp->es_emul) {
1241 esp->es_emul = emul_search(e_name);
1242 if (!esp->es_emul) {
1243 error = ENOENT;
1244 goto out;
1245 }
1246 }
1247
1248 LIST_FOREACH(it, &ex_head, ex_list) {
1249 /* assume tuple (makecmds, probe_func, emulation) is unique */
1250 if (it->es->es_makecmds == esp->es_makecmds
1251 && it->es->u.elf_probe_func == esp->u.elf_probe_func
1252 && it->es->es_emul == esp->es_emul) {
1253 error = EEXIST;
1254 goto out;
1255 }
1256 }
1257
1258 /* if we got here, the entry doesn't exist yet */
1259 MALLOC(it, struct exec_entry *, sizeof(struct exec_entry),
1260 M_EXEC, M_WAITOK);
1261 it->es = esp;
1262 LIST_INSERT_HEAD(&ex_head, it, ex_list);
1263
1264 /* update execsw[] */
1265 exec_init(0);
1266
1267 out:
1268 rw_exit(&exec_lock);
1269 return error;
1270 }
1271
1272 /*
1273 * Remove execsw[] entry.
1274 */
1275 int
1276 exec_remove(const struct execsw *esp)
1277 {
1278 struct exec_entry *it;
1279 int error;
1280
1281 error = 0;
1282 rw_enter(&exec_lock, RW_WRITER);
1283
1284 LIST_FOREACH(it, &ex_head, ex_list) {
1285 /* assume tuple (makecmds, probe_func, emulation) is unique */
1286 if (it->es->es_makecmds == esp->es_makecmds
1287 && it->es->u.elf_probe_func == esp->u.elf_probe_func
1288 && it->es->es_emul == esp->es_emul)
1289 break;
1290 }
1291 if (!it) {
1292 error = ENOENT;
1293 goto out;
1294 }
1295
1296 /* remove item from list and free resources */
1297 LIST_REMOVE(it, ex_list);
1298 FREE(it, M_EXEC);
1299
1300 /* update execsw[] */
1301 exec_init(0);
1302
1303 out:
1304 rw_exit(&exec_lock);
1305 return error;
1306 }
1307
1308 static void
1309 link_es(struct execsw_entry **listp, const struct execsw *esp)
1310 {
1311 struct execsw_entry *et, *e1;
1312
1313 et = (struct execsw_entry *) malloc(sizeof(struct execsw_entry),
1314 M_TEMP, M_WAITOK);
1315 et->next = NULL;
1316 et->es = esp;
1317 if (*listp == NULL) {
1318 *listp = et;
1319 return;
1320 }
1321
1322 switch(et->es->es_prio) {
1323 case EXECSW_PRIO_FIRST:
1324 /* put new entry as the first */
1325 et->next = *listp;
1326 *listp = et;
1327 break;
1328 case EXECSW_PRIO_ANY:
1329 /* put new entry after all *_FIRST and *_ANY entries */
1330 for(e1 = *listp; e1->next
1331 && e1->next->es->es_prio != EXECSW_PRIO_LAST;
1332 e1 = e1->next);
1333 et->next = e1->next;
1334 e1->next = et;
1335 break;
1336 case EXECSW_PRIO_LAST:
1337 /* put new entry as the last one */
1338 for(e1 = *listp; e1->next; e1 = e1->next);
1339 e1->next = et;
1340 break;
1341 default:
1342 #ifdef DIAGNOSTIC
1343 panic("execw[] entry with unknown priority %d found",
1344 et->es->es_prio);
1345 #else
1346 free(et, M_TEMP);
1347 #endif
1348 break;
1349 }
1350 }
1351
1352 /*
1353 * Initialize exec structures. If init_boot is true, also does necessary
1354 * one-time initialization (it's called from main() that way).
1355 * Once system is multiuser, this should be called with exec_lock held,
1356 * i.e. via exec_{add|remove}().
1357 */
1358 int
1359 exec_init(int init_boot)
1360 {
1361 const struct execsw **new_es, * const *old_es;
1362 struct execsw_entry *list, *e1;
1363 struct exec_entry *e2;
1364 int i, es_sz;
1365
1366 if (init_boot) {
1367 /* do one-time initializations */
1368 rw_init(&exec_lock);
1369
1370 /* register compiled-in emulations */
1371 for(i=0; i < nexecs_builtin; i++) {
1372 if (execsw_builtin[i].es_emul)
1373 emul_register(execsw_builtin[i].es_emul, 1);
1374 }
1375 #ifdef DIAGNOSTIC
1376 if (i == 0)
1377 panic("no emulations found in execsw_builtin[]");
1378 #endif
1379 }
1380
1381 /*
1382 * Build execsw[] array from builtin entries and entries added
1383 * at runtime.
1384 */
1385 list = NULL;
1386 for(i=0; i < nexecs_builtin; i++)
1387 link_es(&list, &execsw_builtin[i]);
1388
1389 /* Add dynamically loaded entries */
1390 es_sz = nexecs_builtin;
1391 LIST_FOREACH(e2, &ex_head, ex_list) {
1392 link_es(&list, e2->es);
1393 es_sz++;
1394 }
1395
1396 /*
1397 * Now that we have sorted all execw entries, create new execsw[]
1398 * and free no longer needed memory in the process.
1399 */
1400 new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK);
1401 for(i=0; list; i++) {
1402 new_es[i] = list->es;
1403 e1 = list->next;
1404 free(list, M_TEMP);
1405 list = e1;
1406 }
1407
1408 /*
1409 * New execsw[] array built, now replace old execsw[] and free
1410 * used memory.
1411 */
1412 old_es = execsw;
1413 execsw = new_es;
1414 nexecs = es_sz;
1415 if (old_es)
1416 /*XXXUNCONST*/
1417 free(__UNCONST(old_es), M_EXEC);
1418
1419 /*
1420 * Figure out the maximum size of an exec header.
1421 */
1422 exec_maxhdrsz = 0;
1423 for (i = 0; i < nexecs; i++) {
1424 if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1425 exec_maxhdrsz = execsw[i]->es_hdrsz;
1426 }
1427
1428 return 0;
1429 }
1430 #endif
1431
1432 #ifndef LKM
1433 /*
1434 * Simplified exec_init() for kernels without LKMs. Only initialize
1435 * exec_maxhdrsz and execsw[].
1436 */
1437 int
1438 exec_init(int init_boot)
1439 {
1440 int i;
1441
1442 #ifdef DIAGNOSTIC
1443 if (!init_boot)
1444 panic("exec_init(): called with init_boot == 0");
1445 #endif
1446
1447 /* do one-time initializations */
1448 nexecs = nexecs_builtin;
1449 execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK);
1450
1451 /*
1452 * Fill in execsw[] and figure out the maximum size of an exec header.
1453 */
1454 exec_maxhdrsz = 0;
1455 for(i=0; i < nexecs; i++) {
1456 execsw[i] = &execsw_builtin[i];
1457 if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
1458 exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
1459 }
1460
1461 return 0;
1462
1463 }
1464 #endif /* !LKM */
1465
1466 static int
1467 exec_sigcode_map(struct proc *p, const struct emul *e)
1468 {
1469 vaddr_t va;
1470 vsize_t sz;
1471 int error;
1472 struct uvm_object *uobj;
1473
1474 sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
1475
1476 if (e->e_sigobject == NULL || sz == 0) {
1477 return 0;
1478 }
1479
1480 /*
1481 * If we don't have a sigobject for this emulation, create one.
1482 *
1483 * sigobject is an anonymous memory object (just like SYSV shared
1484 * memory) that we keep a permanent reference to and that we map
1485 * in all processes that need this sigcode. The creation is simple,
1486 * we create an object, add a permanent reference to it, map it in
1487 * kernel space, copy out the sigcode to it and unmap it.
1488 * We map it with PROT_READ|PROT_EXEC into the process just
1489 * the way sys_mmap() would map it.
1490 */
1491
1492 uobj = *e->e_sigobject;
1493 if (uobj == NULL) {
1494 uobj = uao_create(sz, 0);
1495 (*uobj->pgops->pgo_reference)(uobj);
1496 va = vm_map_min(kernel_map);
1497 if ((error = uvm_map(kernel_map, &va, round_page(sz),
1498 uobj, 0, 0,
1499 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
1500 UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) {
1501 printf("kernel mapping failed %d\n", error);
1502 (*uobj->pgops->pgo_detach)(uobj);
1503 return (error);
1504 }
1505 memcpy((void *)va, e->e_sigcode, sz);
1506 #ifdef PMAP_NEED_PROCWR
1507 pmap_procwr(&proc0, va, sz);
1508 #endif
1509 uvm_unmap(kernel_map, va, va + round_page(sz));
1510 *e->e_sigobject = uobj;
1511 }
1512
1513 /* Just a hint to uvm_map where to put it. */
1514 va = e->e_vm_default_addr(p, (vaddr_t)p->p_vmspace->vm_daddr,
1515 round_page(sz));
1516
1517 #ifdef __alpha__
1518 /*
1519 * Tru64 puts /sbin/loader at the end of user virtual memory,
1520 * which causes the above calculation to put the sigcode at
1521 * an invalid address. Put it just below the text instead.
1522 */
1523 if (va == (vaddr_t)vm_map_max(&p->p_vmspace->vm_map)) {
1524 va = (vaddr_t)p->p_vmspace->vm_taddr - round_page(sz);
1525 }
1526 #endif
1527
1528 (*uobj->pgops->pgo_reference)(uobj);
1529 error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz),
1530 uobj, 0, 0,
1531 UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE,
1532 UVM_ADV_RANDOM, 0));
1533 if (error) {
1534 (*uobj->pgops->pgo_detach)(uobj);
1535 return (error);
1536 }
1537 p->p_sigctx.ps_sigcode = (void *)va;
1538 return (0);
1539 }
1540