2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Mach Operating System
28 * Copyright (c) 1987 Carnegie-Mellon University
29 * All rights reserved. The CMU software License Agreement specifies
30 * the terms and conditions for use and redistribution.
36 * Copyright (c) 1982, 1986, 1991, 1993
37 * The Regents of the University of California. All rights reserved.
38 * (c) UNIX System Laboratories, Inc.
39 * All or some portions of this file are derived from material licensed
40 * to the University of California by American Telephone and Telegraph
41 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
42 * the permission of UNIX System Laboratories, Inc.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * from: @(#)kern_exec.c 8.1 (Berkeley) 6/10/93
74 #include <machine/reg.h>
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/filedesc.h>
79 #include <sys/kernel.h>
83 #include <sys/socketvar.h>
84 #include <sys/malloc.h>
85 #include <sys/namei.h>
86 #include <sys/mount.h>
87 #include <sys/vnode.h>
92 #include <sys/kern_audit.h>
94 #include <sys/kdebug.h>
95 #include <sys/signal.h>
96 #include <sys/aio_kern.h>
98 #include <mach/vm_param.h>
100 #include <vm/vm_map.h>
102 extern vm_map_t
vm_map_switch(vm_map_t map
); /* XXX */
104 #include <vm/vm_kern.h>
105 #include <vm/vm_shared_memory_server.h>
107 #include <kern/thread.h>
108 #include <kern/task.h>
110 #include <kern/ast.h>
111 #include <kern/mach_loader.h>
112 #include <mach-o/fat.h>
113 #include <mach-o/loader.h>
114 #include <machine/vmparam.h>
116 #include <sys/ktrace.h>
121 extern vm_map_t bsd_pageable_map
;
123 #define ROUND_PTR(type, addr) \
124 (type *)( ( (unsigned)(addr) + 16 - 1) \
127 static int load_return_to_errno(load_return_t lrtn
);
128 int execve(struct proc
*p
, struct execve_args
*uap
, register_t
*retval
);
129 static int execargs_alloc(vm_offset_t
*addrp
);
130 static int execargs_free(vm_offset_t addr
);
133 execv(p
, args
, retval
)
138 ((struct execve_args
*)args
)->envp
= NULL
;
139 return (execve(p
, args
, retval
));
142 extern char classichandler
[32];
143 extern long classichandler_fsid
;
144 extern long classichandler_fileid
;
147 * Helper routine to get rid of a loop in execve. Given a pointer to
148 * something for the arg list (which might be in kernel space or in user
149 * space), copy it into the kernel buffer at the currentWritePt. This code
150 * does the proper thing to get the data transferred.
151 * bytesWritten, currentWritePt, and bytesLeft are kept up-to-date.
154 static int copyArgument(char *argument
, int pointerInKernel
,
155 int *bytesWritten
,char **currentWritePt
,
160 if (*bytesLeft
<= 0) {
164 if (pointerInKernel
== UIO_SYSSPACE
) {
165 error
= copystr(argument
, *currentWritePt
, (unsigned)*bytesLeft
, &len
);
168 * pointer in kernel == UIO_USERSPACE
169 * Copy in from user space.
171 error
= copyinstr((caddr_t
)argument
, *currentWritePt
, (unsigned)*bytesLeft
,
174 *currentWritePt
+= len
;
175 *bytesWritten
+= len
;
177 } while (error
== ENAMETOOLONG
);
183 execve(p
, uap
, retval
)
184 register struct proc
*p
;
185 register struct execve_args
*uap
;
188 register struct ucred
*cred
= p
->p_ucred
;
189 register struct filedesc
*fdp
= p
->p_fd
;
192 int na
, ne
, ucp
, ap
, cc
;
194 int executingInterpreter
=0;
196 int executingClassic
=0;
197 char binaryWithClassicName
[sizeof(p
->p_comm
)] = {0};
201 struct vattr origvattr
;
202 vm_offset_t execargs
;
204 struct ps_strings ps
;
206 /* Argument(s) to an interpreter. If we're executing a shell
207 * script, the name (#!/bin/csh) is allowed to be followed by
208 * arguments. cfarg holds these arguments.
213 struct mach_header
*mach_header
;
214 struct fat_header
*fat_header
;
215 struct fat_arch fat_arch
;
217 load_result_t load_result
;
218 struct uthread
*uthread
;
222 boolean_t clean_regions
= FALSE
;
223 shared_region_mapping_t shared_region
= NULL
;
224 shared_region_mapping_t initial_region
= NULL
;
227 /* #! and name of interpreter */
228 char ex_shell
[SHSIZE
];
229 /* Mach-O executable */
230 struct mach_header mach_header
;
232 struct fat_header fat_header
;
237 int savedpathlen
= 0;
238 vm_offset_t
*execargsp
;
242 thread_act_t thr_act
;
245 unsigned long arch_offset
=0;
246 unsigned long arch_size
= 0;
247 char *ws_cache_name
= NULL
; /* used for pre-heat */
250 * XXXAUDIT: Currently, we only audit the pathname of the binary.
251 * There may also be poor interaction with dyld.
254 cfarg
[0] = '\0'; /* initialize to null value. */
255 task
= current_task();
256 thr_act
= current_act();
257 uthread
= get_bsdthread_info(thr_act
);
259 if (uthread
->uu_flag
& P_VFORK
) {
260 vfexec
= 1; /* Mark in exec */
262 if (task
!= kernel_task
) {
263 numthreads
= get_task_numacts(task
);
264 if (numthreads
<= 0 )
266 if (numthreads
> 1) {
272 error
= execargs_alloc(&execargs
);
276 savedpath
= (char *)execargs
;
279 * To support new app package launching for Mac OS X, the dyld
280 * needs the first argument to execve() stored on the user stack.
281 * Copyin the "path" at the begining of the "execargs" buffer
284 * We have to do this before namei() because in case of
285 * symbolic links, namei() would overwrite the original "path".
286 * In case the last symbolic link resolved was a relative pathname
287 * we would lose the original "path", which could be an
288 * absolute pathname. This might be unacceptable for dyld.
290 /* XXX We could optimize to avoid copyinstr in the namei() */
293 * XXXAUDIT: Note: the double copyin introduces an audit
294 * race. To correct this race, we must use a single
298 error
= copyinstr(uap
->fname
, savedpath
,
299 MAXPATHLEN
, (size_t *)&savedpathlen
);
301 execargs_free(execargs
);
305 * copyinstr will put in savedpathlen, the count of
306 * characters (including NULL) in the path.
307 * No app profiles under chroot
310 if((fdp
->fd_rdir
== NULLVP
) && (app_profile
!= 0)) {
312 /* grab the name of the file out of its path */
313 /* we will need this for lookup within the */
315 ws_cache_name
= savedpath
+ savedpathlen
;
316 while (ws_cache_name
[0] != '/') {
317 if(ws_cache_name
== savedpath
) {
326 /* Save the name aside for future use */
327 execargsp
= (vm_offset_t
*)((char *)(execargs
) + savedpathlen
);
329 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| SAVENAME
| AUDITVNPATH1
,
330 UIO_USERSPACE
, uap
->fname
, p
);
335 VOP_LEASE(vp
, p
, p
->p_ucred
, LEASE_READ
);
337 if ((error
= VOP_GETATTR(vp
, &origvattr
, p
->p_ucred
, p
)))
340 /* Check mount point */
341 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
346 if ((vp
->v_mount
->mnt_flag
& MNT_NOSUID
) || (p
->p_flag
& P_TRACED
))
347 origvattr
.va_mode
&= ~(VSUID
| VSGID
);
349 *(&vattr
) = *(&origvattr
);
352 error
= check_exec_access(p
, vp
, &vattr
);
357 * Read in first few bytes of file for segment sizes, magic number:
358 * 407 = plain executable
360 * 413 = demand paged RO text
361 * Also an ASCII line beginning with #! is
362 * the file name of a ``shell'' and arguments may be prepended
363 * to the argument list if given here.
365 * SHELL NAMES ARE LIMITED IN LENGTH.
367 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
371 exdata
.ex_shell
[0] = '\0'; /* for zero length files */
373 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
, sizeof (exdata
), 0,
374 UIO_SYSSPACE
, IO_NODELOCKED
, p
->p_ucred
, &resid
, p
);
380 if (resid
> sizeof(exdata
) - min(sizeof(exdata
.mach_header
),
381 sizeof(exdata
.fat_header
))
382 && exdata
.ex_shell
[0] != '#') {
387 mach_header
= &exdata
.mach_header
;
388 fat_header
= &exdata
.fat_header
;
389 if ((mach_header
->magic
== MH_CIGAM
) &&
390 (classichandler
[0] == 0)) {
393 } else if ((mach_header
->magic
== MH_MAGIC
) ||
394 (mach_header
->magic
== MH_CIGAM
)) {
396 } else if ((fat_header
->magic
== FAT_MAGIC
) ||
397 (fat_header
->magic
== FAT_CIGAM
)) {
400 /* If we've already redirected once from an interpreted file
401 * to an interpreter, don't permit the second time.
403 if (exdata
.ex_shell
[0] != '#' ||
404 exdata
.ex_shell
[1] != '!' ||
405 executingInterpreter
) {
409 if (executingClassic
== 1) {
413 cp
= &exdata
.ex_shell
[2]; /* skip "#!" */
414 while (cp
< &exdata
.ex_shell
[SHSIZE
]) {
415 if (*cp
== '\t') /* convert all tabs to spaces */
417 else if (*cp
== '\n' || *cp
== '#') {
418 *cp
= '\0'; /* trunc the line at nl or comment */
420 /* go back and remove the spaces before the /n or # */
421 /* todo: do we have to do this if we fix the passing of args to shells ? */
422 if ( cp
!= &exdata
.ex_shell
[2] ) {
427 } while ( cp
!= &exdata
.ex_shell
[2] );
437 cp
= &exdata
.ex_shell
[2];
441 while (*cp
&& *cp
!= ' ')
450 bcopy((caddr_t
)cp
, (caddr_t
)cfarg
, SHSIZE
);
454 * Support for new app package launching for Mac OS X.
455 * We are about to retry the execve() by changing the path to the
456 * interpreter name. Need to re-initialize the savedpath and
457 * savedpathlen. +1 for NULL.
459 savedpathlen
= (cpnospace
- execnamep
+ 1);
460 error
= copystr(execnamep
, savedpath
,
461 savedpathlen
, (size_t *)&savedpathlen
);
465 /* Save the name aside for future use */
466 execargsp
= (vm_offset_t
*)((char *)(execargs
) + savedpathlen
);
468 executingInterpreter
= 1;
470 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
471 nd
.ni_cnd
.cn_flags
= (nd
.ni_cnd
.cn_flags
& HASBUF
) |
472 (FOLLOW
| LOCKLEAF
| SAVENAME
);
473 nd
.ni_segflg
= UIO_SYSSPACE
;
474 nd
.ni_dirp
= execnamep
;
475 if ((error
= namei(&nd
)))
478 VOP_LEASE(vp
, p
, cred
, LEASE_READ
);
479 if ((error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
)))
485 * Collect arguments on "file" in swap space.
492 * Support for new app package launching for Mac OS X allocates
493 * the "path" at the begining.
494 * execargs get allocated after that
496 cp
= (char *) execargsp
; /* running pointer for copy */
498 * size of execargs less sizeof "path",
499 * a pointer to "path" and a NULL poiter
501 cc
= NCARGS
- savedpathlen
- 2*NBPW
;
503 * Copy arguments into file in argdev area.
508 * If we have a fat file, find "our" executable.
512 * Look up our architecture in the fat file.
514 lret
= fatfile_getarch_affinity(vp
,(vm_offset_t
)fat_header
, &fat_arch
,
515 (p
->p_flag
& P_AFFINITY
));
516 if (lret
!= LOAD_SUCCESS
) {
517 error
= load_return_to_errno(lret
);
520 /* Read the Mach-O header out of it */
521 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&exdata
.mach_header
,
522 sizeof (exdata
.mach_header
),
524 UIO_SYSSPACE
, (IO_UNIT
|IO_NODELOCKED
), cred
, &resid
, p
);
530 /* Did we read a complete header? */
536 /* Is what we found a Mach-O executable */
537 if ((mach_header
->magic
!= MH_MAGIC
) &&
538 (mach_header
->magic
!= MH_CIGAM
)) {
543 arch_offset
= fat_arch
.offset
;
544 arch_size
= fat_arch
.size
;
547 * Load the Mach-O file.
550 arch_size
= (u_long
)vattr
.va_size
;
553 if ( ! check_cpu_subtype(mach_header
->cpusubtype
) ) {
558 if (mach_header
->magic
== MH_CIGAM
) {
560 int classicBinaryLen
= nd
.ni_cnd
.cn_namelen
;
561 if (classicBinaryLen
> MAXCOMLEN
)
562 classicBinaryLen
= MAXCOMLEN
;
563 bcopy((caddr_t
)nd
.ni_cnd
.cn_nameptr
,
564 (caddr_t
)binaryWithClassicName
,
565 (unsigned)classicBinaryLen
);
566 binaryWithClassicName
[classicBinaryLen
] = '\0';
567 executingClassic
= 1;
569 vput(vp
); /* cleanup? */
570 nd
.ni_cnd
.cn_nameiop
= LOOKUP
;
572 nd
.ni_cnd
.cn_flags
= (nd
.ni_cnd
.cn_flags
& HASBUF
) |
573 /* (FOLLOW | LOCKLEAF | SAVENAME) */
574 (LOCKLEAF
| SAVENAME
);
575 nd
.ni_segflg
= UIO_SYSSPACE
;
577 nd
.ni_dirp
= classichandler
;
578 if ((error
= namei(&nd
)) != 0) {
584 VOP_LEASE(vp
,p
,cred
,LEASE_READ
);
585 if ((error
= VOP_GETATTR(vp
,&vattr
,p
->p_ucred
,p
))) {
591 if (uap
->argp
!= NULL
) {
592 /* geez -- why would argp ever be NULL, and why would we proceed? */
594 /* First, handle any argument massaging */
595 if (executingInterpreter
&& executingClassic
) {
596 error
= copyArgument(classichandler
,UIO_SYSSPACE
,&nc
,&cp
,&cc
);
600 /* Now name the interpreter. */
601 error
= copyArgument(savedpath
,UIO_SYSSPACE
,&nc
,&cp
,&cc
);
606 * if we're running an interpreter, as we'd be passing the
607 * command line executable as an argument to the interpreter already.
608 * Doing "execve("myShellScript","bogusName",arg1,arg2,...)
609 * probably shouldn't ever let bogusName be seen by the shell
614 error
= copyArgument(cfarg
,UIO_SYSSPACE
,&nc
,&cp
,&cc
);
619 char* originalExecutable
= uap
->fname
;
620 error
= copyArgument(originalExecutable
,UIO_USERSPACE
,&nc
,&cp
,&cc
);
622 /* remove argv[0] b/c we've already placed it at */
627 /* and continue with rest of the arguments. */
628 } else if (executingClassic
) {
629 error
= copyArgument(classichandler
,UIO_SYSSPACE
,&nc
,&cp
,&cc
);
633 char* originalExecutable
= uap
->fname
;
634 error
= copyArgument(originalExecutable
,UIO_USERSPACE
,&nc
,&cp
,&cc
);
639 /* and rest of arguments continue as before. */
640 } else if (executingInterpreter
) {
641 char *actualExecutable
= nd
.ni_cnd
.cn_nameptr
;
642 error
= copyArgument(actualExecutable
,UIO_SYSSPACE
,&nc
,&cp
,&cc
);
644 /* remove argv[0] b/c we just placed it in the arg list. */
647 /* Copy the argument in the interpreter first line if there
651 error
= copyArgument(cfarg
,UIO_SYSSPACE
,&nc
,&cp
,&cc
);
656 /* copy the name of the file being interpreted, gotten from
657 * the structures passed in to execve.
659 error
= copyArgument(uap
->fname
,UIO_USERSPACE
,&nc
,&cp
,&cc
);
662 /* Now, get rest of arguments */
663 while (uap
->argp
!= NULL
) {
664 char* userArgument
= (char*)fuword((caddr_t
) uap
->argp
);
666 if (userArgument
== NULL
) {
668 } else if ((int)userArgument
== -1) {
669 /* Um... why would it be -1? */
673 error
= copyArgument(userArgument
, UIO_USERSPACE
,&nc
,&cp
,&cc
);
677 /* Now, get the environment */
678 while (uap
->envp
!= NULL
) {
679 char *userEnv
= (char*) fuword((caddr_t
) uap
->envp
);
681 if (userEnv
== NULL
) {
683 } else if ((int)userEnv
== -1) {
687 error
= copyArgument(userEnv
,UIO_USERSPACE
,&nc
,&cp
,&cc
);
694 /* make sure there are nulls are the end!! */
703 /* and round up count of bytes written to next word. */
704 nc
= (nc
+ NBPW
-1) & ~(NBPW
-1);
706 if (vattr
.va_fsid
== classichandler_fsid
&&
707 vattr
.va_fileid
== classichandler_fileid
) {
708 executingClassic
= 1;
712 kern_return_t result
;
714 result
= task_create_internal(task
, FALSE
, &new_task
);
715 if (result
!= KERN_SUCCESS
)
716 printf("execve: task_create failed. Code: 0x%x\n", result
);
718 set_bsdtask_info(new_task
, p
);
722 map
= get_task_map(new_task
);
723 result
= thread_create(new_task
, &thr_act
);
724 if (result
!= KERN_SUCCESS
)
725 printf("execve: thread_create failed. Code: 0x%x\n", result
);
726 uthread
= get_bsdthread_info(thr_act
);
732 * Load the Mach-O file.
734 VOP_UNLOCK(vp
, 0, p
); /* XXX */
736 tws_handle_startup_file(task
, cred
->cr_uid
,
737 ws_cache_name
, vp
, &clean_regions
);
740 vm_get_shared_region(task
, &initial_region
);
741 int parentIsClassic
= (p
->p_flag
& P_CLASSIC
);
742 struct vnode
*rootDir
= p
->p_fd
->fd_rdir
;
744 if ((parentIsClassic
&& !executingClassic
) ||
745 (!parentIsClassic
&& executingClassic
)) {
746 shared_region
= lookup_default_shared_region(
750 machine_slot
[cpu_number()].cpu_type
));
751 if (shared_region
== NULL
) {
752 shared_region_mapping_t old_region
;
753 shared_region_mapping_t new_region
;
754 vm_get_shared_region(current_task(), &old_region
);
755 /* grrrr... this sets current_task(), not task
756 * -- they're different (usually)
758 shared_file_boot_time_init(
762 machine_slot
[cpu_number()].cpu_type
));
763 if ( current_task() != task
) {
764 vm_get_shared_region(current_task(),&new_region
);
765 vm_set_shared_region(task
,new_region
);
766 vm_set_shared_region(current_task(),old_region
);
769 vm_set_shared_region(task
, shared_region
);
771 shared_region_mapping_dealloc(initial_region
);
774 lret
= load_machfile(vp
, mach_header
, arch_offset
,
775 arch_size
, &load_result
, thr_act
, map
, clean_regions
);
777 if (lret
!= LOAD_SUCCESS
) {
778 error
= load_return_to_errno(lret
);
784 /* load_machfile() maps the vnode */
788 * deal with set[ug]id.
790 p
->p_flag
&= ~P_SUGID
;
791 if (((origvattr
.va_mode
& VSUID
) != 0 &&
792 p
->p_ucred
->cr_uid
!= origvattr
.va_uid
)
793 || (origvattr
.va_mode
& VSGID
) != 0 &&
794 p
->p_ucred
->cr_gid
!= origvattr
.va_gid
) {
795 p
->p_ucred
= crcopy(cred
);
798 * If process is being ktraced, turn off - unless
801 if (p
->p_tracep
&& !(p
->p_traceflag
& KTRFAC_ROOT
)) {
802 struct vnode
*tvp
= p
->p_tracep
;
808 if (origvattr
.va_mode
& VSUID
)
809 p
->p_ucred
->cr_uid
= origvattr
.va_uid
;
810 if (origvattr
.va_mode
& VSGID
)
811 p
->p_ucred
->cr_gid
= origvattr
.va_gid
;
814 * Have mach reset the task port. We don't want
815 * anyone who had the task port before a setuid
816 * exec to be able to access/control the task
819 ipc_task_reset(task
);
821 set_security_token(p
);
822 p
->p_flag
|= P_SUGID
;
824 /* Radar 2261856; setuid security hole fix */
825 /* Patch from OpenBSD: A. Ramesh */
827 * XXX For setuid processes, attempt to ensure that
828 * stdin, stdout, and stderr are already allocated.
829 * We do not want userland to accidentally allocate
830 * descriptors in this range which has implied meaning
833 for (i
= 0; i
< 3; i
++) {
834 extern struct fileops vnops
;
835 struct nameidata nd1
;
839 if (p
->p_fd
->fd_ofiles
[i
] == NULL
) {
840 if ((error
= falloc(p
, &fp
, &indx
)) != 0)
842 NDINIT(&nd1
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
,
844 if ((error
= vn_open(&nd1
, FREAD
, 0)) != 0) {
846 p
->p_fd
->fd_ofiles
[indx
] = NULL
;
850 fp
->f_type
= DTYPE_VNODE
;
852 fp
->f_data
= (caddr_t
)nd1
.ni_vp
;
853 VOP_UNLOCK(nd1
.ni_vp
, 0, p
);
857 p
->p_cred
->p_svuid
= p
->p_ucred
->cr_uid
;
858 p
->p_cred
->p_svgid
= p
->p_ucred
->cr_gid
;
860 KNOTE(&p
->p_klist
, NOTE_EXEC
);
862 if (!vfexec
&& (p
->p_flag
& P_TRACED
))
870 VOP_LOCK(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
); /* XXX */
874 if (load_result
.unixproc
&&
875 create_unix_stack(get_task_map(task
),
876 load_result
.user_stack
, load_result
.customstack
, p
)) {
877 error
= load_return_to_errno(LOAD_NOSPACE
);
882 uthread
->uu_ar0
= (void *)get_user_regs(thr_act
);
886 * Copy back arglist if necessary.
890 ucp
= (int)p
->user_stack
;
892 old_map
= vm_map_switch(get_task_map(task
));
894 if (load_result
.unixproc
) {
897 ucp
= ucp
- nc
- NBPW
; /* begining of the STRING AREA */
900 * Support for new app package launching for Mac OS X allocates
901 * the "path" at the begining of the execargs buffer.
902 * copy it just before the string area.
905 pathptr
= ucp
- ((savedpathlen
+ NBPW
-1) & ~(NBPW
-1));
906 error
= copyoutstr(savedpath
, (caddr_t
)pathptr
,
907 (unsigned)savedpathlen
, (size_t *)&len
);
908 savedpathlen
= (savedpathlen
+ NBPW
-1) & ~(NBPW
-1);
912 vm_map_switch(old_map
);
917 * Record the size of the arguments area so that
918 * sysctl_procargs() can return the argument area without having
919 * to parse the arguments.
921 p
->p_argslen
= (int)p
->user_stack
- pathptr
;
922 p
->p_argc
= na
- ne
; /* save argc for sysctl_procargs() */
924 /* Save a NULL pointer below it */
925 (void) suword((caddr_t
)(pathptr
- NBPW
), 0);
927 /* Save the pointer to "path" just below it */
928 (void) suword((caddr_t
)(pathptr
- 2*NBPW
), pathptr
);
931 * na includes arg[] and env[].
932 * NBPW for 2 NULL one each ofter arg[argc -1] and env[n]
934 * skip over saved path, NBPW for pointer to path,
935 * and NBPW for the NULL after pointer to path.
937 ap
= ucp
- na
*NBPW
- 3*NBPW
- savedpathlen
- 2*NBPW
;
939 thread_setuserstack(thr_act
, ap
); /* Set the stack */
941 uthread
->uu_ar0
[SP
] = ap
;
943 (void) suword((caddr_t
)ap
, na
-ne
); /* argc */
947 cp
= (char *) execargsp
;
948 cc
= NCARGS
- savedpathlen
- 2*NBPW
;
949 ps
.ps_argvstr
= (char *)ucp
; /* first argv string */
950 ps
.ps_nargvstr
= na
- ne
; /* argc */
954 (void) suword((caddr_t
)ap
, 0);
956 ps
.ps_envstr
= (char *)ucp
;
961 (void) suword((caddr_t
)ap
, ucp
);
963 error
= copyoutstr(cp
, (caddr_t
)ucp
,
964 (unsigned)cc
, (size_t *)&len
);
969 } while (error
== ENAMETOOLONG
);
971 break; /* bad stack - user's problem */
973 (void) suword((caddr_t
)ap
, 0);
976 if (load_result
.dynlinker
) {
978 ap
= thread_adjuserstack(thr_act
, -4); /* Adjust the stack */
980 ap
= uthread
->uu_ar0
[SP
] -= 4;
982 (void) suword((caddr_t
)ap
, load_result
.mach_header
);
986 vm_map_switch(old_map
);
989 thread_setentrypoint(thr_act
, load_result
.entry_point
); /* Set the entry point */
991 uthread
->uu_ar0
[PC
] = load_result
.entry_point
;
993 #error architecture not implemented!
1000 * Reset signal state.
1002 execsigs(p
, thr_act
);
1005 * Close file descriptors
1006 * which specify close-on-exec.
1011 * need to cancel async IO requests that can be cancelled and wait for those
1012 * already active. MAY BLOCK!
1016 /* FIXME: Till vmspace inherit is fixed: */
1017 if (!vfexec
&& p
->vm_shm
)
1019 /* Clean up the semaphores */
1023 * Remember file name for accounting.
1025 p
->p_acflag
&= ~AFORK
;
1026 /* If the translated name isn't NULL, then we want to use
1027 * that translated name as the name we show as the "real" name.
1028 * Otherwise, use the name passed into exec.
1030 if (0 != binaryWithClassicName
[0]) {
1031 bcopy((caddr_t
)binaryWithClassicName
, (caddr_t
)p
->p_comm
,
1032 sizeof(binaryWithClassicName
));
1034 if (nd
.ni_cnd
.cn_namelen
> MAXCOMLEN
)
1035 nd
.ni_cnd
.cn_namelen
= MAXCOMLEN
;
1036 bcopy((caddr_t
)nd
.ni_cnd
.cn_nameptr
, (caddr_t
)p
->p_comm
,
1037 (unsigned)nd
.ni_cnd
.cn_namelen
);
1038 p
->p_comm
[nd
.ni_cnd
.cn_namelen
] = '\0';
1042 /* This is for kdebug */
1043 long dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
;
1045 /* Collect the pathname for tracing */
1046 kdbg_trace_string(p
, &dbg_arg1
, &dbg_arg2
, &dbg_arg3
, &dbg_arg4
);
1052 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_DATA
, 2)) | DBG_FUNC_NONE
,
1053 p
->p_pid
,0,0,0, (unsigned int)thr_act
);
1054 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
1055 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, (unsigned int)thr_act
);
1059 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_DATA
, 2)) | DBG_FUNC_NONE
,
1061 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING
, 2)) | DBG_FUNC_NONE
,
1062 dbg_arg1
, dbg_arg2
, dbg_arg3
, dbg_arg4
, 0);
1066 if (executingClassic
)
1067 p
->p_flag
|= P_CLASSIC
| P_AFFINITY
;
1069 p
->p_flag
&= ~P_CLASSIC
;
1072 * mark as execed, wakeup the process that vforked (if any) and tell
1073 * it that it now has it's own resources back
1075 p
->p_flag
|= P_EXEC
;
1076 if (p
->p_pptr
&& (p
->p_flag
& P_PPWAIT
)) {
1077 p
->p_flag
&= ~P_PPWAIT
;
1078 wakeup((caddr_t
)p
->p_pptr
);
1081 if (vfexec
&& (p
->p_flag
& P_TRACED
)) {
1082 psignal_vfork(p
, new_task
, thr_act
, SIGTRAP
);
1087 task_deallocate(new_task
);
1088 act_deallocate(thr_act
);
1093 FREE_ZONE(nd
.ni_cnd
.cn_pnbuf
, nd
.ni_cnd
.cn_pnlen
, M_NAMEI
);
1098 execargs_free(execargs
);
1099 if (!error
&& vfexec
) {
1100 vfork_return(current_act(), p
->p_pptr
, p
, retval
);
1101 (void) thread_resume(thr_act
);
1108 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
1111 create_unix_stack(map
, user_stack
, customstack
, p
)
1113 vm_offset_t user_stack
;
1120 p
->user_stack
= (caddr_t
)user_stack
;
1122 size
= round_page_64(unix_stack_size(p
));
1123 addr
= trunc_page_32(user_stack
- size
);
1124 return (vm_allocate(map
, &addr
, size
,
1125 VM_MAKE_TAG(VM_MEMORY_STACK
) | FALSE
));
1127 return(KERN_SUCCESS
);
1130 #include <sys/reboot.h>
1132 char init_program_name
[128] = "/sbin/mach_init\0";
1134 char init_args
[128] = "";
1136 struct execve_args init_exec_args
;
1137 int init_attempts
= 0;
1141 load_init_program(p
)
1144 vm_offset_t init_addr
;
1148 register_t retval
[2];
1149 struct uthread
* ut
;
1153 /* init_args are copied in string form directly from bootstrap */
1156 if (boothowto
& RB_INITNAME
) {
1157 printf("init program? ");
1159 gets(init_program_name
, init_program_name
);
1160 #endif /* FIXME ] */
1163 if (error
&& ((boothowto
& RB_INITNAME
) == 0) &&
1164 (init_attempts
== 1)) {
1165 static char other_init
[] = "/etc/mach_init";
1166 printf("Load of %s, errno %d, trying %s\n",
1167 init_program_name
, error
, other_init
);
1169 bcopy(other_init
, init_program_name
,
1170 sizeof(other_init
));
1176 printf("Load of %s failed, errno %d\n",
1177 init_program_name
, error
);
1179 boothowto
|= RB_INITNAME
;
1184 * Copy out program name.
1187 init_addr
= VM_MIN_ADDRESS
;
1188 (void) vm_allocate(current_map(), &init_addr
,
1192 (void) copyout((caddr_t
) init_program_name
,
1193 (caddr_t
) (init_addr
),
1194 (unsigned) sizeof(init_program_name
)+1);
1196 argv
[0] = (char *) init_addr
;
1197 init_addr
+= sizeof(init_program_name
);
1198 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
1201 * Put out first (and only) argument, similarly.
1202 * Assumes everything fits in a page as allocated
1206 (void) copyout((caddr_t
) init_args
,
1207 (caddr_t
) (init_addr
),
1208 (unsigned) sizeof(init_args
));
1210 argv
[1] = (char *) init_addr
;
1211 init_addr
+= sizeof(init_args
);
1212 init_addr
= (vm_offset_t
)ROUND_PTR(char, init_addr
);
1215 * Null-end the argument list
1218 argv
[2] = (char *) 0;
1221 * Copy out the argument list.
1224 (void) copyout((caddr_t
) argv
,
1225 (caddr_t
) (init_addr
),
1226 (unsigned) sizeof(argv
));
1229 * Set up argument block for fake call to execve.
1232 init_exec_args
.fname
= argv
[0];
1233 init_exec_args
.argp
= (char **) init_addr
;
1234 init_exec_args
.envp
= 0;
1236 /* So that mach_init task
1237 * is set with uid,gid 0 token
1239 set_security_token(p
);
1241 error
= execve(p
,&init_exec_args
,retval
);
1246 * Convert a load_return_t to an errno.
1249 load_return_to_errno(load_return_t lrtn
)
1276 * exec_check_access()
1279 check_exec_access(p
, vp
, vap
)
1287 if (error
= VOP_ACCESS(vp
, VEXEC
, p
->p_ucred
, p
))
1290 if (flag
& P_TRACED
) {
1291 if (error
= VOP_ACCESS(vp
, VREAD
, p
->p_ucred
, p
))
1294 if (vp
->v_type
!= VREG
||
1295 (vap
->va_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0)
1300 #include <mach/mach_types.h>
1301 #include <mach/vm_prot.h>
1302 #include <mach/semaphore.h>
1303 #include <mach/sync_policy.h>
1304 #include <kern/clock.h>
1305 #include <mach/kern_return.h>
1307 extern semaphore_t execve_semaphore
;
1310 execargs_alloc(addrp
)
1315 kret
= semaphore_wait(execve_semaphore
);
1316 if (kret
!= KERN_SUCCESS
)
1320 case KERN_INVALID_ADDRESS
:
1321 case KERN_PROTECTION_FAILURE
:
1324 case KERN_OPERATION_TIMED_OUT
:
1328 kret
= kmem_alloc_pageable(bsd_pageable_map
, addrp
, NCARGS
);
1329 if (kret
!= KERN_SUCCESS
) {
1330 semaphore_signal(execve_semaphore
);
1342 kmem_free(bsd_pageable_map
, addr
, NCARGS
);
1344 kret
= semaphore_signal(execve_semaphore
);
1346 case KERN_INVALID_ADDRESS
:
1347 case KERN_PROTECTION_FAILURE
:
1350 case KERN_OPERATION_TIMED_OUT
: