kern_exec.c revision 1.161 1 /* $NetBSD: kern_exec.c,v 1.161 2002/11/01 19:27:05 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.161 2002/11/01 19:27:05 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/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 return (EJUSTRETURN);
742
743 bad:
744 p->p_flag &= ~P_INEXEC;
745 /* free the vmspace-creation commands, and release their references */
746 kill_vmcmds(&pack.ep_vmcmds);
747 /* kill any opened file descriptor, if necessary */
748 if (pack.ep_flags & EXEC_HASFD) {
749 pack.ep_flags &= ~EXEC_HASFD;
750 (void) fdrelease(p, pack.ep_fd);
751 }
752 /* close and put the exec'd file */
753 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
754 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
755 vput(pack.ep_vp);
756 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
757 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
758
759 freehdr:
760 p->p_flag &= ~P_INEXEC;
761 #ifdef LKM
762 lockmgr(&exec_lock, LK_RELEASE, NULL);
763 #endif
764
765 free(pack.ep_hdr, M_EXEC);
766 return error;
767
768 exec_abort:
769 p->p_flag &= ~P_INEXEC;
770 #ifdef LKM
771 lockmgr(&exec_lock, LK_RELEASE, NULL);
772 #endif
773
774 /*
775 * the old process doesn't exist anymore. exit gracefully.
776 * get rid of the (new) address space we have created, if any, get rid
777 * of our namei data and vnode, and exit noting failure
778 */
779 uvm_deallocate(&vm->vm_map, VM_MIN_ADDRESS,
780 VM_MAXUSER_ADDRESS - VM_MIN_ADDRESS);
781 if (pack.ep_emul_arg)
782 FREE(pack.ep_emul_arg, M_TEMP);
783 PNBUF_PUT(nid.ni_cnd.cn_pnbuf);
784 vn_lock(pack.ep_vp, LK_EXCLUSIVE | LK_RETRY);
785 VOP_CLOSE(pack.ep_vp, FREAD, cred, p);
786 vput(pack.ep_vp);
787 uvm_km_free_wakeup(exec_map, (vaddr_t) argp, NCARGS);
788 free(pack.ep_hdr, M_EXEC);
789 exit1(p, W_EXITCODE(error, SIGABRT));
790
791 /* NOTREACHED */
792 return 0;
793 }
794
795
796 int
797 copyargs(struct proc *p, struct exec_package *pack, struct ps_strings *arginfo,
798 char **stackp, void *argp)
799 {
800 char **cpp, *dp, *sp;
801 size_t len;
802 void *nullp;
803 long argc, envc;
804 int error;
805
806 cpp = (char **)*stackp;
807 nullp = NULL;
808 argc = arginfo->ps_nargvstr;
809 envc = arginfo->ps_nenvstr;
810 if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
811 return error;
812
813 dp = (char *) (cpp + argc + envc + 2 + pack->ep_es->es_arglen);
814 sp = argp;
815
816 /* XXX don't copy them out, remap them! */
817 arginfo->ps_argvstr = cpp; /* remember location of argv for later */
818
819 for (; --argc >= 0; sp += len, dp += len)
820 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
821 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
822 return error;
823
824 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
825 return error;
826
827 arginfo->ps_envstr = cpp; /* remember location of envp for later */
828
829 for (; --envc >= 0; sp += len, dp += len)
830 if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
831 (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
832 return error;
833
834 if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
835 return error;
836
837 *stackp = (char *)cpp;
838 return 0;
839 }
840
841 #ifdef LKM
842 /*
843 * Find an emulation of given name in list of emulations.
844 * Needs to be called with the exec_lock held.
845 */
846 const struct emul *
847 emul_search(const char *name)
848 {
849 struct emul_entry *it;
850
851 LIST_FOREACH(it, &el_head, el_list) {
852 if (strcmp(name, it->el_emul->e_name) == 0)
853 return it->el_emul;
854 }
855
856 return NULL;
857 }
858
859 /*
860 * Add an emulation to list, if it's not there already.
861 */
862 int
863 emul_register(const struct emul *emul, int ro_entry)
864 {
865 struct emul_entry *ee;
866 int error;
867
868 error = 0;
869 lockmgr(&exec_lock, LK_SHARED, NULL);
870
871 if (emul_search(emul->e_name)) {
872 error = EEXIST;
873 goto out;
874 }
875
876 MALLOC(ee, struct emul_entry *, sizeof(struct emul_entry),
877 M_EXEC, M_WAITOK);
878 ee->el_emul = emul;
879 ee->ro_entry = ro_entry;
880 LIST_INSERT_HEAD(&el_head, ee, el_list);
881
882 out:
883 lockmgr(&exec_lock, LK_RELEASE, NULL);
884 return error;
885 }
886
887 /*
888 * Remove emulation with name 'name' from list of supported emulations.
889 */
890 int
891 emul_unregister(const char *name)
892 {
893 const struct proclist_desc *pd;
894 struct emul_entry *it;
895 int i, error;
896 struct proc *ptmp;
897
898 error = 0;
899 lockmgr(&exec_lock, LK_SHARED, NULL);
900
901 LIST_FOREACH(it, &el_head, el_list) {
902 if (strcmp(it->el_emul->e_name, name) == 0)
903 break;
904 }
905
906 if (!it) {
907 error = ENOENT;
908 goto out;
909 }
910
911 if (it->ro_entry) {
912 error = EBUSY;
913 goto out;
914 }
915
916 /* test if any execw[] entry is still using this */
917 for(i=0; i < nexecs; i++) {
918 if (execsw[i]->es_emul == it->el_emul) {
919 error = EBUSY;
920 goto out;
921 }
922 }
923
924 /*
925 * Test if any process is running under this emulation - since
926 * emul_unregister() is running quite sendomly, it's better
927 * to do expensive check here than to use any locking.
928 */
929 proclist_lock_read();
930 for (pd = proclists; pd->pd_list != NULL && !error; pd++) {
931 LIST_FOREACH(ptmp, pd->pd_list, p_list) {
932 if (ptmp->p_emul == it->el_emul) {
933 error = EBUSY;
934 break;
935 }
936 }
937 }
938 proclist_unlock_read();
939
940 if (error)
941 goto out;
942
943
944 /* entry is not used, remove it */
945 LIST_REMOVE(it, el_list);
946 FREE(it, M_EXEC);
947
948 out:
949 lockmgr(&exec_lock, LK_RELEASE, NULL);
950 return error;
951 }
952
953 /*
954 * Add execsw[] entry.
955 */
956 int
957 exec_add(struct execsw *esp, const char *e_name)
958 {
959 struct exec_entry *it;
960 int error;
961
962 error = 0;
963 lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
964
965 if (!esp->es_emul) {
966 esp->es_emul = emul_search(e_name);
967 if (!esp->es_emul) {
968 error = ENOENT;
969 goto out;
970 }
971 }
972
973 LIST_FOREACH(it, &ex_head, ex_list) {
974 /* assume tuple (makecmds, probe_func, emulation) is unique */
975 if (it->es->es_check == esp->es_check
976 && it->es->u.elf_probe_func == esp->u.elf_probe_func
977 && it->es->es_emul == esp->es_emul) {
978 error = EEXIST;
979 goto out;
980 }
981 }
982
983 /* if we got here, the entry doesn't exist yet */
984 MALLOC(it, struct exec_entry *, sizeof(struct exec_entry),
985 M_EXEC, M_WAITOK);
986 it->es = esp;
987 LIST_INSERT_HEAD(&ex_head, it, ex_list);
988
989 /* update execsw[] */
990 exec_init(0);
991
992 out:
993 lockmgr(&exec_lock, LK_RELEASE, NULL);
994 return error;
995 }
996
997 /*
998 * Remove execsw[] entry.
999 */
1000 int
1001 exec_remove(const struct execsw *esp)
1002 {
1003 struct exec_entry *it;
1004 int error;
1005
1006 error = 0;
1007 lockmgr(&exec_lock, LK_EXCLUSIVE, NULL);
1008
1009 LIST_FOREACH(it, &ex_head, ex_list) {
1010 /* assume tuple (makecmds, probe_func, emulation) is unique */
1011 if (it->es->es_check == esp->es_check
1012 && it->es->u.elf_probe_func == esp->u.elf_probe_func
1013 && it->es->es_emul == esp->es_emul)
1014 break;
1015 }
1016 if (!it) {
1017 error = ENOENT;
1018 goto out;
1019 }
1020
1021 /* remove item from list and free resources */
1022 LIST_REMOVE(it, ex_list);
1023 FREE(it, M_EXEC);
1024
1025 /* update execsw[] */
1026 exec_init(0);
1027
1028 out:
1029 lockmgr(&exec_lock, LK_RELEASE, NULL);
1030 return error;
1031 }
1032
1033 static void
1034 link_es(struct execsw_entry **listp, const struct execsw *esp)
1035 {
1036 struct execsw_entry *et, *e1;
1037
1038 MALLOC(et, struct execsw_entry *, sizeof(struct execsw_entry),
1039 M_TEMP, M_WAITOK);
1040 et->next = NULL;
1041 et->es = esp;
1042 if (*listp == NULL) {
1043 *listp = et;
1044 return;
1045 }
1046
1047 switch(et->es->es_prio) {
1048 case EXECSW_PRIO_FIRST:
1049 /* put new entry as the first */
1050 et->next = *listp;
1051 *listp = et;
1052 break;
1053 case EXECSW_PRIO_ANY:
1054 /* put new entry after all *_FIRST and *_ANY entries */
1055 for(e1 = *listp; e1->next
1056 && e1->next->es->es_prio != EXECSW_PRIO_LAST;
1057 e1 = e1->next);
1058 et->next = e1->next;
1059 e1->next = et;
1060 break;
1061 case EXECSW_PRIO_LAST:
1062 /* put new entry as the last one */
1063 for(e1 = *listp; e1->next; e1 = e1->next);
1064 e1->next = et;
1065 break;
1066 default:
1067 #ifdef DIAGNOSTIC
1068 panic("execw[] entry with unknown priority %d found",
1069 et->es->es_prio);
1070 #endif
1071 break;
1072 }
1073 }
1074
1075 /*
1076 * Initialize exec structures. If init_boot is true, also does necessary
1077 * one-time initialization (it's called from main() that way).
1078 * Once system is multiuser, this should be called with exec_lock held,
1079 * i.e. via exec_{add|remove}().
1080 */
1081 int
1082 exec_init(int init_boot)
1083 {
1084 const struct execsw **new_es, * const *old_es;
1085 struct execsw_entry *list, *e1;
1086 struct exec_entry *e2;
1087 int i, es_sz;
1088
1089 if (init_boot) {
1090 /* do one-time initializations */
1091 lockinit(&exec_lock, PWAIT, "execlck", 0, 0);
1092
1093 /* register compiled-in emulations */
1094 for(i=0; i < nexecs_builtin; i++) {
1095 if (execsw_builtin[i].es_emul)
1096 emul_register(execsw_builtin[i].es_emul, 1);
1097 }
1098 #ifdef DIAGNOSTIC
1099 if (i == 0)
1100 panic("no emulations found in execsw_builtin[]");
1101 #endif
1102 }
1103
1104 /*
1105 * Build execsw[] array from builtin entries and entries added
1106 * at runtime.
1107 */
1108 list = NULL;
1109 for(i=0; i < nexecs_builtin; i++)
1110 link_es(&list, &execsw_builtin[i]);
1111
1112 /* Add dynamically loaded entries */
1113 es_sz = nexecs_builtin;
1114 LIST_FOREACH(e2, &ex_head, ex_list) {
1115 link_es(&list, e2->es);
1116 es_sz++;
1117 }
1118
1119 /*
1120 * Now that we have sorted all execw entries, create new execsw[]
1121 * and free no longer needed memory in the process.
1122 */
1123 new_es = malloc(es_sz * sizeof(struct execsw *), M_EXEC, M_WAITOK);
1124 for(i=0; list; i++) {
1125 new_es[i] = list->es;
1126 e1 = list->next;
1127 FREE(list, M_TEMP);
1128 list = e1;
1129 }
1130
1131 /*
1132 * New execsw[] array built, now replace old execsw[] and free
1133 * used memory.
1134 */
1135 old_es = execsw;
1136 execsw = new_es;
1137 nexecs = es_sz;
1138 if (old_es)
1139 free((void *)old_es, M_EXEC);
1140
1141 /*
1142 * Figure out the maximum size of an exec header.
1143 */
1144 exec_maxhdrsz = 0;
1145 for (i = 0; i < nexecs; i++) {
1146 if (execsw[i]->es_hdrsz > exec_maxhdrsz)
1147 exec_maxhdrsz = execsw[i]->es_hdrsz;
1148 }
1149
1150 return 0;
1151 }
1152 #endif
1153
1154 #ifndef LKM
1155 /*
1156 * Simplified exec_init() for kernels without LKMs. Only initialize
1157 * exec_maxhdrsz and execsw[].
1158 */
1159 int
1160 exec_init(int init_boot)
1161 {
1162 int i;
1163
1164 #ifdef DIAGNOSTIC
1165 if (!init_boot)
1166 panic("exec_init(): called with init_boot == 0");
1167 #endif
1168
1169 /* do one-time initializations */
1170 nexecs = nexecs_builtin;
1171 execsw = malloc(nexecs*sizeof(struct execsw *), M_EXEC, M_WAITOK);
1172
1173 /*
1174 * Fill in execsw[] and figure out the maximum size of an exec header.
1175 */
1176 exec_maxhdrsz = 0;
1177 for(i=0; i < nexecs; i++) {
1178 execsw[i] = &execsw_builtin[i];
1179 if (execsw_builtin[i].es_hdrsz > exec_maxhdrsz)
1180 exec_maxhdrsz = execsw_builtin[i].es_hdrsz;
1181 }
1182
1183 return 0;
1184
1185 }
1186 #endif /* !LKM */
1187