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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/7.3/sys/kern/kern_exec.c 202765 2010-01-21 19:17:42Z jhb $");
29
30 #include "opt_hwpmc_hooks.h"
31 #include "opt_kdtrace.h"
32 #include "opt_ktrace.h"
33 #include "opt_mac.h"
34 #include "opt_vm.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/eventhandler.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/sysproto.h>
42 #include <sys/signalvar.h>
43 #include <sys/kernel.h>
44 #include <sys/mount.h>
45 #include <sys/filedesc.h>
46 #include <sys/fcntl.h>
47 #include <sys/acct.h>
48 #include <sys/exec.h>
49 #include <sys/imgact.h>
50 #include <sys/imgact_elf.h>
51 #include <sys/wait.h>
52 #include <sys/malloc.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/pioctl.h>
56 #include <sys/namei.h>
57 #include <sys/resourcevar.h>
58 #include <sys/sdt.h>
59 #include <sys/sf_buf.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysent.h>
62 #include <sys/shm.h>
63 #include <sys/sysctl.h>
64 #include <sys/vnode.h>
65 #ifdef KTRACE
66 #include <sys/ktrace.h>
67 #endif
68
69 #include <vm/vm.h>
70 #include <vm/vm_param.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_pager.h>
78
79 #ifdef HWPMC_HOOKS
80 #include <sys/pmckern.h>
81 #endif
82
83 #include <machine/reg.h>
84
85 #include <security/audit/audit.h>
86 #include <security/mac/mac_framework.h>
87
88 #ifdef KDTRACE_HOOKS
89 #include <sys/dtrace_bsd.h>
90 dtrace_execexit_func_t dtrace_fasttrap_exec;
91 #endif
92
93 SDT_PROVIDER_DECLARE(proc);
94 SDT_PROBE_DEFINE(proc, kernel, , exec);
95 SDT_PROBE_ARGTYPE(proc, kernel, , exec, 0, "char *");
96 SDT_PROBE_DEFINE(proc, kernel, , exec_failure);
97 SDT_PROBE_ARGTYPE(proc, kernel, , exec_failure, 0, "int");
98 SDT_PROBE_DEFINE(proc, kernel, , exec_success);
99 SDT_PROBE_ARGTYPE(proc, kernel, , exec_success, 0, "char *");
100
101 MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
102
103 static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS);
104 static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS);
105 static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS);
106 static int do_execve(struct thread *td, struct image_args *args,
107 struct mac *mac_p);
108 static void exec_free_args(struct image_args *);
109
110 /* XXX This should be vm_size_t. */
111 SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD,
112 NULL, 0, sysctl_kern_ps_strings, "LU", "");
113
114 /* XXX This should be vm_size_t. */
115 SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD,
116 NULL, 0, sysctl_kern_usrstack, "LU", "");
117
118 SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD,
119 NULL, 0, sysctl_kern_stackprot, "I", "");
120
121 u_long ps_arg_cache_limit = PAGE_SIZE / 16;
122 SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
123 &ps_arg_cache_limit, 0, "");
124
125 static int map_at_zero = 1;
126 TUNABLE_INT("security.bsd.map_at_zero", &map_at_zero);
127 SYSCTL_INT(_security_bsd, OID_AUTO, map_at_zero, CTLFLAG_RW, &map_at_zero, 0,
128 "Permit processes to map an object at virtual address 0.");
129
130 static int
131 sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS)
132 {
133 struct proc *p;
134 int error;
135
136 p = curproc;
137 #ifdef SCTL_MASK32
138 if (req->flags & SCTL_MASK32) {
139 unsigned int val;
140 val = (unsigned int)p->p_sysent->sv_psstrings;
141 error = SYSCTL_OUT(req, &val, sizeof(val));
142 } else
143 #endif
144 error = SYSCTL_OUT(req, &p->p_sysent->sv_psstrings,
145 sizeof(p->p_sysent->sv_psstrings));
146 return error;
147 }
148
149 static int
150 sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS)
151 {
152 struct proc *p;
153 int error;
154
155 p = curproc;
156 #ifdef SCTL_MASK32
157 if (req->flags & SCTL_MASK32) {
158 unsigned int val;
159 val = (unsigned int)p->p_sysent->sv_usrstack;
160 error = SYSCTL_OUT(req, &val, sizeof(val));
161 } else
162 #endif
163 error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack,
164 sizeof(p->p_sysent->sv_usrstack));
165 return error;
166 }
167
168 static int
169 sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS)
170 {
171 struct proc *p;
172
173 p = curproc;
174 return (SYSCTL_OUT(req, &p->p_sysent->sv_stackprot,
175 sizeof(p->p_sysent->sv_stackprot)));
176 }
177
178 /*
179 * Each of the items is a pointer to a `const struct execsw', hence the
180 * double pointer here.
181 */
182 static const struct execsw **execsw;
183
184 #ifndef _SYS_SYSPROTO_H_
185 struct execve_args {
186 char *fname;
187 char **argv;
188 char **envv;
189 };
190 #endif
191
192 int
193 execve(td, uap)
194 struct thread *td;
195 struct execve_args /* {
196 char *fname;
197 char **argv;
198 char **envv;
199 } */ *uap;
200 {
201 int error;
202 struct image_args args;
203
204 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
205 uap->argv, uap->envv);
206 if (error == 0)
207 error = kern_execve(td, &args, NULL);
208 return (error);
209 }
210
211 #ifndef _SYS_SYSPROTO_H_
212 struct __mac_execve_args {
213 char *fname;
214 char **argv;
215 char **envv;
216 struct mac *mac_p;
217 };
218 #endif
219
220 int
221 __mac_execve(td, uap)
222 struct thread *td;
223 struct __mac_execve_args /* {
224 char *fname;
225 char **argv;
226 char **envv;
227 struct mac *mac_p;
228 } */ *uap;
229 {
230 #ifdef MAC
231 int error;
232 struct image_args args;
233
234 error = exec_copyin_args(&args, uap->fname, UIO_USERSPACE,
235 uap->argv, uap->envv);
236 if (error == 0)
237 error = kern_execve(td, &args, uap->mac_p);
238 return (error);
239 #else
240 return (ENOSYS);
241 #endif
242 }
243
244 /*
245 * XXX: kern_execve has the astonishing property of not always returning to
246 * the caller. If sufficiently bad things happen during the call to
247 * do_execve(), it can end up calling exit1(); as a result, callers must
248 * avoid doing anything which they might need to undo (e.g., allocating
249 * memory).
250 */
251 int
252 kern_execve(td, args, mac_p)
253 struct thread *td;
254 struct image_args *args;
255 struct mac *mac_p;
256 {
257 struct proc *p = td->td_proc;
258 int error;
259
260 AUDIT_ARG(argv, args->begin_argv, args->argc,
261 args->begin_envv - args->begin_argv);
262 AUDIT_ARG(envv, args->begin_envv, args->envc,
263 args->endp - args->begin_envv);
264 if (p->p_flag & P_HADTHREADS) {
265 PROC_LOCK(p);
266 if (thread_single(SINGLE_BOUNDARY)) {
267 PROC_UNLOCK(p);
268 exec_free_args(args);
269 return (ERESTART); /* Try again later. */
270 }
271 PROC_UNLOCK(p);
272 }
273
274 error = do_execve(td, args, mac_p);
275
276 if (p->p_flag & P_HADTHREADS) {
277 PROC_LOCK(p);
278 /*
279 * If success, we upgrade to SINGLE_EXIT state to
280 * force other threads to suicide.
281 */
282 if (error == 0)
283 thread_single(SINGLE_EXIT);
284 else
285 thread_single_end();
286 PROC_UNLOCK(p);
287 }
288
289 return (error);
290 }
291
292 /*
293 * In-kernel implementation of execve(). All arguments are assumed to be
294 * userspace pointers from the passed thread.
295 */
296 static int
297 do_execve(td, args, mac_p)
298 struct thread *td;
299 struct image_args *args;
300 struct mac *mac_p;
301 {
302 struct proc *p = td->td_proc;
303 struct nameidata nd, *ndp;
304 struct ucred *newcred = NULL, *oldcred;
305 struct uidinfo *euip;
306 register_t *stack_base;
307 int error, len, i;
308 struct image_params image_params, *imgp;
309 struct vattr attr;
310 int (*img_first)(struct image_params *);
311 struct pargs *oldargs = NULL, *newargs = NULL;
312 struct sigacts *oldsigacts, *newsigacts;
313 #ifdef KTRACE
314 struct vnode *tracevp = NULL;
315 struct ucred *tracecred = NULL;
316 #endif
317 struct vnode *textvp = NULL;
318 int credential_changing;
319 int vfslocked;
320 int textset;
321 #ifdef MAC
322 struct label *interplabel = NULL;
323 int will_transition;
324 #endif
325 #ifdef HWPMC_HOOKS
326 struct pmckern_procexec pe;
327 #endif
328
329 vfslocked = 0;
330 imgp = &image_params;
331
332 /*
333 * Lock the process and set the P_INEXEC flag to indicate that
334 * it should be left alone until we're done here. This is
335 * necessary to avoid race conditions - e.g. in ptrace() -
336 * that might allow a local user to illicitly obtain elevated
337 * privileges.
338 */
339 PROC_LOCK(p);
340 KASSERT((p->p_flag & P_INEXEC) == 0,
341 ("%s(): process already has P_INEXEC flag", __func__));
342 p->p_flag |= P_INEXEC;
343 PROC_UNLOCK(p);
344
345 /*
346 * Initialize part of the common data
347 */
348 imgp->proc = p;
349 imgp->execlabel = NULL;
350 imgp->attr = &attr;
351 imgp->entry_addr = 0;
352 imgp->vmspace_destroyed = 0;
353 imgp->interpreted = 0;
354 imgp->opened = 0;
355 imgp->interpreter_name = args->buf + PATH_MAX + ARG_MAX;
356 imgp->auxargs = NULL;
357 imgp->vp = NULL;
358 imgp->object = NULL;
359 imgp->firstpage = NULL;
360 imgp->ps_strings = 0;
361 imgp->auxarg_size = 0;
362 imgp->args = args;
363 imgp->execpath = imgp->freepath = NULL;
364 imgp->execpathp = 0;
365
366 #ifdef MAC
367 error = mac_execve_enter(imgp, mac_p);
368 if (error)
369 goto exec_fail;
370 #endif
371
372 imgp->image_header = NULL;
373
374 /*
375 * Translate the file name. namei() returns a vnode pointer
376 * in ni_vp amoung other things.
377 *
378 * XXXAUDIT: It would be desirable to also audit the name of the
379 * interpreter if this is an interpreted binary.
380 */
381 ndp = &nd;
382 NDINIT(ndp, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME | MPSAFE |
383 AUDITVNODE1, UIO_SYSSPACE, args->fname, td);
384
385 SDT_PROBE(proc, kernel, , exec, args->fname, 0, 0, 0, 0 );
386
387 interpret:
388 error = namei(ndp);
389 if (error)
390 goto exec_fail;
391
392 vfslocked = NDHASGIANT(ndp);
393 imgp->vp = ndp->ni_vp;
394
395 /*
396 * Check file permissions (also 'opens' file)
397 */
398 error = exec_check_permissions(imgp);
399 if (error)
400 goto exec_fail_dealloc;
401
402 imgp->object = imgp->vp->v_object;
403 if (imgp->object != NULL)
404 vm_object_reference(imgp->object);
405
406 /*
407 * Set VV_TEXT now so no one can write to the executable while we're
408 * activating it.
409 *
410 * Remember if this was set before and unset it in case this is not
411 * actually an executable image.
412 */
413 textset = imgp->vp->v_vflag & VV_TEXT;
414 imgp->vp->v_vflag |= VV_TEXT;
415
416 error = exec_map_first_page(imgp);
417 if (error)
418 goto exec_fail_dealloc;
419
420 imgp->proc->p_osrel = 0;
421 /*
422 * If the current process has a special image activator it
423 * wants to try first, call it. For example, emulating shell
424 * scripts differently.
425 */
426 error = -1;
427 if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
428 error = img_first(imgp);
429
430 /*
431 * Loop through the list of image activators, calling each one.
432 * An activator returns -1 if there is no match, 0 on success,
433 * and an error otherwise.
434 */
435 for (i = 0; error == -1 && execsw[i]; ++i) {
436 if (execsw[i]->ex_imgact == NULL ||
437 execsw[i]->ex_imgact == img_first) {
438 continue;
439 }
440 error = (*execsw[i]->ex_imgact)(imgp);
441 }
442
443 if (error) {
444 if (error == -1) {
445 if (textset == 0)
446 imgp->vp->v_vflag &= ~VV_TEXT;
447 error = ENOEXEC;
448 }
449 goto exec_fail_dealloc;
450 }
451
452 /*
453 * Special interpreter operation, cleanup and loop up to try to
454 * activate the interpreter.
455 */
456 if (imgp->interpreted) {
457 exec_unmap_first_page(imgp);
458 /*
459 * VV_TEXT needs to be unset for scripts. There is a short
460 * period before we determine that something is a script where
461 * VV_TEXT will be set. The vnode lock is held over this
462 * entire period so nothing should illegitimately be blocked.
463 */
464 imgp->vp->v_vflag &= ~VV_TEXT;
465 /* free name buffer and old vnode */
466 NDFREE(ndp, NDF_ONLY_PNBUF);
467 #ifdef MAC
468 interplabel = mac_vnode_label_alloc();
469 mac_copy_vnode_label(ndp->ni_vp->v_label, interplabel);
470 #endif
471 if (imgp->opened) {
472 VOP_CLOSE(ndp->ni_vp, FREAD, td->td_ucred, td);
473 imgp->opened = 0;
474 }
475 vput(ndp->ni_vp);
476 vm_object_deallocate(imgp->object);
477 imgp->object = NULL;
478 VFS_UNLOCK_GIANT(vfslocked);
479 vfslocked = 0;
480 /* set new name to that of the interpreter */
481 NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME | MPSAFE,
482 UIO_SYSSPACE, imgp->interpreter_name, td);
483 goto interpret;
484 }
485
486 /*
487 * NB: We unlock the vnode here because it is believed that none
488 * of the sv_copyout_strings/sv_fixup operations require the vnode.
489 */
490 VOP_UNLOCK(imgp->vp, 0, td);
491
492 if (imgp->auxargs != NULL)
493 vn_fullpath(td, imgp->vp, &imgp->execpath, &imgp->freepath);
494
495 /*
496 * Copy out strings (args and env) and initialize stack base
497 */
498 if (p->p_sysent->sv_copyout_strings)
499 stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
500 else
501 stack_base = exec_copyout_strings(imgp);
502
503 /*
504 * If custom stack fixup routine present for this process
505 * let it do the stack setup.
506 * Else stuff argument count as first item on stack
507 */
508 if (p->p_sysent->sv_fixup != NULL)
509 (*p->p_sysent->sv_fixup)(&stack_base, imgp);
510 else
511 suword(--stack_base, imgp->args->argc);
512
513 /*
514 * For security and other reasons, the file descriptor table cannot
515 * be shared after an exec.
516 */
517 fdunshare(p, td);
518
519 /*
520 * Malloc things before we need locks.
521 */
522 newcred = crget();
523 euip = uifind(attr.va_uid);
524 i = imgp->args->begin_envv - imgp->args->begin_argv;
525 /* Cache arguments if they fit inside our allowance */
526 if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
527 newargs = pargs_alloc(i);
528 bcopy(imgp->args->begin_argv, newargs->ar_args, i);
529 }
530
531 /* close files on exec */
532 fdcloseexec(td);
533 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
534
535 /* Get a reference to the vnode prior to locking the proc */
536 VREF(ndp->ni_vp);
537
538 /*
539 * For security and other reasons, signal handlers cannot
540 * be shared after an exec. The new process gets a copy of the old
541 * handlers. In execsigs(), the new process will have its signals
542 * reset.
543 */
544 PROC_LOCK(p);
545 if (sigacts_shared(p->p_sigacts)) {
546 oldsigacts = p->p_sigacts;
547 PROC_UNLOCK(p);
548 newsigacts = sigacts_alloc();
549 sigacts_copy(newsigacts, oldsigacts);
550 PROC_LOCK(p);
551 p->p_sigacts = newsigacts;
552 } else
553 oldsigacts = NULL;
554
555 /* Stop profiling */
556 stopprofclock(p);
557
558 /* reset caught signals */
559 execsigs(p);
560
561 /* name this process - nameiexec(p, ndp) */
562 bzero(p->p_comm, sizeof(p->p_comm));
563 len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
564 bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
565
566 /*
567 * mark as execed, wakeup the process that vforked (if any) and tell
568 * it that it now has its own resources back
569 */
570 p->p_flag |= P_EXEC;
571 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
572 p->p_flag &= ~P_PPWAIT;
573 cv_broadcast(&p->p_pwait);
574 }
575
576 /*
577 * Implement image setuid/setgid.
578 *
579 * Don't honor setuid/setgid if the filesystem prohibits it or if
580 * the process is being traced.
581 *
582 * XXXMAC: For the time being, use NOSUID to also prohibit
583 * transitions on the file system.
584 */
585 oldcred = p->p_ucred;
586 credential_changing = 0;
587 credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
588 attr.va_uid;
589 credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
590 attr.va_gid;
591 #ifdef MAC
592 will_transition = mac_execve_will_transition(oldcred, imgp->vp,
593 interplabel, imgp);
594 credential_changing |= will_transition;
595 #endif
596
597 if (credential_changing &&
598 (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
599 (p->p_flag & P_TRACED) == 0) {
600 /*
601 * Turn off syscall tracing for set-id programs, except for
602 * root. Record any set-id flags first to make sure that
603 * we do not regain any tracing during a possible block.
604 */
605 setsugid(p);
606
607 #ifdef KTRACE
608 if (p->p_tracevp != NULL &&
609 priv_check_cred(oldcred, PRIV_DEBUG_DIFFCRED, 0)) {
610 mtx_lock(&ktrace_mtx);
611 p->p_traceflag = 0;
612 tracevp = p->p_tracevp;
613 p->p_tracevp = NULL;
614 tracecred = p->p_tracecred;
615 p->p_tracecred = NULL;
616 mtx_unlock(&ktrace_mtx);
617 }
618 #endif
619 /*
620 * Close any file descriptors 0..2 that reference procfs,
621 * then make sure file descriptors 0..2 are in use.
622 *
623 * setugidsafety() may call closef() and then pfind()
624 * which may grab the process lock.
625 * fdcheckstd() may call falloc() which may block to
626 * allocate memory, so temporarily drop the process lock.
627 */
628 PROC_UNLOCK(p);
629 VOP_UNLOCK(imgp->vp, 0, td);
630 setugidsafety(td);
631 error = fdcheckstd(td);
632 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
633 if (error != 0)
634 goto done1;
635 PROC_LOCK(p);
636 /*
637 * Set the new credentials.
638 */
639 crcopy(newcred, oldcred);
640 if (attr.va_mode & VSUID)
641 change_euid(newcred, euip);
642 if (attr.va_mode & VSGID)
643 change_egid(newcred, attr.va_gid);
644 #ifdef MAC
645 if (will_transition) {
646 mac_execve_transition(oldcred, newcred, imgp->vp,
647 interplabel, imgp);
648 }
649 #endif
650 /*
651 * Implement correct POSIX saved-id behavior.
652 *
653 * XXXMAC: Note that the current logic will save the
654 * uid and gid if a MAC domain transition occurs, even
655 * though maybe it shouldn't.
656 */
657 change_svuid(newcred, newcred->cr_uid);
658 change_svgid(newcred, newcred->cr_gid);
659 p->p_ucred = newcred;
660 newcred = NULL;
661 } else {
662 if (oldcred->cr_uid == oldcred->cr_ruid &&
663 oldcred->cr_gid == oldcred->cr_rgid)
664 p->p_flag &= ~P_SUGID;
665 /*
666 * Implement correct POSIX saved-id behavior.
667 *
668 * XXX: It's not clear that the existing behavior is
669 * POSIX-compliant. A number of sources indicate that the
670 * saved uid/gid should only be updated if the new ruid is
671 * not equal to the old ruid, or the new euid is not equal
672 * to the old euid and the new euid is not equal to the old
673 * ruid. The FreeBSD code always updates the saved uid/gid.
674 * Also, this code uses the new (replaced) euid and egid as
675 * the source, which may or may not be the right ones to use.
676 */
677 if (oldcred->cr_svuid != oldcred->cr_uid ||
678 oldcred->cr_svgid != oldcred->cr_gid) {
679 crcopy(newcred, oldcred);
680 change_svuid(newcred, newcred->cr_uid);
681 change_svgid(newcred, newcred->cr_gid);
682 p->p_ucred = newcred;
683 newcred = NULL;
684 }
685 }
686
687 /*
688 * Store the vp for use in procfs. This vnode was referenced prior
689 * to locking the proc lock.
690 */
691 textvp = p->p_textvp;
692 p->p_textvp = ndp->ni_vp;
693
694 #ifdef KDTRACE_HOOKS
695 /*
696 * Tell the DTrace fasttrap provider about the exec if it
697 * has declared an interest.
698 */
699 if (dtrace_fasttrap_exec)
700 dtrace_fasttrap_exec(p);
701 #endif
702
703 /*
704 * Notify others that we exec'd, and clear the P_INEXEC flag
705 * as we're now a bona fide freshly-execed process.
706 */
707 KNOTE_LOCKED(&p->p_klist, NOTE_EXEC);
708 p->p_flag &= ~P_INEXEC;
709
710 /*
711 * If tracing the process, trap to debugger so breakpoints
712 * can be set before the program executes.
713 * Use tdsignal to deliver signal to current thread, use
714 * psignal may cause the signal to be delivered to wrong thread
715 * because that thread will exit, remember we are going to enter
716 * single thread mode.
717 */
718 if (p->p_flag & P_TRACED)
719 tdsignal(p, td, SIGTRAP, NULL);
720
721 /* clear "fork but no exec" flag, as we _are_ execing */
722 p->p_acflag &= ~AFORK;
723
724 /*
725 * Free any previous argument cache and replace it with
726 * the new argument cache, if any.
727 */
728 oldargs = p->p_args;
729 p->p_args = newargs;
730 newargs = NULL;
731
732 #ifdef HWPMC_HOOKS
733 /*
734 * Check if system-wide sampling is in effect or if the
735 * current process is using PMCs. If so, do exec() time
736 * processing. This processing needs to happen AFTER the
737 * P_INEXEC flag is cleared.
738 *
739 * The proc lock needs to be released before taking the PMC
740 * SX.
741 */
742 if (PMC_SYSTEM_SAMPLING_ACTIVE() || PMC_PROC_IS_USING_PMCS(p)) {
743 PROC_UNLOCK(p);
744 pe.pm_credentialschanged = credential_changing;
745 pe.pm_entryaddr = imgp->entry_addr;
746
747 PMC_CALL_HOOK_X(td, PMC_FN_PROCESS_EXEC, (void *) &pe);
748 } else
749 PROC_UNLOCK(p);
750 #else /* !HWPMC_HOOKS */
751 PROC_UNLOCK(p);
752 #endif
753
754 /* Set values passed into the program in registers. */
755 if (p->p_sysent->sv_setregs)
756 (*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
757 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
758 else
759 exec_setregs(td, imgp->entry_addr,
760 (u_long)(uintptr_t)stack_base, imgp->ps_strings);
761
762 vfs_mark_atime(imgp->vp, td);
763
764 SDT_PROBE(proc, kernel, , exec_success, args->fname, 0, 0, 0, 0);
765 done1:
766 /*
767 * Free any resources malloc'd earlier that we didn't use.
768 */
769 uifree(euip);
770 if (newcred == NULL)
771 crfree(oldcred);
772 else
773 crfree(newcred);
774 VOP_UNLOCK(imgp->vp, 0, td);
775
776 /*
777 * Handle deferred decrement of ref counts.
778 */
779 if (textvp != NULL) {
780 int tvfslocked;
781
782 tvfslocked = VFS_LOCK_GIANT(textvp->v_mount);
783 vrele(textvp);
784 VFS_UNLOCK_GIANT(tvfslocked);
785 }
786 if (ndp->ni_vp && error != 0)
787 vrele(ndp->ni_vp);
788 #ifdef KTRACE
789 if (tracevp != NULL) {
790 int tvfslocked;
791
792 tvfslocked = VFS_LOCK_GIANT(tracevp->v_mount);
793 vrele(tracevp);
794 VFS_UNLOCK_GIANT(tvfslocked);
795 }
796 if (tracecred != NULL)
797 crfree(tracecred);
798 #endif
799 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY, td);
800 pargs_drop(oldargs);
801 pargs_drop(newargs);
802 if (oldsigacts != NULL)
803 sigacts_free(oldsigacts);
804
805 exec_fail_dealloc:
806
807 /*
808 * free various allocated resources
809 */
810 if (imgp->firstpage != NULL)
811 exec_unmap_first_page(imgp);
812
813 if (imgp->vp != NULL) {
814 NDFREE(ndp, NDF_ONLY_PNBUF);
815 if (imgp->opened)
816 VOP_CLOSE(imgp->vp, FREAD, td->td_ucred, td);
817 vput(imgp->vp);
818 }
819
820 if (imgp->object != NULL)
821 vm_object_deallocate(imgp->object);
822
823 free(imgp->freepath, M_TEMP);
824
825 if (error == 0) {
826 /*
827 * Stop the process here if its stop event mask has
828 * the S_EXEC bit set.
829 */
830 STOPEVENT(p, S_EXEC, 0);
831 goto done2;
832 }
833
834 exec_fail:
835 /* we're done here, clear P_INEXEC */
836 PROC_LOCK(p);
837 p->p_flag &= ~P_INEXEC;
838 PROC_UNLOCK(p);
839
840 SDT_PROBE(proc, kernel, , exec_failure, error, 0, 0, 0, 0);
841
842 done2:
843 #ifdef MAC
844 mac_execve_exit(imgp);
845 if (interplabel != NULL)
846 mac_vnode_label_free(interplabel);
847 #endif
848 VFS_UNLOCK_GIANT(vfslocked);
849 exec_free_args(args);
850
851 if (error && imgp->vmspace_destroyed) {
852 /* sorry, no more process anymore. exit gracefully */
853 exit1(td, W_EXITCODE(0, SIGABRT));
854 /* NOT REACHED */
855 }
856 return (error);
857 }
858
859 int
860 exec_map_first_page(imgp)
861 struct image_params *imgp;
862 {
863 int rv, i;
864 int initial_pagein;
865 vm_page_t ma[VM_INITIAL_PAGEIN];
866 vm_object_t object;
867
868 if (imgp->firstpage != NULL)
869 exec_unmap_first_page(imgp);
870
871 object = imgp->vp->v_object;
872 if (object == NULL)
873 return (EACCES);
874 VM_OBJECT_LOCK(object);
875 #if VM_NRESERVLEVEL > 0
876 if ((object->flags & OBJ_COLORED) == 0) {
877 object->flags |= OBJ_COLORED;
878 object->pg_color = 0;
879 }
880 #endif
881 ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
882 if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
883 initial_pagein = VM_INITIAL_PAGEIN;
884 if (initial_pagein > object->size)
885 initial_pagein = object->size;
886 for (i = 1; i < initial_pagein; i++) {
887 if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
888 if (ma[i]->valid)
889 break;
890 if ((ma[i]->oflags & VPO_BUSY) || ma[i]->busy)
891 break;
892 vm_page_busy(ma[i]);
893 } else {
894 ma[i] = vm_page_alloc(object, i,
895 VM_ALLOC_NORMAL | VM_ALLOC_IFNOTCACHED);
896 if (ma[i] == NULL)
897 break;
898 }
899 }
900 initial_pagein = i;
901 rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
902 ma[0] = vm_page_lookup(object, 0);
903 if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
904 (ma[0]->valid == 0)) {
905 if (ma[0]) {
906 vm_page_lock_queues();
907 vm_page_free(ma[0]);
908 vm_page_unlock_queues();
909 }
910 VM_OBJECT_UNLOCK(object);
911 return (EIO);
912 }
913 }
914 vm_page_lock_queues();
915 vm_page_hold(ma[0]);
916 vm_page_unlock_queues();
917 vm_page_wakeup(ma[0]);
918 VM_OBJECT_UNLOCK(object);
919
920 imgp->firstpage = sf_buf_alloc(ma[0], 0);
921 imgp->image_header = (char *)sf_buf_kva(imgp->firstpage);
922
923 return (0);
924 }
925
926 void
927 exec_unmap_first_page(imgp)
928 struct image_params *imgp;
929 {
930 vm_page_t m;
931
932 if (imgp->firstpage != NULL) {
933 m = sf_buf_page(imgp->firstpage);
934 sf_buf_free(imgp->firstpage);
935 imgp->firstpage = NULL;
936 vm_page_lock_queues();
937 vm_page_unhold(m);
938 vm_page_unlock_queues();
939 }
940 }
941
942 /*
943 * Destroy old address space, and allocate a new stack
944 * The new stack is only SGROWSIZ large because it is grown
945 * automatically in trap.c.
946 */
947 int
948 exec_new_vmspace(imgp, sv)
949 struct image_params *imgp;
950 struct sysentvec *sv;
951 {
952 int error;
953 struct proc *p = imgp->proc;
954 struct vmspace *vmspace = p->p_vmspace;
955 vm_offset_t sv_minuser, stack_addr;
956 vm_map_t map;
957 u_long ssiz;
958
959 imgp->vmspace_destroyed = 1;
960 imgp->sysent = sv;
961
962 /* May be called with Giant held */
963 EVENTHANDLER_INVOKE(process_exec, p, imgp);
964
965 /*
966 * Blow away entire process VM, if address space not shared,
967 * otherwise, create a new VM space so that other threads are
968 * not disrupted
969 */
970 map = &vmspace->vm_map;
971 if (map_at_zero)
972 sv_minuser = sv->sv_minuser;
973 else
974 sv_minuser = MAX(sv->sv_minuser, PAGE_SIZE);
975 if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv_minuser &&
976 vm_map_max(map) == sv->sv_maxuser) {
977 shmexit(vmspace);
978 pmap_remove_pages(vmspace_pmap(vmspace));
979 vm_map_remove(map, vm_map_min(map), vm_map_max(map));
980 } else {
981 error = vmspace_exec(p, sv_minuser, sv->sv_maxuser);
982 if (error)
983 return (error);
984 vmspace = p->p_vmspace;
985 map = &vmspace->vm_map;
986 }
987
988 /* Allocate a new stack */
989 if (sv->sv_maxssiz != NULL)
990 ssiz = *sv->sv_maxssiz;
991 else
992 ssiz = maxssiz;
993 stack_addr = sv->sv_usrstack - ssiz;
994 error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz,
995 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_DOWN);
996 if (error)
997 return (error);
998
999 #ifdef __ia64__
1000 /* Allocate a new register stack */
1001 stack_addr = IA64_BACKINGSTORE;
1002 error = vm_map_stack(map, stack_addr, (vm_size_t)ssiz,
1003 sv->sv_stackprot, VM_PROT_ALL, MAP_STACK_GROWS_UP);
1004 if (error)
1005 return (error);
1006 #endif
1007
1008 /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
1009 * VM_STACK case, but they are still used to monitor the size of the
1010 * process stack so we can check the stack rlimit.
1011 */
1012 vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
1013 vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - ssiz;
1014
1015 return (0);
1016 }
1017
1018 /*
1019 * Copy out argument and environment strings from the old process address
1020 * space into the temporary string buffer.
1021 */
1022 int
1023 exec_copyin_args(struct image_args *args, char *fname,
1024 enum uio_seg segflg, char **argv, char **envv)
1025 {
1026 char *argp, *envp;
1027 int error;
1028 size_t length;
1029
1030 error = 0;
1031
1032 bzero(args, sizeof(*args));
1033 if (argv == NULL)
1034 return (EFAULT);
1035 /*
1036 * Allocate temporary demand zeroed space for argument and
1037 * environment strings:
1038 *
1039 * o ARG_MAX for argument and environment;
1040 * o MAXSHELLCMDLEN for the name of interpreters.
1041 */
1042 args->buf = (char *) kmem_alloc_wait(exec_map,
1043 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1044 if (args->buf == NULL)
1045 return (ENOMEM);
1046 args->begin_argv = args->buf;
1047 args->endp = args->begin_argv;
1048 args->stringspace = ARG_MAX;
1049
1050 args->fname = args->buf + ARG_MAX;
1051
1052 /*
1053 * Copy the file name.
1054 */
1055 error = (segflg == UIO_SYSSPACE) ?
1056 copystr(fname, args->fname, PATH_MAX, &length) :
1057 copyinstr(fname, args->fname, PATH_MAX, &length);
1058 if (error != 0)
1059 goto err_exit;
1060
1061 /*
1062 * extract arguments first
1063 */
1064 while ((argp = (caddr_t) (intptr_t) fuword(argv++))) {
1065 if (argp == (caddr_t) -1) {
1066 error = EFAULT;
1067 goto err_exit;
1068 }
1069 if ((error = copyinstr(argp, args->endp,
1070 args->stringspace, &length))) {
1071 if (error == ENAMETOOLONG)
1072 error = E2BIG;
1073 goto err_exit;
1074 }
1075 args->stringspace -= length;
1076 args->endp += length;
1077 args->argc++;
1078 }
1079
1080 args->begin_envv = args->endp;
1081
1082 /*
1083 * extract environment strings
1084 */
1085 if (envv) {
1086 while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
1087 if (envp == (caddr_t)-1) {
1088 error = EFAULT;
1089 goto err_exit;
1090 }
1091 if ((error = copyinstr(envp, args->endp,
1092 args->stringspace, &length))) {
1093 if (error == ENAMETOOLONG)
1094 error = E2BIG;
1095 goto err_exit;
1096 }
1097 args->stringspace -= length;
1098 args->endp += length;
1099 args->envc++;
1100 }
1101 }
1102
1103 return (0);
1104
1105 err_exit:
1106 exec_free_args(args);
1107 return (error);
1108 }
1109
1110 static void
1111 exec_free_args(struct image_args *args)
1112 {
1113
1114 if (args->buf) {
1115 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
1116 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
1117 args->buf = NULL;
1118 }
1119 }
1120
1121 /*
1122 * Copy strings out to the new process address space, constructing new arg
1123 * and env vector tables. Return a pointer to the base so that it can be used
1124 * as the initial stack pointer.
1125 */
1126 register_t *
1127 exec_copyout_strings(imgp)
1128 struct image_params *imgp;
1129 {
1130 int argc, envc;
1131 char **vectp;
1132 char *stringp, *destp;
1133 register_t *stack_base;
1134 struct ps_strings *arginfo;
1135 struct proc *p;
1136 size_t execpath_len;
1137 int szsigcode;
1138
1139 /*
1140 * Calculate string base and vector table pointers.
1141 * Also deal with signal trampoline code for this exec type.
1142 */
1143 if (imgp->execpath != NULL && imgp->auxargs != NULL)
1144 execpath_len = strlen(imgp->execpath) + 1;
1145 else
1146 execpath_len = 0;
1147 p = imgp->proc;
1148 szsigcode = 0;
1149 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
1150 if (p->p_sysent->sv_szsigcode != NULL)
1151 szsigcode = *(p->p_sysent->sv_szsigcode);
1152 destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
1153 roundup(execpath_len, sizeof(char *)) -
1154 roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
1155
1156 /*
1157 * install sigcode
1158 */
1159 if (szsigcode)
1160 copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
1161 szsigcode), szsigcode);
1162
1163 /*
1164 * Copy the image path for the rtld.
1165 */
1166 if (execpath_len != 0) {
1167 imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len;
1168 copyout(imgp->execpath, (void *)imgp->execpathp,
1169 execpath_len);
1170 }
1171
1172 /*
1173 * If we have a valid auxargs ptr, prepare some room
1174 * on the stack.
1175 */
1176 if (imgp->auxargs) {
1177 /*
1178 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
1179 * lower compatibility.
1180 */
1181 imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
1182 (AT_COUNT * 2);
1183 /*
1184 * The '+ 2' is for the null pointers at the end of each of
1185 * the arg and env vector sets,and imgp->auxarg_size is room
1186 * for argument of Runtime loader.
1187 */
1188 vectp = (char **)(destp - (imgp->args->argc +
1189 imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
1190 sizeof(char *));
1191 } else {
1192 /*
1193 * The '+ 2' is for the null pointers at the end of each of
1194 * the arg and env vector sets
1195 */
1196 vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) *
1197 sizeof(char *));
1198 }
1199
1200 /*
1201 * vectp also becomes our initial stack base
1202 */
1203 stack_base = (register_t *)vectp;
1204
1205 stringp = imgp->args->begin_argv;
1206 argc = imgp->args->argc;
1207 envc = imgp->args->envc;
1208
1209 /*
1210 * Copy out strings - arguments and environment.
1211 */
1212 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
1213
1214 /*
1215 * Fill in "ps_strings" struct for ps, w, etc.
1216 */
1217 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
1218 suword(&arginfo->ps_nargvstr, argc);
1219
1220 /*
1221 * Fill in argument portion of vector table.
1222 */
1223 for (; argc > 0; --argc) {
1224 suword(vectp++, (long)(intptr_t)destp);
1225 while (*stringp++ != 0)
1226 destp++;
1227 destp++;
1228 }
1229
1230 /* a null vector table pointer separates the argp's from the envp's */
1231 suword(vectp++, 0);
1232
1233 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
1234 suword(&arginfo->ps_nenvstr, envc);
1235
1236 /*
1237 * Fill in environment portion of vector table.
1238 */
1239 for (; envc > 0; --envc) {
1240 suword(vectp++, (long)(intptr_t)destp);
1241 while (*stringp++ != 0)
1242 destp++;
1243 destp++;
1244 }
1245
1246 /* end of vector table is a null pointer */
1247 suword(vectp, 0);
1248
1249 return (stack_base);
1250 }
1251
1252 /*
1253 * Check permissions of file to execute.
1254 * Called with imgp->vp locked.
1255 * Return 0 for success or error code on failure.
1256 */
1257 int
1258 exec_check_permissions(imgp)
1259 struct image_params *imgp;
1260 {
1261 struct vnode *vp = imgp->vp;
1262 struct vattr *attr = imgp->attr;
1263 struct thread *td;
1264 int error;
1265
1266 td = curthread; /* XXXKSE */
1267
1268 /* Get file attributes */
1269 error = VOP_GETATTR(vp, attr, td->td_ucred, td);
1270 if (error)
1271 return (error);
1272
1273 #ifdef MAC
1274 error = mac_check_vnode_exec(td->td_ucred, imgp->vp, imgp);
1275 if (error)
1276 return (error);
1277 #endif
1278
1279 /*
1280 * 1) Check if file execution is disabled for the filesystem that this
1281 * file resides on.
1282 * 2) Insure that at least one execute bit is on - otherwise root
1283 * will always succeed, and we don't want to happen unless the
1284 * file really is executable.
1285 * 3) Insure that the file is a regular file.
1286 */
1287 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
1288 ((attr->va_mode & 0111) == 0) ||
1289 (attr->va_type != VREG))
1290 return (EACCES);
1291
1292 /*
1293 * Zero length files can't be exec'd
1294 */
1295 if (attr->va_size == 0)
1296 return (ENOEXEC);
1297
1298 /*
1299 * Check for execute permission to file based on current credentials.
1300 */
1301 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
1302 if (error)
1303 return (error);
1304
1305 /*
1306 * Check number of open-for-writes on the file and deny execution
1307 * if there are any.
1308 */
1309 if (vp->v_writecount)
1310 return (ETXTBSY);
1311
1312 /*
1313 * Call filesystem specific open routine (which does nothing in the
1314 * general case).
1315 */
1316 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
1317 if (error == 0)
1318 imgp->opened = 1;
1319 return (error);
1320 }
1321
1322 /*
1323 * Exec handler registration
1324 */
1325 int
1326 exec_register(execsw_arg)
1327 const struct execsw *execsw_arg;
1328 {
1329 const struct execsw **es, **xs, **newexecsw;
1330 int count = 2; /* New slot and trailing NULL */
1331
1332 if (execsw)
1333 for (es = execsw; *es; es++)
1334 count++;
1335 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1336 if (newexecsw == NULL)
1337 return (ENOMEM);
1338 xs = newexecsw;
1339 if (execsw)
1340 for (es = execsw; *es; es++)
1341 *xs++ = *es;
1342 *xs++ = execsw_arg;
1343 *xs = NULL;
1344 if (execsw)
1345 free(execsw, M_TEMP);
1346 execsw = newexecsw;
1347 return (0);
1348 }
1349
1350 int
1351 exec_unregister(execsw_arg)
1352 const struct execsw *execsw_arg;
1353 {
1354 const struct execsw **es, **xs, **newexecsw;
1355 int count = 1;
1356
1357 if (execsw == NULL)
1358 panic("unregister with no handlers left?\n");
1359
1360 for (es = execsw; *es; es++) {
1361 if (*es == execsw_arg)
1362 break;
1363 }
1364 if (*es == NULL)
1365 return (ENOENT);
1366 for (es = execsw; *es; es++)
1367 if (*es != execsw_arg)
1368 count++;
1369 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1370 if (newexecsw == NULL)
1371 return (ENOMEM);
1372 xs = newexecsw;
1373 for (es = execsw; *es; es++)
1374 if (*es != execsw_arg)
1375 *xs++ = *es;
1376 *xs = NULL;
1377 if (execsw)
1378 free(execsw, M_TEMP);
1379 execsw = newexecsw;
1380 return (0);
1381 }
Cache object: 9007be43c3ca19249fceb2f2a4a53d08
|