FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_exec.c
1 /*
2 * Copyright (c) 1993, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: releng/5.0/sys/kern/kern_exec.c 108086 2002-12-19 09:40:13Z alfred $
27 */
28
29 #include "opt_ktrace.h"
30 #include "opt_mac.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/sysproto.h>
37 #include <sys/signalvar.h>
38 #include <sys/kernel.h>
39 #include <sys/mac.h>
40 #include <sys/mount.h>
41 #include <sys/filedesc.h>
42 #include <sys/fcntl.h>
43 #include <sys/acct.h>
44 #include <sys/exec.h>
45 #include <sys/imgact.h>
46 #include <sys/imgact_elf.h>
47 #include <sys/wait.h>
48 #include <sys/malloc.h>
49 #include <sys/proc.h>
50 #include <sys/pioctl.h>
51 #include <sys/namei.h>
52 #include <sys/sysent.h>
53 #include <sys/shm.h>
54 #include <sys/sysctl.h>
55 #include <sys/user.h>
56 #include <sys/vnode.h>
57 #ifdef KTRACE
58 #include <sys/ktrace.h>
59 #endif
60
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 #include <vm/pmap.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_pager.h>
70
71 #include <machine/reg.h>
72
73 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
74
75 static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback");
76
77 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
78 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
79 static int kern_execve(struct thread *td, char *fname, char **argv,
80 char **envv, struct mac *mac_p);
81
82 /*
83 * callout list for things to do at exec time
84 */
85 struct execlist {
86 execlist_fn function;
87 TAILQ_ENTRY(execlist) next;
88 };
89
90 TAILQ_HEAD(exec_list_head, execlist);
91 static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list);
92
93 /* XXX This should be vm_size_t. */
94 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
95 NULL, 0, sysctl_kern_ps_strings, "LU", "");
96
97 /* XXX This should be vm_size_t. */
98 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
99 NULL, 0, sysctl_kern_usrstack, "LU", "");
100
101 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
102 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
103 &ps_arg_cache_limit, 0, "");
104
105 int ps_argsopen = 1;
106 SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
107
108 #ifdef __ia64__
109 /* XXX HACK */
110 static int regstkpages = 256;
111 SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, ®stkpages, 0, "");
112 #endif
113
114 static int
115 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
116 {
117 struct proc *p;
118
119 p = curproc;
120 return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
121 sizeof(p->p_sysent->sv_psstrings)));
122 }
123
124 static int
125 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
126 {
127 struct proc *p;
128
129 p = curproc;
130 return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
131 sizeof(p->p_sysent->sv_usrstack)));
132 }
133
134 /*
135 * Each of the items is a pointer to a `const struct execsw', hence the
136 * double pointer here.
137 */
138 static const struct execsw **execsw;
139
140 /*
141 * In-kernel implementation of execve(). All arguments are assumed to be
142 * userspace pointers from the passed thread.
143 *
144 * MPSAFE
145 */
146 static int
147 kern_execve(td, fname, argv, envv, mac_p)
148 struct thread *td;
149 char *fname;
150 char **argv;
151 char **envv;
152 struct mac *mac_p;
153 {
154 struct proc *p = td->td_proc;
155 struct nameidata nd, *ndp;
156 struct ucred *newcred = NULL, *oldcred;
157 struct uidinfo *euip;
158 register_t *stack_base;
159 int error, len, i;
160 struct image_params image_params, *imgp;
161 struct vattr attr;
162 int (*img_first)(struct image_params *);
163 struct pargs *oldargs = NULL, *newargs = NULL;
164 struct procsig *oldprocsig, *newprocsig;
165 #ifdef KTRACE
166 struct vnode *tracevp = NULL;
167 #endif
168 struct vnode *textvp = NULL;
169 int credential_changing;
170 int textset;
171 #ifdef MAC
172 struct label interplabel; /* label of the interpreted vnode */
173 struct label execlabel; /* optional label argument */
174 int will_transition, interplabelvalid = 0;
175 #endif
176
177 imgp = &image_params;
178
179 /*
180 * Lock the process and set the P_INEXEC flag to indicate that
181 * it should be left alone until we're done here. This is
182 * necessary to avoid race conditions - e.g. in ptrace() -
183 * that might allow a local user to illicitly obtain elevated
184 * privileges.
185 */
186 PROC_LOCK(p);
187 KASSERT((p->p_flag & P_INEXEC) == 0,
188 ("%s(): process already has P_INEXEC flag", __func__));
189 if (p->p_flag & P_KSES) {
190 if (thread_single(SINGLE_EXIT)) {
191 PROC_UNLOCK(p);
192 return (ERESTART); /* Try again later. */
193 }
194 /*
195 * If we get here all other threads are dead,
196 * so unset the associated flags and lose KSE mode.
197 */
198 p->p_flag &= ~P_KSES;
199 td->td_flags &= ~TDF_UNBOUND;
200 thread_single_end();
201 }
202 p->p_flag |= P_INEXEC;
203 PROC_UNLOCK(p);
204
205 /*
206 * Initialize part of the common data
207 */
208 imgp->proc = p;
209 imgp->userspace_argv = argv;
210 imgp->userspace_envv = envv;
211 imgp->execlabel = NULL;
212 imgp->attr = &attr;
213 imgp->argc = imgp->envc = 0;
214 imgp->argv0 = NULL;
215 imgp->entry_addr = 0;
216 imgp->vmspace_destroyed = 0;
217 imgp->interpreted = 0;
218 imgp->interpreter_name[0] = '\0';
219 imgp->auxargs = NULL;
220 imgp->vp = NULL;
221 imgp->object = NULL;
222 imgp->firstpage = NULL;
223 imgp->ps_strings = 0;
224 imgp->auxarg_size = 0;
225
226 #ifdef MAC
227 error = mac_execve_enter(imgp, mac_p, &execlabel);
228 if (error) {
229 mtx_lock(&Giant);
230 goto exec_fail;
231 }
232 #endif
233
234 /*
235 * Allocate temporary demand zeroed space for argument and
236 * environment strings
237 */
238 imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX +
239 PAGE_SIZE);
240 if (imgp->stringbase == NULL) {
241 error = ENOMEM;
242 mtx_lock(&Giant);
243 goto exec_fail;
244 }
245 imgp->stringp = imgp->stringbase;
246 imgp->stringspace = ARG_MAX;
247 imgp->image_header = imgp->stringbase + ARG_MAX;
248
249 /*
250 * Translate the file name. namei() returns a vnode pointer
251 * in ni_vp amoung other things.
252 */
253 ndp = &nd;
254 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
255 UIO_USERSPACE, fname, td);
256
257 mtx_lock(&Giant);
258 interpret:
259
260 error = namei(ndp);
261 if (error) {
262 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
263 ARG_MAX + PAGE_SIZE);
264 goto exec_fail;
265 }
266
267 imgp->vp = ndp->ni_vp;
268 imgp->fname = fname;
269
270 /*
271 * Check file permissions (also 'opens' file)
272 */
273 error = exec_check_permissions(imgp);
274 if (error)
275 goto exec_fail_dealloc;
276
277 if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0)
278 vm_object_reference(imgp->object);
279
280 /*
281 * Set VV_TEXT now so no one can write to the executable while we're
282 * activating it.
283 *
284 * Remember if this was set before and unset it in case this is not
285 * actually an executable image.
286 */
287 textset = imgp->vp->v_vflag & VV_TEXT;
288 imgp->vp->v_vflag |= VV_TEXT;
289
290 error = exec_map_first_page(imgp);
291 if (error)
292 goto exec_fail_dealloc;
293
294 /*
295 * If the current process has a special image activator it
296 * wants to try first, call it. For example, emulating shell
297 * scripts differently.
298 */
299 error = -1;
300 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
301 error = img_first(imgp);
302
303 /*
304 * Loop through the list of image activators, calling each one.
305 * An activator returns -1 if there is no match, 0 on success,
306 * and an error otherwise.
307 */
308 for (i = 0; error == -1 && execsw[i]; ++i) {
309 if (execsw[i]->ex_imgact == NULL ||
310 execsw[i]->ex_imgact == img_first) {
311 continue;
312 }
313 error = (*execsw[i]->ex_imgact)(imgp);
314 }
315
316 if (error) {
317 if (error == -1) {
318 if (textset == 0)
319 imgp->vp->v_vflag &= ~VV_TEXT;
320 error = ENOEXEC;
321 }
322 goto exec_fail_dealloc;
323 }
324
325 /*
326 * Special interpreter operation, cleanup and loop up to try to
327 * activate the interpreter.
328 */
329 if (imgp->interpreted) {
330 exec_unmap_first_page(imgp);
331 /*
332 * VV_TEXT needs to be unset for scripts. There is a short
333 * period before we determine that something is a script where
334 * VV_TEXT will be set. The vnode lock is held over this
335 * entire period so nothing should illegitimately be blocked.
336 */
337 imgp->vp->v_vflag &= ~VV_TEXT;
338 /* free name buffer and old vnode */
339 NDFREE(ndp, NDF_ONLY_PNBUF);
340 #ifdef MAC
341 mac_init_vnode_label(&interplabel);
342 mac_copy_vnode_label(&ndp->ni_vp->v_label, &interplabel);
343 interplabelvalid = 1;
344 #endif
345 vput(ndp->ni_vp);
346 vm_object_deallocate(imgp->object);
347 imgp->object = NULL;
348 /* set new name to that of the interpreter */
349 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
350 UIO_SYSSPACE, imgp->interpreter_name, td);
351 goto interpret;
352 }
353
354 /*
355 * Copy out strings (args and env) and initialize stack base
356 */
357 if (p->p_sysent->sv_copyout_strings)
358 stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
359 else
360 stack_base = exec_copyout_strings(imgp);
361
362 /*
363 * If custom stack fixup routine present for this process
364 * let it do the stack setup.
365 * Else stuff argument count as first item on stack
366 */
367 if (p->p_sysent->sv_fixup)
368 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
369 else
370 suword(--stack_base, imgp->argc);
371
372 /*
373 * For security and other reasons, the file descriptor table cannot
374 * be shared after an exec.
375 */
376 FILEDESC_LOCK(p->p_fd);
377 if (p->p_fd->fd_refcnt > 1) {
378 struct filedesc *tmp;
379
380 tmp = fdcopy(td);
381 FILEDESC_UNLOCK(p->p_fd);
382 fdfree(td);
383 p->p_fd = tmp;
384 } else
385 FILEDESC_UNLOCK(p->p_fd);
386
387 /*
388 * Malloc things before we need locks.
389 */
390 newcred = crget();
391 euip = uifind(attr.va_uid);
392 i = imgp->endargs - imgp->stringbase;
393 if (ps_arg_cache_limit >= i + sizeof(struct pargs))
394 newargs = pargs_alloc(i);
395
396 /* close files on exec */
397 fdcloseexec(td);
398
399 /* Get a reference to the vnode prior to locking the proc */
400 VREF(ndp->ni_vp);
401
402 /*
403 * For security and other reasons, signal handlers cannot
404 * be shared after an exec. The new process gets a copy of the old
405 * handlers. In execsigs(), the new process will have its signals
406 * reset.
407 */
408 PROC_LOCK(p);
409 mp_fixme("procsig needs a lock");
410 if (p->p_procsig->ps_refcnt > 1) {
411 oldprocsig = p->p_procsig;
412 PROC_UNLOCK(p);
413 MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
414 M_SUBPROC, M_WAITOK);
415 bcopy(oldprocsig, newprocsig, sizeof(*newprocsig));
416 newprocsig->ps_refcnt = 1;
417 oldprocsig->ps_refcnt--;
418 PROC_LOCK(p);
419 p->p_procsig = newprocsig;
420 if (p->p_sigacts == &p->p_uarea->u_sigacts)
421 panic("shared procsig but private sigacts?");
422
423 p->p_uarea->u_sigacts = *p->p_sigacts;
424 p->p_sigacts = &p->p_uarea->u_sigacts;
425 }
426 /* Stop profiling */
427 stopprofclock(p);
428
429 /* reset caught signals */
430 execsigs(p);
431
432 /* name this process - nameiexec(p, ndp) */
433 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
434 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
435 p->p_comm[len] = 0;
436
437 /*
438 * mark as execed, wakeup the process that vforked (if any) and tell
439 * it that it now has its own resources back
440 */
441 p->p_flag |= P_EXEC;
442 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
443 p->p_flag &= ~P_PPWAIT;
444 wakeup(p->p_pptr);
445 }
446
447 /*
448 * Implement image setuid/setgid.
449 *
450 * Don't honor setuid/setgid if the filesystem prohibits it or if
451 * the process is being traced.
452 *
453 * XXXMAC: For the time being, use NOSUID to also prohibit
454 * transitions on the file system.
455 */
456 oldcred = p->p_ucred;
457 credential_changing = 0;
458 credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
459 attr.va_uid;
460 credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
461 attr.va_gid;
462 #ifdef MAC
463 will_transition = mac_execve_will_transition(oldcred, imgp->vp,
464 interplabelvalid ? &interplabel : NULL, imgp);
465 credential_changing |= will_transition;
466 #endif
467
468 if (credential_changing &&
469 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
470 (p->p_flag & P_TRACED) == 0) {
471 /*
472 * Turn off syscall tracing for set-id programs, except for
473 * root. Record any set-id flags first to make sure that
474 * we do not regain any tracing during a possible block.
475 */
476 setsugid(p);
477 #ifdef KTRACE
478 if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) {
479 mtx_lock(&ktrace_mtx);
480 p->p_traceflag = 0;
481 tracevp = p->p_tracep;
482 p->p_tracep = NULL;
483 mtx_unlock(&ktrace_mtx);
484 }
485 #endif
486 /*
487 * Close any file descriptors 0..2 that reference procfs,
488 * then make sure file descriptors 0..2 are in use.
489 *
490 * setugidsafety() may call closef() and then pfind()
491 * which may grab the process lock.
492 * fdcheckstd() may call falloc() which may block to
493 * allocate memory, so temporarily drop the process lock.
494 */
495 PROC_UNLOCK(p);
496 setugidsafety(td);
497 error = fdcheckstd(td);
498 if (error != 0)
499 goto done1;
500 PROC_LOCK(p);
501 /*
502 * Set the new credentials.
503 */
504 crcopy(newcred, oldcred);
505 if (attr.va_mode & VSUID)
506 change_euid(newcred, euip);
507 if (attr.va_mode & VSGID)
508 change_egid(newcred, attr.va_gid);
509 #ifdef MAC
510 if (will_transition) {
511 mac_execve_transition(oldcred, newcred, imgp->vp,
512 interplabelvalid ? &interplabel : NULL, imgp);
513 }
514 #endif
515 /*
516 * Implement correct POSIX saved-id behavior.
517 *
518 * XXXMAC: Note that the current logic will save the
519 * uid and gid if a MAC domain transition occurs, even
520 * though maybe it shouldn't.
521 */
522 change_svuid(newcred, newcred->cr_uid);
523 change_svgid(newcred, newcred->cr_gid);
524 p->p_ucred = newcred;
525 newcred = NULL;
526 } else {
527 if (oldcred->cr_uid == oldcred->cr_ruid &&
528 oldcred->cr_gid == oldcred->cr_rgid)
529 p->p_flag &= ~P_SUGID;
530 /*
531 * Implement correct POSIX saved-id behavior.
532 *
533 * XXX: It's not clear that the existing behavior is
534 * POSIX-compliant. A number of sources indicate that the
535 * saved uid/gid should only be updated if the new ruid is
536 * not equal to the old ruid, or the new euid is not equal
537 * to the old euid and the new euid is not equal to the old
538 * ruid. The FreeBSD code always updates the saved uid/gid.
539 * Also, this code uses the new (replaced) euid and egid as
540 * the source, which may or may not be the right ones to use.
541 */
542 if (oldcred->cr_svuid != oldcred->cr_uid ||
543 oldcred->cr_svgid != oldcred->cr_gid) {
544 crcopy(newcred, oldcred);
545 change_svuid(newcred, newcred->cr_uid);
546 change_svgid(newcred, newcred->cr_gid);
547 p->p_ucred = newcred;
548 newcred = NULL;
549 }
550 }
551
552 /*
553 * Store the vp for use in procfs. This vnode was referenced prior
554 * to locking the proc lock.
555 */
556 textvp = p->p_textvp;
557 p->p_textvp = ndp->ni_vp;
558
559 /*
560 * Notify others that we exec'd, and clear the P_INEXEC flag
561 * as we're now a bona fide freshly-execed process.
562 */
563 KNOTE(&p->p_klist, NOTE_EXEC);
564 p->p_flag &= ~P_INEXEC;
565
566 /*
567 * If tracing the process, trap to debugger so breakpoints
568 * can be set before the program executes.
569 */
570 if (p->p_flag & P_TRACED)
571 psignal(p, SIGTRAP);
572
573 /* clear "fork but no exec" flag, as we _are_ execing */
574 p->p_acflag &= ~AFORK;
575
576 /* Free any previous argument cache */
577 oldargs = p->p_args;
578 p->p_args = NULL;
579
580 /* Cache arguments if they fit inside our allowance */
581 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
582 bcopy(imgp->stringbase, newargs->ar_args, i);
583 p->p_args = newargs;
584 newargs = NULL;
585 }
586 PROC_UNLOCK(p);
587
588 /* Set values passed into the program in registers. */
589 if (p->p_sysent->sv_setregs)
590 (*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
591 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
592 else
593 exec_setregs(td, imgp->entry_addr,
594 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
595
596 done1:
597 /*
598 * Free any resources malloc'd earlier that we didn't use.
599 */
600 uifree(euip);
601 if (newcred == NULL)
602 crfree(oldcred);
603 else
604 crfree(newcred);
605 /*
606 * Handle deferred decrement of ref counts.
607 */
608 if (textvp != NULL)
609 vrele(textvp);
610 if (ndp->ni_vp && error != 0)
611 vrele(ndp->ni_vp);
612 #ifdef KTRACE
613 if (tracevp != NULL)
614 vrele(tracevp);
615 #endif
616 if (oldargs != NULL)
617 pargs_drop(oldargs);
618 if (newargs != NULL)
619 pargs_drop(newargs);
620
621 exec_fail_dealloc:
622
623 /*
624 * free various allocated resources
625 */
626 if (imgp->firstpage)
627 exec_unmap_first_page(imgp);
628
629 if (imgp->vp) {
630 NDFREE(ndp, NDF_ONLY_PNBUF);
631 vput(imgp->vp);
632 }
633
634 if (imgp->stringbase != NULL)
635 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
636 ARG_MAX + PAGE_SIZE);
637
638 if (imgp->object)
639 vm_object_deallocate(imgp->object);
640
641 if (error == 0) {
642 /*
643 * Stop the process here if its stop event mask has
644 * the S_EXEC bit set.
645 */
646 STOPEVENT(p, S_EXEC, 0);
647 goto done2;
648 }
649
650 exec_fail:
651 /* we're done here, clear P_INEXEC */
652 PROC_LOCK(p);
653 p->p_flag &= ~P_INEXEC;
654 PROC_UNLOCK(p);
655
656 if (imgp->vmspace_destroyed) {
657 /* sorry, no more process anymore. exit gracefully */
658 #ifdef MAC
659 mac_execve_exit(imgp);
660 if (interplabelvalid)
661 mac_destroy_vnode_label(&interplabel);
662 #endif
663 exit1(td, W_EXITCODE(0, SIGABRT));
664 /* NOT REACHED */
665 error = 0;
666 }
667 done2:
668 #ifdef MAC
669 mac_execve_exit(imgp);
670 if (interplabelvalid)
671 mac_destroy_vnode_label(&interplabel);
672 #endif
673 mtx_unlock(&Giant);
674 return (error);
675 }
676
677 #ifndef _SYS_SYSPROTO_H_
678 struct execve_args {
679 char *fname;
680 char **argv;
681 char **envv;
682 };
683 #endif
684
685 /*
686 * MPSAFE
687 */
688 int
689 execve(td, uap)
690 struct thread *td;
691 struct execve_args /* {
692 char *fname;
693 char **argv;
694 char **envv;
695 } */ *uap;
696 {
697
698 return (kern_execve(td, uap->fname, uap->argv, uap->envv, NULL));
699 }
700
701 #ifndef _SYS_SYSPROTO_H_
702 struct __mac_execve_args {
703 char *fname;
704 char **argv;
705 char **envv;
706 struct mac *mac_p;
707 };
708 #endif
709
710 /*
711 * MPSAFE
712 */
713 int
714 __mac_execve(td, uap)
715 struct thread *td;
716 struct __mac_execve_args /* {
717 char *fname;
718 char **argv;
719 char **envv;
720 struct mac *mac_p;
721 } */ *uap;
722 {
723
724 #ifdef MAC
725 return (kern_execve(td, uap->fname, uap->argv, uap->envv,
726 uap->mac_p));
727 #else
728 return (ENOSYS);
729 #endif
730 }
731
732 int
733 exec_map_first_page(imgp)
734 struct image_params *imgp;
735 {
736 int rv, i;
737 int initial_pagein;
738 vm_page_t ma[VM_INITIAL_PAGEIN];
739 vm_object_t object;
740
741 GIANT_REQUIRED;
742
743 if (imgp->firstpage) {
744 exec_unmap_first_page(imgp);
745 }
746
747 VOP_GETVOBJECT(imgp->vp, &object);
748
749 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
750
751 if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
752 initial_pagein = VM_INITIAL_PAGEIN;
753 if (initial_pagein > object->size)
754 initial_pagein = object->size;
755 for (i = 1; i < initial_pagein; i++) {
756 if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
757 if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
758 break;
759 if (ma[i]->valid)
760 break;
761 vm_page_busy(ma[i]);
762 } else {
763 ma[i] = vm_page_alloc(object, i,
764 VM_ALLOC_NORMAL);
765 if (ma[i] == NULL)
766 break;
767 }
768 }
769 initial_pagein = i;
770
771 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
772 ma[0] = vm_page_lookup(object, 0);
773
774 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
775 (ma[0]->valid == 0)) {
776 if (ma[0]) {
777 vm_page_lock_queues();
778 pmap_remove_all(ma[0]);
779 vm_page_free(ma[0]);
780 vm_page_unlock_queues();
781 }
782 return (EIO);
783 }
784 }
785 vm_page_lock_queues();
786 vm_page_wire(ma[0]);
787 vm_page_wakeup(ma[0]);
788 vm_page_unlock_queues();
789
790 pmap_qenter((vm_offset_t)imgp->image_header, ma, 1);
791 imgp->firstpage = ma[0];
792
793 return (0);
794 }
795
796 void
797 exec_unmap_first_page(imgp)
798 struct image_params *imgp;
799 {
800 GIANT_REQUIRED;
801
802 if (imgp->firstpage) {
803 pmap_qremove((vm_offset_t)imgp->image_header, 1);
804 vm_page_lock_queues();
805 vm_page_unwire(imgp->firstpage, 1);
806 vm_page_unlock_queues();
807 imgp->firstpage = NULL;
808 }
809 }
810
811 /*
812 * Destroy old address space, and allocate a new stack
813 * The new stack is only SGROWSIZ large because it is grown
814 * automatically in trap.c.
815 */
816 int
817 exec_new_vmspace(imgp, sv)
818 struct image_params *imgp;
819 struct sysentvec *sv;
820 {
821 int error;
822 struct execlist *ep;
823 struct proc *p = imgp->proc;
824 struct vmspace *vmspace = p->p_vmspace;
825 vm_offset_t stack_addr;
826 vm_map_t map;
827
828 GIANT_REQUIRED;
829
830 stack_addr = sv->sv_usrstack - maxssiz;
831
832 imgp->vmspace_destroyed = 1;
833
834 /*
835 * Perform functions registered with at_exec().
836 */
837 TAILQ_FOREACH(ep, &exec_list, next)
838 (*ep->function)(p);
839
840 /*
841 * Blow away entire process VM, if address space not shared,
842 * otherwise, create a new VM space so that other threads are
843 * not disrupted
844 */
845 map = &vmspace->vm_map;
846 if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser &&
847 vm_map_max(map) == sv->sv_maxuser) {
848 if (vmspace->vm_shm)
849 shmexit(p);
850 vm_page_lock_queues();
851 pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map),
852 vm_map_max(map));
853 vm_page_unlock_queues();
854 vm_map_remove(map, vm_map_min(map), vm_map_max(map));
855 } else {
856 vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser);
857 vmspace = p->p_vmspace;
858 map = &vmspace->vm_map;
859 }
860
861 /* Allocate a new stack */
862 error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz,
863 sv->sv_stackprot, VM_PROT_ALL, 0);
864 if (error)
865 return (error);
866
867 #ifdef __ia64__
868 {
869 /*
870 * Allocate backing store. We really need something
871 * similar to vm_map_stack which can allow the backing
872 * store to grow upwards. This will do for now.
873 */
874 vm_offset_t bsaddr;
875 bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz;
876 error = vm_map_find(map, 0, 0, &bsaddr,
877 regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0);
878 FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr;
879 }
880 #endif
881
882 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
883 * VM_STACK case, but they are still used to monitor the size of the
884 * process stack so we can check the stack rlimit.
885 */
886 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
887 vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz;
888
889 return (0);
890 }
891
892 /*
893 * Copy out argument and environment strings from the old process
894 * address space into the temporary string buffer.
895 */
896 int
897 exec_extract_strings(imgp)
898 struct image_params *imgp;
899 {
900 char **argv, **envv;
901 char *argp, *envp;
902 int error;
903 size_t length;
904
905 /*
906 * extract arguments first
907 */
908
909 argv = imgp->userspace_argv;
910
911 if (argv) {
912 argp = (caddr_t)(intptr_t)fuword(argv);
913 if (argp == (caddr_t)-1)
914 return (EFAULT);
915 if (argp)
916 argv++;
917 if (imgp->argv0)
918 argp = imgp->argv0;
919 if (argp) {
920 do {
921 if (argp == (caddr_t)-1)
922 return (EFAULT);
923 if ((error = copyinstr(argp, imgp->stringp,
924 imgp->stringspace, &length))) {
925 if (error == ENAMETOOLONG)
926 return (E2BIG);
927 return (error);
928 }
929 imgp->stringspace -= length;
930 imgp->stringp += length;
931 imgp->argc++;
932 } while ((argp = (caddr_t)(intptr_t)fuword(argv++)));
933 }
934 }
935
936 imgp->endargs = imgp->stringp;
937
938 /*
939 * extract environment strings
940 */
941
942 envv = imgp->userspace_envv;
943
944 if (envv) {
945 while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
946 if (envp == (caddr_t)-1)
947 return (EFAULT);
948 if ((error = copyinstr(envp, imgp->stringp,
949 imgp->stringspace, &length))) {
950 if (error == ENAMETOOLONG)
951 return (E2BIG);
952 return (error);
953 }
954 imgp->stringspace -= length;
955 imgp->stringp += length;
956 imgp->envc++;
957 }
958 }
959
960 return (0);
961 }
962
963 /*
964 * Copy strings out to the new process address space, constructing
965 * new arg and env vector tables. Return a pointer to the base
966 * so that it can be used as the initial stack pointer.
967 */
968 register_t *
969 exec_copyout_strings(imgp)
970 struct image_params *imgp;
971 {
972 int argc, envc;
973 char **vectp;
974 char *stringp, *destp;
975 register_t *stack_base;
976 struct ps_strings *arginfo;
977 struct proc *p;
978 int szsigcode;
979
980 /*
981 * Calculate string base and vector table pointers.
982 * Also deal with signal trampoline code for this exec type.
983 */
984 p = imgp->proc;
985 szsigcode = 0;
986 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
987 if (p->p_sysent->sv_szsigcode != NULL)
988 szsigcode = *(p->p_sysent->sv_szsigcode);
989 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
990 roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
991
992 /*
993 * install sigcode
994 */
995 if (szsigcode)
996 copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
997 szsigcode), szsigcode);
998
999 /*
1000 * If we have a valid auxargs ptr, prepare some room
1001 * on the stack.
1002 */
1003 if (imgp->auxargs) {
1004 /*
1005 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1006 * lower compatibility.
1007 */
1008 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1009 (AT_COUNT * 2);
1010 /*
1011 * The '+ 2' is for the null pointers at the end of each of
1012 * the arg and env vector sets,and imgp->auxarg_size is room
1013 * for argument of Runtime loader.
1014 */
1015 vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
1016 imgp->auxarg_size) * sizeof(char *));
1017
1018 } else
1019 /*
1020 * The '+ 2' is for the null pointers at the end of each of
1021 * the arg and env vector sets
1022 */
1023 vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) *
1024 sizeof(char *));
1025
1026 /*
1027 * vectp also becomes our initial stack base
1028 */
1029 stack_base = (register_t *)vectp;
1030
1031 stringp = imgp->stringbase;
1032 argc = imgp->argc;
1033 envc = imgp->envc;
1034
1035 /*
1036 * Copy out strings - arguments and environment.
1037 */
1038 copyout(stringp, destp, ARG_MAX - imgp->stringspace);
1039
1040 /*
1041 * Fill in "ps_strings" struct for ps, w, etc.
1042 */
1043 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
1044 suword(&arginfo->ps_nargvstr, argc);
1045
1046 /*
1047 * Fill in argument portion of vector table.
1048 */
1049 for (; argc > 0; --argc) {
1050 suword(vectp++, (long)(intptr_t)destp);
1051 while (*stringp++ != 0)
1052 destp++;
1053 destp++;
1054 }
1055
1056 /* a null vector table pointer separates the argp's from the envp's */
1057 suword(vectp++, 0);
1058
1059 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
1060 suword(&arginfo->ps_nenvstr, envc);
1061
1062 /*
1063 * Fill in environment portion of vector table.
1064 */
1065 for (; envc > 0; --envc) {
1066 suword(vectp++, (long)(intptr_t)destp);
1067 while (*stringp++ != 0)
1068 destp++;
1069 destp++;
1070 }
1071
1072 /* end of vector table is a null pointer */
1073 suword(vectp, 0);
1074
1075 return (stack_base);
1076 }
1077
1078 /*
1079 * Check permissions of file to execute.
1080 * Called with imgp->vp locked.
1081 * Return 0 for success or error code on failure.
1082 */
1083 int
1084 exec_check_permissions(imgp)
1085 struct image_params *imgp;
1086 {
1087 struct vnode *vp = imgp->vp;
1088 struct vattr *attr = imgp->attr;
1089 struct thread *td;
1090 int error;
1091
1092 td = curthread; /* XXXKSE */
1093
1094 #ifdef MAC
1095 error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1096 if (error)
1097 return (error);
1098 #endif
1099
1100 /* Get file attributes */
1101 error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1102 if (error)
1103 return (error);
1104
1105 /*
1106 * 1) Check if file execution is disabled for the filesystem that this
1107 * file resides on.
1108 * 2) Insure that at least one execute bit is on - otherwise root
1109 * will always succeed, and we don't want to happen unless the
1110 * file really is executable.
1111 * 3) Insure that the file is a regular file.
1112 */
1113 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
1114 ((attr->va_mode & 0111) == 0) ||
1115 (attr->va_type != VREG))
1116 return (EACCES);
1117
1118 /*
1119 * Zero length files can't be exec'd
1120 */
1121 if (attr->va_size == 0)
1122 return (ENOEXEC);
1123
1124 /*
1125 * Check for execute permission to file based on current credentials.
1126 */
1127 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1128 if (error)
1129 return (error);
1130
1131 /*
1132 * Check number of open-for-writes on the file and deny execution
1133 * if there are any.
1134 */
1135 if (vp->v_writecount)
1136 return (ETXTBSY);
1137
1138 /*
1139 * Call filesystem specific open routine (which does nothing in the
1140 * general case).
1141 */
1142 error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
1143 return (error);
1144 }
1145
1146 /*
1147 * Exec handler registration
1148 */
1149 int
1150 exec_register(execsw_arg)
1151 const struct execsw *execsw_arg;
1152 {
1153 const struct execsw **es, **xs, **newexecsw;
1154 int count = 2; /* New slot and trailing NULL */
1155
1156 if (execsw)
1157 for (es = execsw; *es; es++)
1158 count++;
1159 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1160 if (newexecsw == NULL)
1161 return (ENOMEM);
1162 xs = newexecsw;
1163 if (execsw)
1164 for (es = execsw; *es; es++)
1165 *xs++ = *es;
1166 *xs++ = execsw_arg;
1167 *xs = NULL;
1168 if (execsw)
1169 free(execsw, M_TEMP);
1170 execsw = newexecsw;
1171 return (0);
1172 }
1173
1174 int
1175 exec_unregister(execsw_arg)
1176 const struct execsw *execsw_arg;
1177 {
1178 const struct execsw **es, **xs, **newexecsw;
1179 int count = 1;
1180
1181 if (execsw == NULL)
1182 panic("unregister with no handlers left?\n");
1183
1184 for (es = execsw; *es; es++) {
1185 if (*es == execsw_arg)
1186 break;
1187 }
1188 if (*es == NULL)
1189 return (ENOENT);
1190 for (es = execsw; *es; es++)
1191 if (*es != execsw_arg)
1192 count++;
1193 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1194 if (newexecsw == NULL)
1195 return (ENOMEM);
1196 xs = newexecsw;
1197 for (es = execsw; *es; es++)
1198 if (*es != execsw_arg)
1199 *xs++ = *es;
1200 *xs = NULL;
1201 if (execsw)
1202 free(execsw, M_TEMP);
1203 execsw = newexecsw;
1204 return (0);
1205 }
1206
1207 int
1208 at_exec(function)
1209 execlist_fn function;
1210 {
1211 struct execlist *ep;
1212
1213 #ifdef INVARIANTS
1214 /* Be noisy if the programmer has lost track of things */
1215 if (rm_at_exec(function))
1216 printf("WARNING: exec callout entry (%p) already present\n",
1217 function);
1218 #endif
1219 ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT);
1220 if (ep == NULL)
1221 return (ENOMEM);
1222 ep->function = function;
1223 TAILQ_INSERT_TAIL(&exec_list, ep, next);
1224 return (0);
1225 }
1226
1227 /*
1228 * Scan the exec callout list for the given item and remove it.
1229 * Returns the number of items removed (0 or 1)
1230 */
1231 int
1232 rm_at_exec(function)
1233 execlist_fn function;
1234 {
1235 struct execlist *ep;
1236
1237 TAILQ_FOREACH(ep, &exec_list, next) {
1238 if (ep->function == function) {
1239 TAILQ_REMOVE(&exec_list, ep, next);
1240 free(ep, M_ATEXEC);
1241 return (1);
1242 }
1243 }
1244 return (0);
1245 }
Cache object: 4a1c9cee7c836964461d520c5bff923c
|