Merge FreeBSD rev. 1.8:
[dragonfly.git] / sys / kern / kern_exec.c
1 /*
2  * Copyright (c) 1993, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/kern/kern_exec.c,v 1.107.2.15 2002/07/30 15:40:46 nectar Exp $
27  * $DragonFly: src/sys/kern/kern_exec.c,v 1.19 2004/01/20 18:41:51 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysproto.h>
33 #include <sys/kernel.h>
34 #include <sys/mount.h>
35 #include <sys/filedesc.h>
36 #include <sys/fcntl.h>
37 #include <sys/acct.h>
38 #include <sys/exec.h>
39 #include <sys/imgact.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/kern_syscall.h>
42 #include <sys/wait.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/signalvar.h>
46 #include <sys/pioctl.h>
47 #include <sys/namei.h>
48 #include <sys/sysent.h>
49 #include <sys/shm.h>
50 #include <sys/sysctl.h>
51 #include <sys/vnode.h>
52 #include <sys/aio.h>
53
54 #include <vm/vm.h>
55 #include <vm/vm_param.h>
56 #include <sys/lock.h>
57 #include <vm/pmap.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_extern.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_pager.h>
64
65 #include <sys/user.h>
66 #include <machine/reg.h>
67
68 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
69
70 static register_t *exec_copyout_strings (struct image_params *);
71
72 /* XXX This should be vm_size_t. */
73 static u_long ps_strings = PS_STRINGS;
74 SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
75
76 /* XXX This should be vm_size_t. */
77 static u_long usrstack = USRSTACK;
78 SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
79
80 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
81 SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 
82     &ps_arg_cache_limit, 0, "");
83
84 int ps_argsopen = 1;
85 SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
86
87 void print_execve_args(struct image_args *args);
88 int debug_execve_args = 0;
89 SYSCTL_INT(_kern, OID_AUTO, debug_execve_args, CTLFLAG_RW, &debug_execve_args,
90     0, "");
91
92 void
93 print_execve_args(struct image_args *args)
94 {
95         char *cp;
96         int ndx;
97
98         cp = args->begin_argv;
99         for (ndx = 0; ndx < args->argc; ndx++) {
100                 printf("\targv[%d]: %s\n", ndx, cp);
101                 while (*cp++ != '\0');
102         }
103         for (ndx = 0; ndx < args->envc; ndx++) {
104                 printf("\tenvv[%d]: %s\n", ndx, cp);
105                 while (*cp++ != '\0');
106         }
107 }
108
109 /*
110  * Each of the items is a pointer to a `const struct execsw', hence the
111  * double pointer here.
112  */
113 static const struct execsw **execsw;
114
115 int
116 kern_execve(struct nameidata *ndp, struct image_args *args)
117 {
118         struct thread *td = curthread;
119         struct proc *p = td->td_proc;
120         register_t *stack_base;
121         int error, len, i;
122         struct image_params image_params, *imgp;
123         struct vattr attr;
124         int (*img_first) (struct image_params *);
125
126         if (debug_execve_args) {
127                 printf("%s()\n", __func__);
128                 print_execve_args(args);
129         }
130
131         KKASSERT(p);
132         imgp = &image_params;
133
134         /*
135          * Lock the process and set the P_INEXEC flag to indicate that
136          * it should be left alone until we're done here.  This is
137          * necessary to avoid race conditions - e.g. in ptrace() -
138          * that might allow a local user to illicitly obtain elevated
139          * privileges.
140          */
141         p->p_flag |= P_INEXEC;
142
143         /*
144          * Initialize part of the common data
145          */
146         imgp->proc = p;
147         imgp->args = args;
148         imgp->attr = &attr;
149         imgp->entry_addr = 0;
150         imgp->resident = 0;
151         imgp->vmspace_destroyed = 0;
152         imgp->interpreted = 0;
153         imgp->interpreter_name[0] = 0;
154         imgp->auxargs = NULL;
155         imgp->vp = NULL;
156         imgp->firstpage = NULL;
157         imgp->ps_strings = 0;
158
159         /*
160          * Allocate temporary demand zeroed space for argument and
161          *      environment strings
162          */
163         imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE);
164         if (imgp->image_header == NULL) {
165                 error = ENOMEM;
166                 goto exec_fail;
167         }
168
169 interpret:
170
171         /*
172          * Translate the file name. namei() returns a vnode pointer
173          *      in ni_vp amoung other things.
174          */
175         error = namei(ndp);
176         if (error) {
177                 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
178                     PAGE_SIZE);
179                 goto exec_fail;
180         }
181
182         imgp->vp = ndp->ni_vp;
183
184         /*
185          * Check file permissions (also 'opens' file)
186          */
187         error = exec_check_permissions(imgp);
188         if (error) {
189                 VOP_UNLOCK(imgp->vp, 0, td);
190                 goto exec_fail_dealloc;
191         }
192
193         error = exec_map_first_page(imgp);
194         VOP_UNLOCK(imgp->vp, 0, td);
195         if (error)
196                 goto exec_fail_dealloc;
197
198         if (debug_execve_args && imgp->interpreted) {
199                 printf("    target is interpreted -- recursive pass\n");
200                 printf("    interpreter: %s\n", imgp->interpreter_name);
201                 print_execve_args(args);
202         }
203
204         /*
205          *      If the current process has a special image activator it
206          *      wants to try first, call it.   For example, emulating shell 
207          *      scripts differently.
208          */
209         error = -1;
210         if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
211                 error = img_first(imgp);
212
213         /*
214          *      If the vnode has a registered vmspace, exec the vmspace
215          */
216         if (error == -1 && imgp->vp->v_resident) {
217                 error = exec_resident_imgact(imgp);
218         }
219
220         /*
221          *      Loop through the list of image activators, calling each one.
222          *      An activator returns -1 if there is no match, 0 on success,
223          *      and an error otherwise.
224          */
225         for (i = 0; error == -1 && execsw[i]; ++i) {
226                 if (execsw[i]->ex_imgact == NULL ||
227                     execsw[i]->ex_imgact == img_first) {
228                         continue;
229                 }
230                 error = (*execsw[i]->ex_imgact)(imgp);
231         }
232
233         if (error) {
234                 if (error == -1)
235                         error = ENOEXEC;
236                 goto exec_fail_dealloc;
237         }
238
239         /*
240          * Special interpreter operation, cleanup and loop up to try to
241          * activate the interpreter.
242          */
243         if (imgp->interpreted) {
244                 exec_unmap_first_page(imgp);
245                 /* free name buffer and old vnode */
246                 NDFREE(ndp, NDF_ONLY_PNBUF);
247                 vrele(ndp->ni_vp);
248                 /* set new name to that of the interpreter */
249                 NDINIT(ndp, NAMEI_LOOKUP, 
250                     CNP_LOCKLEAF | CNP_FOLLOW | CNP_SAVENAME,
251                     UIO_SYSSPACE, imgp->interpreter_name, td);
252                 goto interpret;
253         }
254
255         /*
256          * Copy out strings (args and env) and initialize stack base
257          */
258         stack_base = exec_copyout_strings(imgp);
259         p->p_vmspace->vm_minsaddr = (char *)stack_base;
260
261         /*
262          * If custom stack fixup routine present for this process
263          * let it do the stack setup.  If we are running a resident
264          * image there is no auxinfo or other image activator context
265          * so don't try to add fixups to the stack.
266          *
267          * Else stuff argument count as first item on stack
268          */
269         if (p->p_sysent->sv_fixup && imgp->resident == 0)
270                 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
271         else
272                 suword(--stack_base, imgp->args->argc);
273
274         /*
275          * For security and other reasons, the file descriptor table cannot
276          * be shared after an exec.
277          */
278         if (p->p_fd->fd_refcnt > 1) {
279                 struct filedesc *tmp;
280
281                 tmp = fdcopy(p);
282                 fdfree(p);
283                 p->p_fd = tmp;
284         }
285
286         /*
287          * For security and other reasons, signal handlers cannot
288          * be shared after an exec. The new proces gets a copy of the old
289          * handlers. In execsigs(), the new process will have its signals
290          * reset.
291          */
292         if (p->p_procsig->ps_refcnt > 1) {
293                 struct procsig *newprocsig;
294
295                 MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
296                        M_SUBPROC, M_WAITOK);
297                 bcopy(p->p_procsig, newprocsig, sizeof(*newprocsig));
298                 p->p_procsig->ps_refcnt--;
299                 p->p_procsig = newprocsig;
300                 p->p_procsig->ps_refcnt = 1;
301                 if (p->p_sigacts == &p->p_addr->u_sigacts)
302                         panic("shared procsig but private sigacts?");
303
304                 p->p_addr->u_sigacts = *p->p_sigacts;
305                 p->p_sigacts = &p->p_addr->u_sigacts;
306         }
307
308         /* Stop profiling */
309         stopprofclock(p);
310
311         /* close files on exec */
312         fdcloseexec(p);
313
314         /* reset caught signals */
315         execsigs(p);
316
317         /* name this process - nameiexec(p, ndp) */
318         len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
319         bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
320         p->p_comm[len] = 0;
321
322         /*
323          * mark as execed, wakeup the process that vforked (if any) and tell
324          * it that it now has its own resources back
325          */
326         p->p_flag |= P_EXEC;
327         if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
328                 p->p_flag &= ~P_PPWAIT;
329                 wakeup((caddr_t)p->p_pptr);
330         }
331
332         /*
333          * Implement image setuid/setgid.
334          *
335          * Don't honor setuid/setgid if the filesystem prohibits it or if
336          * the process is being traced.
337          */
338         if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
339              ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
340             (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
341             (p->p_flag & P_TRACED) == 0) {
342                 /*
343                  * Turn off syscall tracing for set-id programs, except for
344                  * root.  Record any set-id flags first to make sure that
345                  * we do not regain any tracing during a possible block.
346                  */
347                 setsugid();
348                 if (p->p_tracep && suser(td)) {
349                         struct vnode *vtmp;
350
351                         if ((vtmp = p->p_tracep) != NULL) {
352                                 p->p_tracep = NULL;
353                                 p->p_traceflag = 0;
354                                 vrele(vtmp);
355                         }
356                 }
357                 /* Close any file descriptors 0..2 that reference procfs */
358                 setugidsafety(p);
359                 /* Make sure file descriptors 0..2 are in use. */
360                 error = fdcheckstd(p);
361                 if (error != 0)
362                         goto exec_fail_dealloc;
363                 /*
364                  * Set the new credentials.
365                  */
366                 cratom(&p->p_ucred);
367                 if (attr.va_mode & VSUID)
368                         change_euid(attr.va_uid);
369                 if (attr.va_mode & VSGID)
370                         p->p_ucred->cr_gid = attr.va_gid;
371
372                 /*
373                  * Clear local varsym variables
374                  */
375                 varsymset_clean(&p->p_varsymset);
376         } else {
377                 if (p->p_ucred->cr_uid == p->p_ucred->cr_ruid &&
378                     p->p_ucred->cr_gid == p->p_ucred->cr_rgid)
379                         p->p_flag &= ~P_SUGID;
380         }
381
382         /*
383          * Implement correct POSIX saved-id behavior.
384          */
385         if (p->p_ucred->cr_svuid != p->p_ucred->cr_uid ||
386             p->p_ucred->cr_svgid != p->p_ucred->cr_gid) {
387                 cratom(&p->p_ucred);
388                 p->p_ucred->cr_svuid = p->p_ucred->cr_uid;
389                 p->p_ucred->cr_svgid = p->p_ucred->cr_gid;
390         }
391
392         /*
393          * Store the vp for use in procfs
394          */
395         if (p->p_textvp)                /* release old reference */
396                 vrele(p->p_textvp);
397         VREF(ndp->ni_vp);
398         p->p_textvp = ndp->ni_vp;
399
400         /*
401          * Notify others that we exec'd, and clear the P_INEXEC flag
402          * as we're now a bona fide freshly-execed process.
403          */
404         KNOTE(&p->p_klist, NOTE_EXEC);
405         p->p_flag &= ~P_INEXEC;
406
407         /*
408          * If tracing the process, trap to debugger so breakpoints
409          *      can be set before the program executes.
410          */
411         STOPEVENT(p, S_EXEC, 0);
412
413         if (p->p_flag & P_TRACED)
414                 psignal(p, SIGTRAP);
415
416         /* clear "fork but no exec" flag, as we _are_ execing */
417         p->p_acflag &= ~AFORK;
418
419         /* Set values passed into the program in registers. */
420         setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
421             imgp->ps_strings);
422
423         /* Free any previous argument cache */
424         if (p->p_args && --p->p_args->ar_ref == 0)
425                 FREE(p->p_args, M_PARGS);
426         p->p_args = NULL;
427
428         /* Cache arguments if they fit inside our allowance */
429         i = imgp->args->begin_envv - imgp->args->begin_argv;
430         if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
431                 MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, 
432                     M_PARGS, M_WAITOK);
433                 p->p_args->ar_ref = 1;
434                 p->p_args->ar_length = i;
435                 bcopy(imgp->args->begin_argv, p->p_args->ar_args, i);
436         }
437
438 exec_fail_dealloc:
439
440         /*
441          * free various allocated resources
442          */
443         if (imgp->firstpage)
444                 exec_unmap_first_page(imgp);
445
446         if (imgp->image_header != NULL)
447                 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
448                     PAGE_SIZE);
449
450         if (imgp->vp) {
451                 NDFREE(ndp, NDF_ONLY_PNBUF);
452                 vrele(imgp->vp);
453         }
454
455         if (error == 0)
456                 return (0);
457
458 exec_fail:
459         /* we're done here, clear P_INEXEC */
460         p->p_flag &= ~P_INEXEC;
461         if (imgp->vmspace_destroyed) {
462                 /* sorry, no more process anymore. exit gracefully */
463                 exit1(W_EXITCODE(0, SIGABRT));
464                 /* NOT REACHED */
465                 return(0);
466         } else {
467                 return(error);
468         }
469 }
470
471 /*
472  * execve() system call.
473  */
474 int
475 execve(struct execve_args *uap)
476 {
477         struct thread *td = curthread;
478         struct nameidata nd;
479         struct image_args args;
480         int error;
481
482         NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_FOLLOW | CNP_SAVENAME,
483             UIO_USERSPACE, uap->fname, td);
484
485         error = exec_copyin_args(&args, uap->fname, PATH_USERSPACE,
486                                 uap->argv, uap->envv);
487         if (error == 0)
488                 error = kern_execve(&nd, &args);
489
490         exec_free_args(&args);
491
492         /*
493          * The syscall result is returned in registers to the new program.
494          * Linux will register %edx as an atexit function and we must be
495          * sure to set it to 0.  XXX
496          */
497         if (error == 0)
498                 uap->sysmsg_result64 = 0;
499
500         return (error);
501 }
502
503 int
504 exec_map_first_page(struct image_params *imgp)
505 {
506         int s, rv, i;
507         int initial_pagein;
508         vm_page_t ma[VM_INITIAL_PAGEIN];
509         vm_object_t object;
510
511
512         if (imgp->firstpage) {
513                 exec_unmap_first_page(imgp);
514         }
515
516         VOP_GETVOBJECT(imgp->vp, &object);
517         s = splvm();
518
519         ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
520
521         if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
522                 initial_pagein = VM_INITIAL_PAGEIN;
523                 if (initial_pagein > object->size)
524                         initial_pagein = object->size;
525                 for (i = 1; i < initial_pagein; i++) {
526                         if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
527                                 if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
528                                         break;
529                                 if (ma[i]->valid)
530                                         break;
531                                 vm_page_busy(ma[i]);
532                         } else {
533                                 ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
534                                 if (ma[i] == NULL)
535                                         break;
536                         }
537                 }
538                 initial_pagein = i;
539
540                 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
541                 ma[0] = vm_page_lookup(object, 0);
542
543                 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
544                         if (ma[0]) {
545                                 vm_page_protect(ma[0], VM_PROT_NONE);
546                                 vm_page_free(ma[0]);
547                         }
548                         splx(s);
549                         return EIO;
550                 }
551         }
552
553         vm_page_wire(ma[0]);
554         vm_page_wakeup(ma[0]);
555         splx(s);
556
557         pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
558         imgp->firstpage = ma[0];
559
560         return 0;
561 }
562
563 void
564 exec_unmap_first_page(imgp)
565         struct image_params *imgp;
566 {
567         if (imgp->firstpage) {
568                 pmap_kremove((vm_offset_t) imgp->image_header);
569                 vm_page_unwire(imgp->firstpage, 1);
570                 imgp->firstpage = NULL;
571         }
572 }
573
574 /*
575  * Destroy old address space, and allocate a new stack
576  *      The new stack is only SGROWSIZ large because it is grown
577  *      automatically in trap.c.
578  */
579 int
580 exec_new_vmspace(struct image_params *imgp, struct vmspace *vmcopy)
581 {
582         int error;
583         struct vmspace *vmspace = imgp->proc->p_vmspace;
584         vm_offset_t stack_addr = USRSTACK - maxssiz;
585         vm_map_t map;
586
587         imgp->vmspace_destroyed = 1;
588
589         /*
590          * Prevent a pending AIO from modifying the new address space.
591          */
592         aio_proc_rundown(imgp->proc);
593
594         /*
595          * Blow away entire process VM, if address space not shared,
596          * otherwise, create a new VM space so that other threads are
597          * not disrupted.  If we are execing a resident vmspace we
598          * create a duplicate of it and remap the stack.
599          */
600         map = &vmspace->vm_map;
601         if (vmcopy) {
602                 vmspace_exec(imgp->proc, vmcopy);
603                 vmspace = imgp->proc->p_vmspace;
604                 pmap_remove_pages(vmspace_pmap(vmspace), stack_addr, USRSTACK);
605                 map = &vmspace->vm_map;
606         } else if (vmspace->vm_refcnt == 1) {
607                 shmexit(vmspace);
608                 pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
609                 vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
610         } else {
611                 vmspace_exec(imgp->proc, NULL);
612                 vmspace = imgp->proc->p_vmspace;
613                 map = &vmspace->vm_map;
614         }
615
616         /* Allocate a new stack */
617         error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
618             VM_PROT_ALL, VM_PROT_ALL, 0);
619         if (error)
620                 return (error);
621
622         /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
623          * VM_STACK case, but they are still used to monitor the size of the
624          * process stack so we can check the stack rlimit.
625          */
626         vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
627         vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
628
629         return(0);
630 }
631
632 /*
633  * Copy out argument and environment strings from the old process
634  *      address space into the temporary string buffer.
635  */
636 int
637 exec_copyin_args(struct image_args *args, char *fname,
638                 enum exec_path_segflg segflg, char **argv, char **envv)
639 {
640         char    *argp, *envp;
641         int     error = 0;
642         size_t  length;
643
644         bzero(args, sizeof(*args));
645         args->buf = (char *) kmem_alloc_wait(exec_map, PATH_MAX + ARG_MAX);
646         if (args->buf == NULL)
647                 return (ENOMEM);
648         args->begin_argv = args->buf;
649         args->endp = args->begin_argv;
650         args->space = ARG_MAX;
651
652         args->fname = args->buf + ARG_MAX;
653
654         /*
655          * Copy the file name.
656          */
657         if (segflg == PATH_SYSSPACE) {
658                 error = copystr(fname, args->fname, PATH_MAX, &length);
659         } else if (segflg == PATH_USERSPACE) {
660                 error = copyinstr(fname, args->fname, PATH_MAX, &length);
661         }
662
663         /*
664          * extract argument strings
665          */
666
667         if (argv && error == 0) {
668                 /*
669                  * The argv0 argument for execv() is allowed to be NULL,
670                  * in which case we use our filename as argv[0].
671                  * This guarantees that
672                  * the interpreter knows what file to open in the case
673                  * that we exec an interpreted file.
674                  */
675                 argp = (caddr_t) (intptr_t) fuword(argv);
676                 if (argp == NULL) {
677                         length = strlen(args->fname) + 1;
678                         KKASSERT(length <= args->space);
679                         bcopy(args->fname, args->endp, length);
680                         args->space -= length;
681                         args->endp += length;
682                         args->argc++;
683                         argv++;
684                 }
685                 while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
686                         if (argp == (caddr_t) -1) {
687                                 error = EFAULT;
688                                 goto cleanup;
689                         }
690                         error = copyinstr(argp, args->endp,
691                                             args->space, &length);
692                         if (error == ENAMETOOLONG)
693                                 error = E2BIG;
694                         if (error)
695                                 goto cleanup;
696                         args->space -= length;
697                         args->endp += length;
698                         args->argc++;
699                 }
700         }       
701
702         args->begin_envv = args->endp;
703
704         /*
705          * extract environment strings
706          */
707         if (envv && error == 0) {
708                 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
709                         if (envp == (caddr_t) -1) {
710                                 error = EFAULT;
711                                 goto cleanup;
712                         }
713                         error = copyinstr(envp, args->endp, args->space,
714                             &length);
715                         if (error == ENAMETOOLONG)
716                                 error = E2BIG;
717                         if (error)
718                                 goto cleanup;
719                         args->space -= length;
720                         args->endp += length;
721                         args->envc++;
722                 }
723         }
724
725 cleanup:
726         return (error);
727 }
728
729 void
730 exec_free_args(struct image_args *args)
731 {
732         if (args->buf) {
733                 kmem_free_wakeup(exec_map,
734                                 (vm_offset_t)args->buf, PATH_MAX + ARG_MAX);
735                 args->buf = NULL;
736         }
737 }
738
739 /*
740  * Copy strings out to the new process address space, constructing
741  *      new arg and env vector tables. Return a pointer to the base
742  *      so that it can be used as the initial stack pointer.
743  */
744 register_t *
745 exec_copyout_strings(struct image_params *imgp)
746 {
747         int argc, envc;
748         char **vectp;
749         char *stringp, *destp;
750         register_t *stack_base;
751         struct ps_strings *arginfo;
752         int szsigcode;
753
754         /*
755          * Calculate string base and vector table pointers.
756          * Also deal with signal trampoline code for this exec type.
757          */
758         arginfo = (struct ps_strings *)PS_STRINGS;
759         szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
760         destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
761             roundup((ARG_MAX - imgp->args->space), sizeof(char *));
762
763         /*
764          * install sigcode
765          */
766         if (szsigcode)
767                 copyout(imgp->proc->p_sysent->sv_sigcode,
768                     ((caddr_t)arginfo - szsigcode), szsigcode);
769
770         /*
771          * If we have a valid auxargs ptr, prepare some room
772          * on the stack.
773          *
774          * The '+ 2' is for the null pointers at the end of each of the
775          * arg and env vector sets, and 'AT_COUNT*2' is room for the
776          * ELF Auxargs data.
777          */
778         if (imgp->auxargs) {
779                 vectp = (char **)(destp - (imgp->args->argc +
780                         imgp->args->envc + 2 + AT_COUNT * 2) * sizeof(char*));
781         } else {
782                 vectp = (char **)(destp - (imgp->args->argc +
783                         imgp->args->envc + 2) * sizeof(char*));
784         }
785
786         /*
787          * NOTE: don't bother aligning the stack here for GCC 2.x, it will
788          * be done in crt1.o.  Note that GCC 3.x aligns the stack in main.
789          */
790
791         /*
792          * vectp also becomes our initial stack base
793          */
794         stack_base = (register_t *)vectp;
795
796         stringp = imgp->args->begin_argv;
797         argc = imgp->args->argc;
798         envc = imgp->args->envc;
799
800         /*
801          * Copy out strings - arguments and environment.
802          */
803         copyout(stringp, destp, ARG_MAX - imgp->args->space);
804
805         /*
806          * Fill in "ps_strings" struct for ps, w, etc.
807          */
808         suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
809         suword(&arginfo->ps_nargvstr, argc);
810
811         /*
812          * Fill in argument portion of vector table.
813          */
814         for (; argc > 0; --argc) {
815                 suword(vectp++, (long)(intptr_t)destp);
816                 while (*stringp++ != 0)
817                         destp++;
818                 destp++;
819         }
820
821         /* a null vector table pointer separates the argp's from the envp's */
822         suword(vectp++, 0);
823
824         suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
825         suword(&arginfo->ps_nenvstr, envc);
826
827         /*
828          * Fill in environment portion of vector table.
829          */
830         for (; envc > 0; --envc) {
831                 suword(vectp++, (long)(intptr_t)destp);
832                 while (*stringp++ != 0)
833                         destp++;
834                 destp++;
835         }
836
837         /* end of vector table is a null pointer */
838         suword(vectp, 0);
839
840         return (stack_base);
841 }
842
843 /*
844  * Check permissions of file to execute.
845  *      Return 0 for success or error code on failure.
846  */
847 int
848 exec_check_permissions(imgp)
849         struct image_params *imgp;
850 {
851         struct proc *p = imgp->proc;
852         struct vnode *vp = imgp->vp;
853         struct vattr *attr = imgp->attr;
854         struct thread *td = p->p_thread;
855         int error;
856
857         /* Get file attributes */
858         error = VOP_GETATTR(vp, attr, td);
859         if (error)
860                 return (error);
861
862         /*
863          * 1) Check if file execution is disabled for the filesystem that this
864          *      file resides on.
865          * 2) Insure that at least one execute bit is on - otherwise root
866          *      will always succeed, and we don't want to happen unless the
867          *      file really is executable.
868          * 3) Insure that the file is a regular file.
869          */
870         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
871             ((attr->va_mode & 0111) == 0) ||
872             (attr->va_type != VREG)) {
873                 return (EACCES);
874         }
875
876         /*
877          * Zero length files can't be exec'd
878          */
879         if (attr->va_size == 0)
880                 return (ENOEXEC);
881
882         /*
883          *  Check for execute permission to file based on current credentials.
884          */
885         error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
886         if (error)
887                 return (error);
888
889         /*
890          * Check number of open-for-writes on the file and deny execution
891          * if there are any.
892          */
893         if (vp->v_writecount)
894                 return (ETXTBSY);
895
896         /*
897          * Call filesystem specific open routine (which does nothing in the
898          * general case).
899          */
900         error = VOP_OPEN(vp, FREAD, p->p_ucred, td);
901         if (error)
902                 return (error);
903
904         return (0);
905 }
906
907 /*
908  * Exec handler registration
909  */
910 int
911 exec_register(execsw_arg)
912         const struct execsw *execsw_arg;
913 {
914         const struct execsw **es, **xs, **newexecsw;
915         int count = 2;  /* New slot and trailing NULL */
916
917         if (execsw)
918                 for (es = execsw; *es; es++)
919                         count++;
920         newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
921         if (newexecsw == NULL)
922                 return ENOMEM;
923         xs = newexecsw;
924         if (execsw)
925                 for (es = execsw; *es; es++)
926                         *xs++ = *es;
927         *xs++ = execsw_arg;
928         *xs = NULL;
929         if (execsw)
930                 free(execsw, M_TEMP);
931         execsw = newexecsw;
932         return 0;
933 }
934
935 int
936 exec_unregister(execsw_arg)
937         const struct execsw *execsw_arg;
938 {
939         const struct execsw **es, **xs, **newexecsw;
940         int count = 1;
941
942         if (execsw == NULL)
943                 panic("unregister with no handlers left?\n");
944
945         for (es = execsw; *es; es++) {
946                 if (*es == execsw_arg)
947                         break;
948         }
949         if (*es == NULL)
950                 return ENOENT;
951         for (es = execsw; *es; es++)
952                 if (*es != execsw_arg)
953                         count++;
954         newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
955         if (newexecsw == NULL)
956                 return ENOMEM;
957         xs = newexecsw;
958         for (es = execsw; *es; es++)
959                 if (*es != execsw_arg)
960                         *xs++ = *es;
961         *xs = NULL;
962         if (execsw)
963                 free(execsw, M_TEMP);
964         execsw = newexecsw;
965         return 0;
966 }