]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/kern/kern_exec.c
xnu-344.21.73.tar.gz
[apple/xnu.git] / bsd / kern / kern_exec.c
... / ...
CommitLineData
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
114int app_profile = 0;
115
116extern vm_map_t bsd_pageable_map;
117
118#define ROUND_PTR(type, addr) \
119 (type *)( ( (unsigned)(addr) + 16 - 1) \
120 & ~(16 - 1) )
121
122static int load_return_to_errno(load_return_t lrtn);
123int execve(struct proc *p, struct execve_args *uap, register_t *retval);
124static int execargs_alloc(vm_offset_t *addrp);
125static int execargs_free(vm_offset_t addr);
126
127int
128execv(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 */
138int
139execve(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 clean_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
282again:
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, &clean_regions);
539 }
540
541 lret = load_machfile(vp, mach_header, arch_offset,
542 arch_size, &load_result, thr_act, map, clean_regions);
543
544 if (lret != LOAD_SUCCESS) {
545 error = load_return_to_errno(lret);
546 goto badtoolate;
547 }
548
549 /* load_machfile() maps the vnode */
550 ubc_map(vp);
551
552 /*
553 * deal with set[ug]id.
554 */
555 p->p_flag &= ~P_SUGID;
556 if (((origvattr.va_mode & VSUID) != 0 &&
557 p->p_ucred->cr_uid != origvattr.va_uid)
558 || (origvattr.va_mode & VSGID) != 0 &&
559 p->p_ucred->cr_gid != origvattr.va_gid) {
560 p->p_ucred = crcopy(cred);
561#if KTRACE
562 /*
563 * If process is being ktraced, turn off - unless
564 * root set it.
565 */
566 if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
567 struct vnode *tvp = p->p_tracep;
568 p->p_tracep = NULL;
569 p->p_traceflag = 0;
570 vrele(tvp);
571 }
572#endif
573 if (origvattr.va_mode & VSUID)
574 p->p_ucred->cr_uid = origvattr.va_uid;
575 if (origvattr.va_mode & VSGID)
576 p->p_ucred->cr_gid = origvattr.va_gid;
577
578 set_security_token(p);
579 p->p_flag |= P_SUGID;
580
581 /* Radar 2261856; setuid security hole fix */
582 /* Patch from OpenBSD: A. Ramesh */
583 /*
584 * XXX For setuid processes, attempt to ensure that
585 * stdin, stdout, and stderr are already allocated.
586 * We do not want userland to accidentally allocate
587 * descriptors in this range which has implied meaning
588 * to libc.
589 */
590 for (i = 0; i < 3; i++) {
591 extern struct fileops vnops;
592 struct nameidata nd1;
593 struct file *fp;
594 int indx;
595
596 if (p->p_fd->fd_ofiles[i] == NULL) {
597 if ((error = falloc(p, &fp, &indx)) != 0)
598 continue;
599 NDINIT(&nd1, LOOKUP, FOLLOW, UIO_SYSSPACE,
600 "/dev/null", p);
601 if ((error = vn_open(&nd1, FREAD, 0)) != 0) {
602 ffree(fp);
603 p->p_fd->fd_ofiles[indx] = NULL;
604 break;
605 }
606 fp->f_flag = FREAD;
607 fp->f_type = DTYPE_VNODE;
608 fp->f_ops = &vnops;
609 fp->f_data = (caddr_t)nd1.ni_vp;
610 VOP_UNLOCK(nd1.ni_vp, 0, p);
611 }
612 }
613 }
614 p->p_cred->p_svuid = p->p_ucred->cr_uid;
615 p->p_cred->p_svgid = p->p_ucred->cr_gid;
616
617 if (!vfexec && (p->p_flag & P_TRACED))
618 psignal(p, SIGTRAP);
619
620 if (error) {
621 goto badtoolate;
622 }
623 VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
624 vput(vp);
625 vp = NULL;
626
627 if (load_result.unixproc &&
628 create_unix_stack(get_task_map(task),
629 load_result.user_stack, load_result.customstack, p)) {
630 error = load_return_to_errno(LOAD_NOSPACE);
631 goto badtoolate;
632 }
633
634 if (vfexec) {
635 uthread->uu_ar0 = (void *)get_user_regs(thr_act);
636 }
637
638 /*
639 * Copy back arglist if necessary.
640 */
641
642
643 ucp = p->user_stack;
644 if (vfexec) {
645 old_map = vm_map_switch(get_task_map(task));
646 }
647 if (load_result.unixproc) {
648 int pathptr;
649
650 ucp = ucp - nc - NBPW; /* begining of the STRING AREA */
651
652 /*
653 * Support for new app package launching for Mac OS X allocates
654 * the "path" at the begining of the execargs buffer.
655 * copy it just before the string area.
656 */
657 savedpathlen = (savedpathlen + NBPW-1) & ~(NBPW-1);
658 len = 0;
659 pathptr = ucp - savedpathlen;
660 error = copyoutstr(savedpath, (caddr_t)pathptr,
661 (unsigned)savedpathlen, &len);
662 if (error) {
663 if (vfexec)
664 vm_map_switch(old_map);
665 goto badtoolate;
666 }
667
668 /* Save a NULL pointer below it */
669 (void) suword((caddr_t)(pathptr - NBPW), 0);
670
671 /* Save the pointer to "path" just below it */
672 (void) suword((caddr_t)(pathptr - 2*NBPW), pathptr);
673
674 /*
675 * na includes arg[] and env[].
676 * NBPW for 2 NULL one each ofter arg[argc -1] and env[n]
677 * NBPW for argc
678 * skip over saved path, NBPW for pointer to path,
679 * and NBPW for the NULL after pointer to path.
680 */
681 ap = ucp - na*NBPW - 3*NBPW - savedpathlen - 2*NBPW;
682#if defined(ppc)
683 thread_setuserstack(thr_act, ap); /* Set the stack */
684#else
685 uthread->uu_ar0[SP] = ap;
686#endif
687 (void) suword((caddr_t)ap, na-ne); /* argc */
688 nc = 0;
689 cc = 0;
690
691 cp = (char *) execargsp;
692 cc = NCARGS - savedpathlen - 2*NBPW;
693 ps.ps_argvstr = (char *)ucp; /* first argv string */
694 ps.ps_nargvstr = na - ne; /* argc */
695 for (;;) {
696 ap += NBPW;
697 if (na == ne) {
698 (void) suword((caddr_t)ap, 0);
699 ap += NBPW;
700 ps.ps_envstr = (char *)ucp;
701 ps.ps_nenvstr = ne;
702 }
703 if (--na < 0)
704 break;
705 (void) suword((caddr_t)ap, ucp);
706 do {
707 error = copyoutstr(cp, (caddr_t)ucp,
708 (unsigned)cc, &len);
709 ucp += len;
710 cp += len;
711 nc += len;
712 cc -= len;
713 } while (error == ENAMETOOLONG);
714 if (error == EFAULT)
715 break; /* bad stack - user's problem */
716 }
717 (void) suword((caddr_t)ap, 0);
718 }
719
720 if (load_result.dynlinker) {
721#if defined(ppc)
722 ap = thread_adjuserstack(thr_act, -4); /* Adjust the stack */
723#else
724 ap = uthread->uu_ar0[SP] -= 4;
725#endif
726 (void) suword((caddr_t)ap, load_result.mach_header);
727 }
728
729 if (vfexec) {
730 vm_map_switch(old_map);
731 }
732#if defined(ppc)
733 thread_setentrypoint(thr_act, load_result.entry_point); /* Set the entry point */
734#elif defined(i386)
735 uthread->uu_ar0[PC] = load_result.entry_point;
736#else
737#error architecture not implemented!
738#endif
739
740 /* Stop profiling */
741 stopprofclock(p);
742
743 /*
744 * Reset signal state.
745 */
746 execsigs(p, thr_act);
747
748 /*
749 * Close file descriptors
750 * which specify close-on-exec.
751 */
752 fdexec(p);
753 /* FIXME: Till vmspace inherit is fixed: */
754 if (!vfexec && p->vm_shm)
755 shmexit(p);
756 /* Clean up the semaphores */
757 semexit(p);
758
759 /*
760 * Remember file name for accounting.
761 */
762 p->p_acflag &= ~AFORK;
763 if (nd.ni_cnd.cn_namelen > MAXCOMLEN)
764 nd.ni_cnd.cn_namelen = MAXCOMLEN;
765 bcopy((caddr_t)nd.ni_cnd.cn_nameptr, (caddr_t)p->p_comm,
766 (unsigned)nd.ni_cnd.cn_namelen);
767 p->p_comm[nd.ni_cnd.cn_namelen] = '\0';
768
769 {
770 /* This is for kdebug */
771 long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4;
772
773 /* Collect the pathname for tracing */
774 kdbg_trace_string(p, &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4);
775
776 if (vfexec)
777 KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_STRING, 2)) | DBG_FUNC_NONE,
778 dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, getshuttle_thread(thr_act));
779 else
780 KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING, 2)) | DBG_FUNC_NONE,
781 dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0);
782 }
783
784 /*
785 * mark as execed, wakeup the process that vforked (if any) and tell
786 * it that it now has it's own resources back
787 */
788 p->p_flag |= P_EXEC;
789 if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
790 p->p_flag &= ~P_PPWAIT;
791 wakeup((caddr_t)p->p_pptr);
792 }
793
794 if (vfexec && (p->p_flag & P_TRACED)) {
795 psignal_vfork(p, new_task, thr_act, SIGTRAP);
796 }
797
798badtoolate:
799 if (vfexec) {
800 task_deallocate(new_task);
801 act_deallocate(thr_act);
802 if (error)
803 error = 0;
804 }
805bad:
806 FREE_ZONE(nd.ni_cnd.cn_pnbuf, nd.ni_cnd.cn_pnlen, M_NAMEI);
807 if (vp)
808 vput(vp);
809bad1:
810 if (execargs)
811 execargs_free(execargs);
812 if (!error && vfexec) {
813 vfork_return(current_act(), p->p_pptr, p, retval);
814 (void) thread_resume(thr_act);
815 return(0);
816 }
817 return(error);
818}
819
820
821#define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur)
822
823kern_return_t
824create_unix_stack(map, user_stack, customstack, p)
825 vm_map_t map;
826 vm_offset_t user_stack;
827 int customstack;
828 struct proc *p;
829{
830 vm_size_t size;
831 vm_offset_t addr;
832
833 p->user_stack = user_stack;
834 if (!customstack) {
835 size = round_page_64(unix_stack_size(p));
836 addr = trunc_page_32(user_stack - size);
837 return (vm_allocate(map,&addr, size, FALSE));
838 } else
839 return(KERN_SUCCESS);
840}
841
842#include <sys/reboot.h>
843
844char init_program_name[128] = "/sbin/mach_init\0";
845
846char init_args[128] = "";
847
848struct execve_args init_exec_args;
849int init_attempts = 0;
850
851
852void
853load_init_program(p)
854 struct proc *p;
855{
856 vm_offset_t init_addr;
857 int *old_ap;
858 char *argv[3];
859 int error;
860 register_t retval[2];
861 struct uthread * ut;
862
863 error = 0;
864
865 /* init_args are copied in string form directly from bootstrap */
866
867 do {
868 if (boothowto & RB_INITNAME) {
869 printf("init program? ");
870#if FIXME /* [ */
871 gets(init_program_name, init_program_name);
872#endif /* FIXME ] */
873 }
874
875 if (error && ((boothowto & RB_INITNAME) == 0) &&
876 (init_attempts == 1)) {
877 static char other_init[] = "/etc/mach_init";
878 printf("Load of %s, errno %d, trying %s\n",
879 init_program_name, error, other_init);
880 error = 0;
881 bcopy(other_init, init_program_name,
882 sizeof(other_init));
883 }
884
885 init_attempts++;
886
887 if (error) {
888 printf("Load of %s failed, errno %d\n",
889 init_program_name, error);
890 error = 0;
891 boothowto |= RB_INITNAME;
892 continue;
893 }
894
895 /*
896 * Copy out program name.
897 */
898
899 init_addr = VM_MIN_ADDRESS;
900 (void) vm_allocate(current_map(), &init_addr,
901 PAGE_SIZE, TRUE);
902 if (init_addr == 0)
903 init_addr++;
904 (void) copyout((caddr_t) init_program_name,
905 (caddr_t) (init_addr),
906 (unsigned) sizeof(init_program_name)+1);
907
908 argv[0] = (char *) init_addr;
909 init_addr += sizeof(init_program_name);
910 init_addr = (vm_offset_t)ROUND_PTR(char, init_addr);
911
912 /*
913 * Put out first (and only) argument, similarly.
914 * Assumes everything fits in a page as allocated
915 * above.
916 */
917
918 (void) copyout((caddr_t) init_args,
919 (caddr_t) (init_addr),
920 (unsigned) sizeof(init_args));
921
922 argv[1] = (char *) init_addr;
923 init_addr += sizeof(init_args);
924 init_addr = (vm_offset_t)ROUND_PTR(char, init_addr);
925
926 /*
927 * Null-end the argument list
928 */
929
930 argv[2] = (char *) 0;
931
932 /*
933 * Copy out the argument list.
934 */
935
936 (void) copyout((caddr_t) argv,
937 (caddr_t) (init_addr),
938 (unsigned) sizeof(argv));
939
940 /*
941 * Set up argument block for fake call to execve.
942 */
943
944 init_exec_args.fname = argv[0];
945 init_exec_args.argp = (char **) init_addr;
946 init_exec_args.envp = 0;
947
948 /* So that mach_init task
949 * is set with uid,gid 0 token
950 */
951 set_security_token(p);
952
953 error = execve(p,&init_exec_args,retval);
954 } while (error);
955}
956
957/*
958 * Convert a load_return_t to an errno.
959 */
960static int
961load_return_to_errno(load_return_t lrtn)
962{
963 switch (lrtn) {
964 case LOAD_SUCCESS:
965 return 0;
966 case LOAD_BADARCH:
967 return EBADARCH;
968 case LOAD_BADMACHO:
969 return EBADMACHO;
970 case LOAD_SHLIB:
971 return ESHLIBVERS;
972 case LOAD_NOSPACE:
973 return ENOMEM;
974 case LOAD_PROTECT:
975 return EACCES;
976 case LOAD_RESOURCE:
977 case LOAD_FAILURE:
978 default:
979 return EBADEXEC;
980 }
981}
982
983/*
984 * exec_check_access()
985 */
986int
987check_exec_access(p, vp, vap)
988 struct proc *p;
989 struct vnode *vp;
990 struct vattr *vap;
991{
992 int flag;
993 int error;
994
995 if (error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p))
996 return (error);
997 flag = p->p_flag;
998 if (flag & P_TRACED) {
999 if (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p))
1000 return (error);
1001 }
1002 if (vp->v_type != VREG ||
1003 (vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
1004 return (EACCES);
1005 return (0);
1006}
1007
1008#include <mach/mach_types.h>
1009#include <mach/vm_prot.h>
1010#include <mach/semaphore.h>
1011#include <mach/sync_policy.h>
1012#include <kern/clock.h>
1013#include <mach/kern_return.h>
1014
1015extern semaphore_t execve_semaphore;
1016
1017static int
1018execargs_alloc(addrp)
1019 vm_offset_t *addrp;
1020{
1021 kern_return_t kret;
1022
1023 kret = semaphore_wait(execve_semaphore);
1024 if (kret != KERN_SUCCESS)
1025 switch (kret) {
1026 default:
1027 return (EINVAL);
1028 case KERN_INVALID_ADDRESS:
1029 case KERN_PROTECTION_FAILURE:
1030 return (EACCES);
1031 case KERN_ABORTED:
1032 case KERN_OPERATION_TIMED_OUT:
1033 return (EINTR);
1034 }
1035
1036 kret = kmem_alloc_pageable(bsd_pageable_map, addrp, NCARGS);
1037 if (kret != KERN_SUCCESS)
1038 return (ENOMEM);
1039
1040 return (0);
1041}
1042
1043static int
1044execargs_free(addr)
1045 vm_offset_t addr;
1046{
1047 kern_return_t kret;
1048
1049 kmem_free(bsd_pageable_map, addr, NCARGS);
1050
1051 kret = semaphore_signal(execve_semaphore);
1052 switch (kret) {
1053 case KERN_INVALID_ADDRESS:
1054 case KERN_PROTECTION_FAILURE:
1055 return (EINVAL);
1056 case KERN_ABORTED:
1057 case KERN_OPERATION_TIMED_OUT:
1058 return (EINTR);
1059 case KERN_SUCCESS:
1060 return(0);
1061 default:
1062 return (EINVAL);
1063 }
1064}
1065