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