blob: 381ab8914be016349d4fafadf6997d7eb0abfd8d [file] [log] [blame]
ths0458df22007-06-03 15:33:32 +00001/* Code for loading Linux executables. Mostly linux kernel code. */
pbrooke5fe0c52006-06-11 13:32:59 +00002
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <errno.h>
7#include <unistd.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11#include "qemu.h"
12
13#define NGROUPS 32
14
15/* ??? This should really be somewhere else. */
bellard579a97f2007-11-11 14:26:47 +000016abi_long memcpy_to_target(abi_ulong dest, const void *src,
17 unsigned long len)
pbrooke5fe0c52006-06-11 13:32:59 +000018{
19 void *host_ptr;
20
bellard579a97f2007-11-11 14:26:47 +000021 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
22 if (!host_ptr)
23 return -TARGET_EFAULT;
pbrooke5fe0c52006-06-11 13:32:59 +000024 memcpy(host_ptr, src, len);
25 unlock_user(host_ptr, dest, 1);
bellard579a97f2007-11-11 14:26:47 +000026 return 0;
pbrooke5fe0c52006-06-11 13:32:59 +000027}
28
pbrooke5fe0c52006-06-11 13:32:59 +000029static int count(char ** vec)
30{
31 int i;
32
33 for(i = 0; *vec; i++) {
34 vec++;
35 }
36
37 return(i);
38}
39
40static int prepare_binprm(struct linux_binprm *bprm)
41{
42 struct stat st;
43 int mode;
Juan Quintela331c23b2011-06-16 17:37:08 +010044 int retval;
pbrooke5fe0c52006-06-11 13:32:59 +000045
46 if(fstat(bprm->fd, &st) < 0) {
47 return(-errno);
48 }
49
50 mode = st.st_mode;
51 if(!S_ISREG(mode)) { /* Must be regular file */
52 return(-EACCES);
53 }
54 if(!(mode & 0111)) { /* Must have at least one execute bit set */
55 return(-EACCES);
56 }
57
58 bprm->e_uid = geteuid();
59 bprm->e_gid = getegid();
pbrooke5fe0c52006-06-11 13:32:59 +000060
61 /* Set-uid? */
62 if(mode & S_ISUID) {
63 bprm->e_uid = st.st_uid;
pbrooke5fe0c52006-06-11 13:32:59 +000064 }
65
66 /* Set-gid? */
67 /*
68 * If setgid is set but no group execute bit then this
69 * is a candidate for mandatory locking, not a setgid
70 * executable.
71 */
72 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
73 bprm->e_gid = st.st_gid;
pbrooke5fe0c52006-06-11 13:32:59 +000074 }
75
Richard Henderson9955ffa2010-07-27 10:25:30 -070076 retval = read(bprm->fd, bprm->buf, BPRM_BUF_SIZE);
77 if (retval < 0) {
pbrooke5fe0c52006-06-11 13:32:59 +000078 perror("prepare_binprm");
79 exit(-1);
pbrooke5fe0c52006-06-11 13:32:59 +000080 }
Richard Henderson9955ffa2010-07-27 10:25:30 -070081 if (retval < BPRM_BUF_SIZE) {
82 /* Make sure the rest of the loader won't read garbage. */
83 memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval);
pbrooke5fe0c52006-06-11 13:32:59 +000084 }
Richard Henderson9955ffa2010-07-27 10:25:30 -070085 return retval;
pbrooke5fe0c52006-06-11 13:32:59 +000086}
87
88/* Construct the envp and argv tables on the target stack. */
blueswir1992f48a2007-10-14 16:27:31 +000089abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
90 abi_ulong stringp, int push_ptr)
pbrooke5fe0c52006-06-11 13:32:59 +000091{
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030092 TaskState *ts = (TaskState *)thread_env->opaque;
blueswir1992f48a2007-10-14 16:27:31 +000093 int n = sizeof(abi_ulong);
94 abi_ulong envp;
95 abi_ulong argv;
pbrooke5fe0c52006-06-11 13:32:59 +000096
97 sp -= (envc + 1) * n;
98 envp = sp;
99 sp -= (argc + 1) * n;
100 argv = sp;
101 if (push_ptr) {
bellard2f619692007-11-16 10:46:05 +0000102 /* FIXME - handle put_user() failures */
103 sp -= n;
104 put_user_ual(envp, sp);
105 sp -= n;
106 put_user_ual(argv, sp);
pbrooke5fe0c52006-06-11 13:32:59 +0000107 }
bellard2f619692007-11-16 10:46:05 +0000108 sp -= n;
109 /* FIXME - handle put_user() failures */
110 put_user_ual(argc, sp);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300111 ts->info->arg_start = stringp;
pbrooke5fe0c52006-06-11 13:32:59 +0000112 while (argc-- > 0) {
bellard2f619692007-11-16 10:46:05 +0000113 /* FIXME - handle put_user() failures */
114 put_user_ual(stringp, argv);
115 argv += n;
pbrooke5fe0c52006-06-11 13:32:59 +0000116 stringp += target_strlen(stringp) + 1;
117 }
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300118 ts->info->arg_end = stringp;
bellard2f619692007-11-16 10:46:05 +0000119 /* FIXME - handle put_user() failures */
120 put_user_ual(0, argv);
pbrooke5fe0c52006-06-11 13:32:59 +0000121 while (envc-- > 0) {
bellard2f619692007-11-16 10:46:05 +0000122 /* FIXME - handle put_user() failures */
123 put_user_ual(stringp, envp);
124 envp += n;
pbrooke5fe0c52006-06-11 13:32:59 +0000125 stringp += target_strlen(stringp) + 1;
126 }
bellard2f619692007-11-16 10:46:05 +0000127 /* FIXME - handle put_user() failures */
128 put_user_ual(0, envp);
pbrooke5fe0c52006-06-11 13:32:59 +0000129
130 return sp;
131}
132
ths5fafdf22007-09-16 21:08:06 +0000133int loader_exec(const char * filename, char ** argv, char ** envp,
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300134 struct target_pt_regs * regs, struct image_info *infop,
135 struct linux_binprm *bprm)
pbrooke5fe0c52006-06-11 13:32:59 +0000136{
pbrooke5fe0c52006-06-11 13:32:59 +0000137 int retval;
138 int i;
139
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300140 bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
Richard Henderson9955ffa2010-07-27 10:25:30 -0700141 memset(bprm->page, 0, sizeof(bprm->page));
pbrooke5fe0c52006-06-11 13:32:59 +0000142 retval = open(filename, O_RDONLY);
Peter Maydell885c1d12012-08-24 06:55:53 +0000143 if (retval < 0) {
144 return -errno;
145 }
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300146 bprm->fd = retval;
147 bprm->filename = (char *)filename;
148 bprm->argc = count(argv);
149 bprm->argv = argv;
150 bprm->envc = count(envp);
151 bprm->envp = envp;
pbrooke5fe0c52006-06-11 13:32:59 +0000152
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300153 retval = prepare_binprm(bprm);
pbrooke5fe0c52006-06-11 13:32:59 +0000154
pbrooke5fe0c52006-06-11 13:32:59 +0000155 if(retval>=0) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300156 if (bprm->buf[0] == 0x7f
157 && bprm->buf[1] == 'E'
158 && bprm->buf[2] == 'L'
159 && bprm->buf[3] == 'F') {
Richard Henderson6495a042010-04-22 17:24:58 -0700160 retval = load_elf_binary(bprm, regs, infop);
pbrooke5fe0c52006-06-11 13:32:59 +0000161#if defined(TARGET_HAS_BFLT)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300162 } else if (bprm->buf[0] == 'b'
163 && bprm->buf[1] == 'F'
164 && bprm->buf[2] == 'L'
165 && bprm->buf[3] == 'T') {
166 retval = load_flt_binary(bprm,regs,infop);
pbrooke5fe0c52006-06-11 13:32:59 +0000167#endif
168 } else {
Peter Maydell885c1d12012-08-24 06:55:53 +0000169 return -ENOEXEC;
pbrooke5fe0c52006-06-11 13:32:59 +0000170 }
171 }
ths3b46e622007-09-17 08:09:54 +0000172
pbrooke5fe0c52006-06-11 13:32:59 +0000173 if(retval>=0) {
174 /* success. Initialize important registers */
175 do_init_thread(regs, infop);
176 return retval;
177 }
178
179 /* Something went wrong, return the inode and free the argument pages*/
180 for (i=0 ; i<MAX_ARG_PAGES ; i++) {
Peter Maydell7dd47662011-11-09 19:22:11 +0000181 g_free(bprm->page[i]);
pbrooke5fe0c52006-06-11 13:32:59 +0000182 }
183 return(retval);
184}