Fix exec from setuid/setgid binaries
[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.64 2008/10/26 04:29:19 sephe 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/priv.h>
46 #include <sys/ktrace.h>
47 #include <sys/signalvar.h>
48 #include <sys/pioctl.h>
49 #include <sys/nlookup.h>
50 #include <sys/sfbuf.h>
51 #include <sys/sysent.h>
52 #include <sys/shm.h>
53 #include <sys/sysctl.h>
54 #include <sys/vnode.h>
55 #include <sys/vmmeter.h>
56 #include <sys/aio.h>
57 #include <sys/libkern.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 #include <sys/lock.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_map.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_object.h>
68 #include <vm/vnode_pager.h>
69 #include <vm/vm_pager.h>
70
71 #include <sys/user.h>
72 #include <sys/reg.h>
73
74 #include <sys/thread2.h>
75
76 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
77 MALLOC_DEFINE(M_EXECARGS, "exec-args", "Exec arguments");
78
79 static register_t *exec_copyout_strings (struct image_params *);
80
81 /* XXX This should be vm_size_t. */
82 static u_long ps_strings = PS_STRINGS;
83 SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
84
85 /* XXX This should be vm_size_t. */
86 static u_long usrstack = USRSTACK;
87 SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
88
89 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
90 SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 
91     &ps_arg_cache_limit, 0, "");
92
93 int ps_argsopen = 1;
94 SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
95
96 void print_execve_args(struct image_args *args);
97 int debug_execve_args = 0;
98 SYSCTL_INT(_kern, OID_AUTO, debug_execve_args, CTLFLAG_RW, &debug_execve_args,
99     0, "");
100
101 /*
102  * Exec arguments object cache
103  */
104 static struct objcache *exec_objcache;
105
106 static
107 void
108 exec_objcache_init(void *arg __unused)
109 {
110         int cluster_limit;
111
112         cluster_limit = 16;     /* up to this many objects */
113         exec_objcache = objcache_create_mbacked(
114                                         M_EXECARGS, PATH_MAX + ARG_MAX,
115                                         &cluster_limit,
116                                         2,      /* minimal magazine capacity */
117                                         NULL, NULL, NULL);
118 }
119 SYSINIT(exec_objcache, SI_BOOT2_MACHDEP, SI_ORDER_ANY, exec_objcache_init, 0);
120
121 /*
122  * stackgap_random specifies if the stackgap should have a random size added
123  * to it.  It must be a power of 2.  If non-zero, the stack gap will be 
124  * calculated as: ALIGN(karc4random() & (stackgap_random - 1)).
125  */
126 static int stackgap_random = 1024;
127 static int
128 sysctl_kern_stackgap(SYSCTL_HANDLER_ARGS)
129 {
130         int error, new_val;
131         new_val = stackgap_random;
132         error = sysctl_handle_int(oidp, &new_val, 0, req);
133         if (error != 0 || req->newptr == NULL)
134                 return (error);
135         if ((new_val < 0) || (new_val > 16 * PAGE_SIZE) || ! powerof2(new_val))
136                 return (EINVAL);
137         stackgap_random = new_val;
138
139         return(0);
140 }
141
142 SYSCTL_PROC(_kern, OID_AUTO, stackgap_random, CTLFLAG_RW|CTLTYPE_UINT, 
143         0, 0, sysctl_kern_stackgap, "IU", "Max random stack gap (power of 2)");
144         
145 void
146 print_execve_args(struct image_args *args)
147 {
148         char *cp;
149         int ndx;
150
151         cp = args->begin_argv;
152         for (ndx = 0; ndx < args->argc; ndx++) {
153                 kprintf("\targv[%d]: %s\n", ndx, cp);
154                 while (*cp++ != '\0');
155         }
156         for (ndx = 0; ndx < args->envc; ndx++) {
157                 kprintf("\tenvv[%d]: %s\n", ndx, cp);
158                 while (*cp++ != '\0');
159         }
160 }
161
162 /*
163  * Each of the items is a pointer to a `const struct execsw', hence the
164  * double pointer here.
165  */
166 static const struct execsw **execsw;
167
168 /*
169  * Replace current vmspace with a new binary.
170  * Returns 0 on success, > 0 on recoverable error (use as errno).
171  * Returns -1 on lethal error which demands killing of the current
172  * process!
173  */
174 int
175 kern_execve(struct nlookupdata *nd, struct image_args *args)
176 {
177         struct thread *td = curthread;
178         struct lwp *lp = td->td_lwp;
179         struct proc *p = td->td_proc;
180         register_t *stack_base;
181         int error, len, i;
182         struct image_params image_params, *imgp;
183         struct vattr attr;
184         int (*img_first) (struct image_params *);
185
186         if (debug_execve_args) {
187                 kprintf("%s()\n", __func__);
188                 print_execve_args(args);
189         }
190
191         KKASSERT(p);
192         imgp = &image_params;
193
194         /*
195          * NOTE: P_INEXEC is handled by exec_new_vmspace() now.  We make
196          * no modifications to the process at all until we get there.
197          *
198          * Note that multiple threads may be trying to exec at the same
199          * time.  exec_new_vmspace() handles that too.
200          */
201
202         /*
203          * Initialize part of the common data
204          */
205         imgp->proc = p;
206         imgp->args = args;
207         imgp->attr = &attr;
208         imgp->entry_addr = 0;
209         imgp->resident = 0;
210         imgp->vmspace_destroyed = 0;
211         imgp->interpreted = 0;
212         imgp->interpreter_name[0] = 0;
213         imgp->auxargs = NULL;
214         imgp->vp = NULL;
215         imgp->firstpage = NULL;
216         imgp->ps_strings = 0;
217         imgp->image_header = NULL;
218
219 interpret:
220
221         /*
222          * Translate the file name to a vnode.  Unlock the cache entry to
223          * improve parallelism for programs exec'd in parallel.
224          */
225         if ((error = nlookup(nd)) != 0)
226                 goto exec_fail;
227         error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp);
228         KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
229         nd->nl_flags &= ~NLC_NCPISLOCKED;
230         cache_unlock(&nd->nl_nch);
231         if (error)
232                 goto exec_fail;
233
234         /*
235          * Check file permissions (also 'opens' file)
236          */
237         error = exec_check_permissions(imgp);
238         if (error) {
239                 vn_unlock(imgp->vp);
240                 goto exec_fail_dealloc;
241         }
242
243         error = exec_map_first_page(imgp);
244         vn_unlock(imgp->vp);
245         if (error)
246                 goto exec_fail_dealloc;
247
248         if (debug_execve_args && imgp->interpreted) {
249                 kprintf("    target is interpreted -- recursive pass\n");
250                 kprintf("    interpreter: %s\n", imgp->interpreter_name);
251                 print_execve_args(args);
252         }
253
254         /*
255          *      If the current process has a special image activator it
256          *      wants to try first, call it.   For example, emulating shell 
257          *      scripts differently.
258          */
259         error = -1;
260         if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
261                 error = img_first(imgp);
262
263         /*
264          *      If the vnode has a registered vmspace, exec the vmspace
265          */
266         if (error == -1 && imgp->vp->v_resident) {
267                 error = exec_resident_imgact(imgp);
268         }
269
270         /*
271          *      Loop through the list of image activators, calling each one.
272          *      An activator returns -1 if there is no match, 0 on success,
273          *      and an error otherwise.
274          */
275         for (i = 0; error == -1 && execsw[i]; ++i) {
276                 if (execsw[i]->ex_imgact == NULL ||
277                     execsw[i]->ex_imgact == img_first) {
278                         continue;
279                 }
280                 error = (*execsw[i]->ex_imgact)(imgp);
281         }
282
283         if (error) {
284                 if (error == -1)
285                         error = ENOEXEC;
286                 goto exec_fail_dealloc;
287         }
288
289         /*
290          * Special interpreter operation, cleanup and loop up to try to
291          * activate the interpreter.
292          */
293         if (imgp->interpreted) {
294                 exec_unmap_first_page(imgp);
295                 nlookup_done(nd);
296                 vrele(imgp->vp);
297                 imgp->vp = NULL;
298                 error = nlookup_init(nd, imgp->interpreter_name, UIO_SYSSPACE,
299                                         NLC_FOLLOW);
300                 if (error)
301                         goto exec_fail;
302                 goto interpret;
303         }
304
305         /*
306          * Copy out strings (args and env) and initialize stack base
307          */
308         stack_base = exec_copyout_strings(imgp);
309         p->p_vmspace->vm_minsaddr = (char *)stack_base;
310
311         /*
312          * If custom stack fixup routine present for this process
313          * let it do the stack setup.  If we are running a resident
314          * image there is no auxinfo or other image activator context
315          * so don't try to add fixups to the stack.
316          *
317          * Else stuff argument count as first item on stack
318          */
319         if (p->p_sysent->sv_fixup && imgp->resident == 0)
320                 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
321         else
322                 suword(--stack_base, imgp->args->argc);
323
324         /*
325          * For security and other reasons, the file descriptor table cannot
326          * be shared after an exec.
327          */
328         if (p->p_fd->fd_refcnt > 1) {
329                 struct filedesc *tmp;
330
331                 tmp = fdcopy(p);
332                 fdfree(p, tmp);
333         }
334
335         /*
336          * For security and other reasons, signal handlers cannot
337          * be shared after an exec. The new proces gets a copy of the old
338          * handlers. In execsigs(), the new process will have its signals
339          * reset.
340          */
341         if (p->p_sigacts->ps_refcnt > 1) {
342                 struct sigacts *newsigacts;
343
344                 newsigacts = (struct sigacts *)kmalloc(sizeof(*newsigacts),
345                        M_SUBPROC, M_WAITOK);
346                 bcopy(p->p_sigacts, newsigacts, sizeof(*newsigacts));
347                 p->p_sigacts->ps_refcnt--;
348                 p->p_sigacts = newsigacts;
349                 p->p_sigacts->ps_refcnt = 1;
350         }
351
352         /*
353          * For security and other reasons virtual kernels cannot be
354          * inherited by an exec.  This also allows a virtual kernel
355          * to fork/exec unrelated applications.
356          */
357         if (p->p_vkernel)
358                 vkernel_exit(p);
359
360         /* Stop profiling */
361         stopprofclock(p);
362
363         /* close files on exec */
364         fdcloseexec(p);
365
366         /* reset caught signals */
367         execsigs(p);
368
369         /* name this process - nameiexec(p, ndp) */
370         len = min(nd->nl_nch.ncp->nc_nlen, MAXCOMLEN);
371         bcopy(nd->nl_nch.ncp->nc_name, p->p_comm, len);
372         p->p_comm[len] = 0;
373         bcopy(p->p_comm, lp->lwp_thread->td_comm, MAXCOMLEN+1);
374
375         /*
376          * mark as execed, wakeup the process that vforked (if any) and tell
377          * it that it now has its own resources back
378          */
379         p->p_flag |= P_EXEC;
380         if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
381                 p->p_flag &= ~P_PPWAIT;
382                 wakeup((caddr_t)p->p_pptr);
383         }
384
385         /*
386          * Implement image setuid/setgid.
387          *
388          * Don't honor setuid/setgid if the filesystem prohibits it or if
389          * the process is being traced.
390          */
391         if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
392              ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
393             (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
394             (p->p_flag & P_TRACED) == 0) {
395                 /*
396                  * Turn off syscall tracing for set-id programs, except for
397                  * root.  Record any set-id flags first to make sure that
398                  * we do not regain any tracing during a possible block.
399                  */
400                 setsugid();
401                 if (p->p_tracenode && priv_check(td, PRIV_ROOT) != 0) {
402                         ktrdestroy(&p->p_tracenode);
403                         p->p_traceflag = 0;
404                 }
405                 /* Close any file descriptors 0..2 that reference procfs */
406                 setugidsafety(p);
407                 /* Make sure file descriptors 0..2 are in use. */
408                 error = fdcheckstd(p);
409                 if (error != 0)
410                         goto exec_fail_dealloc;
411                 /*
412                  * Set the new credentials.
413                  */
414                 cratom(&p->p_ucred);
415                 if (attr.va_mode & VSUID)
416                         change_euid(attr.va_uid);
417                 if (attr.va_mode & VSGID)
418                         p->p_ucred->cr_gid = attr.va_gid;
419
420                 /*
421                  * Clear local varsym variables
422                  */
423                 varsymset_clean(&p->p_varsymset);
424         } else {
425                 if (p->p_ucred->cr_uid == p->p_ucred->cr_ruid &&
426                     p->p_ucred->cr_gid == p->p_ucred->cr_rgid)
427                         p->p_flag &= ~P_SUGID;
428         }
429
430         /*
431          * Implement correct POSIX saved-id behavior.
432          */
433         if (p->p_ucred->cr_svuid != p->p_ucred->cr_uid ||
434             p->p_ucred->cr_svgid != p->p_ucred->cr_gid) {
435                 cratom(&p->p_ucred);
436                 p->p_ucred->cr_svuid = p->p_ucred->cr_uid;
437                 p->p_ucred->cr_svgid = p->p_ucred->cr_gid;
438         }
439
440         /*
441          * Store the vp for use in procfs
442          */
443         if (p->p_textvp)                /* release old reference */
444                 vrele(p->p_textvp);
445         p->p_textvp = imgp->vp;
446         vref(p->p_textvp);
447
448         /*
449          * Notify others that we exec'd, and clear the P_INEXEC flag
450          * as we're now a bona fide freshly-execed process.
451          */
452         KNOTE(&p->p_klist, NOTE_EXEC);
453         p->p_flag &= ~P_INEXEC;
454
455         /*
456          * If tracing the process, trap to debugger so breakpoints
457          *      can be set before the program executes.
458          */
459         STOPEVENT(p, S_EXEC, 0);
460
461         if (p->p_flag & P_TRACED)
462                 ksignal(p, SIGTRAP);
463
464         /* clear "fork but no exec" flag, as we _are_ execing */
465         p->p_acflag &= ~AFORK;
466
467         /* Set values passed into the program in registers. */
468         exec_setregs(imgp->entry_addr, (u_long)(uintptr_t)stack_base,
469             imgp->ps_strings);
470
471         /* Set the access time on the vnode */
472         vn_mark_atime(imgp->vp, td);
473
474         /* Free any previous argument cache */
475         if (p->p_args && --p->p_args->ar_ref == 0)
476                 FREE(p->p_args, M_PARGS);
477         p->p_args = NULL;
478
479         /* Cache arguments if they fit inside our allowance */
480         i = imgp->args->begin_envv - imgp->args->begin_argv;
481         if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
482                 MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, 
483                     M_PARGS, M_WAITOK);
484                 p->p_args->ar_ref = 1;
485                 p->p_args->ar_length = i;
486                 bcopy(imgp->args->begin_argv, p->p_args->ar_args, i);
487         }
488
489 exec_fail_dealloc:
490
491         /*
492          * free various allocated resources
493          */
494         if (imgp->firstpage)
495                 exec_unmap_first_page(imgp);
496
497         if (imgp->vp) {
498                 vrele(imgp->vp);
499                 imgp->vp = NULL;
500         }
501
502         if (error == 0) {
503                 ++mycpu->gd_cnt.v_exec;
504                 return (0);
505         }
506
507 exec_fail:
508         /*
509          * we're done here, clear P_INEXEC if we were the ones that
510          * set it.  Otherwise if vmspace_destroyed is still set we
511          * raced another thread and that thread is responsible for
512          * clearing it.
513          */
514         if (imgp->vmspace_destroyed & 2)
515                 p->p_flag &= ~P_INEXEC;
516         if (imgp->vmspace_destroyed) {
517                 /*
518                  * Sorry, no more process anymore. exit gracefully.
519                  * However we can't die right here, because our
520                  * caller might have to clean up, so indicate a
521                  * lethal error by returning -1.
522                  */
523                 return(-1);
524         } else {
525                 return(error);
526         }
527 }
528
529 /*
530  * execve() system call.
531  */
532 int
533 sys_execve(struct execve_args *uap)
534 {
535         struct nlookupdata nd;
536         struct image_args args;
537         int error;
538
539         error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
540         bzero(&args, sizeof(args));
541         if (error == 0) {
542                 error = exec_copyin_args(&args, uap->fname, PATH_USERSPACE,
543                                         uap->argv, uap->envv);
544         }
545         if (error == 0)
546                 error = kern_execve(&nd, &args);
547         nlookup_done(&nd);
548         exec_free_args(&args);
549
550         if (error < 0) {
551                 /* We hit a lethal error condition.  Let's die now. */
552                 exit1(W_EXITCODE(0, SIGABRT));
553                 /* NOTREACHED */
554         }
555
556         /*
557          * The syscall result is returned in registers to the new program.
558          * Linux will register %edx as an atexit function and we must be
559          * sure to set it to 0.  XXX
560          */
561         if (error == 0)
562                 uap->sysmsg_result64 = 0;
563
564         return (error);
565 }
566
567 int
568 exec_map_first_page(struct image_params *imgp)
569 {
570         int rv, i;
571         int initial_pagein;
572         vm_page_t ma[VM_INITIAL_PAGEIN];
573         vm_page_t m;
574         vm_object_t object;
575
576         if (imgp->firstpage)
577                 exec_unmap_first_page(imgp);
578
579         /*
580          * The file has to be mappable.
581          */
582         if ((object = imgp->vp->v_object) == NULL)
583                 return (EIO);
584
585         /*
586          * We shouldn't need protection for vm_page_grab() but we certainly
587          * need it for the lookup loop below (lookup/busy race), since
588          * an interrupt can unbusy and free the page before our busy check.
589          */
590         crit_enter();
591         m = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
592
593         if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
594                 ma[0] = m;
595                 initial_pagein = VM_INITIAL_PAGEIN;
596                 if (initial_pagein > object->size)
597                         initial_pagein = object->size;
598                 for (i = 1; i < initial_pagein; i++) {
599                         if ((m = vm_page_lookup(object, i)) != NULL) {
600                                 if ((m->flags & PG_BUSY) || m->busy)
601                                         break;
602                                 if (m->valid)
603                                         break;
604                                 vm_page_busy(m);
605                         } else {
606                                 m = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
607                                 if (m == NULL)
608                                         break;
609                         }
610                         ma[i] = m;
611                 }
612                 initial_pagein = i;
613
614                 /*
615                  * get_pages unbusies all the requested pages except the
616                  * primary page (at index 0 in this case).  The primary
617                  * page may have been wired during the pagein (e.g. by
618                  * the buffer cache) so vnode_pager_freepage() must be
619                  * used to properly release it.
620                  */
621                 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
622                 m = vm_page_lookup(object, 0);
623
624                 if (rv != VM_PAGER_OK || m == NULL || m->valid == 0) {
625                         if (m) {
626                                 vm_page_protect(m, VM_PROT_NONE);
627                                 vnode_pager_freepage(m);
628                         }
629                         crit_exit();
630                         return EIO;
631                 }
632         }
633         vm_page_hold(m);
634         vm_page_wakeup(m);      /* unbusy the page */
635         crit_exit();
636
637         imgp->firstpage = sf_buf_alloc(m, SFB_CPUPRIVATE);
638         imgp->image_header = (void *)sf_buf_kva(imgp->firstpage);
639
640         return 0;
641 }
642
643 void
644 exec_unmap_first_page(struct image_params *imgp)
645 {
646         vm_page_t m;
647
648         crit_enter();
649         if (imgp->firstpage != NULL) {
650                 m = sf_buf_page(imgp->firstpage);
651                 sf_buf_free(imgp->firstpage);
652                 imgp->firstpage = NULL;
653                 imgp->image_header = NULL;
654                 vm_page_unhold(m);
655         }
656         crit_exit();
657 }
658
659 /*
660  * Destroy old address space, and allocate a new stack
661  *      The new stack is only SGROWSIZ large because it is grown
662  *      automatically in trap.c.
663  *
664  * This is the point of no return.
665  */
666 int
667 exec_new_vmspace(struct image_params *imgp, struct vmspace *vmcopy)
668 {
669         struct vmspace *vmspace = imgp->proc->p_vmspace;
670         vm_offset_t stack_addr = USRSTACK - maxssiz;
671         struct proc *p;
672         vm_map_t map;
673         int error;
674
675         /*
676          * Indicate that we cannot gracefully error out any more, kill
677          * any other threads present, and set P_INEXEC to indicate that
678          * we are now messing with the process structure proper.
679          *
680          * If killalllwps() races return an error which coupled with
681          * vmspace_destroyed will cause us to exit.  This is what we
682          * want since another thread is patiently waiting for us to exit
683          * in that case.
684          */
685         p = curproc;
686         imgp->vmspace_destroyed = 1;
687
688         if (curthread->td_proc->p_nthreads > 1) {
689                 error = killalllwps(1);
690                 if (error)
691                         return (error);
692         }
693         imgp->vmspace_destroyed |= 2;   /* we are responsible for P_INEXEC */
694         p->p_flag |= P_INEXEC;
695
696         /*
697          * Prevent a pending AIO from modifying the new address space.
698          */
699         aio_proc_rundown(imgp->proc);
700
701         /*
702          * Blow away entire process VM, if address space not shared,
703          * otherwise, create a new VM space so that other threads are
704          * not disrupted.  If we are execing a resident vmspace we
705          * create a duplicate of it and remap the stack.
706          *
707          * The exitingcnt test is not strictly necessary but has been
708          * included for code sanity (to make the code more deterministic).
709          */
710         map = &vmspace->vm_map;
711         if (vmcopy) {
712                 vmspace_exec(imgp->proc, vmcopy);
713                 vmspace = imgp->proc->p_vmspace;
714                 pmap_remove_pages(vmspace_pmap(vmspace), stack_addr, USRSTACK);
715                 map = &vmspace->vm_map;
716         } else if (vmspace->vm_sysref.refcnt == 1 &&
717                    vmspace->vm_exitingcnt == 0) {
718                 shmexit(vmspace);
719                 if (vmspace->vm_upcalls)
720                         upc_release(vmspace, ONLY_LWP_IN_PROC(imgp->proc));
721                 pmap_remove_pages(vmspace_pmap(vmspace),
722                         0, VM_MAX_USER_ADDRESS);
723                 vm_map_remove(map, 0, VM_MAX_USER_ADDRESS);
724         } else {
725                 vmspace_exec(imgp->proc, NULL);
726                 vmspace = imgp->proc->p_vmspace;
727                 map = &vmspace->vm_map;
728         }
729
730         /* Allocate a new stack */
731         error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
732                              0, VM_PROT_ALL, VM_PROT_ALL, 0);
733         if (error)
734                 return (error);
735
736         /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
737          * VM_STACK case, but they are still used to monitor the size of the
738          * process stack so we can check the stack rlimit.
739          */
740         vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
741         vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
742
743         return(0);
744 }
745
746 /*
747  * Copy out argument and environment strings from the old process
748  *      address space into the temporary string buffer.
749  */
750 int
751 exec_copyin_args(struct image_args *args, char *fname,
752                 enum exec_path_segflg segflg, char **argv, char **envv)
753 {
754         char    *argp, *envp;
755         int     error = 0;
756         size_t  length;
757
758         args->buf = objcache_get(exec_objcache, M_WAITOK);
759         if (args->buf == NULL)
760                 return (ENOMEM);
761         args->begin_argv = args->buf;
762         args->endp = args->begin_argv;
763         args->space = ARG_MAX;
764
765         args->fname = args->buf + ARG_MAX;
766
767         /*
768          * Copy the file name.
769          */
770         if (segflg == PATH_SYSSPACE) {
771                 error = copystr(fname, args->fname, PATH_MAX, &length);
772         } else if (segflg == PATH_USERSPACE) {
773                 error = copyinstr(fname, args->fname, PATH_MAX, &length);
774         }
775
776         /*
777          * Extract argument strings.  argv may not be NULL.  The argv
778          * array is terminated by a NULL entry.  We special-case the
779          * situation where argv[0] is NULL by passing { filename, NULL }
780          * to the new program to guarentee that the interpreter knows what
781          * file to open in case we exec an interpreted file.   Note that
782          * a NULL argv[0] terminates the argv[] array.
783          *
784          * XXX the special-casing of argv[0] is historical and needs to be
785          * revisited.
786          */
787         if (argv == NULL)
788                 error = EFAULT;
789         if (error == 0) {
790                 while ((argp = (caddr_t)(intptr_t)fuword(argv++)) != NULL) {
791                         if (argp == (caddr_t)-1) {
792                                 error = EFAULT;
793                                 break;
794                         }
795                         error = copyinstr(argp, args->endp,
796                                             args->space, &length);
797                         if (error) {
798                                 if (error == ENAMETOOLONG)
799                                         error = E2BIG;
800                                 break;
801                         }
802                         args->space -= length;
803                         args->endp += length;
804                         args->argc++;
805                 }
806                 if (args->argc == 0 && error == 0) {
807                         length = strlen(args->fname) + 1;
808                         if (length > args->space) {
809                                 error = E2BIG;
810                         } else {
811                                 bcopy(args->fname, args->endp, length);
812                                 args->space -= length;
813                                 args->endp += length;
814                                 args->argc++;
815                         }
816                 }
817         }       
818
819         args->begin_envv = args->endp;
820
821         /*
822          * extract environment strings.  envv may be NULL.
823          */
824         if (envv && error == 0) {
825                 while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
826                         if (envp == (caddr_t) -1) {
827                                 error = EFAULT;
828                                 break;
829                         }
830                         error = copyinstr(envp, args->endp, args->space,
831                             &length);
832                         if (error) {
833                                 if (error == ENAMETOOLONG)
834                                         error = E2BIG;
835                                 break;
836                         }
837                         args->space -= length;
838                         args->endp += length;
839                         args->envc++;
840                 }
841         }
842         return (error);
843 }
844
845 void
846 exec_free_args(struct image_args *args)
847 {
848         if (args->buf) {
849                 objcache_put(exec_objcache, args->buf);
850                 args->buf = NULL;
851         }
852 }
853
854 /*
855  * Copy strings out to the new process address space, constructing
856  *      new arg and env vector tables. Return a pointer to the base
857  *      so that it can be used as the initial stack pointer.
858  */
859 register_t *
860 exec_copyout_strings(struct image_params *imgp)
861 {
862         int argc, envc, sgap;
863         char **vectp;
864         char *stringp, *destp;
865         register_t *stack_base;
866         struct ps_strings *arginfo;
867         int szsigcode;
868
869         /*
870          * Calculate string base and vector table pointers.
871          * Also deal with signal trampoline code for this exec type.
872          */
873         arginfo = (struct ps_strings *)PS_STRINGS;
874         szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
875         if (stackgap_random != 0)
876                 sgap = ALIGN(karc4random() & (stackgap_random - 1));
877         else
878                 sgap = 0;
879         destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - sgap -
880             roundup((ARG_MAX - imgp->args->space), sizeof(char *));
881
882         /*
883          * install sigcode
884          */
885         if (szsigcode)
886                 copyout(imgp->proc->p_sysent->sv_sigcode,
887                     ((caddr_t)arginfo - szsigcode), szsigcode);
888
889         /*
890          * If we have a valid auxargs ptr, prepare some room
891          * on the stack.
892          *
893          * The '+ 2' is for the null pointers at the end of each of the
894          * arg and env vector sets, and 'AT_COUNT*2' is room for the
895          * ELF Auxargs data.
896          */
897         if (imgp->auxargs) {
898                 vectp = (char **)(destp - (imgp->args->argc +
899                         imgp->args->envc + 2 + AT_COUNT * 2) * sizeof(char*));
900         } else {
901                 vectp = (char **)(destp - (imgp->args->argc +
902                         imgp->args->envc + 2) * sizeof(char*));
903         }
904
905         /*
906          * NOTE: don't bother aligning the stack here for GCC 2.x, it will
907          * be done in crt1.o.  Note that GCC 3.x aligns the stack in main.
908          */
909
910         /*
911          * vectp also becomes our initial stack base
912          */
913         stack_base = (register_t *)vectp;
914
915         stringp = imgp->args->begin_argv;
916         argc = imgp->args->argc;
917         envc = imgp->args->envc;
918
919         /*
920          * Copy out strings - arguments and environment.
921          */
922         copyout(stringp, destp, ARG_MAX - imgp->args->space);
923
924         /*
925          * Fill in "ps_strings" struct for ps, w, etc.
926          */
927         suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
928         suword(&arginfo->ps_nargvstr, argc);
929
930         /*
931          * Fill in argument portion of vector table.
932          */
933         for (; argc > 0; --argc) {
934                 suword(vectp++, (long)(intptr_t)destp);
935                 while (*stringp++ != 0)
936                         destp++;
937                 destp++;
938         }
939
940         /* a null vector table pointer separates the argp's from the envp's */
941         suword(vectp++, 0);
942
943         suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
944         suword(&arginfo->ps_nenvstr, envc);
945
946         /*
947          * Fill in environment portion of vector table.
948          */
949         for (; envc > 0; --envc) {
950                 suword(vectp++, (long)(intptr_t)destp);
951                 while (*stringp++ != 0)
952                         destp++;
953                 destp++;
954         }
955
956         /* end of vector table is a null pointer */
957         suword(vectp, 0);
958
959         return (stack_base);
960 }
961
962 /*
963  * Check permissions of file to execute.
964  *      Return 0 for success or error code on failure.
965  */
966 int
967 exec_check_permissions(struct image_params *imgp)
968 {
969         struct proc *p = imgp->proc;
970         struct vnode *vp = imgp->vp;
971         struct vattr *attr = imgp->attr;
972         int error;
973
974         /* Get file attributes */
975         error = VOP_GETATTR(vp, attr);
976         if (error)
977                 return (error);
978
979         /*
980          * 1) Check if file execution is disabled for the filesystem that this
981          *      file resides on.
982          * 2) Insure that at least one execute bit is on - otherwise root
983          *      will always succeed, and we don't want to happen unless the
984          *      file really is executable.
985          * 3) Insure that the file is a regular file.
986          */
987         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
988             ((attr->va_mode & 0111) == 0) ||
989             (attr->va_type != VREG)) {
990                 return (EACCES);
991         }
992
993         /*
994          * Zero length files can't be exec'd
995          */
996         if (attr->va_size == 0)
997                 return (ENOEXEC);
998
999         /*
1000          *  Check for execute permission to file based on current credentials.
1001          */
1002         error = VOP_EACCESS(vp, VEXEC, p->p_ucred);
1003         if (error)
1004                 return (error);
1005
1006         /*
1007          * Check number of open-for-writes on the file and deny execution
1008          * if there are any.
1009          */
1010         if (vp->v_writecount)
1011                 return (ETXTBSY);
1012
1013         /*
1014          * Call filesystem specific open routine, which allows us to read,
1015          * write, and mmap the file.  Without the VOP_OPEN we can only
1016          * stat the file.
1017          */
1018         error = VOP_OPEN(vp, FREAD, p->p_ucred, NULL);
1019         if (error)
1020                 return (error);
1021
1022         return (0);
1023 }
1024
1025 /*
1026  * Exec handler registration
1027  */
1028 int
1029 exec_register(const struct execsw *execsw_arg)
1030 {
1031         const struct execsw **es, **xs, **newexecsw;
1032         int count = 2;  /* New slot and trailing NULL */
1033
1034         if (execsw)
1035                 for (es = execsw; *es; es++)
1036                         count++;
1037         newexecsw = kmalloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1038         xs = newexecsw;
1039         if (execsw)
1040                 for (es = execsw; *es; es++)
1041                         *xs++ = *es;
1042         *xs++ = execsw_arg;
1043         *xs = NULL;
1044         if (execsw)
1045                 kfree(execsw, M_TEMP);
1046         execsw = newexecsw;
1047         return 0;
1048 }
1049
1050 int
1051 exec_unregister(const struct execsw *execsw_arg)
1052 {
1053         const struct execsw **es, **xs, **newexecsw;
1054         int count = 1;
1055
1056         if (execsw == NULL)
1057                 panic("unregister with no handlers left?");
1058
1059         for (es = execsw; *es; es++) {
1060                 if (*es == execsw_arg)
1061                         break;
1062         }
1063         if (*es == NULL)
1064                 return ENOENT;
1065         for (es = execsw; *es; es++)
1066                 if (*es != execsw_arg)
1067                         count++;
1068         newexecsw = kmalloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1069         xs = newexecsw;
1070         for (es = execsw; *es; es++)
1071                 if (*es != execsw_arg)
1072                         *xs++ = *es;
1073         *xs = NULL;
1074         if (execsw)
1075                 kfree(execsw, M_TEMP);
1076         execsw = newexecsw;
1077         return 0;
1078 }