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