]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2003 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/kern_audit.h> | |
93 | #include <sys/exec.h> | |
94 | #include <sys/kdebug.h> | |
95 | #include <sys/signal.h> | |
96 | #include <sys/aio_kern.h> | |
97 | ||
98 | #include <mach/vm_param.h> | |
99 | ||
100 | #include <vm/vm_map.h> | |
101 | ||
102 | extern vm_map_t vm_map_switch(vm_map_t map); /* XXX */ | |
103 | ||
104 | #include <vm/vm_kern.h> | |
105 | #include <vm/vm_shared_memory_server.h> | |
106 | ||
107 | #include <kern/thread.h> | |
108 | #include <kern/task.h> | |
109 | ||
110 | #include <kern/ast.h> | |
111 | #include <kern/mach_loader.h> | |
112 | #include <mach-o/fat.h> | |
113 | #include <mach-o/loader.h> | |
114 | #include <machine/vmparam.h> | |
115 | #if KTRACE | |
116 | #include <sys/ktrace.h> | |
117 | #endif | |
118 | ||
119 | int app_profile = 0; | |
120 | ||
121 | extern vm_map_t bsd_pageable_map; | |
122 | ||
123 | #define ROUND_PTR(type, addr) \ | |
124 | (type *)( ( (unsigned)(addr) + 16 - 1) \ | |
125 | & ~(16 - 1) ) | |
126 | ||
127 | static int load_return_to_errno(load_return_t lrtn); | |
128 | int execve(struct proc *p, struct execve_args *uap, register_t *retval); | |
129 | static int execargs_alloc(vm_offset_t *addrp); | |
130 | static int execargs_free(vm_offset_t addr); | |
131 | ||
132 | int | |
133 | execv(p, args, retval) | |
134 | struct proc *p; | |
135 | void *args; | |
136 | int *retval; | |
137 | { | |
138 | ((struct execve_args *)args)->envp = NULL; | |
139 | return (execve(p, args, retval)); | |
140 | } | |
141 | ||
142 | extern char classichandler[32]; | |
143 | extern long classichandler_fsid; | |
144 | extern long classichandler_fileid; | |
145 | ||
146 | /* | |
147 | * Helper routine to get rid of a loop in execve. Given a pointer to | |
148 | * something for the arg list (which might be in kernel space or in user | |
149 | * space), copy it into the kernel buffer at the currentWritePt. This code | |
150 | * does the proper thing to get the data transferred. | |
151 | * bytesWritten, currentWritePt, and bytesLeft are kept up-to-date. | |
152 | */ | |
153 | ||
154 | static int copyArgument(char *argument, int pointerInKernel, | |
155 | int *bytesWritten,char **currentWritePt, | |
156 | int *bytesLeft){ | |
157 | int error = 0; | |
158 | do { | |
159 | size_t len = 0; | |
160 | if (*bytesLeft <= 0) { | |
161 | error = E2BIG; | |
162 | break; | |
163 | } | |
164 | if (pointerInKernel == UIO_SYSSPACE) { | |
165 | error = copystr(argument, *currentWritePt, (unsigned)*bytesLeft, &len); | |
166 | } else { | |
167 | /* | |
168 | * pointer in kernel == UIO_USERSPACE | |
169 | * Copy in from user space. | |
170 | */ | |
171 | error = copyinstr((caddr_t)argument, *currentWritePt, (unsigned)*bytesLeft, | |
172 | &len); | |
173 | } | |
174 | *currentWritePt += len; | |
175 | *bytesWritten += len; | |
176 | *bytesLeft -= len; | |
177 | } while (error == ENAMETOOLONG); | |
178 | return error; | |
179 | } | |
180 | ||
181 | /* ARGSUSED */ | |
182 | int | |
183 | execve(p, uap, retval) | |
184 | register struct proc *p; | |
185 | register struct execve_args *uap; | |
186 | register_t *retval; | |
187 | { | |
188 | register struct ucred *cred = p->p_ucred; | |
189 | register struct filedesc *fdp = p->p_fd; | |
190 | int nc; | |
191 | char *cp; | |
192 | int na, ne, ucp, ap, cc; | |
193 | unsigned len; | |
194 | int executingInterpreter=0; | |
195 | ||
196 | int executingClassic=0; | |
197 | char binaryWithClassicName[sizeof(p->p_comm)] = {0}; | |
198 | char *execnamep; | |
199 | struct vnode *vp; | |
200 | struct vattr vattr; | |
201 | struct vattr origvattr; | |
202 | vm_offset_t execargs; | |
203 | struct nameidata nd; | |
204 | struct ps_strings ps; | |
205 | #define SHSIZE 512 | |
206 | /* Argument(s) to an interpreter. If we're executing a shell | |
207 | * script, the name (#!/bin/csh) is allowed to be followed by | |
208 | * arguments. cfarg holds these arguments. | |
209 | */ | |
210 | char cfarg[SHSIZE]; | |
211 | boolean_t is_fat; | |
212 | kern_return_t ret; | |
213 | struct mach_header *mach_header; | |
214 | struct fat_header *fat_header; | |
215 | struct fat_arch fat_arch; | |
216 | load_return_t lret; | |
217 | load_result_t load_result; | |
218 | struct uthread *uthread; | |
219 | vm_map_t old_map; | |
220 | vm_map_t map; | |
221 | int i; | |
222 | boolean_t clean_regions = FALSE; | |
223 | shared_region_mapping_t shared_region = NULL; | |
224 | shared_region_mapping_t initial_region = NULL; | |
225 | ||
226 | union { | |
227 | /* #! and name of interpreter */ | |
228 | char ex_shell[SHSIZE]; | |
229 | /* Mach-O executable */ | |
230 | struct mach_header mach_header; | |
231 | /* Fat executable */ | |
232 | struct fat_header fat_header; | |
233 | char pad[512]; | |
234 | } exdata; | |
235 | int resid, error; | |
236 | char *savedpath; | |
237 | int savedpathlen = 0; | |
238 | vm_offset_t *execargsp; | |
239 | char *cpnospace; | |
240 | task_t task; | |
241 | task_t new_task; | |
242 | thread_act_t thr_act; | |
243 | int numthreads; | |
244 | int vfexec=0; | |
245 | unsigned long arch_offset =0; | |
246 | unsigned long arch_size = 0; | |
247 | char *ws_cache_name = NULL; /* used for pre-heat */ | |
248 | ||
249 | /* | |
250 | * XXXAUDIT: Currently, we only audit the pathname of the binary. | |
251 | * There may also be poor interaction with dyld. | |
252 | */ | |
253 | ||
254 | cfarg[0] = '\0'; /* initialize to null value. */ | |
255 | task = current_task(); | |
256 | thr_act = current_act(); | |
257 | uthread = get_bsdthread_info(thr_act); | |
258 | ||
259 | if (uthread->uu_flag & P_VFORK) { | |
260 | vfexec = 1; /* Mark in exec */ | |
261 | } else { | |
262 | if (task != kernel_task) { | |
263 | numthreads = get_task_numacts(task); | |
264 | if (numthreads <= 0 ) | |
265 | return(EINVAL); | |
266 | if (numthreads > 1) { | |
267 | return(EOPNOTSUPP); | |
268 | } | |
269 | } | |
270 | } | |
271 | ||
272 | error = execargs_alloc(&execargs); | |
273 | if (error) | |
274 | return(error); | |
275 | ||
276 | savedpath = (char *)execargs; | |
277 | ||
278 | /* | |
279 | * To support new app package launching for Mac OS X, the dyld | |
280 | * needs the first argument to execve() stored on the user stack. | |
281 | * Copyin the "path" at the begining of the "execargs" buffer | |
282 | * allocated above. | |
283 | * | |
284 | * We have to do this before namei() because in case of | |
285 | * symbolic links, namei() would overwrite the original "path". | |
286 | * In case the last symbolic link resolved was a relative pathname | |
287 | * we would lose the original "path", which could be an | |
288 | * absolute pathname. This might be unacceptable for dyld. | |
289 | */ | |
290 | /* XXX We could optimize to avoid copyinstr in the namei() */ | |
291 | ||
292 | /* | |
293 | * XXXAUDIT: Note: the double copyin introduces an audit | |
294 | * race. To correct this race, we must use a single | |
295 | * copyin(). | |
296 | */ | |
297 | ||
298 | error = copyinstr(uap->fname, savedpath, | |
299 | MAXPATHLEN, (size_t *)&savedpathlen); | |
300 | if (error) { | |
301 | execargs_free(execargs); | |
302 | return(error); | |
303 | } | |
304 | /* | |
305 | * copyinstr will put in savedpathlen, the count of | |
306 | * characters (including NULL) in the path. | |
307 | * No app profiles under chroot | |
308 | */ | |
309 | ||
310 | if((fdp->fd_rdir == NULLVP) && (app_profile != 0)) { | |
311 | ||
312 | /* grab the name of the file out of its path */ | |
313 | /* we will need this for lookup within the */ | |
314 | /* name file */ | |
315 | ws_cache_name = savedpath + savedpathlen; | |
316 | while (ws_cache_name[0] != '/') { | |
317 | if(ws_cache_name == savedpath) { | |
318 | ws_cache_name--; | |
319 | break; | |
320 | } | |
321 | ws_cache_name--; | |
322 | } | |
323 | ws_cache_name++; | |
324 | } | |
325 | ||
326 | /* Save the name aside for future use */ | |
327 | execargsp = (vm_offset_t *)((char *)(execargs) + savedpathlen); | |
328 | ||
329 | NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | SAVENAME | AUDITVNPATH1, | |
330 | UIO_USERSPACE, uap->fname, p); | |
331 | error = namei(&nd); | |
332 | if (error) | |
333 | goto bad1; | |
334 | vp = nd.ni_vp; | |
335 | VOP_LEASE(vp, p, p->p_ucred, LEASE_READ); | |
336 | ||
337 | if ((error = VOP_GETATTR(vp, &origvattr, p->p_ucred, p))) | |
338 | goto bad; | |
339 | ||
340 | /* Check mount point */ | |
341 | if (vp->v_mount->mnt_flag & MNT_NOEXEC) { | |
342 | error = EACCES; | |
343 | goto bad; | |
344 | } | |
345 | ||
346 | if ((vp->v_mount->mnt_flag & MNT_NOSUID) || (p->p_flag & P_TRACED)) | |
347 | origvattr.va_mode &= ~(VSUID | VSGID); | |
348 | ||
349 | *(&vattr) = *(&origvattr); | |
350 | ||
351 | again: | |
352 | error = check_exec_access(p, vp, &vattr); | |
353 | if (error) | |
354 | goto bad; | |
355 | ||
356 | /* | |
357 | * Read in first few bytes of file for segment sizes, magic number: | |
358 | * 407 = plain executable | |
359 | * 410 = RO text | |
360 | * 413 = demand paged RO text | |
361 | * Also an ASCII line beginning with #! is | |
362 | * the file name of a ``shell'' and arguments may be prepended | |
363 | * to the argument list if given here. | |
364 | * | |
365 | * SHELL NAMES ARE LIMITED IN LENGTH. | |
366 | * | |
367 | * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM | |
368 | * THE ASCII LINE. | |
369 | */ | |
370 | ||
371 | exdata.ex_shell[0] = '\0'; /* for zero length files */ | |
372 | ||
373 | error = vn_rdwr(UIO_READ, vp, (caddr_t)&exdata, sizeof (exdata), 0, | |
374 | UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p); | |
375 | ||
376 | if (error) | |
377 | goto bad; | |
378 | ||
379 | #ifndef lint | |
380 | if (resid > sizeof(exdata) - min(sizeof(exdata.mach_header), | |
381 | sizeof(exdata.fat_header)) | |
382 | && exdata.ex_shell[0] != '#') { | |
383 | error = ENOEXEC; | |
384 | goto bad; | |
385 | } | |
386 | #endif /* lint */ | |
387 | mach_header = &exdata.mach_header; | |
388 | fat_header = &exdata.fat_header; | |
389 | if ((mach_header->magic == MH_CIGAM) && | |
390 | (classichandler[0] == 0)) { | |
391 | error = EBADARCH; | |
392 | goto bad; | |
393 | } else if ((mach_header->magic == MH_MAGIC) || | |
394 | (mach_header->magic == MH_CIGAM)) { | |
395 | is_fat = FALSE; | |
396 | } else if ((fat_header->magic == FAT_MAGIC) || | |
397 | (fat_header->magic == FAT_CIGAM)) { | |
398 | is_fat = TRUE; | |
399 | } else { | |
400 | /* If we've already redirected once from an interpreted file | |
401 | * to an interpreter, don't permit the second time. | |
402 | */ | |
403 | if (exdata.ex_shell[0] != '#' || | |
404 | exdata.ex_shell[1] != '!' || | |
405 | executingInterpreter) { | |
406 | error = ENOEXEC; | |
407 | goto bad; | |
408 | } | |
409 | if (executingClassic == 1) { | |
410 | error = EBADARCH; | |
411 | goto bad; | |
412 | } | |
413 | cp = &exdata.ex_shell[2]; /* skip "#!" */ | |
414 | while (cp < &exdata.ex_shell[SHSIZE]) { | |
415 | if (*cp == '\t') /* convert all tabs to spaces */ | |
416 | *cp = ' '; | |
417 | else if (*cp == '\n' || *cp == '#') { | |
418 | *cp = '\0'; /* trunc the line at nl or comment */ | |
419 | ||
420 | /* go back and remove the spaces before the /n or # */ | |
421 | /* todo: do we have to do this if we fix the passing of args to shells ? */ | |
422 | if ( cp != &exdata.ex_shell[2] ) { | |
423 | do { | |
424 | if ( *(cp-1) != ' ') | |
425 | break; | |
426 | *(--cp) = '\0'; | |
427 | } while ( cp != &exdata.ex_shell[2] ); | |
428 | } | |
429 | break; | |
430 | } | |
431 | cp++; | |
432 | } | |
433 | if (*cp != '\0') { | |
434 | error = ENOEXEC; | |
435 | goto bad; | |
436 | } | |
437 | cp = &exdata.ex_shell[2]; | |
438 | while (*cp == ' ') | |
439 | cp++; | |
440 | execnamep = cp; | |
441 | while (*cp && *cp != ' ') | |
442 | cp++; | |
443 | cfarg[0] = '\0'; | |
444 | cpnospace = cp; | |
445 | if (*cp) { | |
446 | *cp++ = '\0'; | |
447 | while (*cp == ' ') | |
448 | cp++; | |
449 | if (*cp) | |
450 | bcopy((caddr_t)cp, (caddr_t)cfarg, SHSIZE); | |
451 | } | |
452 | ||
453 | /* | |
454 | * Support for new app package launching for Mac OS X. | |
455 | * We are about to retry the execve() by changing the path to the | |
456 | * interpreter name. Need to re-initialize the savedpath and | |
457 | * savedpathlen. +1 for NULL. | |
458 | */ | |
459 | savedpathlen = (cpnospace - execnamep + 1); | |
460 | error = copystr(execnamep, savedpath, | |
461 | savedpathlen, (size_t *)&savedpathlen); | |
462 | if (error) | |
463 | goto bad; | |
464 | ||
465 | /* Save the name aside for future use */ | |
466 | execargsp = (vm_offset_t *)((char *)(execargs) + savedpathlen); | |
467 | ||
468 | executingInterpreter= 1; | |
469 | vput(vp); | |
470 | nd.ni_cnd.cn_nameiop = LOOKUP; | |
471 | nd.ni_cnd.cn_flags = (nd.ni_cnd.cn_flags & HASBUF) | | |
472 | (FOLLOW | LOCKLEAF | SAVENAME); | |
473 | nd.ni_segflg = UIO_SYSSPACE; | |
474 | nd.ni_dirp = execnamep; | |
475 | if ((error = namei(&nd))) | |
476 | goto bad1; | |
477 | vp = nd.ni_vp; | |
478 | VOP_LEASE(vp, p, cred, LEASE_READ); | |
479 | if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))) | |
480 | goto bad; | |
481 | goto again; | |
482 | } | |
483 | ||
484 | /* | |
485 | * Collect arguments on "file" in swap space. | |
486 | */ | |
487 | na = 0; | |
488 | ne = 0; | |
489 | nc = 0; | |
490 | cc = 0; | |
491 | /* | |
492 | * Support for new app package launching for Mac OS X allocates | |
493 | * the "path" at the begining. | |
494 | * execargs get allocated after that | |
495 | */ | |
496 | cp = (char *) execargsp; /* running pointer for copy */ | |
497 | /* | |
498 | * size of execargs less sizeof "path", | |
499 | * a pointer to "path" and a NULL poiter | |
500 | */ | |
501 | cc = NCARGS - savedpathlen - 2*NBPW; | |
502 | /* | |
503 | * Copy arguments into file in argdev area. | |
504 | */ | |
505 | ||
506 | ||
507 | /* | |
508 | * If we have a fat file, find "our" executable. | |
509 | */ | |
510 | if (is_fat) { | |
511 | /* | |
512 | * Look up our architecture in the fat file. | |
513 | */ | |
514 | lret = fatfile_getarch_affinity(vp,(vm_offset_t)fat_header, &fat_arch, | |
515 | (p->p_flag & P_AFFINITY)); | |
516 | if (lret != LOAD_SUCCESS) { | |
517 | error = load_return_to_errno(lret); | |
518 | goto bad; | |
519 | } | |
520 | /* Read the Mach-O header out of it */ | |
521 | error = vn_rdwr(UIO_READ, vp, (caddr_t)&exdata.mach_header, | |
522 | sizeof (exdata.mach_header), | |
523 | fat_arch.offset, | |
524 | UIO_SYSSPACE, (IO_UNIT|IO_NODELOCKED), cred, &resid, p); | |
525 | ||
526 | if (error) { | |
527 | goto bad; | |
528 | } | |
529 | ||
530 | /* Did we read a complete header? */ | |
531 | if (resid) { | |
532 | error = EBADEXEC; | |
533 | goto bad; | |
534 | } | |
535 | ||
536 | /* Is what we found a Mach-O executable */ | |
537 | if ((mach_header->magic != MH_MAGIC) && | |
538 | (mach_header->magic != MH_CIGAM)) { | |
539 | error = ENOEXEC; | |
540 | goto bad; | |
541 | } | |
542 | ||
543 | arch_offset = fat_arch.offset; | |
544 | arch_size = fat_arch.size; | |
545 | } else { | |
546 | /* | |
547 | * Load the Mach-O file. | |
548 | */ | |
549 | arch_offset = 0; | |
550 | arch_size = (u_long)vattr.va_size; | |
551 | } | |
552 | ||
553 | if ( ! check_cpu_subtype(mach_header->cpusubtype) ) { | |
554 | error = EBADARCH; | |
555 | goto bad; | |
556 | } | |
557 | ||
558 | if (mach_header->magic == MH_CIGAM) { | |
559 | ||
560 | int classicBinaryLen = nd.ni_cnd.cn_namelen; | |
561 | if (classicBinaryLen > MAXCOMLEN) | |
562 | classicBinaryLen = MAXCOMLEN; | |
563 | bcopy((caddr_t)nd.ni_cnd.cn_nameptr, | |
564 | (caddr_t)binaryWithClassicName, | |
565 | (unsigned)classicBinaryLen); | |
566 | binaryWithClassicName[classicBinaryLen] = '\0'; | |
567 | executingClassic = 1; | |
568 | ||
569 | vput(vp); /* cleanup? */ | |
570 | nd.ni_cnd.cn_nameiop = LOOKUP; | |
571 | ||
572 | nd.ni_cnd.cn_flags = (nd.ni_cnd.cn_flags & HASBUF) | | |
573 | /* (FOLLOW | LOCKLEAF | SAVENAME) */ | |
574 | (LOCKLEAF | SAVENAME); | |
575 | nd.ni_segflg = UIO_SYSSPACE; | |
576 | ||
577 | nd.ni_dirp = classichandler; | |
578 | if ((error = namei(&nd)) != 0) { | |
579 | error = EBADARCH; | |
580 | goto bad1; | |
581 | } | |
582 | vp = nd.ni_vp; | |
583 | ||
584 | VOP_LEASE(vp,p,cred,LEASE_READ); | |
585 | if ((error = VOP_GETATTR(vp,&vattr,p->p_ucred,p))) { | |
586 | goto bad; | |
587 | } | |
588 | goto again; | |
589 | } | |
590 | ||
591 | if (uap->argp != NULL) { | |
592 | /* geez -- why would argp ever be NULL, and why would we proceed? */ | |
593 | ||
594 | /* First, handle any argument massaging */ | |
595 | if (executingInterpreter && executingClassic) { | |
596 | error = copyArgument(classichandler,UIO_SYSSPACE,&nc,&cp,&cc); | |
597 | na++; | |
598 | if (error) goto bad; | |
599 | ||
600 | /* Now name the interpreter. */ | |
601 | error = copyArgument(savedpath,UIO_SYSSPACE,&nc,&cp,&cc); | |
602 | na++; | |
603 | if (error) goto bad; | |
604 | ||
605 | /* | |
606 | * if we're running an interpreter, as we'd be passing the | |
607 | * command line executable as an argument to the interpreter already. | |
608 | * Doing "execve("myShellScript","bogusName",arg1,arg2,...) | |
609 | * probably shouldn't ever let bogusName be seen by the shell | |
610 | * script. | |
611 | */ | |
612 | ||
613 | if (cfarg[0]) { | |
614 | error = copyArgument(cfarg,UIO_SYSSPACE,&nc,&cp,&cc); | |
615 | na++; | |
616 | if (error) goto bad; | |
617 | } | |
618 | ||
619 | char* originalExecutable = uap->fname; | |
620 | error = copyArgument(originalExecutable,UIO_USERSPACE,&nc,&cp,&cc); | |
621 | na++; | |
622 | /* remove argv[0] b/c we've already placed it at */ | |
623 | /* this point */ | |
624 | uap->argp++; | |
625 | if (error) goto bad; | |
626 | ||
627 | /* and continue with rest of the arguments. */ | |
628 | } else if (executingClassic) { | |
629 | error = copyArgument(classichandler,UIO_SYSSPACE,&nc,&cp,&cc); | |
630 | na++; | |
631 | if (error) goto bad; | |
632 | ||
633 | char* originalExecutable = uap->fname; | |
634 | error = copyArgument(originalExecutable,UIO_USERSPACE,&nc,&cp,&cc); | |
635 | if (error) goto bad; | |
636 | uap->argp++; | |
637 | na++; | |
638 | ||
639 | /* and rest of arguments continue as before. */ | |
640 | } else if (executingInterpreter) { | |
641 | char *actualExecutable = nd.ni_cnd.cn_nameptr; | |
642 | error = copyArgument(actualExecutable,UIO_SYSSPACE,&nc,&cp,&cc); | |
643 | na++; | |
644 | /* remove argv[0] b/c we just placed it in the arg list. */ | |
645 | uap->argp++; | |
646 | if (error) goto bad; | |
647 | /* Copy the argument in the interpreter first line if there | |
648 | * was one. | |
649 | */ | |
650 | if (cfarg[0]) { | |
651 | error = copyArgument(cfarg,UIO_SYSSPACE,&nc,&cp,&cc); | |
652 | na++; | |
653 | if (error) goto bad; | |
654 | } | |
655 | ||
656 | /* copy the name of the file being interpreted, gotten from | |
657 | * the structures passed in to execve. | |
658 | */ | |
659 | error = copyArgument(uap->fname,UIO_USERSPACE,&nc,&cp,&cc); | |
660 | na++; | |
661 | } | |
662 | /* Now, get rest of arguments */ | |
663 | while (uap->argp != NULL) { | |
664 | char* userArgument = (char*)fuword((caddr_t) uap->argp); | |
665 | uap->argp++; | |
666 | if (userArgument == NULL) { | |
667 | break; | |
668 | } else if ((int)userArgument == -1) { | |
669 | /* Um... why would it be -1? */ | |
670 | error = EFAULT; | |
671 | goto bad; | |
672 | } | |
673 | error = copyArgument(userArgument, UIO_USERSPACE,&nc,&cp,&cc); | |
674 | if (error) goto bad; | |
675 | na++; | |
676 | } | |
677 | /* Now, get the environment */ | |
678 | while (uap->envp != NULL) { | |
679 | char *userEnv = (char*) fuword((caddr_t) uap->envp); | |
680 | uap->envp++; | |
681 | if (userEnv == NULL) { | |
682 | break; | |
683 | } else if ((int)userEnv == -1) { | |
684 | error = EFAULT; | |
685 | goto bad; | |
686 | } | |
687 | error = copyArgument(userEnv,UIO_USERSPACE,&nc,&cp,&cc); | |
688 | if (error) goto bad; | |
689 | na++; | |
690 | ne++; | |
691 | } | |
692 | } | |
693 | ||
694 | /* make sure there are nulls are the end!! */ | |
695 | { | |
696 | int cnt = 3; | |
697 | char *mp = cp; | |
698 | ||
699 | while ( cnt-- ) | |
700 | *mp++ = '\0'; | |
701 | } | |
702 | ||
703 | /* and round up count of bytes written to next word. */ | |
704 | nc = (nc + NBPW-1) & ~(NBPW-1); | |
705 | ||
706 | if (vattr.va_fsid == classichandler_fsid && | |
707 | vattr.va_fileid == classichandler_fileid) { | |
708 | executingClassic = 1; | |
709 | } | |
710 | ||
711 | if (vfexec) { | |
712 | kern_return_t result; | |
713 | ||
714 | result = task_create_internal(task, FALSE, &new_task); | |
715 | if (result != KERN_SUCCESS) | |
716 | printf("execve: task_create failed. Code: 0x%x\n", result); | |
717 | p->task = new_task; | |
718 | set_bsdtask_info(new_task, p); | |
719 | if (p->p_nice != 0) | |
720 | resetpriority(p); | |
721 | task = new_task; | |
722 | map = get_task_map(new_task); | |
723 | result = thread_create(new_task, &thr_act); | |
724 | if (result != KERN_SUCCESS) | |
725 | printf("execve: thread_create failed. Code: 0x%x\n", result); | |
726 | uthread = get_bsdthread_info(thr_act); | |
727 | } else { | |
728 | map = VM_MAP_NULL; | |
729 | } | |
730 | ||
731 | /* | |
732 | * Load the Mach-O file. | |
733 | */ | |
734 | VOP_UNLOCK(vp, 0, p); /* XXX */ | |
735 | if(ws_cache_name) { | |
736 | tws_handle_startup_file(task, cred->cr_uid, | |
737 | ws_cache_name, vp, &clean_regions); | |
738 | } | |
739 | ||
740 | vm_get_shared_region(task, &initial_region); | |
741 | int parentIsClassic = (p->p_flag & P_CLASSIC); | |
742 | struct vnode *rootDir = p->p_fd->fd_rdir; | |
743 | ||
744 | if ((parentIsClassic && !executingClassic) || | |
745 | (!parentIsClassic && executingClassic)) { | |
746 | shared_region = lookup_default_shared_region( | |
747 | (int)rootDir, | |
748 | (executingClassic ? | |
749 | CPU_TYPE_POWERPC : | |
750 | machine_slot[cpu_number()].cpu_type)); | |
751 | if (shared_region == NULL) { | |
752 | shared_region_mapping_t old_region; | |
753 | shared_region_mapping_t new_region; | |
754 | vm_get_shared_region(current_task(), &old_region); | |
755 | /* grrrr... this sets current_task(), not task | |
756 | * -- they're different (usually) | |
757 | */ | |
758 | shared_file_boot_time_init( | |
759 | (int)rootDir, | |
760 | (executingClassic ? | |
761 | CPU_TYPE_POWERPC : | |
762 | machine_slot[cpu_number()].cpu_type)); | |
763 | if ( current_task() != task ) { | |
764 | vm_get_shared_region(current_task(),&new_region); | |
765 | vm_set_shared_region(task,new_region); | |
766 | vm_set_shared_region(current_task(),old_region); | |
767 | } | |
768 | } else { | |
769 | vm_set_shared_region(task, shared_region); | |
770 | } | |
771 | shared_region_mapping_dealloc(initial_region); | |
772 | } | |
773 | ||
774 | lret = load_machfile(vp, mach_header, arch_offset, | |
775 | arch_size, &load_result, thr_act, map, clean_regions); | |
776 | ||
777 | if (lret != LOAD_SUCCESS) { | |
778 | error = load_return_to_errno(lret); | |
779 | vrele(vp); | |
780 | vp = NULL; | |
781 | goto badtoolate; | |
782 | } | |
783 | ||
784 | /* load_machfile() maps the vnode */ | |
785 | ubc_map(vp); | |
786 | ||
787 | /* | |
788 | * deal with set[ug]id. | |
789 | */ | |
790 | p->p_flag &= ~P_SUGID; | |
791 | if (((origvattr.va_mode & VSUID) != 0 && | |
792 | p->p_ucred->cr_uid != origvattr.va_uid) | |
793 | || (origvattr.va_mode & VSGID) != 0 && | |
794 | p->p_ucred->cr_gid != origvattr.va_gid) { | |
795 | p->p_ucred = crcopy(cred); | |
796 | #if KTRACE | |
797 | /* | |
798 | * If process is being ktraced, turn off - unless | |
799 | * root set it. | |
800 | */ | |
801 | if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) { | |
802 | struct vnode *tvp = p->p_tracep; | |
803 | p->p_tracep = NULL; | |
804 | p->p_traceflag = 0; | |
805 | vrele(tvp); | |
806 | } | |
807 | #endif | |
808 | if (origvattr.va_mode & VSUID) | |
809 | p->p_ucred->cr_uid = origvattr.va_uid; | |
810 | if (origvattr.va_mode & VSGID) | |
811 | p->p_ucred->cr_gid = origvattr.va_gid; | |
812 | ||
813 | /* | |
814 | * Have mach reset the task port. We don't want | |
815 | * anyone who had the task port before a setuid | |
816 | * exec to be able to access/control the task | |
817 | * after. | |
818 | */ | |
819 | ipc_task_reset(task); | |
820 | ||
821 | set_security_token(p); | |
822 | p->p_flag |= P_SUGID; | |
823 | ||
824 | /* Radar 2261856; setuid security hole fix */ | |
825 | /* Patch from OpenBSD: A. Ramesh */ | |
826 | /* | |
827 | * XXX For setuid processes, attempt to ensure that | |
828 | * stdin, stdout, and stderr are already allocated. | |
829 | * We do not want userland to accidentally allocate | |
830 | * descriptors in this range which has implied meaning | |
831 | * to libc. | |
832 | */ | |
833 | for (i = 0; i < 3; i++) { | |
834 | extern struct fileops vnops; | |
835 | struct nameidata nd1; | |
836 | struct file *fp; | |
837 | int indx; | |
838 | ||
839 | if (p->p_fd->fd_ofiles[i] == NULL) { | |
840 | if ((error = falloc(p, &fp, &indx)) != 0) | |
841 | continue; | |
842 | NDINIT(&nd1, LOOKUP, FOLLOW, UIO_SYSSPACE, | |
843 | "/dev/null", p); | |
844 | if ((error = vn_open(&nd1, FREAD, 0)) != 0) { | |
845 | ffree(fp); | |
846 | p->p_fd->fd_ofiles[indx] = NULL; | |
847 | break; | |
848 | } | |
849 | fp->f_flag = FREAD; | |
850 | fp->f_type = DTYPE_VNODE; | |
851 | fp->f_ops = &vnops; | |
852 | fp->f_data = (caddr_t)nd1.ni_vp; | |
853 | VOP_UNLOCK(nd1.ni_vp, 0, p); | |
854 | } | |
855 | } | |
856 | } | |
857 | p->p_cred->p_svuid = p->p_ucred->cr_uid; | |
858 | p->p_cred->p_svgid = p->p_ucred->cr_gid; | |
859 | ||
860 | KNOTE(&p->p_klist, NOTE_EXEC); | |
861 | ||
862 | if (!vfexec && (p->p_flag & P_TRACED)) | |
863 | psignal(p, SIGTRAP); | |
864 | ||
865 | if (error) { | |
866 | vrele(vp); | |
867 | vp = NULL; | |
868 | goto badtoolate; | |
869 | } | |
870 | VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */ | |
871 | vput(vp); | |
872 | vp = NULL; | |
873 | ||
874 | if (load_result.unixproc && | |
875 | create_unix_stack(get_task_map(task), | |
876 | load_result.user_stack, load_result.customstack, p)) { | |
877 | error = load_return_to_errno(LOAD_NOSPACE); | |
878 | goto badtoolate; | |
879 | } | |
880 | ||
881 | if (vfexec) { | |
882 | uthread->uu_ar0 = (void *)get_user_regs(thr_act); | |
883 | } | |
884 | ||
885 | /* | |
886 | * Copy back arglist if necessary. | |
887 | */ | |
888 | ||
889 | ||
890 | ucp = (int)p->user_stack; | |
891 | if (vfexec) { | |
892 | old_map = vm_map_switch(get_task_map(task)); | |
893 | } | |
894 | if (load_result.unixproc) { | |
895 | int pathptr; | |
896 | ||
897 | ucp = ucp - nc - NBPW; /* begining of the STRING AREA */ | |
898 | ||
899 | /* | |
900 | * Support for new app package launching for Mac OS X allocates | |
901 | * the "path" at the begining of the execargs buffer. | |
902 | * copy it just before the string area. | |
903 | */ | |
904 | len = 0; | |
905 | pathptr = ucp - ((savedpathlen + NBPW-1) & ~(NBPW-1)); | |
906 | error = copyoutstr(savedpath, (caddr_t)pathptr, | |
907 | (unsigned)savedpathlen, (size_t *)&len); | |
908 | savedpathlen = (savedpathlen + NBPW-1) & ~(NBPW-1); | |
909 | ||
910 | if (error) { | |
911 | if (vfexec) | |
912 | vm_map_switch(old_map); | |
913 | goto badtoolate; | |
914 | } | |
915 | ||
916 | /* | |
917 | * Record the size of the arguments area so that | |
918 | * sysctl_procargs() can return the argument area without having | |
919 | * to parse the arguments. | |
920 | */ | |
921 | p->p_argslen = (int)p->user_stack - pathptr; | |
922 | p->p_argc = na - ne; /* save argc for sysctl_procargs() */ | |
923 | ||
924 | /* Save a NULL pointer below it */ | |
925 | (void) suword((caddr_t)(pathptr - NBPW), 0); | |
926 | ||
927 | /* Save the pointer to "path" just below it */ | |
928 | (void) suword((caddr_t)(pathptr - 2*NBPW), pathptr); | |
929 | ||
930 | /* | |
931 | * na includes arg[] and env[]. | |
932 | * NBPW for 2 NULL one each ofter arg[argc -1] and env[n] | |
933 | * NBPW for argc | |
934 | * skip over saved path, NBPW for pointer to path, | |
935 | * and NBPW for the NULL after pointer to path. | |
936 | */ | |
937 | ap = ucp - na*NBPW - 3*NBPW - savedpathlen - 2*NBPW; | |
938 | #if defined(ppc) | |
939 | thread_setuserstack(thr_act, ap); /* Set the stack */ | |
940 | #else | |
941 | uthread->uu_ar0[SP] = ap; | |
942 | #endif | |
943 | (void) suword((caddr_t)ap, na-ne); /* argc */ | |
944 | nc = 0; | |
945 | cc = 0; | |
946 | ||
947 | cp = (char *) execargsp; | |
948 | cc = NCARGS - savedpathlen - 2*NBPW; | |
949 | ps.ps_argvstr = (char *)ucp; /* first argv string */ | |
950 | ps.ps_nargvstr = na - ne; /* argc */ | |
951 | for (;;) { | |
952 | ap += NBPW; | |
953 | if (na == ne) { | |
954 | (void) suword((caddr_t)ap, 0); | |
955 | ap += NBPW; | |
956 | ps.ps_envstr = (char *)ucp; | |
957 | ps.ps_nenvstr = ne; | |
958 | } | |
959 | if (--na < 0) | |
960 | break; | |
961 | (void) suword((caddr_t)ap, ucp); | |
962 | do { | |
963 | error = copyoutstr(cp, (caddr_t)ucp, | |
964 | (unsigned)cc, (size_t *)&len); | |
965 | ucp += len; | |
966 | cp += len; | |
967 | nc += len; | |
968 | cc -= len; | |
969 | } while (error == ENAMETOOLONG); | |
970 | if (error == EFAULT) | |
971 | break; /* bad stack - user's problem */ | |
972 | } | |
973 | (void) suword((caddr_t)ap, 0); | |
974 | } | |
975 | ||
976 | if (load_result.dynlinker) { | |
977 | #if defined(ppc) | |
978 | ap = thread_adjuserstack(thr_act, -4); /* Adjust the stack */ | |
979 | #else | |
980 | ap = uthread->uu_ar0[SP] -= 4; | |
981 | #endif | |
982 | (void) suword((caddr_t)ap, load_result.mach_header); | |
983 | } | |
984 | ||
985 | if (vfexec) { | |
986 | vm_map_switch(old_map); | |
987 | } | |
988 | #if defined(ppc) | |
989 | thread_setentrypoint(thr_act, load_result.entry_point); /* Set the entry point */ | |
990 | #elif defined(i386) | |
991 | uthread->uu_ar0[PC] = load_result.entry_point; | |
992 | #else | |
993 | #error architecture not implemented! | |
994 | #endif | |
995 | ||
996 | /* Stop profiling */ | |
997 | stopprofclock(p); | |
998 | ||
999 | /* | |
1000 | * Reset signal state. | |
1001 | */ | |
1002 | execsigs(p, thr_act); | |
1003 | ||
1004 | /* | |
1005 | * Close file descriptors | |
1006 | * which specify close-on-exec. | |
1007 | */ | |
1008 | fdexec(p); | |
1009 | ||
1010 | /* | |
1011 | * need to cancel async IO requests that can be cancelled and wait for those | |
1012 | * already active. MAY BLOCK! | |
1013 | */ | |
1014 | _aio_exec( p ); | |
1015 | ||
1016 | /* FIXME: Till vmspace inherit is fixed: */ | |
1017 | if (!vfexec && p->vm_shm) | |
1018 | shmexec(p); | |
1019 | /* Clean up the semaphores */ | |
1020 | semexit(p); | |
1021 | ||
1022 | /* | |
1023 | * Remember file name for accounting. | |
1024 | */ | |
1025 | p->p_acflag &= ~AFORK; | |
1026 | /* If the translated name isn't NULL, then we want to use | |
1027 | * that translated name as the name we show as the "real" name. | |
1028 | * Otherwise, use the name passed into exec. | |
1029 | */ | |
1030 | if (0 != binaryWithClassicName[0]) { | |
1031 | bcopy((caddr_t)binaryWithClassicName, (caddr_t)p->p_comm, | |
1032 | sizeof(binaryWithClassicName)); | |
1033 | } else { | |
1034 | if (nd.ni_cnd.cn_namelen > MAXCOMLEN) | |
1035 | nd.ni_cnd.cn_namelen = MAXCOMLEN; | |
1036 | bcopy((caddr_t)nd.ni_cnd.cn_nameptr, (caddr_t)p->p_comm, | |
1037 | (unsigned)nd.ni_cnd.cn_namelen); | |
1038 | p->p_comm[nd.ni_cnd.cn_namelen] = '\0'; | |
1039 | } | |
1040 | ||
1041 | { | |
1042 | /* This is for kdebug */ | |
1043 | long dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4; | |
1044 | ||
1045 | /* Collect the pathname for tracing */ | |
1046 | kdbg_trace_string(p, &dbg_arg1, &dbg_arg2, &dbg_arg3, &dbg_arg4); | |
1047 | ||
1048 | ||
1049 | ||
1050 | if (vfexec) | |
1051 | { | |
1052 | KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_DATA, 2)) | DBG_FUNC_NONE, | |
1053 | p->p_pid ,0,0,0, (unsigned int)thr_act); | |
1054 | KERNEL_DEBUG_CONSTANT1((TRACEDBG_CODE(DBG_TRACE_STRING, 2)) | DBG_FUNC_NONE, | |
1055 | dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, (unsigned int)thr_act); | |
1056 | } | |
1057 | else | |
1058 | { | |
1059 | KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_DATA, 2)) | DBG_FUNC_NONE, | |
1060 | p->p_pid ,0,0,0,0); | |
1061 | KERNEL_DEBUG_CONSTANT((TRACEDBG_CODE(DBG_TRACE_STRING, 2)) | DBG_FUNC_NONE, | |
1062 | dbg_arg1, dbg_arg2, dbg_arg3, dbg_arg4, 0); | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | if (executingClassic) | |
1067 | p->p_flag |= P_CLASSIC | P_AFFINITY; | |
1068 | else | |
1069 | p->p_flag &= ~P_CLASSIC; | |
1070 | ||
1071 | /* | |
1072 | * mark as execed, wakeup the process that vforked (if any) and tell | |
1073 | * it that it now has it's own resources back | |
1074 | */ | |
1075 | p->p_flag |= P_EXEC; | |
1076 | if (p->p_pptr && (p->p_flag & P_PPWAIT)) { | |
1077 | p->p_flag &= ~P_PPWAIT; | |
1078 | wakeup((caddr_t)p->p_pptr); | |
1079 | } | |
1080 | ||
1081 | if (vfexec && (p->p_flag & P_TRACED)) { | |
1082 | psignal_vfork(p, new_task, thr_act, SIGTRAP); | |
1083 | } | |
1084 | ||
1085 | badtoolate: | |
1086 | if (vfexec) { | |
1087 | task_deallocate(new_task); | |
1088 | act_deallocate(thr_act); | |
1089 | if (error) | |
1090 | error = 0; | |
1091 | } | |
1092 | bad: | |
1093 | FREE_ZONE(nd.ni_cnd.cn_pnbuf, nd.ni_cnd.cn_pnlen, M_NAMEI); | |
1094 | if (vp) | |
1095 | vput(vp); | |
1096 | bad1: | |
1097 | if (execargs) | |
1098 | execargs_free(execargs); | |
1099 | if (!error && vfexec) { | |
1100 | vfork_return(current_act(), p->p_pptr, p, retval); | |
1101 | (void) thread_resume(thr_act); | |
1102 | return(0); | |
1103 | } | |
1104 | return(error); | |
1105 | } | |
1106 | ||
1107 | ||
1108 | #define unix_stack_size(p) (p->p_rlimit[RLIMIT_STACK].rlim_cur) | |
1109 | ||
1110 | kern_return_t | |
1111 | create_unix_stack(map, user_stack, customstack, p) | |
1112 | vm_map_t map; | |
1113 | vm_offset_t user_stack; | |
1114 | int customstack; | |
1115 | struct proc *p; | |
1116 | { | |
1117 | vm_size_t size; | |
1118 | vm_offset_t addr; | |
1119 | ||
1120 | p->user_stack = (caddr_t)user_stack; | |
1121 | if (!customstack) { | |
1122 | size = round_page_64(unix_stack_size(p)); | |
1123 | addr = trunc_page_32(user_stack - size); | |
1124 | return (vm_allocate(map, &addr, size, | |
1125 | VM_MAKE_TAG(VM_MEMORY_STACK) | FALSE)); | |
1126 | } else | |
1127 | return(KERN_SUCCESS); | |
1128 | } | |
1129 | ||
1130 | #include <sys/reboot.h> | |
1131 | ||
1132 | char init_program_name[128] = "/sbin/mach_init\0"; | |
1133 | ||
1134 | char init_args[128] = ""; | |
1135 | ||
1136 | struct execve_args init_exec_args; | |
1137 | int init_attempts = 0; | |
1138 | ||
1139 | ||
1140 | void | |
1141 | load_init_program(p) | |
1142 | struct proc *p; | |
1143 | { | |
1144 | vm_offset_t init_addr; | |
1145 | int *old_ap; | |
1146 | char *argv[3]; | |
1147 | int error; | |
1148 | register_t retval[2]; | |
1149 | struct uthread * ut; | |
1150 | ||
1151 | error = 0; | |
1152 | ||
1153 | /* init_args are copied in string form directly from bootstrap */ | |
1154 | ||
1155 | do { | |
1156 | if (boothowto & RB_INITNAME) { | |
1157 | printf("init program? "); | |
1158 | #if FIXME /* [ */ | |
1159 | gets(init_program_name, init_program_name); | |
1160 | #endif /* FIXME ] */ | |
1161 | } | |
1162 | ||
1163 | if (error && ((boothowto & RB_INITNAME) == 0) && | |
1164 | (init_attempts == 1)) { | |
1165 | static char other_init[] = "/etc/mach_init"; | |
1166 | printf("Load of %s, errno %d, trying %s\n", | |
1167 | init_program_name, error, other_init); | |
1168 | error = 0; | |
1169 | bcopy(other_init, init_program_name, | |
1170 | sizeof(other_init)); | |
1171 | } | |
1172 | ||
1173 | init_attempts++; | |
1174 | ||
1175 | if (error) { | |
1176 | printf("Load of %s failed, errno %d\n", | |
1177 | init_program_name, error); | |
1178 | error = 0; | |
1179 | boothowto |= RB_INITNAME; | |
1180 | continue; | |
1181 | } | |
1182 | ||
1183 | /* | |
1184 | * Copy out program name. | |
1185 | */ | |
1186 | ||
1187 | init_addr = VM_MIN_ADDRESS; | |
1188 | (void) vm_allocate(current_map(), &init_addr, | |
1189 | PAGE_SIZE, TRUE); | |
1190 | if (init_addr == 0) | |
1191 | init_addr++; | |
1192 | (void) copyout((caddr_t) init_program_name, | |
1193 | (caddr_t) (init_addr), | |
1194 | (unsigned) sizeof(init_program_name)+1); | |
1195 | ||
1196 | argv[0] = (char *) init_addr; | |
1197 | init_addr += sizeof(init_program_name); | |
1198 | init_addr = (vm_offset_t)ROUND_PTR(char, init_addr); | |
1199 | ||
1200 | /* | |
1201 | * Put out first (and only) argument, similarly. | |
1202 | * Assumes everything fits in a page as allocated | |
1203 | * above. | |
1204 | */ | |
1205 | ||
1206 | (void) copyout((caddr_t) init_args, | |
1207 | (caddr_t) (init_addr), | |
1208 | (unsigned) sizeof(init_args)); | |
1209 | ||
1210 | argv[1] = (char *) init_addr; | |
1211 | init_addr += sizeof(init_args); | |
1212 | init_addr = (vm_offset_t)ROUND_PTR(char, init_addr); | |
1213 | ||
1214 | /* | |
1215 | * Null-end the argument list | |
1216 | */ | |
1217 | ||
1218 | argv[2] = (char *) 0; | |
1219 | ||
1220 | /* | |
1221 | * Copy out the argument list. | |
1222 | */ | |
1223 | ||
1224 | (void) copyout((caddr_t) argv, | |
1225 | (caddr_t) (init_addr), | |
1226 | (unsigned) sizeof(argv)); | |
1227 | ||
1228 | /* | |
1229 | * Set up argument block for fake call to execve. | |
1230 | */ | |
1231 | ||
1232 | init_exec_args.fname = argv[0]; | |
1233 | init_exec_args.argp = (char **) init_addr; | |
1234 | init_exec_args.envp = 0; | |
1235 | ||
1236 | /* So that mach_init task | |
1237 | * is set with uid,gid 0 token | |
1238 | */ | |
1239 | set_security_token(p); | |
1240 | ||
1241 | error = execve(p,&init_exec_args,retval); | |
1242 | } while (error); | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * Convert a load_return_t to an errno. | |
1247 | */ | |
1248 | static int | |
1249 | load_return_to_errno(load_return_t lrtn) | |
1250 | { | |
1251 | switch (lrtn) { | |
1252 | case LOAD_SUCCESS: | |
1253 | return 0; | |
1254 | case LOAD_BADARCH: | |
1255 | return EBADARCH; | |
1256 | case LOAD_BADMACHO: | |
1257 | return EBADMACHO; | |
1258 | case LOAD_SHLIB: | |
1259 | return ESHLIBVERS; | |
1260 | case LOAD_NOSPACE: | |
1261 | case LOAD_RESOURCE: | |
1262 | return ENOMEM; | |
1263 | case LOAD_PROTECT: | |
1264 | return EACCES; | |
1265 | case LOAD_ENOENT: | |
1266 | return ENOENT; | |
1267 | case LOAD_IOERROR: | |
1268 | return EIO; | |
1269 | case LOAD_FAILURE: | |
1270 | default: | |
1271 | return EBADEXEC; | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | /* | |
1276 | * exec_check_access() | |
1277 | */ | |
1278 | int | |
1279 | check_exec_access(p, vp, vap) | |
1280 | struct proc *p; | |
1281 | struct vnode *vp; | |
1282 | struct vattr *vap; | |
1283 | { | |
1284 | int flag; | |
1285 | int error; | |
1286 | ||
1287 | if (error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) | |
1288 | return (error); | |
1289 | flag = p->p_flag; | |
1290 | if (flag & P_TRACED) { | |
1291 | if (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) | |
1292 | return (error); | |
1293 | } | |
1294 | if (vp->v_type != VREG || | |
1295 | (vap->va_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) | |
1296 | return (EACCES); | |
1297 | return (0); | |
1298 | } | |
1299 | ||
1300 | #include <mach/mach_types.h> | |
1301 | #include <mach/vm_prot.h> | |
1302 | #include <mach/semaphore.h> | |
1303 | #include <mach/sync_policy.h> | |
1304 | #include <kern/clock.h> | |
1305 | #include <mach/kern_return.h> | |
1306 | ||
1307 | extern semaphore_t execve_semaphore; | |
1308 | ||
1309 | static int | |
1310 | execargs_alloc(addrp) | |
1311 | vm_offset_t *addrp; | |
1312 | { | |
1313 | kern_return_t kret; | |
1314 | ||
1315 | kret = semaphore_wait(execve_semaphore); | |
1316 | if (kret != KERN_SUCCESS) | |
1317 | switch (kret) { | |
1318 | default: | |
1319 | return (EINVAL); | |
1320 | case KERN_INVALID_ADDRESS: | |
1321 | case KERN_PROTECTION_FAILURE: | |
1322 | return (EACCES); | |
1323 | case KERN_ABORTED: | |
1324 | case KERN_OPERATION_TIMED_OUT: | |
1325 | return (EINTR); | |
1326 | } | |
1327 | ||
1328 | kret = kmem_alloc_pageable(bsd_pageable_map, addrp, NCARGS); | |
1329 | if (kret != KERN_SUCCESS) { | |
1330 | semaphore_signal(execve_semaphore); | |
1331 | return (ENOMEM); | |
1332 | } | |
1333 | return (0); | |
1334 | } | |
1335 | ||
1336 | static int | |
1337 | execargs_free(addr) | |
1338 | vm_offset_t addr; | |
1339 | { | |
1340 | kern_return_t kret; | |
1341 | ||
1342 | kmem_free(bsd_pageable_map, addr, NCARGS); | |
1343 | ||
1344 | kret = semaphore_signal(execve_semaphore); | |
1345 | switch (kret) { | |
1346 | case KERN_INVALID_ADDRESS: | |
1347 | case KERN_PROTECTION_FAILURE: | |
1348 | return (EINVAL); | |
1349 | case KERN_ABORTED: | |
1350 | case KERN_OPERATION_TIMED_OUT: | |
1351 | return (EINTR); | |
1352 | case KERN_SUCCESS: | |
1353 | return(0); | |
1354 | default: | |
1355 | return (EINVAL); | |
1356 | } | |
1357 | } |