xref: /minix/minix/servers/vfs/exec.c (revision ebfedea0)
1 /* This file handles the EXEC system call.  It performs the work as follows:
2  *    - see if the permissions allow the file to be executed
3  *    - read the header and extract the sizes
4  *    - fetch the initial args and environment from the user space
5  *    - allocate the memory for the new process
6  *    - copy the initial stack from PM to the process
7  *    - read in the text and data segments and copy to the process
8  *    - take care of setuid and setgid bits
9  *    - fix up 'mproc' table
10  *    - tell kernel about EXEC
11  *    - save offset to initial argc (for ps)
12  *
13  * The entry points into this file are:
14  *   pm_exec:	 perform the EXEC system call
15  */
16 
17 #include "fs.h"
18 #include <sys/stat.h>
19 #include <sys/mman.h>
20 #include <minix/callnr.h>
21 #include <minix/endpoint.h>
22 #include <minix/com.h>
23 #include <minix/u64.h>
24 #include <lib.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/dirent.h>
29 #include <sys/exec.h>
30 #include <sys/param.h>
31 #include "path.h"
32 #include "vnode.h"
33 #include "file.h"
34 #include <minix/vfsif.h>
35 #include <machine/vmparam.h>
36 #include <assert.h>
37 #include <fcntl.h>
38 
39 #define _KERNEL	/* for ELF_AUX_ENTRIES */
40 #include <libexec.h>
41 
42 /* fields only used by elf and in VFS */
43 struct vfs_exec_info {
44     struct exec_info args;		/* libexec exec args */
45     struct vnode *vp;			/* Exec file's vnode */
46     struct vmnt *vmp;			/* Exec file's vmnt */
47     struct stat sb;			/* Exec file's stat structure */
48     int userflags;			/* exec() flags from userland */
49     int is_dyn;				/* Dynamically linked executable */
50     int elf_main_fd;			/* Dyn: FD of main program execuatble */
51     char execname[PATH_MAX];		/* Full executable invocation */
52     int vmfd;
53     int vmfd_used;
54 };
55 
56 static int patch_stack(struct vnode *vp, char stack[ARG_MAX],
57 	size_t *stk_bytes, char path[PATH_MAX], vir_bytes *vsp);
58 static int is_script(struct vfs_exec_info *execi);
59 static int insert_arg(char stack[ARG_MAX], size_t *stk_bytes, char *arg,
60 	vir_bytes *vsp, char replace);
61 static void clo_exec(struct fproc *rfp);
62 static int stack_prepare_elf(struct vfs_exec_info *execi,
63 	char *curstack, size_t *frame_len, vir_bytes *vsp);
64 static int map_header(struct vfs_exec_info *execi);
65 static int read_seg(struct exec_info *execi, off_t off, vir_bytes seg_addr, size_t seg_bytes);
66 
67 #define PTRSIZE	sizeof(char *) /* Size of pointers in argv[] and envp[]. */
68 
69 /* Array of loaders for different object file formats */
70 typedef int (*exechook_t)(struct vfs_exec_info *execpackage);
71 typedef int (*stackhook_t)(struct vfs_exec_info *execi, char *curstack,
72 	size_t *frame_len, vir_bytes *vsp);
73 struct exec_loaders {
74 	libexec_exec_loadfunc_t load_object;	 /* load executable into memory */
75 	stackhook_t setup_stack; /* prepare stack before argc and argv push */
76 };
77 
78 static const struct exec_loaders exec_loaders[] = {
79 	{ libexec_load_elf,  stack_prepare_elf },
80 	{ NULL, NULL }
81 };
82 
83 #define lock_exec() lock_proc(fproc_addr(VM_PROC_NR))
84 #define unlock_exec() unlock_proc(fproc_addr(VM_PROC_NR))
85 
86 /*===========================================================================*
87  *				get_read_vp				     *
88  *===========================================================================*/
89 static int get_read_vp(struct vfs_exec_info *execi,
90   char *fullpath, int copyprogname, int sugid, struct lookup *resolve, struct fproc *fp)
91 {
92 /* Make the executable that we want to exec() into the binary pointed
93  * to by 'fullpath.' This function fills in necessary details in the execi
94  * structure, such as opened vnode. It unlocks and releases the vnode if
95  * it was already there. This makes it easy to change the executable
96  * during the exec(), which is often necessary, by calling this function
97  * more than once. This is specifically necessary when we discover the
98  * executable is actually a script or a dynamically linked executable.
99  */
100 	int r;
101 
102 	/* Caller wants to switch vp to the file in 'fullpath.'
103 	 * unlock and put it first if there is any there.
104 	 */
105 	if(execi->vp) {
106 		unlock_vnode(execi->vp);
107 		put_vnode(execi->vp);
108 		execi->vp = NULL;
109 	}
110 
111 	/* Remember/overwrite the executable name if requested. */
112 	if(copyprogname) {
113 		char *cp = strrchr(fullpath, '/');
114 		if(cp) cp++;
115 		else cp = fullpath;
116 		strlcpy(execi->args.progname, cp, sizeof(execi->args.progname));
117 		execi->args.progname[sizeof(execi->args.progname)-1] = '\0';
118 	}
119 
120 	/* Open executable */
121 	if ((execi->vp = eat_path(resolve, fp)) == NULL)
122 		return err_code;
123 
124 	unlock_vmnt(execi->vmp);
125 
126 	if (!S_ISREG(execi->vp->v_mode))
127 		return ENOEXEC;
128 	else if ((r = forbidden(fp, execi->vp, X_BIT)) != OK)
129 		return r;
130 	else
131 		r = req_stat(execi->vp->v_fs_e, execi->vp->v_inode_nr,
132 			VFS_PROC_NR, (vir_bytes) &(execi->sb));
133 
134 	if (r != OK) return r;
135 
136 	/* If caller wants us to, honour suid/guid mode bits. */
137         if (sugid) {
138 		/* Deal with setuid/setgid executables */
139 		if (execi->vp->v_mode & I_SET_UID_BIT) {
140 			execi->args.new_uid = execi->vp->v_uid;
141 			execi->args.allow_setuid = 1;
142 		}
143 		if (execi->vp->v_mode & I_SET_GID_BIT) {
144 			execi->args.new_gid = execi->vp->v_gid;
145 			execi->args.allow_setuid = 1;
146 		}
147         }
148 
149 	/* Read in first chunk of file. */
150 	if((r=map_header(execi)) != OK)
151 		return r;
152 
153 	return OK;
154 }
155 
156 #define FAILCHECK(expr) if((r=(expr)) != OK) { goto pm_execfinal; } while(0)
157 #define Get_read_vp(e,f,p,s,rs,fp) do { \
158 	r=get_read_vp(&e,f,p,s,rs,fp); if(r != OK) { FAILCHECK(r); }	\
159 	} while(0)
160 
161 static int vfs_memmap(struct exec_info *execi,
162         vir_bytes vaddr, vir_bytes len, vir_bytes foffset, u16_t clearend,
163 	int protflags)
164 {
165 	struct vfs_exec_info *vi = (struct vfs_exec_info *) execi->opaque;
166 	struct vnode *vp = ((struct vfs_exec_info *) execi->opaque)->vp;
167 	int r;
168 	u16_t flags = 0;
169 
170 	if(protflags & PROT_WRITE)
171 		flags |= MVM_WRITABLE;
172 
173 	r = minix_vfs_mmap(execi->proc_e, foffset, len,
174 	        vp->v_dev, vp->v_inode_nr, vi->vmfd, vaddr, clearend, flags);
175 	if(r == OK) {
176 		vi->vmfd_used = 1;
177 	}
178 
179 	return r;
180 }
181 
182 /*===========================================================================*
183  *				pm_exec					     *
184  *===========================================================================*/
185 int pm_exec(vir_bytes path, size_t path_len, vir_bytes frame, size_t frame_len,
186 	vir_bytes *pc, vir_bytes *newsp, vir_bytes *UNUSED(ps_str))
187 {
188 /* Perform the execve(name, argv, envp) call.  The user library builds a
189  * complete stack image, including pointers, args, environ, etc.  The stack
190  * is copied to a buffer inside VFS, and then to the new core image.
191  *
192  * ps_str is not currently used, but may be if the ps_strings structure has to
193  * be moved to another location.
194  */
195   int r;
196   vir_bytes vsp;
197   static char mbuf[ARG_MAX];	/* buffer for stack and zeroes */
198   struct vfs_exec_info execi;
199   int i;
200   static char fullpath[PATH_MAX],
201   	elf_interpreter[PATH_MAX],
202 	firstexec[PATH_MAX],
203 	finalexec[PATH_MAX];
204   struct lookup resolve;
205   struct fproc *vmfp = fproc_addr(VM_PROC_NR);
206   stackhook_t makestack = NULL;
207   struct filp *newfilp = NULL;
208 
209   lock_exec();
210 
211   /* unset execi values are 0. */
212   memset(&execi, 0, sizeof(execi));
213   execi.vmfd = -1;
214 
215   /* passed from exec() libc code */
216   execi.userflags = 0;
217   execi.args.stack_high = minix_get_user_sp();
218   execi.args.stack_size = DEFAULT_STACK_LIMIT;
219 
220   lookup_init(&resolve, fullpath, PATH_NOFLAGS, &execi.vmp, &execi.vp);
221 
222   resolve.l_vmnt_lock = VMNT_READ;
223   resolve.l_vnode_lock = VNODE_READ;
224 
225   /* Fetch the stack from the user before destroying the old core image. */
226   if (frame_len > ARG_MAX)
227 	FAILCHECK(ENOMEM); /* stack too big */
228 
229   r = sys_datacopy_wrapper(fp->fp_endpoint, (vir_bytes) frame, SELF, (vir_bytes) mbuf,
230 		   (size_t) frame_len);
231   if (r != OK) { /* can't fetch stack (e.g. bad virtual addr) */
232         printf("VFS: pm_exec: sys_datacopy failed\n");
233 	FAILCHECK(r);
234   }
235 
236   /* Compute the current virtual stack pointer, has to be done before calling
237    * patch_stack, which needs it, and will adapt as required. */
238   vsp = execi.args.stack_high - frame_len;
239 
240   /* The default is to keep the original user and group IDs */
241   execi.args.new_uid = fp->fp_effuid;
242   execi.args.new_gid = fp->fp_effgid;
243 
244   /* Get the exec file name. */
245   FAILCHECK(fetch_name(path, path_len, fullpath));
246   strlcpy(finalexec, fullpath, PATH_MAX);
247   strlcpy(firstexec, fullpath, PATH_MAX);
248 
249   /* Get_read_vp will return an opened vn in execi.
250    * if necessary it releases the existing vp so we can
251    * switch after we find out what's inside the file.
252    * It reads the start of the file.
253    */
254   Get_read_vp(execi, fullpath, 1, 1, &resolve, fp);
255 
256   /* If this is a script (i.e. has a #!/interpreter line),
257    * retrieve the name of the interpreter and open that
258    * executable instead.
259    */
260   if(is_script(&execi)) {
261   	/* patch_stack will add interpreter name and
262 	 * args to stack and retrieve the new binary
263 	 * name into fullpath.
264 	 */
265 	FAILCHECK(fetch_name(path, path_len, fullpath));
266 	FAILCHECK(patch_stack(execi.vp, mbuf, &frame_len, fullpath, &vsp));
267 
268 	strlcpy(finalexec, fullpath, PATH_MAX);
269   	strlcpy(firstexec, fullpath, PATH_MAX);
270 	Get_read_vp(execi, fullpath, 1, 0, &resolve, fp);
271   }
272 
273   /* If this is a dynamically linked executable, retrieve
274    * the name of that interpreter in elf_interpreter and open that
275    * executable instead. But open the current executable in an
276    * fd for the current process.
277    */
278   if(elf_has_interpreter(execi.args.hdr, execi.args.hdr_len,
279 	elf_interpreter, sizeof(elf_interpreter))) {
280 	/* Switch the executable vnode to the interpreter */
281 	execi.is_dyn = 1;
282 
283 	/* The interpreter (loader) needs an fd to the main program,
284 	 * which is currently in finalexec
285 	 */
286 	if ((r = execi.elf_main_fd =
287 	    common_open(finalexec, O_RDONLY, 0, TRUE /*for_exec*/)) < 0) {
288 		printf("VFS: exec: dynamic: open main exec failed %s (%d)\n",
289 			fullpath, r);
290 		FAILCHECK(r);
291 	}
292 
293 	/* ld.so is linked at 0, but it can relocate itself; we
294 	 * want it higher to trap NULL pointer dereferences.
295 	 * Let's put it below the stack, and reserve 10MB for ld.so.
296 	 */
297 	execi.args.load_offset =
298 		 execi.args.stack_high - execi.args.stack_size - 0xa00000;
299 
300 	/* Remember it */
301 	strlcpy(execi.execname, finalexec, PATH_MAX);
302 
303 	/* The executable we need to execute first (loader)
304 	 * is in elf_interpreter, and has to be in fullpath to
305 	 * be looked up
306 	 */
307 	strlcpy(fullpath, elf_interpreter, PATH_MAX);
308 	strlcpy(firstexec, elf_interpreter, PATH_MAX);
309 	Get_read_vp(execi, fullpath, 0, 0, &resolve, fp);
310   }
311 
312   /* We also want an FD for VM to mmap() the process in if possible. */
313   {
314 	struct vnode *vp = execi.vp;
315 	assert(vp);
316 	if ((vp->v_vmnt->m_fs_flags & RES_HASPEEK) &&
317 			major(vp->v_dev) != MEMORY_MAJOR) {
318 		int newfd = -1;
319 		if(get_fd(vmfp, 0, R_BIT, &newfd, &newfilp) == OK) {
320 			assert(newfd >= 0 && newfd < OPEN_MAX);
321 			assert(!vmfp->fp_filp[newfd]);
322 			newfilp->filp_count = 1;
323 			newfilp->filp_vno = vp;
324 			newfilp->filp_flags = O_RDONLY;
325 			vmfp->fp_filp[newfd] = newfilp;
326 			/* dup_vnode(vp); */
327 			execi.vmfd = newfd;
328 			execi.args.memmap = vfs_memmap;
329 		}
330 	}
331   }
332 
333   /* callback functions and data */
334   execi.args.copymem = read_seg;
335   execi.args.clearproc = libexec_clearproc_vm_procctl;
336   execi.args.clearmem = libexec_clear_sys_memset;
337   execi.args.allocmem_prealloc_cleared = libexec_alloc_mmap_prealloc_cleared;
338   execi.args.allocmem_prealloc_junk = libexec_alloc_mmap_prealloc_junk;
339   execi.args.allocmem_ondemand = libexec_alloc_mmap_ondemand;
340   execi.args.opaque = &execi;
341 
342   execi.args.proc_e = fp->fp_endpoint;
343   execi.args.frame_len = frame_len;
344   execi.args.filesize = execi.vp->v_size;
345 
346   for (i = 0; exec_loaders[i].load_object != NULL; i++) {
347       r = (*exec_loaders[i].load_object)(&execi.args);
348       /* Loaded successfully, so no need to try other loaders */
349       if (r == OK) { makestack = exec_loaders[i].setup_stack; break; }
350   }
351 
352   FAILCHECK(r);
353 
354   /* Inform PM */
355   FAILCHECK(libexec_pm_newexec(fp->fp_endpoint, &execi.args));
356 
357   /* Save off PC */
358   *pc = execi.args.pc;
359 
360   /* call a stack-setup function if this executable type wants it */
361   if(makestack) FAILCHECK(makestack(&execi, mbuf, &frame_len, &vsp));
362 
363   /* Copy the stack from VFS to new core image. */
364   FAILCHECK(sys_datacopy_wrapper(SELF, (vir_bytes) mbuf, fp->fp_endpoint,
365 	(vir_bytes) vsp, (phys_bytes)frame_len));
366 
367   /* Return new stack pointer to caller */
368   *newsp = vsp;
369 
370   clo_exec(fp);
371 
372   if (execi.args.allow_setuid) {
373 	/* If after loading the image we're still allowed to run with
374 	 * setuid or setgid, change credentials now */
375 	fp->fp_effuid = execi.args.new_uid;
376 	fp->fp_effgid = execi.args.new_gid;
377   }
378 
379   /* Remember the new name of the process */
380   strlcpy(fp->fp_name, execi.args.progname, PROC_NAME_LEN);
381 
382 pm_execfinal:
383   if(newfilp) unlock_filp(newfilp);
384   else if (execi.vp != NULL) {
385 	unlock_vnode(execi.vp);
386 	put_vnode(execi.vp);
387   }
388 
389   if(execi.vmfd >= 0 && !execi.vmfd_used) {
390   	if(OK != close_fd(vmfp, execi.vmfd)) {
391 		printf("VFS: unexpected close fail of vm fd\n");
392 	}
393   }
394 
395   unlock_exec();
396 
397   return(r);
398 }
399 
400 /* This is a copy-paste of the same macro in minix/lib/libc/sys/stack_utils.c.
401  * Keep it synchronized. */
402 #define STACK_MIN_SZ \
403 ( \
404        sizeof(int) + sizeof(void *) * 2 + \
405        sizeof(AuxInfo) * PMEF_AUXVECTORS + PMEF_EXECNAMELEN1 + \
406        sizeof(struct ps_strings) \
407 )
408 
409 static int stack_prepare_elf(struct vfs_exec_info *execi, char *frame, size_t *frame_size,
410 	vir_bytes *vsp)
411 {
412 	AuxInfo *aux_vec, *aux_vec_end;
413 	vir_bytes vap; /* Address in proc space of the first AuxVec. */
414 	Elf_Ehdr const * const elf_header = (Elf_Ehdr *) execi->args.hdr;
415 	struct ps_strings const * const psp = (struct ps_strings *)
416 		(frame + (*frame_size - sizeof(struct ps_strings)));
417 
418 	size_t const execname_len = strlen(execi->execname);
419 
420 	if (!execi->is_dyn)
421 		return OK;
422 
423 	if (execi->args.hdr_len < sizeof(*elf_header)) {
424 		printf("VFS: malformed ELF headers for exec\n");
425 		return ENOEXEC;
426 	}
427 
428 	if (*frame_size < STACK_MIN_SZ) {
429 		printf("VFS: malformed stack for exec(), smaller than minimum"
430 			" possible size.\n");
431 		return ENOEXEC;
432 	}
433 
434 	/* Find first Aux vector in the stack frame. */
435 	vap = (vir_bytes)(psp->ps_envstr + (psp->ps_nenvstr + 1));
436 	aux_vec = (AuxInfo *) (frame + (vap - *vsp));
437 	aux_vec_end = aux_vec + PMEF_AUXVECTORS;
438 
439 	if (((char *)aux_vec < frame) ||
440 		((char *)aux_vec > (frame + *frame_size))) {
441 		printf("VFS: malformed stack for exec(), first AuxVector is"
442 		       " not on the stack.\n");
443 		return ENOEXEC;
444 	}
445 
446 	if (((char *)aux_vec_end < frame) ||
447 		((char *)aux_vec_end > (frame + *frame_size))) {
448 		printf("VFS: malformed stack for exec(), last AuxVector is"
449 		       " not on the stack.\n");
450 		return ENOEXEC;
451 	}
452 
453 	/* Userland provides a fully filled stack frame, with argc, argv, envp
454 	 * and then all the argv and envp strings; consistent with ELF ABI,
455 	 * except for a list of Aux vectors that should be between envp points
456 	 * and the start of the strings.
457 	 *
458 	 * It would take some very unpleasant hackery to insert the aux vectors
459 	 * before the strings, and correct all the pointers, so the exec code
460 	 * in libc makes space for us.
461 	 */
462 
463 #define AUXINFO(a, type, value) \
464 	do { \
465 		if (a < aux_vec_end) { \
466 			a->a_type = type; \
467 			a->a_v = value; \
468 			a++; \
469 		} else { \
470 			printf("VFS: No more room for ELF AuxVec type %d, skipping it for %s\n", type, execi->execname); \
471 			(aux_vec_end - 1)->a_type = AT_NULL; \
472 			(aux_vec_end - 1)->a_v = 0; \
473 		} \
474 	} while(0)
475 
476 	AUXINFO(aux_vec, AT_BASE, execi->args.load_base);
477 	AUXINFO(aux_vec, AT_ENTRY, execi->args.pc);
478 	AUXINFO(aux_vec, AT_EXECFD, execi->elf_main_fd);
479 #if 0
480 	AUXINFO(aux_vec, AT_PHDR, XXX ); /* should be &phdr[0] */
481 	AUXINFO(aux_vec, AT_PHENT, elf_header->e_phentsize);
482 	AUXINFO(aux_vec, AT_PHNUM, elf_header->e_phnum);
483 
484 	AUXINFO(aux_vec, AT_RUID, XXX);
485 	AUXINFO(aux_vec, AT_RGID, XXX);
486 #endif
487 	AUXINFO(aux_vec, AT_EUID, execi->args.new_uid);
488 	AUXINFO(aux_vec, AT_EGID, execi->args.new_gid);
489 	AUXINFO(aux_vec, AT_PAGESZ, PAGE_SIZE);
490 
491 	if(execname_len < PMEF_EXECNAMELEN1) {
492 		char *spacestart;
493 		vir_bytes userp;
494 
495 		/* Empty space starts after aux_vec table; we can put the name
496 		 * here. */
497 		spacestart = (char *) aux_vec + 2 * sizeof(AuxInfo);
498 		strlcpy(spacestart, execi->execname, PMEF_EXECNAMELEN1);
499 		memset(spacestart + execname_len, '\0',
500 			PMEF_EXECNAMELEN1 - execname_len);
501 
502 		/* What will the address of the string for the user be */
503 		userp = *vsp + (spacestart - frame);
504 
505 		/* Move back to where the AT_NULL is */
506 		AUXINFO(aux_vec, AT_SUN_EXECNAME, userp);
507 	}
508 
509 	/* Always terminate with AT_NULL */
510 	AUXINFO(aux_vec, AT_NULL, 0);
511 
512 	return OK;
513 }
514 
515 /*===========================================================================*
516  *				is_script				     *
517  *===========================================================================*/
518 static int is_script(struct vfs_exec_info *execi)
519 {
520 /* Is Interpreted script? */
521   assert(execi->args.hdr != NULL);
522 
523   return(execi->args.hdr[0] == '#' && execi->args.hdr[1] == '!'
524   	&& execi->args.hdr_len >= 2);
525 }
526 
527 /*===========================================================================*
528  *				patch_stack				     *
529  *===========================================================================*/
530 static int patch_stack(vp, stack, stk_bytes, path, vsp)
531 struct vnode *vp;		/* pointer for open script file */
532 char stack[ARG_MAX];		/* pointer to stack image within VFS */
533 size_t *stk_bytes;		/* size of initial stack */
534 char path[PATH_MAX];		/* path to script file */
535 vir_bytes *vsp;
536 {
537 /* Patch the argument vector to include the path name of the script to be
538  * interpreted, and all strings on the #! line.  Returns the path name of
539  * the interpreter.
540  */
541   enum { INSERT=FALSE, REPLACE=TRUE };
542   int n, r;
543   off_t pos, new_pos;
544   char *sp, *interp = NULL;
545   size_t cum_io;
546   char buf[PAGE_SIZE];
547 
548   /* Make 'path' the new argv[0]. */
549   if (!insert_arg(stack, stk_bytes, path, vsp, REPLACE)) return(ENOMEM);
550 
551   pos = 0;	/* Read from the start of the file */
552 
553   /* Issue request */
554   r = req_readwrite(vp->v_fs_e, vp->v_inode_nr, pos, READING, VFS_PROC_NR,
555 			(vir_bytes) buf, sizeof(buf), &new_pos, &cum_io);
556 
557   if (r != OK) return(r);
558 
559   n = vp->v_size;
560   if (n > sizeof(buf))
561 	n = sizeof(buf);
562   if (n < 2) return ENOEXEC;
563 
564   sp = &(buf[2]);				/* just behind the #! */
565   n -= 2;
566   if (n > PATH_MAX) n = PATH_MAX;
567 
568   /* Use the 'path' variable for temporary storage */
569   memcpy(path, sp, n);
570 
571   if ((sp = memchr(path, '\n', n)) == NULL) /* must be a proper line */
572 	return(ENOEXEC);
573 
574   /* Move sp backwards through script[], prepending each string to stack. */
575   for (;;) {
576 	/* skip spaces behind argument. */
577 	while (sp > path && (*--sp == ' ' || *sp == '\t')) {}
578 	if (sp == path) break;
579 
580 	sp[1] = 0;
581 	/* Move to the start of the argument. */
582 	while (sp > path && sp[-1] != ' ' && sp[-1] != '\t') --sp;
583 
584 	interp = sp;
585 	if (!insert_arg(stack, stk_bytes, sp, vsp, INSERT)) {
586 		printf("VFS: patch_stack: insert_arg failed\n");
587 		return(ENOMEM);
588 	}
589   }
590 
591   if(!interp)
592   	return ENOEXEC;
593 
594   if (interp != path)
595 	memmove(path, interp, strlen(interp)+1);
596 
597   return(OK);
598 }
599 
600 /*===========================================================================*
601  *				insert_arg				     *
602  *===========================================================================*/
603 static int insert_arg(char stack[ARG_MAX], size_t *stk_bytes, char *arg,
604 	vir_bytes *vsp, char replace)
605 {
606 	/* Patch the stack so that arg will become argv[0]. Be careful, the
607 	 * stack may be filled with garbage, although it normally looks like
608 	 * this:
609 	 *	nargs argv[0] ... argv[nargs-1] NULL envp[0] ... NULL
610 	 * followed by the strings "pointed" to by the argv[i] and the envp[i].
611 	 * The * pointers are in the new process address space.
612 	 *
613 	 * Return true iff the operation succeeded.
614 	 */
615 	struct ps_strings *psp;
616 	int offset;
617 	size_t old_bytes = *stk_bytes;
618 
619 	int const arg_len = strlen(arg) + 1;
620 
621 	/* Offset to argv[0][0] in the stack frame. */
622 	int const a0 = (int)(((char **)stack)[1] - *vsp);
623 
624 	/* Check that argv[0] points within the stack frame. */
625 	if ((a0 < 0) || (a0 >= old_bytes)) {
626 		printf("vfs:: argv[0][] not within stack range!! %i\n", a0);
627 		return FALSE;
628 	}
629 
630 	if (!replace) {
631 		/* Prepending arg adds one pointer, one string and a zero byte. */
632 		offset = arg_len + PTRSIZE;
633 	} else {
634 		/* replacing argv[0] with arg adds the difference in length of
635 		 * the two strings. Make sure we don't go beyond the stack size
636 		 * when computing the length of the current argv[0]. */
637 		offset = arg_len - strnlen(stack + a0, ARG_MAX - a0 - 1);
638 	}
639 
640 	/* As ps_strings follows the strings, ensure the offset is word aligned. */
641 	offset = offset + (PTRSIZE - ((PTRSIZE + offset) % PTRSIZE));
642 
643 	/* The stack will grow (or shrink) by offset bytes. */
644 	if ((*stk_bytes += offset) > ARG_MAX) {
645 		printf("vfs:: offset too big!! %zu (max %d)\n", *stk_bytes,
646 			ARG_MAX);
647 		return FALSE;
648 	}
649 
650 	/* Reposition the strings by offset bytes */
651 	memmove(stack + a0 + offset, stack + a0, old_bytes - a0);
652 
653 	/* Put arg in the new space, leaving padding in front of it. */
654 	strlcpy(stack + a0 + offset - arg_len, arg, arg_len);
655 
656 	if (!replace) {
657 		/* Make space for a new argv[0]. */
658 		memmove(stack + 2 * PTRSIZE,
659 			stack + 1 * PTRSIZE, a0 - 2 * PTRSIZE);
660 
661 		((char **) stack)[0]++;	/* nargs++; */
662 	}
663 
664 	/* set argv[0] correctly */
665 	((char **) stack)[1] = (char *) a0 - arg_len + *vsp;
666 
667 	/* Update stack pointer in the process address space. */
668 	*vsp -= offset;
669 
670 	/* Update argv and envp in ps_strings */
671 	psp = (struct ps_strings *) (stack + *stk_bytes - sizeof(struct ps_strings));
672 	psp->ps_argvstr -= (offset / PTRSIZE);
673 	if (!replace) {
674 		psp->ps_nargvstr++;
675 	}
676 	psp->ps_envstr = psp->ps_argvstr + psp->ps_nargvstr + 1;
677 
678 	return TRUE;
679 }
680 
681 /*===========================================================================*
682  *				read_seg				     *
683  *===========================================================================*/
684 static int read_seg(struct exec_info *execi, off_t off, vir_bytes seg_addr, size_t seg_bytes)
685 {
686 /*
687  * The byte count on read is usually smaller than the segment count, because
688  * a segment is padded out to a click multiple, and the data segment is only
689  * partially initialized.
690  */
691   int r;
692   off_t new_pos;
693   size_t cum_io;
694   struct vnode *vp = ((struct vfs_exec_info *) execi->opaque)->vp;
695 
696   /* Make sure that the file is big enough */
697   if (off + seg_bytes > LONG_MAX) return(EIO);
698   if ((unsigned long) vp->v_size < off+seg_bytes) return(EIO);
699 
700   if ((r = req_readwrite(vp->v_fs_e, vp->v_inode_nr, off, READING,
701 		 execi->proc_e, (vir_bytes) seg_addr, seg_bytes,
702 		 &new_pos, &cum_io)) != OK) {
703     printf("VFS: read_seg: req_readwrite failed (data)\n");
704     return(r);
705   }
706 
707   if (r == OK && cum_io != seg_bytes)
708 	printf("VFS: read_seg segment has not been read properly\n");
709 
710 	return(r);
711 }
712 
713 
714 /*===========================================================================*
715  *				clo_exec				     *
716  *===========================================================================*/
717 static void clo_exec(struct fproc *rfp)
718 {
719 /* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec).
720  */
721   int i;
722 
723   /* Check the file desriptors one by one for presence of FD_CLOEXEC. */
724   for (i = 0; i < OPEN_MAX; i++)
725 	if ( FD_ISSET(i, &rfp->fp_cloexec_set))
726 		(void) close_fd(rfp, i);
727 }
728 
729 /*===========================================================================*
730  *				map_header				     *
731  *===========================================================================*/
732 static int map_header(struct vfs_exec_info *execi)
733 {
734   int r;
735   size_t cum_io;
736   off_t pos, new_pos;
737   /* Assume that header is not larger than a page. Align the buffer reasonably
738    * well, because libexec casts it to a structure directly and therefore
739    * expects it to be aligned appropriately. From here we can only guess the
740    * proper alignment, but 64 bits should work for all versions of ELF..
741    */
742   static char hdr[PAGE_SIZE] __aligned(8);
743 
744   pos = 0;	/* Read from the start of the file */
745 
746   /* How much is sensible to read */
747   execi->args.hdr_len = MIN(execi->vp->v_size, sizeof(hdr));
748   execi->args.hdr = hdr;
749 
750   r = req_readwrite(execi->vp->v_fs_e, execi->vp->v_inode_nr,
751   	pos, READING, VFS_PROC_NR, (vir_bytes) hdr,
752 	execi->args.hdr_len, &new_pos, &cum_io);
753   if (r != OK) {
754 	printf("VFS: exec: map_header: req_readwrite failed\n");
755 	return(r);
756   }
757 
758   return(OK);
759 }
760