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