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