blob: a27e1d0d8bc2c0cce58c93018a1f195b31624cc3 [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
Peter Maydelld39594e2016-01-26 18:17:02 +00003#include "qemu/osdep.h"
pbrooke5fe0c52006-06-11 13:32:59 +00004
5#include "qemu.h"
6
7#define NGROUPS 32
8
9/* ??? This should really be somewhere else. */
bellard579a97f2007-11-11 14:26:47 +000010abi_long memcpy_to_target(abi_ulong dest, const void *src,
11 unsigned long len)
pbrooke5fe0c52006-06-11 13:32:59 +000012{
13 void *host_ptr;
14
bellard579a97f2007-11-11 14:26:47 +000015 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
16 if (!host_ptr)
17 return -TARGET_EFAULT;
pbrooke5fe0c52006-06-11 13:32:59 +000018 memcpy(host_ptr, src, len);
19 unlock_user(host_ptr, dest, 1);
bellard579a97f2007-11-11 14:26:47 +000020 return 0;
pbrooke5fe0c52006-06-11 13:32:59 +000021}
22
pbrooke5fe0c52006-06-11 13:32:59 +000023static int count(char ** vec)
24{
25 int i;
26
27 for(i = 0; *vec; i++) {
28 vec++;
29 }
30
31 return(i);
32}
33
34static int prepare_binprm(struct linux_binprm *bprm)
35{
36 struct stat st;
37 int mode;
Juan Quintela331c23b2011-06-16 17:37:08 +010038 int retval;
pbrooke5fe0c52006-06-11 13:32:59 +000039
40 if(fstat(bprm->fd, &st) < 0) {
Paolo Bonzini7d374352018-12-13 23:37:37 +010041 return(-errno);
pbrooke5fe0c52006-06-11 13:32:59 +000042 }
43
44 mode = st.st_mode;
45 if(!S_ISREG(mode)) { /* Must be regular file */
Paolo Bonzini7d374352018-12-13 23:37:37 +010046 return(-EACCES);
pbrooke5fe0c52006-06-11 13:32:59 +000047 }
48 if(!(mode & 0111)) { /* Must have at least one execute bit set */
Paolo Bonzini7d374352018-12-13 23:37:37 +010049 return(-EACCES);
pbrooke5fe0c52006-06-11 13:32:59 +000050 }
51
52 bprm->e_uid = geteuid();
53 bprm->e_gid = getegid();
pbrooke5fe0c52006-06-11 13:32:59 +000054
55 /* Set-uid? */
56 if(mode & S_ISUID) {
Paolo Bonzini7d374352018-12-13 23:37:37 +010057 bprm->e_uid = st.st_uid;
pbrooke5fe0c52006-06-11 13:32:59 +000058 }
59
60 /* Set-gid? */
61 /*
62 * If setgid is set but no group execute bit then this
63 * is a candidate for mandatory locking, not a setgid
64 * executable.
65 */
66 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
Paolo Bonzini7d374352018-12-13 23:37:37 +010067 bprm->e_gid = st.st_gid;
pbrooke5fe0c52006-06-11 13:32:59 +000068 }
69
Richard Henderson9955ffa2010-07-27 10:25:30 -070070 retval = read(bprm->fd, bprm->buf, BPRM_BUF_SIZE);
71 if (retval < 0) {
Paolo Bonzini7d374352018-12-13 23:37:37 +010072 perror("prepare_binprm");
73 exit(-1);
pbrooke5fe0c52006-06-11 13:32:59 +000074 }
Richard Henderson9955ffa2010-07-27 10:25:30 -070075 if (retval < BPRM_BUF_SIZE) {
76 /* Make sure the rest of the loader won't read garbage. */
77 memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval);
pbrooke5fe0c52006-06-11 13:32:59 +000078 }
Richard Henderson9955ffa2010-07-27 10:25:30 -070079 return retval;
pbrooke5fe0c52006-06-11 13:32:59 +000080}
81
82/* Construct the envp and argv tables on the target stack. */
blueswir1992f48a2007-10-14 16:27:31 +000083abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
84 abi_ulong stringp, int push_ptr)
pbrooke5fe0c52006-06-11 13:32:59 +000085{
Andreas Färber0429a972013-08-26 18:14:44 +020086 TaskState *ts = (TaskState *)thread_cpu->opaque;
blueswir1992f48a2007-10-14 16:27:31 +000087 int n = sizeof(abi_ulong);
88 abi_ulong envp;
89 abi_ulong argv;
pbrooke5fe0c52006-06-11 13:32:59 +000090
91 sp -= (envc + 1) * n;
92 envp = sp;
93 sp -= (argc + 1) * n;
94 argv = sp;
95 if (push_ptr) {
bellard2f619692007-11-16 10:46:05 +000096 /* FIXME - handle put_user() failures */
97 sp -= n;
98 put_user_ual(envp, sp);
99 sp -= n;
100 put_user_ual(argv, sp);
pbrooke5fe0c52006-06-11 13:32:59 +0000101 }
bellard2f619692007-11-16 10:46:05 +0000102 sp -= n;
103 /* FIXME - handle put_user() failures */
104 put_user_ual(argc, sp);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300105 ts->info->arg_start = stringp;
pbrooke5fe0c52006-06-11 13:32:59 +0000106 while (argc-- > 0) {
bellard2f619692007-11-16 10:46:05 +0000107 /* FIXME - handle put_user() failures */
108 put_user_ual(stringp, argv);
109 argv += n;
pbrooke5fe0c52006-06-11 13:32:59 +0000110 stringp += target_strlen(stringp) + 1;
111 }
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300112 ts->info->arg_end = stringp;
bellard2f619692007-11-16 10:46:05 +0000113 /* FIXME - handle put_user() failures */
114 put_user_ual(0, argv);
pbrooke5fe0c52006-06-11 13:32:59 +0000115 while (envc-- > 0) {
bellard2f619692007-11-16 10:46:05 +0000116 /* FIXME - handle put_user() failures */
117 put_user_ual(stringp, envp);
118 envp += n;
pbrooke5fe0c52006-06-11 13:32:59 +0000119 stringp += target_strlen(stringp) + 1;
120 }
bellard2f619692007-11-16 10:46:05 +0000121 /* FIXME - handle put_user() failures */
122 put_user_ual(0, envp);
pbrooke5fe0c52006-06-11 13:32:59 +0000123
124 return sp;
125}
126
Laurent Vivier03cfd8f2013-08-30 01:46:44 +0200127int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300128 struct target_pt_regs * regs, struct image_info *infop,
129 struct linux_binprm *bprm)
pbrooke5fe0c52006-06-11 13:32:59 +0000130{
pbrooke5fe0c52006-06-11 13:32:59 +0000131 int retval;
pbrooke5fe0c52006-06-11 13:32:59 +0000132
Laurent Vivier03cfd8f2013-08-30 01:46:44 +0200133 bprm->fd = fdexec;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300134 bprm->filename = (char *)filename;
135 bprm->argc = count(argv);
136 bprm->argv = argv;
137 bprm->envc = count(envp);
138 bprm->envp = envp;
pbrooke5fe0c52006-06-11 13:32:59 +0000139
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300140 retval = prepare_binprm(bprm);
pbrooke5fe0c52006-06-11 13:32:59 +0000141
pbrooke5fe0c52006-06-11 13:32:59 +0000142 if(retval>=0) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300143 if (bprm->buf[0] == 0x7f
144 && bprm->buf[1] == 'E'
145 && bprm->buf[2] == 'L'
146 && bprm->buf[3] == 'F') {
Will Newtonf0116c52014-01-09 09:10:50 +0000147 retval = load_elf_binary(bprm, infop);
pbrooke5fe0c52006-06-11 13:32:59 +0000148#if defined(TARGET_HAS_BFLT)
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300149 } else if (bprm->buf[0] == 'b'
150 && bprm->buf[1] == 'F'
151 && bprm->buf[2] == 'L'
152 && bprm->buf[3] == 'T') {
Will Newtonf0116c52014-01-09 09:10:50 +0000153 retval = load_flt_binary(bprm, infop);
pbrooke5fe0c52006-06-11 13:32:59 +0000154#endif
155 } else {
Peter Maydell885c1d12012-08-24 06:55:53 +0000156 return -ENOEXEC;
pbrooke5fe0c52006-06-11 13:32:59 +0000157 }
158 }
ths3b46e622007-09-17 08:09:54 +0000159
pbrooke5fe0c52006-06-11 13:32:59 +0000160 if(retval>=0) {
161 /* success. Initialize important registers */
162 do_init_thread(regs, infop);
163 return retval;
164 }
165
pbrooke5fe0c52006-06-11 13:32:59 +0000166 return(retval);
167}