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