]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_exec.c
c5f9ce4167db1795d8606bfe60cbcdf2359f9a6f
[apple/xnu.git] / bsd / kern / kern_exec.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
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.
31 */
32
33 #include <cputypes.h>
34
35 /*-
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.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
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.
59 *
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
70 * SUCH DAMAGE.
71 *
72 * from: @(#)kern_exec.c 8.1 (Berkeley) 6/10/93
73 */
74 #include <machine/reg.h>
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/filedesc.h>
79 #include <sys/kernel.h>
80 #include <sys/proc.h>
81 #include <sys/user.h>
82 #include <sys/buf.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>
88 #include <sys/file.h>
89 #include <sys/stat.h>
90 #include <sys/uio.h>
91 #include <sys/acct.h>
92 #include <sys/exec.h>
93 #include <sys/kdebug.h>
94 #include <sys/signal.h>
95
96 #include <mach/vm_param.h>
97
98 #include <vm/vm_map.h>
99 #include <vm/vm_kern.h>
100 #include <vm/vm_shared_memory_server.h>
101
102 #include <kern/thread.h>
103 #include <kern/task.h>
104
105 #include <kern/ast.h>
106 #include <kern/mach_loader.h>
107 #include <mach-o/fat.h>
108 #include <mach-o/loader.h>
109 #include <machine/vmparam.h>
110 #if KTRACE
111 #include <sys/ktrace.h>
112 #endif
113
114 int app_profile = 0;
115
116 extern vm_map_t bsd_pageable_map;
117
118 #define ROUND_PTR(type, addr) \
119 (type *)( ( (unsigned)(addr) + 16 - 1) \
120 & ~(16 - 1) )
121
122 static int load_return_to_errno(load_return_t lrtn);
123 int execve(struct proc *p, struct execve_args *uap, register_t *retval);
124 static int execargs_alloc(vm_offset_t *addrp);
125 static int execargs_free(vm_offset_t addr);
126
127 int
128 execv(p, args, retval)
129 struct proc *p;
130 void *args;
131 int *retval;
132 {
133 ((struct execve_args *)args)->envp = NULL;
134 return (execve(p, args, retval));
135 }
136
137 /* ARGSUSED */
138 int
139 execve(p, uap, retval)
140 register struct proc *p;
141 register struct execve_args *uap;
142 register_t *retval;
143 {
144 register struct ucred *cred = p->p_ucred;
145 register struct filedesc *fdp = p->p_fd;
146 register nc;
147 register char *cp;
148 int na, ne, ucp, ap, cc;
149 unsigned len;
150 int indir;
151 char *sharg;
152 char *execnamep;
153 struct vnode *vp;
154 struct vattr vattr;
155 struct vattr origvattr;
156 vm_offset_t execargs;
157 struct nameidata nd;
158 struct ps_strings ps;
159 #define SHSIZE 512
160 char cfarg[SHSIZE];
161 boolean_t is_fat;
162 kern_return_t ret;
163 struct mach_header *mach_header;
164 struct fat_header *fat_header;
165 struct fat_arch fat_arch;
166 load_return_t lret;
167 load_result_t load_result;
168 struct uthread *uthread;
169 vm_map_t old_map;
170 vm_map_t map;
171 int i;
172 boolean_t new_shared_regions = FALSE;
173 union {
174 /* #! and name of interpreter */
175 char ex_shell[SHSIZE];
176 /* Mach-O executable */
177 struct mach_header mach_header;
178 /* Fat executable */
179 struct fat_header fat_header;
180 char pad[512];
181 } exdata;
182 int resid, error;
183 char *savedpath;
184 int savedpathlen = 0;
185 vm_offset_t *execargsp;
186 char *cpnospace;
187 task_t task;
188 task_t new_task;
189 thread_act_t thr_act;
190 int numthreads;
191 int vfexec=0;
192 unsigned long arch_offset =0;
193 unsigned long arch_size = 0;
194 char *ws_cache_name = NULL; /* used for pre-heat */
195
196 task = current_task();
197 thr_act = current_act();
198 uthread = get_bsdthread_info(thr_act);
199
200 if (uthread->uu_flag & P_VFORK) {
201 vfexec = 1; /* Mark in exec */
202 } else {
203 if (task != kernel_task) {
204 numthreads = get_task_numacts(task);
205 if (numthreads <= 0 )
206 return(EINVAL);
207 if (numthreads > 1) {
208 return(EOPNOTSUPP);
209 }
210 }
211 }
212
213 error = execargs_alloc(&execargs);
214 if (error)
215 return(error);
216
217 savedpath = execargs;
218
219 /*
220 * To support new app package launching for Mac OS X, the dyld
221 * needs the first argument to execve() stored on the user stack.
222 * Copyin the "path" at the begining of the "execargs" buffer
223 * allocated above.
224 *
225 * We have to do this before namei() because in case of
226 * symbolic links, namei() would overwrite the original "path".
227 * In case the last symbolic link resolved was a relative pathname
228 * we would lose the original "path", which could be an
229 * absolute pathname. This might be unacceptable for dyld.
230 */
231 /* XXX We could optimize to avoid copyinstr in the namei() */
232
233 error = copyinstr(uap->fname, savedpath, MAXPATHLEN, &savedpathlen);
234 if (error)
235 return (error);
236 /*
237 * copyinstr will put in savedpathlen, the count of
238 * characters (including NULL) in the path.
239 */
240
241 if(app_profile != 0) {
242
243 /* grab the name of the file out of its path */
244 /* we will need this for lookup within the */
245 /* name file */
246 ws_cache_name = savedpath + savedpathlen;
247 while (ws_cache_name[0] != '/') {
248 if(ws_cache_name == savedpath) {
249 ws_cache_name--;
250 break;
251 }
252 ws_cache_name--;
253 }
254 ws_cache_name++;
255 }
256
257 /* Save the name aside for future use */
258 execargsp = (vm_offset_t *)((char *)(execargs) + savedpathlen);
259
260 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | SAVENAME,
261 UIO_USERSPACE, uap->fname, p);
262 if ((error = namei(&nd)))
263 goto bad1;
264 vp = nd.ni_vp;
265 VOP_LEASE(vp, p, p->p_ucred, LEASE_READ);
266
267 if ((error = VOP_GETATTR(vp, &origvattr, p->p_ucred, p)))
268 goto bad;
269
270 /* Check mount point */
271 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
272 error = EACCES;
273 goto bad;
274 }
275
276 indir = 0;
277 if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED))
278 origvattr.va_mode &= ~(VSUID | VSGID);
279
280 *(&vattr) = *(&origvattr);
281
282 again:
283 error = check_exec_access(p, vp, &vattr);
284 if (error)
285 goto bad;
286
287 /*
288 * Read in first few bytes of file for segment sizes, magic number:
289 * 407 = plain executable
290 * 410 = RO text
291 * 413 = demand paged RO text
292 * Also an ASCII line beginning with #! is
293 * the file name of a ``shell'' and arguments may be prepended
294 * to the argument list if given here.
295 *
296 * SHELL NAMES ARE LIMITED IN LENGTH.
297 *
298 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
299 * THE ASCII LINE.
300 */
301
302 exdata.ex_shell[0] = '\0'; /* for zero length files */
303
304 error = vn_rdwr(UIO_READ, vp, (caddr_t)&exdata, sizeof (exdata), 0,
305 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p);
306
307 if (error)
308 goto bad;
309
310 #ifndef lint
311 if (resid > sizeof(exdata) - min(sizeof(exdata.mach_header),
312 sizeof(exdata.fat_header))
313 && exdata.ex_shell[0] != '#') {
314 error = ENOEXEC;
315 goto bad;
316 }
317 #endif /* lint */
318 mach_header = &exdata.mach_header;
319 fat_header = &exdata.fat_header;
320 if (mach_header->magic == MH_MAGIC)
321 is_fat = FALSE;
322 else if (fat_header->magic == FAT_MAGIC ||
323 fat_header->magic == FAT_CIGAM)
324 is_fat = TRUE;
325 else if (mach_header->magic == MH_CIGAM) {
326 error = EBADARCH;
327 goto bad;
328 } else {
329 if (exdata.ex_shell[0] != '#' ||
330 exdata.ex_shell[1] != '!' ||
331 indir) {
332 error = ENOEXEC;
333 goto bad;
334 }
335 cp = &exdata.ex_shell[2]; /* skip "#!" */
336 while (cp < &exdata.ex_shell[SHSIZE]) {
337 if (*cp == '\t')
338 *cp = ' ';
339 else if (*cp == '\n') {
340 *cp = '\0';
341 break;
342 }
343 cp++;
344 }
345 if (*cp != '\0') {
346 error = ENOEXEC;
347 goto bad;
348 }
349 cp = &exdata.ex_shell[2];
350 while (*cp == ' ')
351 cp++;
352 execnamep = cp;
353 while (*cp && *cp != ' ')
354 cp++;
355 cfarg[0] = '\0';
356 cpnospace = cp;
357 if (*cp) {
358 *cp++ = '\0';
359 while (*cp == ' ')
360 cp++;
361 if (*cp)
362 bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE);
363 }
364
365 /*
366 * Support for new app package launching for Mac OS X.
367 * We are about to retry the execve() by changing the path to the
368 * interpreter name. Need to re-initialize the savedpath and
369 * savedpathlen. +1 for NULL.
370 */
371 savedpathlen = (cpnospace - execnamep + 1);
372 error = copystr(execnamep, savedpath, savedpathlen, &savedpathlen);
373 if (error)
374 goto bad;
375
376 /* Save the name aside for future use */
377 execargsp = (vm_offset_t *)((char *)(execargs) + savedpathlen);
378
379 indir = 1;
380 vput(vp);
381 nd.ni_cnd.cn_nameiop = LOOKUP;
382 nd.ni_cnd.cn_flags = (nd.ni_cnd.cn_flags & HASBUF) |
383 (FOLLOW | LOCKLEAF | SAVENAME);
384 nd.ni_segflg = UIO_SYSSPACE;
385 nd.ni_dirp = execnamep;
386 if ((error = namei(&nd)))
387 goto bad1;
388 vp = nd.ni_vp;
389 VOP_LEASE(vp, p, cred, LEASE_READ);
390 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)))
391 goto bad;
392 goto again;
393 }
394
395 /*
396 * Collect arguments on "file" in swap space.
397 */
398 na = 0;
399 ne = 0;
400 nc = 0;
401 cc = 0;
402 /*
403 * Support for new app package launching for Mac OS X allocates
404 * the "path" at the begining.
405 * execargs get allocated after that
406 */
407 cp = (char *) execargsp; /* running pointer for copy */
408 /*
409 * size of execargs less sizeof "path",
410 * a pointer to "path" and a NULL poiter
411 */
412 cc = NCARGS - savedpathlen - 2*NBPW;
413 /*
414 * Copy arguments into file in argdev area.
415 */
416 if (uap->argp) for (;;) {
417 ap = NULL;
418 sharg = NULL;
419 if (indir && na == 0) {
420 sharg = nd.ni_cnd.cn_nameptr;
421 ap = (int)sharg;
422 uap->argp++; /* ignore argv[0] */
423 } else if (indir && (na == 1 && cfarg[0])) {
424 sharg = cfarg;
425 ap = (int)sharg;
426 } else if (indir && (na == 1 || (na == 2 && cfarg[0])))
427 ap = (int)uap->fname;
428 else if (uap->argp) {
429 ap = fuword((caddr_t)uap->argp);
430 uap->argp++;
431 }
432 if (ap == NULL && uap->envp) {
433 uap->argp = NULL;
434 if ((ap = fuword((caddr_t)uap->envp)) != NULL)
435 uap->envp++, ne++;
436 }
437 if (ap == NULL)
438 break;
439 na++;
440 if (ap == -1) {
441 error = EFAULT;
442 break;
443 }
444 do {
445 if (nc >= (NCARGS - savedpathlen - 2*NBPW -1)) {
446 error = E2BIG;
447 break;
448 }
449 if (sharg) {
450 error = copystr(sharg, cp, (unsigned)cc, &len);
451 sharg += len;
452 } else {
453 error = copyinstr((caddr_t)ap, cp, (unsigned)cc,
454 &len);
455 ap += len;
456 }
457 cp += len;
458 nc += len;
459 cc -= len;
460 } while (error == ENAMETOOLONG);
461 if (error) {
462 goto bad;
463 }
464 }
465 nc = (nc + NBPW-1) & ~(NBPW-1);
466
467 /*
468 * If we have a fat file, find "our" executable.
469 */
470 if (is_fat) {
471 /*
472 * Look up our architecture in the fat file.
473 */
474 lret = fatfile_getarch(vp, (vm_offset_t)fat_header, &fat_arch);
475 if (lret != LOAD_SUCCESS) {
476 error = load_return_to_errno(lret);
477 goto bad;
478 }
479 /* Read the Mach-O header out of it */
480 error = vn_rdwr(UIO_READ, vp, (caddr_t)&exdata.mach_header,
481 sizeof (exdata.mach_header),
482 fat_arch.offset,
483 UIO_SYSSPACE, (IO_UNIT|IO_NODELOCKED), cred, &resid, p);
484
485 if (error) {
486 goto bad;
487 }
488
489 /* Did we read a complete header? */
490 if (resid) {
491 error = EBADEXEC;
492 goto bad;
493 }
494
495 /* Is what we found a Mach-O executable */
496 if (mach_header->magic != MH_MAGIC) {
497 error = ENOEXEC;
498 goto bad;
499 }
500
501 arch_offset = fat_arch.offset;
502 arch_size = fat_arch.size;
503 } else {
504 /*
505 * Load the Mach-O file.
506 */
507 arch_offset = 0;
508 arch_size = (u_long)vattr.va_size;
509 }
510
511 if (vfexec) {
512 kern_return_t result;
513
514 result = task_create_local(task, FALSE, FALSE, &new_task);
515 if (result != KERN_SUCCESS)
516 printf("execve: task_create failed. Code: 0x%x\n", result);
517 p->task = new_task;
518 set_bsdtask_info(new_task, p);
519 if (p->p_nice != 0)
520 resetpriority(p);
521 task = new_task;
522 map = get_task_map(new_task);
523 result = thread_create(new_task, &thr_act);
524 if (result != KERN_SUCCESS)
525 printf("execve: thread_create failed. Code: 0x%x\n", result);
526 uthread = get_bsdthread_info(thr_act);
527 } else {
528 map = VM_MAP_NULL;
529
530 }
531
532 /*
533 * Load the Mach-O file.
534 */
535 VOP_UNLOCK(vp, 0, p);
536 if(ws_cache_name) {
537 tws_handle_startup_file(task, cred->cr_uid,
538 ws_cache_name, vp, &new_shared_regions);
539 }
540 if (new_shared_regions) {
541 shared_region_mapping_t new_shared_region;
542 shared_region_mapping_t old_shared_region;
543
544 if (shared_file_create_system_region(&new_shared_region))
545 panic("couldn't create system_shared_region\n");
546
547 vm_get_shared_region(task, &old_shared_region);
548 vm_set_shared_region(task, new_shared_region);
549
550 shared_region_mapping_dealloc(old_shared_region);
551 }
552
553 lret = load_machfile(vp, mach_header, arch_offset,
554 arch_size, &load_result, thr_act, map);
555
556 if (lret != LOAD_SUCCESS) {
557 error = load_return_to_errno(lret);
558 goto badtoolate;
559 }
560
561 /* load_machfile() maps the vnode */
562 ubc_map(vp);
563
564 /*
565 * deal with set[ug]id.
566 */
567 p->p_flag &= ~P_SUGID;
568 if (((origvattr.va_mode & VSUID) != 0 &&
569 p->p_ucred->cr_uid != origvattr.va_uid)
570 || (origvattr.va_mode & VSGID) != 0 &&
571 p->p_ucred->cr_gid != origvattr.va_gid) {
572 p->p_ucred = crcopy(cred);
573 #if KTRACE
574 /*
575 * If process is being ktraced, turn off - unless
576 * root set it.
577 */
578 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
579 struct vnode *tvp = p->p_tracep;
580 p->p_tracep = NULL;
581 p->p_traceflag = 0;
582 vrele(tvp);
583 }
584 #endif
585 if (origvattr.va_mode & VSUID)
586 p->p_ucred->cr_uid = origvattr.va_uid;
587 if (origvattr.va_mode & VSGID)
588 p->p_ucred->cr_gid = origvattr.va_gid;
589
590 set_security_token(p);
591 p->p_flag |= P_SUGID;
592
593 /* Radar 2261856; setuid security hole fix */
594 /* Patch from OpenBSD: A. Ramesh */
595 /*
596 * XXX For setuid processes, attempt to ensure that
597 * stdin, stdout, and stderr are already allocated.
598 * We do not want userland to accidentally allocate
599 * descriptors in this range which has implied meaning
600 * to libc.
601 */
602 for (i = 0; i < 3; i++) {
603 extern struct fileops vnops;
604 struct nameidata nd1;
605 struct file *fp;
606 int indx;
607
608 if (p->p_fd->fd_ofiles[i] == NULL) {
609 if ((error = falloc(p, &fp, &indx)) != 0)
610 continue;
611 NDINIT(&nd1, LOOKUP, FOLLOW, UIO_SYSSPACE,
612 "/dev/null", p);
613 if ((error = vn_open(&nd1, FREAD, 0)) != 0) {
614 ffree(fp);
615 p->p_fd->fd_ofiles[indx] = NULL;
616 break;
617 }
618 fp->f_flag = FREAD;
619 fp->f_type = DTYPE_VNODE;
620 fp->f_ops = &vnops;
621 fp->f_data = (caddr_t)nd1.ni_vp;
622 VOP_UNLOCK(nd1.ni_vp, 0, p);
623 }
624 }
625 }
626 p->p_cred->p_svuid = p->p_ucred->cr_uid;
627 p->p_cred->p_svgid = p->p_ucred->cr_gid;
628
629 if (!vfexec && (p->p_flag & P_TRACED))
630 psignal(p, SIGTRAP);
631
632 if (error) {
633 goto badtoolate;
634 }
635 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
636 vput(vp);
637 vp = NULL;
638
639 if (load_result.unixproc &&
640 create_unix_stack(get_task_map(task),
641 load_result.user_stack, load_result.customstack, p)) {
642 error = load_return_to_errno(LOAD_NOSPACE);
643 goto badtoolate;
644 }
645
646 if (vfexec) {
647 uthread->uu_ar0 = (void *)get_user_regs(thr_act);
648 }
649
650 /*
651 * Copy back arglist if necessary.
652 */
653
654
655 ucp = p->user_stack;
656 if (vfexec) {
657 old_map = vm_map_switch(get_task_map(task));
658 }
659 if (load_result.unixproc) {
660 int pathptr;
661
662 ucp = ucp - nc - NBPW; /* begining of the STRING AREA */
663
664 /*
665 * Support for new app package launching for Mac OS X allocates
666 * the "path" at the begining of the execargs buffer.
667 * copy it just before the string area.
668 */
669 savedpathlen = (savedpathlen + NBPW-1) & ~(NBPW-1);
670 len = 0;
671 pathptr = ucp - savedpathlen;
672 error = copyoutstr(savedpath, (caddr_t)pathptr,
673 (unsigned)savedpathlen, &len);
674 if (error) {
675 if (vfexec)
676 vm_map_switch(old_map);
677 goto badtoolate;
678 }
679
680 /* Save a NULL pointer below it */
681 (void) suword((caddr_t)(pathptr - NBPW), 0);
682
683 /* Save the pointer to "path" just below it */
684 (void) suword((caddr_t)(pathptr - 2*NBPW), pathptr);
685
686 /*
687 * na includes arg[] and env[].
688 * NBPW for 2 NULL one each ofter arg[argc -1] and env[n]
689 * NBPW for argc
690 * skip over saved path, NBPW for pointer to path,
691 * and NBPW for the NULL after pointer to path.
692 */
693 ap = ucp - na*NBPW - 3*NBPW - savedpathlen - 2*NBPW;
694 #if defined(ppc)
695 thread_setuserstack(thr_act, ap); /* Set the stack */
696 #else
697 uthread->uu_ar0[SP] = ap;
698 #endif
699 (void) suword((caddr_t)ap, na-ne); /* argc */
700 nc = 0;
701 cc = 0;
702
703 cp = (char *) execargsp;
704 cc = NCARGS - savedpathlen - 2*NBPW;
705 ps.ps_argvstr = (char *)ucp; /* first argv string */
706 ps.ps_nargvstr = na - ne; /* argc */
707 for (;;) {
708 ap += NBPW;
709 if (na == ne) {
710 (void) suword((caddr_t)ap, 0);
711 ap += NBPW;
712 ps.ps_envstr = (char *)ucp;
713 ps.ps_nenvstr = ne;
714 }
715 if (--na < 0)
716 break;
717 (void) suword((caddr_t)ap, ucp);
718 do {
719 error = copyoutstr(cp, (caddr_t)ucp,
720 (unsigned)cc, &len);
721 ucp += len;
722 cp += len;
723 nc += len;
724 cc -= len;
725 } while (error == ENAMETOOLONG);
726 if (error == EFAULT)
727 break; /* bad stack - user's problem */
728 }
729 (void) suword((caddr_t)ap, 0);
730 }
731
732 if (load_result.dynlinker) {
733 #if defined(ppc)
734 ap = thread_adjuserstack(thr_act, -4); /* Adjust the stack */
735 #else
736 ap = uthread->uu_ar0[SP] -= 4;
737 #endif
738 (void) suword((caddr_t)ap, load_result.mach_header);
739 }
740
741 if (vfexec) {
742 vm_map_switch(old_map);
743 }
744 #if defined(ppc)
745 thread_setentrypoint(thr_act, load_result.entry_point); /* Set the entry point */
746 #elif defined(i386)
747 uthread->uu_ar0[PC] = load_result.entry_point;
748 #else
749 #error architecture not implemented!
750 #endif
751
752 /* Stop profiling */
753 stopprofclock(p);
754
755 /*
756 * Reset signal state.
757 */
758 execsigs(p, thr_act);
759
760 /*
761 * Close file descriptors
762 * which specify close-on-exec.
763 */
764 fdexec(p);
765 /* FIXME: Till vmspace inherit is fixed: */
766 if (!vfexec && p->vm_shm)
767 shmexit(p);
768 /* Clean up the semaphores */
769 semexit(p);
770
771 /*
772 * Remember file name for accounting.
773 */
774 p->p_acflag &= ~AFORK;
775 if (nd.ni_cnd.cn_namelen > MAXCOMLEN)
776 nd.ni_cnd.cn_namelen = MAXCOMLEN;
777 bcopy((caddr_t)nd.ni_cnd.cn_nameptr, (caddr_t)p->p_comm,
778 (unsigned)nd.ni_cnd.cn_namelen);
779 p->p_comm[nd.ni_cnd.cn_namelen] = '\0';
780
781 {
782 /* This is for kdebug */
783 long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
784
785 /* Collect the pathname for tracing */
786 kdbg_trace_string(p, &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
787
788 if (vfexec)
789 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_STRING, 2)) | DBG_FUNC_NONE,
790 dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, getshuttle_thread(thr_act));
791 else
792 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING, 2)) | DBG_FUNC_NONE,
793 dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0);
794 }
795
796 /*
797 * mark as execed, wakeup the process that vforked (if any) and tell
798 * it that it now has it's own resources back
799 */
800 p->p_flag |= P_EXEC;
801 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
802 p->p_flag &= ~P_PPWAIT;
803 wakeup((caddr_t)p->p_pptr);
804 }
805
806 if (vfexec && (p->p_flag & P_TRACED)) {
807 psignal_vfork(p, new_task, thr_act, SIGTRAP);
808 }
809
810 badtoolate:
811 if (vfexec) {
812 task_deallocate(new_task);
813 act_deallocate(thr_act);
814 if (error)
815 error = 0;
816 }
817 bad:
818 FREE_ZONE(nd.ni_cnd.cn_pnbuf, nd.ni_cnd.cn_pnlen, M_NAMEI);
819 if (vp)
820 vput(vp);
821 bad1:
822 if (execargs)
823 execargs_free(execargs);
824 if (!error && vfexec) {
825 vfork_return(current_act(), p->p_pptr, p, retval);
826 (void) thread_resume(thr_act);
827 return(0);
828 }
829 return(error);
830 }
831
832
833 #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
834
835 kern_return_t
836 create_unix_stack(map, user_stack, customstack, p)
837 vm_map_t map;
838 vm_offset_t user_stack;
839 int customstack;
840 struct proc *p;
841 {
842 vm_size_t size;
843 vm_offset_t addr;
844
845 p->user_stack = user_stack;
846 if (!customstack) {
847 size = round_page(unix_stack_size(p));
848 addr = trunc_page(user_stack - size);
849 return (vm_allocate(map,&addr, size, FALSE));
850 } else
851 return(KERN_SUCCESS);
852 }
853
854 #include <sys/reboot.h>
855
856 char init_program_name[128] = "/sbin/mach_init\0";
857
858 char init_args[128] = "";
859
860 struct execve_args init_exec_args;
861 int init_attempts = 0;
862
863
864 void
865 load_init_program(p)
866 struct proc *p;
867 {
868 vm_offset_t init_addr;
869 int *old_ap;
870 char *argv[3];
871 int error;
872 register_t retval[2];
873 struct uthread * ut;
874
875 error = 0;
876
877 /* init_args are copied in string form directly from bootstrap */
878
879 do {
880 if (boothowto & RB_INITNAME) {
881 printf("init program? ");
882 #if FIXME /* [ */
883 gets(init_program_name, init_program_name);
884 #endif /* FIXME ] */
885 }
886
887 if (error && ((boothowto & RB_INITNAME) == 0) &&
888 (init_attempts == 1)) {
889 static char other_init[] = "/etc/mach_init";
890 printf("Load of %s, errno %d, trying %s\n",
891 init_program_name, error, other_init);
892 error = 0;
893 bcopy(other_init, init_program_name,
894 sizeof(other_init));
895 }
896
897 init_attempts++;
898
899 if (error) {
900 printf("Load of %s failed, errno %d\n",
901 init_program_name, error);
902 error = 0;
903 boothowto |= RB_INITNAME;
904 continue;
905 }
906
907 /*
908 * Copy out program name.
909 */
910
911 init_addr = VM_MIN_ADDRESS;
912 (void) vm_allocate(current_map(), &init_addr,
913 PAGE_SIZE, TRUE);
914 if (init_addr == 0)
915 init_addr++;
916 (void) copyout((caddr_t) init_program_name,
917 (caddr_t) (init_addr),
918 (unsigned) sizeof(init_program_name)+1);
919
920 argv[0] = (char *) init_addr;
921 init_addr += sizeof(init_program_name);
922 init_addr = (vm_offset_t)ROUND_PTR(char, init_addr);
923
924 /*
925 * Put out first (and only) argument, similarly.
926 * Assumes everything fits in a page as allocated
927 * above.
928 */
929
930 (void) copyout((caddr_t) init_args,
931 (caddr_t) (init_addr),
932 (unsigned) sizeof(init_args));
933
934 argv[1] = (char *) init_addr;
935 init_addr += sizeof(init_args);
936 init_addr = (vm_offset_t)ROUND_PTR(char, init_addr);
937
938 /*
939 * Null-end the argument list
940 */
941
942 argv[2] = (char *) 0;
943
944 /*
945 * Copy out the argument list.
946 */
947
948 (void) copyout((caddr_t) argv,
949 (caddr_t) (init_addr),
950 (unsigned) sizeof(argv));
951
952 /*
953 * Set up argument block for fake call to execve.
954 */
955
956 init_exec_args.fname = argv[0];
957 init_exec_args.argp = (char **) init_addr;
958 init_exec_args.envp = 0;
959
960 /* So that mach_init task
961 * is set with uid,gid 0 token
962 */
963 set_security_token(p);
964
965 error = execve(p,&init_exec_args,retval);
966 } while (error);
967 }
968
969 /*
970 * Convert a load_return_t to an errno.
971 */
972 static int
973 load_return_to_errno(load_return_t lrtn)
974 {
975 switch (lrtn) {
976 case LOAD_SUCCESS:
977 return 0;
978 case LOAD_BADARCH:
979 return EBADARCH;
980 case LOAD_BADMACHO:
981 return EBADMACHO;
982 case LOAD_SHLIB:
983 return ESHLIBVERS;
984 case LOAD_NOSPACE:
985 return ENOMEM;
986 case LOAD_PROTECT:
987 return EACCES;
988 case LOAD_RESOURCE:
989 case LOAD_FAILURE:
990 default:
991 return EBADEXEC;
992 }
993 }
994
995 /*
996 * exec_check_access()
997 */
998 int
999 check_exec_access(p, vp, vap)
1000 struct proc *p;
1001 struct vnode *vp;
1002 struct vattr *vap;
1003 {
1004 int flag;
1005 int error;
1006
1007 if (error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p))
1008 return (error);
1009 flag = p->p_flag;
1010 if (flag & P_TRACED) {
1011 if (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p))
1012 return (error);
1013 }
1014 if (vp->v_type != VREG ||
1015 (vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
1016 return (EACCES);
1017 return (0);
1018 }
1019
1020 #include <mach/mach_types.h>
1021 #include <mach/vm_prot.h>
1022 #include <mach/semaphore.h>
1023 #include <mach/sync_policy.h>
1024 #include <kern/clock.h>
1025 #include <mach/kern_return.h>
1026
1027 extern semaphore_t execve_semaphore;
1028
1029 static int
1030 execargs_alloc(addrp)
1031 vm_offset_t *addrp;
1032 {
1033 kern_return_t kret;
1034
1035 kret = semaphore_wait(execve_semaphore);
1036 if (kret != KERN_SUCCESS)
1037 switch (kret) {
1038 default:
1039 return (EINVAL);
1040 case KERN_INVALID_ADDRESS:
1041 case KERN_PROTECTION_FAILURE:
1042 return (EACCES);
1043 case KERN_ABORTED:
1044 case KERN_OPERATION_TIMED_OUT:
1045 return (EINTR);
1046 }
1047
1048 kret = kmem_alloc_pageable(bsd_pageable_map, addrp, NCARGS);
1049 if (kret != KERN_SUCCESS)
1050 return (ENOMEM);
1051
1052 return (0);
1053 }
1054
1055 static int
1056 execargs_free(addr)
1057 vm_offset_t addr;
1058 {
1059 kern_return_t kret;
1060
1061 kmem_free(bsd_pageable_map, addr, NCARGS);
1062
1063 kret = semaphore_signal(execve_semaphore);
1064 switch (kret) {
1065 case KERN_INVALID_ADDRESS:
1066 case KERN_PROTECTION_FAILURE:
1067 return (EINVAL);
1068 case KERN_ABORTED:
1069 case KERN_OPERATION_TIMED_OUT:
1070 return (EINTR);
1071 case KERN_SUCCESS:
1072 return(0);
1073 default:
1074 return (EINVAL);
1075 }
1076 }
1077