-
Notifications
You must be signed in to change notification settings - Fork 934
/
exec_elf.c
318 lines (267 loc) · 7.6 KB
/
exec_elf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#define _SYSTEM 1
#include <minix/type.h>
#include <minix/const.h>
#include <minix/com.h>
#include <minix/syslib.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <libexec.h>
#include <string.h>
#include <machine/elf.h>
#include <machine/vmparam.h>
#include <machine/memory.h>
#include <minix/syslib.h>
/* For verbose logging */
#define ELF_DEBUG 0
/* Support only 32-bit ELF objects */
#define __ELF_WORD_SIZE 32
#define SECTOR_SIZE 512
static int check_header(Elf_Ehdr *hdr);
static int elf_sane(Elf_Ehdr *hdr)
{
if (check_header(hdr) != OK) {
return 0;
}
if((hdr->e_type != ET_EXEC) && (hdr->e_type != ET_DYN)) {
return 0;
}
if ((hdr->e_phoff > SECTOR_SIZE) ||
(hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > SECTOR_SIZE) {
#if ELF_DEBUG
printf("libexec: peculiar phoff\n");
#endif
return 0;
}
return 1;
}
static int elf_ph_sane(Elf_Phdr *phdr)
{
if (rounddown((uintptr_t)phdr, sizeof(Elf_Addr)) != (uintptr_t)phdr) {
return 0;
}
return 1;
}
static int elf_unpack(char *exec_hdr,
size_t hdr_len, Elf_Ehdr **hdr, Elf_Phdr **phdr)
{
if(hdr_len < sizeof(Elf_Ehdr))
return ENOEXEC;
*hdr = (Elf_Ehdr *) exec_hdr;
if(!elf_sane(*hdr)) {
return ENOEXEC;
}
*phdr = (Elf_Phdr *)(exec_hdr + (*hdr)->e_phoff);
if(!elf_ph_sane(*phdr)) {
return ENOEXEC;
}
#if 0
if((int)((*phdr) + (*hdr)->e_phnum) >= hdr_len) return ENOEXEC;
#endif
return OK;
}
#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
(ehdr).e_ident[EI_MAG3] == ELFMAG3)
static int check_header(Elf_Ehdr *hdr)
{
if (!IS_ELF(*hdr) ||
hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
hdr->e_ident[EI_VERSION] != EV_CURRENT ||
hdr->e_phentsize != sizeof(Elf_Phdr) ||
hdr->e_version != ELF_TARG_VER)
return ENOEXEC;
return OK;
}
/* Return >0 if there is an ELF interpreter (i.e. it is a dynamically linked
* executable) and we could extract it successfully.
* Return 0 if there isn't one.
* Return <0 on error.
*/
int elf_has_interpreter(char *exec_hdr, /* executable header */
size_t hdr_len, char *interp, size_t maxsz)
{
Elf_Ehdr *hdr = NULL;
Elf_Phdr *phdr = NULL;
int e, i;
if((e=elf_unpack(exec_hdr, hdr_len, &hdr, &phdr)) != OK) return 0;
for (i = 0; i < hdr->e_phnum; i++) {
switch (phdr[i].p_type) {
case PT_INTERP:
if(!interp) return 1;
if(phdr[i].p_filesz >= maxsz)
return -1;
if(phdr[i].p_offset + phdr[i].p_filesz >= hdr_len)
return -1;
memcpy(interp, exec_hdr + phdr[i].p_offset, phdr[i].p_filesz);
interp[phdr[i].p_filesz] = '\0';
return 1;
default:
continue;
}
}
return 0;
}
int libexec_load_elf(struct exec_info *execi)
{
Elf_Ehdr *hdr = NULL;
Elf_Phdr *phdr = NULL;
int e, i = 0;
int first = 1;
vir_bytes startv = 0, stacklow;
assert(execi != NULL);
assert(execi->hdr != NULL);
if((e=elf_unpack(execi->hdr, execi->hdr_len, &hdr, &phdr)) != OK) {
return e;
}
/* this function can load the dynamic linker, but that
* shouldn't require an interpreter itself.
*/
i = elf_has_interpreter(execi->hdr, execi->hdr_len, NULL, 0);
if(i > 0) {
return ENOEXEC;
}
execi->stack_size = roundup(execi->stack_size, PAGE_SIZE);
execi->stack_high = rounddown(execi->stack_high, PAGE_SIZE);
stacklow = execi->stack_high - execi->stack_size;
assert(execi->copymem);
assert(execi->clearmem);
assert(execi->allocmem_prealloc_cleared);
assert(execi->allocmem_prealloc_junk);
assert(execi->allocmem_ondemand);
for (i = 0; i < hdr->e_phnum; i++) {
Elf_Phdr *ph = &phdr[i];
off_t file_limit = ph->p_offset + ph->p_filesz;
/* sanity check binary before wiping out the target process */
if(execi->filesize < file_limit) {
return ENOEXEC;
}
}
if(execi->clearproc) execi->clearproc(execi);
for (i = 0; i < hdr->e_phnum; i++) {
vir_bytes seg_membytes, page_offset, p_vaddr, vaddr;
vir_bytes chunk, vfileend, vmemend;
off_t foffset, fbytes;
Elf_Phdr *ph = &phdr[i];
int try_mmap = 1;
u16_t clearend = 0;
int pagechunk;
int mmap_prot = PROT_READ;
#if ELF_DEBUG
printf("libexec: -------------------\n");
printf("libexec: phdr %x (%d)\n", (uint32_t)ph, i);
#endif
if(!(ph->p_flags & PF_R)) {
printf("libexec: warning: unreadable segment\n");
}
if(ph->p_flags & PF_W) {
mmap_prot |= PROT_WRITE;
#if ELF_DEBUG
printf("libexec: adding PROT_WRITE\n");
#endif
} else {
#if ELF_DEBUG
printf("libexec: not adding PROT_WRITE\n");
#endif
}
if (ph->p_type != PT_LOAD || ph->p_memsz == 0) continue;
if((ph->p_vaddr % PAGE_SIZE) != (ph->p_offset % PAGE_SIZE)) {
printf("libexec: unaligned ELF program?\n");
try_mmap = 0;
}
if(!execi->memmap) {
try_mmap = 0;
}
foffset = ph->p_offset;
fbytes = ph->p_filesz;
vaddr = p_vaddr = ph->p_vaddr + execi->load_offset;
seg_membytes = ph->p_memsz;
page_offset = vaddr % PAGE_SIZE;
vaddr -= page_offset;
foffset -= page_offset;
seg_membytes += page_offset;
fbytes += page_offset;
vfileend = p_vaddr + ph->p_filesz;
/* if there's usable memory after the file end, we have
* to tell VM to clear the memory part of the page when it's
* mapped in
*/
if((pagechunk = (vfileend % PAGE_SIZE))
&& ph->p_filesz < ph->p_memsz) {
clearend = PAGE_SIZE - pagechunk;
}
seg_membytes = roundup(seg_membytes, PAGE_SIZE);
fbytes = roundup(fbytes, PAGE_SIZE);
if(first || startv > vaddr) startv = vaddr;
first = 0;
if ((ph->p_flags & PF_X) != 0 && execi->text_size < seg_membytes)
execi->text_size = seg_membytes;
else
execi->data_size = seg_membytes;
if(try_mmap && execi->memmap(execi, vaddr, fbytes, foffset, clearend, mmap_prot) == OK) {
#if ELF_DEBUG
printf("libexec: mmap 0x%lx-0x%llx done, clearend 0x%x\n",
vaddr, vaddr+fbytes, clearend);
#endif
if(seg_membytes > fbytes) {
int rem_mem = seg_membytes - fbytes;;
vir_bytes remstart = vaddr + fbytes;
if(execi->allocmem_ondemand(execi,
remstart, rem_mem) != OK) {
printf("libexec: mmap extra mem failed\n");
return ENOMEM;
}
#if ELF_DEBUG
else printf("libexec: allocated 0x%lx-0x%lx\n",
remstart, remstart+rem_mem);
#endif
}
} else {
/* make us some memory */
if(execi->allocmem_prealloc_junk(execi, vaddr, seg_membytes) != OK) {
if(execi->clearproc) execi->clearproc(execi);
return ENOMEM;
}
#if ELF_DEBUG
printf("libexec: mmapped 0x%lx-0x%lx\n", vaddr, vaddr+seg_membytes);
#endif
/* Copy executable section into it */
if(execi->copymem(execi, ph->p_offset, p_vaddr, ph->p_filesz) != OK) {
if(execi->clearproc) execi->clearproc(execi);
return ENOMEM;
}
#if ELF_DEBUG
printf("libexec: copied 0x%lx-0x%lx\n", p_vaddr, p_vaddr+ph->p_filesz);
#endif
/* Clear remaining bits */
vmemend = vaddr + seg_membytes;
if((chunk = p_vaddr - vaddr) > 0) {
#if ELF_DEBUG
printf("libexec: start clearing 0x%lx-0x%lx\n", vaddr, vaddr+chunk);
#endif
execi->clearmem(execi, vaddr, chunk);
}
if((chunk = vmemend - vfileend) > 0) {
#if ELF_DEBUG
printf("libexec: end clearing 0x%lx-0x%lx\n", vfileend, vfileend+chunk);
#endif
execi->clearmem(execi, vfileend, chunk);
}
}
}
/* Make it a stack */
if(execi->allocmem_ondemand(execi, stacklow, execi->stack_size) != OK) {
if(execi->clearproc) execi->clearproc(execi);
return ENOMEM;
}
#if ELF_DEBUG
printf("libexec: stack mmapped 0x%lx-0x%lx\n", stacklow, stacklow+execi->stack_size);
#endif
/* record entry point and lowest load vaddr for caller */
execi->pc = hdr->e_entry + execi->load_offset;
execi->load_base = startv;
return OK;
}