root / linux-user / linuxload.c @ 9ea37780
History | View | Annotate | Download (5 kB)
1 | 0458df24 | ths | /* Code for loading Linux executables. Mostly linux kernel code. */
|
---|---|---|---|
2 | e5fe0c52 | pbrook | |
3 | e5fe0c52 | pbrook | #include <sys/types.h> |
4 | e5fe0c52 | pbrook | #include <sys/stat.h> |
5 | e5fe0c52 | pbrook | #include <fcntl.h> |
6 | e5fe0c52 | pbrook | #include <errno.h> |
7 | e5fe0c52 | pbrook | #include <unistd.h> |
8 | e5fe0c52 | pbrook | #include <stdio.h> |
9 | e5fe0c52 | pbrook | #include <stdlib.h> |
10 | e5fe0c52 | pbrook | |
11 | e5fe0c52 | pbrook | #include "qemu.h" |
12 | e5fe0c52 | pbrook | |
13 | e5fe0c52 | pbrook | #define NGROUPS 32 |
14 | e5fe0c52 | pbrook | |
15 | e5fe0c52 | pbrook | /* ??? This should really be somewhere else. */
|
16 | 579a97f7 | bellard | abi_long memcpy_to_target(abi_ulong dest, const void *src, |
17 | 579a97f7 | bellard | unsigned long len) |
18 | e5fe0c52 | pbrook | { |
19 | e5fe0c52 | pbrook | void *host_ptr;
|
20 | e5fe0c52 | pbrook | |
21 | 579a97f7 | bellard | host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
|
22 | 579a97f7 | bellard | if (!host_ptr)
|
23 | 579a97f7 | bellard | return -TARGET_EFAULT;
|
24 | e5fe0c52 | pbrook | memcpy(host_ptr, src, len); |
25 | e5fe0c52 | pbrook | unlock_user(host_ptr, dest, 1);
|
26 | 579a97f7 | bellard | return 0; |
27 | e5fe0c52 | pbrook | } |
28 | e5fe0c52 | pbrook | |
29 | e5fe0c52 | pbrook | static int in_group_p(gid_t g) |
30 | e5fe0c52 | pbrook | { |
31 | e5fe0c52 | pbrook | /* return TRUE if we're in the specified group, FALSE otherwise */
|
32 | e5fe0c52 | pbrook | int ngroup;
|
33 | e5fe0c52 | pbrook | int i;
|
34 | e5fe0c52 | pbrook | gid_t grouplist[NGROUPS]; |
35 | e5fe0c52 | pbrook | |
36 | e5fe0c52 | pbrook | ngroup = getgroups(NGROUPS, grouplist); |
37 | e5fe0c52 | pbrook | for(i = 0; i < ngroup; i++) { |
38 | e5fe0c52 | pbrook | if(grouplist[i] == g) {
|
39 | e5fe0c52 | pbrook | return 1; |
40 | e5fe0c52 | pbrook | } |
41 | e5fe0c52 | pbrook | } |
42 | e5fe0c52 | pbrook | return 0; |
43 | e5fe0c52 | pbrook | } |
44 | e5fe0c52 | pbrook | |
45 | e5fe0c52 | pbrook | static int count(char ** vec) |
46 | e5fe0c52 | pbrook | { |
47 | e5fe0c52 | pbrook | int i;
|
48 | e5fe0c52 | pbrook | |
49 | e5fe0c52 | pbrook | for(i = 0; *vec; i++) { |
50 | e5fe0c52 | pbrook | vec++; |
51 | e5fe0c52 | pbrook | } |
52 | e5fe0c52 | pbrook | |
53 | e5fe0c52 | pbrook | return(i);
|
54 | e5fe0c52 | pbrook | } |
55 | e5fe0c52 | pbrook | |
56 | e5fe0c52 | pbrook | static int prepare_binprm(struct linux_binprm *bprm) |
57 | e5fe0c52 | pbrook | { |
58 | e5fe0c52 | pbrook | struct stat st;
|
59 | e5fe0c52 | pbrook | int mode;
|
60 | e5fe0c52 | pbrook | int retval, id_change;
|
61 | e5fe0c52 | pbrook | |
62 | e5fe0c52 | pbrook | if(fstat(bprm->fd, &st) < 0) { |
63 | e5fe0c52 | pbrook | return(-errno);
|
64 | e5fe0c52 | pbrook | } |
65 | e5fe0c52 | pbrook | |
66 | e5fe0c52 | pbrook | mode = st.st_mode; |
67 | e5fe0c52 | pbrook | if(!S_ISREG(mode)) { /* Must be regular file */ |
68 | e5fe0c52 | pbrook | return(-EACCES);
|
69 | e5fe0c52 | pbrook | } |
70 | e5fe0c52 | pbrook | if(!(mode & 0111)) { /* Must have at least one execute bit set */ |
71 | e5fe0c52 | pbrook | return(-EACCES);
|
72 | e5fe0c52 | pbrook | } |
73 | e5fe0c52 | pbrook | |
74 | e5fe0c52 | pbrook | bprm->e_uid = geteuid(); |
75 | e5fe0c52 | pbrook | bprm->e_gid = getegid(); |
76 | e5fe0c52 | pbrook | id_change = 0;
|
77 | e5fe0c52 | pbrook | |
78 | e5fe0c52 | pbrook | /* Set-uid? */
|
79 | e5fe0c52 | pbrook | if(mode & S_ISUID) {
|
80 | e5fe0c52 | pbrook | bprm->e_uid = st.st_uid; |
81 | e5fe0c52 | pbrook | if(bprm->e_uid != geteuid()) {
|
82 | e5fe0c52 | pbrook | id_change = 1;
|
83 | e5fe0c52 | pbrook | } |
84 | e5fe0c52 | pbrook | } |
85 | e5fe0c52 | pbrook | |
86 | e5fe0c52 | pbrook | /* Set-gid? */
|
87 | e5fe0c52 | pbrook | /*
|
88 | e5fe0c52 | pbrook | * If setgid is set but no group execute bit then this
|
89 | e5fe0c52 | pbrook | * is a candidate for mandatory locking, not a setgid
|
90 | e5fe0c52 | pbrook | * executable.
|
91 | e5fe0c52 | pbrook | */
|
92 | e5fe0c52 | pbrook | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
|
93 | e5fe0c52 | pbrook | bprm->e_gid = st.st_gid; |
94 | e5fe0c52 | pbrook | if (!in_group_p(bprm->e_gid)) {
|
95 | e5fe0c52 | pbrook | id_change = 1;
|
96 | e5fe0c52 | pbrook | } |
97 | e5fe0c52 | pbrook | } |
98 | e5fe0c52 | pbrook | |
99 | e5fe0c52 | pbrook | retval = lseek(bprm->fd, 0L, SEEK_SET);
|
100 | e5fe0c52 | pbrook | if(retval >= 0) { |
101 | e5fe0c52 | pbrook | retval = read(bprm->fd, bprm->buf, 128);
|
102 | e5fe0c52 | pbrook | } |
103 | e5fe0c52 | pbrook | if(retval < 0) { |
104 | e5fe0c52 | pbrook | perror("prepare_binprm");
|
105 | e5fe0c52 | pbrook | exit(-1);
|
106 | e5fe0c52 | pbrook | /* return(-errno); */
|
107 | e5fe0c52 | pbrook | } |
108 | e5fe0c52 | pbrook | else {
|
109 | e5fe0c52 | pbrook | return(retval);
|
110 | e5fe0c52 | pbrook | } |
111 | e5fe0c52 | pbrook | } |
112 | e5fe0c52 | pbrook | |
113 | e5fe0c52 | pbrook | /* Construct the envp and argv tables on the target stack. */
|
114 | 992f48a0 | blueswir1 | abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp, |
115 | 992f48a0 | blueswir1 | abi_ulong stringp, int push_ptr)
|
116 | e5fe0c52 | pbrook | { |
117 | edf8e2af | Mika Westerberg | TaskState *ts = (TaskState *)thread_env->opaque; |
118 | 992f48a0 | blueswir1 | int n = sizeof(abi_ulong); |
119 | 992f48a0 | blueswir1 | abi_ulong envp; |
120 | 992f48a0 | blueswir1 | abi_ulong argv; |
121 | e5fe0c52 | pbrook | |
122 | e5fe0c52 | pbrook | sp -= (envc + 1) * n;
|
123 | e5fe0c52 | pbrook | envp = sp; |
124 | e5fe0c52 | pbrook | sp -= (argc + 1) * n;
|
125 | e5fe0c52 | pbrook | argv = sp; |
126 | e5fe0c52 | pbrook | if (push_ptr) {
|
127 | 2f619698 | bellard | /* FIXME - handle put_user() failures */
|
128 | 2f619698 | bellard | sp -= n; |
129 | 2f619698 | bellard | put_user_ual(envp, sp); |
130 | 2f619698 | bellard | sp -= n; |
131 | 2f619698 | bellard | put_user_ual(argv, sp); |
132 | e5fe0c52 | pbrook | } |
133 | 2f619698 | bellard | sp -= n; |
134 | 2f619698 | bellard | /* FIXME - handle put_user() failures */
|
135 | 2f619698 | bellard | put_user_ual(argc, sp); |
136 | edf8e2af | Mika Westerberg | ts->info->arg_start = stringp; |
137 | e5fe0c52 | pbrook | while (argc-- > 0) { |
138 | 2f619698 | bellard | /* FIXME - handle put_user() failures */
|
139 | 2f619698 | bellard | put_user_ual(stringp, argv); |
140 | 2f619698 | bellard | argv += n; |
141 | e5fe0c52 | pbrook | stringp += target_strlen(stringp) + 1;
|
142 | e5fe0c52 | pbrook | } |
143 | edf8e2af | Mika Westerberg | ts->info->arg_end = stringp; |
144 | 2f619698 | bellard | /* FIXME - handle put_user() failures */
|
145 | 2f619698 | bellard | put_user_ual(0, argv);
|
146 | e5fe0c52 | pbrook | while (envc-- > 0) { |
147 | 2f619698 | bellard | /* FIXME - handle put_user() failures */
|
148 | 2f619698 | bellard | put_user_ual(stringp, envp); |
149 | 2f619698 | bellard | envp += n; |
150 | e5fe0c52 | pbrook | stringp += target_strlen(stringp) + 1;
|
151 | e5fe0c52 | pbrook | } |
152 | 2f619698 | bellard | /* FIXME - handle put_user() failures */
|
153 | 2f619698 | bellard | put_user_ual(0, envp);
|
154 | e5fe0c52 | pbrook | |
155 | e5fe0c52 | pbrook | return sp;
|
156 | e5fe0c52 | pbrook | } |
157 | e5fe0c52 | pbrook | |
158 | 5fafdf24 | ths | int loader_exec(const char * filename, char ** argv, char ** envp, |
159 | edf8e2af | Mika Westerberg | struct target_pt_regs * regs, struct image_info *infop, |
160 | edf8e2af | Mika Westerberg | struct linux_binprm *bprm)
|
161 | e5fe0c52 | pbrook | { |
162 | e5fe0c52 | pbrook | int retval;
|
163 | e5fe0c52 | pbrook | int i;
|
164 | e5fe0c52 | pbrook | |
165 | edf8e2af | Mika Westerberg | bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int); |
166 | e5fe0c52 | pbrook | for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */ |
167 | 7cba04f6 | Blue Swirl | bprm->page[i] = NULL;
|
168 | e5fe0c52 | pbrook | retval = open(filename, O_RDONLY); |
169 | e5fe0c52 | pbrook | if (retval < 0) |
170 | e5fe0c52 | pbrook | return retval;
|
171 | edf8e2af | Mika Westerberg | bprm->fd = retval; |
172 | edf8e2af | Mika Westerberg | bprm->filename = (char *)filename;
|
173 | edf8e2af | Mika Westerberg | bprm->argc = count(argv); |
174 | edf8e2af | Mika Westerberg | bprm->argv = argv; |
175 | edf8e2af | Mika Westerberg | bprm->envc = count(envp); |
176 | edf8e2af | Mika Westerberg | bprm->envp = envp; |
177 | e5fe0c52 | pbrook | |
178 | edf8e2af | Mika Westerberg | retval = prepare_binprm(bprm); |
179 | e5fe0c52 | pbrook | |
180 | 38d0662a | pbrook | infop->host_argv = argv; |
181 | 38d0662a | pbrook | |
182 | e5fe0c52 | pbrook | if(retval>=0) { |
183 | edf8e2af | Mika Westerberg | if (bprm->buf[0] == 0x7f |
184 | edf8e2af | Mika Westerberg | && bprm->buf[1] == 'E' |
185 | edf8e2af | Mika Westerberg | && bprm->buf[2] == 'L' |
186 | edf8e2af | Mika Westerberg | && bprm->buf[3] == 'F') { |
187 | cb33da57 | blueswir1 | #ifndef TARGET_HAS_ELFLOAD32
|
188 | edf8e2af | Mika Westerberg | retval = load_elf_binary(bprm,regs,infop); |
189 | cb33da57 | blueswir1 | #else
|
190 | edf8e2af | Mika Westerberg | retval = load_elf_binary_multi(bprm, regs, infop); |
191 | cb33da57 | blueswir1 | #endif
|
192 | e5fe0c52 | pbrook | #if defined(TARGET_HAS_BFLT)
|
193 | edf8e2af | Mika Westerberg | } else if (bprm->buf[0] == 'b' |
194 | edf8e2af | Mika Westerberg | && bprm->buf[1] == 'F' |
195 | edf8e2af | Mika Westerberg | && bprm->buf[2] == 'L' |
196 | edf8e2af | Mika Westerberg | && bprm->buf[3] == 'T') { |
197 | edf8e2af | Mika Westerberg | retval = load_flt_binary(bprm,regs,infop); |
198 | e5fe0c52 | pbrook | #endif
|
199 | e5fe0c52 | pbrook | } else {
|
200 | e5fe0c52 | pbrook | fprintf(stderr, "Unknown binary format\n");
|
201 | e5fe0c52 | pbrook | return -1; |
202 | e5fe0c52 | pbrook | } |
203 | e5fe0c52 | pbrook | } |
204 | 3b46e624 | ths | |
205 | e5fe0c52 | pbrook | if(retval>=0) { |
206 | e5fe0c52 | pbrook | /* success. Initialize important registers */
|
207 | e5fe0c52 | pbrook | do_init_thread(regs, infop); |
208 | e5fe0c52 | pbrook | return retval;
|
209 | e5fe0c52 | pbrook | } |
210 | e5fe0c52 | pbrook | |
211 | e5fe0c52 | pbrook | /* Something went wrong, return the inode and free the argument pages*/
|
212 | e5fe0c52 | pbrook | for (i=0 ; i<MAX_ARG_PAGES ; i++) { |
213 | edf8e2af | Mika Westerberg | free(bprm->page[i]); |
214 | e5fe0c52 | pbrook | } |
215 | e5fe0c52 | pbrook | return(retval);
|
216 | e5fe0c52 | pbrook | } |