Deleted Added
full compact
kern_sharedpage.c (15448) kern_sharedpage.c (15494)
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 *
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 * $Id: kern_exec.c,v 1.38 1996/04/08 01:21:59 davidg Exp $
26 * $Id: kern_exec.c,v 1.39 1996/04/29 15:07:59 smpatel Exp $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/sysproto.h>
32#include <sys/signalvar.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/wait.h>
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/sysproto.h>
32#include <sys/signalvar.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/wait.h>
42#include <sys/proc.h>
42#include <sys/malloc.h>
43#include <sys/malloc.h>
44#include <sys/namei.h>
43#include <sys/sysent.h>
44#include <sys/syslog.h>
45#include <sys/shm.h>
46#include <sys/sysctl.h>
45#include <sys/sysent.h>
46#include <sys/syslog.h>
47#include <sys/shm.h>
48#include <sys/sysctl.h>
49#include <sys/vnode.h>
47
48#include <vm/vm.h>
49#include <vm/vm_param.h>
50#include <vm/vm_prot.h>
51#include <vm/lock.h>
52#include <vm/pmap.h>
53#include <vm/vm_map.h>
54#include <vm/vm_kern.h>
55#include <vm/vm_extern.h>
56
57#include <machine/reg.h>
58
59static int *exec_copyout_strings __P((struct image_params *));
60
61static int exec_check_permissions(struct image_params *);
62
63/*
64 * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts
65 * of the sysctl code also assumes this, and sizeof(int) == sizeof(long).
66 */
67static caddr_t ps_strings = (caddr_t)PS_STRINGS;
68SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, "");
69
70static caddr_t usrstack = (caddr_t)USRSTACK;
71SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, "");
72
73/*
74 * execsw_set is constructed for us by the linker. Each of the items
75 * is a pointer to a `const struct execsw', hence the double pointer here.
76 */
77static const struct execsw **execsw =
78 (const struct execsw **)&execsw_set.ls_items[0];
79
80#ifndef _SYS_SYSPROTO_H_
81struct execve_args {
82 char *fname;
83 char **argv;
84 char **envv;
85};
86#endif
87
88/*
89 * execve() system call.
90 */
91int
92execve(p, uap, retval)
93 struct proc *p;
94 register struct execve_args *uap;
95 int *retval;
96{
97 struct nameidata nd, *ndp;
98 int *stack_base;
99 int error, len, i;
100 struct image_params image_params, *imgp;
101 struct vattr attr;
102
103 imgp = &image_params;
104
105 /*
106 * Initialize part of the common data
107 */
108 imgp->proc = p;
109 imgp->uap = uap;
110 imgp->attr = &attr;
111 imgp->image_header = NULL;
112 imgp->argc = imgp->envc = 0;
113 imgp->entry_addr = 0;
114 imgp->vmspace_destroyed = 0;
115 imgp->interpreted = 0;
116 imgp->interpreter_name[0] = '\0';
117 imgp->auxargs = NULL;
118
119 /*
120 * Allocate temporary demand zeroed space for argument and
121 * environment strings
122 */
123 imgp->stringbase = (char *)kmem_alloc_pageable(exec_map, ARG_MAX);
124 if (imgp->stringbase == NULL) {
125 error = ENOMEM;
126 goto exec_fail;
127 }
128 imgp->stringp = imgp->stringbase;
129 imgp->stringspace = ARG_MAX;
130
131 /*
132 * Translate the file name. namei() returns a vnode pointer
133 * in ni_vp amoung other things.
134 */
135 ndp = &nd;
136 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
137 UIO_USERSPACE, uap->fname, p);
138
139interpret:
140
141 error = namei(ndp);
142 if (error) {
143 kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
144 goto exec_fail;
145 }
146
147 imgp->vp = ndp->ni_vp;
148 if (imgp->vp == NULL) {
149 error = ENOEXEC;
150 goto exec_fail_dealloc;
151 }
152
153 /*
154 * Check file permissions (also 'opens' file)
155 */
156 error = exec_check_permissions(imgp);
157
158 /*
159 * Lose the lock on the vnode. It's no longer needed, and must not
160 * exist for the pagefault paging to work below.
161 */
162 VOP_UNLOCK(imgp->vp);
163
164 if (error)
165 goto exec_fail_dealloc;
166
167 /*
168 * Map the image header (first page) of the file into
169 * kernel address space
170 */
171 error = vm_mmap(kernel_map, /* map */
172 (vm_offset_t *)&imgp->image_header, /* address */
173 PAGE_SIZE, /* size */
174 VM_PROT_READ, /* protection */
175 VM_PROT_READ, /* max protection */
176 0, /* flags */
177 (caddr_t)imgp->vp, /* vnode */
178 0); /* offset */
179 if (error) {
180 uprintf("mmap failed: %d\n",error);
181 goto exec_fail_dealloc;
182 }
183
184 /*
185 * Loop through list of image activators, calling each one.
186 * If there is no match, the activator returns -1. If there
187 * is a match, but there was an error during the activation,
188 * the error is returned. Otherwise 0 means success. If the
189 * image is interpreted, loop back up and try activating
190 * the interpreter.
191 */
192 for (i = 0; execsw[i]; ++i) {
193 if (execsw[i]->ex_imgact)
194 error = (*execsw[i]->ex_imgact)(imgp);
195 else
196 continue;
197
198 if (error == -1)
199 continue;
200 if (error)
201 goto exec_fail_dealloc;
202 if (imgp->interpreted) {
203 /* free old vnode and name buffer */
204 vrele(ndp->ni_vp);
205 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
206 if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
207 (vm_offset_t)imgp->image_header + PAGE_SIZE))
208 panic("execve: header dealloc failed (1)");
209
210 /* set new name to that of the interpreter */
211 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
212 UIO_SYSSPACE, imgp->interpreter_name, p);
213 goto interpret;
214 }
215 break;
216 }
217 /* If we made it through all the activators and none matched, exit. */
218 if (error == -1) {
219 error = ENOEXEC;
220 goto exec_fail_dealloc;
221 }
222
223 /*
224 * Copy out strings (args and env) and initialize stack base
225 */
226 stack_base = exec_copyout_strings(imgp);
227 p->p_vmspace->vm_minsaddr = (char *)stack_base;
228
229 /*
230 * If custom stack fixup routine present for this process
231 * let it do the stack setup.
232 * Else stuff argument count as first item on stack
233 */
234 if (p->p_sysent->sv_fixup)
235 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
236 else
237 suword(--stack_base, imgp->argc);
238
239 /* close files on exec */
240 fdcloseexec(p);
241
242 /* reset caught signals */
243 execsigs(p);
244
245 /* name this process - nameiexec(p, ndp) */
246 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
247 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
248 p->p_comm[len] = 0;
249
250 /*
251 * mark as execed, wakeup the process that vforked (if any) and tell
252 * it that it now has it's own resources back
253 */
254 p->p_flag |= P_EXEC;
255 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
256 p->p_flag &= ~P_PPWAIT;
257 wakeup((caddr_t)p->p_pptr);
258 }
259
260 /*
261 * Implement image setuid/setgid. Disallow if the process is
262 * being traced.
263 */
264 if ((attr.va_mode & (VSUID | VSGID)) &&
265 (p->p_flag & P_TRACED) == 0) {
266 /*
267 * Turn off syscall tracing for set-id programs, except for
268 * root.
269 */
270 if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) {
271 p->p_traceflag = 0;
272 vrele(p->p_tracep);
273 p->p_tracep = NULL;
274 }
275 /*
276 * Set the new credentials.
277 */
278 p->p_ucred = crcopy(p->p_ucred);
279 if (attr.va_mode & VSUID)
280 p->p_ucred->cr_uid = attr.va_uid;
281 if (attr.va_mode & VSGID)
282 p->p_ucred->cr_groups[0] = attr.va_gid;
283 p->p_flag |= P_SUGID;
284 } else {
285 p->p_flag &= ~P_SUGID;
286 }
287
288 /*
289 * Implement correct POSIX saved-id behavior.
290 */
291 p->p_cred->p_svuid = p->p_ucred->cr_uid;
292 p->p_cred->p_svgid = p->p_ucred->cr_gid;
293
294 /*
295 * Store the vp for use in procfs
296 */
297 if (p->p_textvp) /* release old reference */
298 vrele(p->p_textvp);
299 VREF(ndp->ni_vp);
300 p->p_textvp = ndp->ni_vp;
301
302 /*
303 * If tracing the process, trap to debugger so breakpoints
304 * can be set before the program executes.
305 */
306 if (p->p_flag & P_TRACED)
307 psignal(p, SIGTRAP);
308
309 /* clear "fork but no exec" flag, as we _are_ execing */
310 p->p_acflag &= ~AFORK;
311
312 /* Set entry address */
313 setregs(p, imgp->entry_addr, (u_long)stack_base);
314
315 /*
316 * free various allocated resources
317 */
318 kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
319 if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
320 (vm_offset_t)imgp->image_header + PAGE_SIZE))
321 panic("execve: header dealloc failed (2)");
322 vrele(ndp->ni_vp);
323 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
324
325 return (0);
326
327exec_fail_dealloc:
328 if (imgp->stringbase != NULL)
329 kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
330 if (imgp->image_header && imgp->image_header != (char *)-1)
331 if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
332 (vm_offset_t)imgp->image_header + PAGE_SIZE))
333 panic("execve: header dealloc failed (3)");
334 if (ndp->ni_vp)
335 vrele(ndp->ni_vp);
336 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
337
338exec_fail:
339 if (imgp->vmspace_destroyed) {
340 /* sorry, no more process anymore. exit gracefully */
341 exit1(p, W_EXITCODE(0, SIGABRT));
342 /* NOT REACHED */
343 return(0);
344 } else {
345 return(error);
346 }
347}
348
349/*
350 * Destroy old address space, and allocate a new stack
351 * The new stack is only SGROWSIZ large because it is grown
352 * automatically in trap.c.
353 */
354int
355exec_new_vmspace(imgp)
356 struct image_params *imgp;
357{
358 int error;
359 struct vmspace *vmspace = imgp->proc->p_vmspace;
360 caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ);
361
362 imgp->vmspace_destroyed = 1;
363
364 /* Blow away entire process VM */
365 if (vmspace->vm_shm)
366 shmexit(imgp->proc);
367 vm_map_remove(&vmspace->vm_map, 0, USRSTACK);
368
369 /* Allocate a new stack */
370 error = vm_map_find(&vmspace->vm_map, NULL, 0, (vm_offset_t *)&stack_addr,
371 SGROWSIZ, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
372 if (error)
373 return(error);
374
375 vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
376
377 /* Initialize maximum stack address */
378 vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
379
380 return(0);
381}
382
383/*
384 * Copy out argument and environment strings from the old process
385 * address space into the temporary string buffer.
386 */
387int
388exec_extract_strings(imgp)
389 struct image_params *imgp;
390{
391 char **argv, **envv;
392 char *argp, *envp;
393 int error, length;
394
395 /*
396 * extract arguments first
397 */
398
399 argv = imgp->uap->argv;
400
401 if (argv) {
402 while ((argp = (caddr_t) fuword(argv++))) {
403 if (argp == (caddr_t) -1)
404 return (EFAULT);
405 if ((error = copyinstr(argp, imgp->stringp,
406 imgp->stringspace, &length))) {
407 if (error == ENAMETOOLONG)
408 return(E2BIG);
409 return (error);
410 }
411 imgp->stringspace -= length;
412 imgp->stringp += length;
413 imgp->argc++;
414 }
415 }
416
417 /*
418 * extract environment strings
419 */
420
421 envv = imgp->uap->envv;
422
423 if (envv) {
424 while ((envp = (caddr_t) fuword(envv++))) {
425 if (envp == (caddr_t) -1)
426 return (EFAULT);
427 if ((error = copyinstr(envp, imgp->stringp,
428 imgp->stringspace, &length))) {
429 if (error == ENAMETOOLONG)
430 return(E2BIG);
431 return (error);
432 }
433 imgp->stringspace -= length;
434 imgp->stringp += length;
435 imgp->envc++;
436 }
437 }
438
439 return (0);
440}
441
442/*
443 * Copy strings out to the new process address space, constructing
444 * new arg and env vector tables. Return a pointer to the base
445 * so that it can be used as the initial stack pointer.
446 */
447int *
448exec_copyout_strings(imgp)
449 struct image_params *imgp;
450{
451 int argc, envc;
452 char **vectp;
453 char *stringp, *destp;
454 int *stack_base;
455 struct ps_strings *arginfo;
456 int szsigcode;
457
458 /*
459 * Calculate string base and vector table pointers.
460 * Also deal with signal trampoline code for this exec type.
461 */
462 arginfo = PS_STRINGS;
463 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
464 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
465 roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
466
467 /*
468 * install sigcode
469 */
470 if (szsigcode)
471 copyout(imgp->proc->p_sysent->sv_sigcode,
472 ((caddr_t)arginfo - szsigcode), szsigcode);
473
474 /*
475 * If we have a valid auxargs ptr, prepare some room
476 * on the stack.
477 */
478 if (imgp->auxargs)
479 /*
480 * The '+ 2' is for the null pointers at the end of each of the
481 * arg and env vector sets, and 'AT_COUNT*2' is room for the
482 * ELF Auxargs data.
483 */
484 vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
485 AT_COUNT*2) * sizeof(char*));
486 else
487 /*
488 * The '+ 2' is for the null pointers at the end of each of the
489 * arg and env vector sets
490 */
491 vectp = (char **)
492 (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
493
494 /*
495 * vectp also becomes our initial stack base
496 */
497 stack_base = (int *)vectp;
498
499 stringp = imgp->stringbase;
500 argc = imgp->argc;
501 envc = imgp->envc;
502
503 /*
504 * Copy out strings - arguments and environment.
505 */
506 copyout(stringp, destp, ARG_MAX - imgp->stringspace);
507
508 /*
509 * Fill in "ps_strings" struct for ps, w, etc.
510 */
511 suword(&arginfo->ps_argvstr, (int)vectp);
512 suword(&arginfo->ps_nargvstr, argc);
513
514 /*
515 * Fill in argument portion of vector table.
516 */
517 for (; argc > 0; --argc) {
518 suword(vectp++, (int)destp);
519 while (*stringp++ != 0)
520 destp++;
521 destp++;
522 }
523
524 /* a null vector table pointer seperates the argp's from the envp's */
525 suword(vectp++, NULL);
526
527 suword(&arginfo->ps_envstr, (int)vectp);
528 suword(&arginfo->ps_nenvstr, envc);
529
530 /*
531 * Fill in environment portion of vector table.
532 */
533 for (; envc > 0; --envc) {
534 suword(vectp++, (int)destp);
535 while (*stringp++ != 0)
536 destp++;
537 destp++;
538 }
539
540 /* end of vector table is a null pointer */
541 suword(vectp, NULL);
542
543 return (stack_base);
544}
545
546/*
547 * Check permissions of file to execute.
548 * Return 0 for success or error code on failure.
549 */
550static int
551exec_check_permissions(imgp)
552 struct image_params *imgp;
553{
554 struct proc *p = imgp->proc;
555 struct vnode *vp = imgp->vp;
556 struct vattr *attr = imgp->attr;
557 int error;
558
559 /*
560 * Check number of open-for-writes on the file and deny execution
561 * if there are any.
562 */
563 if (vp->v_writecount) {
564 return (ETXTBSY);
565 }
566
567 /* Get file attributes */
568 error = VOP_GETATTR(vp, attr, p->p_ucred, p);
569 if (error)
570 return (error);
571
572 /*
573 * 1) Check if file execution is disabled for the filesystem that this
574 * file resides on.
575 * 2) Insure that at least one execute bit is on - otherwise root
576 * will always succeed, and we don't want to happen unless the
577 * file really is executable.
578 * 3) Insure that the file is a regular file.
579 */
580 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
581 ((attr->va_mode & 0111) == 0) ||
582 (attr->va_type != VREG)) {
583 return (EACCES);
584 }
585
586 /*
587 * Zero length files can't be exec'd
588 */
589 if (attr->va_size == 0)
590 return (ENOEXEC);
591
592 /*
593 * Disable setuid/setgid if the filesystem prohibits it or if
594 * the process is being traced.
595 */
596 if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
597 attr->va_mode &= ~(VSUID | VSGID);
598
599 /*
600 * Check for execute permission to file based on current credentials.
601 * Then call filesystem specific open routine (which does nothing
602 * in the general case).
603 */
604 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
605 if (error)
606 return (error);
607
608 error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
609 if (error)
610 return (error);
611
612 return (0);
613}
50
51#include <vm/vm.h>
52#include <vm/vm_param.h>
53#include <vm/vm_prot.h>
54#include <vm/lock.h>
55#include <vm/pmap.h>
56#include <vm/vm_map.h>
57#include <vm/vm_kern.h>
58#include <vm/vm_extern.h>
59
60#include <machine/reg.h>
61
62static int *exec_copyout_strings __P((struct image_params *));
63
64static int exec_check_permissions(struct image_params *);
65
66/*
67 * XXX trouble here if sizeof(caddr_t) != sizeof(int), other parts
68 * of the sysctl code also assumes this, and sizeof(int) == sizeof(long).
69 */
70static caddr_t ps_strings = (caddr_t)PS_STRINGS;
71SYSCTL_INT(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, "");
72
73static caddr_t usrstack = (caddr_t)USRSTACK;
74SYSCTL_INT(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, "");
75
76/*
77 * execsw_set is constructed for us by the linker. Each of the items
78 * is a pointer to a `const struct execsw', hence the double pointer here.
79 */
80static const struct execsw **execsw =
81 (const struct execsw **)&execsw_set.ls_items[0];
82
83#ifndef _SYS_SYSPROTO_H_
84struct execve_args {
85 char *fname;
86 char **argv;
87 char **envv;
88};
89#endif
90
91/*
92 * execve() system call.
93 */
94int
95execve(p, uap, retval)
96 struct proc *p;
97 register struct execve_args *uap;
98 int *retval;
99{
100 struct nameidata nd, *ndp;
101 int *stack_base;
102 int error, len, i;
103 struct image_params image_params, *imgp;
104 struct vattr attr;
105
106 imgp = &image_params;
107
108 /*
109 * Initialize part of the common data
110 */
111 imgp->proc = p;
112 imgp->uap = uap;
113 imgp->attr = &attr;
114 imgp->image_header = NULL;
115 imgp->argc = imgp->envc = 0;
116 imgp->entry_addr = 0;
117 imgp->vmspace_destroyed = 0;
118 imgp->interpreted = 0;
119 imgp->interpreter_name[0] = '\0';
120 imgp->auxargs = NULL;
121
122 /*
123 * Allocate temporary demand zeroed space for argument and
124 * environment strings
125 */
126 imgp->stringbase = (char *)kmem_alloc_pageable(exec_map, ARG_MAX);
127 if (imgp->stringbase == NULL) {
128 error = ENOMEM;
129 goto exec_fail;
130 }
131 imgp->stringp = imgp->stringbase;
132 imgp->stringspace = ARG_MAX;
133
134 /*
135 * Translate the file name. namei() returns a vnode pointer
136 * in ni_vp amoung other things.
137 */
138 ndp = &nd;
139 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
140 UIO_USERSPACE, uap->fname, p);
141
142interpret:
143
144 error = namei(ndp);
145 if (error) {
146 kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
147 goto exec_fail;
148 }
149
150 imgp->vp = ndp->ni_vp;
151 if (imgp->vp == NULL) {
152 error = ENOEXEC;
153 goto exec_fail_dealloc;
154 }
155
156 /*
157 * Check file permissions (also 'opens' file)
158 */
159 error = exec_check_permissions(imgp);
160
161 /*
162 * Lose the lock on the vnode. It's no longer needed, and must not
163 * exist for the pagefault paging to work below.
164 */
165 VOP_UNLOCK(imgp->vp);
166
167 if (error)
168 goto exec_fail_dealloc;
169
170 /*
171 * Map the image header (first page) of the file into
172 * kernel address space
173 */
174 error = vm_mmap(kernel_map, /* map */
175 (vm_offset_t *)&imgp->image_header, /* address */
176 PAGE_SIZE, /* size */
177 VM_PROT_READ, /* protection */
178 VM_PROT_READ, /* max protection */
179 0, /* flags */
180 (caddr_t)imgp->vp, /* vnode */
181 0); /* offset */
182 if (error) {
183 uprintf("mmap failed: %d\n",error);
184 goto exec_fail_dealloc;
185 }
186
187 /*
188 * Loop through list of image activators, calling each one.
189 * If there is no match, the activator returns -1. If there
190 * is a match, but there was an error during the activation,
191 * the error is returned. Otherwise 0 means success. If the
192 * image is interpreted, loop back up and try activating
193 * the interpreter.
194 */
195 for (i = 0; execsw[i]; ++i) {
196 if (execsw[i]->ex_imgact)
197 error = (*execsw[i]->ex_imgact)(imgp);
198 else
199 continue;
200
201 if (error == -1)
202 continue;
203 if (error)
204 goto exec_fail_dealloc;
205 if (imgp->interpreted) {
206 /* free old vnode and name buffer */
207 vrele(ndp->ni_vp);
208 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
209 if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
210 (vm_offset_t)imgp->image_header + PAGE_SIZE))
211 panic("execve: header dealloc failed (1)");
212
213 /* set new name to that of the interpreter */
214 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
215 UIO_SYSSPACE, imgp->interpreter_name, p);
216 goto interpret;
217 }
218 break;
219 }
220 /* If we made it through all the activators and none matched, exit. */
221 if (error == -1) {
222 error = ENOEXEC;
223 goto exec_fail_dealloc;
224 }
225
226 /*
227 * Copy out strings (args and env) and initialize stack base
228 */
229 stack_base = exec_copyout_strings(imgp);
230 p->p_vmspace->vm_minsaddr = (char *)stack_base;
231
232 /*
233 * If custom stack fixup routine present for this process
234 * let it do the stack setup.
235 * Else stuff argument count as first item on stack
236 */
237 if (p->p_sysent->sv_fixup)
238 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
239 else
240 suword(--stack_base, imgp->argc);
241
242 /* close files on exec */
243 fdcloseexec(p);
244
245 /* reset caught signals */
246 execsigs(p);
247
248 /* name this process - nameiexec(p, ndp) */
249 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
250 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
251 p->p_comm[len] = 0;
252
253 /*
254 * mark as execed, wakeup the process that vforked (if any) and tell
255 * it that it now has it's own resources back
256 */
257 p->p_flag |= P_EXEC;
258 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
259 p->p_flag &= ~P_PPWAIT;
260 wakeup((caddr_t)p->p_pptr);
261 }
262
263 /*
264 * Implement image setuid/setgid. Disallow if the process is
265 * being traced.
266 */
267 if ((attr.va_mode & (VSUID | VSGID)) &&
268 (p->p_flag & P_TRACED) == 0) {
269 /*
270 * Turn off syscall tracing for set-id programs, except for
271 * root.
272 */
273 if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) {
274 p->p_traceflag = 0;
275 vrele(p->p_tracep);
276 p->p_tracep = NULL;
277 }
278 /*
279 * Set the new credentials.
280 */
281 p->p_ucred = crcopy(p->p_ucred);
282 if (attr.va_mode & VSUID)
283 p->p_ucred->cr_uid = attr.va_uid;
284 if (attr.va_mode & VSGID)
285 p->p_ucred->cr_groups[0] = attr.va_gid;
286 p->p_flag |= P_SUGID;
287 } else {
288 p->p_flag &= ~P_SUGID;
289 }
290
291 /*
292 * Implement correct POSIX saved-id behavior.
293 */
294 p->p_cred->p_svuid = p->p_ucred->cr_uid;
295 p->p_cred->p_svgid = p->p_ucred->cr_gid;
296
297 /*
298 * Store the vp for use in procfs
299 */
300 if (p->p_textvp) /* release old reference */
301 vrele(p->p_textvp);
302 VREF(ndp->ni_vp);
303 p->p_textvp = ndp->ni_vp;
304
305 /*
306 * If tracing the process, trap to debugger so breakpoints
307 * can be set before the program executes.
308 */
309 if (p->p_flag & P_TRACED)
310 psignal(p, SIGTRAP);
311
312 /* clear "fork but no exec" flag, as we _are_ execing */
313 p->p_acflag &= ~AFORK;
314
315 /* Set entry address */
316 setregs(p, imgp->entry_addr, (u_long)stack_base);
317
318 /*
319 * free various allocated resources
320 */
321 kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
322 if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
323 (vm_offset_t)imgp->image_header + PAGE_SIZE))
324 panic("execve: header dealloc failed (2)");
325 vrele(ndp->ni_vp);
326 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
327
328 return (0);
329
330exec_fail_dealloc:
331 if (imgp->stringbase != NULL)
332 kmem_free(exec_map, (vm_offset_t)imgp->stringbase, ARG_MAX);
333 if (imgp->image_header && imgp->image_header != (char *)-1)
334 if (vm_map_remove(kernel_map, (vm_offset_t)imgp->image_header,
335 (vm_offset_t)imgp->image_header + PAGE_SIZE))
336 panic("execve: header dealloc failed (3)");
337 if (ndp->ni_vp)
338 vrele(ndp->ni_vp);
339 FREE(ndp->ni_cnd.cn_pnbuf, M_NAMEI);
340
341exec_fail:
342 if (imgp->vmspace_destroyed) {
343 /* sorry, no more process anymore. exit gracefully */
344 exit1(p, W_EXITCODE(0, SIGABRT));
345 /* NOT REACHED */
346 return(0);
347 } else {
348 return(error);
349 }
350}
351
352/*
353 * Destroy old address space, and allocate a new stack
354 * The new stack is only SGROWSIZ large because it is grown
355 * automatically in trap.c.
356 */
357int
358exec_new_vmspace(imgp)
359 struct image_params *imgp;
360{
361 int error;
362 struct vmspace *vmspace = imgp->proc->p_vmspace;
363 caddr_t stack_addr = (caddr_t) (USRSTACK - SGROWSIZ);
364
365 imgp->vmspace_destroyed = 1;
366
367 /* Blow away entire process VM */
368 if (vmspace->vm_shm)
369 shmexit(imgp->proc);
370 vm_map_remove(&vmspace->vm_map, 0, USRSTACK);
371
372 /* Allocate a new stack */
373 error = vm_map_find(&vmspace->vm_map, NULL, 0, (vm_offset_t *)&stack_addr,
374 SGROWSIZ, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
375 if (error)
376 return(error);
377
378 vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
379
380 /* Initialize maximum stack address */
381 vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
382
383 return(0);
384}
385
386/*
387 * Copy out argument and environment strings from the old process
388 * address space into the temporary string buffer.
389 */
390int
391exec_extract_strings(imgp)
392 struct image_params *imgp;
393{
394 char **argv, **envv;
395 char *argp, *envp;
396 int error, length;
397
398 /*
399 * extract arguments first
400 */
401
402 argv = imgp->uap->argv;
403
404 if (argv) {
405 while ((argp = (caddr_t) fuword(argv++))) {
406 if (argp == (caddr_t) -1)
407 return (EFAULT);
408 if ((error = copyinstr(argp, imgp->stringp,
409 imgp->stringspace, &length))) {
410 if (error == ENAMETOOLONG)
411 return(E2BIG);
412 return (error);
413 }
414 imgp->stringspace -= length;
415 imgp->stringp += length;
416 imgp->argc++;
417 }
418 }
419
420 /*
421 * extract environment strings
422 */
423
424 envv = imgp->uap->envv;
425
426 if (envv) {
427 while ((envp = (caddr_t) fuword(envv++))) {
428 if (envp == (caddr_t) -1)
429 return (EFAULT);
430 if ((error = copyinstr(envp, imgp->stringp,
431 imgp->stringspace, &length))) {
432 if (error == ENAMETOOLONG)
433 return(E2BIG);
434 return (error);
435 }
436 imgp->stringspace -= length;
437 imgp->stringp += length;
438 imgp->envc++;
439 }
440 }
441
442 return (0);
443}
444
445/*
446 * Copy strings out to the new process address space, constructing
447 * new arg and env vector tables. Return a pointer to the base
448 * so that it can be used as the initial stack pointer.
449 */
450int *
451exec_copyout_strings(imgp)
452 struct image_params *imgp;
453{
454 int argc, envc;
455 char **vectp;
456 char *stringp, *destp;
457 int *stack_base;
458 struct ps_strings *arginfo;
459 int szsigcode;
460
461 /*
462 * Calculate string base and vector table pointers.
463 * Also deal with signal trampoline code for this exec type.
464 */
465 arginfo = PS_STRINGS;
466 szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
467 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
468 roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
469
470 /*
471 * install sigcode
472 */
473 if (szsigcode)
474 copyout(imgp->proc->p_sysent->sv_sigcode,
475 ((caddr_t)arginfo - szsigcode), szsigcode);
476
477 /*
478 * If we have a valid auxargs ptr, prepare some room
479 * on the stack.
480 */
481 if (imgp->auxargs)
482 /*
483 * The '+ 2' is for the null pointers at the end of each of the
484 * arg and env vector sets, and 'AT_COUNT*2' is room for the
485 * ELF Auxargs data.
486 */
487 vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
488 AT_COUNT*2) * sizeof(char*));
489 else
490 /*
491 * The '+ 2' is for the null pointers at the end of each of the
492 * arg and env vector sets
493 */
494 vectp = (char **)
495 (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
496
497 /*
498 * vectp also becomes our initial stack base
499 */
500 stack_base = (int *)vectp;
501
502 stringp = imgp->stringbase;
503 argc = imgp->argc;
504 envc = imgp->envc;
505
506 /*
507 * Copy out strings - arguments and environment.
508 */
509 copyout(stringp, destp, ARG_MAX - imgp->stringspace);
510
511 /*
512 * Fill in "ps_strings" struct for ps, w, etc.
513 */
514 suword(&arginfo->ps_argvstr, (int)vectp);
515 suword(&arginfo->ps_nargvstr, argc);
516
517 /*
518 * Fill in argument portion of vector table.
519 */
520 for (; argc > 0; --argc) {
521 suword(vectp++, (int)destp);
522 while (*stringp++ != 0)
523 destp++;
524 destp++;
525 }
526
527 /* a null vector table pointer seperates the argp's from the envp's */
528 suword(vectp++, NULL);
529
530 suword(&arginfo->ps_envstr, (int)vectp);
531 suword(&arginfo->ps_nenvstr, envc);
532
533 /*
534 * Fill in environment portion of vector table.
535 */
536 for (; envc > 0; --envc) {
537 suword(vectp++, (int)destp);
538 while (*stringp++ != 0)
539 destp++;
540 destp++;
541 }
542
543 /* end of vector table is a null pointer */
544 suword(vectp, NULL);
545
546 return (stack_base);
547}
548
549/*
550 * Check permissions of file to execute.
551 * Return 0 for success or error code on failure.
552 */
553static int
554exec_check_permissions(imgp)
555 struct image_params *imgp;
556{
557 struct proc *p = imgp->proc;
558 struct vnode *vp = imgp->vp;
559 struct vattr *attr = imgp->attr;
560 int error;
561
562 /*
563 * Check number of open-for-writes on the file and deny execution
564 * if there are any.
565 */
566 if (vp->v_writecount) {
567 return (ETXTBSY);
568 }
569
570 /* Get file attributes */
571 error = VOP_GETATTR(vp, attr, p->p_ucred, p);
572 if (error)
573 return (error);
574
575 /*
576 * 1) Check if file execution is disabled for the filesystem that this
577 * file resides on.
578 * 2) Insure that at least one execute bit is on - otherwise root
579 * will always succeed, and we don't want to happen unless the
580 * file really is executable.
581 * 3) Insure that the file is a regular file.
582 */
583 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
584 ((attr->va_mode & 0111) == 0) ||
585 (attr->va_type != VREG)) {
586 return (EACCES);
587 }
588
589 /*
590 * Zero length files can't be exec'd
591 */
592 if (attr->va_size == 0)
593 return (ENOEXEC);
594
595 /*
596 * Disable setuid/setgid if the filesystem prohibits it or if
597 * the process is being traced.
598 */
599 if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
600 attr->va_mode &= ~(VSUID | VSGID);
601
602 /*
603 * Check for execute permission to file based on current credentials.
604 * Then call filesystem specific open routine (which does nothing
605 * in the general case).
606 */
607 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
608 if (error)
609 return (error);
610
611 error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
612 if (error)
613 return (error);
614
615 return (0);
616}