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