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