Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /****************************************************************************/ |
3 | /* | |
4 | * linux/fs/binfmt_flat.c | |
5 | * | |
6 | * Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com> | |
7 | * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com> | |
8 | * Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com> | |
9 | * Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com> | |
10 | * based heavily on: | |
11 | * | |
12 | * linux/fs/binfmt_aout.c: | |
13 | * Copyright (C) 1991, 1992, 1996 Linus Torvalds | |
14 | * linux/fs/binfmt_flat.c for 2.0 kernel | |
15 | * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com> | |
16 | * JAN/99 -- coded full program relocation (gerg@snapgear.com) | |
17 | */ | |
18 | ||
4adbb6ac NP |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
20 | ||
1da177e4 LT |
21 | #include <linux/kernel.h> |
22 | #include <linux/sched.h> | |
68db0cf1 | 23 | #include <linux/sched/task_stack.h> |
1da177e4 LT |
24 | #include <linux/mm.h> |
25 | #include <linux/mman.h> | |
1da177e4 LT |
26 | #include <linux/errno.h> |
27 | #include <linux/signal.h> | |
28 | #include <linux/string.h> | |
29 | #include <linux/fs.h> | |
30 | #include <linux/file.h> | |
1da177e4 LT |
31 | #include <linux/ptrace.h> |
32 | #include <linux/user.h> | |
33 | #include <linux/slab.h> | |
34 | #include <linux/binfmts.h> | |
35 | #include <linux/personality.h> | |
36 | #include <linux/init.h> | |
37 | #include <linux/flat.h> | |
13c3f50c | 38 | #include <linux/uaccess.h> |
472f95f3 | 39 | #include <linux/vmalloc.h> |
1da177e4 LT |
40 | |
41 | #include <asm/byteorder.h> | |
1da177e4 LT |
42 | #include <asm/unaligned.h> |
43 | #include <asm/cacheflush.h> | |
c3dc5bec | 44 | #include <asm/page.h> |
06d2bfed | 45 | #include <asm/flat.h> |
1da177e4 | 46 | |
02da2833 CH |
47 | #ifndef flat_get_relocate_addr |
48 | #define flat_get_relocate_addr(rel) (rel) | |
49 | #endif | |
50 | ||
1da177e4 LT |
51 | /****************************************************************************/ |
52 | ||
c3dc5bec | 53 | /* |
2e94de8a MF |
54 | * User data (data section and bss) needs to be aligned. |
55 | * We pick 0x20 here because it is the max value elf2flt has always | |
56 | * used in producing FLAT files, and because it seems to be large | |
57 | * enough to make all the gcc alignment related tests happy. | |
58 | */ | |
59 | #define FLAT_DATA_ALIGN (0x20) | |
60 | ||
61 | /* | |
62 | * User data (stack) also needs to be aligned. | |
63 | * Here we can be a bit looser than the data sections since this | |
64 | * needs to only meet arch ABI requirements. | |
c3dc5bec | 65 | */ |
2952095c | 66 | #define FLAT_STACK_ALIGN max_t(unsigned long, sizeof(void *), ARCH_SLAB_MINALIGN) |
c3dc5bec | 67 | |
1da177e4 LT |
68 | #define RELOC_FAILED 0xff00ff01 /* Relocation incorrect somewhere */ |
69 | #define UNLOADED_LIB 0x7ff000ff /* Placeholder for unused library */ | |
70 | ||
71 | struct lib_info { | |
72 | struct { | |
73 | unsigned long start_code; /* Start of text segment */ | |
74 | unsigned long start_data; /* Start of data segment */ | |
75 | unsigned long start_brk; /* End of data segment */ | |
76 | unsigned long text_len; /* Length of text segment */ | |
77 | unsigned long entry; /* Start address for this module */ | |
78 | unsigned long build_date; /* When this one was compiled */ | |
13c3f50c | 79 | bool loaded; /* Has this library been loaded? */ |
1da177e4 LT |
80 | } lib_list[MAX_SHARED_LIBS]; |
81 | }; | |
82 | ||
83 | #ifdef CONFIG_BINFMT_SHARED_FLAT | |
84 | static int load_flat_shared_library(int id, struct lib_info *p); | |
85 | #endif | |
86 | ||
71613c3b | 87 | static int load_flat_binary(struct linux_binprm *); |
f6151dfe | 88 | static int flat_core_dump(struct coredump_params *cprm); |
1da177e4 | 89 | |
1da177e4 LT |
90 | static struct linux_binfmt flat_format = { |
91 | .module = THIS_MODULE, | |
92 | .load_binary = load_flat_binary, | |
93 | .core_dump = flat_core_dump, | |
94 | .min_coredump = PAGE_SIZE | |
95 | }; | |
96 | ||
97 | /****************************************************************************/ | |
98 | /* | |
99 | * Routine writes a core dump image in the current directory. | |
100 | * Currently only a stub-function. | |
101 | */ | |
102 | ||
f6151dfe | 103 | static int flat_core_dump(struct coredump_params *cprm) |
1da177e4 | 104 | { |
4adbb6ac NP |
105 | pr_warn("Process %s:%d received signr %d and should have core dumped\n", |
106 | current->comm, current->pid, cprm->siginfo->si_signo); | |
13c3f50c | 107 | return 1; |
1da177e4 LT |
108 | } |
109 | ||
110 | /****************************************************************************/ | |
111 | /* | |
112 | * create_flat_tables() parses the env- and arg-strings in new user | |
113 | * memory and creates the pointer tables from them, and puts their | |
a97d157d | 114 | * addresses on the "stack", recording the new stack pointer value. |
1da177e4 LT |
115 | */ |
116 | ||
a97d157d | 117 | static int create_flat_tables(struct linux_binprm *bprm, unsigned long arg_start) |
1da177e4 | 118 | { |
a97d157d NP |
119 | char __user *p; |
120 | unsigned long __user *sp; | |
121 | long i, len; | |
1da177e4 | 122 | |
a97d157d NP |
123 | p = (char __user *)arg_start; |
124 | sp = (unsigned long __user *)current->mm->start_stack; | |
125 | ||
126 | sp -= bprm->envc + 1; | |
127 | sp -= bprm->argc + 1; | |
bdd15a28 CH |
128 | if (IS_ENABLED(CONFIG_BINFMT_FLAT_ARGVP_ENVP_ON_STACK)) |
129 | sp -= 2; /* argvp + envp */ | |
a97d157d NP |
130 | sp -= 1; /* &argc */ |
131 | ||
132 | current->mm->start_stack = (unsigned long)sp & -FLAT_STACK_ALIGN; | |
133 | sp = (unsigned long __user *)current->mm->start_stack; | |
134 | ||
135 | __put_user(bprm->argc, sp++); | |
bdd15a28 | 136 | if (IS_ENABLED(CONFIG_BINFMT_FLAT_ARGVP_ENVP_ON_STACK)) { |
a97d157d NP |
137 | unsigned long argv, envp; |
138 | argv = (unsigned long)(sp + 2); | |
139 | envp = (unsigned long)(sp + 2 + bprm->argc + 1); | |
140 | __put_user(argv, sp++); | |
141 | __put_user(envp, sp++); | |
142 | } | |
143 | ||
144 | current->mm->arg_start = (unsigned long)p; | |
145 | for (i = bprm->argc; i > 0; i--) { | |
146 | __put_user((unsigned long)p, sp++); | |
147 | len = strnlen_user(p, MAX_ARG_STRLEN); | |
148 | if (!len || len > MAX_ARG_STRLEN) | |
149 | return -EINVAL; | |
150 | p += len; | |
151 | } | |
152 | __put_user(0, sp++); | |
153 | current->mm->arg_end = (unsigned long)p; | |
154 | ||
155 | current->mm->env_start = (unsigned long) p; | |
156 | for (i = bprm->envc; i > 0; i--) { | |
157 | __put_user((unsigned long)p, sp++); | |
158 | len = strnlen_user(p, MAX_ARG_STRLEN); | |
159 | if (!len || len > MAX_ARG_STRLEN) | |
160 | return -EINVAL; | |
161 | p += len; | |
162 | } | |
163 | __put_user(0, sp++); | |
164 | current->mm->env_end = (unsigned long)p; | |
165 | ||
166 | return 0; | |
1da177e4 LT |
167 | } |
168 | ||
169 | /****************************************************************************/ | |
170 | ||
171 | #ifdef CONFIG_BINFMT_ZFLAT | |
172 | ||
173 | #include <linux/zlib.h> | |
174 | ||
175 | #define LBUFSIZE 4000 | |
176 | ||
177 | /* gzip flag byte */ | |
178 | #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */ | |
179 | #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ | |
180 | #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ | |
181 | #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ | |
182 | #define COMMENT 0x10 /* bit 4 set: file comment present */ | |
183 | #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */ | |
184 | #define RESERVED 0xC0 /* bit 6,7: reserved */ | |
185 | ||
bdd1d2d3 CH |
186 | static int decompress_exec(struct linux_binprm *bprm, loff_t fpos, char *dst, |
187 | long len, int fd) | |
1da177e4 LT |
188 | { |
189 | unsigned char *buf; | |
190 | z_stream strm; | |
1da177e4 LT |
191 | int ret, retval; |
192 | ||
bdd1d2d3 | 193 | pr_debug("decompress_exec(offset=%llx,buf=%p,len=%lx)\n", fpos, dst, len); |
1da177e4 LT |
194 | |
195 | memset(&strm, 0, sizeof(strm)); | |
196 | strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL); | |
9367bb73 | 197 | if (!strm.workspace) |
1da177e4 | 198 | return -ENOMEM; |
9367bb73 | 199 | |
1da177e4 | 200 | buf = kmalloc(LBUFSIZE, GFP_KERNEL); |
9367bb73 | 201 | if (!buf) { |
1da177e4 LT |
202 | retval = -ENOMEM; |
203 | goto out_free; | |
204 | } | |
205 | ||
206 | /* Read in first chunk of data and parse gzip header. */ | |
bdd1d2d3 | 207 | ret = kernel_read(bprm->file, buf, LBUFSIZE, &fpos); |
1da177e4 LT |
208 | |
209 | strm.next_in = buf; | |
210 | strm.avail_in = ret; | |
211 | strm.total_in = 0; | |
212 | ||
213 | retval = -ENOEXEC; | |
214 | ||
215 | /* Check minimum size -- gzip header */ | |
216 | if (ret < 10) { | |
4adbb6ac | 217 | pr_debug("file too small?\n"); |
1da177e4 LT |
218 | goto out_free_buf; |
219 | } | |
220 | ||
221 | /* Check gzip magic number */ | |
222 | if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) { | |
4adbb6ac | 223 | pr_debug("unknown compression magic?\n"); |
1da177e4 LT |
224 | goto out_free_buf; |
225 | } | |
226 | ||
227 | /* Check gzip method */ | |
228 | if (buf[2] != 8) { | |
4adbb6ac | 229 | pr_debug("unknown compression method?\n"); |
1da177e4 LT |
230 | goto out_free_buf; |
231 | } | |
232 | /* Check gzip flags */ | |
233 | if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) || | |
234 | (buf[3] & RESERVED)) { | |
4adbb6ac | 235 | pr_debug("unknown flags?\n"); |
1da177e4 LT |
236 | goto out_free_buf; |
237 | } | |
238 | ||
239 | ret = 10; | |
240 | if (buf[3] & EXTRA_FIELD) { | |
241 | ret += 2 + buf[10] + (buf[11] << 8); | |
13c3f50c | 242 | if (unlikely(ret >= LBUFSIZE)) { |
4adbb6ac | 243 | pr_debug("buffer overflow (EXTRA)?\n"); |
1da177e4 LT |
244 | goto out_free_buf; |
245 | } | |
246 | } | |
247 | if (buf[3] & ORIG_NAME) { | |
f4cfb18d | 248 | while (ret < LBUFSIZE && buf[ret++] != 0) |
1da177e4 | 249 | ; |
13c3f50c | 250 | if (unlikely(ret == LBUFSIZE)) { |
4adbb6ac | 251 | pr_debug("buffer overflow (ORIG_NAME)?\n"); |
1da177e4 LT |
252 | goto out_free_buf; |
253 | } | |
254 | } | |
255 | if (buf[3] & COMMENT) { | |
f4cfb18d | 256 | while (ret < LBUFSIZE && buf[ret++] != 0) |
1da177e4 | 257 | ; |
13c3f50c | 258 | if (unlikely(ret == LBUFSIZE)) { |
4adbb6ac | 259 | pr_debug("buffer overflow (COMMENT)?\n"); |
1da177e4 LT |
260 | goto out_free_buf; |
261 | } | |
262 | } | |
263 | ||
264 | strm.next_in += ret; | |
265 | strm.avail_in -= ret; | |
266 | ||
267 | strm.next_out = dst; | |
268 | strm.avail_out = len; | |
269 | strm.total_out = 0; | |
270 | ||
271 | if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) { | |
4adbb6ac | 272 | pr_debug("zlib init failed?\n"); |
1da177e4 LT |
273 | goto out_free_buf; |
274 | } | |
275 | ||
276 | while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) { | |
bdd1d2d3 | 277 | ret = kernel_read(bprm->file, buf, LBUFSIZE, &fpos); |
1da177e4 LT |
278 | if (ret <= 0) |
279 | break; | |
1da177e4 LT |
280 | len -= ret; |
281 | ||
282 | strm.next_in = buf; | |
283 | strm.avail_in = ret; | |
284 | strm.total_in = 0; | |
285 | } | |
286 | ||
287 | if (ret < 0) { | |
4adbb6ac | 288 | pr_debug("decompression failed (%d), %s\n", |
1da177e4 LT |
289 | ret, strm.msg); |
290 | goto out_zlib; | |
291 | } | |
292 | ||
293 | retval = 0; | |
294 | out_zlib: | |
295 | zlib_inflateEnd(&strm); | |
296 | out_free_buf: | |
297 | kfree(buf); | |
298 | out_free: | |
299 | kfree(strm.workspace); | |
1da177e4 LT |
300 | return retval; |
301 | } | |
302 | ||
303 | #endif /* CONFIG_BINFMT_ZFLAT */ | |
304 | ||
305 | /****************************************************************************/ | |
306 | ||
307 | static unsigned long | |
308 | calc_reloc(unsigned long r, struct lib_info *p, int curid, int internalp) | |
309 | { | |
310 | unsigned long addr; | |
311 | int id; | |
312 | unsigned long start_brk; | |
313 | unsigned long start_data; | |
314 | unsigned long text_len; | |
315 | unsigned long start_code; | |
316 | ||
317 | #ifdef CONFIG_BINFMT_SHARED_FLAT | |
318 | if (r == 0) | |
319 | id = curid; /* Relocs of 0 are always self referring */ | |
320 | else { | |
321 | id = (r >> 24) & 0xff; /* Find ID for this reloc */ | |
322 | r &= 0x00ffffff; /* Trim ID off here */ | |
323 | } | |
324 | if (id >= MAX_SHARED_LIBS) { | |
4adbb6ac | 325 | pr_err("reference 0x%lx to shared library %d", r, id); |
1da177e4 LT |
326 | goto failed; |
327 | } | |
328 | if (curid != id) { | |
329 | if (internalp) { | |
4adbb6ac NP |
330 | pr_err("reloc address 0x%lx not in same module " |
331 | "(%d != %d)", r, curid, id); | |
1da177e4 | 332 | goto failed; |
13c3f50c NP |
333 | } else if (!p->lib_list[id].loaded && |
334 | load_flat_shared_library(id, p) < 0) { | |
4adbb6ac | 335 | pr_err("failed to load library %d", id); |
1da177e4 LT |
336 | goto failed; |
337 | } | |
338 | /* Check versioning information (i.e. time stamps) */ | |
339 | if (p->lib_list[id].build_date && p->lib_list[curid].build_date && | |
340 | p->lib_list[curid].build_date < p->lib_list[id].build_date) { | |
4adbb6ac | 341 | pr_err("library %d is younger than %d", id, curid); |
1da177e4 LT |
342 | goto failed; |
343 | } | |
344 | } | |
345 | #else | |
346 | id = 0; | |
347 | #endif | |
348 | ||
349 | start_brk = p->lib_list[id].start_brk; | |
350 | start_data = p->lib_list[id].start_data; | |
351 | start_code = p->lib_list[id].start_code; | |
352 | text_len = p->lib_list[id].text_len; | |
353 | ||
9ee24b2a | 354 | if (r > start_brk - start_data + text_len) { |
4adbb6ac | 355 | pr_err("reloc outside program 0x%lx (0 - 0x%lx/0x%lx)", |
13c3f50c | 356 | r, start_brk-start_data+text_len, text_len); |
1da177e4 LT |
357 | goto failed; |
358 | } | |
359 | ||
360 | if (r < text_len) /* In text segment */ | |
361 | addr = r + start_code; | |
362 | else /* In data segment */ | |
363 | addr = r - text_len + start_data; | |
364 | ||
365 | /* Range checked already above so doing the range tests is redundant...*/ | |
13c3f50c | 366 | return addr; |
1da177e4 LT |
367 | |
368 | failed: | |
4adbb6ac | 369 | pr_cont(", killing %s!\n", current->comm); |
1da177e4 LT |
370 | send_sig(SIGSEGV, current, 0); |
371 | ||
372 | return RELOC_FAILED; | |
373 | } | |
374 | ||
375 | /****************************************************************************/ | |
376 | ||
cf9a566c | 377 | #ifdef CONFIG_BINFMT_FLAT_OLD |
34303435 | 378 | static void old_reloc(unsigned long rl) |
1da177e4 | 379 | { |
13c3f50c | 380 | static const char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" }; |
1da177e4 | 381 | flat_v2_reloc_t r; |
1b2ce442 NP |
382 | unsigned long __user *ptr; |
383 | unsigned long val; | |
13c3f50c | 384 | |
1da177e4 LT |
385 | r.value = rl; |
386 | #if defined(CONFIG_COLDFIRE) | |
1b2ce442 | 387 | ptr = (unsigned long __user *)(current->mm->start_code + r.reloc.offset); |
1da177e4 | 388 | #else |
1b2ce442 | 389 | ptr = (unsigned long __user *)(current->mm->start_data + r.reloc.offset); |
1da177e4 | 390 | #endif |
1b2ce442 | 391 | get_user(val, ptr); |
1da177e4 | 392 | |
4adbb6ac NP |
393 | pr_debug("Relocation of variable at DATASEG+%x " |
394 | "(address %p, currently %lx) into segment %s\n", | |
1b2ce442 | 395 | r.reloc.offset, ptr, val, segment[r.reloc.type]); |
13c3f50c | 396 | |
1da177e4 LT |
397 | switch (r.reloc.type) { |
398 | case OLD_FLAT_RELOC_TYPE_TEXT: | |
1b2ce442 | 399 | val += current->mm->start_code; |
1da177e4 LT |
400 | break; |
401 | case OLD_FLAT_RELOC_TYPE_DATA: | |
1b2ce442 | 402 | val += current->mm->start_data; |
1da177e4 LT |
403 | break; |
404 | case OLD_FLAT_RELOC_TYPE_BSS: | |
1b2ce442 | 405 | val += current->mm->end_data; |
1da177e4 LT |
406 | break; |
407 | default: | |
4adbb6ac | 408 | pr_err("Unknown relocation type=%x\n", r.reloc.type); |
1da177e4 LT |
409 | break; |
410 | } | |
1b2ce442 | 411 | put_user(val, ptr); |
1da177e4 | 412 | |
1b2ce442 | 413 | pr_debug("Relocation became %lx\n", val); |
13c3f50c | 414 | } |
cf9a566c | 415 | #endif /* CONFIG_BINFMT_FLAT_OLD */ |
1da177e4 LT |
416 | |
417 | /****************************************************************************/ | |
418 | ||
13c3f50c | 419 | static int load_flat_file(struct linux_binprm *bprm, |
1da177e4 LT |
420 | struct lib_info *libinfo, int id, unsigned long *extra_stack) |
421 | { | |
13c3f50c NP |
422 | struct flat_hdr *hdr; |
423 | unsigned long textpos, datapos, realdatastart; | |
468138d7 | 424 | u32 text_len, data_len, bss_len, stack_len, full_data, flags; |
13c3f50c | 425 | unsigned long len, memp, memp_size, extra, rlim; |
3b977718 CH |
426 | __be32 __user *reloc; |
427 | u32 __user *rp; | |
1da177e4 | 428 | struct inode *inode; |
13c3f50c | 429 | int i, rev, relocs; |
1da177e4 LT |
430 | loff_t fpos; |
431 | unsigned long start_code, end_code; | |
13c3f50c | 432 | ssize_t result; |
1ad3dcc0 | 433 | int ret; |
1da177e4 LT |
434 | |
435 | hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */ | |
496ad9aa | 436 | inode = file_inode(bprm->file); |
1da177e4 LT |
437 | |
438 | text_len = ntohl(hdr->data_start); | |
439 | data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start); | |
440 | bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end); | |
441 | stack_len = ntohl(hdr->stack_size); | |
442 | if (extra_stack) { | |
443 | stack_len += *extra_stack; | |
444 | *extra_stack = stack_len; | |
445 | } | |
446 | relocs = ntohl(hdr->reloc_count); | |
447 | flags = ntohl(hdr->flags); | |
448 | rev = ntohl(hdr->rev); | |
3dc20cb2 | 449 | full_data = data_len + relocs * sizeof(unsigned long); |
1da177e4 | 450 | |
845884d3 | 451 | if (strncmp(hdr->magic, "bFLT", 4)) { |
1da177e4 | 452 | /* |
e2a366dc MF |
453 | * Previously, here was a printk to tell people |
454 | * "BINFMT_FLAT: bad header magic". | |
455 | * But for the kernel which also use ELF FD-PIC format, this | |
456 | * error message is confusing. | |
1da177e4 | 457 | * because a lot of people do not manage to produce good |
1da177e4 | 458 | */ |
1ad3dcc0 LY |
459 | ret = -ENOEXEC; |
460 | goto err; | |
845884d3 GU |
461 | } |
462 | ||
463 | if (flags & FLAT_FLAG_KTRACE) | |
4adbb6ac | 464 | pr_info("Loading file: %s\n", bprm->filename); |
845884d3 | 465 | |
cf9a566c | 466 | #ifdef CONFIG_BINFMT_FLAT_OLD |
845884d3 | 467 | if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) { |
4adbb6ac NP |
468 | pr_err("bad flat file version 0x%x (supported 0x%lx and 0x%lx)\n", |
469 | rev, FLAT_VERSION, OLD_FLAT_VERSION); | |
1ad3dcc0 LY |
470 | ret = -ENOEXEC; |
471 | goto err; | |
1da177e4 | 472 | } |
13c3f50c | 473 | |
1da177e4 LT |
474 | /* Don't allow old format executables to use shared libraries */ |
475 | if (rev == OLD_FLAT_VERSION && id != 0) { | |
4adbb6ac NP |
476 | pr_err("shared libraries are not available before rev 0x%lx\n", |
477 | FLAT_VERSION); | |
1ad3dcc0 LY |
478 | ret = -ENOEXEC; |
479 | goto err; | |
1da177e4 LT |
480 | } |
481 | ||
cf9a566c CH |
482 | /* |
483 | * fix up the flags for the older format, there were all kinds | |
484 | * of endian hacks, this only works for the simple cases | |
485 | */ | |
486 | if (rev == OLD_FLAT_VERSION && | |
487 | (flags || IS_ENABLED(CONFIG_BINFMT_FLAT_OLD_ALWAYS_RAM))) | |
488 | flags = FLAT_FLAG_RAM; | |
489 | ||
490 | #else /* CONFIG_BINFMT_FLAT_OLD */ | |
491 | if (rev != FLAT_VERSION) { | |
492 | pr_err("bad flat file version 0x%x (supported 0x%lx)\n", | |
493 | rev, FLAT_VERSION); | |
494 | ret = -ENOEXEC; | |
495 | goto err; | |
496 | } | |
497 | #endif /* !CONFIG_BINFMT_FLAT_OLD */ | |
498 | ||
c995ee28 NP |
499 | /* |
500 | * Make sure the header params are sane. | |
501 | * 28 bits (256 MB) is way more than reasonable in this case. | |
502 | * If some top bits are set we have probable binary corruption. | |
503 | */ | |
504 | if ((text_len | data_len | bss_len | stack_len | full_data) >> 28) { | |
505 | pr_err("bad header\n"); | |
506 | ret = -ENOEXEC; | |
507 | goto err; | |
508 | } | |
509 | ||
1da177e4 LT |
510 | #ifndef CONFIG_BINFMT_ZFLAT |
511 | if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) { | |
4adbb6ac | 512 | pr_err("Support for ZFLAT executables is not enabled.\n"); |
1ad3dcc0 LY |
513 | ret = -ENOEXEC; |
514 | goto err; | |
1da177e4 LT |
515 | } |
516 | #endif | |
517 | ||
518 | /* | |
519 | * Check initial limits. This avoids letting people circumvent | |
520 | * size limits imposed on them by creating programs with large | |
521 | * arrays in the data or bss. | |
522 | */ | |
d554ed89 | 523 | rlim = rlimit(RLIMIT_DATA); |
1da177e4 LT |
524 | if (rlim >= RLIM_INFINITY) |
525 | rlim = ~0; | |
1ad3dcc0 LY |
526 | if (data_len + bss_len > rlim) { |
527 | ret = -ENOMEM; | |
528 | goto err; | |
529 | } | |
530 | ||
1da177e4 LT |
531 | /* Flush all traces of the currently running executable */ |
532 | if (id == 0) { | |
13c3f50c NP |
533 | ret = flush_old_exec(bprm); |
534 | if (ret) | |
df88912a | 535 | goto err; |
1da177e4 LT |
536 | |
537 | /* OK, This is the point of no return */ | |
fcc18e83 | 538 | set_personality(PER_LINUX_32BIT); |
221af7f8 | 539 | setup_new_exec(bprm); |
1da177e4 LT |
540 | } |
541 | ||
542 | /* | |
543 | * calculate the extra space we need to map in | |
544 | */ | |
0e647c04 AM |
545 | extra = max_t(unsigned long, bss_len + stack_len, |
546 | relocs * sizeof(unsigned long)); | |
1da177e4 LT |
547 | |
548 | /* | |
549 | * there are a couple of cases here, the separate code/data | |
550 | * case, and then the fully copied to RAM case which lumps | |
551 | * it all together. | |
552 | */ | |
015feacf | 553 | if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) { |
1da177e4 LT |
554 | /* |
555 | * this should give us a ROM ptr, but if it doesn't we don't | |
556 | * really care | |
557 | */ | |
4adbb6ac | 558 | pr_debug("ROM mapping of file (we hope)\n"); |
1da177e4 | 559 | |
6be5ceb0 | 560 | textpos = vm_mmap(bprm->file, 0, text_len, PROT_READ|PROT_EXEC, |
925d1c40 | 561 | MAP_PRIVATE|MAP_EXECUTABLE, 0); |
0b8c78f2 | 562 | if (!textpos || IS_ERR_VALUE(textpos)) { |
1ad3dcc0 | 563 | ret = textpos; |
13c3f50c NP |
564 | if (!textpos) |
565 | ret = -ENOMEM; | |
4adbb6ac | 566 | pr_err("Unable to mmap process text, errno %d\n", ret); |
df88912a | 567 | goto err; |
1da177e4 LT |
568 | } |
569 | ||
72613e5f | 570 | len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long); |
0f3e442a | 571 | len = PAGE_ALIGN(len); |
13c3f50c | 572 | realdatastart = vm_mmap(NULL, 0, len, |
72613e5f | 573 | PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0); |
1da177e4 | 574 | |
0b8c78f2 | 575 | if (realdatastart == 0 || IS_ERR_VALUE(realdatastart)) { |
13c3f50c | 576 | ret = realdatastart; |
1da177e4 | 577 | if (!realdatastart) |
13c3f50c | 578 | ret = -ENOMEM; |
4adbb6ac | 579 | pr_err("Unable to allocate RAM for process data, " |
13c3f50c | 580 | "errno %d\n", ret); |
7696e0c3 | 581 | vm_munmap(textpos, text_len); |
df88912a | 582 | goto err; |
1da177e4 | 583 | } |
c3dc5bec OS |
584 | datapos = ALIGN(realdatastart + |
585 | MAX_SHARED_LIBS * sizeof(unsigned long), | |
586 | FLAT_DATA_ALIGN); | |
1da177e4 | 587 | |
a8605423 | 588 | pr_debug("Allocated data+bss+stack (%u bytes): %lx\n", |
4adbb6ac | 589 | data_len + bss_len + stack_len, datapos); |
1da177e4 LT |
590 | |
591 | fpos = ntohl(hdr->data_start); | |
592 | #ifdef CONFIG_BINFMT_ZFLAT | |
593 | if (flags & FLAT_FLAG_GZDATA) { | |
13c3f50c | 594 | result = decompress_exec(bprm, fpos, (char *)datapos, |
3dc20cb2 | 595 | full_data, 0); |
1da177e4 LT |
596 | } else |
597 | #endif | |
598 | { | |
3dc20cb2 AV |
599 | result = read_code(bprm->file, datapos, fpos, |
600 | full_data); | |
1da177e4 | 601 | } |
0b8c78f2 | 602 | if (IS_ERR_VALUE(result)) { |
13c3f50c | 603 | ret = result; |
4adbb6ac | 604 | pr_err("Unable to read data+bss, errno %d\n", ret); |
7696e0c3 AV |
605 | vm_munmap(textpos, text_len); |
606 | vm_munmap(realdatastart, len); | |
df88912a | 607 | goto err; |
1da177e4 LT |
608 | } |
609 | ||
3b977718 | 610 | reloc = (__be32 __user *) |
13c3f50c | 611 | (datapos + (ntohl(hdr->reloc_start) - text_len)); |
1da177e4 | 612 | memp = realdatastart; |
0f3e442a | 613 | memp_size = len; |
1da177e4 LT |
614 | } else { |
615 | ||
468138d7 | 616 | len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32); |
0f3e442a | 617 | len = PAGE_ALIGN(len); |
13c3f50c | 618 | textpos = vm_mmap(NULL, 0, len, |
72613e5f | 619 | PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0); |
72613e5f | 620 | |
0b8c78f2 | 621 | if (!textpos || IS_ERR_VALUE(textpos)) { |
1ad3dcc0 | 622 | ret = textpos; |
13c3f50c NP |
623 | if (!textpos) |
624 | ret = -ENOMEM; | |
4adbb6ac | 625 | pr_err("Unable to allocate RAM for process text/data, " |
13c3f50c | 626 | "errno %d\n", ret); |
df88912a | 627 | goto err; |
1da177e4 LT |
628 | } |
629 | ||
630 | realdatastart = textpos + ntohl(hdr->data_start); | |
c3dc5bec | 631 | datapos = ALIGN(realdatastart + |
468138d7 | 632 | MAX_SHARED_LIBS * sizeof(u32), |
c3dc5bec OS |
633 | FLAT_DATA_ALIGN); |
634 | ||
3b977718 | 635 | reloc = (__be32 __user *) |
c3dc5bec | 636 | (datapos + (ntohl(hdr->reloc_start) - text_len)); |
1da177e4 | 637 | memp = textpos; |
0f3e442a | 638 | memp_size = len; |
1da177e4 LT |
639 | #ifdef CONFIG_BINFMT_ZFLAT |
640 | /* | |
641 | * load it all in and treat it like a RAM load from now on | |
642 | */ | |
643 | if (flags & FLAT_FLAG_GZIP) { | |
472f95f3 | 644 | #ifndef CONFIG_MMU |
13c3f50c NP |
645 | result = decompress_exec(bprm, sizeof(struct flat_hdr), |
646 | (((char *)textpos) + sizeof(struct flat_hdr)), | |
3dc20cb2 | 647 | (text_len + full_data |
13c3f50c | 648 | - sizeof(struct flat_hdr)), |
1da177e4 LT |
649 | 0); |
650 | memmove((void *) datapos, (void *) realdatastart, | |
3dc20cb2 | 651 | full_data); |
472f95f3 NP |
652 | #else |
653 | /* | |
654 | * This is used on MMU systems mainly for testing. | |
655 | * Let's use a kernel buffer to simplify things. | |
656 | */ | |
657 | long unz_text_len = text_len - sizeof(struct flat_hdr); | |
658 | long unz_len = unz_text_len + full_data; | |
659 | char *unz_data = vmalloc(unz_len); | |
660 | if (!unz_data) { | |
661 | result = -ENOMEM; | |
662 | } else { | |
663 | result = decompress_exec(bprm, sizeof(struct flat_hdr), | |
664 | unz_data, unz_len, 0); | |
665 | if (result == 0 && | |
666 | (copy_to_user((void __user *)textpos + sizeof(struct flat_hdr), | |
667 | unz_data, unz_text_len) || | |
668 | copy_to_user((void __user *)datapos, | |
669 | unz_data + unz_text_len, full_data))) | |
670 | result = -EFAULT; | |
671 | vfree(unz_data); | |
672 | } | |
673 | #endif | |
1da177e4 | 674 | } else if (flags & FLAT_FLAG_GZDATA) { |
3dc20cb2 | 675 | result = read_code(bprm->file, textpos, 0, text_len); |
472f95f3 NP |
676 | if (!IS_ERR_VALUE(result)) { |
677 | #ifndef CONFIG_MMU | |
1da177e4 | 678 | result = decompress_exec(bprm, text_len, (char *) datapos, |
3dc20cb2 | 679 | full_data, 0); |
472f95f3 NP |
680 | #else |
681 | char *unz_data = vmalloc(full_data); | |
682 | if (!unz_data) { | |
683 | result = -ENOMEM; | |
684 | } else { | |
685 | result = decompress_exec(bprm, text_len, | |
686 | unz_data, full_data, 0); | |
687 | if (result == 0 && | |
688 | copy_to_user((void __user *)datapos, | |
689 | unz_data, full_data)) | |
690 | result = -EFAULT; | |
691 | vfree(unz_data); | |
692 | } | |
1da177e4 | 693 | #endif |
472f95f3 NP |
694 | } |
695 | } else | |
696 | #endif /* CONFIG_BINFMT_ZFLAT */ | |
1da177e4 | 697 | { |
3dc20cb2 AV |
698 | result = read_code(bprm->file, textpos, 0, text_len); |
699 | if (!IS_ERR_VALUE(result)) | |
700 | result = read_code(bprm->file, datapos, | |
701 | ntohl(hdr->data_start), | |
702 | full_data); | |
1da177e4 | 703 | } |
0b8c78f2 | 704 | if (IS_ERR_VALUE(result)) { |
13c3f50c | 705 | ret = result; |
4adbb6ac | 706 | pr_err("Unable to read code+data+bss, errno %d\n", ret); |
7696e0c3 | 707 | vm_munmap(textpos, text_len + data_len + extra + |
468138d7 | 708 | MAX_SHARED_LIBS * sizeof(u32)); |
df88912a | 709 | goto err; |
1da177e4 LT |
710 | } |
711 | } | |
712 | ||
13c3f50c NP |
713 | start_code = textpos + sizeof(struct flat_hdr); |
714 | end_code = textpos + text_len; | |
715 | text_len -= sizeof(struct flat_hdr); /* the real code len */ | |
1da177e4 LT |
716 | |
717 | /* The main program needs a little extra setup in the task structure */ | |
1da177e4 LT |
718 | if (id == 0) { |
719 | current->mm->start_code = start_code; | |
720 | current->mm->end_code = end_code; | |
721 | current->mm->start_data = datapos; | |
722 | current->mm->end_data = datapos + data_len; | |
723 | /* | |
724 | * set up the brk stuff, uses any slack left in data/bss/stack | |
725 | * allocation. We put the brk after the bss (between the bss | |
726 | * and stack) like other platforms. | |
0f3e442a DH |
727 | * Userspace code relies on the stack pointer starting out at |
728 | * an address right at the end of a page. | |
1da177e4 LT |
729 | */ |
730 | current->mm->start_brk = datapos + data_len + bss_len; | |
731 | current->mm->brk = (current->mm->start_brk + 3) & ~3; | |
015feacf | 732 | #ifndef CONFIG_MMU |
0f3e442a | 733 | current->mm->context.end_brk = memp + memp_size - stack_len; |
015feacf | 734 | #endif |
1da177e4 LT |
735 | } |
736 | ||
13c3f50c | 737 | if (flags & FLAT_FLAG_KTRACE) { |
4adbb6ac NP |
738 | pr_info("Mapping is %lx, Entry point is %x, data_start is %x\n", |
739 | textpos, 0x00ffffff&ntohl(hdr->entry), ntohl(hdr->data_start)); | |
740 | pr_info("%s %s: TEXT=%lx-%lx DATA=%lx-%lx BSS=%lx-%lx\n", | |
741 | id ? "Lib" : "Load", bprm->filename, | |
742 | start_code, end_code, datapos, datapos + data_len, | |
743 | datapos + data_len, (datapos + data_len + bss_len + 3) & ~3); | |
13c3f50c | 744 | } |
1da177e4 LT |
745 | |
746 | /* Store the current module values into the global library structure */ | |
747 | libinfo->lib_list[id].start_code = start_code; | |
748 | libinfo->lib_list[id].start_data = datapos; | |
749 | libinfo->lib_list[id].start_brk = datapos + data_len + bss_len; | |
750 | libinfo->lib_list[id].text_len = text_len; | |
751 | libinfo->lib_list[id].loaded = 1; | |
752 | libinfo->lib_list[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos; | |
753 | libinfo->lib_list[id].build_date = ntohl(hdr->build_date); | |
13c3f50c | 754 | |
1da177e4 LT |
755 | /* |
756 | * We just load the allocations into some temporary memory to | |
757 | * help simplify all this mumbo jumbo | |
758 | * | |
759 | * We've got two different sections of relocation entries. | |
25985edc | 760 | * The first is the GOT which resides at the beginning of the data segment |
1da177e4 LT |
761 | * and is terminated with a -1. This one can be relocated in place. |
762 | * The second is the extra relocation entries tacked after the image's | |
763 | * data segment. These require a little more processing as the entry is | |
764 | * really an offset into the image which contains an offset into the | |
765 | * image. | |
766 | */ | |
767 | if (flags & FLAT_FLAG_GOTPIC) { | |
468138d7 AV |
768 | for (rp = (u32 __user *)datapos; ; rp++) { |
769 | u32 addr, rp_val; | |
6e572ffe NP |
770 | if (get_user(rp_val, rp)) |
771 | return -EFAULT; | |
772 | if (rp_val == 0xffffffff) | |
773 | break; | |
774 | if (rp_val) { | |
775 | addr = calc_reloc(rp_val, libinfo, id, 0); | |
1ad3dcc0 LY |
776 | if (addr == RELOC_FAILED) { |
777 | ret = -ENOEXEC; | |
df88912a | 778 | goto err; |
1ad3dcc0 | 779 | } |
6e572ffe NP |
780 | if (put_user(addr, rp)) |
781 | return -EFAULT; | |
1da177e4 LT |
782 | } |
783 | } | |
784 | } | |
785 | ||
786 | /* | |
787 | * Now run through the relocation entries. | |
788 | * We've got to be careful here as C++ produces relocatable zero | |
789 | * entries in the constructor and destructor tables which are then | |
790 | * tested for being not zero (which will always occur unless we're | |
791 | * based from address zero). This causes an endless loop as __start | |
792 | * is at zero. The solution used is to not relocate zero addresses. | |
793 | * This has the negative side effect of not allowing a global data | |
794 | * reference to be statically initialised to _stext (I've moved | |
795 | * __start to address 4 so that is okay). | |
796 | */ | |
797 | if (rev > OLD_FLAT_VERSION) { | |
468138d7 | 798 | u32 __maybe_unused persistent = 0; |
13c3f50c | 799 | for (i = 0; i < relocs; i++) { |
468138d7 | 800 | u32 addr, relval; |
3b977718 | 801 | __be32 tmp; |
1da177e4 | 802 | |
13c3f50c NP |
803 | /* |
804 | * Get the address of the pointer to be | |
805 | * relocated (of course, the address has to be | |
806 | * relocated first). | |
807 | */ | |
3b977718 | 808 | if (get_user(tmp, reloc + i)) |
6e572ffe | 809 | return -EFAULT; |
3b977718 | 810 | relval = ntohl(tmp); |
1da177e4 | 811 | addr = flat_get_relocate_addr(relval); |
468138d7 AV |
812 | rp = (u32 __user *)calc_reloc(addr, libinfo, id, 1); |
813 | if (rp == (u32 __user *)RELOC_FAILED) { | |
1ad3dcc0 | 814 | ret = -ENOEXEC; |
df88912a | 815 | goto err; |
1ad3dcc0 | 816 | } |
1da177e4 LT |
817 | |
818 | /* Get the pointer's value. */ | |
468138d7 AV |
819 | ret = flat_get_addr_from_rp(rp, relval, flags, |
820 | &addr, &persistent); | |
821 | if (unlikely(ret)) | |
822 | goto err; | |
823 | ||
1da177e4 LT |
824 | if (addr != 0) { |
825 | /* | |
826 | * Do the relocation. PIC relocs in the data section are | |
827 | * already in target order | |
828 | */ | |
3b977718 CH |
829 | if ((flags & FLAT_FLAG_GOTPIC) == 0) { |
830 | /* | |
831 | * Meh, the same value can have a different | |
832 | * byte order based on a flag.. | |
833 | */ | |
834 | addr = ntohl((__force __be32)addr); | |
835 | } | |
1da177e4 | 836 | addr = calc_reloc(addr, libinfo, id, 0); |
1ad3dcc0 LY |
837 | if (addr == RELOC_FAILED) { |
838 | ret = -ENOEXEC; | |
df88912a | 839 | goto err; |
1ad3dcc0 | 840 | } |
1da177e4 LT |
841 | |
842 | /* Write back the relocated pointer. */ | |
468138d7 AV |
843 | ret = flat_put_addr_at_rp(rp, addr, relval); |
844 | if (unlikely(ret)) | |
845 | goto err; | |
1da177e4 LT |
846 | } |
847 | } | |
cf9a566c | 848 | #ifdef CONFIG_BINFMT_FLAT_OLD |
1da177e4 | 849 | } else { |
1b2ce442 | 850 | for (i = 0; i < relocs; i++) { |
3b977718 | 851 | __be32 relval; |
1b2ce442 NP |
852 | if (get_user(relval, reloc + i)) |
853 | return -EFAULT; | |
3b977718 | 854 | old_reloc(ntohl(relval)); |
1b2ce442 | 855 | } |
cf9a566c | 856 | #endif /* CONFIG_BINFMT_FLAT_OLD */ |
1da177e4 | 857 | } |
13c3f50c | 858 | |
1da177e4 LT |
859 | flush_icache_range(start_code, end_code); |
860 | ||
861 | /* zero the BSS, BRK and stack areas */ | |
467aa146 NP |
862 | if (clear_user((void __user *)(datapos + data_len), bss_len + |
863 | (memp + memp_size - stack_len - /* end brk */ | |
864 | libinfo->lib_list[id].start_brk) + /* start brk */ | |
865 | stack_len)) | |
866 | return -EFAULT; | |
1da177e4 LT |
867 | |
868 | return 0; | |
1ad3dcc0 LY |
869 | err: |
870 | return ret; | |
1da177e4 LT |
871 | } |
872 | ||
873 | ||
874 | /****************************************************************************/ | |
875 | #ifdef CONFIG_BINFMT_SHARED_FLAT | |
876 | ||
877 | /* | |
878 | * Load a shared library into memory. The library gets its own data | |
879 | * segment (including bss) but not argv/argc/environ. | |
880 | */ | |
881 | ||
882 | static int load_flat_shared_library(int id, struct lib_info *libs) | |
883 | { | |
884 | struct linux_binprm bprm; | |
885 | int res; | |
886 | char buf[16]; | |
887 | ||
3a852d3b DH |
888 | memset(&bprm, 0, sizeof(bprm)); |
889 | ||
1da177e4 LT |
890 | /* Create the file name */ |
891 | sprintf(buf, "/lib/lib%d.so", id); | |
892 | ||
893 | /* Open the file up */ | |
894 | bprm.filename = buf; | |
895 | bprm.file = open_exec(bprm.filename); | |
896 | res = PTR_ERR(bprm.file); | |
897 | if (IS_ERR(bprm.file)) | |
898 | return res; | |
899 | ||
3440625d LT |
900 | bprm.cred = prepare_exec_creds(); |
901 | res = -ENOMEM; | |
902 | if (!bprm.cred) | |
903 | goto out; | |
904 | ||
3a852d3b DH |
905 | /* We don't really care about recalculating credentials at this point |
906 | * as we're past the point of no return and are dealing with shared | |
907 | * libraries. | |
908 | */ | |
ddb4a144 | 909 | bprm.called_set_creds = 1; |
3a852d3b | 910 | |
1da177e4 LT |
911 | res = prepare_binprm(&bprm); |
912 | ||
287980e4 | 913 | if (!res) |
1da177e4 | 914 | res = load_flat_file(&bprm, libs, id, NULL); |
3440625d LT |
915 | |
916 | abort_creds(bprm.cred); | |
917 | ||
918 | out: | |
919 | allow_write_access(bprm.file); | |
920 | fput(bprm.file); | |
921 | ||
13c3f50c | 922 | return res; |
1da177e4 LT |
923 | } |
924 | ||
925 | #endif /* CONFIG_BINFMT_SHARED_FLAT */ | |
926 | /****************************************************************************/ | |
927 | ||
928 | /* | |
929 | * These are the functions used to load flat style executables and shared | |
930 | * libraries. There is no binary dependent code anywhere else. | |
931 | */ | |
932 | ||
13c3f50c | 933 | static int load_flat_binary(struct linux_binprm *bprm) |
1da177e4 LT |
934 | { |
935 | struct lib_info libinfo; | |
71613c3b | 936 | struct pt_regs *regs = current_pt_regs(); |
015feacf | 937 | unsigned long stack_len = 0; |
1da177e4 | 938 | unsigned long start_addr; |
1da177e4 LT |
939 | int res; |
940 | int i, j; | |
941 | ||
942 | memset(&libinfo, 0, sizeof(libinfo)); | |
13c3f50c | 943 | |
1da177e4 LT |
944 | /* |
945 | * We have to add the size of our arguments to our stack size | |
946 | * otherwise it's too easy for users to create stack overflows | |
947 | * by passing in a huge argument list. And yes, we have to be | |
948 | * pedantic and include space for the argv/envp array as it may have | |
949 | * a lot of entries. | |
950 | */ | |
015feacf NP |
951 | #ifndef CONFIG_MMU |
952 | stack_len += PAGE_SIZE * MAX_ARG_PAGES - bprm->p; /* the strings */ | |
953 | #endif | |
a97d157d NP |
954 | stack_len += (bprm->argc + 1) * sizeof(char *); /* the argv array */ |
955 | stack_len += (bprm->envc + 1) * sizeof(char *); /* the envp array */ | |
956 | stack_len = ALIGN(stack_len, FLAT_STACK_ALIGN); | |
13c3f50c | 957 | |
1da177e4 | 958 | res = load_flat_file(bprm, &libinfo, 0, &stack_len); |
287980e4 | 959 | if (res < 0) |
1da177e4 | 960 | return res; |
13c3f50c | 961 | |
1da177e4 | 962 | /* Update data segment pointers for all libraries */ |
af521f92 NP |
963 | for (i = 0; i < MAX_SHARED_LIBS; i++) { |
964 | if (!libinfo.lib_list[i].loaded) | |
965 | continue; | |
966 | for (j = 0; j < MAX_SHARED_LIBS; j++) { | |
967 | unsigned long val = libinfo.lib_list[j].loaded ? | |
968 | libinfo.lib_list[j].start_data : UNLOADED_LIB; | |
969 | unsigned long __user *p = (unsigned long __user *) | |
970 | libinfo.lib_list[i].start_data; | |
971 | p -= j + 1; | |
972 | if (put_user(val, p)) | |
973 | return -EFAULT; | |
974 | } | |
975 | } | |
1da177e4 | 976 | |
a6f76f23 | 977 | install_exec_creds(bprm); |
1da177e4 LT |
978 | |
979 | set_binfmt(&flat_format); | |
980 | ||
015feacf NP |
981 | #ifdef CONFIG_MMU |
982 | res = setup_arg_pages(bprm, STACK_TOP, EXSTACK_DEFAULT); | |
983 | if (!res) | |
984 | res = create_flat_tables(bprm, bprm->p); | |
985 | #else | |
a97d157d NP |
986 | /* Stash our initial stack pointer into the mm structure */ |
987 | current->mm->start_stack = | |
988 | ((current->mm->context.end_brk + stack_len + 3) & ~3) - 4; | |
989 | pr_debug("sp=%lx\n", current->mm->start_stack); | |
1da177e4 | 990 | |
687fd773 | 991 | /* copy the arg pages onto the stack */ |
a97d157d NP |
992 | res = transfer_args_to_stack(bprm, ¤t->mm->start_stack); |
993 | if (!res) | |
994 | res = create_flat_tables(bprm, current->mm->start_stack); | |
015feacf | 995 | #endif |
687fd773 NP |
996 | if (res) |
997 | return res; | |
1da177e4 | 998 | |
1da177e4 LT |
999 | /* Fake some return addresses to ensure the call chain will |
1000 | * initialise library in order for us. We are required to call | |
1001 | * lib 1 first, then 2, ... and finally the main program (id 0). | |
1002 | */ | |
1003 | start_addr = libinfo.lib_list[0].entry; | |
1004 | ||
1005 | #ifdef CONFIG_BINFMT_SHARED_FLAT | |
13c3f50c | 1006 | for (i = MAX_SHARED_LIBS-1; i > 0; i--) { |
1da177e4 LT |
1007 | if (libinfo.lib_list[i].loaded) { |
1008 | /* Push previos first to call address */ | |
a97d157d NP |
1009 | unsigned long __user *sp; |
1010 | current->mm->start_stack -= sizeof(unsigned long); | |
1011 | sp = (unsigned long __user *)current->mm->start_stack; | |
1012 | __put_user(start_addr, sp); | |
1da177e4 LT |
1013 | start_addr = libinfo.lib_list[i].entry; |
1014 | } | |
1015 | } | |
1016 | #endif | |
13c3f50c | 1017 | |
74c27c43 TY |
1018 | #ifdef FLAT_PLAT_INIT |
1019 | FLAT_PLAT_INIT(regs); | |
1020 | #endif | |
13c3f50c | 1021 | |
b8383831 | 1022 | finalize_exec(bprm); |
4adbb6ac NP |
1023 | pr_debug("start_thread(regs=0x%p, entry=0x%lx, start_stack=0x%lx)\n", |
1024 | regs, start_addr, current->mm->start_stack); | |
1da177e4 LT |
1025 | start_thread(regs, start_addr, current->mm->start_stack); |
1026 | ||
1da177e4 LT |
1027 | return 0; |
1028 | } | |
1029 | ||
1030 | /****************************************************************************/ | |
1031 | ||
1032 | static int __init init_flat_binfmt(void) | |
1033 | { | |
8fc3dc5a AV |
1034 | register_binfmt(&flat_format); |
1035 | return 0; | |
1da177e4 | 1036 | } |
1da177e4 | 1037 | core_initcall(init_flat_binfmt); |
1da177e4 LT |
1038 | |
1039 | /****************************************************************************/ |