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