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