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