blob: a181b58cedd507d0d5f35135c16c43f432eb4461 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * linux/fs/binfmt_elf.c
3 *
4 * These are the functions used to load ELF format executables as used
5 * on SVr4 machines. Information on the format may be found in the book
6 * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
7 * Tools".
8 *
9 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
10 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/fs.h>
15#include <linux/mm.h>
16#include <linux/mman.h>
17#include <linux/errno.h>
18#include <linux/signal.h>
19#include <linux/binfmts.h>
20#include <linux/string.h>
21#include <linux/file.h>
22#include <linux/slab.h>
23#include <linux/personality.h>
24#include <linux/elfcore.h>
25#include <linux/init.h>
26#include <linux/highuid.h>
27#include <linux/compiler.h>
28#include <linux/highmem.h>
29#include <linux/pagemap.h>
30#include <linux/security.h>
31#include <linux/random.h>
32#include <linux/elf.h>
33#include <linux/utsname.h>
34#include <linux/coredump.h>
35#include <asm/uaccess.h>
36#include <asm/param.h>
37#include <asm/page.h>
38#include <asm/exec.h>
39
40static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
41static int load_elf_library(struct file *);
42static unsigned long elf_map(struct file *, unsigned long, struct elf_phdr *,
43 int, int, unsigned long);
44
45/*
46 * If we don't support core dumping, then supply a NULL so we
47 * don't even try.
48 */
49#ifdef CONFIG_ELF_CORE
50static int elf_core_dump(struct coredump_params *cprm);
51#else
52#define elf_core_dump NULL
53#endif
54
55#if ELF_EXEC_PAGESIZE > PAGE_SIZE
56#define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
57#else
58#define ELF_MIN_ALIGN PAGE_SIZE
59#endif
60
61#ifndef ELF_CORE_EFLAGS
62#define ELF_CORE_EFLAGS 0
63#endif
64
65#define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
66#define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
67#define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
68
69static struct linux_binfmt elf_format = {
70 .module = THIS_MODULE,
71 .load_binary = load_elf_binary,
72 .load_shlib = load_elf_library,
73 .core_dump = elf_core_dump,
74 .min_coredump = ELF_EXEC_PAGESIZE,
75};
76
77#define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
78
79static int set_brk(unsigned long start, unsigned long end)
80{
81 start = ELF_PAGEALIGN(start);
82 end = ELF_PAGEALIGN(end);
83 if (end > start) {
84 unsigned long addr;
85 addr = vm_brk(start, end - start);
86 if (BAD_ADDR(addr))
87 return addr;
88 }
89 current->mm->start_brk = current->mm->brk = end;
90 return 0;
91}
92
93/* We need to explicitly zero any fractional pages
94 after the data section (i.e. bss). This would
95 contain the junk from the file that should not
96 be in memory
97 */
98static int padzero(unsigned long elf_bss)
99{
100 unsigned long nbyte;
101
102 nbyte = ELF_PAGEOFFSET(elf_bss);
103 if (nbyte) {
104 nbyte = ELF_MIN_ALIGN - nbyte;
105 if (clear_user((void __user *) elf_bss, nbyte))
106 return -EFAULT;
107 }
108 return 0;
109}
110
111/* Let's use some macros to make this stack manipulation a little clearer */
112#ifdef CONFIG_STACK_GROWSUP
113#define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
114#define STACK_ROUND(sp, items) \
115 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
116#define STACK_ALLOC(sp, len) ({ \
117 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
118 old_sp; })
119#else
120#define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
121#define STACK_ROUND(sp, items) \
122 (((unsigned long) (sp - items)) &~ 15UL)
123#define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
124#endif
125
126#ifndef ELF_BASE_PLATFORM
127/*
128 * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
129 * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
130 * will be copied to the user stack in the same manner as AT_PLATFORM.
131 */
132#define ELF_BASE_PLATFORM NULL
133#endif
134
135static int
136create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
137 unsigned long load_addr, unsigned long interp_load_addr)
138{
139 unsigned long p = bprm->p;
140 int argc = bprm->argc;
141 int envc = bprm->envc;
142 elf_addr_t __user *argv;
143 elf_addr_t __user *envp;
144 elf_addr_t __user *sp;
145 elf_addr_t __user *u_platform;
146 elf_addr_t __user *u_base_platform;
147 elf_addr_t __user *u_rand_bytes;
148 const char *k_platform = ELF_PLATFORM;
149 const char *k_base_platform = ELF_BASE_PLATFORM;
150 unsigned char k_rand_bytes[16];
151 int items;
152 elf_addr_t *elf_info;
153 int ei_index = 0;
154 const struct cred *cred = current_cred();
155 struct vm_area_struct *vma;
156
157 /*
158 * In some cases (e.g. Hyper-Threading), we want to avoid L1
159 * evictions by the processes running on the same package. One
160 * thing we can do is to shuffle the initial stack for them.
161 */
162
163 p = arch_align_stack(p);
164
165 /*
166 * If this architecture has a platform capability string, copy it
167 * to userspace. In some cases (Sparc), this info is impossible
168 * for userspace to get any other way, in others (i386) it is
169 * merely difficult.
170 */
171 u_platform = NULL;
172 if (k_platform) {
173 size_t len = strlen(k_platform) + 1;
174
175 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
176 if (__copy_to_user(u_platform, k_platform, len))
177 return -EFAULT;
178 }
179
180 /*
181 * If this architecture has a "base" platform capability
182 * string, copy it to userspace.
183 */
184 u_base_platform = NULL;
185 if (k_base_platform) {
186 size_t len = strlen(k_base_platform) + 1;
187
188 u_base_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
189 if (__copy_to_user(u_base_platform, k_base_platform, len))
190 return -EFAULT;
191 }
192
193 /*
194 * Generate 16 random bytes for userspace PRNG seeding.
195 */
196 get_random_bytes(k_rand_bytes, sizeof(k_rand_bytes));
197 u_rand_bytes = (elf_addr_t __user *)
198 STACK_ALLOC(p, sizeof(k_rand_bytes));
199 if (__copy_to_user(u_rand_bytes, k_rand_bytes, sizeof(k_rand_bytes)))
200 return -EFAULT;
201
202 /* Create the ELF interpreter info */
203 elf_info = (elf_addr_t *)current->mm->saved_auxv;
204 /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
205#define NEW_AUX_ENT(id, val) \
206 do { \
207 elf_info[ei_index++] = id; \
208 elf_info[ei_index++] = val; \
209 } while (0)
210
211#ifdef ARCH_DLINFO
212 /*
213 * ARCH_DLINFO must come first so PPC can do its special alignment of
214 * AUXV.
215 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
216 * ARCH_DLINFO changes
217 */
218 ARCH_DLINFO;
219#endif
220 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
221 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
222 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
223 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
224 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
225 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
226 NEW_AUX_ENT(AT_BASE, interp_load_addr);
227 NEW_AUX_ENT(AT_FLAGS, 0);
228 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
229 NEW_AUX_ENT(AT_UID, cred->uid);
230 NEW_AUX_ENT(AT_EUID, cred->euid);
231 NEW_AUX_ENT(AT_GID, cred->gid);
232 NEW_AUX_ENT(AT_EGID, cred->egid);
233 NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
234 NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes);
235 NEW_AUX_ENT(AT_EXECFN, bprm->exec);
236 if (k_platform) {
237 NEW_AUX_ENT(AT_PLATFORM,
238 (elf_addr_t)(unsigned long)u_platform);
239 }
240 if (k_base_platform) {
241 NEW_AUX_ENT(AT_BASE_PLATFORM,
242 (elf_addr_t)(unsigned long)u_base_platform);
243 }
244 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
245 NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
246 }
247#undef NEW_AUX_ENT
248 /* AT_NULL is zero; clear the rest too */
249 memset(&elf_info[ei_index], 0,
250 sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
251
252 /* And advance past the AT_NULL entry. */
253 ei_index += 2;
254
255 sp = STACK_ADD(p, ei_index);
256
257 items = (argc + 1) + (envc + 1) + 1;
258 bprm->p = STACK_ROUND(sp, items);
259
260 /* Point sp at the lowest address on the stack */
261#ifdef CONFIG_STACK_GROWSUP
262 sp = (elf_addr_t __user *)bprm->p - items - ei_index;
263 bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
264#else
265 sp = (elf_addr_t __user *)bprm->p;
266#endif
267
268
269 /*
270 * Grow the stack manually; some architectures have a limit on how
271 * far ahead a user-space access may be in order to grow the stack.
272 */
273 vma = find_extend_vma(current->mm, bprm->p);
274 if (!vma)
275 return -EFAULT;
276
277 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
278 if (__put_user(argc, sp++))
279 return -EFAULT;
280 argv = sp;
281 envp = argv + argc + 1;
282
283 /* Populate argv and envp */
284 p = current->mm->arg_end = current->mm->arg_start;
285 while (argc-- > 0) {
286 size_t len;
287 if (__put_user((elf_addr_t)p, argv++))
288 return -EFAULT;
289 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
290 if (!len || len > MAX_ARG_STRLEN)
291 return -EINVAL;
292 p += len;
293 }
294 if (__put_user(0, argv))
295 return -EFAULT;
296 current->mm->arg_end = current->mm->env_start = p;
297 while (envc-- > 0) {
298 size_t len;
299 if (__put_user((elf_addr_t)p, envp++))
300 return -EFAULT;
301 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
302 if (!len || len > MAX_ARG_STRLEN)
303 return -EINVAL;
304 p += len;
305 }
306 if (__put_user(0, envp))
307 return -EFAULT;
308 current->mm->env_end = p;
309
310 /* Put the elf_info on the stack in the right place. */
311 sp = (elf_addr_t __user *)envp + 1;
312 if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
313 return -EFAULT;
314 return 0;
315}
316
317static unsigned long elf_map(struct file *filep, unsigned long addr,
318 struct elf_phdr *eppnt, int prot, int type,
319 unsigned long total_size)
320{
321 unsigned long map_addr;
322 unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr);
323 unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr);
324 addr = ELF_PAGESTART(addr);
325 size = ELF_PAGEALIGN(size);
326
327 /* mmap() will return -EINVAL if given a zero size, but a
328 * segment with zero filesize is perfectly valid */
329 if (!size)
330 return addr;
331
332 down_write(&current->mm->mmap_sem);
333 /*
334 * total_size is the size of the ELF (interpreter) image.
335 * The _first_ mmap needs to know the full size, otherwise
336 * randomization might put this image into an overlapping
337 * position with the ELF binary image. (since size < total_size)
338 * So we first map the 'big' image - and unmap the remainder at
339 * the end. (which unmap is needed for ELF images with holes.)
340 */
341 if (total_size) {
342 total_size = ELF_PAGEALIGN(total_size);
343 map_addr = do_mmap(filep, addr, total_size, prot, type, off);
344 if (!BAD_ADDR(map_addr))
345 do_munmap(current->mm, map_addr+size, total_size-size);
346 } else
347 map_addr = do_mmap(filep, addr, size, prot, type, off);
348
349 up_write(&current->mm->mmap_sem);
350 return(map_addr);
351}
352
353static unsigned long total_mapping_size(struct elf_phdr *cmds, int nr)
354{
355 int i, first_idx = -1, last_idx = -1;
356
357 for (i = 0; i < nr; i++) {
358 if (cmds[i].p_type == PT_LOAD) {
359 last_idx = i;
360 if (first_idx == -1)
361 first_idx = i;
362 }
363 }
364 if (first_idx == -1)
365 return 0;
366
367 return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz -
368 ELF_PAGESTART(cmds[first_idx].p_vaddr);
369}
370
371
372/* This is much more generalized than the library routine read function,
373 so we keep this separate. Technically the library read function
374 is only provided so that we can read a.out libraries that have
375 an ELF header */
376
377static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
378 struct file *interpreter, unsigned long *interp_map_addr,
379 unsigned long no_base)
380{
381 struct elf_phdr *elf_phdata;
382 struct elf_phdr *eppnt;
383 unsigned long load_addr = 0;
384 int load_addr_set = 0;
385 unsigned long last_bss = 0, elf_bss = 0;
386 unsigned long error = ~0UL;
387 unsigned long total_size;
388 int retval, i, size;
389
390 /* First of all, some simple consistency checks */
391 if (interp_elf_ex->e_type != ET_EXEC &&
392 interp_elf_ex->e_type != ET_DYN)
393 goto out;
394 if (!elf_check_arch(interp_elf_ex))
395 goto out;
396 if (!interpreter->f_op || !interpreter->f_op->mmap)
397 goto out;
398
399 /*
400 * If the size of this structure has changed, then punt, since
401 * we will be doing the wrong thing.
402 */
403 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
404 goto out;
405 if (interp_elf_ex->e_phnum < 1 ||
406 interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
407 goto out;
408
409 /* Now read in all of the header information */
410 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
411 if (size > ELF_MIN_ALIGN)
412 goto out;
413 elf_phdata = kmalloc(size, GFP_KERNEL);
414 if (!elf_phdata)
415 goto out;
416
417 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
418 (char *)elf_phdata, size);
419 error = -EIO;
420 if (retval != size) {
421 if (retval < 0)
422 error = retval;
423 goto out_close;
424 }
425
426 total_size = total_mapping_size(elf_phdata, interp_elf_ex->e_phnum);
427 if (!total_size) {
428 error = -EINVAL;
429 goto out_close;
430 }
431
432 eppnt = elf_phdata;
433 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
434 if (eppnt->p_type == PT_LOAD) {
435 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
436 int elf_prot = 0;
437 unsigned long vaddr = 0;
438 unsigned long k, map_addr;
439
440 if (eppnt->p_flags & PF_R)
441 elf_prot = PROT_READ;
442 if (eppnt->p_flags & PF_W)
443 elf_prot |= PROT_WRITE;
444 if (eppnt->p_flags & PF_X)
445 elf_prot |= PROT_EXEC;
446 vaddr = eppnt->p_vaddr;
447 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
448 elf_type |= MAP_FIXED;
449 else if (no_base && interp_elf_ex->e_type == ET_DYN)
450 load_addr = -vaddr;
451
452 map_addr = elf_map(interpreter, load_addr + vaddr,
453 eppnt, elf_prot, elf_type, total_size);
454 total_size = 0;
455 if (!*interp_map_addr)
456 *interp_map_addr = map_addr;
457 error = map_addr;
458 if (BAD_ADDR(map_addr))
459 goto out_close;
460
461 if (!load_addr_set &&
462 interp_elf_ex->e_type == ET_DYN) {
463 load_addr = map_addr - ELF_PAGESTART(vaddr);
464 load_addr_set = 1;
465 }
466
467 /*
468 * Check to see if the section's size will overflow the
469 * allowed task size. Note that p_filesz must always be
470 * <= p_memsize so it's only necessary to check p_memsz.
471 */
472 k = load_addr + eppnt->p_vaddr;
473 if (BAD_ADDR(k) ||
474 eppnt->p_filesz > eppnt->p_memsz ||
475 eppnt->p_memsz > TASK_SIZE ||
476 TASK_SIZE - eppnt->p_memsz < k) {
477 error = -ENOMEM;
478 goto out_close;
479 }
480
481 /*
482 * Find the end of the file mapping for this phdr, and
483 * keep track of the largest address we see for this.
484 */
485 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
486 if (k > elf_bss)
487 elf_bss = k;
488
489 /*
490 * Do the same thing for the memory mapping - between
491 * elf_bss and last_bss is the bss section.
492 */
493 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
494 if (k > last_bss)
495 last_bss = k;
496 }
497 }
498
499 if (last_bss > elf_bss) {
500 /*
501 * Now fill out the bss section. First pad the last page up
502 * to the page boundary, and then perform a mmap to make sure
503 * that there are zero-mapped pages up to and including the
504 * last bss page.
505 */
506 if (padzero(elf_bss)) {
507 error = -EFAULT;
508 goto out_close;
509 }
510
511 /* What we have mapped so far */
512 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
513
514 /* Map the last of the bss segment */
515 error = vm_brk(elf_bss, last_bss - elf_bss);
516 if (BAD_ADDR(error))
517 goto out_close;
518 }
519
520 error = load_addr;
521
522out_close:
523 kfree(elf_phdata);
524out:
525 return error;
526}
527
528/*
529 * These are the functions used to load ELF style executables and shared
530 * libraries. There is no binary dependent code anywhere else.
531 */
532
533#define INTERPRETER_NONE 0
534#define INTERPRETER_ELF 2
535
536#ifndef STACK_RND_MASK
537#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
538#endif
539
540static unsigned long randomize_stack_top(unsigned long stack_top)
541{
542 unsigned long random_variable = 0;
543
544 if ((current->flags & PF_RANDOMIZE) &&
545 !(current->personality & ADDR_NO_RANDOMIZE)) {
546 random_variable = (unsigned long) get_random_int();
547 random_variable &= STACK_RND_MASK;
548 random_variable <<= PAGE_SHIFT;
549 }
550#ifdef CONFIG_STACK_GROWSUP
551 return PAGE_ALIGN(stack_top) + random_variable;
552#else
553 return PAGE_ALIGN(stack_top) - random_variable;
554#endif
555}
556
557static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
558{
559 struct file *interpreter = NULL; /* to shut gcc up */
560 unsigned long load_addr = 0, load_bias = 0;
561 int load_addr_set = 0;
562 char * elf_interpreter = NULL;
563 unsigned long error;
564 struct elf_phdr *elf_ppnt, *elf_phdata;
565 unsigned long elf_bss, elf_brk;
566 int retval, i;
567 unsigned int size;
568 unsigned long elf_entry;
569 unsigned long interp_load_addr = 0;
570 unsigned long start_code, end_code, start_data, end_data;
571 unsigned long reloc_func_desc __maybe_unused = 0;
572 int executable_stack = EXSTACK_DEFAULT;
573 unsigned long def_flags = 0;
574 struct {
575 struct elfhdr elf_ex;
576 struct elfhdr interp_elf_ex;
577 } *loc;
578
579 loc = kmalloc(sizeof(*loc), GFP_KERNEL);
580 if (!loc) {
581 retval = -ENOMEM;
582 goto out_ret;
583 }
584
585 /* Get the exec-header */
586 loc->elf_ex = *((struct elfhdr *)bprm->buf);
587
588 retval = -ENOEXEC;
589 /* First of all, some simple consistency checks */
590 if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
591 goto out;
592
593 if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
594 goto out;
595 if (!elf_check_arch(&loc->elf_ex))
596 goto out;
597 if (!bprm->file->f_op || !bprm->file->f_op->mmap)
598 goto out;
599
600 /* Now read in all of the header information */
601 if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
602 goto out;
603 if (loc->elf_ex.e_phnum < 1 ||
604 loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
605 goto out;
606 size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
607 retval = -ENOMEM;
608 elf_phdata = kmalloc(size, GFP_KERNEL);
609 if (!elf_phdata)
610 goto out;
611
612 retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
613 (char *)elf_phdata, size);
614 if (retval != size) {
615 if (retval >= 0)
616 retval = -EIO;
617 goto out_free_ph;
618 }
619
620 elf_ppnt = elf_phdata;
621 elf_bss = 0;
622 elf_brk = 0;
623
624 start_code = ~0UL;
625 end_code = 0;
626 start_data = 0;
627 end_data = 0;
628
629 for (i = 0; i < loc->elf_ex.e_phnum; i++) {
630 if (elf_ppnt->p_type == PT_INTERP) {
631 /* This is the program interpreter used for
632 * shared libraries - for now assume that this
633 * is an a.out format binary
634 */
635 retval = -ENOEXEC;
636 if (elf_ppnt->p_filesz > PATH_MAX ||
637 elf_ppnt->p_filesz < 2)
638 goto out_free_ph;
639
640 retval = -ENOMEM;
641 elf_interpreter = kmalloc(elf_ppnt->p_filesz,
642 GFP_KERNEL);
643 if (!elf_interpreter)
644 goto out_free_ph;
645
646 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
647 elf_interpreter,
648 elf_ppnt->p_filesz);
649 if (retval != elf_ppnt->p_filesz) {
650 if (retval >= 0)
651 retval = -EIO;
652 goto out_free_interp;
653 }
654 /* make sure path is NULL terminated */
655 retval = -ENOEXEC;
656 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
657 goto out_free_interp;
658
659 interpreter = open_exec(elf_interpreter);
660 retval = PTR_ERR(interpreter);
661 if (IS_ERR(interpreter))
662 goto out_free_interp;
663
664 /*
665 * If the binary is not readable then enforce
666 * mm->dumpable = 0 regardless of the interpreter's
667 * permissions.
668 */
669 would_dump(bprm, interpreter);
670
671 retval = kernel_read(interpreter, 0, bprm->buf,
672 BINPRM_BUF_SIZE);
673 if (retval != BINPRM_BUF_SIZE) {
674 if (retval >= 0)
675 retval = -EIO;
676 goto out_free_dentry;
677 }
678
679 /* Get the exec headers */
680 loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
681 break;
682 }
683 elf_ppnt++;
684 }
685
686 elf_ppnt = elf_phdata;
687 for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
688 if (elf_ppnt->p_type == PT_GNU_STACK) {
689 if (elf_ppnt->p_flags & PF_X)
690 executable_stack = EXSTACK_ENABLE_X;
691 else
692 executable_stack = EXSTACK_DISABLE_X;
693 break;
694 }
695
696 /* Some simple consistency checks for the interpreter */
697 if (elf_interpreter) {
698 retval = -ELIBBAD;
699 /* Not an ELF interpreter */
700 if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
701 goto out_free_dentry;
702 /* Verify the interpreter has a valid arch */
703 if (!elf_check_arch(&loc->interp_elf_ex))
704 goto out_free_dentry;
705 }
706
707 /* Flush all traces of the currently running executable */
708 retval = flush_old_exec(bprm);
709 if (retval)
710 goto out_free_dentry;
711
712 /* OK, This is the point of no return */
713 current->mm->def_flags = def_flags;
714
715 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
716 may depend on the personality. */
717 SET_PERSONALITY(loc->elf_ex);
718 if (elf_read_implies_exec(loc->elf_ex, executable_stack))
719 current->personality |= READ_IMPLIES_EXEC;
720
721 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
722 current->flags |= PF_RANDOMIZE;
723
724 setup_new_exec(bprm);
725
726 /* Do this so that we can load the interpreter, if need be. We will
727 change some of these later */
728 current->mm->free_area_cache = current->mm->mmap_base;
729 current->mm->cached_hole_size = 0;
730 retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
731 executable_stack);
732 if (retval < 0) {
733 send_sig(SIGKILL, current, 0);
734 goto out_free_dentry;
735 }
736
737 current->mm->start_stack = bprm->p;
738
739 /* Now we do a little grungy work by mmapping the ELF image into
740 the correct location in memory. */
741 for(i = 0, elf_ppnt = elf_phdata;
742 i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
743 int elf_prot = 0, elf_flags;
744 unsigned long k, vaddr;
745 unsigned long total_size = 0;
746
747 if (elf_ppnt->p_type != PT_LOAD)
748 continue;
749
750 if (unlikely (elf_brk > elf_bss)) {
751 unsigned long nbyte;
752
753 /* There was a PT_LOAD segment with p_memsz > p_filesz
754 before this one. Map anonymous pages, if needed,
755 and clear the area. */
756 retval = set_brk(elf_bss + load_bias,
757 elf_brk + load_bias);
758 if (retval) {
759 send_sig(SIGKILL, current, 0);
760 goto out_free_dentry;
761 }
762 nbyte = ELF_PAGEOFFSET(elf_bss);
763 if (nbyte) {
764 nbyte = ELF_MIN_ALIGN - nbyte;
765 if (nbyte > elf_brk - elf_bss)
766 nbyte = elf_brk - elf_bss;
767 if (clear_user((void __user *)elf_bss +
768 load_bias, nbyte)) {
769 /*
770 * This bss-zeroing can fail if the ELF
771 * file specifies odd protections. So
772 * we don't check the return value
773 */
774 }
775 }
776 }
777
778 if (elf_ppnt->p_flags & PF_R)
779 elf_prot |= PROT_READ;
780 if (elf_ppnt->p_flags & PF_W)
781 elf_prot |= PROT_WRITE;
782 if (elf_ppnt->p_flags & PF_X)
783 elf_prot |= PROT_EXEC;
784
785 elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
786
787 vaddr = elf_ppnt->p_vaddr;
788 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
789 elf_flags |= MAP_FIXED;
790 } else if (loc->elf_ex.e_type == ET_DYN) {
791 /* Try and get dynamic programs out of the way of the
792 * default mmap base, as well as whatever program they
793 * might try to exec. This is because the brk will
794 * follow the loader, and is not movable. */
795#ifdef CONFIG_ARCH_BINFMT_ELF_RANDOMIZE_PIE
796 /* Memory randomization might have been switched off
797 * in runtime via sysctl.
798 * If that is the case, retain the original non-zero
799 * load_bias value in order to establish proper
800 * non-randomized mappings.
801 */
802 if (current->flags & PF_RANDOMIZE)
803 load_bias = 0;
804 else
805 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
806#else
807 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
808#endif
809 total_size = total_mapping_size(elf_phdata,
810 loc->elf_ex.e_phnum);
811 if (!total_size) {
812 error = -EINVAL;
813 goto out_free_dentry;
814 }
815 }
816
817 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
818 elf_prot, elf_flags, total_size);
819 if (BAD_ADDR(error)) {
820 send_sig(SIGKILL, current, 0);
821 retval = IS_ERR((void *)error) ?
822 PTR_ERR((void*)error) : -EINVAL;
823 goto out_free_dentry;
824 }
825
826 if (!load_addr_set) {
827 load_addr_set = 1;
828 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
829 if (loc->elf_ex.e_type == ET_DYN) {
830 load_bias += error -
831 ELF_PAGESTART(load_bias + vaddr);
832 load_addr += load_bias;
833 reloc_func_desc = load_bias;
834 }
835 }
836 k = elf_ppnt->p_vaddr;
837 if (k < start_code)
838 start_code = k;
839 if (start_data < k)
840 start_data = k;
841
842 /*
843 * Check to see if the section's size will overflow the
844 * allowed task size. Note that p_filesz must always be
845 * <= p_memsz so it is only necessary to check p_memsz.
846 */
847 if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
848 elf_ppnt->p_memsz > TASK_SIZE ||
849 TASK_SIZE - elf_ppnt->p_memsz < k) {
850 /* set_brk can never work. Avoid overflows. */
851 send_sig(SIGKILL, current, 0);
852 retval = -EINVAL;
853 goto out_free_dentry;
854 }
855
856 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
857
858 if (k > elf_bss)
859 elf_bss = k;
860 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
861 end_code = k;
862 if (end_data < k)
863 end_data = k;
864 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
865 if (k > elf_brk)
866 elf_brk = k;
867 }
868
869 loc->elf_ex.e_entry += load_bias;
870 elf_bss += load_bias;
871 elf_brk += load_bias;
872 start_code += load_bias;
873 end_code += load_bias;
874 start_data += load_bias;
875 end_data += load_bias;
876
877 /* Calling set_brk effectively mmaps the pages that we need
878 * for the bss and break sections. We must do this before
879 * mapping in the interpreter, to make sure it doesn't wind
880 * up getting placed where the bss needs to go.
881 */
882 retval = set_brk(elf_bss, elf_brk);
883 if (retval) {
884 send_sig(SIGKILL, current, 0);
885 goto out_free_dentry;
886 }
887 if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
888 send_sig(SIGSEGV, current, 0);
889 retval = -EFAULT; /* Nobody gets to see this, but.. */
890 goto out_free_dentry;
891 }
892
893 if (elf_interpreter) {
894 unsigned long uninitialized_var(interp_map_addr);
895
896 elf_entry = load_elf_interp(&loc->interp_elf_ex,
897 interpreter,
898 &interp_map_addr,
899 load_bias);
900 if (!IS_ERR((void *)elf_entry)) {
901 /*
902 * load_elf_interp() returns relocation
903 * adjustment
904 */
905 interp_load_addr = elf_entry;
906 elf_entry += loc->interp_elf_ex.e_entry;
907 }
908 if (BAD_ADDR(elf_entry)) {
909 force_sig(SIGSEGV, current);
910 retval = IS_ERR((void *)elf_entry) ?
911 (int)elf_entry : -EINVAL;
912 goto out_free_dentry;
913 }
914 reloc_func_desc = interp_load_addr;
915
916 allow_write_access(interpreter);
917 fput(interpreter);
918 kfree(elf_interpreter);
919 } else {
920 elf_entry = loc->elf_ex.e_entry;
921 if (BAD_ADDR(elf_entry)) {
922 force_sig(SIGSEGV, current);
923 retval = -EINVAL;
924 goto out_free_dentry;
925 }
926 }
927
928 kfree(elf_phdata);
929
930 set_binfmt(&elf_format);
931
932#ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
933 retval = arch_setup_additional_pages(bprm, !!elf_interpreter);
934 if (retval < 0) {
935 send_sig(SIGKILL, current, 0);
936 goto out;
937 }
938#endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
939
940 install_exec_creds(bprm);
941 retval = create_elf_tables(bprm, &loc->elf_ex,
942 load_addr, interp_load_addr);
943 if (retval < 0) {
944 send_sig(SIGKILL, current, 0);
945 goto out;
946 }
947 /* N.B. passed_fileno might not be initialized? */
948 current->mm->end_code = end_code;
949 current->mm->start_code = start_code;
950 current->mm->start_data = start_data;
951 current->mm->end_data = end_data;
952 current->mm->start_stack = bprm->p;
953
954#ifdef arch_randomize_brk
955 if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) {
956 current->mm->brk = current->mm->start_brk =
957 arch_randomize_brk(current->mm);
958#ifdef CONFIG_COMPAT_BRK
959 current->brk_randomized = 1;
960#endif
961 }
962#endif
963
964 if (current->personality & MMAP_PAGE_ZERO) {
965 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
966 and some applications "depend" upon this behavior.
967 Since we do not have the power to recompile these, we
968 emulate the SVr4 behavior. Sigh. */
969 error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
970 MAP_FIXED | MAP_PRIVATE, 0);
971 }
972
973#ifdef ELF_PLAT_INIT
974 /*
975 * The ABI may specify that certain registers be set up in special
976 * ways (on i386 %edx is the address of a DT_FINI function, for
977 * example. In addition, it may also specify (eg, PowerPC64 ELF)
978 * that the e_entry field is the address of the function descriptor
979 * for the startup routine, rather than the address of the startup
980 * routine itself. This macro performs whatever initialization to
981 * the regs structure is required as well as any relocations to the
982 * function descriptor entries when executing dynamically links apps.
983 */
984 ELF_PLAT_INIT(regs, reloc_func_desc);
985#endif
986
987 start_thread(regs, elf_entry, bprm->p);
988 retval = 0;
989out:
990 kfree(loc);
991out_ret:
992 return retval;
993
994 /* error cleanup */
995out_free_dentry:
996 allow_write_access(interpreter);
997 if (interpreter)
998 fput(interpreter);
999out_free_interp:
1000 kfree(elf_interpreter);
1001out_free_ph:
1002 kfree(elf_phdata);
1003 goto out;
1004}
1005
1006/* This is really simpleminded and specialized - we are loading an
1007 a.out library that is given an ELF header. */
1008static int load_elf_library(struct file *file)
1009{
1010 struct elf_phdr *elf_phdata;
1011 struct elf_phdr *eppnt;
1012 unsigned long elf_bss, bss, len;
1013 int retval, error, i, j;
1014 struct elfhdr elf_ex;
1015
1016 error = -ENOEXEC;
1017 retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
1018 if (retval != sizeof(elf_ex))
1019 goto out;
1020
1021 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1022 goto out;
1023
1024 /* First of all, some simple consistency checks */
1025 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
1026 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
1027 goto out;
1028
1029 /* Now read in all of the header information */
1030
1031 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1032 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1033
1034 error = -ENOMEM;
1035 elf_phdata = kmalloc(j, GFP_KERNEL);
1036 if (!elf_phdata)
1037 goto out;
1038
1039 eppnt = elf_phdata;
1040 error = -ENOEXEC;
1041 retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1042 if (retval != j)
1043 goto out_free_ph;
1044
1045 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1046 if ((eppnt + i)->p_type == PT_LOAD)
1047 j++;
1048 if (j != 1)
1049 goto out_free_ph;
1050
1051 while (eppnt->p_type != PT_LOAD)
1052 eppnt++;
1053
1054 /* Now use mmap to map the library into memory. */
1055 error = vm_mmap(file,
1056 ELF_PAGESTART(eppnt->p_vaddr),
1057 (eppnt->p_filesz +
1058 ELF_PAGEOFFSET(eppnt->p_vaddr)),
1059 PROT_READ | PROT_WRITE | PROT_EXEC,
1060 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1061 (eppnt->p_offset -
1062 ELF_PAGEOFFSET(eppnt->p_vaddr)));
1063 if (error != ELF_PAGESTART(eppnt->p_vaddr))
1064 goto out_free_ph;
1065
1066 elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1067 if (padzero(elf_bss)) {
1068 error = -EFAULT;
1069 goto out_free_ph;
1070 }
1071
1072 len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
1073 ELF_MIN_ALIGN - 1);
1074 bss = eppnt->p_memsz + eppnt->p_vaddr;
1075 if (bss > len)
1076 vm_brk(len, bss - len);
1077 error = 0;
1078
1079out_free_ph:
1080 kfree(elf_phdata);
1081out:
1082 return error;
1083}
1084
1085#ifdef CONFIG_ELF_CORE
1086/*
1087 * ELF core dumper
1088 *
1089 * Modelled on fs/exec.c:aout_core_dump()
1090 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1091 */
1092
1093/*
1094 * The purpose of always_dump_vma() is to make sure that special kernel mappings
1095 * that are useful for post-mortem analysis are included in every core dump.
1096 * In that way we ensure that the core dump is fully interpretable later
1097 * without matching up the same kernel and hardware config to see what PC values
1098 * meant. These special mappings include - vDSO, vsyscall, and other
1099 * architecture specific mappings
1100 */
1101static bool always_dump_vma(struct vm_area_struct *vma)
1102{
1103 /* Any vsyscall mappings? */
1104 if (vma == get_gate_vma(vma->vm_mm))
1105 return true;
1106 /*
1107 * arch_vma_name() returns non-NULL for special architecture mappings,
1108 * such as vDSO sections.
1109 */
1110 if (arch_vma_name(vma))
1111 return true;
1112
1113 return false;
1114}
1115
1116/*
1117 * Decide what to dump of a segment, part, all or none.
1118 */
1119static unsigned long vma_dump_size(struct vm_area_struct *vma,
1120 unsigned long mm_flags)
1121{
1122#define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1123
1124 /* always dump the vdso and vsyscall sections */
1125 if (always_dump_vma(vma))
1126 goto whole;
1127
1128 if (vma->vm_flags & VM_NODUMP)
1129 return 0;
1130
1131 /* Hugetlb memory check */
1132 if (vma->vm_flags & VM_HUGETLB) {
1133 if ((vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_SHARED))
1134 goto whole;
1135 if (!(vma->vm_flags & VM_SHARED) && FILTER(HUGETLB_PRIVATE))
1136 goto whole;
1137 }
1138
1139 /* Do not dump I/O mapped devices or special mappings */
1140 if (vma->vm_flags & (VM_IO | VM_RESERVED))
1141 return 0;
1142
1143 /* By default, dump shared memory if mapped from an anonymous file. */
1144 if (vma->vm_flags & VM_SHARED) {
1145 if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0 ?
1146 FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1147 goto whole;
1148 return 0;
1149 }
1150
1151 /* Dump segments that have been written to. */
1152 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1153 goto whole;
1154 if (vma->vm_file == NULL)
1155 return 0;
1156
1157 if (FILTER(MAPPED_PRIVATE))
1158 goto whole;
1159
1160 /*
1161 * If this looks like the beginning of a DSO or executable mapping,
1162 * check for an ELF header. If we find one, dump the first page to
1163 * aid in determining what was mapped here.
1164 */
1165 if (FILTER(ELF_HEADERS) &&
1166 vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {
1167 u32 __user *header = (u32 __user *) vma->vm_start;
1168 u32 word;
1169 mm_segment_t fs = get_fs();
1170 /*
1171 * Doing it this way gets the constant folded by GCC.
1172 */
1173 union {
1174 u32 cmp;
1175 char elfmag[SELFMAG];
1176 } magic;
1177 BUILD_BUG_ON(SELFMAG != sizeof word);
1178 magic.elfmag[EI_MAG0] = ELFMAG0;
1179 magic.elfmag[EI_MAG1] = ELFMAG1;
1180 magic.elfmag[EI_MAG2] = ELFMAG2;
1181 magic.elfmag[EI_MAG3] = ELFMAG3;
1182 /*
1183 * Switch to the user "segment" for get_user(),
1184 * then put back what elf_core_dump() had in place.
1185 */
1186 set_fs(USER_DS);
1187 if (unlikely(get_user(word, header)))
1188 word = 0;
1189 set_fs(fs);
1190 if (word == magic.cmp)
1191 return PAGE_SIZE;
1192 }
1193
1194#undef FILTER
1195
1196 return 0;
1197
1198whole:
1199 return vma->vm_end - vma->vm_start;
1200}
1201
1202/* An ELF note in memory */
1203struct memelfnote
1204{
1205 const char *name;
1206 int type;
1207 unsigned int datasz;
1208 void *data;
1209};
1210
1211static int notesize(struct memelfnote *en)
1212{
1213 int sz;
1214
1215 sz = sizeof(struct elf_note);
1216 sz += roundup(strlen(en->name) + 1, 4);
1217 sz += roundup(en->datasz, 4);
1218
1219 return sz;
1220}
1221
1222#define DUMP_WRITE(addr, nr, foffset) \
1223 do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
1224
1225static int alignfile(struct file *file, loff_t *foffset)
1226{
1227 static const char buf[4] = { 0, };
1228 DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
1229 return 1;
1230}
1231
1232static int writenote(struct memelfnote *men, struct file *file,
1233 loff_t *foffset)
1234{
1235 struct elf_note en;
1236 en.n_namesz = strlen(men->name) + 1;
1237 en.n_descsz = men->datasz;
1238 en.n_type = men->type;
1239
1240 DUMP_WRITE(&en, sizeof(en), foffset);
1241 DUMP_WRITE(men->name, en.n_namesz, foffset);
1242 if (!alignfile(file, foffset))
1243 return 0;
1244 DUMP_WRITE(men->data, men->datasz, foffset);
1245 if (!alignfile(file, foffset))
1246 return 0;
1247
1248 return 1;
1249}
1250#undef DUMP_WRITE
1251
1252static void fill_elf_header(struct elfhdr *elf, int segs,
1253 u16 machine, u32 flags, u8 osabi)
1254{
1255 memset(elf, 0, sizeof(*elf));
1256
1257 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1258 elf->e_ident[EI_CLASS] = ELF_CLASS;
1259 elf->e_ident[EI_DATA] = ELF_DATA;
1260 elf->e_ident[EI_VERSION] = EV_CURRENT;
1261 elf->e_ident[EI_OSABI] = ELF_OSABI;
1262
1263 elf->e_type = ET_CORE;
1264 elf->e_machine = machine;
1265 elf->e_version = EV_CURRENT;
1266 elf->e_phoff = sizeof(struct elfhdr);
1267 elf->e_flags = flags;
1268 elf->e_ehsize = sizeof(struct elfhdr);
1269 elf->e_phentsize = sizeof(struct elf_phdr);
1270 elf->e_phnum = segs;
1271
1272 return;
1273}
1274
1275static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1276{
1277 phdr->p_type = PT_NOTE;
1278 phdr->p_offset = offset;
1279 phdr->p_vaddr = 0;
1280 phdr->p_paddr = 0;
1281 phdr->p_filesz = sz;
1282 phdr->p_memsz = 0;
1283 phdr->p_flags = 0;
1284 phdr->p_align = 0;
1285 return;
1286}
1287
1288static void fill_note(struct memelfnote *note, const char *name, int type,
1289 unsigned int sz, void *data)
1290{
1291 note->name = name;
1292 note->type = type;
1293 note->datasz = sz;
1294 note->data = data;
1295 return;
1296}
1297
1298/*
1299 * fill up all the fields in prstatus from the given task struct, except
1300 * registers which need to be filled up separately.
1301 */
1302static void fill_prstatus(struct elf_prstatus *prstatus,
1303 struct task_struct *p, long signr)
1304{
1305 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1306 prstatus->pr_sigpend = p->pending.signal.sig[0];
1307 prstatus->pr_sighold = p->blocked.sig[0];
1308 rcu_read_lock();
1309 prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1310 rcu_read_unlock();
1311 prstatus->pr_pid = task_pid_vnr(p);
1312 prstatus->pr_pgrp = task_pgrp_vnr(p);
1313 prstatus->pr_sid = task_session_vnr(p);
1314 if (thread_group_leader(p)) {
1315 struct task_cputime cputime;
1316
1317 /*
1318 * This is the record for the group leader. It shows the
1319 * group-wide total, not its individual thread total.
1320 */
1321 thread_group_cputime(p, &cputime);
1322 cputime_to_timeval(cputime.utime, &prstatus->pr_utime);
1323 cputime_to_timeval(cputime.stime, &prstatus->pr_stime);
1324 } else {
1325 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1326 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1327 }
1328 cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1329 cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1330}
1331
1332static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1333 struct mm_struct *mm)
1334{
1335 const struct cred *cred;
1336 unsigned int i, len;
1337
1338 /* first copy the parameters from user space */
1339 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1340
1341 len = mm->arg_end - mm->arg_start;
1342 if (len >= ELF_PRARGSZ)
1343 len = ELF_PRARGSZ-1;
1344 if (copy_from_user(&psinfo->pr_psargs,
1345 (const char __user *)mm->arg_start, len))
1346 return -EFAULT;
1347 for(i = 0; i < len; i++)
1348 if (psinfo->pr_psargs[i] == 0)
1349 psinfo->pr_psargs[i] = ' ';
1350 psinfo->pr_psargs[len] = 0;
1351
1352 rcu_read_lock();
1353 psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1354 rcu_read_unlock();
1355 psinfo->pr_pid = task_pid_vnr(p);
1356 psinfo->pr_pgrp = task_pgrp_vnr(p);
1357 psinfo->pr_sid = task_session_vnr(p);
1358
1359 i = p->state ? ffz(~p->state) + 1 : 0;
1360 psinfo->pr_state = i;
1361 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1362 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1363 psinfo->pr_nice = task_nice(p);
1364 psinfo->pr_flag = p->flags;
1365 rcu_read_lock();
1366 cred = __task_cred(p);
1367 SET_UID(psinfo->pr_uid, cred->uid);
1368 SET_GID(psinfo->pr_gid, cred->gid);
1369 rcu_read_unlock();
1370 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1371
1372 return 0;
1373}
1374
1375static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
1376{
1377 elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv;
1378 int i = 0;
1379 do
1380 i += 2;
1381 while (auxv[i - 2] != AT_NULL);
1382 fill_note(note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv);
1383}
1384
1385#ifdef CORE_DUMP_USE_REGSET
1386#include <linux/regset.h>
1387
1388struct elf_thread_core_info {
1389 struct elf_thread_core_info *next;
1390 struct task_struct *task;
1391 struct elf_prstatus prstatus;
1392 struct memelfnote notes[0];
1393};
1394
1395struct elf_note_info {
1396 struct elf_thread_core_info *thread;
1397 struct memelfnote psinfo;
1398 struct memelfnote auxv;
1399 size_t size;
1400 int thread_notes;
1401};
1402
1403/*
1404 * When a regset has a writeback hook, we call it on each thread before
1405 * dumping user memory. On register window machines, this makes sure the
1406 * user memory backing the register data is up to date before we read it.
1407 */
1408static void do_thread_regset_writeback(struct task_struct *task,
1409 const struct user_regset *regset)
1410{
1411 if (regset->writeback)
1412 regset->writeback(task, regset, 1);
1413}
1414
1415#ifndef PR_REG_SIZE
1416#define PR_REG_SIZE(S) sizeof(S)
1417#endif
1418
1419#ifndef PRSTATUS_SIZE
1420#define PRSTATUS_SIZE(S) sizeof(S)
1421#endif
1422
1423#ifndef PR_REG_PTR
1424#define PR_REG_PTR(S) (&((S)->pr_reg))
1425#endif
1426
1427#ifndef SET_PR_FPVALID
1428#define SET_PR_FPVALID(S, V) ((S)->pr_fpvalid = (V))
1429#endif
1430
1431static int fill_thread_core_info(struct elf_thread_core_info *t,
1432 const struct user_regset_view *view,
1433 long signr, size_t *total)
1434{
1435 unsigned int i;
1436
1437 /*
1438 * NT_PRSTATUS is the one special case, because the regset data
1439 * goes into the pr_reg field inside the note contents, rather
1440 * than being the whole note contents. We fill the reset in here.
1441 * We assume that regset 0 is NT_PRSTATUS.
1442 */
1443 fill_prstatus(&t->prstatus, t->task, signr);
1444 (void) view->regsets[0].get(t->task, &view->regsets[0],
1445 0, PR_REG_SIZE(t->prstatus.pr_reg),
1446 PR_REG_PTR(&t->prstatus), NULL);
1447
1448 fill_note(&t->notes[0], "CORE", NT_PRSTATUS,
1449 PRSTATUS_SIZE(t->prstatus), &t->prstatus);
1450 *total += notesize(&t->notes[0]);
1451
1452 do_thread_regset_writeback(t->task, &view->regsets[0]);
1453
1454 /*
1455 * Each other regset might generate a note too. For each regset
1456 * that has no core_note_type or is inactive, we leave t->notes[i]
1457 * all zero and we'll know to skip writing it later.
1458 */
1459 for (i = 1; i < view->n; ++i) {
1460 const struct user_regset *regset = &view->regsets[i];
1461 do_thread_regset_writeback(t->task, regset);
1462 if (regset->core_note_type && regset->get &&
1463 (!regset->active || regset->active(t->task, regset))) {
1464 int ret;
1465 size_t size = regset->n * regset->size;
1466 void *data = kmalloc(size, GFP_KERNEL);
1467 if (unlikely(!data))
1468 return 0;
1469 ret = regset->get(t->task, regset,
1470 0, size, data, NULL);
1471 if (unlikely(ret))
1472 kfree(data);
1473 else {
1474 if (regset->core_note_type != NT_PRFPREG)
1475 fill_note(&t->notes[i], "LINUX",
1476 regset->core_note_type,
1477 size, data);
1478 else {
1479 SET_PR_FPVALID(&t->prstatus, 1);
1480 fill_note(&t->notes[i], "CORE",
1481 NT_PRFPREG, size, data);
1482 }
1483 *total += notesize(&t->notes[i]);
1484 }
1485 }
1486 }
1487
1488 return 1;
1489}
1490
1491static int fill_note_info(struct elfhdr *elf, int phdrs,
1492 struct elf_note_info *info,
1493 long signr, struct pt_regs *regs)
1494{
1495 struct task_struct *dump_task = current;
1496 const struct user_regset_view *view = task_user_regset_view(dump_task);
1497 struct elf_thread_core_info *t;
1498 struct elf_prpsinfo *psinfo;
1499 struct core_thread *ct;
1500 unsigned int i;
1501
1502 info->size = 0;
1503 info->thread = NULL;
1504
1505 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1506 if (psinfo == NULL)
1507 return 0;
1508
1509 fill_note(&info->psinfo, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1510
1511 /*
1512 * Figure out how many notes we're going to need for each thread.
1513 */
1514 info->thread_notes = 0;
1515 for (i = 0; i < view->n; ++i)
1516 if (view->regsets[i].core_note_type != 0)
1517 ++info->thread_notes;
1518
1519 /*
1520 * Sanity check. We rely on regset 0 being in NT_PRSTATUS,
1521 * since it is our one special case.
1522 */
1523 if (unlikely(info->thread_notes == 0) ||
1524 unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) {
1525 WARN_ON(1);
1526 return 0;
1527 }
1528
1529 /*
1530 * Initialize the ELF file header.
1531 */
1532 fill_elf_header(elf, phdrs,
1533 view->e_machine, view->e_flags, view->ei_osabi);
1534
1535 /*
1536 * Allocate a structure for each thread.
1537 */
1538 for (ct = &dump_task->mm->core_state->dumper; ct; ct = ct->next) {
1539 t = kzalloc(offsetof(struct elf_thread_core_info,
1540 notes[info->thread_notes]),
1541 GFP_KERNEL);
1542 if (unlikely(!t))
1543 return 0;
1544
1545 t->task = ct->task;
1546 if (ct->task == dump_task || !info->thread) {
1547 t->next = info->thread;
1548 info->thread = t;
1549 } else {
1550 /*
1551 * Make sure to keep the original task at
1552 * the head of the list.
1553 */
1554 t->next = info->thread->next;
1555 info->thread->next = t;
1556 }
1557 }
1558
1559 /*
1560 * Now fill in each thread's information.
1561 */
1562 for (t = info->thread; t != NULL; t = t->next)
1563 if (!fill_thread_core_info(t, view, signr, &info->size))
1564 return 0;
1565
1566 /*
1567 * Fill in the two process-wide notes.
1568 */
1569 fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm);
1570 info->size += notesize(&info->psinfo);
1571
1572 fill_auxv_note(&info->auxv, current->mm);
1573 info->size += notesize(&info->auxv);
1574
1575 return 1;
1576}
1577
1578static size_t get_note_info_size(struct elf_note_info *info)
1579{
1580 return info->size;
1581}
1582
1583/*
1584 * Write all the notes for each thread. When writing the first thread, the
1585 * process-wide notes are interleaved after the first thread-specific note.
1586 */
1587static int write_note_info(struct elf_note_info *info,
1588 struct file *file, loff_t *foffset)
1589{
1590 bool first = 1;
1591 struct elf_thread_core_info *t = info->thread;
1592
1593 do {
1594 int i;
1595
1596 if (!writenote(&t->notes[0], file, foffset))
1597 return 0;
1598
1599 if (first && !writenote(&info->psinfo, file, foffset))
1600 return 0;
1601 if (first && !writenote(&info->auxv, file, foffset))
1602 return 0;
1603
1604 for (i = 1; i < info->thread_notes; ++i)
1605 if (t->notes[i].data &&
1606 !writenote(&t->notes[i], file, foffset))
1607 return 0;
1608
1609 first = 0;
1610 t = t->next;
1611 } while (t);
1612
1613 return 1;
1614}
1615
1616static void free_note_info(struct elf_note_info *info)
1617{
1618 struct elf_thread_core_info *threads = info->thread;
1619 while (threads) {
1620 unsigned int i;
1621 struct elf_thread_core_info *t = threads;
1622 threads = t->next;
1623 WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus);
1624 for (i = 1; i < info->thread_notes; ++i)
1625 kfree(t->notes[i].data);
1626 kfree(t);
1627 }
1628 kfree(info->psinfo.data);
1629}
1630
1631#else
1632
1633/* Here is the structure in which status of each thread is captured. */
1634struct elf_thread_status
1635{
1636 struct list_head list;
1637 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1638 elf_fpregset_t fpu; /* NT_PRFPREG */
1639 struct task_struct *thread;
1640#ifdef ELF_CORE_COPY_XFPREGS
1641 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
1642#endif
1643 struct memelfnote notes[3];
1644 int num_notes;
1645};
1646
1647/*
1648 * In order to add the specific thread information for the elf file format,
1649 * we need to keep a linked list of every threads pr_status and then create
1650 * a single section for them in the final core file.
1651 */
1652static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1653{
1654 int sz = 0;
1655 struct task_struct *p = t->thread;
1656 t->num_notes = 0;
1657
1658 fill_prstatus(&t->prstatus, p, signr);
1659 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1660
1661 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1662 &(t->prstatus));
1663 t->num_notes++;
1664 sz += notesize(&t->notes[0]);
1665
1666 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
1667 &t->fpu))) {
1668 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1669 &(t->fpu));
1670 t->num_notes++;
1671 sz += notesize(&t->notes[1]);
1672 }
1673
1674#ifdef ELF_CORE_COPY_XFPREGS
1675 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
1676 fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
1677 sizeof(t->xfpu), &t->xfpu);
1678 t->num_notes++;
1679 sz += notesize(&t->notes[2]);
1680 }
1681#endif
1682 return sz;
1683}
1684
1685struct elf_note_info {
1686 struct memelfnote *notes;
1687 struct elf_prstatus *prstatus; /* NT_PRSTATUS */
1688 struct elf_prpsinfo *psinfo; /* NT_PRPSINFO */
1689 struct list_head thread_list;
1690 elf_fpregset_t *fpu;
1691#ifdef ELF_CORE_COPY_XFPREGS
1692 elf_fpxregset_t *xfpu;
1693#endif
1694 int thread_status_size;
1695 int numnote;
1696};
1697
1698static int elf_note_info_init(struct elf_note_info *info)
1699{
1700 memset(info, 0, sizeof(*info));
1701 INIT_LIST_HEAD(&info->thread_list);
1702
1703 /* Allocate space for six ELF notes */
1704 info->notes = kmalloc(6 * sizeof(struct memelfnote), GFP_KERNEL);
1705 if (!info->notes)
1706 return 0;
1707 info->psinfo = kmalloc(sizeof(*info->psinfo), GFP_KERNEL);
1708 if (!info->psinfo)
1709 return 0;
1710 info->prstatus = kmalloc(sizeof(*info->prstatus), GFP_KERNEL);
1711 if (!info->prstatus)
1712 return 0;
1713 info->fpu = kmalloc(sizeof(*info->fpu), GFP_KERNEL);
1714 if (!info->fpu)
1715 return 0;
1716#ifdef ELF_CORE_COPY_XFPREGS
1717 info->xfpu = kmalloc(sizeof(*info->xfpu), GFP_KERNEL);
1718 if (!info->xfpu)
1719 return 0;
1720#endif
1721 return 1;
1722}
1723
1724static int fill_note_info(struct elfhdr *elf, int phdrs,
1725 struct elf_note_info *info,
1726 long signr, struct pt_regs *regs)
1727{
1728 struct list_head *t;
1729
1730 if (!elf_note_info_init(info))
1731 return 0;
1732
1733 if (signr) {
1734 struct core_thread *ct;
1735 struct elf_thread_status *ets;
1736
1737 for (ct = current->mm->core_state->dumper.next;
1738 ct; ct = ct->next) {
1739 ets = kzalloc(sizeof(*ets), GFP_KERNEL);
1740 if (!ets)
1741 return 0;
1742
1743 ets->thread = ct->task;
1744 list_add(&ets->list, &info->thread_list);
1745 }
1746
1747 list_for_each(t, &info->thread_list) {
1748 int sz;
1749
1750 ets = list_entry(t, struct elf_thread_status, list);
1751 sz = elf_dump_thread_status(signr, ets);
1752 info->thread_status_size += sz;
1753 }
1754 }
1755 /* now collect the dump for the current */
1756 memset(info->prstatus, 0, sizeof(*info->prstatus));
1757 fill_prstatus(info->prstatus, current, signr);
1758 elf_core_copy_regs(&info->prstatus->pr_reg, regs);
1759
1760 /* Set up header */
1761 fill_elf_header(elf, phdrs, ELF_ARCH, ELF_CORE_EFLAGS, ELF_OSABI);
1762
1763 /*
1764 * Set up the notes in similar form to SVR4 core dumps made
1765 * with info from their /proc.
1766 */
1767
1768 fill_note(info->notes + 0, "CORE", NT_PRSTATUS,
1769 sizeof(*info->prstatus), info->prstatus);
1770 fill_psinfo(info->psinfo, current->group_leader, current->mm);
1771 fill_note(info->notes + 1, "CORE", NT_PRPSINFO,
1772 sizeof(*info->psinfo), info->psinfo);
1773
1774 info->numnote = 2;
1775
1776 fill_auxv_note(&info->notes[info->numnote++], current->mm);
1777
1778 /* Try to dump the FPU. */
1779 info->prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs,
1780 info->fpu);
1781 if (info->prstatus->pr_fpvalid)
1782 fill_note(info->notes + info->numnote++,
1783 "CORE", NT_PRFPREG, sizeof(*info->fpu), info->fpu);
1784#ifdef ELF_CORE_COPY_XFPREGS
1785 if (elf_core_copy_task_xfpregs(current, info->xfpu))
1786 fill_note(info->notes + info->numnote++,
1787 "LINUX", ELF_CORE_XFPREG_TYPE,
1788 sizeof(*info->xfpu), info->xfpu);
1789#endif
1790
1791 return 1;
1792}
1793
1794static size_t get_note_info_size(struct elf_note_info *info)
1795{
1796 int sz = 0;
1797 int i;
1798
1799 for (i = 0; i < info->numnote; i++)
1800 sz += notesize(info->notes + i);
1801
1802 sz += info->thread_status_size;
1803
1804 return sz;
1805}
1806
1807static int write_note_info(struct elf_note_info *info,
1808 struct file *file, loff_t *foffset)
1809{
1810 int i;
1811 struct list_head *t;
1812
1813 for (i = 0; i < info->numnote; i++)
1814 if (!writenote(info->notes + i, file, foffset))
1815 return 0;
1816
1817 /* write out the thread status notes section */
1818 list_for_each(t, &info->thread_list) {
1819 struct elf_thread_status *tmp =
1820 list_entry(t, struct elf_thread_status, list);
1821
1822 for (i = 0; i < tmp->num_notes; i++)
1823 if (!writenote(&tmp->notes[i], file, foffset))
1824 return 0;
1825 }
1826
1827 return 1;
1828}
1829
1830static void free_note_info(struct elf_note_info *info)
1831{
1832 while (!list_empty(&info->thread_list)) {
1833 struct list_head *tmp = info->thread_list.next;
1834 list_del(tmp);
1835 kfree(list_entry(tmp, struct elf_thread_status, list));
1836 }
1837
1838 kfree(info->prstatus);
1839 kfree(info->psinfo);
1840 kfree(info->notes);
1841 kfree(info->fpu);
1842#ifdef ELF_CORE_COPY_XFPREGS
1843 kfree(info->xfpu);
1844#endif
1845}
1846
1847#endif
1848
1849static struct vm_area_struct *first_vma(struct task_struct *tsk,
1850 struct vm_area_struct *gate_vma)
1851{
1852 struct vm_area_struct *ret = tsk->mm->mmap;
1853
1854 if (ret)
1855 return ret;
1856 return gate_vma;
1857}
1858/*
1859 * Helper function for iterating across a vma list. It ensures that the caller
1860 * will visit `gate_vma' prior to terminating the search.
1861 */
1862static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1863 struct vm_area_struct *gate_vma)
1864{
1865 struct vm_area_struct *ret;
1866
1867 ret = this_vma->vm_next;
1868 if (ret)
1869 return ret;
1870 if (this_vma == gate_vma)
1871 return NULL;
1872 return gate_vma;
1873}
1874
1875static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
1876 elf_addr_t e_shoff, int segs)
1877{
1878 elf->e_shoff = e_shoff;
1879 elf->e_shentsize = sizeof(*shdr4extnum);
1880 elf->e_shnum = 1;
1881 elf->e_shstrndx = SHN_UNDEF;
1882
1883 memset(shdr4extnum, 0, sizeof(*shdr4extnum));
1884
1885 shdr4extnum->sh_type = SHT_NULL;
1886 shdr4extnum->sh_size = elf->e_shnum;
1887 shdr4extnum->sh_link = elf->e_shstrndx;
1888 shdr4extnum->sh_info = segs;
1889}
1890
1891static size_t elf_core_vma_data_size(struct vm_area_struct *gate_vma,
1892 unsigned long mm_flags)
1893{
1894 struct vm_area_struct *vma;
1895 size_t size = 0;
1896
1897 for (vma = first_vma(current, gate_vma); vma != NULL;
1898 vma = next_vma(vma, gate_vma))
1899 size += vma_dump_size(vma, mm_flags);
1900 return size;
1901}
1902
1903/*
1904 * Actual dumper
1905 *
1906 * This is a two-pass process; first we find the offsets of the bits,
1907 * and then they are actually written out. If we run out of core limit
1908 * we just truncate.
1909 */
1910static int elf_core_dump(struct coredump_params *cprm)
1911{
1912 int has_dumped = 0;
1913 mm_segment_t fs;
1914 int segs;
1915 size_t size = 0;
1916 struct vm_area_struct *vma, *gate_vma;
1917 struct elfhdr *elf = NULL;
1918 loff_t offset = 0, dataoff, foffset;
1919 struct elf_note_info info;
1920 struct elf_phdr *phdr4note = NULL;
1921 struct elf_shdr *shdr4extnum = NULL;
1922 Elf_Half e_phnum;
1923 elf_addr_t e_shoff;
1924
1925 /*
1926 * We no longer stop all VM operations.
1927 *
1928 * This is because those proceses that could possibly change map_count
1929 * or the mmap / vma pages are now blocked in do_exit on current
1930 * finishing this core dump.
1931 *
1932 * Only ptrace can touch these memory addresses, but it doesn't change
1933 * the map_count or the pages allocated. So no possibility of crashing
1934 * exists while dumping the mm->vm_next areas to the core file.
1935 */
1936
1937 /* alloc memory for large data structures: too large to be on stack */
1938 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1939 if (!elf)
1940 goto out;
1941 /*
1942 * The number of segs are recored into ELF header as 16bit value.
1943 * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here.
1944 */
1945 segs = current->mm->map_count;
1946 segs += elf_core_extra_phdrs();
1947
1948 gate_vma = get_gate_vma(current->mm);
1949 if (gate_vma != NULL)
1950 segs++;
1951
1952 /* for notes section */
1953 segs++;
1954
1955 /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
1956 * this, kernel supports extended numbering. Have a look at
1957 * include/linux/elf.h for further information. */
1958 e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
1959
1960 /*
1961 * Collect all the non-memory information about the process for the
1962 * notes. This also sets up the file header.
1963 */
1964 if (!fill_note_info(elf, e_phnum, &info, cprm->signr, cprm->regs))
1965 goto cleanup;
1966
1967 has_dumped = 1;
1968 current->flags |= PF_DUMPCORE;
1969
1970 fs = get_fs();
1971 set_fs(KERNEL_DS);
1972
1973 offset += sizeof(*elf); /* Elf header */
1974 offset += segs * sizeof(struct elf_phdr); /* Program headers */
1975 foffset = offset;
1976
1977 /* Write notes phdr entry */
1978 {
1979 size_t sz = get_note_info_size(&info);
1980
1981 sz += elf_coredump_extra_notes_size();
1982
1983 phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
1984 if (!phdr4note)
1985 goto end_coredump;
1986
1987 fill_elf_note_phdr(phdr4note, sz, offset);
1988 offset += sz;
1989 }
1990
1991 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1992
1993 offset += elf_core_vma_data_size(gate_vma, cprm->mm_flags);
1994 offset += elf_core_extra_data_size();
1995 e_shoff = offset;
1996
1997 if (e_phnum == PN_XNUM) {
1998 shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
1999 if (!shdr4extnum)
2000 goto end_coredump;
2001 fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
2002 }
2003
2004 offset = dataoff;
2005
2006 size += sizeof(*elf);
2007 if (size > cprm->limit || !dump_write(cprm->file, elf, sizeof(*elf)))
2008 goto end_coredump;
2009
2010 size += sizeof(*phdr4note);
2011 if (size > cprm->limit
2012 || !dump_write(cprm->file, phdr4note, sizeof(*phdr4note)))
2013 goto end_coredump;
2014
2015 /* Write program headers for segments dump */
2016 for (vma = first_vma(current, gate_vma); vma != NULL;
2017 vma = next_vma(vma, gate_vma)) {
2018 struct elf_phdr phdr;
2019
2020 phdr.p_type = PT_LOAD;
2021 phdr.p_offset = offset;
2022 phdr.p_vaddr = vma->vm_start;
2023 phdr.p_paddr = 0;
2024 phdr.p_filesz = vma_dump_size(vma, cprm->mm_flags);
2025 phdr.p_memsz = vma->vm_end - vma->vm_start;
2026 offset += phdr.p_filesz;
2027 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
2028 if (vma->vm_flags & VM_WRITE)
2029 phdr.p_flags |= PF_W;
2030 if (vma->vm_flags & VM_EXEC)
2031 phdr.p_flags |= PF_X;
2032 phdr.p_align = ELF_EXEC_PAGESIZE;
2033
2034 size += sizeof(phdr);
2035 if (size > cprm->limit
2036 || !dump_write(cprm->file, &phdr, sizeof(phdr)))
2037 goto end_coredump;
2038 }
2039
2040 if (!elf_core_write_extra_phdrs(cprm->file, offset, &size, cprm->limit))
2041 goto end_coredump;
2042
2043 /* write out the notes section */
2044 if (!write_note_info(&info, cprm->file, &foffset))
2045 goto end_coredump;
2046
2047 if (elf_coredump_extra_notes_write(cprm->file, &foffset))
2048 goto end_coredump;
2049
2050 /* Align to page */
2051 if (!dump_seek(cprm->file, dataoff - foffset))
2052 goto end_coredump;
2053
2054 for (vma = first_vma(current, gate_vma); vma != NULL;
2055 vma = next_vma(vma, gate_vma)) {
2056 unsigned long addr;
2057 unsigned long end;
2058
2059 end = vma->vm_start + vma_dump_size(vma, cprm->mm_flags);
2060
2061 for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
2062 struct page *page;
2063 int stop;
2064
2065 page = get_dump_page(addr);
2066 if (page) {
2067 void *kaddr = kmap(page);
2068 stop = ((size += PAGE_SIZE) > cprm->limit) ||
2069 !dump_write(cprm->file, kaddr,
2070 PAGE_SIZE);
2071 kunmap(page);
2072 page_cache_release(page);
2073 } else
2074 stop = !dump_seek(cprm->file, PAGE_SIZE);
2075 if (stop)
2076 goto end_coredump;
2077 }
2078 }
2079
2080 if (!elf_core_write_extra_data(cprm->file, &size, cprm->limit))
2081 goto end_coredump;
2082
2083 if (e_phnum == PN_XNUM) {
2084 size += sizeof(*shdr4extnum);
2085 if (size > cprm->limit
2086 || !dump_write(cprm->file, shdr4extnum,
2087 sizeof(*shdr4extnum)))
2088 goto end_coredump;
2089 }
2090
2091end_coredump:
2092 set_fs(fs);
2093
2094cleanup:
2095 free_note_info(&info);
2096 kfree(shdr4extnum);
2097 kfree(phdr4note);
2098 kfree(elf);
2099out:
2100 return has_dumped;
2101}
2102
2103#endif /* CONFIG_ELF_CORE */
2104
2105static int __init init_elf_binfmt(void)
2106{
2107 register_binfmt(&elf_format);
2108 return 0;
2109}
2110
2111static void __exit exit_elf_binfmt(void)
2112{
2113 /* Remove the COFF and ELF loaders. */
2114 unregister_binfmt(&elf_format);
2115}
2116
2117core_initcall(init_elf_binfmt);
2118module_exit(exit_elf_binfmt);
2119MODULE_LICENSE("GPL");