コード例 #1
0
/*===========================================================================*
 *				pm_exec					     *
 *===========================================================================*/
int pm_exec(endpoint_t proc_e, vir_bytes path, size_t path_len,
		   vir_bytes frame, size_t frame_len, vir_bytes *pc,
		   vir_bytes *newsp, int user_exec_flags)
{
/* Perform the execve(name, argv, envp) call.  The user library builds a
 * complete stack image, including pointers, args, environ, etc.  The stack
 * is copied to a buffer inside VFS, and then to the new core image.
 */
  int r, slot;
  vir_bytes vsp;
  struct fproc *rfp;
  int extrabase = 0;
  static char mbuf[ARG_MAX];	/* buffer for stack and zeroes */
  struct vfs_exec_info execi;
  int i;
  static char fullpath[PATH_MAX],
  	elf_interpreter[PATH_MAX],
	firstexec[PATH_MAX],
	finalexec[PATH_MAX];
  struct lookup resolve;
  struct fproc *vmfp = &fproc[VM_PROC_NR];
  stackhook_t makestack = NULL;
  static int n;
  n++;
  struct filp *newfilp = NULL;

  lock_exec();
  lock_proc(vmfp, 0);

  /* unset execi values are 0. */
  memset(&execi, 0, sizeof(execi));
  execi.vmfd = -1;

  /* passed from exec() libc code */
  execi.userflags = user_exec_flags;
  execi.args.stack_high = kinfo.user_sp;
  execi.args.stack_size = DEFAULT_STACK_LIMIT;

  okendpt(proc_e, &slot);
  rfp = fp = &fproc[slot];

  lookup_init(&resolve, fullpath, PATH_NOFLAGS, &execi.vmp, &execi.vp);

  resolve.l_vmnt_lock = VMNT_READ;
  resolve.l_vnode_lock = VNODE_READ;

  /* Fetch the stack from the user before destroying the old core image. */
  if (frame_len > ARG_MAX)
	FAILCHECK(ENOMEM); /* stack too big */

  r = sys_datacopy(proc_e, (vir_bytes) frame, SELF, (vir_bytes) mbuf,
		   (size_t) frame_len);
  if (r != OK) { /* can't fetch stack (e.g. bad virtual addr) */
        printf("VFS: pm_exec: sys_datacopy failed\n");
	FAILCHECK(r);
  }

  /* The default is to keep the original user and group IDs */
  execi.args.new_uid = rfp->fp_effuid;
  execi.args.new_gid = rfp->fp_effgid;

  /* Get the exec file name. */
  FAILCHECK(fetch_name(path, path_len, fullpath));
  strlcpy(finalexec, fullpath, PATH_MAX);
  strlcpy(firstexec, fullpath, PATH_MAX);

  /* Get_read_vp will return an opened vn in execi.
   * if necessary it releases the existing vp so we can
   * switch after we find out what's inside the file.
   * It reads the start of the file.
   */
  Get_read_vp(execi, fullpath, 1, 1, &resolve, fp);

  /* If this is a script (i.e. has a #!/interpreter line),
   * retrieve the name of the interpreter and open that
   * executable instead.
   */
  if(is_script(&execi)) {
  	/* patch_stack will add interpreter name and
	 * args to stack and retrieve the new binary
	 * name into fullpath.
	 */
	FAILCHECK(fetch_name(path, path_len, fullpath));
	FAILCHECK(patch_stack(execi.vp, mbuf, &frame_len, fullpath));
	strlcpy(finalexec, fullpath, PATH_MAX);
  	strlcpy(firstexec, fullpath, PATH_MAX);
	Get_read_vp(execi, fullpath, 1, 0, &resolve, fp);
  }

  /* If this is a dynamically linked executable, retrieve
   * the name of that interpreter in elf_interpreter and open that
   * executable instead. But open the current executable in an
   * fd for the current process.
   */
  if(elf_has_interpreter(execi.args.hdr, execi.args.hdr_len,
	elf_interpreter, sizeof(elf_interpreter))) {
	/* Switch the executable vnode to the interpreter */
	execi.is_dyn = 1;

	/* The interpreter (loader) needs an fd to the main program,
	 * which is currently in finalexec
	 */
	if((r = execi.elf_main_fd = common_open(finalexec, O_RDONLY, 0)) < 0) {
		printf("VFS: exec: dynamic: open main exec failed %s (%d)\n",
			fullpath, r);
		FAILCHECK(r);
	}

	/* ld.so is linked at 0, but it can relocate itself; we
	 * want it higher to trap NULL pointer dereferences. 
	 */
	execi.args.load_offset = 0x10000;

	/* Remember it */
	strlcpy(execi.execname, finalexec, PATH_MAX);

	/* The executable we need to execute first (loader)
	 * is in elf_interpreter, and has to be in fullpath to
	 * be looked up
	 */
	strlcpy(fullpath, elf_interpreter, PATH_MAX);
	strlcpy(firstexec, elf_interpreter, PATH_MAX);
	Get_read_vp(execi, fullpath, 0, 0, &resolve, fp);
  }

  /* We also want an FD for VM to mmap() the process in if possible. */
  {
	struct vnode *vp = execi.vp;
	assert(vp);
	if(vp->v_vmnt->m_haspeek && major(vp->v_dev) != MEMORY_MAJOR) {
		int newfd = -1;
		if(get_fd(vmfp, 0, R_BIT, &newfd, &newfilp) == OK) {
			assert(newfd >= 0 && newfd < OPEN_MAX);
			assert(!vmfp->fp_filp[newfd]);
			newfilp->filp_count = 1;
			newfilp->filp_vno = vp;
			newfilp->filp_flags = O_RDONLY;
			FD_SET(newfd, &vmfp->fp_filp_inuse);
			vmfp->fp_filp[newfd] = newfilp;
			/* dup_vnode(vp); */
			execi.vmfd = newfd;
			execi.args.memmap = vfs_memmap;
		}
	}
  }

  /* callback functions and data */
  execi.args.copymem = read_seg;
  execi.args.clearproc = libexec_clearproc_vm_procctl;
  execi.args.clearmem = libexec_clear_sys_memset;
  execi.args.allocmem_prealloc_cleared = libexec_alloc_mmap_prealloc_cleared;
  execi.args.allocmem_prealloc_junk = libexec_alloc_mmap_prealloc_junk;
  execi.args.allocmem_ondemand = libexec_alloc_mmap_ondemand;
  execi.args.opaque = &execi;

  execi.args.proc_e = proc_e;
  execi.args.frame_len = frame_len;
  execi.args.filesize = execi.vp->v_size;

  for (i = 0; exec_loaders[i].load_object != NULL; i++) {
      r = (*exec_loaders[i].load_object)(&execi.args);
      /* Loaded successfully, so no need to try other loaders */
      if (r == OK) { makestack = exec_loaders[i].setup_stack; break; }
  }

  FAILCHECK(r);

  /* Inform PM */
  FAILCHECK(libexec_pm_newexec(proc_e, &execi.args));

  /* Save off PC */
  *pc = execi.args.pc;

  /* call a stack-setup function if this executable type wants it */
  vsp = execi.args.stack_high - frame_len;
  if(makestack) FAILCHECK(makestack(&execi, mbuf, &frame_len, &vsp, &extrabase));

  /* Patch up stack and copy it from VFS to new core image. */
  libexec_patch_ptr(mbuf, vsp + extrabase);
  FAILCHECK(sys_datacopy(SELF, (vir_bytes) mbuf, proc_e, (vir_bytes) vsp,
		   (phys_bytes)frame_len));

  /* Return new stack pointer to caller */
  *newsp = vsp;

  clo_exec(rfp);

  if (execi.args.allow_setuid) {
	/* If after loading the image we're still allowed to run with
	 * setuid or setgid, change credentials now */
	rfp->fp_effuid = execi.args.new_uid;
	rfp->fp_effgid = execi.args.new_gid;
  }

  /* Remember the new name of the process */
  strlcpy(rfp->fp_name, execi.args.progname, PROC_NAME_LEN);

pm_execfinal:
  if(newfilp) unlock_filp(newfilp);
  else if (execi.vp != NULL) {
	unlock_vnode(execi.vp);
	put_vnode(execi.vp);
  }

  if(execi.vmfd >= 0 && !execi.vmfd_used) {
  	if(OK != close_fd(vmfp, execi.vmfd)) {
		printf("VFS: unexpected close fail of vm fd\n");
	}
  }

  unlock_proc(vmfp);
  unlock_exec();

  return(r);
}
コード例 #2
0
ファイル: exec_elf.c プロジェクト: kl07/minix-course
int libexec_load_elf(struct exec_info *execi)
{
	Elf_Ehdr *hdr = NULL;
	Elf_Phdr *phdr = NULL;
	int e, i = 0;
	int first = 1;
	vir_bytes startv = 0, stacklow;

	assert(execi != NULL);
	assert(execi->hdr != NULL);

	if((e=elf_unpack(execi->hdr, execi->hdr_len, &hdr, &phdr)) != OK) {
		return e;
	 }

	/* this function can load the dynamic linker, but that
	 * shouldn't require an interpreter itself.
	 */
	i = elf_has_interpreter(execi->hdr, execi->hdr_len, NULL, 0);
	if(i > 0) {
	      return ENOEXEC;
	}

	execi->stack_size = roundup(execi->stack_size, PAGE_SIZE);
	execi->stack_high = rounddown(execi->stack_high, PAGE_SIZE);
	stacklow = execi->stack_high - execi->stack_size;

	assert(execi->copymem);
	assert(execi->clearmem);
	assert(execi->allocmem_prealloc_cleared);
	assert(execi->allocmem_prealloc_junk);
	assert(execi->allocmem_ondemand);

	for (i = 0; i < hdr->e_phnum; i++) {
		Elf_Phdr *ph = &phdr[i];
		off_t file_limit = ph->p_offset + ph->p_filesz;
		/* sanity check binary before wiping out the target process */
		if(execi->filesize < file_limit) {
			return ENOEXEC;
		}
	}

	if(execi->clearproc) execi->clearproc(execi);

	for (i = 0; i < hdr->e_phnum; i++) {
		vir_bytes seg_membytes, page_offset, p_vaddr, vaddr;
		vir_bytes chunk, vfileend, vmemend;
		off_t foffset, fbytes;
		Elf_Phdr *ph = &phdr[i];
		int try_mmap = 1;
		u16_t clearend = 0;
		int pagechunk;
		int mmap_prot = PROT_READ;

		if(!(ph->p_flags & PF_R)) {
			printf("libexec: warning: unreadable segment\n");
		}

		if(ph->p_flags & PF_W) {
			mmap_prot |= PROT_WRITE;
#if ELF_DEBUG
			printf("libexec: adding PROT_WRITE\n");
#endif
		} else {
#if ELF_DEBUG
			printf("libexec: not adding PROT_WRITE\n");
#endif
		}

		if (ph->p_type != PT_LOAD || ph->p_memsz == 0) continue;

		if((ph->p_vaddr % PAGE_SIZE) != (ph->p_offset % PAGE_SIZE)) {
			printf("libexec: unaligned ELF program?\n");
			try_mmap = 0;
		}

		if(!execi->memmap) {
			try_mmap = 0;
		}

		foffset = ph->p_offset;
		fbytes = ph->p_filesz;
		vaddr = p_vaddr = ph->p_vaddr + execi->load_offset;
		seg_membytes = ph->p_memsz;

		page_offset = vaddr % PAGE_SIZE;
		vaddr -= page_offset;
		foffset -= page_offset;
		seg_membytes += page_offset;
		fbytes += page_offset;
		vfileend  = p_vaddr + ph->p_filesz;

		/* if there's usable memory after the file end, we have
		 * to tell VM to clear the memory part of the page when it's
		 * mapped in
		 */
		if((pagechunk = (vfileend % PAGE_SIZE))
			&& ph->p_filesz < ph->p_memsz) {
			clearend = PAGE_SIZE - pagechunk;
		}

		seg_membytes = roundup(seg_membytes, PAGE_SIZE);
		fbytes = roundup(fbytes, PAGE_SIZE);

		if(first || startv > vaddr) startv = vaddr;
		first = 0;

		if ((ph->p_flags & PF_X) != 0 && execi->text_size < seg_membytes)
			execi->text_size = seg_membytes;
		else
			execi->data_size = seg_membytes;

		if(try_mmap && execi->memmap(execi, vaddr, fbytes, foffset, clearend, mmap_prot) == OK) {
#if ELF_DEBUG
			printf("libexec: mmap 0x%lx-0x%lx done, clearend 0x%x\n",
				vaddr, vaddr+fbytes, clearend);
#endif

			if(seg_membytes > fbytes) {
				int rem_mem = seg_membytes - fbytes;;
				vir_bytes remstart = vaddr + fbytes;
				if(execi->allocmem_ondemand(execi,
					remstart, rem_mem) != OK) {
					printf("libexec: mmap extra mem failed\n");
					return ENOMEM;
				}
#if ELF_DEBUG
				else printf("libexec: allocated 0x%lx-0x%lx\n",

					remstart, remstart+rem_mem);
#endif
			}
		} else {
			/* make us some memory */
			if(execi->allocmem_prealloc_junk(execi, vaddr, seg_membytes) != OK) {
				if(execi->clearproc) execi->clearproc(execi);
				return ENOMEM;
			}

#if ELF_DEBUG
			printf("mmapped 0x%lx-0x%lx\n", vaddr, vaddr+seg_membytes);
#endif

			/* Copy executable section into it */
			if(execi->copymem(execi, ph->p_offset, p_vaddr, ph->p_filesz) != OK) {
				if(execi->clearproc) execi->clearproc(execi);
				return ENOMEM;
			}

#if ELF_DEBUG
			printf("copied 0x%lx-0x%lx\n", p_vaddr, p_vaddr+ph->p_filesz);
#endif

			/* Clear remaining bits */
			vmemend = vaddr + seg_membytes;
			if((chunk = p_vaddr - vaddr) > 0) {
#if ELF_DEBUG
				printf("start clearing 0x%lx-0x%lx\n", vaddr, vaddr+chunk);
#endif
				execi->clearmem(execi, vaddr, chunk);
			}
	
			if((chunk = vmemend - vfileend) > 0) {
#if ELF_DEBUG
				printf("end clearing 0x%lx-0x%lx\n", vfileend, vaddr+chunk);
#endif
				execi->clearmem(execi, vfileend, chunk);
			}
		}
	}

	/* Make it a stack */
	if(execi->allocmem_ondemand(execi, stacklow, execi->stack_size) != OK) {
		if(execi->clearproc) execi->clearproc(execi);
		return ENOMEM;
	}

#if ELF_DEBUG
	printf("stack mmapped 0x%lx-0x%lx\n", stacklow, stacklow+execi->stack_size);
#endif

	/* record entry point and lowest load vaddr for caller */
	execi->pc = hdr->e_entry + execi->load_offset;
	execi->load_base = startv;

	return OK;
}
コード例 #3
0
int libexec_load_elf(struct exec_info *execi)
{
	Elf_Ehdr *hdr = NULL;
	Elf_Phdr *phdr = NULL;
	int e, i = 0;
	int first = 1;
	vir_bytes startv = 0, stacklow;

	assert(execi != NULL);
	assert(execi->hdr != NULL);

	if((e=elf_unpack(execi->hdr, execi->hdr_len, &hdr, &phdr)) != OK) {
		printf("libexec_load_elf: elf_unpack failed\n");
		return e;
	 }

	/* this function can load the dynamic linker, but that
	 * shouldn't require an interpreter itself.
	 */
	i = elf_has_interpreter(execi->hdr, execi->hdr_len, NULL, 0);
	if(i > 0) {
	      return ENOEXEC;
	}

	execi->stack_size = roundup(execi->stack_size, PAGE_SIZE);
	execi->stack_high = rounddown(execi->stack_high, PAGE_SIZE);
	stacklow = execi->stack_high - execi->stack_size;

	assert(execi->copymem);
	assert(execi->clearmem);
	assert(execi->allocmem_prealloc);
	assert(execi->allocmem_ondemand);

	if(execi->clearproc) execi->clearproc(execi);

	for (i = 0; i < hdr->e_phnum; i++) {
		vir_bytes seg_membytes, page_offset, p_vaddr, vaddr;
		vir_bytes chunk, vfileend, vmemend;
		Elf_Phdr *ph = &phdr[i];
		if (ph->p_type != PT_LOAD || ph->p_memsz == 0) continue;
		vaddr = p_vaddr = ph->p_vaddr + execi->load_offset;
		seg_membytes = ph->p_memsz;
		page_offset = vaddr % PAGE_SIZE;
		vaddr -= page_offset;
		seg_membytes += page_offset;
		seg_membytes = roundup(seg_membytes, PAGE_SIZE);
		if(first || startv > vaddr) startv = vaddr;
		first = 0;

		/* make us some memory */
		if(execi->allocmem_prealloc(execi, vaddr, seg_membytes) != OK) {
			if(execi->clearproc) execi->clearproc(execi);
			return ENOMEM;
		}

#if ELF_DEBUG
		printf("mmapped 0x%lx-0x%lx\n", vaddr, vaddr+seg_membytes);
#endif

		/* Copy executable section into it */
		if(execi->copymem(execi, ph->p_offset, p_vaddr, ph->p_filesz) != OK) {
			if(execi->clearproc) execi->clearproc(execi);
			return ENOMEM;
		}

#if ELF_DEBUG
		printf("copied 0x%lx-0x%lx\n", p_vaddr, p_vaddr+ph->p_filesz);
#endif

		/* Clear remaining bits */
		vfileend  = p_vaddr + ph->p_filesz;
		vmemend = vaddr + seg_membytes;
		if((chunk = p_vaddr - vaddr) > 0) {
#if ELF_DEBUG
			printf("start clearing 0x%lx-0x%lx\n", vaddr, vaddr+chunk);
#endif
			execi->clearmem(execi, vaddr, chunk);
		}
		if((chunk = vmemend - vfileend) > 0) {
#if ELF_DEBUG
			printf("end clearing 0x%lx-0x%lx\n", vfileend, vaddr+chunk);
#endif
			execi->clearmem(execi, vfileend, chunk);
		}
	}

	/* Make it a stack */
	if(execi->allocmem_ondemand(execi, stacklow, execi->stack_size) != OK) {
		if(execi->clearproc) execi->clearproc(execi);
		return ENOMEM;
	}

#if ELF_DEBUG
	printf("stack mmapped 0x%lx-0x%lx\n", stacklow, stacklow+execi->stack_size);
#endif

	/* record entry point and lowest load vaddr for caller */
	execi->pc = hdr->e_entry + execi->load_offset;
	execi->load_base = startv;

	return OK;
}