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