kern_exec.c revision 1.180 1 /* $NetBSD: kern_exec.c,v 1.180 2003/12/20 19:01:30 fvdl 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.180 2003/12/20 19:01:30 fvdl Exp $");
37
38 #include "opt_ktrace.h"
39 #include "opt_syscall_debug.h"
40 #include "opt_compat_netbsd.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/mount.h>
48 #include <sys/malloc.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/file.h>
52 #include <sys/acct.h>
53 #include <sys/exec.h>
54 #include <sys/ktrace.h>
55 #include <sys/resourcevar.h>
56 #include <sys/wait.h>
57 #include <sys/mman.h>
58 #include <sys/ras.h>
59 #include <sys/signalvar.h>
60 #include <sys/stat.h>
61 #include <sys/syscall.h>
62
63 #include <sys/sa.h>
64 #include <sys/savar.h>
65 #include <sys/syscallargs.h>
66
67 #include <uvm/uvm_extern.h>
68
69 #include <machine/cpu.h>
70 #include <machine/reg.h>
71
72 static int exec_sigcode_map(struct proc *, const struct emul *);
73
74 #ifdef DEBUG_EXEC
75 #define DPRINTF(a) uprintf a
76 #else
77 #define DPRINTF(a)
78 #endif /* DEBUG_EXEC */
79
80 MALLOC_DEFINE(M_EXEC, "exec", "argument lists & other mem used by exec");
81
82 /*
83 * Exec function switch:
84 *
85 * Note that each makecmds function is responsible for loading the
86 * exec package with the necessary functions for any exec-type-specific
87 * handling.
88 *
89 * Functions for specific exec types should be defined in their own
90 * header file.
91 */
92 extern const struct execsw execsw_builtin[];
93 extern int nexecs_builtin;
94 static const struct execsw **execsw = NULL;
95 static int nexecs;
96
97 u_int exec_maxhdrsz; /* must not be static - netbsd32 needs it */
98
99 #ifdef LKM
100 /* list of supported emulations */
101 static
102 LIST_HEAD(emlist_head, emul_entry) el_head = LIST_HEAD_INITIALIZER(el_head);
103 struct emul_entry {
104 LIST_ENTRY(emul_entry) el_list;
105 const struct emul *el_emul;
106 int ro_entry;
107 };
108
109 /* list of dynamically loaded execsw entries */
110 static
111 LIST_HEAD(execlist_head, exec_entry) ex_head = LIST_HEAD_INITIALIZER(ex_head);
112 struct exec_entry {
113 LIST_ENTRY(exec_entry) ex_list;
114 const struct execsw *es;
115 };
116
117 /* structure used for building execw[] */
118 struct execsw_entry {
119 struct execsw_entry *next;
120 const struct execsw *es;
121 };
122 #endif /* LKM */
123
124 #ifdef SYSCALL_DEBUG
125 extern const char * const syscallnames[];
126 #endif
127 #ifdef __HAVE_SYSCALL_INTERN
128 void syscall_intern(struct proc *);
129 #else
130 void syscall(void);
131 #endif
132
133 #if !defined(__HAVE_SIGINFO) || defined(COMPAT_16)
134 extern char sigcode[], esigcode[];
135 struct uvm_object *emul_netbsd_object;
136 #endif
137
138 /* NetBSD emul struct */
139 const struct emul emul_netbsd = {
140 "netbsd",
141 NULL, /* emulation path */
142 #ifndef __HAVE_MINIMAL_EMUL
143 EMUL_HAS_SYS___syscall,
144 NULL,
145 SYS_syscall,
146 SYS_NSYSENT,
147 #endif
148 sysent,
149 #ifdef SYSCALL_DEBUG
150 syscallnames,
151 #else
152 NULL,
153 #endif
154 sendsig,
155 trapsignal,
156 NULL,
157 #if !defined(__HAVE_SIGINFO) || defined(COMPAT_16)
158 sigcode,
159 esigcode,
160 &emul_netbsd_object,
161 #else
162 NULL,
163 NULL,
164 NULL,
165 #endif
166 setregs,
167 NULL,
168 NULL,
169 NULL,
170 NULL,
171 NULL,
172 #ifdef __HAVE_SYSCALL_INTERN
173 syscall_intern,
174 #else
175 syscall,
176 #endif
177 NULL,
178 NULL,
179 };
180
181 #ifdef LKM
182 /*
183 * Exec lock. Used to control access to execsw[] structures.
184 * This must not be static so that netbsd32 can access it, too.
185 */
186 struct lock exec_lock;
187
188 static void link_es(struct execsw_entry **, const struct execsw *);
189 #endif /* LKM */
190
191 /*
192 * check exec:
193 * given an "executable" described in the exec package's namei info,
194 * see what we can do with it.
195 *
196 * ON ENTRY:
197 * exec package with appropriate namei info
198 * proc pointer of exec'ing proc
199 * iff verified exec enabled then flag indicating a direct exec or
200 * an indirect exec (i.e. for a shell script interpreter)
201 * NO SELF-LOCKED VNODES
202 *
203 * ON EXIT:
204 * error: nothing held, etc. exec header still allocated.
205 * ok: filled exec package, executable's vnode (unlocked).
206 *
207 * EXEC SWITCH ENTRY:
208 * Locked vnode to check, exec package, proc.
209 *
210 * EXEC SWITCH EXIT:
211 * ok: return 0, filled exec package, executable's vnode (unlocked).
212 * error: destructive:
213 * everything deallocated execept exec header.
214 * non-destructive:
215 * error code, executable's vnode (unlocked),
216 * exec header unmodified.
217 */
218 int
219 #ifdef VERIFIED_EXEC
220 check_exec(struct proc *p, struct exec_package *epp, int direct_exec)
221 #else
222 check_exec(struct proc *p, struct exec_package *epp)
223 #endif
224 {
225 int error, i;
226 struct vnode *vp;
227 struct nameidata *ndp;
228 size_t resid;
229
230 ndp = epp->ep_ndp;
231 ndp->ni_cnd.cn_nameiop = LOOKUP;
232 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | SAVENAME;
233 /* first get the vnode */
234 if ((error = namei(ndp)) != 0)
235 return error;
236 epp->ep_vp = vp = ndp->ni_vp;
237
238 /* check access and type */
239 if (vp->v_type != VREG) {
240 error = EACCES;
241 goto bad1;
242 }
243 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
244 goto bad1;
245
246 /* get attributes */
247 if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
248 goto bad1;
249
250 /* Check mount point */
251 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
252 error = EACCES;
253 goto bad1;
254 }
255 if (vp->v_mount->mnt_flag & MNT_NOSUID)
256 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
257
258 /* try to open it */
259 if ((error = VOP_OPEN(vp, FREAD, p->p_ucred, p)) != 0)
260 goto bad1;
261
262 /* unlock vp, since we need it unlocked from here on out. */
263 VOP_UNLOCK(vp, 0);
264
265
266 #ifdef VERIFIED_EXEC
267 /* Evaluate signature for file... */
268 if ((error = check_veriexec(p, vp, epp, direct_exec)) != 0)
269 goto bad2;
270 #endif
271
272 /* now we have the file, get the exec header */
273 uvn_attach(vp, VM_PROT_READ);
274 error = vn_rdwr(UIO_READ, vp, epp->ep_hdr, epp->ep_hdrlen, 0,
275 UIO_SYSSPACE, 0, p->p_ucred, &resid, p);
276 if (error)
277 goto bad2;
278 epp->ep_hdrvalid = epp->ep_hdrlen - resid;
279
280 /*
281 * Set up default address space limits. Can be overridden
282 * by individual exec packages.
283 *
284 * XXX probably should be all done in the exec pakages.
285 */
286 epp->ep_vm_minaddr = VM_MIN_ADDRESS;
287 epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS;
288 /*
289 * set up the vmcmds for creation of the process
290 * address space
291 */
292 error = ENOEXEC;
293 for (i = 0; i < nexecs && error != 0; i++) {
294 int newerror;
295
296 epp->ep_esch = execsw[i];
297 newerror = (*execsw[i]->es_check)(p, epp);
298 /* make sure the first "interesting" error code is saved. */
299 if (!newerror || error == ENOEXEC)
300 error = newerror;
301
302 /* if es_check call was successful, update epp->ep_es */
303 if (!newerror && (epp->ep_flags & EXEC_HASES) == 0)
304 epp->ep_es = execsw[i];
305
306 if (epp->ep_flags & EXEC_DESTR && error != 0)
307 return error;
308 }
309 if (!error) {
310 /* check that entry point is sane */
311 if (epp->ep_entry > VM_MAXUSER_ADDRESS)
312 error = ENOEXEC;
313
314 /* check limits */
315 if ((epp->ep_tsize > MAXTSIZ) ||
316 (epp->ep_dsize >
317 (u_quad_t)p->p_rlimit[RLIMIT_DATA].rlim_cur))
318 error = ENOMEM;
319
320 if (!error)
321 return (0);
322 }
323
324 /*
325 * free any vmspace-creation commands,
326 * and release their references
327 */
328 kill_vmcmds(&epp->ep_vmcmds);
329
330 bad2:
331 /*
332 * close and release the vnode, restore the old one, free the
333 * pathname buf, and punt.
334 */
335 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
336 VOP_CLOSE(vp, FREAD, p->p_ucred, p);
337 vput(vp);
338 PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
339 return error;
340
341 bad1:
342 /*
343 * free the namei pathname buffer, and put the vnode
344 * (which we don't yet have open).
345 */
346 vput(vp); /* was still locked */
347 PNBUF_PUT(ndp->ni_cnd.cn_pnbuf);
348 return error;
349 }
350
351 /*
352 * exec system call
353 */
354 /* ARGSUSED */
355 int
356 sys_execve(struct lwp *l, void *v, register_t *retval)
357 {
358 struct sys_execve_args /* {
359 syscallarg(const char *) path;
360 syscallarg(char * const *) argp;
361 syscallarg(char * const *) envp;
362 } */ *uap = v;
363 int error;
364 u_int i;
365 struct exec_package pack;
366 struct nameidata nid;
367 struct vattr attr;
368 struct proc *p;
369 struct ucred *cred;
370 char *argp;
371 char * const *cpp;
372 char *dp, *sp;
373 long argc, envc;
374 size_t len;
375 char *stack;
376 struct ps_strings arginfo;
377 struct vmspace *vm;
378 char **tmpfap;
379 int szsigcode;
380 struct exec_vmcmd *base_vcp;
381 int oldlwpflags;
382
383 /* Disable scheduler activation upcalls. */
384 oldlwpflags = l->l_flag & (L_SA | L_SA_UPCALL);
385 if (l->l_flag & L_SA)
386 l->l_flag &= ~(L_SA | L_SA_UPCALL);
387
388 p = l->l_proc;
389 /*
390 * Lock the process and set the P_INEXEC flag to indicate that
391 * it should be left alone until we're done here. This is
392 * necessary to avoid race conditions - e.g. in ptrace() -
393 * that might allow a local user to illicitly obtain elevated
394 * privileges.
395 */
396 p->p_flag |= P_INEXEC;
397
398 cred = p->p_ucred;
399 base_vcp = NULL;
400 /*
401 * Init the namei data to point the file user's program name.
402 * This is done here rather than in check_exec(), so that it's
403 * possible to override this settings if any of makecmd/probe
404 * functions call check_exec() recursively - for example,
405 * see exec_script_makecmds().
406 */
407 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
408
409 /*
410 * initialize the fields of the exec package.
411 */
412 pack.ep_name = SCARG(uap, path);
413 pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
414 pack.ep_hdrlen = exec_maxhdrsz;
415 pack.ep_hdrvalid = 0;
416 pack.ep_ndp = &nid;
417 pack.ep_emul_arg = NULL;
418 pack.ep_vmcmds.evs_cnt = 0;
419 pack.ep_vmcmds.evs_used = 0;
420 pack.ep_vap = &attr;
421 pack.ep_flags = 0;
422
423 #ifdef LKM
424 lockmgr(&exec_lock, LK_SHARED, NULL);
425 #endif
426
427 /* see if we can run it. */
428 #ifdef VERIFIED_EXEC
429 if ((error = check_exec(p, &pack, 1)) != 0)
430 /* if ((error = check_exec(p, &pack, 0)) != 0) */
431 #else
432 if ((error = check_exec(p, &pack)) != 0)
433 #endif
434 goto freehdr;
435
436 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
437
438 /* allocate an argument buffer */
439 argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
440 #ifdef DIAGNOSTIC
441 if (argp == (vaddr_t) 0)
442 panic("execve: argp == NULL");
443 #endif
444 dp = argp;
445 argc = 0;
446
447 /* copy the fake args list, if there's one, freeing it as we go */
448 if (pack.ep_flags & EXEC_HASARGL) {
449 tmpfap = pack.ep_fa;
450 while (*tmpfap != NULL) {
451 char *cp;
452
453 cp = *tmpfap;
454 while (*cp)
455 *dp++ = *cp++;
456 dp++;
457
458 FREE(*tmpfap, M_EXEC);
459 tmpfap++; argc++;
460 }
461 FREE(pack.ep_fa, M_EXEC);
462 pack.ep_flags &= ~EXEC_HASARGL;
463 }
464
465 /* Now get argv & environment */
466 if (!(cpp = SCARG(uap, argp))) {
467 error = EINVAL;
468 goto bad;
469 }
470
471 if (pack.ep_flags & EXEC_SKIPARG)
472 cpp++;
473
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 #ifdef KTRACE
486 if (KTRPOINT(p, KTR_EXEC_ARG))
487 ktrkmem(p, KTR_EXEC_ARG, dp, len - 1);
488 #endif
489 dp += len;
490 cpp++;
491 argc++;
492 }
493
494 envc = 0;
495 /* environment need not be there */
496 if ((cpp = SCARG(uap, envp)) != NULL ) {
497 while (1) {
498 len = argp + ARG_MAX - dp;
499 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
500 goto bad;
501 if (!sp)
502 break;
503 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
504 if (error == ENAMETOOLONG)
505 error = E2BIG;
506 goto bad;
507 }
508 #ifdef KTRACE
509 if (KTRPOINT(p, KTR_EXEC_ENV))
510 ktrkmem(p, KTR_EXEC_ENV, dp, len - 1);
511 #endif
512 dp += len;
513 cpp++;
514 envc++;
515 }
516 }
517
518 dp = (char *) ALIGN(dp);
519
520 szsigcode = pack.ep_es->es_emul->e_esigcode -
521 pack.ep_es->es_emul->e_sigcode;
522
523 /* Now check if args & environ fit into new stack */
524 if (pack.ep_flags & EXEC_32)
525 len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
526 sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
527 szsigcode + sizeof(struct ps_strings)) - argp;
528 else
529 len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
530 sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
531 szsigcode + sizeof(struct ps_strings)) - argp;
532
533 len = ALIGN(len); /* make the stack "safely" aligned */
534
535 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
536 error = ENOMEM;
537 goto bad;
538 }
539
540 /* Get rid of other LWPs/ */
541 p->p_flag |= P_WEXIT; /* XXX hack. lwp-exit stuff wants to see it. */
542 exit_lwps(l);
543 p->p_flag &= ~P_WEXIT;
544 KDASSERT(p->p_nlwps == 1);
545
546 /* This is now LWP 1 */
547 l->l_lid = 1;
548 p->p_nlwpid = 1;
549
550 /* Release any SA state. */
551 if (p->p_sa)
552 sa_release(p);
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_pptr->p_nstopchild++;
822 p->p_stat = SSTOP;
823 l->l_stat = LSSTOP;
824 p->p_nrlwps--;
825 mi_switch(l, NULL);
826 SCHED_ASSERT_UNLOCKED();
827 splx(s);
828 }
829
830 return (EJUSTRETURN);
831
832 bad:
833 p->p_flag &= ~P_INEXEC;
834 /* free the vmspace-creation commands, and release their references */
835 kill_vmcmds(&pack.ep_vmcmds);
836 /* kill any opened file descriptor, if necessary */
837 if (pack.ep_flags & EXEC_HASFD) {
838 pack.ep_flags &= ~EXEC_HASFD;
839 (void) fdrelease(p, pack.ep_fd);
840 }
841 /* close and put the exec'd file */
842 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
843 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
844 vput(pack.ep_vp);
845 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
846 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
847
848 freehdr:
849 l->l_flag |= oldlwpflags;
850 p->p_flag &= ~P_INEXEC;
851 #ifdef LKM
852 lockmgr(&exec_lock, LK_RELEASE, NULL);
853 #endif
854
855 free(pack.ep_hdr, M_EXEC);
856 return error;
857
858 exec_abort:
859 p->p_flag &= ~P_INEXEC;
860 #ifdef LKM
861 lockmgr(&exec_lock, LK_RELEASE, NULL);
862 #endif
863
864 /*
865 * the old process doesn't exist anymore. exit gracefully.
866 * get rid of the (new) address space we have created, if any, get rid
867 * of our namei data and vnode, and exit noting failure
868 */
869 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
870 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
871 if (pack.ep_emul_arg)
872 FREE(pack.ep_emul_arg, M_TEMP);
873 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
874 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
875 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
876 vput(pack.ep_vp);
877 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
878 free(pack.ep_hdr, M_EXEC);
879 exit1(l, W_EXITCODE(error, SIGABRT));
880
881 /* NOTREACHED */
882 return 0;
883 }
884
885
886 int
887 copyargs(struct proc *p, struct exec_package *pack, struct ps_strings *arginfo,
888 char **stackp, void *argp)
889 {
890 char **cpp, *dp, *sp;
891 size_t len;
892 void *nullp;
893 long argc, envc;
894 int error;
895
896 cpp = (char **)*stackp;
897 nullp = NULL;
898 argc = arginfo->ps_nargvstr;
899 envc = arginfo->ps_nenvstr;
900 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
901 return error;
902
903 dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
904 sp = argp;
905
906 /* XXX don't copy them out, remap them! */
907 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
908
909 for (; --argc >= 0; sp += len, dp += len)
910 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
911 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
912 return error;
913
914 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
915 return error;
916
917 arginfo->ps_envstr = cpp; /* remember location of envp for later */
918
919 for (; --envc >= 0; sp += len, dp += len)
920 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
921 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
922 return error;
923
924 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
925 return error;
926
927 *stackp = (char *)cpp;
928 return 0;
929 }
930
931 #ifdef LKM
932 /*
933 * Find an emulation of given name in list of emulations.
934 * Needs to be called with the exec_lock held.
935 */
936 const struct emul *
937 emul_search(const char *name)
938 {
939 struct emul_entry *it;
940
941 LIST_FOREACH(it, &el_head, el_list) {
942 if (strcmp(name, it->el_emul->e_name) == 0)
943 return it->el_emul;
944 }
945
946 return NULL;
947 }
948
949 /*
950 * Add an emulation to list, if it's not there already.
951 */
952 int
953 emul_register(const struct emul *emul, int ro_entry)
954 {
955 struct emul_entry *ee;
956 int error;
957
958 error = 0;
959 lockmgr(&exec_lock, LK_SHARED, NULL);
960
961 if (emul_search(emul->e_name)) {
962 error = EEXIST;
963 goto out;
964 }
965
966 MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry),
967 M_EXEC, M_WAITOK);
968 ee->el_emul = emul;
969 ee->ro_entry = ro_entry;
970 LIST_INSERT_HEAD(&el_head, ee, el_list);
971
972 out:
973 lockmgr(&exec_lock, LK_RELEASE, NULL);
974 return error;
975 }
976
977 /*
978 * Remove emulation with name 'name' from list of supported emulations.
979 */
980 int
981 emul_unregister(const char *name)
982 {
983 const struct proclist_desc *pd;
984 struct emul_entry *it;
985 int i, error;
986 struct proc *ptmp;
987
988 error = 0;
989 lockmgr(&exec_lock, LK_SHARED, NULL);
990
991 LIST_FOREACH(it, &el_head, el_list) {
992 if (strcmp(it->el_emul->e_name, name) == 0)
993 break;
994 }
995
996 if (!it) {
997 error = ENOENT;
998 goto out;
999 }
1000
1001 if (it->ro_entry) {
1002 error = EBUSY;
1003 goto out;
1004 }
1005
1006 /* test if any execw[] entry is still using this */
1007 for(i=0; i < nexecs; i++) {
1008 if (execsw[i]->es_emul == it->el_emul) {
1009 error = EBUSY;
1010 goto out;
1011 }
1012 }
1013
1014 /*
1015 * Test if any process is running under this emulation - since
1016 * emul_unregister() is running quite sendomly, it's better
1017 * to do expensive check here than to use any locking.
1018 */
1019 proclist_lock_read();
1020 for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
1021 LIST_FOREACH(ptmp, pd->pd_list, p_list) {
1022 if (ptmp->p_emul == it->el_emul) {
1023 error = EBUSY;
1024 break;
1025 }
1026 }
1027 }
1028 proclist_unlock_read();
1029
1030 if (error)
1031 goto out;
1032
1033
1034 /* entry is not used, remove it */
1035 LIST_REMOVE(it, el_list);
1036 FREE(it, M_EXEC);
1037
1038 out:
1039 lockmgr(&exec_lock, LK_RELEASE, NULL);
1040 return error;
1041 }
1042
1043 /*
1044 * Add execsw[] entry.
1045 */
1046 int
1047 exec_add(struct execsw *esp, const char *e_name)
1048 {
1049 struct exec_entry *it;
1050 int error;
1051
1052 error = 0;
1053 lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
1054
1055 if (!esp->es_emul) {
1056 esp->es_emul = emul_search(e_name);
1057 if (!esp->es_emul) {
1058 error = ENOENT;
1059 goto out;
1060 }
1061 }
1062
1063 LIST_FOREACH(it, &ex_head, ex_list) {
1064 /* assume tuple (makecmds, probe_func, emulation) is unique */
1065 if (it->es->es_check == esp->es_check
1066 && it->es->u.elf_probe_func == esp->u.elf_probe_func
1067 && it->es->es_emul == esp->es_emul) {
1068 error = EEXIST;
1069 goto out;
1070 }
1071 }
1072
1073 /* if we got here, the entry doesn't exist yet */
1074 MALLOC(it, struct exec_entry *, sizeof(struct exec_entry),
1075 M_EXEC, M_WAITOK);
1076 it->es = esp;
1077 LIST_INSERT_HEAD(&ex_head, it, ex_list);
1078
1079 /* update execsw[] */
1080 exec_init(0);
1081
1082 out:
1083 lockmgr(&exec_lock, LK_RELEASE, NULL);
1084 return error;
1085 }
1086
1087 /*
1088 * Remove execsw[] entry.
1089 */
1090 int
1091 exec_remove(const struct execsw *esp)
1092 {
1093 struct exec_entry *it;
1094 int error;
1095
1096 error = 0;
1097 lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
1098
1099 LIST_FOREACH(it, &ex_head, ex_list) {
1100 /* assume tuple (makecmds, probe_func, emulation) is unique */
1101 if (it->es->es_check == esp->es_check
1102 && it->es->u.elf_probe_func == esp->u.elf_probe_func
1103 && it->es->es_emul == esp->es_emul)
1104 break;
1105 }
1106 if (!it) {
1107 error = ENOENT;
1108 goto out;
1109 }
1110
1111 /* remove item from list and free resources */
1112 LIST_REMOVE(it, ex_list);
1113 FREE(it, M_EXEC);
1114
1115 /* update execsw[] */
1116 exec_init(0);
1117
1118 out:
1119 lockmgr(&exec_lock, LK_RELEASE, NULL);
1120 return error;
1121 }
1122
1123 static void
1124 link_es(struct execsw_entry **listp, const struct execsw *esp)
1125 {
1126 struct execsw_entry *et, *e1;
1127
1128 MALLOC(et, struct execsw_entry *, sizeof(struct execsw_entry),
1129 M_TEMP, M_WAITOK);
1130 et->next = NULL;
1131 et->es = esp;
1132 if (*listp == NULL) {
1133 *listp = et;
1134 return;
1135 }
1136
1137 switch(et->es->es_prio) {
1138 case EXECSW_PRIO_FIRST:
1139 /* put new entry as the first */
1140 et->next = *listp;
1141 *listp = et;
1142 break;
1143 case EXECSW_PRIO_ANY:
1144 /* put new entry after all *_FIRST and *_ANY entries */
1145 for(e1 = *listp; e1->next
1146 && e1->next->es->es_prio != EXECSW_PRIO_LAST;
1147 e1 = e1->next);
1148 et->next = e1->next;
1149 e1->next = et;
1150 break;
1151 case EXECSW_PRIO_LAST:
1152 /* put new entry as the last one */
1153 for(e1 = *listp; e1->next; e1 = e1->next);
1154 e1->next = et;
1155 break;
1156 default:
1157 #ifdef DIAGNOSTIC
1158 panic("execw[] entry with unknown priority %d found",
1159 et->es->es_prio);
1160 #endif
1161 break;
1162 }
1163 }
1164
1165 /*
1166 * Initialize exec structures. If init_boot is true, also does necessary
1167 * one-time initialization (it's called from main() that way).
1168 * Once system is multiuser, this should be called with exec_lock held,
1169 * i.e. via exec_{add|remove}().
1170 */
1171 int
1172 exec_init(int init_boot)
1173 {
1174 const struct execsw **new_es, * const *old_es;
1175 struct execsw_entry *list, *e1;
1176 struct exec_entry *e2;
1177 int i, es_sz;
1178
1179 if (init_boot) {
1180 /* do one-time initializations */
1181 lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
1182
1183 /* register compiled-in emulations */
1184 for(i=0; i < nexecs_builtin; i++) {
1185 if (execsw_builtin[i].es_emul)
1186 emul_register(execsw_builtin[i].es_emul, 1);
1187 }
1188 #ifdef DIAGNOSTIC
1189 if (i == 0)
1190 panic("no emulations found in execsw_builtin[]");
1191 #endif
1192 }
1193
1194 /*
1195 * Build execsw[] array from builtin entries and entries added
1196 * at runtime.
1197 */
1198 list = NULL;
1199 for(i=0; i < nexecs_builtin; i++)
1200 link_es(&list, &execsw_builtin[i]);
1201
1202 /* Add dynamically loaded entries */
1203 es_sz = nexecs_builtin;
1204 LIST_FOREACH(e2, &ex_head, ex_list) {
1205 link_es(&list, e2->es);
1206 es_sz++;
1207 }
1208
1209 /*
1210 * Now that we have sorted all execw entries, create new execsw[]
1211 * and free no longer needed memory in the process.
1212 */
1213 new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK);
1214 for(i=0; list; i++) {
1215 new_es[i] = list->es;
1216 e1 = list->next;
1217 FREE(list, M_TEMP);
1218 list = e1;
1219 }
1220
1221 /*
1222 * New execsw[] array built, now replace old execsw[] and free
1223 * used memory.
1224 */
1225 old_es = execsw;
1226 execsw = new_es;
1227 nexecs = es_sz;
1228 if (old_es)
1229 free((void *)old_es, M_EXEC);
1230
1231 /*
1232 * Figure out the maximum size of an exec header.
1233 */
1234 exec_maxhdrsz = 0;
1235 for (i = 0; i < nexecs; i++) {
1236 if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1237 exec_maxhdrsz = execsw[i]->es_hdrsz;
1238 }
1239
1240 return 0;
1241 }
1242 #endif
1243
1244 #ifndef LKM
1245 /*
1246 * Simplified exec_init() for kernels without LKMs. Only initialize
1247 * exec_maxhdrsz and execsw[].
1248 */
1249 int
1250 exec_init(int init_boot)
1251 {
1252 int i;
1253
1254 #ifdef DIAGNOSTIC
1255 if (!init_boot)
1256 panic("exec_init(): called with init_boot == 0");
1257 #endif
1258
1259 /* do one-time initializations */
1260 nexecs = nexecs_builtin;
1261 execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK);
1262
1263 /*
1264 * Fill in execsw[] and figure out the maximum size of an exec header.
1265 */
1266 exec_maxhdrsz = 0;
1267 for(i=0; i < nexecs; i++) {
1268 execsw[i] = &execsw_builtin[i];
1269 if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
1270 exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
1271 }
1272
1273 return 0;
1274
1275 }
1276 #endif /* !LKM */
1277
1278 static int
1279 exec_sigcode_map(struct proc *p, const struct emul *e)
1280 {
1281 vaddr_t va;
1282 vsize_t sz;
1283 int error;
1284 struct uvm_object *uobj;
1285
1286 if (e->e_sigobject == NULL) {
1287 return 0;
1288 }
1289
1290 /*
1291 * If we don't have a sigobject for this emulation, create one.
1292 *
1293 * sigobject is an anonymous memory object (just like SYSV shared
1294 * memory) that we keep a permanent reference to and that we map
1295 * in all processes that need this sigcode. The creation is simple,
1296 * we create an object, add a permanent reference to it, map it in
1297 * kernel space, copy out the sigcode to it and unmap it.
1298 * The we map it with PROT_READ|PROT_EXEC into the process just
1299 * the way sys_mmap would map it.
1300 */
1301
1302 sz = (vaddr_t)e->e_esigcode - (vaddr_t)e->e_sigcode;
1303 uobj = *e->e_sigobject;
1304 if (uobj == NULL) {
1305 uobj = uao_create(sz, 0);
1306 uao_reference(uobj);
1307 va = vm_map_min(kernel_map);
1308 if ((error = uvm_map(kernel_map, &va, round_page(sz),
1309 uobj, 0, 0,
1310 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
1311 UVM_INH_SHARE, UVM_ADV_RANDOM, 0)))) {
1312 printf("kernel mapping failed %d\n", error);
1313 (*uobj->pgops->pgo_detach)(uobj);
1314 return (error);
1315 }
1316 memcpy((void *)va, e->e_sigcode, sz);
1317 #ifdef PMAP_NEED_PROCWR
1318 pmap_procwr(&proc0, va, sz);
1319 #endif
1320 uvm_unmap(kernel_map, va, va + round_page(sz));
1321 *e->e_sigobject = uobj;
1322 }
1323
1324 /* Just a hint to uvm_map where to put it. */
1325 va = VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, round_page(sz));
1326 (*uobj->pgops->pgo_reference)(uobj);
1327 error = uvm_map(&p->p_vmspace->vm_map, &va, round_page(sz),
1328 uobj, 0, 0,
1329 UVM_MAPFLAG(UVM_PROT_RX, UVM_PROT_RX, UVM_INH_SHARE,
1330 UVM_ADV_RANDOM, 0));
1331 if (error) {
1332 (*uobj->pgops->pgo_detach)(uobj);
1333 return (error);
1334 }
1335 p->p_sigctx.ps_sigcode = (void *)va;
1336 return (0);
1337 }
1338