Files
xemu/linux-user/linuxload.c
T

176 lines
4.2 KiB
C
Raw Normal View History

2007-06-03 15:33:32 +00:00
/* Code for loading Linux executables. Mostly linux kernel code. */
2006-06-11 13:32:59 +00:00
2016-01-26 18:17:02 +00:00
#include "qemu/osdep.h"
2006-06-11 13:32:59 +00:00
#include "qemu.h"
#include "user-internals.h"
#include "loader.h"
2006-06-11 13:32:59 +00:00
#define NGROUPS 32
/* ??? This should really be somewhere else. */
abi_long memcpy_to_target(abi_ulong dest, const void *src, unsigned long len)
2006-06-11 13:32:59 +00:00
{
void *host_ptr;
host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
if (!host_ptr) {
return -TARGET_EFAULT;
}
2006-06-11 13:32:59 +00:00
memcpy(host_ptr, src, len);
unlock_user(host_ptr, dest, 1);
return 0;
2006-06-11 13:32:59 +00:00
}
static int count(char **vec)
2006-06-11 13:32:59 +00:00
{
int i;
2006-06-11 13:32:59 +00:00
for (i = 0; *vec; i++) {
2006-06-11 13:32:59 +00:00
vec++;
}
return i;
2006-06-11 13:32:59 +00:00
}
static int prepare_binprm(struct linux_binprm *bprm)
{
struct stat st;
2006-06-11 13:32:59 +00:00
int mode;
int retval;
2006-06-11 13:32:59 +00:00
if (fstat(bprm->fd, &st) < 0) {
return -errno;
2006-06-11 13:32:59 +00:00
}
mode = st.st_mode;
if (!S_ISREG(mode)) { /* Must be regular file */
return -EACCES;
2006-06-11 13:32:59 +00:00
}
if (!(mode & 0111)) { /* Must have at least one execute bit set */
return -EACCES;
2006-06-11 13:32:59 +00:00
}
bprm->e_uid = geteuid();
bprm->e_gid = getegid();
/* Set-uid? */
if (mode & S_ISUID) {
2018-12-13 23:37:37 +01:00
bprm->e_uid = st.st_uid;
2006-06-11 13:32:59 +00:00
}
/* Set-gid? */
/*
* If setgid is set but no group execute bit then this
* is a candidate for mandatory locking, not a setgid
* executable.
*/
if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
2018-12-13 23:37:37 +01:00
bprm->e_gid = st.st_gid;
2006-06-11 13:32:59 +00:00
}
retval = read(bprm->fd, bprm->buf, BPRM_BUF_SIZE);
if (retval < 0) {
2018-12-13 23:37:37 +01:00
perror("prepare_binprm");
exit(-1);
2006-06-11 13:32:59 +00:00
}
if (retval < BPRM_BUF_SIZE) {
/* Make sure the rest of the loader won't read garbage. */
memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval);
2006-06-11 13:32:59 +00:00
}
return retval;
2006-06-11 13:32:59 +00:00
}
/* Construct the envp and argv tables on the target stack. */
abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
abi_ulong stringp, int push_ptr)
2006-06-11 13:32:59 +00:00
{
TaskState *ts = (TaskState *)thread_cpu->opaque;
int n = sizeof(abi_ulong);
abi_ulong envp;
abi_ulong argv;
2006-06-11 13:32:59 +00:00
sp -= (envc + 1) * n;
envp = sp;
sp -= (argc + 1) * n;
argv = sp;
ts->info->envp = envp;
ts->info->envc = envc;
ts->info->argv = argv;
ts->info->argc = argc;
2006-06-11 13:32:59 +00:00
if (push_ptr) {
/* FIXME - handle put_user() failures */
sp -= n;
put_user_ual(envp, sp);
sp -= n;
put_user_ual(argv, sp);
2006-06-11 13:32:59 +00:00
}
sp -= n;
/* FIXME - handle put_user() failures */
put_user_ual(argc, sp);
ts->info->arg_strings = stringp;
2006-06-11 13:32:59 +00:00
while (argc-- > 0) {
/* FIXME - handle put_user() failures */
put_user_ual(stringp, argv);
argv += n;
2006-06-11 13:32:59 +00:00
stringp += target_strlen(stringp) + 1;
}
/* FIXME - handle put_user() failures */
put_user_ual(0, argv);
ts->info->env_strings = stringp;
2006-06-11 13:32:59 +00:00
while (envc-- > 0) {
/* FIXME - handle put_user() failures */
put_user_ual(stringp, envp);
envp += n;
2006-06-11 13:32:59 +00:00
stringp += target_strlen(stringp) + 1;
}
/* FIXME - handle put_user() failures */
put_user_ual(0, envp);
2006-06-11 13:32:59 +00:00
return sp;
}
int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
struct target_pt_regs *regs, struct image_info *infop,
struct linux_binprm *bprm)
2006-06-11 13:32:59 +00:00
{
int retval;
bprm->fd = fdexec;
bprm->filename = (char *)filename;
bprm->argc = count(argv);
bprm->argv = argv;
bprm->envc = count(envp);
bprm->envp = envp;
2006-06-11 13:32:59 +00:00
retval = prepare_binprm(bprm);
2006-06-11 13:32:59 +00:00
if (retval >= 0) {
if (bprm->buf[0] == 0x7f
&& bprm->buf[1] == 'E'
&& bprm->buf[2] == 'L'
&& bprm->buf[3] == 'F') {
retval = load_elf_binary(bprm, infop);
2006-06-11 13:32:59 +00:00
#if defined(TARGET_HAS_BFLT)
} else if (bprm->buf[0] == 'b'
&& bprm->buf[1] == 'F'
&& bprm->buf[2] == 'L'
&& bprm->buf[3] == 'T') {
retval = load_flt_binary(bprm, infop);
2006-06-11 13:32:59 +00:00
#endif
} else {
return -ENOEXEC;
2006-06-11 13:32:59 +00:00
}
}
if (retval >= 0) {
2006-06-11 13:32:59 +00:00
/* success. Initialize important registers */
do_init_thread(regs, infop);
return retval;
}
return retval;
2006-06-11 13:32:59 +00:00
}