kern_exec.c revision 1.147 1 /* $NetBSD: kern_exec.c,v 1.147 2001/11/23 22:02:39 jdolecek 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.147 2001/11/23 22:02:39 jdolecek 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 cred = p->p_ucred;
344 base_vcp = NULL;
345 /*
346 * Init the namei data to point the file user's program name.
347 * This is done here rather than in check_exec(), so that it's
348 * possible to override this settings if any of makecmd/probe
349 * functions call check_exec() recursively - for example,
350 * see exec_script_makecmds().
351 */
352 NDINIT(&nid, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
353
354 /*
355 * initialize the fields of the exec package.
356 */
357 pack.ep_name = SCARG(uap, path);
358 pack.ep_hdr = malloc(exec_maxhdrsz, M_EXEC, M_WAITOK);
359 pack.ep_hdrlen = exec_maxhdrsz;
360 pack.ep_hdrvalid = 0;
361 pack.ep_ndp = &nid;
362 pack.ep_emul_arg = NULL;
363 pack.ep_vmcmds.evs_cnt = 0;
364 pack.ep_vmcmds.evs_used = 0;
365 pack.ep_vap = &attr;
366 pack.ep_flags = 0;
367
368 #ifdef LKM
369 lockmgr(&exec_lock, LK_SHARED, NULL);
370 #endif
371
372 /* see if we can run it. */
373 if ((error = check_exec(p, &pack)) != 0)
374 goto freehdr;
375
376 /* XXX -- THE FOLLOWING SECTION NEEDS MAJOR CLEANUP */
377
378 /* allocate an argument buffer */
379 argp = (char *) uvm_km_valloc_wait(exec_map, NCARGS);
380 #ifdef DIAGNOSTIC
381 if (argp == (vaddr_t) 0)
382 panic("execve: argp == NULL");
383 #endif
384 dp = argp;
385 argc = 0;
386
387 /* copy the fake args list, if there's one, freeing it as we go */
388 if (pack.ep_flags & EXEC_HASARGL) {
389 tmpfap = pack.ep_fa;
390 while (*tmpfap != NULL) {
391 char *cp;
392
393 cp = *tmpfap;
394 while (*cp)
395 *dp++ = *cp++;
396 dp++;
397
398 FREE(*tmpfap, M_EXEC);
399 tmpfap++; argc++;
400 }
401 FREE(pack.ep_fa, M_EXEC);
402 pack.ep_flags &= ~EXEC_HASARGL;
403 }
404
405 /* Now get argv & environment */
406 if (!(cpp = SCARG(uap, argp))) {
407 error = EINVAL;
408 goto bad;
409 }
410
411 if (pack.ep_flags & EXEC_SKIPARG)
412 cpp++;
413
414 while (1) {
415 len = argp + ARG_MAX - dp;
416 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
417 goto bad;
418 if (!sp)
419 break;
420 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
421 if (error == ENAMETOOLONG)
422 error = E2BIG;
423 goto bad;
424 }
425 dp += len;
426 cpp++;
427 argc++;
428 }
429
430 envc = 0;
431 /* environment need not be there */
432 if ((cpp = SCARG(uap, envp)) != NULL ) {
433 while (1) {
434 len = argp + ARG_MAX - dp;
435 if ((error = copyin(cpp, &sp, sizeof(sp))) != 0)
436 goto bad;
437 if (!sp)
438 break;
439 if ((error = copyinstr(sp, dp, len, &len)) != 0) {
440 if (error == ENAMETOOLONG)
441 error = E2BIG;
442 goto bad;
443 }
444 dp += len;
445 cpp++;
446 envc++;
447 }
448 }
449
450 dp = (char *) ALIGN(dp);
451
452 szsigcode = pack.ep_es->es_emul->e_esigcode -
453 pack.ep_es->es_emul->e_sigcode;
454
455 /* Now check if args & environ fit into new stack */
456 if (pack.ep_flags & EXEC_32)
457 len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
458 sizeof(int) + sizeof(int) + dp + STACKGAPLEN +
459 szsigcode + sizeof(struct ps_strings)) - argp;
460 else
461 len = ((argc + envc + 2 + pack.ep_es->es_arglen) *
462 sizeof(char *) + sizeof(int) + dp + STACKGAPLEN +
463 szsigcode + sizeof(struct ps_strings)) - argp;
464
465 len = ALIGN(len); /* make the stack "safely" aligned */
466
467 if (len > pack.ep_ssize) { /* in effect, compare to initial limit */
468 error = ENOMEM;
469 goto bad;
470 }
471
472 /* adjust "active stack depth" for process VSZ */
473 pack.ep_ssize = len; /* maybe should go elsewhere, but... */
474
475 /*
476 * Do whatever is necessary to prepare the address space
477 * for remapping. Note that this might replace the current
478 * vmspace with another!
479 */
480 uvmspace_exec(p, pack.ep_vm_minaddr, pack.ep_vm_maxaddr);
481
482 /* Now map address space */
483 vm = p->p_vmspace;
484 vm->vm_taddr = (char *) pack.ep_taddr;
485 vm->vm_tsize = btoc(pack.ep_tsize);
486 vm->vm_daddr = (char *) pack.ep_daddr;
487 vm->vm_dsize = btoc(pack.ep_dsize);
488 vm->vm_ssize = btoc(pack.ep_ssize);
489 vm->vm_maxsaddr = (char *) pack.ep_maxsaddr;
490 vm->vm_minsaddr = (char *) pack.ep_minsaddr;
491
492 /* create the new process's VM space by running the vmcmds */
493 #ifdef DIAGNOSTIC
494 if (pack.ep_vmcmds.evs_used == 0)
495 panic("execve: no vmcmds");
496 #endif
497 for (i = 0; i < pack.ep_vmcmds.evs_used && !error; i++) {
498 struct exec_vmcmd *vcp;
499
500 vcp = &pack.ep_vmcmds.evs_cmds[i];
501 if (vcp->ev_flags & VMCMD_RELATIVE) {
502 #ifdef DIAGNOSTIC
503 if (base_vcp == NULL)
504 panic("execve: relative vmcmd with no base");
505 if (vcp->ev_flags & VMCMD_BASE)
506 panic("execve: illegal base & relative vmcmd");
507 #endif
508 vcp->ev_addr += base_vcp->ev_addr;
509 }
510 error = (*vcp->ev_proc)(p, vcp);
511 #ifdef DEBUG_EXEC
512 if (error) {
513 int j;
514 struct exec_vmcmd *vp = &pack.ep_vmcmds.evs_cmds[0];
515 for (j = 0; j <= i; j++)
516 uprintf(
517 "vmcmd[%d] = %#lx/%#lx fd@%#lx prot=0%o flags=%d\n",
518 j, vp[j].ev_addr, vp[j].ev_len,
519 vp[j].ev_offset, vp[j].ev_prot,
520 vp[j].ev_flags);
521 }
522 #endif /* DEBUG_EXEC */
523 if (vcp->ev_flags & VMCMD_BASE)
524 base_vcp = vcp;
525 }
526
527 /* free the vmspace-creation commands, and release their references */
528 kill_vmcmds(&pack.ep_vmcmds);
529
530 /* if an error happened, deallocate and punt */
531 if (error) {
532 DPRINTF(("execve: vmcmd %i failed: %d\n", i - 1, error));
533 goto exec_abort;
534 }
535
536 /* remember information about the process */
537 arginfo.ps_nargvstr = argc;
538 arginfo.ps_nenvstr = envc;
539
540 stack = (char *) (vm->vm_minsaddr - len);
541 /* Now copy argc, args & environ to new stack */
542 error = (*pack.ep_es->es_copyargs)(&pack, &arginfo, &stack, argp);
543 if (error) {
544 DPRINTF(("execve: copyargs failed %d\n", error));
545 goto exec_abort;
546 }
547 /* Move the stack back to original point */
548 stack = (char *) (vm->vm_minsaddr - len);
549
550 /* fill process ps_strings info */
551 p->p_psstr = (struct ps_strings *)(vm->vm_minsaddr
552 - sizeof(struct ps_strings));
553 p->p_psargv = offsetof(struct ps_strings, ps_argvstr);
554 p->p_psnargv = offsetof(struct ps_strings, ps_nargvstr);
555 p->p_psenv = offsetof(struct ps_strings, ps_envstr);
556 p->p_psnenv = offsetof(struct ps_strings, ps_nenvstr);
557
558 /* copy out the process's ps_strings structure */
559 if ((error = copyout(&arginfo, (char *)p->p_psstr,
560 sizeof(arginfo))) != 0) {
561 DPRINTF(("execve: ps_strings copyout %p->%p size %ld failed\n",
562 &arginfo, (char *)p->p_psstr, (long)sizeof(arginfo)));
563 goto exec_abort;
564 }
565
566 /* copy out the process's signal trapoline code */
567 if (szsigcode) {
568 if ((error = copyout((char *)pack.ep_es->es_emul->e_sigcode,
569 p->p_sigctx.ps_sigcode = (char *)p->p_psstr - szsigcode,
570 szsigcode)) != 0) {
571 DPRINTF(("execve: sig trampoline copyout failed\n"));
572 goto exec_abort;
573 }
574 #ifdef PMAP_NEED_PROCWR
575 /* This is code. Let the pmap do what is needed. */
576 pmap_procwr(p, (vaddr_t)p->p_sigctx.ps_sigcode, szsigcode);
577 #endif
578 }
579
580 stopprofclock(p); /* stop profiling */
581 fdcloseexec(p); /* handle close on exec */
582 execsigs(p); /* reset catched signals */
583 p->p_ctxlink = NULL; /* reset ucontext link */
584
585 /* set command name & other accounting info */
586 len = min(nid.ni_cnd.cn_namelen, MAXCOMLEN);
587 memcpy(p->p_comm, nid.ni_cnd.cn_nameptr, len);
588 p->p_comm[len] = 0;
589 p->p_acflag &= ~AFORK;
590
591 /* record proc's vnode, for use by procfs and others */
592 if (p->p_textvp)
593 vrele(p->p_textvp);
594 VREF(pack.ep_vp);
595 p->p_textvp = pack.ep_vp;
596
597 p->p_flag |= P_EXEC;
598 if (p->p_flag & P_PPWAIT) {
599 p->p_flag &= ~P_PPWAIT;
600 wakeup((caddr_t) p->p_pptr);
601 }
602
603 /*
604 * deal with set[ug]id.
605 * MNT_NOSUID has already been used to disable s[ug]id.
606 */
607 if ((p->p_flag & P_TRACED) == 0 &&
608
609 (((attr.va_mode & S_ISUID) != 0 &&
610 p->p_ucred->cr_uid != attr.va_uid) ||
611
612 ((attr.va_mode & S_ISGID) != 0 &&
613 p->p_ucred->cr_gid != attr.va_gid))) {
614 /*
615 * Mark the process as SUGID before we do
616 * anything that might block.
617 */
618 p_sugid(p);
619
620 p->p_ucred = crcopy(cred);
621 #ifdef KTRACE
622 /*
623 * If process is being ktraced, turn off - unless
624 * root set it.
625 */
626 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT))
627 ktrderef(p);
628 #endif
629 if (attr.va_mode & S_ISUID)
630 p->p_ucred->cr_uid = attr.va_uid;
631 if (attr.va_mode & S_ISGID)
632 p->p_ucred->cr_gid = attr.va_gid;
633 } else
634 p->p_flag &= ~P_SUGID;
635 p->p_cred->p_svuid = p->p_ucred->cr_uid;
636 p->p_cred->p_svgid = p->p_ucred->cr_gid;
637
638 doexechooks(p);
639
640 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
641
642 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
643 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
644 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
645 vput(pack.ep_vp);
646
647 /* setup new registers and do misc. setup. */
648 (*pack.ep_es->es_emul->e_setregs)(p, &pack, (u_long) stack);
649 if (pack.ep_es->es_setregs)
650 (*pack.ep_es->es_setregs)(p, &pack, (u_long) stack);
651
652 if (p->p_flag & P_TRACED)
653 psignal(p, SIGTRAP);
654
655 free(pack.ep_hdr, M_EXEC);
656
657 /*
658 * Call emulation specific exec hook. This can setup setup per-process
659 * p->p_emuldata or do any other per-process stuff an emulation needs.
660 *
661 * If we are executing process of different emulation than the
662 * original forked process, call e_proc_exit() of the old emulation
663 * first, then e_proc_exec() of new emulation. If the emulation is
664 * same, the exec hook code should deallocate any old emulation
665 * resources held previously by this process.
666 */
667 if (p->p_emul && p->p_emul->e_proc_exit
668 && p->p_emul != pack.ep_es->es_emul)
669 (*p->p_emul->e_proc_exit)(p);
670
671 /*
672 * Call exec hook. Emulation code may NOT store reference to anything
673 * from &pack.
674 */
675 if (pack.ep_es->es_emul->e_proc_exec)
676 (*pack.ep_es->es_emul->e_proc_exec)(p, &pack);
677
678 /* update p_emul, the old value is no longer needed */
679 p->p_emul = pack.ep_es->es_emul;
680 #ifdef __HAVE_SYSCALL_INTERN
681 (*p->p_emul->e_syscall_intern)(p);
682 #endif
683 #ifdef KTRACE
684 if (KTRPOINT(p, KTR_EMUL))
685 ktremul(p);
686 #endif
687
688 #ifdef LKM
689 lockmgr(&exec_lock, LK_RELEASE, NULL);
690 #endif
691
692 return (EJUSTRETURN);
693
694 bad:
695 /* free the vmspace-creation commands, and release their references */
696 kill_vmcmds(&pack.ep_vmcmds);
697 /* kill any opened file descriptor, if necessary */
698 if (pack.ep_flags & EXEC_HASFD) {
699 pack.ep_flags &= ~EXEC_HASFD;
700 (void) fdrelease(p, pack.ep_fd);
701 }
702 /* close and put the exec'd file */
703 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
704 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
705 vput(pack.ep_vp);
706 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
707 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
708
709 freehdr:
710 #ifdef LKM
711 lockmgr(&exec_lock, LK_RELEASE, NULL);
712 #endif
713
714 free(pack.ep_hdr, M_EXEC);
715 return error;
716
717 exec_abort:
718 #ifdef LKM
719 lockmgr(&exec_lock, LK_RELEASE, NULL);
720 #endif
721
722 /*
723 * the old process doesn't exist anymore. exit gracefully.
724 * get rid of the (new) address space we have created, if any, get rid
725 * of our namei data and vnode, and exit noting failure
726 */
727 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
728 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
729 if (pack.ep_emul_arg)
730 FREE(pack.ep_emul_arg, M_TEMP);
731 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
732 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
733 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
734 vput(pack.ep_vp);
735 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
736 free(pack.ep_hdr, M_EXEC);
737 exit1(p, W_EXITCODE(error, SIGABRT));
738
739 /* NOTREACHED */
740 return 0;
741 }
742
743
744 int
745 copyargs(struct exec_package *pack, struct ps_strings *arginfo,
746 char **stackp, void *argp)
747 {
748 char **cpp, *dp, *sp;
749 size_t len;
750 void *nullp;
751 long argc, envc;
752 int error;
753
754 cpp = (char **)*stackp;
755 nullp = NULL;
756 argc = arginfo->ps_nargvstr;
757 envc = arginfo->ps_nenvstr;
758 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
759 return error;
760
761 dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
762 sp = argp;
763
764 /* XXX don't copy them out, remap them! */
765 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
766
767 for (; --argc >= 0; sp += len, dp += len)
768 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
769 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
770 return error;
771
772 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
773 return error;
774
775 arginfo->ps_envstr = cpp; /* remember location of envp for later */
776
777 for (; --envc >= 0; sp += len, dp += len)
778 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
779 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
780 return error;
781
782 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
783 return error;
784
785 *stackp = (char *)cpp;
786 return 0;
787 }
788
789 #ifdef LKM
790 /*
791 * Find an emulation of given name in list of emulations.
792 */
793 static const struct emul *
794 emul_search(const char *name)
795 {
796 struct emul_entry *it;
797
798 LIST_FOREACH(it, &el_head, el_list) {
799 if (strcmp(name, it->el_emul->e_name) == 0)
800 return it->el_emul;
801 }
802
803 return NULL;
804 }
805
806 /*
807 * Add an emulation to list, if it's not there already.
808 */
809 int
810 emul_register(const struct emul *emul, int ro_entry)
811 {
812 struct emul_entry *ee;
813 int error;
814
815 error = 0;
816 lockmgr(&exec_lock, LK_SHARED, NULL);
817
818 if (emul_search(emul->e_name)) {
819 error = EEXIST;
820 goto out;
821 }
822
823 MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry),
824 M_EXEC, M_WAITOK);
825 ee->el_emul = emul;
826 ee->ro_entry = ro_entry;
827 LIST_INSERT_HEAD(&el_head, ee, el_list);
828
829 out:
830 lockmgr(&exec_lock, LK_RELEASE, NULL);
831 return error;
832 }
833
834 /*
835 * Remove emulation with name 'name' from list of supported emulations.
836 */
837 int
838 emul_unregister(const char *name)
839 {
840 const struct proclist_desc *pd;
841 struct emul_entry *it;
842 int i, error;
843 struct proc *ptmp;
844
845 error = 0;
846 lockmgr(&exec_lock, LK_SHARED, NULL);
847
848 LIST_FOREACH(it, &el_head, el_list) {
849 if (strcmp(it->el_emul->e_name, name) == 0)
850 break;
851 }
852
853 if (!it) {
854 error = ENOENT;
855 goto out;
856 }
857
858 if (it->ro_entry) {
859 error = EBUSY;
860 goto out;
861 }
862
863 /* test if any execw[] entry is still using this */
864 for(i=0; i < nexecs; i++) {
865 if (execsw[i]->es_emul == it->el_emul) {
866 error = EBUSY;
867 goto out;
868 }
869 }
870
871 /*
872 * Test if any process is running under this emulation - since
873 * emul_unregister() is running quite sendomly, it's better
874 * to do expensive check here than to use any locking.
875 */
876 proclist_lock_read();
877 for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
878 LIST_FOREACH(ptmp, pd->pd_list, p_list) {
879 if (ptmp->p_emul == it->el_emul) {
880 error = EBUSY;
881 break;
882 }
883 }
884 }
885 proclist_unlock_read();
886
887 if (error)
888 goto out;
889
890
891 /* entry is not used, remove it */
892 LIST_REMOVE(it, el_list);
893 FREE(it, M_EXEC);
894
895 out:
896 lockmgr(&exec_lock, LK_RELEASE, NULL);
897 return error;
898 }
899
900 /*
901 * Add execsw[] entry.
902 */
903 int
904 exec_add(struct execsw *esp, const char *e_name)
905 {
906 struct exec_entry *it;
907 int error;
908
909 error = 0;
910 lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
911
912 if (!esp->es_emul) {
913 esp->es_emul = emul_search(e_name);
914 if (!esp->es_emul) {
915 error = ENOENT;
916 goto out;
917 }
918 }
919
920 LIST_FOREACH(it, &ex_head, ex_list) {
921 /* assume tuple (makecmds, probe_func, emulation) is unique */
922 if (it->es->es_check == esp->es_check
923 && it->es->u.elf_probe_func == esp->u.elf_probe_func
924 && it->es->es_emul == esp->es_emul) {
925 error = EEXIST;
926 goto out;
927 }
928 }
929
930 /* if we got here, the entry doesn't exist yet */
931 MALLOC(it, struct exec_entry *, sizeof(struct exec_entry),
932 M_EXEC, M_WAITOK);
933 it->es = esp;
934 LIST_INSERT_HEAD(&ex_head, it, ex_list);
935
936 /* update execsw[] */
937 exec_init(0);
938
939 out:
940 lockmgr(&exec_lock, LK_RELEASE, NULL);
941 return error;
942 }
943
944 /*
945 * Remove execsw[] entry.
946 */
947 int
948 exec_remove(const struct execsw *esp)
949 {
950 struct exec_entry *it;
951 int error;
952
953 error = 0;
954 lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
955
956 LIST_FOREACH(it, &ex_head, ex_list) {
957 /* assume tuple (makecmds, probe_func, emulation) is unique */
958 if (it->es->es_check == esp->es_check
959 && it->es->u.elf_probe_func == esp->u.elf_probe_func
960 && it->es->es_emul == esp->es_emul)
961 break;
962 }
963 if (!it) {
964 error = ENOENT;
965 goto out;
966 }
967
968 /* remove item from list and free resources */
969 LIST_REMOVE(it, ex_list);
970 FREE(it, M_EXEC);
971
972 /* update execsw[] */
973 exec_init(0);
974
975 out:
976 lockmgr(&exec_lock, LK_RELEASE, NULL);
977 return error;
978 }
979
980 static void
981 link_es(struct execsw_entry **listp, const struct execsw *esp)
982 {
983 struct execsw_entry *et, *e1;
984
985 MALLOC(et, struct execsw_entry *, sizeof(struct execsw_entry),
986 M_TEMP, M_WAITOK);
987 et->next = NULL;
988 et->es = esp;
989 if (*listp == NULL) {
990 *listp = et;
991 return;
992 }
993
994 switch(et->es->es_prio) {
995 case EXECSW_PRIO_FIRST:
996 /* put new entry as the first */
997 et->next = *listp;
998 *listp = et;
999 break;
1000 case EXECSW_PRIO_ANY:
1001 /* put new entry after all *_FIRST and *_ANY entries */
1002 for(e1 = *listp; e1->next
1003 && e1->next->es->es_prio != EXECSW_PRIO_LAST;
1004 e1 = e1->next);
1005 et->next = e1->next;
1006 e1->next = et;
1007 break;
1008 case EXECSW_PRIO_LAST:
1009 /* put new entry as the last one */
1010 for(e1 = *listp; e1->next; e1 = e1->next);
1011 e1->next = et;
1012 break;
1013 default:
1014 #ifdef DIAGNOSTIC
1015 panic("execw[] entry with unknown priority %d found\n",
1016 et->es->es_prio);
1017 #endif
1018 break;
1019 }
1020 }
1021
1022 /*
1023 * Initialize exec structures. If init_boot is true, also does necessary
1024 * one-time initialization (it's called from main() that way).
1025 * Once system is multiuser, this should be called with exec_lock held,
1026 * i.e. via exec_{add|remove}().
1027 */
1028 int
1029 exec_init(int init_boot)
1030 {
1031 const struct execsw **new_es, * const *old_es;
1032 struct execsw_entry *list, *e1;
1033 struct exec_entry *e2;
1034 int i, es_sz;
1035
1036 if (init_boot) {
1037 /* do one-time initializations */
1038 lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
1039
1040 /* register compiled-in emulations */
1041 for(i=0; i < nexecs_builtin; i++) {
1042 if (execsw_builtin[i].es_emul)
1043 emul_register(execsw_builtin[i].es_emul, 1);
1044 }
1045 #ifdef DIAGNOSTIC
1046 if (i == 0)
1047 panic("no emulations found in execsw_builtin[]\n");
1048 #endif
1049 }
1050
1051 /*
1052 * Build execsw[] array from builtin entries and entries added
1053 * at runtime.
1054 */
1055 list = NULL;
1056 for(i=0; i < nexecs_builtin; i++)
1057 link_es(&list, &execsw_builtin[i]);
1058
1059 /* Add dynamically loaded entries */
1060 es_sz = nexecs_builtin;
1061 LIST_FOREACH(e2, &ex_head, ex_list) {
1062 link_es(&list, e2->es);
1063 es_sz++;
1064 }
1065
1066 /*
1067 * Now that we have sorted all execw entries, create new execsw[]
1068 * and free no longer needed memory in the process.
1069 */
1070 new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK);
1071 for(i=0; list; i++) {
1072 new_es[i] = list->es;
1073 e1 = list->next;
1074 FREE(list, M_TEMP);
1075 list = e1;
1076 }
1077
1078 /*
1079 * New execsw[] array built, now replace old execsw[] and free
1080 * used memory.
1081 */
1082 old_es = execsw;
1083 execsw = new_es;
1084 nexecs = es_sz;
1085 if (old_es)
1086 free((void *)old_es, M_EXEC);
1087
1088 /*
1089 * Figure out the maximum size of an exec header.
1090 */
1091 exec_maxhdrsz = 0;
1092 for (i = 0; i < nexecs; i++) {
1093 if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1094 exec_maxhdrsz = execsw[i]->es_hdrsz;
1095 }
1096
1097 return 0;
1098 }
1099 #endif
1100
1101 #ifndef LKM
1102 /*
1103 * Simplified exec_init() for kernels without LKMs. Only initialize
1104 * exec_maxhdrsz and execsw[].
1105 */
1106 int
1107 exec_init(int init_boot)
1108 {
1109 int i;
1110
1111 #ifdef DIAGNOSTIC
1112 if (!init_boot)
1113 panic("exec_init(): called with init_boot == 0");
1114 #endif
1115
1116 /* do one-time initializations */
1117 nexecs = nexecs_builtin;
1118 execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK);
1119
1120 /*
1121 * Fill in execsw[] and figure out the maximum size of an exec header.
1122 */
1123 exec_maxhdrsz = 0;
1124 for(i=0; i < nexecs; i++) {
1125 execsw[i] = &execsw_builtin[i];
1126 if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
1127 exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
1128 }
1129
1130 return 0;
1131
1132 }
1133 #endif /* !LKM */
1134