blob: d48ff5f370f4e5122c3402ed44889564366ad317 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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/stat.h>
16#include <linux/time.h>
17#include <linux/mm.h>
18#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
20#include <linux/signal.h>
21#include <linux/binfmts.h>
22#include <linux/string.h>
23#include <linux/file.h>
24#include <linux/fcntl.h>
25#include <linux/ptrace.h>
26#include <linux/slab.h>
27#include <linux/shm.h>
28#include <linux/personality.h>
29#include <linux/elfcore.h>
30#include <linux/init.h>
31#include <linux/highuid.h>
32#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/compiler.h>
34#include <linux/highmem.h>
35#include <linux/pagemap.h>
36#include <linux/security.h>
37#include <linux/syscalls.h>
38#include <linux/random.h>
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070039#include <linux/elf.h>
Alexey Dobriyan7e80d0d2007-05-08 00:28:59 -070040#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
42#include <asm/param.h>
43#include <asm/page.h>
44
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070045static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
46static int load_elf_library(struct file *);
Andrew Mortonbb1ad822008-01-30 13:31:07 +010047static unsigned long elf_map(struct file *, unsigned long, struct elf_phdr *,
48 int, int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * If we don't support core dumping, then supply a NULL so we
52 * don't even try.
53 */
Matt Mackall708e9a72006-01-08 01:05:25 -080054#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
Neil Horman7dc0b222007-10-16 23:26:34 -070055static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#else
57#define elf_core_dump NULL
58#endif
59
60#if ELF_EXEC_PAGESIZE > PAGE_SIZE
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070061#define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070063#define ELF_MIN_ALIGN PAGE_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
66#ifndef ELF_CORE_EFLAGS
67#define ELF_CORE_EFLAGS 0
68#endif
69
70#define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
71#define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
72#define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
73
74static struct linux_binfmt elf_format = {
75 .module = THIS_MODULE,
76 .load_binary = load_elf_binary,
77 .load_shlib = load_elf_library,
78 .core_dump = elf_core_dump,
Andi Kleen9fbbd4d2007-02-13 13:26:26 +010079 .min_coredump = ELF_EXEC_PAGESIZE,
80 .hasvdso = 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
Andrew Mortond4e3cc32007-07-21 04:37:32 -070083#define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static int set_brk(unsigned long start, unsigned long end)
86{
87 start = ELF_PAGEALIGN(start);
88 end = ELF_PAGEALIGN(end);
89 if (end > start) {
90 unsigned long addr;
91 down_write(&current->mm->mmap_sem);
92 addr = do_brk(start, end - start);
93 up_write(&current->mm->mmap_sem);
94 if (BAD_ADDR(addr))
95 return addr;
96 }
97 current->mm->start_brk = current->mm->brk = end;
98 return 0;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* We need to explicitly zero any fractional pages
102 after the data section (i.e. bss). This would
103 contain the junk from the file that should not
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700104 be in memory
105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static int padzero(unsigned long elf_bss)
107{
108 unsigned long nbyte;
109
110 nbyte = ELF_PAGEOFFSET(elf_bss);
111 if (nbyte) {
112 nbyte = ELF_MIN_ALIGN - nbyte;
113 if (clear_user((void __user *) elf_bss, nbyte))
114 return -EFAULT;
115 }
116 return 0;
117}
118
Ohad Ben-Cohen09c6dd32008-02-03 18:05:15 +0200119/* Let's use some macros to make this stack manipulation a little clearer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#ifdef CONFIG_STACK_GROWSUP
121#define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
122#define STACK_ROUND(sp, items) \
123 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700124#define STACK_ALLOC(sp, len) ({ \
125 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
126 old_sp; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#else
128#define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
129#define STACK_ROUND(sp, items) \
130 (((unsigned long) (sp - items)) &~ 15UL)
131#define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
132#endif
133
134static int
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700135create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
Andi Kleend20894a2008-02-08 04:21:54 -0800136 unsigned long load_addr, unsigned long interp_load_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 unsigned long p = bprm->p;
139 int argc = bprm->argc;
140 int envc = bprm->envc;
141 elf_addr_t __user *argv;
142 elf_addr_t __user *envp;
143 elf_addr_t __user *sp;
144 elf_addr_t __user *u_platform;
145 const char *k_platform = ELF_PLATFORM;
146 int items;
147 elf_addr_t *elf_info;
148 int ei_index = 0;
149 struct task_struct *tsk = current;
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700150 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /*
Franck Bui-Huud68c9d62007-10-16 23:30:24 -0700153 * In some cases (e.g. Hyper-Threading), we want to avoid L1
154 * evictions by the processes running on the same package. One
155 * thing we can do is to shuffle the initial stack for them.
156 */
157
158 p = arch_align_stack(p);
159
160 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * If this architecture has a platform capability string, copy it
162 * to userspace. In some cases (Sparc), this info is impossible
163 * for userspace to get any other way, in others (i386) it is
164 * merely difficult.
165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 u_platform = NULL;
167 if (k_platform) {
168 size_t len = strlen(k_platform) + 1;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
171 if (__copy_to_user(u_platform, k_platform, len))
172 return -EFAULT;
173 }
174
175 /* Create the ELF interpreter info */
Jesper Juhl785d5572006-06-23 02:05:35 -0700176 elf_info = (elf_addr_t *)current->mm->saved_auxv;
Olaf Hering4f9a58d2007-10-16 23:30:12 -0700177 /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define NEW_AUX_ENT(id, val) \
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700179 do { \
Jesper Juhl785d5572006-06-23 02:05:35 -0700180 elf_info[ei_index++] = id; \
181 elf_info[ei_index++] = val; \
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700182 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184#ifdef ARCH_DLINFO
185 /*
186 * ARCH_DLINFO must come first so PPC can do its special alignment of
187 * AUXV.
Olaf Hering4f9a58d2007-10-16 23:30:12 -0700188 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
189 * ARCH_DLINFO changes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191 ARCH_DLINFO;
192#endif
193 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
194 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
195 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
196 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700197 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
199 NEW_AUX_ENT(AT_BASE, interp_load_addr);
200 NEW_AUX_ENT(AT_FLAGS, 0);
201 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
Jesper Juhl785d5572006-06-23 02:05:35 -0700202 NEW_AUX_ENT(AT_UID, tsk->uid);
203 NEW_AUX_ENT(AT_EUID, tsk->euid);
204 NEW_AUX_ENT(AT_GID, tsk->gid);
205 NEW_AUX_ENT(AT_EGID, tsk->egid);
206 NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (k_platform) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700208 NEW_AUX_ENT(AT_PLATFORM,
Jesper Juhl785d5572006-06-23 02:05:35 -0700209 (elf_addr_t)(unsigned long)u_platform);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
Jesper Juhl785d5572006-06-23 02:05:35 -0700212 NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214#undef NEW_AUX_ENT
215 /* AT_NULL is zero; clear the rest too */
216 memset(&elf_info[ei_index], 0,
217 sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
218
219 /* And advance past the AT_NULL entry. */
220 ei_index += 2;
221
222 sp = STACK_ADD(p, ei_index);
223
Andi Kleend20894a2008-02-08 04:21:54 -0800224 items = (argc + 1) + (envc + 1) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 bprm->p = STACK_ROUND(sp, items);
226
227 /* Point sp at the lowest address on the stack */
228#ifdef CONFIG_STACK_GROWSUP
229 sp = (elf_addr_t __user *)bprm->p - items - ei_index;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700230 bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#else
232 sp = (elf_addr_t __user *)bprm->p;
233#endif
234
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700235
236 /*
237 * Grow the stack manually; some architectures have a limit on how
238 * far ahead a user-space access may be in order to grow the stack.
239 */
240 vma = find_extend_vma(current->mm, bprm->p);
241 if (!vma)
242 return -EFAULT;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
245 if (__put_user(argc, sp++))
246 return -EFAULT;
Andi Kleend20894a2008-02-08 04:21:54 -0800247 argv = sp;
248 envp = argv + argc + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* Populate argv and envp */
Greg Kroah-Hartmana84a5052005-05-11 00:10:44 -0700251 p = current->mm->arg_end = current->mm->arg_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 while (argc-- > 0) {
253 size_t len;
Heiko Carstens841d5fb2006-12-06 20:36:35 -0800254 if (__put_user((elf_addr_t)p, argv++))
255 return -EFAULT;
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700256 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
257 if (!len || len > MAX_ARG_STRLEN)
WANG Cong23c49712008-05-08 21:52:33 +0800258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 p += len;
260 }
261 if (__put_user(0, argv))
262 return -EFAULT;
263 current->mm->arg_end = current->mm->env_start = p;
264 while (envc-- > 0) {
265 size_t len;
Heiko Carstens841d5fb2006-12-06 20:36:35 -0800266 if (__put_user((elf_addr_t)p, envp++))
267 return -EFAULT;
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700268 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
269 if (!len || len > MAX_ARG_STRLEN)
WANG Cong23c49712008-05-08 21:52:33 +0800270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 p += len;
272 }
273 if (__put_user(0, envp))
274 return -EFAULT;
275 current->mm->env_end = p;
276
277 /* Put the elf_info on the stack in the right place. */
278 sp = (elf_addr_t __user *)envp + 1;
279 if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
280 return -EFAULT;
281 return 0;
282}
283
284#ifndef elf_map
285
286static unsigned long elf_map(struct file *filep, unsigned long addr,
Jiri Kosinacc503c12008-01-30 13:31:07 +0100287 struct elf_phdr *eppnt, int prot, int type,
288 unsigned long total_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 unsigned long map_addr;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100291 unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr);
292 unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr);
293 addr = ELF_PAGESTART(addr);
294 size = ELF_PAGEALIGN(size);
Jan Kratochvil60bfba72007-07-15 23:40:06 -0700295
Andrew Mortond4e3cc32007-07-21 04:37:32 -0700296 /* mmap() will return -EINVAL if given a zero size, but a
297 * segment with zero filesize is perfectly valid */
Jiri Kosinacc503c12008-01-30 13:31:07 +0100298 if (!size)
299 return addr;
300
301 down_write(&current->mm->mmap_sem);
302 /*
303 * total_size is the size of the ELF (interpreter) image.
304 * The _first_ mmap needs to know the full size, otherwise
305 * randomization might put this image into an overlapping
306 * position with the ELF binary image. (since size < total_size)
307 * So we first map the 'big' image - and unmap the remainder at
308 * the end. (which unmap is needed for ELF images with holes.)
309 */
310 if (total_size) {
311 total_size = ELF_PAGEALIGN(total_size);
312 map_addr = do_mmap(filep, addr, total_size, prot, type, off);
313 if (!BAD_ADDR(map_addr))
314 do_munmap(current->mm, map_addr+size, total_size-size);
315 } else
316 map_addr = do_mmap(filep, addr, size, prot, type, off);
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 up_write(&current->mm->mmap_sem);
319 return(map_addr);
320}
321
322#endif /* !elf_map */
323
Jiri Kosinacc503c12008-01-30 13:31:07 +0100324static unsigned long total_mapping_size(struct elf_phdr *cmds, int nr)
325{
326 int i, first_idx = -1, last_idx = -1;
327
328 for (i = 0; i < nr; i++) {
329 if (cmds[i].p_type == PT_LOAD) {
330 last_idx = i;
331 if (first_idx == -1)
332 first_idx = i;
333 }
334 }
335 if (first_idx == -1)
336 return 0;
337
338 return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz -
339 ELF_PAGESTART(cmds[first_idx].p_vaddr);
340}
341
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/* This is much more generalized than the library routine read function,
344 so we keep this separate. Technically the library read function
345 is only provided so that we can read a.out libraries that have
346 an ELF header */
347
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700348static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
Jiri Kosinacc503c12008-01-30 13:31:07 +0100349 struct file *interpreter, unsigned long *interp_map_addr,
350 unsigned long no_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct elf_phdr *elf_phdata;
353 struct elf_phdr *eppnt;
354 unsigned long load_addr = 0;
355 int load_addr_set = 0;
356 unsigned long last_bss = 0, elf_bss = 0;
357 unsigned long error = ~0UL;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100358 unsigned long total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int retval, i, size;
360
361 /* First of all, some simple consistency checks */
362 if (interp_elf_ex->e_type != ET_EXEC &&
363 interp_elf_ex->e_type != ET_DYN)
364 goto out;
365 if (!elf_check_arch(interp_elf_ex))
366 goto out;
367 if (!interpreter->f_op || !interpreter->f_op->mmap)
368 goto out;
369
370 /*
371 * If the size of this structure has changed, then punt, since
372 * we will be doing the wrong thing.
373 */
374 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
375 goto out;
376 if (interp_elf_ex->e_phnum < 1 ||
377 interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
378 goto out;
379
380 /* Now read in all of the header information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
382 if (size > ELF_MIN_ALIGN)
383 goto out;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700384 elf_phdata = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!elf_phdata)
386 goto out;
387
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700388 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
389 (char *)elf_phdata,size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 error = -EIO;
391 if (retval != size) {
392 if (retval < 0)
393 error = retval;
394 goto out_close;
395 }
396
Jiri Kosinacc503c12008-01-30 13:31:07 +0100397 total_size = total_mapping_size(elf_phdata, interp_elf_ex->e_phnum);
398 if (!total_size) {
399 error = -EINVAL;
400 goto out_close;
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 eppnt = elf_phdata;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700404 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
405 if (eppnt->p_type == PT_LOAD) {
406 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
407 int elf_prot = 0;
408 unsigned long vaddr = 0;
409 unsigned long k, map_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700411 if (eppnt->p_flags & PF_R)
412 elf_prot = PROT_READ;
413 if (eppnt->p_flags & PF_W)
414 elf_prot |= PROT_WRITE;
415 if (eppnt->p_flags & PF_X)
416 elf_prot |= PROT_EXEC;
417 vaddr = eppnt->p_vaddr;
418 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
419 elf_type |= MAP_FIXED;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100420 else if (no_base && interp_elf_ex->e_type == ET_DYN)
421 load_addr = -vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700423 map_addr = elf_map(interpreter, load_addr + vaddr,
Andrew Mortonbb1ad822008-01-30 13:31:07 +0100424 eppnt, elf_prot, elf_type, total_size);
Jiri Kosinacc503c12008-01-30 13:31:07 +0100425 total_size = 0;
426 if (!*interp_map_addr)
427 *interp_map_addr = map_addr;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700428 error = map_addr;
429 if (BAD_ADDR(map_addr))
430 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700432 if (!load_addr_set &&
433 interp_elf_ex->e_type == ET_DYN) {
434 load_addr = map_addr - ELF_PAGESTART(vaddr);
435 load_addr_set = 1;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700438 /*
439 * Check to see if the section's size will overflow the
440 * allowed task size. Note that p_filesz must always be
441 * <= p_memsize so it's only necessary to check p_memsz.
442 */
443 k = load_addr + eppnt->p_vaddr;
Chuck Ebbertce510592006-07-03 00:24:14 -0700444 if (BAD_ADDR(k) ||
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700445 eppnt->p_filesz > eppnt->p_memsz ||
446 eppnt->p_memsz > TASK_SIZE ||
447 TASK_SIZE - eppnt->p_memsz < k) {
448 error = -ENOMEM;
449 goto out_close;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700452 /*
453 * Find the end of the file mapping for this phdr, and
454 * keep track of the largest address we see for this.
455 */
456 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
457 if (k > elf_bss)
458 elf_bss = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700460 /*
461 * Do the same thing for the memory mapping - between
462 * elf_bss and last_bss is the bss section.
463 */
464 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
465 if (k > last_bss)
466 last_bss = k;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 /*
471 * Now fill out the bss section. First pad the last page up
472 * to the page boundary, and then perform a mmap to make sure
473 * that there are zero-mapped pages up to and including the
474 * last bss page.
475 */
476 if (padzero(elf_bss)) {
477 error = -EFAULT;
478 goto out_close;
479 }
480
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700481 /* What we have mapped so far */
482 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 /* Map the last of the bss segment */
485 if (last_bss > elf_bss) {
486 down_write(&current->mm->mmap_sem);
487 error = do_brk(elf_bss, last_bss - elf_bss);
488 up_write(&current->mm->mmap_sem);
489 if (BAD_ADDR(error))
490 goto out_close;
491 }
492
Jiri Kosinacc503c12008-01-30 13:31:07 +0100493 error = load_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495out_close:
496 kfree(elf_phdata);
497out:
498 return error;
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
502 * These are the functions used to load ELF style executables and shared
503 * libraries. There is no binary dependent code anywhere else.
504 */
505
506#define INTERPRETER_NONE 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507#define INTERPRETER_ELF 2
508
Andi Kleen913bd902006-03-25 16:29:09 +0100509#ifndef STACK_RND_MASK
James Bottomleyd1cabd62007-03-16 13:38:35 -0800510#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
Andi Kleen913bd902006-03-25 16:29:09 +0100511#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513static unsigned long randomize_stack_top(unsigned long stack_top)
514{
515 unsigned int random_variable = 0;
516
Andi Kleenc16b63e2006-09-26 10:52:28 +0200517 if ((current->flags & PF_RANDOMIZE) &&
518 !(current->personality & ADDR_NO_RANDOMIZE)) {
Andi Kleen913bd902006-03-25 16:29:09 +0100519 random_variable = get_random_int() & STACK_RND_MASK;
520 random_variable <<= PAGE_SHIFT;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#ifdef CONFIG_STACK_GROWSUP
Andi Kleen913bd902006-03-25 16:29:09 +0100523 return PAGE_ALIGN(stack_top) + random_variable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#else
Andi Kleen913bd902006-03-25 16:29:09 +0100525 return PAGE_ALIGN(stack_top) - random_variable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526#endif
527}
528
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700529static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct file *interpreter = NULL; /* to shut gcc up */
532 unsigned long load_addr = 0, load_bias = 0;
533 int load_addr_set = 0;
534 char * elf_interpreter = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned long error;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700536 struct elf_phdr *elf_ppnt, *elf_phdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 unsigned long elf_bss, elf_brk;
538 int elf_exec_fileno;
539 int retval, i;
540 unsigned int size;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100541 unsigned long elf_entry;
542 unsigned long interp_load_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unsigned long start_code, end_code, start_data, end_data;
544 unsigned long reloc_func_desc = 0;
David Rientjes8de61e62006-12-06 20:40:16 -0800545 int executable_stack = EXSTACK_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 unsigned long def_flags = 0;
547 struct {
548 struct elfhdr elf_ex;
549 struct elfhdr interp_elf_ex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 } *loc;
551
552 loc = kmalloc(sizeof(*loc), GFP_KERNEL);
553 if (!loc) {
554 retval = -ENOMEM;
555 goto out_ret;
556 }
557
558 /* Get the exec-header */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700559 loc->elf_ex = *((struct elfhdr *)bprm->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 retval = -ENOEXEC;
562 /* First of all, some simple consistency checks */
563 if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
564 goto out;
565
566 if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
567 goto out;
568 if (!elf_check_arch(&loc->elf_ex))
569 goto out;
570 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
571 goto out;
572
573 /* Now read in all of the header information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
575 goto out;
576 if (loc->elf_ex.e_phnum < 1 ||
577 loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
578 goto out;
579 size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
580 retval = -ENOMEM;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700581 elf_phdata = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (!elf_phdata)
583 goto out;
584
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700585 retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
586 (char *)elf_phdata, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (retval != size) {
588 if (retval >= 0)
589 retval = -EIO;
590 goto out_free_ph;
591 }
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 retval = get_unused_fd();
594 if (retval < 0)
Al Virofd8328b2008-04-22 05:11:59 -0400595 goto out_free_ph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 get_file(bprm->file);
597 fd_install(elf_exec_fileno = retval, bprm->file);
598
599 elf_ppnt = elf_phdata;
600 elf_bss = 0;
601 elf_brk = 0;
602
603 start_code = ~0UL;
604 end_code = 0;
605 start_data = 0;
606 end_data = 0;
607
608 for (i = 0; i < loc->elf_ex.e_phnum; i++) {
609 if (elf_ppnt->p_type == PT_INTERP) {
610 /* This is the program interpreter used for
611 * shared libraries - for now assume that this
612 * is an a.out format binary
613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 retval = -ENOEXEC;
615 if (elf_ppnt->p_filesz > PATH_MAX ||
616 elf_ppnt->p_filesz < 2)
617 goto out_free_file;
618
619 retval = -ENOMEM;
Jesper Juhl792db3a2006-01-09 20:54:45 -0800620 elf_interpreter = kmalloc(elf_ppnt->p_filesz,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700621 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (!elf_interpreter)
623 goto out_free_file;
624
625 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700626 elf_interpreter,
627 elf_ppnt->p_filesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (retval != elf_ppnt->p_filesz) {
629 if (retval >= 0)
630 retval = -EIO;
631 goto out_free_interp;
632 }
633 /* make sure path is NULL terminated */
634 retval = -ENOEXEC;
635 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
636 goto out_free_interp;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /*
639 * The early SET_PERSONALITY here is so that the lookup
640 * for the interpreter happens in the namespace of the
641 * to-be-execed image. SET_PERSONALITY can select an
642 * alternate root.
643 *
644 * However, SET_PERSONALITY is NOT allowed to switch
645 * this task into the new images's memory mapping
646 * policy - that is, TASK_SIZE must still evaluate to
647 * that which is appropriate to the execing application.
648 * This is because exit_mmap() needs to have TASK_SIZE
649 * evaluate to the size of the old image.
650 *
651 * So if (say) a 64-bit application is execing a 32-bit
652 * application it is the architecture's responsibility
653 * to defer changing the value of TASK_SIZE until the
654 * switch really is going to happen - do this in
655 * flush_thread(). - akpm
656 */
Andi Kleen612a95b2008-01-30 13:33:32 +0100657 SET_PERSONALITY(loc->elf_ex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 interpreter = open_exec(elf_interpreter);
660 retval = PTR_ERR(interpreter);
661 if (IS_ERR(interpreter))
662 goto out_free_interp;
Alexey Dobriyan1fb84492007-01-26 00:57:16 -0800663
664 /*
665 * If the binary is not readable then enforce
666 * mm->dumpable = 0 regardless of the interpreter's
667 * permissions.
668 */
669 if (file_permission(interpreter, MAY_READ) < 0)
670 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
671
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700672 retval = kernel_read(interpreter, 0, bprm->buf,
673 BINPRM_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (retval != BINPRM_BUF_SIZE) {
675 if (retval >= 0)
676 retval = -EIO;
677 goto out_free_dentry;
678 }
679
680 /* Get the exec headers */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700681 loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
683 }
684 elf_ppnt++;
685 }
686
687 elf_ppnt = elf_phdata;
688 for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
689 if (elf_ppnt->p_type == PT_GNU_STACK) {
690 if (elf_ppnt->p_flags & PF_X)
691 executable_stack = EXSTACK_ENABLE_X;
692 else
693 executable_stack = EXSTACK_DISABLE_X;
694 break;
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* Some simple consistency checks for the interpreter */
698 if (elf_interpreter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 retval = -ELIBBAD;
Andi Kleend20894a2008-02-08 04:21:54 -0800700 /* Not an ELF interpreter */
701 if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto out_free_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* Verify the interpreter has a valid arch */
Andi Kleend20894a2008-02-08 04:21:54 -0800704 if (!elf_check_arch(&loc->interp_elf_ex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto out_free_dentry;
706 } else {
707 /* Executables without an interpreter also need a personality */
Andi Kleen612a95b2008-01-30 13:33:32 +0100708 SET_PERSONALITY(loc->elf_ex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* Flush all traces of the currently running executable */
712 retval = flush_old_exec(bprm);
713 if (retval)
714 goto out_free_dentry;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* OK, This is the point of no return */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 current->flags &= ~PF_FORKNOEXEC;
718 current->mm->def_flags = def_flags;
719
720 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
721 may depend on the personality. */
Andi Kleen612a95b2008-01-30 13:33:32 +0100722 SET_PERSONALITY(loc->elf_ex, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (elf_read_implies_exec(loc->elf_ex, executable_stack))
724 current->personality |= READ_IMPLIES_EXEC;
725
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700726 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 current->flags |= PF_RANDOMIZE;
728 arch_pick_mmap_layout(current->mm);
729
730 /* Do this so that we can load the interpreter, if need be. We will
731 change some of these later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 current->mm->free_area_cache = current->mm->mmap_base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700733 current->mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
735 executable_stack);
736 if (retval < 0) {
737 send_sig(SIGKILL, current, 0);
738 goto out_free_dentry;
739 }
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 current->mm->start_stack = bprm->p;
742
743 /* Now we do a little grungy work by mmaping the ELF image into
Jiri Kosinacc503c12008-01-30 13:31:07 +0100744 the correct location in memory. */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700745 for(i = 0, elf_ppnt = elf_phdata;
746 i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int elf_prot = 0, elf_flags;
748 unsigned long k, vaddr;
749
750 if (elf_ppnt->p_type != PT_LOAD)
751 continue;
752
753 if (unlikely (elf_brk > elf_bss)) {
754 unsigned long nbyte;
755
756 /* There was a PT_LOAD segment with p_memsz > p_filesz
757 before this one. Map anonymous pages, if needed,
758 and clear the area. */
759 retval = set_brk (elf_bss + load_bias,
760 elf_brk + load_bias);
761 if (retval) {
762 send_sig(SIGKILL, current, 0);
763 goto out_free_dentry;
764 }
765 nbyte = ELF_PAGEOFFSET(elf_bss);
766 if (nbyte) {
767 nbyte = ELF_MIN_ALIGN - nbyte;
768 if (nbyte > elf_brk - elf_bss)
769 nbyte = elf_brk - elf_bss;
770 if (clear_user((void __user *)elf_bss +
771 load_bias, nbyte)) {
772 /*
773 * This bss-zeroing can fail if the ELF
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700774 * file specifies odd protections. So
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * we don't check the return value
776 */
777 }
778 }
779 }
780
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700781 if (elf_ppnt->p_flags & PF_R)
782 elf_prot |= PROT_READ;
783 if (elf_ppnt->p_flags & PF_W)
784 elf_prot |= PROT_WRITE;
785 if (elf_ppnt->p_flags & PF_X)
786 elf_prot |= PROT_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700788 elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 vaddr = elf_ppnt->p_vaddr;
791 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
792 elf_flags |= MAP_FIXED;
793 } else if (loc->elf_ex.e_type == ET_DYN) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700794 /* Try and get dynamic programs out of the way of the
795 * default mmap base, as well as whatever program they
796 * might try to exec. This is because the brk will
797 * follow the loader, and is not movable. */
Jiri Kosinacc503c12008-01-30 13:31:07 +0100798#ifdef CONFIG_X86
799 load_bias = 0;
800#else
Linus Torvalds90cb28e2007-01-06 13:28:21 -0800801 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
Jiri Kosinacc503c12008-01-30 13:31:07 +0100802#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700805 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
Andrew Mortonbb1ad822008-01-30 13:31:07 +0100806 elf_prot, elf_flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (BAD_ADDR(error)) {
808 send_sig(SIGKILL, current, 0);
Alexey Kuznetsovb140f252007-05-08 00:31:57 -0700809 retval = IS_ERR((void *)error) ?
810 PTR_ERR((void*)error) : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto out_free_dentry;
812 }
813
814 if (!load_addr_set) {
815 load_addr_set = 1;
816 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
817 if (loc->elf_ex.e_type == ET_DYN) {
818 load_bias += error -
819 ELF_PAGESTART(load_bias + vaddr);
820 load_addr += load_bias;
821 reloc_func_desc = load_bias;
822 }
823 }
824 k = elf_ppnt->p_vaddr;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700825 if (k < start_code)
826 start_code = k;
827 if (start_data < k)
828 start_data = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /*
831 * Check to see if the section's size will overflow the
832 * allowed task size. Note that p_filesz must always be
833 * <= p_memsz so it is only necessary to check p_memsz.
834 */
Chuck Ebbertce510592006-07-03 00:24:14 -0700835 if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 elf_ppnt->p_memsz > TASK_SIZE ||
837 TASK_SIZE - elf_ppnt->p_memsz < k) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700838 /* set_brk can never work. Avoid overflows. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 send_sig(SIGKILL, current, 0);
Alexey Kuznetsovb140f252007-05-08 00:31:57 -0700840 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto out_free_dentry;
842 }
843
844 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
845
846 if (k > elf_bss)
847 elf_bss = k;
848 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
849 end_code = k;
850 if (end_data < k)
851 end_data = k;
852 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
853 if (k > elf_brk)
854 elf_brk = k;
855 }
856
857 loc->elf_ex.e_entry += load_bias;
858 elf_bss += load_bias;
859 elf_brk += load_bias;
860 start_code += load_bias;
861 end_code += load_bias;
862 start_data += load_bias;
863 end_data += load_bias;
864
865 /* Calling set_brk effectively mmaps the pages that we need
866 * for the bss and break sections. We must do this before
867 * mapping in the interpreter, to make sure it doesn't wind
868 * up getting placed where the bss needs to go.
869 */
870 retval = set_brk(elf_bss, elf_brk);
871 if (retval) {
872 send_sig(SIGKILL, current, 0);
873 goto out_free_dentry;
874 }
akpm@osdl.org6de50512005-10-11 08:29:08 -0700875 if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 send_sig(SIGSEGV, current, 0);
877 retval = -EFAULT; /* Nobody gets to see this, but.. */
878 goto out_free_dentry;
879 }
880
881 if (elf_interpreter) {
Andi Kleend20894a2008-02-08 04:21:54 -0800882 unsigned long uninitialized_var(interp_map_addr);
Jiri Kosinacc503c12008-01-30 13:31:07 +0100883
Andi Kleend20894a2008-02-08 04:21:54 -0800884 elf_entry = load_elf_interp(&loc->interp_elf_ex,
885 interpreter,
886 &interp_map_addr,
887 load_bias);
888 if (!IS_ERR((void *)elf_entry)) {
889 /*
890 * load_elf_interp() returns relocation
891 * adjustment
892 */
893 interp_load_addr = elf_entry;
894 elf_entry += loc->interp_elf_ex.e_entry;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (BAD_ADDR(elf_entry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 force_sig(SIGSEGV, current);
Chuck Ebbertce510592006-07-03 00:24:14 -0700898 retval = IS_ERR((void *)elf_entry) ?
899 (int)elf_entry : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto out_free_dentry;
901 }
902 reloc_func_desc = interp_load_addr;
903
904 allow_write_access(interpreter);
905 fput(interpreter);
906 kfree(elf_interpreter);
907 } else {
908 elf_entry = loc->elf_ex.e_entry;
Suresh Siddha5342fba2006-02-26 04:18:28 +0100909 if (BAD_ADDR(elf_entry)) {
Chuck Ebbertce510592006-07-03 00:24:14 -0700910 force_sig(SIGSEGV, current);
911 retval = -EINVAL;
Suresh Siddha5342fba2006-02-26 04:18:28 +0100912 goto out_free_dentry;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
916 kfree(elf_phdata);
917
Andi Kleend20894a2008-02-08 04:21:54 -0800918 sys_close(elf_exec_fileno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 set_binfmt(&elf_format);
921
Benjamin Herrenschmidt547ee842005-04-16 15:24:35 -0700922#ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
923 retval = arch_setup_additional_pages(bprm, executable_stack);
924 if (retval < 0) {
925 send_sig(SIGKILL, current, 0);
Roland McGrath18c8baf2005-04-28 15:17:19 -0700926 goto out;
Benjamin Herrenschmidt547ee842005-04-16 15:24:35 -0700927 }
928#endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 compute_creds(bprm);
931 current->flags &= ~PF_FORKNOEXEC;
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700932 retval = create_elf_tables(bprm, &loc->elf_ex,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700933 load_addr, interp_load_addr);
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700934 if (retval < 0) {
935 send_sig(SIGKILL, current, 0);
936 goto out;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /* N.B. passed_fileno might not be initialized? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 current->mm->end_code = end_code;
940 current->mm->start_code = start_code;
941 current->mm->start_data = start_data;
942 current->mm->end_data = end_data;
943 current->mm->start_stack = bprm->p;
944
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100945#ifdef arch_randomize_brk
Ingo Molnar32a93232008-02-06 22:39:44 +0100946 if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1))
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100947 current->mm->brk = current->mm->start_brk =
948 arch_randomize_brk(current->mm);
949#endif
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (current->personality & MMAP_PAGE_ZERO) {
952 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
953 and some applications "depend" upon this behavior.
954 Since we do not have the power to recompile these, we
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700955 emulate the SVr4 behavior. Sigh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 down_write(&current->mm->mmap_sem);
957 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
958 MAP_FIXED | MAP_PRIVATE, 0);
959 up_write(&current->mm->mmap_sem);
960 }
961
962#ifdef ELF_PLAT_INIT
963 /*
964 * The ABI may specify that certain registers be set up in special
965 * ways (on i386 %edx is the address of a DT_FINI function, for
966 * example. In addition, it may also specify (eg, PowerPC64 ELF)
967 * that the e_entry field is the address of the function descriptor
968 * for the startup routine, rather than the address of the startup
969 * routine itself. This macro performs whatever initialization to
970 * the regs structure is required as well as any relocations to the
971 * function descriptor entries when executing dynamically links apps.
972 */
973 ELF_PLAT_INIT(regs, reloc_func_desc);
974#endif
975
976 start_thread(regs, elf_entry, bprm->p);
977 if (unlikely(current->ptrace & PT_PTRACED)) {
978 if (current->ptrace & PT_TRACE_EXEC)
979 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
980 else
981 send_sig(SIGTRAP, current, 0);
982 }
983 retval = 0;
984out:
985 kfree(loc);
986out_ret:
987 return retval;
988
989 /* error cleanup */
990out_free_dentry:
991 allow_write_access(interpreter);
992 if (interpreter)
993 fput(interpreter);
994out_free_interp:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800995 kfree(elf_interpreter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996out_free_file:
997 sys_close(elf_exec_fileno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998out_free_ph:
999 kfree(elf_phdata);
1000 goto out;
1001}
1002
1003/* This is really simpleminded and specialized - we are loading an
1004 a.out library that is given an ELF header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005static int load_elf_library(struct file *file)
1006{
1007 struct elf_phdr *elf_phdata;
1008 struct elf_phdr *eppnt;
1009 unsigned long elf_bss, bss, len;
1010 int retval, error, i, j;
1011 struct elfhdr elf_ex;
1012
1013 error = -ENOEXEC;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001014 retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (retval != sizeof(elf_ex))
1016 goto out;
1017
1018 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1019 goto out;
1020
1021 /* First of all, some simple consistency checks */
1022 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001023 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto out;
1025
1026 /* Now read in all of the header information */
1027
1028 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1029 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1030
1031 error = -ENOMEM;
1032 elf_phdata = kmalloc(j, GFP_KERNEL);
1033 if (!elf_phdata)
1034 goto out;
1035
1036 eppnt = elf_phdata;
1037 error = -ENOEXEC;
1038 retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1039 if (retval != j)
1040 goto out_free_ph;
1041
1042 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1043 if ((eppnt + i)->p_type == PT_LOAD)
1044 j++;
1045 if (j != 1)
1046 goto out_free_ph;
1047
1048 while (eppnt->p_type != PT_LOAD)
1049 eppnt++;
1050
1051 /* Now use mmap to map the library into memory. */
1052 down_write(&current->mm->mmap_sem);
1053 error = do_mmap(file,
1054 ELF_PAGESTART(eppnt->p_vaddr),
1055 (eppnt->p_filesz +
1056 ELF_PAGEOFFSET(eppnt->p_vaddr)),
1057 PROT_READ | PROT_WRITE | PROT_EXEC,
1058 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1059 (eppnt->p_offset -
1060 ELF_PAGEOFFSET(eppnt->p_vaddr)));
1061 up_write(&current->mm->mmap_sem);
1062 if (error != ELF_PAGESTART(eppnt->p_vaddr))
1063 goto out_free_ph;
1064
1065 elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1066 if (padzero(elf_bss)) {
1067 error = -EFAULT;
1068 goto out_free_ph;
1069 }
1070
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001071 len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
1072 ELF_MIN_ALIGN - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 bss = eppnt->p_memsz + eppnt->p_vaddr;
1074 if (bss > len) {
1075 down_write(&current->mm->mmap_sem);
1076 do_brk(len, bss - len);
1077 up_write(&current->mm->mmap_sem);
1078 }
1079 error = 0;
1080
1081out_free_ph:
1082 kfree(elf_phdata);
1083out:
1084 return error;
1085}
1086
1087/*
1088 * Note that some platforms still use traditional core dumps and not
1089 * the ELF core dump. Each platform can select it as appropriate.
1090 */
Matt Mackall708e9a72006-01-08 01:05:25 -08001091#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093/*
1094 * ELF core dumper
1095 *
1096 * Modelled on fs/exec.c:aout_core_dump()
1097 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1098 */
1099/*
1100 * These are the only things you should do on a core-file: use only these
1101 * functions to write out all the necessary info.
1102 */
1103static int dump_write(struct file *file, const void *addr, int nr)
1104{
1105 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1106}
1107
Daniel Jacobowitz5db92852005-06-15 22:26:34 -07001108static int dump_seek(struct file *file, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Andi Kleend025c9d2006-09-30 23:29:28 -07001110 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
Petr Vandrovec7f14daa2006-10-13 04:13:16 +02001111 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return 0;
Andi Kleend025c9d2006-09-30 23:29:28 -07001113 } else {
1114 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
1115 if (!buf)
1116 return 0;
1117 while (off > 0) {
1118 unsigned long n = off;
1119 if (n > PAGE_SIZE)
1120 n = PAGE_SIZE;
1121 if (!dump_write(file, buf, n))
1122 return 0;
1123 off -= n;
1124 }
1125 free_page((unsigned long)buf);
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 1;
1128}
1129
1130/*
Roland McGrath82df3972007-10-16 23:27:02 -07001131 * Decide what to dump of a segment, part, all or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Roland McGrath82df3972007-10-16 23:27:02 -07001133static unsigned long vma_dump_size(struct vm_area_struct *vma,
1134 unsigned long mm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Roland McGrathe5b97dd2007-01-26 00:56:48 -08001136 /* The vma can be set up to tell us the answer directly. */
1137 if (vma->vm_flags & VM_ALWAYSDUMP)
Roland McGrath82df3972007-10-16 23:27:02 -07001138 goto whole;
Roland McGrathe5b97dd2007-01-26 00:56:48 -08001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* Do not dump I/O mapped devices or special mappings */
1141 if (vma->vm_flags & (VM_IO | VM_RESERVED))
1142 return 0;
1143
Roland McGrath82df3972007-10-16 23:27:02 -07001144#define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1145
Kawai, Hidehiroa1b59e82007-07-19 01:48:29 -07001146 /* By default, dump shared memory if mapped from an anonymous file. */
1147 if (vma->vm_flags & VM_SHARED) {
Roland McGrath82df3972007-10-16 23:27:02 -07001148 if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0 ?
1149 FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1150 goto whole;
1151 return 0;
Kawai, Hidehiroa1b59e82007-07-19 01:48:29 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Roland McGrath82df3972007-10-16 23:27:02 -07001154 /* Dump segments that have been written to. */
1155 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1156 goto whole;
1157 if (vma->vm_file == NULL)
1158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Roland McGrath82df3972007-10-16 23:27:02 -07001160 if (FILTER(MAPPED_PRIVATE))
1161 goto whole;
1162
1163 /*
1164 * If this looks like the beginning of a DSO or executable mapping,
1165 * check for an ELF header. If we find one, dump the first page to
1166 * aid in determining what was mapped here.
1167 */
1168 if (FILTER(ELF_HEADERS) && vma->vm_file != NULL && vma->vm_pgoff == 0) {
1169 u32 __user *header = (u32 __user *) vma->vm_start;
1170 u32 word;
1171 /*
1172 * Doing it this way gets the constant folded by GCC.
1173 */
1174 union {
1175 u32 cmp;
1176 char elfmag[SELFMAG];
1177 } magic;
1178 BUILD_BUG_ON(SELFMAG != sizeof word);
1179 magic.elfmag[EI_MAG0] = ELFMAG0;
1180 magic.elfmag[EI_MAG1] = ELFMAG1;
1181 magic.elfmag[EI_MAG2] = ELFMAG2;
1182 magic.elfmag[EI_MAG3] = ELFMAG3;
1183 if (get_user(word, header) == 0 && word == magic.cmp)
1184 return PAGE_SIZE;
1185 }
1186
1187#undef FILTER
1188
1189 return 0;
1190
1191whole:
1192 return vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/* An ELF note in memory */
1196struct memelfnote
1197{
1198 const char *name;
1199 int type;
1200 unsigned int datasz;
1201 void *data;
1202};
1203
1204static int notesize(struct memelfnote *en)
1205{
1206 int sz;
1207
1208 sz = sizeof(struct elf_note);
1209 sz += roundup(strlen(en->name) + 1, 4);
1210 sz += roundup(en->datasz, 4);
1211
1212 return sz;
1213}
1214
Andi Kleend025c9d2006-09-30 23:29:28 -07001215#define DUMP_WRITE(addr, nr, foffset) \
1216 do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Andi Kleend025c9d2006-09-30 23:29:28 -07001218static int alignfile(struct file *file, loff_t *foffset)
1219{
Petr Vandroveca7a0d862006-10-13 18:42:07 +02001220 static const char buf[4] = { 0, };
Andi Kleend025c9d2006-09-30 23:29:28 -07001221 DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
1222 return 1;
1223}
1224
1225static int writenote(struct memelfnote *men, struct file *file,
1226 loff_t *foffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct elf_note en;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 en.n_namesz = strlen(men->name) + 1;
1230 en.n_descsz = men->datasz;
1231 en.n_type = men->type;
1232
Andi Kleend025c9d2006-09-30 23:29:28 -07001233 DUMP_WRITE(&en, sizeof(en), foffset);
1234 DUMP_WRITE(men->name, en.n_namesz, foffset);
1235 if (!alignfile(file, foffset))
1236 return 0;
1237 DUMP_WRITE(men->data, men->datasz, foffset);
1238 if (!alignfile(file, foffset))
1239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 return 1;
1242}
1243#undef DUMP_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245#define DUMP_WRITE(addr, nr) \
1246 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1247 goto end_coredump;
1248#define DUMP_SEEK(off) \
1249 if (!dump_seek(file, (off))) \
1250 goto end_coredump;
1251
Roland McGrath3aba4812008-01-30 13:31:44 +01001252static void fill_elf_header(struct elfhdr *elf, int segs,
1253 u16 machine, u32 flags, u8 osabi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Cyrill Gorcunov6970c8e2008-04-29 01:01:18 -07001255 memset(elf, 0, sizeof(*elf));
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 elf->e_type = ET_CORE;
Roland McGrath3aba4812008-01-30 13:31:44 +01001264 elf->e_machine = machine;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 elf->e_version = EV_CURRENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 elf->e_phoff = sizeof(struct elfhdr);
Roland McGrath3aba4812008-01-30 13:31:44 +01001267 elf->e_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 elf->e_ehsize = sizeof(struct elfhdr);
1269 elf->e_phentsize = sizeof(struct elf_phdr);
1270 elf->e_phnum = segs;
Cyrill Gorcunov6970c8e2008-04-29 01:01:18 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return;
1273}
1274
Andrew Morton8d6b5eee2006-09-25 23:32:04 -07001275static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
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/*
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001299 * fill up all the fields in prstatus from the given task struct, except
1300 * registers which need to be filled up separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
1302static void fill_prstatus(struct elf_prstatus *prstatus,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001303 struct task_struct *p, long signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
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];
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001308 prstatus->pr_pid = task_pid_vnr(p);
Roland McGrath45626bb2008-01-07 14:22:44 -08001309 prstatus->pr_ppid = task_pid_vnr(p->real_parent);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001310 prstatus->pr_pgrp = task_pgrp_vnr(p);
1311 prstatus->pr_sid = task_session_vnr(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (thread_group_leader(p)) {
1313 /*
1314 * This is the record for the group leader. Add in the
1315 * cumulative times of previous dead threads. This total
1316 * won't include the time of each live thread whose state
1317 * is included in the core dump. The final total reported
1318 * to our parent process when it calls wait4 will include
1319 * those sums as well as the little bit more time it takes
1320 * this and each other thread to finish dying after the
1321 * core dump synchronization phase.
1322 */
1323 cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1324 &prstatus->pr_utime);
1325 cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1326 &prstatus->pr_stime);
1327 } else {
1328 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1329 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1330 }
1331 cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1332 cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1333}
1334
1335static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1336 struct mm_struct *mm)
1337{
Greg Kroah-Hartmana84a5052005-05-11 00:10:44 -07001338 unsigned int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /* first copy the parameters from user space */
1341 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1342
1343 len = mm->arg_end - mm->arg_start;
1344 if (len >= ELF_PRARGSZ)
1345 len = ELF_PRARGSZ-1;
1346 if (copy_from_user(&psinfo->pr_psargs,
1347 (const char __user *)mm->arg_start, len))
1348 return -EFAULT;
1349 for(i = 0; i < len; i++)
1350 if (psinfo->pr_psargs[i] == 0)
1351 psinfo->pr_psargs[i] = ' ';
1352 psinfo->pr_psargs[len] = 0;
1353
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001354 psinfo->pr_pid = task_pid_vnr(p);
Roland McGrath45626bb2008-01-07 14:22:44 -08001355 psinfo->pr_ppid = task_pid_vnr(p->real_parent);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001356 psinfo->pr_pgrp = task_pgrp_vnr(p);
1357 psinfo->pr_sid = task_session_vnr(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 i = p->state ? ffz(~p->state) + 1 : 0;
1360 psinfo->pr_state = i;
Carsten Otte55148542006-03-25 03:08:22 -08001361 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1363 psinfo->pr_nice = task_nice(p);
1364 psinfo->pr_flag = p->flags;
1365 SET_UID(psinfo->pr_uid, p->uid);
1366 SET_GID(psinfo->pr_gid, p->gid);
1367 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1368
1369 return 0;
1370}
1371
Roland McGrath3aba4812008-01-30 13:31:44 +01001372static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
1373{
1374 elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv;
1375 int i = 0;
1376 do
1377 i += 2;
1378 while (auxv[i - 2] != AT_NULL);
1379 fill_note(note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv);
1380}
1381
Roland McGrath4206d3a2008-01-30 13:31:45 +01001382#ifdef CORE_DUMP_USE_REGSET
1383#include <linux/regset.h>
1384
1385struct elf_thread_core_info {
1386 struct elf_thread_core_info *next;
1387 struct task_struct *task;
1388 struct elf_prstatus prstatus;
1389 struct memelfnote notes[0];
1390};
1391
1392struct elf_note_info {
1393 struct elf_thread_core_info *thread;
1394 struct memelfnote psinfo;
1395 struct memelfnote auxv;
1396 size_t size;
1397 int thread_notes;
1398};
1399
Roland McGrathd31472b2008-03-04 14:28:30 -08001400/*
1401 * When a regset has a writeback hook, we call it on each thread before
1402 * dumping user memory. On register window machines, this makes sure the
1403 * user memory backing the register data is up to date before we read it.
1404 */
1405static void do_thread_regset_writeback(struct task_struct *task,
1406 const struct user_regset *regset)
1407{
1408 if (regset->writeback)
1409 regset->writeback(task, regset, 1);
1410}
1411
Roland McGrath4206d3a2008-01-30 13:31:45 +01001412static int fill_thread_core_info(struct elf_thread_core_info *t,
1413 const struct user_regset_view *view,
1414 long signr, size_t *total)
1415{
1416 unsigned int i;
1417
1418 /*
1419 * NT_PRSTATUS is the one special case, because the regset data
1420 * goes into the pr_reg field inside the note contents, rather
1421 * than being the whole note contents. We fill the reset in here.
1422 * We assume that regset 0 is NT_PRSTATUS.
1423 */
1424 fill_prstatus(&t->prstatus, t->task, signr);
1425 (void) view->regsets[0].get(t->task, &view->regsets[0],
1426 0, sizeof(t->prstatus.pr_reg),
1427 &t->prstatus.pr_reg, NULL);
1428
1429 fill_note(&t->notes[0], "CORE", NT_PRSTATUS,
1430 sizeof(t->prstatus), &t->prstatus);
1431 *total += notesize(&t->notes[0]);
1432
Roland McGrathd31472b2008-03-04 14:28:30 -08001433 do_thread_regset_writeback(t->task, &view->regsets[0]);
1434
Roland McGrath4206d3a2008-01-30 13:31:45 +01001435 /*
1436 * Each other regset might generate a note too. For each regset
1437 * that has no core_note_type or is inactive, we leave t->notes[i]
1438 * all zero and we'll know to skip writing it later.
1439 */
1440 for (i = 1; i < view->n; ++i) {
1441 const struct user_regset *regset = &view->regsets[i];
Roland McGrathd31472b2008-03-04 14:28:30 -08001442 do_thread_regset_writeback(t->task, regset);
Roland McGrath4206d3a2008-01-30 13:31:45 +01001443 if (regset->core_note_type &&
1444 (!regset->active || regset->active(t->task, regset))) {
1445 int ret;
1446 size_t size = regset->n * regset->size;
1447 void *data = kmalloc(size, GFP_KERNEL);
1448 if (unlikely(!data))
1449 return 0;
1450 ret = regset->get(t->task, regset,
1451 0, size, data, NULL);
1452 if (unlikely(ret))
1453 kfree(data);
1454 else {
1455 if (regset->core_note_type != NT_PRFPREG)
1456 fill_note(&t->notes[i], "LINUX",
1457 regset->core_note_type,
1458 size, data);
1459 else {
1460 t->prstatus.pr_fpvalid = 1;
1461 fill_note(&t->notes[i], "CORE",
1462 NT_PRFPREG, size, data);
1463 }
1464 *total += notesize(&t->notes[i]);
1465 }
1466 }
1467 }
1468
1469 return 1;
1470}
1471
1472static int fill_note_info(struct elfhdr *elf, int phdrs,
1473 struct elf_note_info *info,
1474 long signr, struct pt_regs *regs)
1475{
1476 struct task_struct *dump_task = current;
1477 const struct user_regset_view *view = task_user_regset_view(dump_task);
1478 struct elf_thread_core_info *t;
1479 struct elf_prpsinfo *psinfo;
1480 struct task_struct *g, *p;
1481 unsigned int i;
1482
1483 info->size = 0;
1484 info->thread = NULL;
1485
1486 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1487 fill_note(&info->psinfo, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1488
1489 if (psinfo == NULL)
1490 return 0;
1491
1492 /*
1493 * Figure out how many notes we're going to need for each thread.
1494 */
1495 info->thread_notes = 0;
1496 for (i = 0; i < view->n; ++i)
1497 if (view->regsets[i].core_note_type != 0)
1498 ++info->thread_notes;
1499
1500 /*
1501 * Sanity check. We rely on regset 0 being in NT_PRSTATUS,
1502 * since it is our one special case.
1503 */
1504 if (unlikely(info->thread_notes == 0) ||
1505 unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) {
1506 WARN_ON(1);
1507 return 0;
1508 }
1509
1510 /*
1511 * Initialize the ELF file header.
1512 */
1513 fill_elf_header(elf, phdrs,
1514 view->e_machine, view->e_flags, view->ei_osabi);
1515
1516 /*
1517 * Allocate a structure for each thread.
1518 */
1519 rcu_read_lock();
1520 do_each_thread(g, p)
1521 if (p->mm == dump_task->mm) {
1522 t = kzalloc(offsetof(struct elf_thread_core_info,
1523 notes[info->thread_notes]),
1524 GFP_ATOMIC);
1525 if (unlikely(!t)) {
1526 rcu_read_unlock();
1527 return 0;
1528 }
1529 t->task = p;
1530 if (p == dump_task || !info->thread) {
1531 t->next = info->thread;
1532 info->thread = t;
1533 } else {
1534 /*
1535 * Make sure to keep the original task at
1536 * the head of the list.
1537 */
1538 t->next = info->thread->next;
1539 info->thread->next = t;
1540 }
1541 }
1542 while_each_thread(g, p);
1543 rcu_read_unlock();
1544
1545 /*
1546 * Now fill in each thread's information.
1547 */
1548 for (t = info->thread; t != NULL; t = t->next)
1549 if (!fill_thread_core_info(t, view, signr, &info->size))
1550 return 0;
1551
1552 /*
1553 * Fill in the two process-wide notes.
1554 */
1555 fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm);
1556 info->size += notesize(&info->psinfo);
1557
1558 fill_auxv_note(&info->auxv, current->mm);
1559 info->size += notesize(&info->auxv);
1560
1561 return 1;
1562}
1563
1564static size_t get_note_info_size(struct elf_note_info *info)
1565{
1566 return info->size;
1567}
1568
1569/*
1570 * Write all the notes for each thread. When writing the first thread, the
1571 * process-wide notes are interleaved after the first thread-specific note.
1572 */
1573static int write_note_info(struct elf_note_info *info,
1574 struct file *file, loff_t *foffset)
1575{
1576 bool first = 1;
1577 struct elf_thread_core_info *t = info->thread;
1578
1579 do {
1580 int i;
1581
1582 if (!writenote(&t->notes[0], file, foffset))
1583 return 0;
1584
1585 if (first && !writenote(&info->psinfo, file, foffset))
1586 return 0;
1587 if (first && !writenote(&info->auxv, file, foffset))
1588 return 0;
1589
1590 for (i = 1; i < info->thread_notes; ++i)
1591 if (t->notes[i].data &&
1592 !writenote(&t->notes[i], file, foffset))
1593 return 0;
1594
1595 first = 0;
1596 t = t->next;
1597 } while (t);
1598
1599 return 1;
1600}
1601
1602static void free_note_info(struct elf_note_info *info)
1603{
1604 struct elf_thread_core_info *threads = info->thread;
1605 while (threads) {
1606 unsigned int i;
1607 struct elf_thread_core_info *t = threads;
1608 threads = t->next;
1609 WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus);
1610 for (i = 1; i < info->thread_notes; ++i)
1611 kfree(t->notes[i].data);
1612 kfree(t);
1613 }
1614 kfree(info->psinfo.data);
1615}
1616
1617#else
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619/* Here is the structure in which status of each thread is captured. */
1620struct elf_thread_status
1621{
1622 struct list_head list;
1623 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1624 elf_fpregset_t fpu; /* NT_PRFPREG */
1625 struct task_struct *thread;
1626#ifdef ELF_CORE_COPY_XFPREGS
Mark Nelson5b20cd82007-10-16 23:25:39 -07001627 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628#endif
1629 struct memelfnote notes[3];
1630 int num_notes;
1631};
1632
1633/*
1634 * In order to add the specific thread information for the elf file format,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001635 * we need to keep a linked list of every threads pr_status and then create
1636 * a single section for them in the final core file.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 */
1638static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1639{
1640 int sz = 0;
1641 struct task_struct *p = t->thread;
1642 t->num_notes = 0;
1643
1644 fill_prstatus(&t->prstatus, p, signr);
1645 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1646
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001647 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1648 &(t->prstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 t->num_notes++;
1650 sz += notesize(&t->notes[0]);
1651
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001652 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
1653 &t->fpu))) {
1654 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1655 &(t->fpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 t->num_notes++;
1657 sz += notesize(&t->notes[1]);
1658 }
1659
1660#ifdef ELF_CORE_COPY_XFPREGS
1661 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
Mark Nelson5b20cd82007-10-16 23:25:39 -07001662 fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
1663 sizeof(t->xfpu), &t->xfpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 t->num_notes++;
1665 sz += notesize(&t->notes[2]);
1666 }
1667#endif
1668 return sz;
1669}
1670
Roland McGrath3aba4812008-01-30 13:31:44 +01001671struct elf_note_info {
1672 struct memelfnote *notes;
1673 struct elf_prstatus *prstatus; /* NT_PRSTATUS */
1674 struct elf_prpsinfo *psinfo; /* NT_PRPSINFO */
1675 struct list_head thread_list;
1676 elf_fpregset_t *fpu;
1677#ifdef ELF_CORE_COPY_XFPREGS
1678 elf_fpxregset_t *xfpu;
1679#endif
1680 int thread_status_size;
1681 int numnote;
1682};
1683
1684static int fill_note_info(struct elfhdr *elf, int phdrs,
1685 struct elf_note_info *info,
1686 long signr, struct pt_regs *regs)
1687{
1688#define NUM_NOTES 6
1689 struct list_head *t;
1690 struct task_struct *g, *p;
1691
1692 info->notes = NULL;
1693 info->prstatus = NULL;
1694 info->psinfo = NULL;
1695 info->fpu = NULL;
1696#ifdef ELF_CORE_COPY_XFPREGS
1697 info->xfpu = NULL;
1698#endif
1699 INIT_LIST_HEAD(&info->thread_list);
1700
1701 info->notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote),
1702 GFP_KERNEL);
1703 if (!info->notes)
1704 return 0;
1705 info->psinfo = kmalloc(sizeof(*info->psinfo), GFP_KERNEL);
1706 if (!info->psinfo)
1707 return 0;
1708 info->prstatus = kmalloc(sizeof(*info->prstatus), GFP_KERNEL);
1709 if (!info->prstatus)
1710 return 0;
1711 info->fpu = kmalloc(sizeof(*info->fpu), GFP_KERNEL);
1712 if (!info->fpu)
1713 return 0;
1714#ifdef ELF_CORE_COPY_XFPREGS
1715 info->xfpu = kmalloc(sizeof(*info->xfpu), GFP_KERNEL);
1716 if (!info->xfpu)
1717 return 0;
1718#endif
1719
1720 info->thread_status_size = 0;
1721 if (signr) {
WANG Cong4220b7f2008-04-29 01:01:18 -07001722 struct elf_thread_status *ets;
Roland McGrath3aba4812008-01-30 13:31:44 +01001723 rcu_read_lock();
1724 do_each_thread(g, p)
1725 if (current->mm == p->mm && current != p) {
WANG Cong4220b7f2008-04-29 01:01:18 -07001726 ets = kzalloc(sizeof(*ets), GFP_ATOMIC);
1727 if (!ets) {
Roland McGrath3aba4812008-01-30 13:31:44 +01001728 rcu_read_unlock();
1729 return 0;
1730 }
WANG Cong4220b7f2008-04-29 01:01:18 -07001731 ets->thread = p;
1732 list_add(&ets->list, &info->thread_list);
Roland McGrath3aba4812008-01-30 13:31:44 +01001733 }
1734 while_each_thread(g, p);
1735 rcu_read_unlock();
1736 list_for_each(t, &info->thread_list) {
Roland McGrath3aba4812008-01-30 13:31:44 +01001737 int sz;
1738
WANG Cong4220b7f2008-04-29 01:01:18 -07001739 ets = list_entry(t, struct elf_thread_status, list);
1740 sz = elf_dump_thread_status(signr, ets);
Roland McGrath3aba4812008-01-30 13:31:44 +01001741 info->thread_status_size += sz;
1742 }
1743 }
1744 /* now collect the dump for the current */
1745 memset(info->prstatus, 0, sizeof(*info->prstatus));
1746 fill_prstatus(info->prstatus, current, signr);
1747 elf_core_copy_regs(&info->prstatus->pr_reg, regs);
1748
1749 /* Set up header */
1750 fill_elf_header(elf, phdrs, ELF_ARCH, ELF_CORE_EFLAGS, ELF_OSABI);
1751
1752 /*
1753 * Set up the notes in similar form to SVR4 core dumps made
1754 * with info from their /proc.
1755 */
1756
1757 fill_note(info->notes + 0, "CORE", NT_PRSTATUS,
1758 sizeof(*info->prstatus), info->prstatus);
1759 fill_psinfo(info->psinfo, current->group_leader, current->mm);
1760 fill_note(info->notes + 1, "CORE", NT_PRPSINFO,
1761 sizeof(*info->psinfo), info->psinfo);
1762
1763 info->numnote = 2;
1764
1765 fill_auxv_note(&info->notes[info->numnote++], current->mm);
1766
1767 /* Try to dump the FPU. */
1768 info->prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, regs,
1769 info->fpu);
1770 if (info->prstatus->pr_fpvalid)
1771 fill_note(info->notes + info->numnote++,
1772 "CORE", NT_PRFPREG, sizeof(*info->fpu), info->fpu);
1773#ifdef ELF_CORE_COPY_XFPREGS
1774 if (elf_core_copy_task_xfpregs(current, info->xfpu))
1775 fill_note(info->notes + info->numnote++,
1776 "LINUX", ELF_CORE_XFPREG_TYPE,
1777 sizeof(*info->xfpu), info->xfpu);
1778#endif
1779
1780 return 1;
1781
1782#undef NUM_NOTES
1783}
1784
1785static size_t get_note_info_size(struct elf_note_info *info)
1786{
1787 int sz = 0;
1788 int i;
1789
1790 for (i = 0; i < info->numnote; i++)
1791 sz += notesize(info->notes + i);
1792
1793 sz += info->thread_status_size;
1794
1795 return sz;
1796}
1797
1798static int write_note_info(struct elf_note_info *info,
1799 struct file *file, loff_t *foffset)
1800{
1801 int i;
1802 struct list_head *t;
1803
1804 for (i = 0; i < info->numnote; i++)
1805 if (!writenote(info->notes + i, file, foffset))
1806 return 0;
1807
1808 /* write out the thread status notes section */
1809 list_for_each(t, &info->thread_list) {
1810 struct elf_thread_status *tmp =
1811 list_entry(t, struct elf_thread_status, list);
1812
1813 for (i = 0; i < tmp->num_notes; i++)
1814 if (!writenote(&tmp->notes[i], file, foffset))
1815 return 0;
1816 }
1817
1818 return 1;
1819}
1820
1821static void free_note_info(struct elf_note_info *info)
1822{
1823 while (!list_empty(&info->thread_list)) {
1824 struct list_head *tmp = info->thread_list.next;
1825 list_del(tmp);
1826 kfree(list_entry(tmp, struct elf_thread_status, list));
1827 }
1828
1829 kfree(info->prstatus);
1830 kfree(info->psinfo);
1831 kfree(info->notes);
1832 kfree(info->fpu);
1833#ifdef ELF_CORE_COPY_XFPREGS
1834 kfree(info->xfpu);
1835#endif
1836}
1837
Roland McGrath4206d3a2008-01-30 13:31:45 +01001838#endif
1839
Roland McGrathf47aef52007-01-26 00:56:49 -08001840static struct vm_area_struct *first_vma(struct task_struct *tsk,
1841 struct vm_area_struct *gate_vma)
1842{
1843 struct vm_area_struct *ret = tsk->mm->mmap;
1844
1845 if (ret)
1846 return ret;
1847 return gate_vma;
1848}
1849/*
1850 * Helper function for iterating across a vma list. It ensures that the caller
1851 * will visit `gate_vma' prior to terminating the search.
1852 */
1853static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1854 struct vm_area_struct *gate_vma)
1855{
1856 struct vm_area_struct *ret;
1857
1858 ret = this_vma->vm_next;
1859 if (ret)
1860 return ret;
1861 if (this_vma == gate_vma)
1862 return NULL;
1863 return gate_vma;
1864}
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866/*
1867 * Actual dumper
1868 *
1869 * This is a two-pass process; first we find the offsets of the bits,
1870 * and then they are actually written out. If we run out of core limit
1871 * we just truncate.
1872 */
Neil Horman7dc0b222007-10-16 23:26:34 -07001873static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 int has_dumped = 0;
1876 mm_segment_t fs;
1877 int segs;
1878 size_t size = 0;
Roland McGrathf47aef52007-01-26 00:56:49 -08001879 struct vm_area_struct *vma, *gate_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 struct elfhdr *elf = NULL;
Andi Kleend025c9d2006-09-30 23:29:28 -07001881 loff_t offset = 0, dataoff, foffset;
Kawai, Hidehiroa1b59e82007-07-19 01:48:29 -07001882 unsigned long mm_flags;
Roland McGrath3aba4812008-01-30 13:31:44 +01001883 struct elf_note_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 /*
1886 * We no longer stop all VM operations.
1887 *
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001888 * This is because those proceses that could possibly change map_count
1889 * or the mmap / vma pages are now blocked in do_exit on current
1890 * finishing this core dump.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 *
1892 * Only ptrace can touch these memory addresses, but it doesn't change
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001893 * the map_count or the pages allocated. So no possibility of crashing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 * exists while dumping the mm->vm_next areas to the core file.
1895 */
1896
1897 /* alloc memory for large data structures: too large to be on stack */
1898 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1899 if (!elf)
WANG Cong5f719552008-05-06 12:45:35 +08001900 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 segs = current->mm->map_count;
1903#ifdef ELF_CORE_EXTRA_PHDRS
1904 segs += ELF_CORE_EXTRA_PHDRS;
1905#endif
1906
Roland McGrathf47aef52007-01-26 00:56:49 -08001907 gate_vma = get_gate_vma(current);
1908 if (gate_vma != NULL)
1909 segs++;
1910
Roland McGrath3aba4812008-01-30 13:31:44 +01001911 /*
1912 * Collect all the non-memory information about the process for the
1913 * notes. This also sets up the file header.
1914 */
1915 if (!fill_note_info(elf, segs + 1, /* including notes section */
1916 &info, signr, regs))
1917 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 has_dumped = 1;
1920 current->flags |= PF_DUMPCORE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 fs = get_fs();
1923 set_fs(KERNEL_DS);
1924
1925 DUMP_WRITE(elf, sizeof(*elf));
1926 offset += sizeof(*elf); /* Elf header */
Petr Vandroveca7a0d862006-10-13 18:42:07 +02001927 offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
1928 foffset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* Write notes phdr entry */
1931 {
1932 struct elf_phdr phdr;
Roland McGrath3aba4812008-01-30 13:31:44 +01001933 size_t sz = get_note_info_size(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Michael Ellermane5501492007-09-19 14:38:12 +10001935 sz += elf_coredump_extra_notes_size();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 fill_elf_note_phdr(&phdr, sz, offset);
1938 offset += sz;
1939 DUMP_WRITE(&phdr, sizeof(phdr));
1940 }
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1943
Kawai, Hidehiroa1b59e82007-07-19 01:48:29 -07001944 /*
1945 * We must use the same mm->flags while dumping core to avoid
1946 * inconsistency between the program headers and bodies, otherwise an
1947 * unusable core file can be generated.
1948 */
1949 mm_flags = current->mm->flags;
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* Write program headers for segments dump */
Roland McGrathf47aef52007-01-26 00:56:49 -08001952 for (vma = first_vma(current, gate_vma); vma != NULL;
1953 vma = next_vma(vma, gate_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct elf_phdr phdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 phdr.p_type = PT_LOAD;
1957 phdr.p_offset = offset;
1958 phdr.p_vaddr = vma->vm_start;
1959 phdr.p_paddr = 0;
Roland McGrath82df3972007-10-16 23:27:02 -07001960 phdr.p_filesz = vma_dump_size(vma, mm_flags);
1961 phdr.p_memsz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 offset += phdr.p_filesz;
1963 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001964 if (vma->vm_flags & VM_WRITE)
1965 phdr.p_flags |= PF_W;
1966 if (vma->vm_flags & VM_EXEC)
1967 phdr.p_flags |= PF_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 phdr.p_align = ELF_EXEC_PAGESIZE;
1969
1970 DUMP_WRITE(&phdr, sizeof(phdr));
1971 }
1972
1973#ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1974 ELF_CORE_WRITE_EXTRA_PHDRS;
1975#endif
1976
1977 /* write out the notes section */
Roland McGrath3aba4812008-01-30 13:31:44 +01001978 if (!write_note_info(&info, file, &foffset))
1979 goto end_coredump;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Michael Ellermane5501492007-09-19 14:38:12 +10001981 if (elf_coredump_extra_notes_write(file, &foffset))
1982 goto end_coredump;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001983
Andi Kleend025c9d2006-09-30 23:29:28 -07001984 /* Align to page */
1985 DUMP_SEEK(dataoff - foffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Roland McGrathf47aef52007-01-26 00:56:49 -08001987 for (vma = first_vma(current, gate_vma); vma != NULL;
1988 vma = next_vma(vma, gate_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 unsigned long addr;
Roland McGrath82df3972007-10-16 23:27:02 -07001990 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Roland McGrath82df3972007-10-16 23:27:02 -07001992 end = vma->vm_start + vma_dump_size(vma, mm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Roland McGrath82df3972007-10-16 23:27:02 -07001994 for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001995 struct page *page;
WANG Cong4220b7f2008-04-29 01:01:18 -07001996 struct vm_area_struct *tmp_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
WANG Cong4220b7f2008-04-29 01:01:18 -07001999 &page, &tmp_vma) <= 0) {
Andi Kleend025c9d2006-09-30 23:29:28 -07002000 DUMP_SEEK(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 } else {
Nick Piggin557ed1f2007-10-16 01:24:40 -07002002 if (page == ZERO_PAGE(0)) {
Brian Pomerantz03221702007-04-01 23:49:41 -07002003 if (!dump_seek(file, PAGE_SIZE)) {
2004 page_cache_release(page);
2005 goto end_coredump;
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 } else {
2008 void *kaddr;
WANG Cong4220b7f2008-04-29 01:01:18 -07002009 flush_cache_page(tmp_vma, addr,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07002010 page_to_pfn(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 kaddr = kmap(page);
2012 if ((size += PAGE_SIZE) > limit ||
2013 !dump_write(file, kaddr,
2014 PAGE_SIZE)) {
2015 kunmap(page);
2016 page_cache_release(page);
2017 goto end_coredump;
2018 }
2019 kunmap(page);
2020 }
2021 page_cache_release(page);
2022 }
2023 }
2024 }
2025
2026#ifdef ELF_CORE_WRITE_EXTRA_DATA
2027 ELF_CORE_WRITE_EXTRA_DATA;
2028#endif
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030end_coredump:
2031 set_fs(fs);
2032
2033cleanup:
Roland McGrath3aba4812008-01-30 13:31:44 +01002034 free_note_info(&info);
WANG Cong5f719552008-05-06 12:45:35 +08002035 kfree(elf);
2036out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return has_dumped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
2040#endif /* USE_ELF_CORE_DUMP */
2041
2042static int __init init_elf_binfmt(void)
2043{
2044 return register_binfmt(&elf_format);
2045}
2046
2047static void __exit exit_elf_binfmt(void)
2048{
2049 /* Remove the COFF and ELF loaders. */
2050 unregister_binfmt(&elf_format);
2051}
2052
2053core_initcall(init_elf_binfmt);
2054module_exit(exit_elf_binfmt);
2055MODULE_LICENSE("GPL");