2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Mach Operating System
25 * Copyright (c) 1987 Carnegie-Mellon University
26 * All rights reserved. The CMU software License Agreement specifies
27 * the terms and conditions for use and redistribution.
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * from: @(#)kern_exec.c 8.1 (Berkeley) 6/10/93
71 #include <machine/reg.h>
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/filedesc.h>
76 #include <sys/kernel.h>
80 #include <sys/socketvar.h>
81 #include <sys/malloc.h>
82 #include <sys/namei.h>
83 #include <sys/mount.h>
84 #include <sys/vnode.h>
90 #include <sys/kdebug.h>
91 #include <sys/signal.h>
93 #include <mach/vm_param.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_kern.h>
98 #include <kern/thread.h>
99 #include <kern/task.h>
101 #include <kern/ast.h>
102 #include <kern/mach_loader.h>
103 #include <mach-o/fat.h>
104 #include <mach-o/loader.h>
105 #include <machine/vmparam.h>
107 extern vm_map_t bsd_pageable_map
;
109 #define ROUND_PTR(type, addr) \
110 (type *)( ( (unsigned)(addr) + 16 - 1) \
113 static int load_return_to_errno(load_return_t lrtn
);
114 int execve(struct proc
*p
, struct execve_args
*uap
, register_t
*retval
);
117 execv(p
, args
, retval
)
122 ((struct execve_args
*)args
)->envp
= NULL
;
123 return (execve(p
, args
, retval
));
128 execve(p
, uap
, retval
)
129 register struct proc
*p
;
130 register struct execve_args
*uap
;
133 register struct ucred
*cred
= p
->p_ucred
;
134 register struct filedesc
*fdp
= p
->p_fd
;
137 int na
, ne
, ucp
, ap
, cc
;
144 struct vattr origvattr
;
145 vm_offset_t execargs
;
147 struct ps_strings ps
;
152 struct mach_header
*mach_header
;
153 struct fat_header
*fat_header
;
154 struct fat_arch fat_arch
;
156 load_result_t load_result
;
157 struct uthread
*uthread
;
160 /* #! and name of interpreter */
161 char ex_shell
[SHSIZE
];
162 /* Mach-O executable */
163 struct mach_header mach_header
;
165 struct fat_header fat_header
;
170 int savedpathlen
= 0;
171 vm_offset_t
*execargsp
;
176 tsk
= current_task();
179 if(tsk
!= kernel_task
) {
180 numthreads
= get_task_numacts(tsk
);
181 if (numthreads
<= 0 )
183 if (numthreads
> 1) {
188 ret
= kmem_alloc_pageable(bsd_pageable_map
, &execargs
, NCARGS
);
189 if (ret
!= KERN_SUCCESS
)
192 uthread
= get_bsdthread_info(current_act());
194 savedpath
= execargs
;
197 * To support new app package launching for Mac OS X, the dyld
198 * needs the first argument to execve() stored on the user stack.
199 * Copyin the "path" at the begining of the "execargs" buffer
202 * We have to do this before namei() because in case of
203 * symbolic links, namei() would overwrite the original "path".
204 * In case the last symbolic link resolved was a relative pathname
205 * we would loose the original "path", which could be an
206 * absolute pathname. This might be unacceptable for dyld.
208 /* XXX We could optimize to avoid copyinstr in the namei() */
210 error
= copyinstr(uap
->fname
, savedpath
, MAXPATHLEN
, &savedpathlen
);
214 * copyinstr will put in savedpathlen, the count of
215 * characters (including NULL) in the path.
218 /* Save the name aside for future use */
219 execargsp
= (vm_offset_t
*)((char *)(execargs
) + savedpathlen
);
221 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| SAVENAME
,
222 UIO_USERSPACE
, uap
->fname
, p
);
223 if ((error
= namei(&nd
)))
226 VOP_LEASE(vp
, p
, p
->p_ucred
, LEASE_READ
);
228 if ((error
= VOP_GETATTR(vp
, &origvattr
, p
->p_ucred
, p
)))
231 /* Check mount point */
232 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
238 if ((vp
->v_mount
->mnt_flag
& MNT_NOSUID
) || (p
->p_flag
& P_TRACED
))
239 origvattr
.va_mode
&= ~(VSUID
| VSGID
);
241 *(&vattr
) = *(&origvattr
);
244 error
= check_exec_access(p
, vp
, &vattr
);
249 * Read in first few bytes of file for segment sizes, magic number:
250 * 407 = plain executable
252 * 413 = demand paged RO text
253 * Also an ASCII line beginning with #! is
254 * the file name of a ``shell'' and arguments may be prepended
255 * to the argument list if given here.
257 * SHELL NAMES ARE LIMITED IN LENGTH.
259 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
263 exdata
.ex_shell
[0] = '\0'; /* for zero length files */
265 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
, sizeof (exdata
), 0,
266 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
, p
);
272 if (resid
> sizeof(exdata
) - min(sizeof(exdata
.mach_header
),
273 sizeof(exdata
.fat_header
))
274 && exdata
.ex_shell
[0] != '#') {
279 mach_header
= &exdata
.mach_header
;
280 fat_header
= &exdata
.fat_header
;
281 if (mach_header
->magic
== MH_MAGIC
)
283 else if (fat_header
->magic
== FAT_MAGIC
||
284 fat_header
->magic
== FAT_CIGAM
)
286 else if (mach_header
->magic
== MH_CIGAM
) {
290 if (exdata
.ex_shell
[0] != '#' ||
291 exdata
.ex_shell
[1] != '!' ||
296 cp
= &exdata
.ex_shell
[2]; /* skip "#!" */
297 while (cp
< &exdata
.ex_shell
[SHSIZE
]) {
300 else if (*cp
== '\n') {
310 cp
= &exdata
.ex_shell
[2];
314 while (*cp
&& *cp
!= ' ')
323 bcopy((caddr_t
)cp
, (caddr_t
)cfarg
, SHSIZE
);
327 * Support for new app package launching for Mac OS X.
328 * We are about to retry the execve() by changing the path to the
329 * interpreter name. Need to re-initialize the savedpath and
330 * savedpathlen. +1 for NULL.
332 savedpathlen
= (cpnospace
- execnamep
+ 1);
333 error
= copystr(execnamep
, savedpath
, savedpathlen
, &savedpathlen
);
337 /* Save the name aside for future use */
338 execargsp
= (vm_offset_t
*)((char *)(execargs
) + savedpathlen
);
342 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
343 nd
.ni_cnd
.cn_flags
= (nd
.ni_cnd
.cn_flags
& HASBUF
) |
344 (FOLLOW
| LOCKLEAF
| SAVENAME
);
345 nd
.ni_segflg
= UIO_SYSSPACE
;
346 nd
.ni_dirp
= execnamep
;
347 if ((error
= namei(&nd
)))
350 VOP_LEASE(vp
, p
, cred
, LEASE_READ
);
351 if ((error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
)))
357 * Collect arguments on "file" in swap space.
364 * Support for new app package launching for Mac OS X allocates
365 * the "path" at the begining.
366 * execargs get allocated after that
368 cp
= (char *) execargsp
; /* running pointer for copy */
370 * size of execargs less sizeof "path",
371 * a pointer to "path" and a NULL poiter
373 cc
= NCARGS
- savedpathlen
- 2*NBPW
;
375 * Copy arguments into file in argdev area.
377 if (uap
->argp
) for (;;) {
380 if (indir
&& na
== 0) {
381 sharg
= nd
.ni_cnd
.cn_nameptr
;
383 uap
->argp
++; /* ignore argv[0] */
384 } else if (indir
&& (na
== 1 && cfarg
[0])) {
387 } else if (indir
&& (na
== 1 || (na
== 2 && cfarg
[0])))
388 ap
= (int)uap
->fname
;
389 else if (uap
->argp
) {
390 ap
= fuword((caddr_t
)uap
->argp
);
393 if (ap
== NULL
&& uap
->envp
) {
395 if ((ap
= fuword((caddr_t
)uap
->envp
)) != NULL
)
406 if (nc
>= (NCARGS
- savedpathlen
- 2*NBPW
-1)) {
411 error
= copystr(sharg
, cp
, (unsigned)cc
, &len
);
414 error
= copyinstr((caddr_t
)ap
, cp
, (unsigned)cc
,
421 } while (error
== ENAMETOOLONG
);
426 nc
= (nc
+ NBPW
-1) & ~(NBPW
-1);
429 * If we have a fat file, find "our" executable.
433 * Look up our architecture in the fat file.
435 lret
= fatfile_getarch(vp
, (vm_offset_t
)fat_header
, &fat_arch
);
436 if (lret
!= LOAD_SUCCESS
) {
437 error
= load_return_to_errno(lret
);
440 /* Read the Mach-O header out of it */
441 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
.mach_header
,
442 sizeof (exdata
.mach_header
),
444 UIO_SYSSPACE
, (IO_UNIT
|IO_NODELOCKED
), cred
, &resid
, p
);
450 /* Did we read a complete header? */
456 /* Is what we found a Mach-O executable */
457 if (mach_header
->magic
!= MH_MAGIC
) {
463 * Load the Mach-O file.
465 VOP_UNLOCK(vp
, 0, p
);
466 lret
= load_machfile(vp
, mach_header
, fat_arch
.offset
,
467 fat_arch
.size
, &load_result
);
470 * Load the Mach-O file.
472 VOP_UNLOCK(vp
, 0, p
);
473 lret
= load_machfile(vp
, mach_header
, 0,
474 (u_long
)vattr
.va_size
, &load_result
);
477 if (lret
!= LOAD_SUCCESS
) {
478 error
= load_return_to_errno(lret
);
482 /* load_machfile() maps the vnode */
486 * deal with set[ug]id.
488 p
->p_flag
&= ~P_SUGID
;
489 if (((origvattr
.va_mode
& VSUID
) != 0 &&
490 p
->p_ucred
->cr_uid
!= origvattr
.va_uid
)
491 || (origvattr
.va_mode
& VSGID
) != 0 &&
492 p
->p_ucred
->cr_gid
!= origvattr
.va_gid
) {
493 p
->p_ucred
= crcopy(cred
);
496 * If process is being ktraced, turn off - unless
499 if (p
->p_tracep
&& !(p
->p_traceflag
& KTRFAC_ROOT
)) {
505 if (origvattr
.va_mode
& VSUID
)
506 p
->p_ucred
->cr_uid
= origvattr
.va_uid
;
507 if (origvattr
.va_mode
& VSGID
)
508 p
->p_ucred
->cr_gid
= origvattr
.va_gid
;
510 set_security_token(p
);
511 p
->p_flag
|= P_SUGID
;
513 /* Radar 2261856; setuid security hole fix */
514 /* Patch from OpenBSD: A. Ramesh */
516 * XXX For setuid processes, attempt to ensure that
517 * stdin, stdout, and stderr are already allocated.
518 * We do not want userland to accidentally allocate
519 * descriptors in this range which has implied meaning
522 for (i
= 0; i
< 3; i
++) {
523 extern struct fileops vnops
;
524 struct nameidata nd1
;
528 if (p
->p_fd
->fd_ofiles
[i
] == NULL
) {
529 if ((error
= falloc(p
, &fp
, &indx
)) != 0)
531 NDINIT(&nd1
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
,
533 if ((error
= vn_open(&nd1
, FREAD
, 0)) != 0) {
535 p
->p_fd
->fd_ofiles
[indx
] = NULL
;
539 fp
->f_type
= DTYPE_VNODE
;
541 fp
->f_data
= (caddr_t
)nd1
.ni_vp
;
542 VOP_UNLOCK(nd1
.ni_vp
, 0, p
);
546 p
->p_cred
->p_svuid
= p
->p_ucred
->cr_uid
;
547 p
->p_cred
->p_svgid
= p
->p_ucred
->cr_gid
;
549 if (p
->p_flag
& P_TRACED
) {
552 thread_apc_set(current_act(), bsd_ast
);
561 VOP_LOCK(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
565 if (load_result
.unixproc
&&
566 create_unix_stack(current_map(),
567 load_result
.user_stack
, p
)) {
568 error
= load_return_to_errno(LOAD_NOSPACE
);
573 * Copy back arglist if necessary.
577 if (load_result
.unixproc
) {
580 ucp
= ucp
- nc
- NBPW
; /* begining of the STRING AREA */
583 * Support for new app package launching for Mac OS X allocates
584 * the "path" at the begining of the execargs buffer.
585 * copy it just before the string area.
587 savedpathlen
= (savedpathlen
+ NBPW
-1) & ~(NBPW
-1);
589 pathptr
= ucp
- savedpathlen
;
590 error
= copyoutstr(savedpath
, (caddr_t
)pathptr
,
591 (unsigned)savedpathlen
, &len
);
595 /* Save a NULL pointer below it */
596 (void) suword((caddr_t
)(pathptr
- NBPW
), 0);
598 /* Save the pointer to "path" just below it */
599 (void) suword((caddr_t
)(pathptr
- 2*NBPW
), pathptr
);
602 * na includes arg[] and env[].
603 * NBPW for 2 NULL one each ofter arg[argc -1] and env[n]
605 * skip over saved path, NBPW for pointer to path,
606 * and NBPW for the NULL after pointer to path.
608 ap
= ucp
- na
*NBPW
- 3*NBPW
- savedpathlen
- 2*NBPW
;
609 uthread
->uu_ar0
[SP
] = ap
;
610 (void) suword((caddr_t
)ap
, na
-ne
); /* argc */
614 cp
= (char *) execargsp
;
615 cc
= NCARGS
- savedpathlen
- 2*NBPW
;
616 ps
.ps_argvstr
= (char *)ucp
; /* first argv string */
617 ps
.ps_nargvstr
= na
- ne
; /* argc */
621 (void) suword((caddr_t
)ap
, 0);
623 ps
.ps_envstr
= (char *)ucp
;
628 (void) suword((caddr_t
)ap
, ucp
);
630 error
= copyoutstr(cp
, (caddr_t
)ucp
,
636 } while (error
== ENAMETOOLONG
);
638 break; /* bad stack - user's problem */
640 (void) suword((caddr_t
)ap
, 0);
643 if (load_result
.dynlinker
) {
644 ap
= uthread
->uu_ar0
[SP
] -= 4;
645 (void) suword((caddr_t
)ap
, load_result
.mach_header
);
648 #if defined(i386) || defined(ppc)
649 uthread
->uu_ar0
[PC
] = load_result
.entry_point
;
651 #error architecture not implemented!
658 * Reset signal state.
663 * Close file descriptors
664 * which specify close-on-exec.
667 /* FIXME: Till vmspace inherit is fixed: */
672 * Remember file name for accounting.
674 p
->p_acflag
&= ~AFORK
;
675 if (nd
.ni_cnd
.cn_namelen
> MAXCOMLEN
)
676 nd
.ni_cnd
.cn_namelen
= MAXCOMLEN
;
677 bcopy((caddr_t
)nd
.ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_comm
,
678 (unsigned)nd
.ni_cnd
.cn_namelen
);
679 p
->p_comm
[nd
.ni_cnd
.cn_namelen
] = '\0';
682 /* This is for kdebug */
683 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
685 /* Collect the pathname for tracing */
686 kdbg_trace_string(p
, &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
687 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
688 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
692 * mark as execed, wakeup the process that vforked (if any) and tell
693 * it that it now has it's own resources back
696 if (p
->p_pptr
&& (p
->p_flag
& P_PPWAIT
)) {
697 p
->p_flag
&= ~P_PPWAIT
;
698 wakeup((caddr_t
)p
->p_pptr
);
702 FREE_ZONE(nd
.ni_cnd
.cn_pnbuf
, nd
.ni_cnd
.cn_pnlen
, M_NAMEI
);
708 kmem_free_wakeup(bsd_pageable_map
, execargs
, NCARGS
);
711 kmem_free(bsd_pageable_map
, execargs
, NCARGS
);
717 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
720 create_unix_stack(map
, user_stack
, p
)
722 vm_offset_t user_stack
;
728 p
->user_stack
= user_stack
;
729 size
= round_page(unix_stack_size(p
));
731 /* stack always points to first address for stacks */
733 #else STACK_GROWTH_UP
734 addr
= trunc_page(user_stack
- size
);
735 #endif /* STACK_GROWTH_UP */
736 return (vm_allocate(map
,&addr
, size
, FALSE
));
739 #include <sys/reboot.h>
741 char init_program_name
[128] = "/sbin/mach_init\0";
743 char init_args
[128] = "";
745 struct execve_args init_exec_args
;
746 int init_attempts
= 0;
753 vm_offset_t init_addr
;
757 register_t retval
[2];
764 /* init_args are copied in string form directly from bootstrap */
767 if (boothowto
& RB_INITNAME
) {
768 printf("init program? ");
770 gets(init_program_name
, init_program_name
);
774 if (error
&& ((boothowto
& RB_INITNAME
) == 0) &&
775 (init_attempts
== 1)) {
776 static char other_init
[] = "/etc/mach_init";
777 printf("Load of %s, errno %d, trying %s\n",
778 init_program_name
, error
, other_init
);
780 bcopy(other_init
, init_program_name
,
787 printf("Load of %s failed, errno %d\n",
788 init_program_name
, error
);
790 boothowto
|= RB_INITNAME
;
795 * Copy out program name.
798 init_addr
= VM_MIN_ADDRESS
;
799 (void) vm_allocate(current_map(), &init_addr
,
803 (void) copyout((caddr_t
) init_program_name
,
804 (caddr_t
) (init_addr
),
805 (unsigned) sizeof(init_program_name
)+1);
807 argv
[0] = (char *) init_addr
;
808 init_addr
+= sizeof(init_program_name
);
809 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
812 * Put out first (and only) argument, similarly.
813 * Assumes everything fits in a page as allocated
817 (void) copyout((caddr_t
) init_args
,
818 (caddr_t
) (init_addr
),
819 (unsigned) sizeof(init_args
));
821 argv
[1] = (char *) init_addr
;
822 init_addr
+= sizeof(init_args
);
823 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
826 * Null-end the argument list
829 argv
[2] = (char *) 0;
832 * Copy out the argument list.
835 (void) copyout((caddr_t
) argv
,
836 (caddr_t
) (init_addr
),
837 (unsigned) sizeof(argv
));
840 * Set up argument block for fake call to execve.
843 init_exec_args
.fname
= argv
[0];
844 init_exec_args
.argp
= (char **) init_addr
;
845 init_exec_args
.envp
= 0;
847 /* So that mach_init task
848 * is set with uid,gid 0 token
850 set_security_token(p
);
852 error
= execve(p
,&init_exec_args
,retval
);
859 * Convert a load_return_t to an errno.
862 load_return_to_errno(load_return_t lrtn
)
885 * exec_check_access()
888 check_exec_access(p
, vp
, vap
)
896 if (error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
, p
))
899 if (flag
& P_TRACED
) {
900 if (error
= VOP_ACCESS(vp
, VREAD
, p
->p_ucred
, p
))
903 if (vp
->v_type
!= VREG
||
904 (vap
->va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)