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