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