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