1
2
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
16abi_long memcpy_to_target(abi_ulong dest, const void *src,
17 unsigned long len)
18{
19 void *host_ptr;
20
21 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
22 if (!host_ptr)
23 return -TARGET_EFAULT;
24 memcpy(host_ptr, src, len);
25 unlock_user(host_ptr, dest, 1);
26 return 0;
27}
28
29static 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;
44 int retval;
45
46 if(fstat(bprm->fd, &st) < 0) {
47 return(-errno);
48 }
49
50 mode = st.st_mode;
51 if(!S_ISREG(mode)) {
52 return(-EACCES);
53 }
54 if(!(mode & 0111)) {
55 return(-EACCES);
56 }
57
58 bprm->e_uid = geteuid();
59 bprm->e_gid = getegid();
60
61
62 if(mode & S_ISUID) {
63 bprm->e_uid = st.st_uid;
64 }
65
66
67
68
69
70
71
72 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
73 bprm->e_gid = st.st_gid;
74 }
75
76 retval = read(bprm->fd, bprm->buf, BPRM_BUF_SIZE);
77 if (retval < 0) {
78 perror("prepare_binprm");
79 exit(-1);
80 }
81 if (retval < BPRM_BUF_SIZE) {
82
83 memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval);
84 }
85 return retval;
86}
87
88
89abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
90 abi_ulong stringp, int push_ptr)
91{
92 TaskState *ts = (TaskState *)thread_cpu->opaque;
93 int n = sizeof(abi_ulong);
94 abi_ulong envp;
95 abi_ulong argv;
96
97 sp -= (envc + 1) * n;
98 envp = sp;
99 sp -= (argc + 1) * n;
100 argv = sp;
101 if (push_ptr) {
102
103 sp -= n;
104 put_user_ual(envp, sp);
105 sp -= n;
106 put_user_ual(argv, sp);
107 }
108 sp -= n;
109
110 put_user_ual(argc, sp);
111 ts->info->arg_start = stringp;
112 while (argc-- > 0) {
113
114 put_user_ual(stringp, argv);
115 argv += n;
116 stringp += target_strlen(stringp) + 1;
117 }
118 ts->info->arg_end = stringp;
119
120 put_user_ual(0, argv);
121 while (envc-- > 0) {
122
123 put_user_ual(stringp, envp);
124 envp += n;
125 stringp += target_strlen(stringp) + 1;
126 }
127
128 put_user_ual(0, envp);
129
130 return sp;
131}
132
133int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
134 struct target_pt_regs * regs, struct image_info *infop,
135 struct linux_binprm *bprm)
136{
137 int retval;
138
139 bprm->fd = fdexec;
140 bprm->filename = (char *)filename;
141 bprm->argc = count(argv);
142 bprm->argv = argv;
143 bprm->envc = count(envp);
144 bprm->envp = envp;
145
146 retval = prepare_binprm(bprm);
147
148 if(retval>=0) {
149 if (bprm->buf[0] == 0x7f
150 && bprm->buf[1] == 'E'
151 && bprm->buf[2] == 'L'
152 && bprm->buf[3] == 'F') {
153 retval = load_elf_binary(bprm, infop);
154#if defined(TARGET_HAS_BFLT)
155 } else if (bprm->buf[0] == 'b'
156 && bprm->buf[1] == 'F'
157 && bprm->buf[2] == 'L'
158 && bprm->buf[3] == 'T') {
159 retval = load_flt_binary(bprm, infop);
160#endif
161 } else {
162 return -ENOEXEC;
163 }
164 }
165
166 if(retval>=0) {
167
168 do_init_thread(regs, infop);
169 return retval;
170 }
171
172 return(retval);
173}
174