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