blob: b8bca1ebc1a0899f0e5417c95f66d07b2a209d9b [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>
19#include <linux/a.out.h>
20#include <linux/errno.h>
21#include <linux/signal.h>
22#include <linux/binfmts.h>
23#include <linux/string.h>
24#include <linux/file.h>
25#include <linux/fcntl.h>
26#include <linux/ptrace.h>
27#include <linux/slab.h>
28#include <linux/shm.h>
29#include <linux/personality.h>
30#include <linux/elfcore.h>
31#include <linux/init.h>
32#include <linux/highuid.h>
33#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/compiler.h>
35#include <linux/highmem.h>
36#include <linux/pagemap.h>
37#include <linux/security.h>
38#include <linux/syscalls.h>
39#include <linux/random.h>
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070040#include <linux/elf.h>
Alexey Dobriyan7e80d0d2007-05-08 00:28:59 -070041#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43#include <asm/param.h>
44#include <asm/page.h>
45
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070046static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs);
47static int load_elf_library(struct file *);
Andrew Mortonbb1ad822008-01-30 13:31:07 +010048static unsigned long elf_map(struct file *, unsigned long, struct elf_phdr *,
49 int, int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * If we don't support core dumping, then supply a NULL so we
53 * don't even try.
54 */
Matt Mackall708e9a72006-01-08 01:05:25 -080055#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
Neil Horman7dc0b222007-10-16 23:26:34 -070056static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#else
58#define elf_core_dump NULL
59#endif
60
61#if ELF_EXEC_PAGESIZE > PAGE_SIZE
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070062#define ELF_MIN_ALIGN ELF_EXEC_PAGESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#else
Jesper Juhlf4e5cc22006-06-23 02:05:35 -070064#define ELF_MIN_ALIGN PAGE_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#endif
66
67#ifndef ELF_CORE_EFLAGS
68#define ELF_CORE_EFLAGS 0
69#endif
70
71#define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
72#define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
73#define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
74
75static struct linux_binfmt elf_format = {
76 .module = THIS_MODULE,
77 .load_binary = load_elf_binary,
78 .load_shlib = load_elf_library,
79 .core_dump = elf_core_dump,
Andi Kleen9fbbd4d2007-02-13 13:26:26 +010080 .min_coredump = ELF_EXEC_PAGESIZE,
81 .hasvdso = 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
Andrew Mortond4e3cc32007-07-21 04:37:32 -070084#define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86static int set_brk(unsigned long start, unsigned long end)
87{
88 start = ELF_PAGEALIGN(start);
89 end = ELF_PAGEALIGN(end);
90 if (end > start) {
91 unsigned long addr;
92 down_write(&current->mm->mmap_sem);
93 addr = do_brk(start, end - start);
94 up_write(&current->mm->mmap_sem);
95 if (BAD_ADDR(addr))
96 return addr;
97 }
98 current->mm->start_brk = current->mm->brk = end;
99 return 0;
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* We need to explicitly zero any fractional pages
103 after the data section (i.e. bss). This would
104 contain the junk from the file that should not
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700105 be in memory
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int padzero(unsigned long elf_bss)
108{
109 unsigned long nbyte;
110
111 nbyte = ELF_PAGEOFFSET(elf_bss);
112 if (nbyte) {
113 nbyte = ELF_MIN_ALIGN - nbyte;
114 if (clear_user((void __user *) elf_bss, nbyte))
115 return -EFAULT;
116 }
117 return 0;
118}
119
120/* Let's use some macros to make this stack manipulation a litle clearer */
121#ifdef CONFIG_STACK_GROWSUP
122#define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
123#define STACK_ROUND(sp, items) \
124 ((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700125#define STACK_ALLOC(sp, len) ({ \
126 elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
127 old_sp; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#else
129#define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
130#define STACK_ROUND(sp, items) \
131 (((unsigned long) (sp - items)) &~ 15UL)
132#define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
133#endif
134
135static int
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700136create_elf_tables(struct linux_binprm *bprm, struct elfhdr *exec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int interp_aout, unsigned long load_addr,
138 unsigned long interp_load_addr)
139{
140 unsigned long p = bprm->p;
141 int argc = bprm->argc;
142 int envc = bprm->envc;
143 elf_addr_t __user *argv;
144 elf_addr_t __user *envp;
145 elf_addr_t __user *sp;
146 elf_addr_t __user *u_platform;
147 const char *k_platform = ELF_PLATFORM;
148 int items;
149 elf_addr_t *elf_info;
150 int ei_index = 0;
151 struct task_struct *tsk = current;
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700152 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /*
Franck Bui-Huud68c9d62007-10-16 23:30:24 -0700155 * In some cases (e.g. Hyper-Threading), we want to avoid L1
156 * evictions by the processes running on the same package. One
157 * thing we can do is to shuffle the initial stack for them.
158 */
159
160 p = arch_align_stack(p);
161
162 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * If this architecture has a platform capability string, copy it
164 * to userspace. In some cases (Sparc), this info is impossible
165 * for userspace to get any other way, in others (i386) it is
166 * merely difficult.
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 u_platform = NULL;
169 if (k_platform) {
170 size_t len = strlen(k_platform) + 1;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
173 if (__copy_to_user(u_platform, k_platform, len))
174 return -EFAULT;
175 }
176
177 /* Create the ELF interpreter info */
Jesper Juhl785d5572006-06-23 02:05:35 -0700178 elf_info = (elf_addr_t *)current->mm->saved_auxv;
Olaf Hering4f9a58d2007-10-16 23:30:12 -0700179 /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#define NEW_AUX_ENT(id, val) \
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700181 do { \
Jesper Juhl785d5572006-06-23 02:05:35 -0700182 elf_info[ei_index++] = id; \
183 elf_info[ei_index++] = val; \
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700184 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186#ifdef ARCH_DLINFO
187 /*
188 * ARCH_DLINFO must come first so PPC can do its special alignment of
189 * AUXV.
Olaf Hering4f9a58d2007-10-16 23:30:12 -0700190 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
191 * ARCH_DLINFO changes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193 ARCH_DLINFO;
194#endif
195 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
196 NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
197 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
198 NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700199 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
201 NEW_AUX_ENT(AT_BASE, interp_load_addr);
202 NEW_AUX_ENT(AT_FLAGS, 0);
203 NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
Jesper Juhl785d5572006-06-23 02:05:35 -0700204 NEW_AUX_ENT(AT_UID, tsk->uid);
205 NEW_AUX_ENT(AT_EUID, tsk->euid);
206 NEW_AUX_ENT(AT_GID, tsk->gid);
207 NEW_AUX_ENT(AT_EGID, tsk->egid);
208 NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (k_platform) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700210 NEW_AUX_ENT(AT_PLATFORM,
Jesper Juhl785d5572006-06-23 02:05:35 -0700211 (elf_addr_t)(unsigned long)u_platform);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
Jesper Juhl785d5572006-06-23 02:05:35 -0700214 NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216#undef NEW_AUX_ENT
217 /* AT_NULL is zero; clear the rest too */
218 memset(&elf_info[ei_index], 0,
219 sizeof current->mm->saved_auxv - ei_index * sizeof elf_info[0]);
220
221 /* And advance past the AT_NULL entry. */
222 ei_index += 2;
223
224 sp = STACK_ADD(p, ei_index);
225
226 items = (argc + 1) + (envc + 1);
227 if (interp_aout) {
228 items += 3; /* a.out interpreters require argv & envp too */
229 } else {
230 items += 1; /* ELF interpreters only put argc on the stack */
231 }
232 bprm->p = STACK_ROUND(sp, items);
233
234 /* Point sp at the lowest address on the stack */
235#ifdef CONFIG_STACK_GROWSUP
236 sp = (elf_addr_t __user *)bprm->p - items - ei_index;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700237 bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#else
239 sp = (elf_addr_t __user *)bprm->p;
240#endif
241
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700242
243 /*
244 * Grow the stack manually; some architectures have a limit on how
245 * far ahead a user-space access may be in order to grow the stack.
246 */
247 vma = find_extend_vma(current->mm, bprm->p);
248 if (!vma)
249 return -EFAULT;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Now, let's put argc (and argv, envp if appropriate) on the stack */
252 if (__put_user(argc, sp++))
253 return -EFAULT;
254 if (interp_aout) {
255 argv = sp + 2;
256 envp = argv + argc + 1;
Heiko Carstens841d5fb2006-12-06 20:36:35 -0800257 if (__put_user((elf_addr_t)(unsigned long)argv, sp++) ||
258 __put_user((elf_addr_t)(unsigned long)envp, sp++))
259 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else {
261 argv = sp;
262 envp = argv + argc + 1;
263 }
264
265 /* Populate argv and envp */
Greg Kroah-Hartmana84a5052005-05-11 00:10:44 -0700266 p = current->mm->arg_end = current->mm->arg_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 while (argc-- > 0) {
268 size_t len;
Heiko Carstens841d5fb2006-12-06 20:36:35 -0800269 if (__put_user((elf_addr_t)p, argv++))
270 return -EFAULT;
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700271 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
272 if (!len || len > MAX_ARG_STRLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return 0;
274 p += len;
275 }
276 if (__put_user(0, argv))
277 return -EFAULT;
278 current->mm->arg_end = current->mm->env_start = p;
279 while (envc-- > 0) {
280 size_t len;
Heiko Carstens841d5fb2006-12-06 20:36:35 -0800281 if (__put_user((elf_addr_t)p, envp++))
282 return -EFAULT;
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700283 len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
284 if (!len || len > MAX_ARG_STRLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286 p += len;
287 }
288 if (__put_user(0, envp))
289 return -EFAULT;
290 current->mm->env_end = p;
291
292 /* Put the elf_info on the stack in the right place. */
293 sp = (elf_addr_t __user *)envp + 1;
294 if (copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t)))
295 return -EFAULT;
296 return 0;
297}
298
299#ifndef elf_map
300
301static unsigned long elf_map(struct file *filep, unsigned long addr,
Jiri Kosinacc503c12008-01-30 13:31:07 +0100302 struct elf_phdr *eppnt, int prot, int type,
303 unsigned long total_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 unsigned long map_addr;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100306 unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr);
307 unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr);
308 addr = ELF_PAGESTART(addr);
309 size = ELF_PAGEALIGN(size);
Jan Kratochvil60bfba72007-07-15 23:40:06 -0700310
Andrew Mortond4e3cc32007-07-21 04:37:32 -0700311 /* mmap() will return -EINVAL if given a zero size, but a
312 * segment with zero filesize is perfectly valid */
Jiri Kosinacc503c12008-01-30 13:31:07 +0100313 if (!size)
314 return addr;
315
316 down_write(&current->mm->mmap_sem);
317 /*
318 * total_size is the size of the ELF (interpreter) image.
319 * The _first_ mmap needs to know the full size, otherwise
320 * randomization might put this image into an overlapping
321 * position with the ELF binary image. (since size < total_size)
322 * So we first map the 'big' image - and unmap the remainder at
323 * the end. (which unmap is needed for ELF images with holes.)
324 */
325 if (total_size) {
326 total_size = ELF_PAGEALIGN(total_size);
327 map_addr = do_mmap(filep, addr, total_size, prot, type, off);
328 if (!BAD_ADDR(map_addr))
329 do_munmap(current->mm, map_addr+size, total_size-size);
330 } else
331 map_addr = do_mmap(filep, addr, size, prot, type, off);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 up_write(&current->mm->mmap_sem);
334 return(map_addr);
335}
336
337#endif /* !elf_map */
338
Jiri Kosinacc503c12008-01-30 13:31:07 +0100339static unsigned long total_mapping_size(struct elf_phdr *cmds, int nr)
340{
341 int i, first_idx = -1, last_idx = -1;
342
343 for (i = 0; i < nr; i++) {
344 if (cmds[i].p_type == PT_LOAD) {
345 last_idx = i;
346 if (first_idx == -1)
347 first_idx = i;
348 }
349 }
350 if (first_idx == -1)
351 return 0;
352
353 return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz -
354 ELF_PAGESTART(cmds[first_idx].p_vaddr);
355}
356
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/* This is much more generalized than the library routine read function,
359 so we keep this separate. Technically the library read function
360 is only provided so that we can read a.out libraries that have
361 an ELF header */
362
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700363static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
Jiri Kosinacc503c12008-01-30 13:31:07 +0100364 struct file *interpreter, unsigned long *interp_map_addr,
365 unsigned long no_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct elf_phdr *elf_phdata;
368 struct elf_phdr *eppnt;
369 unsigned long load_addr = 0;
370 int load_addr_set = 0;
371 unsigned long last_bss = 0, elf_bss = 0;
372 unsigned long error = ~0UL;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100373 unsigned long total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int retval, i, size;
375
376 /* First of all, some simple consistency checks */
377 if (interp_elf_ex->e_type != ET_EXEC &&
378 interp_elf_ex->e_type != ET_DYN)
379 goto out;
380 if (!elf_check_arch(interp_elf_ex))
381 goto out;
382 if (!interpreter->f_op || !interpreter->f_op->mmap)
383 goto out;
384
385 /*
386 * If the size of this structure has changed, then punt, since
387 * we will be doing the wrong thing.
388 */
389 if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
390 goto out;
391 if (interp_elf_ex->e_phnum < 1 ||
392 interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
393 goto out;
394
395 /* Now read in all of the header information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
397 if (size > ELF_MIN_ALIGN)
398 goto out;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700399 elf_phdata = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!elf_phdata)
401 goto out;
402
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700403 retval = kernel_read(interpreter, interp_elf_ex->e_phoff,
404 (char *)elf_phdata,size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 error = -EIO;
406 if (retval != size) {
407 if (retval < 0)
408 error = retval;
409 goto out_close;
410 }
411
Jiri Kosinacc503c12008-01-30 13:31:07 +0100412 total_size = total_mapping_size(elf_phdata, interp_elf_ex->e_phnum);
413 if (!total_size) {
414 error = -EINVAL;
415 goto out_close;
416 }
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 eppnt = elf_phdata;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700419 for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
420 if (eppnt->p_type == PT_LOAD) {
421 int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
422 int elf_prot = 0;
423 unsigned long vaddr = 0;
424 unsigned long k, map_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700426 if (eppnt->p_flags & PF_R)
427 elf_prot = PROT_READ;
428 if (eppnt->p_flags & PF_W)
429 elf_prot |= PROT_WRITE;
430 if (eppnt->p_flags & PF_X)
431 elf_prot |= PROT_EXEC;
432 vaddr = eppnt->p_vaddr;
433 if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
434 elf_type |= MAP_FIXED;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100435 else if (no_base && interp_elf_ex->e_type == ET_DYN)
436 load_addr = -vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700438 map_addr = elf_map(interpreter, load_addr + vaddr,
Andrew Mortonbb1ad822008-01-30 13:31:07 +0100439 eppnt, elf_prot, elf_type, total_size);
Jiri Kosinacc503c12008-01-30 13:31:07 +0100440 total_size = 0;
441 if (!*interp_map_addr)
442 *interp_map_addr = map_addr;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700443 error = map_addr;
444 if (BAD_ADDR(map_addr))
445 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700447 if (!load_addr_set &&
448 interp_elf_ex->e_type == ET_DYN) {
449 load_addr = map_addr - ELF_PAGESTART(vaddr);
450 load_addr_set = 1;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700453 /*
454 * Check to see if the section's size will overflow the
455 * allowed task size. Note that p_filesz must always be
456 * <= p_memsize so it's only necessary to check p_memsz.
457 */
458 k = load_addr + eppnt->p_vaddr;
Chuck Ebbertce510592006-07-03 00:24:14 -0700459 if (BAD_ADDR(k) ||
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700460 eppnt->p_filesz > eppnt->p_memsz ||
461 eppnt->p_memsz > TASK_SIZE ||
462 TASK_SIZE - eppnt->p_memsz < k) {
463 error = -ENOMEM;
464 goto out_close;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700467 /*
468 * Find the end of the file mapping for this phdr, and
469 * keep track of the largest address we see for this.
470 */
471 k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
472 if (k > elf_bss)
473 elf_bss = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700475 /*
476 * Do the same thing for the memory mapping - between
477 * elf_bss and last_bss is the bss section.
478 */
479 k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
480 if (k > last_bss)
481 last_bss = k;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
485 /*
486 * Now fill out the bss section. First pad the last page up
487 * to the page boundary, and then perform a mmap to make sure
488 * that there are zero-mapped pages up to and including the
489 * last bss page.
490 */
491 if (padzero(elf_bss)) {
492 error = -EFAULT;
493 goto out_close;
494 }
495
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700496 /* What we have mapped so far */
497 elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* Map the last of the bss segment */
500 if (last_bss > elf_bss) {
501 down_write(&current->mm->mmap_sem);
502 error = do_brk(elf_bss, last_bss - elf_bss);
503 up_write(&current->mm->mmap_sem);
504 if (BAD_ADDR(error))
505 goto out_close;
506 }
507
Jiri Kosinacc503c12008-01-30 13:31:07 +0100508 error = load_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510out_close:
511 kfree(elf_phdata);
512out:
513 return error;
514}
515
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700516static unsigned long load_aout_interp(struct exec *interp_ex,
517 struct file *interpreter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 unsigned long text_data, elf_entry = ~0UL;
520 char __user * addr;
521 loff_t offset;
522
523 current->mm->end_code = interp_ex->a_text;
524 text_data = interp_ex->a_text + interp_ex->a_data;
525 current->mm->end_data = text_data;
526 current->mm->brk = interp_ex->a_bss + text_data;
527
528 switch (N_MAGIC(*interp_ex)) {
529 case OMAGIC:
530 offset = 32;
531 addr = (char __user *)0;
532 break;
533 case ZMAGIC:
534 case QMAGIC:
535 offset = N_TXTOFF(*interp_ex);
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700536 addr = (char __user *)N_TXTADDR(*interp_ex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
538 default:
539 goto out;
540 }
541
542 down_write(&current->mm->mmap_sem);
543 do_brk(0, text_data);
544 up_write(&current->mm->mmap_sem);
545 if (!interpreter->f_op || !interpreter->f_op->read)
546 goto out;
547 if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
548 goto out;
549 flush_icache_range((unsigned long)addr,
550 (unsigned long)addr + text_data);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 down_write(&current->mm->mmap_sem);
553 do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
554 interp_ex->a_bss);
555 up_write(&current->mm->mmap_sem);
556 elf_entry = interp_ex->a_entry;
557
558out:
559 return elf_entry;
560}
561
562/*
563 * These are the functions used to load ELF style executables and shared
564 * libraries. There is no binary dependent code anywhere else.
565 */
566
567#define INTERPRETER_NONE 0
568#define INTERPRETER_AOUT 1
569#define INTERPRETER_ELF 2
570
Andi Kleen913bd902006-03-25 16:29:09 +0100571#ifndef STACK_RND_MASK
James Bottomleyd1cabd632007-03-16 13:38:35 -0800572#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
Andi Kleen913bd902006-03-25 16:29:09 +0100573#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575static unsigned long randomize_stack_top(unsigned long stack_top)
576{
577 unsigned int random_variable = 0;
578
Andi Kleenc16b63e02006-09-26 10:52:28 +0200579 if ((current->flags & PF_RANDOMIZE) &&
580 !(current->personality & ADDR_NO_RANDOMIZE)) {
Andi Kleen913bd902006-03-25 16:29:09 +0100581 random_variable = get_random_int() & STACK_RND_MASK;
582 random_variable <<= PAGE_SHIFT;
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#ifdef CONFIG_STACK_GROWSUP
Andi Kleen913bd902006-03-25 16:29:09 +0100585 return PAGE_ALIGN(stack_top) + random_variable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586#else
Andi Kleen913bd902006-03-25 16:29:09 +0100587 return PAGE_ALIGN(stack_top) - random_variable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588#endif
589}
590
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700591static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct file *interpreter = NULL; /* to shut gcc up */
594 unsigned long load_addr = 0, load_bias = 0;
595 int load_addr_set = 0;
596 char * elf_interpreter = NULL;
597 unsigned int interpreter_type = INTERPRETER_NONE;
598 unsigned char ibcs2_interpreter = 0;
599 unsigned long error;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700600 struct elf_phdr *elf_ppnt, *elf_phdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 unsigned long elf_bss, elf_brk;
602 int elf_exec_fileno;
603 int retval, i;
604 unsigned int size;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100605 unsigned long elf_entry;
606 unsigned long interp_load_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 unsigned long start_code, end_code, start_data, end_data;
608 unsigned long reloc_func_desc = 0;
609 char passed_fileno[6];
610 struct files_struct *files;
David Rientjes8de61e62006-12-06 20:40:16 -0800611 int executable_stack = EXSTACK_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned long def_flags = 0;
613 struct {
614 struct elfhdr elf_ex;
615 struct elfhdr interp_elf_ex;
616 struct exec interp_ex;
617 } *loc;
618
619 loc = kmalloc(sizeof(*loc), GFP_KERNEL);
620 if (!loc) {
621 retval = -ENOMEM;
622 goto out_ret;
623 }
624
625 /* Get the exec-header */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700626 loc->elf_ex = *((struct elfhdr *)bprm->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 retval = -ENOEXEC;
629 /* First of all, some simple consistency checks */
630 if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
631 goto out;
632
633 if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
634 goto out;
635 if (!elf_check_arch(&loc->elf_ex))
636 goto out;
637 if (!bprm->file->f_op||!bprm->file->f_op->mmap)
638 goto out;
639
640 /* Now read in all of the header information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
642 goto out;
643 if (loc->elf_ex.e_phnum < 1 ||
644 loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
645 goto out;
646 size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
647 retval = -ENOMEM;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700648 elf_phdata = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (!elf_phdata)
650 goto out;
651
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700652 retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
653 (char *)elf_phdata, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (retval != size) {
655 if (retval >= 0)
656 retval = -EIO;
657 goto out_free_ph;
658 }
659
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700660 files = current->files; /* Refcounted so ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 retval = unshare_files();
662 if (retval < 0)
663 goto out_free_ph;
664 if (files == current->files) {
665 put_files_struct(files);
666 files = NULL;
667 }
668
669 /* exec will make our files private anyway, but for the a.out
670 loader stuff we need to do it earlier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 retval = get_unused_fd();
672 if (retval < 0)
673 goto out_free_fh;
674 get_file(bprm->file);
675 fd_install(elf_exec_fileno = retval, bprm->file);
676
677 elf_ppnt = elf_phdata;
678 elf_bss = 0;
679 elf_brk = 0;
680
681 start_code = ~0UL;
682 end_code = 0;
683 start_data = 0;
684 end_data = 0;
685
686 for (i = 0; i < loc->elf_ex.e_phnum; i++) {
687 if (elf_ppnt->p_type == PT_INTERP) {
688 /* This is the program interpreter used for
689 * shared libraries - for now assume that this
690 * is an a.out format binary
691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 retval = -ENOEXEC;
693 if (elf_ppnt->p_filesz > PATH_MAX ||
694 elf_ppnt->p_filesz < 2)
695 goto out_free_file;
696
697 retval = -ENOMEM;
Jesper Juhl792db3a2006-01-09 20:54:45 -0800698 elf_interpreter = kmalloc(elf_ppnt->p_filesz,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700699 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (!elf_interpreter)
701 goto out_free_file;
702
703 retval = kernel_read(bprm->file, elf_ppnt->p_offset,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700704 elf_interpreter,
705 elf_ppnt->p_filesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (retval != elf_ppnt->p_filesz) {
707 if (retval >= 0)
708 retval = -EIO;
709 goto out_free_interp;
710 }
711 /* make sure path is NULL terminated */
712 retval = -ENOEXEC;
713 if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
714 goto out_free_interp;
715
716 /* If the program interpreter is one of these two,
717 * then assume an iBCS2 image. Otherwise assume
718 * a native linux image.
719 */
720 if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
721 strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
722 ibcs2_interpreter = 1;
723
724 /*
725 * The early SET_PERSONALITY here is so that the lookup
726 * for the interpreter happens in the namespace of the
727 * to-be-execed image. SET_PERSONALITY can select an
728 * alternate root.
729 *
730 * However, SET_PERSONALITY is NOT allowed to switch
731 * this task into the new images's memory mapping
732 * policy - that is, TASK_SIZE must still evaluate to
733 * that which is appropriate to the execing application.
734 * This is because exit_mmap() needs to have TASK_SIZE
735 * evaluate to the size of the old image.
736 *
737 * So if (say) a 64-bit application is execing a 32-bit
738 * application it is the architecture's responsibility
739 * to defer changing the value of TASK_SIZE until the
740 * switch really is going to happen - do this in
741 * flush_thread(). - akpm
742 */
743 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
744
745 interpreter = open_exec(elf_interpreter);
746 retval = PTR_ERR(interpreter);
747 if (IS_ERR(interpreter))
748 goto out_free_interp;
Alexey Dobriyan1fb84492007-01-26 00:57:16 -0800749
750 /*
751 * If the binary is not readable then enforce
752 * mm->dumpable = 0 regardless of the interpreter's
753 * permissions.
754 */
755 if (file_permission(interpreter, MAY_READ) < 0)
756 bprm->interp_flags |= BINPRM_FLAGS_ENFORCE_NONDUMP;
757
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700758 retval = kernel_read(interpreter, 0, bprm->buf,
759 BINPRM_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (retval != BINPRM_BUF_SIZE) {
761 if (retval >= 0)
762 retval = -EIO;
763 goto out_free_dentry;
764 }
765
766 /* Get the exec headers */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700767 loc->interp_ex = *((struct exec *)bprm->buf);
768 loc->interp_elf_ex = *((struct elfhdr *)bprm->buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
770 }
771 elf_ppnt++;
772 }
773
774 elf_ppnt = elf_phdata;
775 for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
776 if (elf_ppnt->p_type == PT_GNU_STACK) {
777 if (elf_ppnt->p_flags & PF_X)
778 executable_stack = EXSTACK_ENABLE_X;
779 else
780 executable_stack = EXSTACK_DISABLE_X;
781 break;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* Some simple consistency checks for the interpreter */
785 if (elf_interpreter) {
Andi Kleen8e9073e2007-10-16 23:26:48 -0700786 static int warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;
788
789 /* Now figure out which format our binary is */
790 if ((N_MAGIC(loc->interp_ex) != OMAGIC) &&
791 (N_MAGIC(loc->interp_ex) != ZMAGIC) &&
792 (N_MAGIC(loc->interp_ex) != QMAGIC))
793 interpreter_type = INTERPRETER_ELF;
794
795 if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
796 interpreter_type &= ~INTERPRETER_ELF;
797
Andi Kleen8e9073e2007-10-16 23:26:48 -0700798 if (interpreter_type == INTERPRETER_AOUT && warn < 10) {
799 printk(KERN_WARNING "a.out ELF interpreter %s is "
800 "deprecated and will not be supported "
801 "after Linux 2.6.25\n", elf_interpreter);
802 warn++;
803 }
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 retval = -ELIBBAD;
806 if (!interpreter_type)
807 goto out_free_dentry;
808
809 /* Make sure only one type was selected */
810 if ((interpreter_type & INTERPRETER_ELF) &&
811 interpreter_type != INTERPRETER_ELF) {
812 // FIXME - ratelimit this before re-enabling
813 // printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
814 interpreter_type = INTERPRETER_ELF;
815 }
816 /* Verify the interpreter has a valid arch */
817 if ((interpreter_type == INTERPRETER_ELF) &&
818 !elf_check_arch(&loc->interp_elf_ex))
819 goto out_free_dentry;
820 } else {
821 /* Executables without an interpreter also need a personality */
822 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
823 }
824
825 /* OK, we are done with that, now set up the arg stuff,
826 and then start this sucker up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
828 char *passed_p = passed_fileno;
829 sprintf(passed_fileno, "%d", elf_exec_fileno);
830
831 if (elf_interpreter) {
832 retval = copy_strings_kernel(1, &passed_p, bprm);
833 if (retval)
834 goto out_free_dentry;
835 bprm->argc++;
836 }
837 }
838
839 /* Flush all traces of the currently running executable */
840 retval = flush_old_exec(bprm);
841 if (retval)
842 goto out_free_dentry;
843
844 /* Discard our unneeded old files struct */
845 if (files) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 put_files_struct(files);
847 files = NULL;
848 }
849
850 /* OK, This is the point of no return */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 current->flags &= ~PF_FORKNOEXEC;
852 current->mm->def_flags = def_flags;
853
854 /* Do this immediately, since STACK_TOP as used in setup_arg_pages
855 may depend on the personality. */
856 SET_PERSONALITY(loc->elf_ex, ibcs2_interpreter);
857 if (elf_read_implies_exec(loc->elf_ex, executable_stack))
858 current->personality |= READ_IMPLIES_EXEC;
859
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700860 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 current->flags |= PF_RANDOMIZE;
862 arch_pick_mmap_layout(current->mm);
863
864 /* Do this so that we can load the interpreter, if need be. We will
865 change some of these later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 current->mm->free_area_cache = current->mm->mmap_base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700867 current->mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
869 executable_stack);
870 if (retval < 0) {
871 send_sig(SIGKILL, current, 0);
872 goto out_free_dentry;
873 }
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 current->mm->start_stack = bprm->p;
876
877 /* Now we do a little grungy work by mmaping the ELF image into
Jiri Kosinacc503c12008-01-30 13:31:07 +0100878 the correct location in memory. */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700879 for(i = 0, elf_ppnt = elf_phdata;
880 i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 int elf_prot = 0, elf_flags;
882 unsigned long k, vaddr;
883
884 if (elf_ppnt->p_type != PT_LOAD)
885 continue;
886
887 if (unlikely (elf_brk > elf_bss)) {
888 unsigned long nbyte;
889
890 /* There was a PT_LOAD segment with p_memsz > p_filesz
891 before this one. Map anonymous pages, if needed,
892 and clear the area. */
893 retval = set_brk (elf_bss + load_bias,
894 elf_brk + load_bias);
895 if (retval) {
896 send_sig(SIGKILL, current, 0);
897 goto out_free_dentry;
898 }
899 nbyte = ELF_PAGEOFFSET(elf_bss);
900 if (nbyte) {
901 nbyte = ELF_MIN_ALIGN - nbyte;
902 if (nbyte > elf_brk - elf_bss)
903 nbyte = elf_brk - elf_bss;
904 if (clear_user((void __user *)elf_bss +
905 load_bias, nbyte)) {
906 /*
907 * This bss-zeroing can fail if the ELF
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700908 * file specifies odd protections. So
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * we don't check the return value
910 */
911 }
912 }
913 }
914
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700915 if (elf_ppnt->p_flags & PF_R)
916 elf_prot |= PROT_READ;
917 if (elf_ppnt->p_flags & PF_W)
918 elf_prot |= PROT_WRITE;
919 if (elf_ppnt->p_flags & PF_X)
920 elf_prot |= PROT_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700922 elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 vaddr = elf_ppnt->p_vaddr;
925 if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
926 elf_flags |= MAP_FIXED;
927 } else if (loc->elf_ex.e_type == ET_DYN) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700928 /* Try and get dynamic programs out of the way of the
929 * default mmap base, as well as whatever program they
930 * might try to exec. This is because the brk will
931 * follow the loader, and is not movable. */
Jiri Kosinacc503c12008-01-30 13:31:07 +0100932#ifdef CONFIG_X86
933 load_bias = 0;
934#else
Linus Torvalds90cb28e2007-01-06 13:28:21 -0800935 load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
Jiri Kosinacc503c12008-01-30 13:31:07 +0100936#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700939 error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
Andrew Mortonbb1ad822008-01-30 13:31:07 +0100940 elf_prot, elf_flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (BAD_ADDR(error)) {
942 send_sig(SIGKILL, current, 0);
Alexey Kuznetsovb140f2512007-05-08 00:31:57 -0700943 retval = IS_ERR((void *)error) ?
944 PTR_ERR((void*)error) : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 goto out_free_dentry;
946 }
947
948 if (!load_addr_set) {
949 load_addr_set = 1;
950 load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
951 if (loc->elf_ex.e_type == ET_DYN) {
952 load_bias += error -
953 ELF_PAGESTART(load_bias + vaddr);
954 load_addr += load_bias;
955 reloc_func_desc = load_bias;
956 }
957 }
958 k = elf_ppnt->p_vaddr;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700959 if (k < start_code)
960 start_code = k;
961 if (start_data < k)
962 start_data = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /*
965 * Check to see if the section's size will overflow the
966 * allowed task size. Note that p_filesz must always be
967 * <= p_memsz so it is only necessary to check p_memsz.
968 */
Chuck Ebbertce510592006-07-03 00:24:14 -0700969 if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 elf_ppnt->p_memsz > TASK_SIZE ||
971 TASK_SIZE - elf_ppnt->p_memsz < k) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -0700972 /* set_brk can never work. Avoid overflows. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 send_sig(SIGKILL, current, 0);
Alexey Kuznetsovb140f2512007-05-08 00:31:57 -0700974 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto out_free_dentry;
976 }
977
978 k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
979
980 if (k > elf_bss)
981 elf_bss = k;
982 if ((elf_ppnt->p_flags & PF_X) && end_code < k)
983 end_code = k;
984 if (end_data < k)
985 end_data = k;
986 k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
987 if (k > elf_brk)
988 elf_brk = k;
989 }
990
991 loc->elf_ex.e_entry += load_bias;
992 elf_bss += load_bias;
993 elf_brk += load_bias;
994 start_code += load_bias;
995 end_code += load_bias;
996 start_data += load_bias;
997 end_data += load_bias;
998
999 /* Calling set_brk effectively mmaps the pages that we need
1000 * for the bss and break sections. We must do this before
1001 * mapping in the interpreter, to make sure it doesn't wind
1002 * up getting placed where the bss needs to go.
1003 */
1004 retval = set_brk(elf_bss, elf_brk);
1005 if (retval) {
1006 send_sig(SIGKILL, current, 0);
1007 goto out_free_dentry;
1008 }
akpm@osdl.org6de50512005-10-11 08:29:08 -07001009 if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 send_sig(SIGSEGV, current, 0);
1011 retval = -EFAULT; /* Nobody gets to see this, but.. */
1012 goto out_free_dentry;
1013 }
1014
1015 if (elf_interpreter) {
Jiri Kosinacc503c12008-01-30 13:31:07 +01001016 if (interpreter_type == INTERPRETER_AOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 elf_entry = load_aout_interp(&loc->interp_ex,
1018 interpreter);
Jiri Kosinacc503c12008-01-30 13:31:07 +01001019 } else {
1020 unsigned long uninitialized_var(interp_map_addr);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 elf_entry = load_elf_interp(&loc->interp_elf_ex,
1023 interpreter,
Jiri Kosinacc503c12008-01-30 13:31:07 +01001024 &interp_map_addr,
1025 load_bias);
1026 if (!IS_ERR((void *)elf_entry)) {
1027 /*
1028 * load_elf_interp() returns relocation
1029 * adjustment
1030 */
1031 interp_load_addr = elf_entry;
1032 elf_entry += loc->interp_elf_ex.e_entry;
1033 }
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (BAD_ADDR(elf_entry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 force_sig(SIGSEGV, current);
Chuck Ebbertce510592006-07-03 00:24:14 -07001037 retval = IS_ERR((void *)elf_entry) ?
1038 (int)elf_entry : -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 goto out_free_dentry;
1040 }
1041 reloc_func_desc = interp_load_addr;
1042
1043 allow_write_access(interpreter);
1044 fput(interpreter);
1045 kfree(elf_interpreter);
1046 } else {
1047 elf_entry = loc->elf_ex.e_entry;
Suresh Siddha5342fba2006-02-26 04:18:28 +01001048 if (BAD_ADDR(elf_entry)) {
Chuck Ebbertce510592006-07-03 00:24:14 -07001049 force_sig(SIGSEGV, current);
1050 retval = -EINVAL;
Suresh Siddha5342fba2006-02-26 04:18:28 +01001051 goto out_free_dentry;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
1055 kfree(elf_phdata);
1056
1057 if (interpreter_type != INTERPRETER_AOUT)
1058 sys_close(elf_exec_fileno);
1059
1060 set_binfmt(&elf_format);
1061
Benjamin Herrenschmidt547ee842005-04-16 15:24:35 -07001062#ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
1063 retval = arch_setup_additional_pages(bprm, executable_stack);
1064 if (retval < 0) {
1065 send_sig(SIGKILL, current, 0);
Roland McGrath18c8baff2005-04-28 15:17:19 -07001066 goto out;
Benjamin Herrenschmidt547ee842005-04-16 15:24:35 -07001067 }
1068#endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 compute_creds(bprm);
1071 current->flags &= ~PF_FORKNOEXEC;
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001072 retval = create_elf_tables(bprm, &loc->elf_ex,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001073 (interpreter_type == INTERPRETER_AOUT),
1074 load_addr, interp_load_addr);
Ollie Wildb6a2fea2007-07-19 01:48:16 -07001075 if (retval < 0) {
1076 send_sig(SIGKILL, current, 0);
1077 goto out;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* N.B. passed_fileno might not be initialized? */
1080 if (interpreter_type == INTERPRETER_AOUT)
1081 current->mm->arg_start += strlen(passed_fileno) + 1;
1082 current->mm->end_code = end_code;
1083 current->mm->start_code = start_code;
1084 current->mm->start_data = start_data;
1085 current->mm->end_data = end_data;
1086 current->mm->start_stack = bprm->p;
1087
Jiri Kosinac1d171a2008-01-30 13:30:40 +01001088#ifdef arch_randomize_brk
1089 if (current->flags & PF_RANDOMIZE)
1090 current->mm->brk = current->mm->start_brk =
1091 arch_randomize_brk(current->mm);
1092#endif
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (current->personality & MMAP_PAGE_ZERO) {
1095 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
1096 and some applications "depend" upon this behavior.
1097 Since we do not have the power to recompile these, we
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001098 emulate the SVr4 behavior. Sigh. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 down_write(&current->mm->mmap_sem);
1100 error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
1101 MAP_FIXED | MAP_PRIVATE, 0);
1102 up_write(&current->mm->mmap_sem);
1103 }
1104
1105#ifdef ELF_PLAT_INIT
1106 /*
1107 * The ABI may specify that certain registers be set up in special
1108 * ways (on i386 %edx is the address of a DT_FINI function, for
1109 * example. In addition, it may also specify (eg, PowerPC64 ELF)
1110 * that the e_entry field is the address of the function descriptor
1111 * for the startup routine, rather than the address of the startup
1112 * routine itself. This macro performs whatever initialization to
1113 * the regs structure is required as well as any relocations to the
1114 * function descriptor entries when executing dynamically links apps.
1115 */
1116 ELF_PLAT_INIT(regs, reloc_func_desc);
1117#endif
1118
1119 start_thread(regs, elf_entry, bprm->p);
1120 if (unlikely(current->ptrace & PT_PTRACED)) {
1121 if (current->ptrace & PT_TRACE_EXEC)
1122 ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
1123 else
1124 send_sig(SIGTRAP, current, 0);
1125 }
1126 retval = 0;
1127out:
1128 kfree(loc);
1129out_ret:
1130 return retval;
1131
1132 /* error cleanup */
1133out_free_dentry:
1134 allow_write_access(interpreter);
1135 if (interpreter)
1136 fput(interpreter);
1137out_free_interp:
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001138 kfree(elf_interpreter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139out_free_file:
1140 sys_close(elf_exec_fileno);
1141out_free_fh:
Kirill Korotaev3b9b8ab2006-09-29 02:00:05 -07001142 if (files)
1143 reset_files_struct(current, files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144out_free_ph:
1145 kfree(elf_phdata);
1146 goto out;
1147}
1148
1149/* This is really simpleminded and specialized - we are loading an
1150 a.out library that is given an ELF header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151static int load_elf_library(struct file *file)
1152{
1153 struct elf_phdr *elf_phdata;
1154 struct elf_phdr *eppnt;
1155 unsigned long elf_bss, bss, len;
1156 int retval, error, i, j;
1157 struct elfhdr elf_ex;
1158
1159 error = -ENOEXEC;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001160 retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (retval != sizeof(elf_ex))
1162 goto out;
1163
1164 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1165 goto out;
1166
1167 /* First of all, some simple consistency checks */
1168 if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001169 !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto out;
1171
1172 /* Now read in all of the header information */
1173
1174 j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1175 /* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1176
1177 error = -ENOMEM;
1178 elf_phdata = kmalloc(j, GFP_KERNEL);
1179 if (!elf_phdata)
1180 goto out;
1181
1182 eppnt = elf_phdata;
1183 error = -ENOEXEC;
1184 retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1185 if (retval != j)
1186 goto out_free_ph;
1187
1188 for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1189 if ((eppnt + i)->p_type == PT_LOAD)
1190 j++;
1191 if (j != 1)
1192 goto out_free_ph;
1193
1194 while (eppnt->p_type != PT_LOAD)
1195 eppnt++;
1196
1197 /* Now use mmap to map the library into memory. */
1198 down_write(&current->mm->mmap_sem);
1199 error = do_mmap(file,
1200 ELF_PAGESTART(eppnt->p_vaddr),
1201 (eppnt->p_filesz +
1202 ELF_PAGEOFFSET(eppnt->p_vaddr)),
1203 PROT_READ | PROT_WRITE | PROT_EXEC,
1204 MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
1205 (eppnt->p_offset -
1206 ELF_PAGEOFFSET(eppnt->p_vaddr)));
1207 up_write(&current->mm->mmap_sem);
1208 if (error != ELF_PAGESTART(eppnt->p_vaddr))
1209 goto out_free_ph;
1210
1211 elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1212 if (padzero(elf_bss)) {
1213 error = -EFAULT;
1214 goto out_free_ph;
1215 }
1216
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001217 len = ELF_PAGESTART(eppnt->p_filesz + eppnt->p_vaddr +
1218 ELF_MIN_ALIGN - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 bss = eppnt->p_memsz + eppnt->p_vaddr;
1220 if (bss > len) {
1221 down_write(&current->mm->mmap_sem);
1222 do_brk(len, bss - len);
1223 up_write(&current->mm->mmap_sem);
1224 }
1225 error = 0;
1226
1227out_free_ph:
1228 kfree(elf_phdata);
1229out:
1230 return error;
1231}
1232
1233/*
1234 * Note that some platforms still use traditional core dumps and not
1235 * the ELF core dump. Each platform can select it as appropriate.
1236 */
Matt Mackall708e9a72006-01-08 01:05:25 -08001237#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239/*
1240 * ELF core dumper
1241 *
1242 * Modelled on fs/exec.c:aout_core_dump()
1243 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1244 */
1245/*
1246 * These are the only things you should do on a core-file: use only these
1247 * functions to write out all the necessary info.
1248 */
1249static int dump_write(struct file *file, const void *addr, int nr)
1250{
1251 return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
1252}
1253
Daniel Jacobowitz5db92852005-06-15 22:26:34 -07001254static int dump_seek(struct file *file, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Andi Kleend025c9d2006-09-30 23:29:28 -07001256 if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
Petr Vandrovec7f14daa2006-10-13 04:13:16 +02001257 if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return 0;
Andi Kleend025c9d2006-09-30 23:29:28 -07001259 } else {
1260 char *buf = (char *)get_zeroed_page(GFP_KERNEL);
1261 if (!buf)
1262 return 0;
1263 while (off > 0) {
1264 unsigned long n = off;
1265 if (n > PAGE_SIZE)
1266 n = PAGE_SIZE;
1267 if (!dump_write(file, buf, n))
1268 return 0;
1269 off -= n;
1270 }
1271 free_page((unsigned long)buf);
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return 1;
1274}
1275
1276/*
Roland McGrath82df3972007-10-16 23:27:02 -07001277 * Decide what to dump of a segment, part, all or none.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 */
Roland McGrath82df3972007-10-16 23:27:02 -07001279static unsigned long vma_dump_size(struct vm_area_struct *vma,
1280 unsigned long mm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Roland McGrathe5b97dd2007-01-26 00:56:48 -08001282 /* The vma can be set up to tell us the answer directly. */
1283 if (vma->vm_flags & VM_ALWAYSDUMP)
Roland McGrath82df3972007-10-16 23:27:02 -07001284 goto whole;
Roland McGrathe5b97dd2007-01-26 00:56:48 -08001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /* Do not dump I/O mapped devices or special mappings */
1287 if (vma->vm_flags & (VM_IO | VM_RESERVED))
1288 return 0;
1289
Roland McGrath82df3972007-10-16 23:27:02 -07001290#define FILTER(type) (mm_flags & (1UL << MMF_DUMP_##type))
1291
Kawai, Hidehiroa1b59e82007-07-19 01:48:29 -07001292 /* By default, dump shared memory if mapped from an anonymous file. */
1293 if (vma->vm_flags & VM_SHARED) {
Roland McGrath82df3972007-10-16 23:27:02 -07001294 if (vma->vm_file->f_path.dentry->d_inode->i_nlink == 0 ?
1295 FILTER(ANON_SHARED) : FILTER(MAPPED_SHARED))
1296 goto whole;
1297 return 0;
Kawai, Hidehiroa1b59e82007-07-19 01:48:29 -07001298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Roland McGrath82df3972007-10-16 23:27:02 -07001300 /* Dump segments that have been written to. */
1301 if (vma->anon_vma && FILTER(ANON_PRIVATE))
1302 goto whole;
1303 if (vma->vm_file == NULL)
1304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Roland McGrath82df3972007-10-16 23:27:02 -07001306 if (FILTER(MAPPED_PRIVATE))
1307 goto whole;
1308
1309 /*
1310 * If this looks like the beginning of a DSO or executable mapping,
1311 * check for an ELF header. If we find one, dump the first page to
1312 * aid in determining what was mapped here.
1313 */
1314 if (FILTER(ELF_HEADERS) && vma->vm_file != NULL && vma->vm_pgoff == 0) {
1315 u32 __user *header = (u32 __user *) vma->vm_start;
1316 u32 word;
1317 /*
1318 * Doing it this way gets the constant folded by GCC.
1319 */
1320 union {
1321 u32 cmp;
1322 char elfmag[SELFMAG];
1323 } magic;
1324 BUILD_BUG_ON(SELFMAG != sizeof word);
1325 magic.elfmag[EI_MAG0] = ELFMAG0;
1326 magic.elfmag[EI_MAG1] = ELFMAG1;
1327 magic.elfmag[EI_MAG2] = ELFMAG2;
1328 magic.elfmag[EI_MAG3] = ELFMAG3;
1329 if (get_user(word, header) == 0 && word == magic.cmp)
1330 return PAGE_SIZE;
1331 }
1332
1333#undef FILTER
1334
1335 return 0;
1336
1337whole:
1338 return vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/* An ELF note in memory */
1342struct memelfnote
1343{
1344 const char *name;
1345 int type;
1346 unsigned int datasz;
1347 void *data;
1348};
1349
1350static int notesize(struct memelfnote *en)
1351{
1352 int sz;
1353
1354 sz = sizeof(struct elf_note);
1355 sz += roundup(strlen(en->name) + 1, 4);
1356 sz += roundup(en->datasz, 4);
1357
1358 return sz;
1359}
1360
Andi Kleend025c9d2006-09-30 23:29:28 -07001361#define DUMP_WRITE(addr, nr, foffset) \
1362 do { if (!dump_write(file, (addr), (nr))) return 0; *foffset += (nr); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Andi Kleend025c9d2006-09-30 23:29:28 -07001364static int alignfile(struct file *file, loff_t *foffset)
1365{
Petr Vandroveca7a0d862006-10-13 18:42:07 +02001366 static const char buf[4] = { 0, };
Andi Kleend025c9d2006-09-30 23:29:28 -07001367 DUMP_WRITE(buf, roundup(*foffset, 4) - *foffset, foffset);
1368 return 1;
1369}
1370
1371static int writenote(struct memelfnote *men, struct file *file,
1372 loff_t *foffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct elf_note en;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 en.n_namesz = strlen(men->name) + 1;
1376 en.n_descsz = men->datasz;
1377 en.n_type = men->type;
1378
Andi Kleend025c9d2006-09-30 23:29:28 -07001379 DUMP_WRITE(&en, sizeof(en), foffset);
1380 DUMP_WRITE(men->name, en.n_namesz, foffset);
1381 if (!alignfile(file, foffset))
1382 return 0;
1383 DUMP_WRITE(men->data, men->datasz, foffset);
1384 if (!alignfile(file, foffset))
1385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 return 1;
1388}
1389#undef DUMP_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391#define DUMP_WRITE(addr, nr) \
1392 if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
1393 goto end_coredump;
1394#define DUMP_SEEK(off) \
1395 if (!dump_seek(file, (off))) \
1396 goto end_coredump;
1397
Arjan van de Ven858119e2006-01-14 13:20:43 -08001398static void fill_elf_header(struct elfhdr *elf, int segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1401 elf->e_ident[EI_CLASS] = ELF_CLASS;
1402 elf->e_ident[EI_DATA] = ELF_DATA;
1403 elf->e_ident[EI_VERSION] = EV_CURRENT;
1404 elf->e_ident[EI_OSABI] = ELF_OSABI;
1405 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1406
1407 elf->e_type = ET_CORE;
1408 elf->e_machine = ELF_ARCH;
1409 elf->e_version = EV_CURRENT;
1410 elf->e_entry = 0;
1411 elf->e_phoff = sizeof(struct elfhdr);
1412 elf->e_shoff = 0;
1413 elf->e_flags = ELF_CORE_EFLAGS;
1414 elf->e_ehsize = sizeof(struct elfhdr);
1415 elf->e_phentsize = sizeof(struct elf_phdr);
1416 elf->e_phnum = segs;
1417 elf->e_shentsize = 0;
1418 elf->e_shnum = 0;
1419 elf->e_shstrndx = 0;
1420 return;
1421}
1422
Andrew Morton8d6b5eee2006-09-25 23:32:04 -07001423static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 phdr->p_type = PT_NOTE;
1426 phdr->p_offset = offset;
1427 phdr->p_vaddr = 0;
1428 phdr->p_paddr = 0;
1429 phdr->p_filesz = sz;
1430 phdr->p_memsz = 0;
1431 phdr->p_flags = 0;
1432 phdr->p_align = 0;
1433 return;
1434}
1435
1436static void fill_note(struct memelfnote *note, const char *name, int type,
1437 unsigned int sz, void *data)
1438{
1439 note->name = name;
1440 note->type = type;
1441 note->datasz = sz;
1442 note->data = data;
1443 return;
1444}
1445
1446/*
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001447 * fill up all the fields in prstatus from the given task struct, except
1448 * registers which need to be filled up separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
1450static void fill_prstatus(struct elf_prstatus *prstatus,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001451 struct task_struct *p, long signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1454 prstatus->pr_sigpend = p->pending.signal.sig[0];
1455 prstatus->pr_sighold = p->blocked.sig[0];
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001456 prstatus->pr_pid = task_pid_vnr(p);
Roland McGrath45626bb2008-01-07 14:22:44 -08001457 prstatus->pr_ppid = task_pid_vnr(p->real_parent);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001458 prstatus->pr_pgrp = task_pgrp_vnr(p);
1459 prstatus->pr_sid = task_session_vnr(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (thread_group_leader(p)) {
1461 /*
1462 * This is the record for the group leader. Add in the
1463 * cumulative times of previous dead threads. This total
1464 * won't include the time of each live thread whose state
1465 * is included in the core dump. The final total reported
1466 * to our parent process when it calls wait4 will include
1467 * those sums as well as the little bit more time it takes
1468 * this and each other thread to finish dying after the
1469 * core dump synchronization phase.
1470 */
1471 cputime_to_timeval(cputime_add(p->utime, p->signal->utime),
1472 &prstatus->pr_utime);
1473 cputime_to_timeval(cputime_add(p->stime, p->signal->stime),
1474 &prstatus->pr_stime);
1475 } else {
1476 cputime_to_timeval(p->utime, &prstatus->pr_utime);
1477 cputime_to_timeval(p->stime, &prstatus->pr_stime);
1478 }
1479 cputime_to_timeval(p->signal->cutime, &prstatus->pr_cutime);
1480 cputime_to_timeval(p->signal->cstime, &prstatus->pr_cstime);
1481}
1482
1483static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1484 struct mm_struct *mm)
1485{
Greg Kroah-Hartmana84a5052005-05-11 00:10:44 -07001486 unsigned int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /* first copy the parameters from user space */
1489 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1490
1491 len = mm->arg_end - mm->arg_start;
1492 if (len >= ELF_PRARGSZ)
1493 len = ELF_PRARGSZ-1;
1494 if (copy_from_user(&psinfo->pr_psargs,
1495 (const char __user *)mm->arg_start, len))
1496 return -EFAULT;
1497 for(i = 0; i < len; i++)
1498 if (psinfo->pr_psargs[i] == 0)
1499 psinfo->pr_psargs[i] = ' ';
1500 psinfo->pr_psargs[len] = 0;
1501
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001502 psinfo->pr_pid = task_pid_vnr(p);
Roland McGrath45626bb2008-01-07 14:22:44 -08001503 psinfo->pr_ppid = task_pid_vnr(p->real_parent);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001504 psinfo->pr_pgrp = task_pgrp_vnr(p);
1505 psinfo->pr_sid = task_session_vnr(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 i = p->state ? ffz(~p->state) + 1 : 0;
1508 psinfo->pr_state = i;
Carsten Otte55148542006-03-25 03:08:22 -08001509 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1511 psinfo->pr_nice = task_nice(p);
1512 psinfo->pr_flag = p->flags;
1513 SET_UID(psinfo->pr_uid, p->uid);
1514 SET_GID(psinfo->pr_gid, p->gid);
1515 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1516
1517 return 0;
1518}
1519
1520/* Here is the structure in which status of each thread is captured. */
1521struct elf_thread_status
1522{
1523 struct list_head list;
1524 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1525 elf_fpregset_t fpu; /* NT_PRFPREG */
1526 struct task_struct *thread;
1527#ifdef ELF_CORE_COPY_XFPREGS
Mark Nelson5b20cd82007-10-16 23:25:39 -07001528 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529#endif
1530 struct memelfnote notes[3];
1531 int num_notes;
1532};
1533
1534/*
1535 * In order to add the specific thread information for the elf file format,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001536 * we need to keep a linked list of every threads pr_status and then create
1537 * a single section for them in the final core file.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
1539static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1540{
1541 int sz = 0;
1542 struct task_struct *p = t->thread;
1543 t->num_notes = 0;
1544
1545 fill_prstatus(&t->prstatus, p, signr);
1546 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1547
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001548 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1549 &(t->prstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 t->num_notes++;
1551 sz += notesize(&t->notes[0]);
1552
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001553 if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
1554 &t->fpu))) {
1555 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1556 &(t->fpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 t->num_notes++;
1558 sz += notesize(&t->notes[1]);
1559 }
1560
1561#ifdef ELF_CORE_COPY_XFPREGS
1562 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
Mark Nelson5b20cd82007-10-16 23:25:39 -07001563 fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
1564 sizeof(t->xfpu), &t->xfpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 t->num_notes++;
1566 sz += notesize(&t->notes[2]);
1567 }
1568#endif
1569 return sz;
1570}
1571
Roland McGrathf47aef52007-01-26 00:56:49 -08001572static struct vm_area_struct *first_vma(struct task_struct *tsk,
1573 struct vm_area_struct *gate_vma)
1574{
1575 struct vm_area_struct *ret = tsk->mm->mmap;
1576
1577 if (ret)
1578 return ret;
1579 return gate_vma;
1580}
1581/*
1582 * Helper function for iterating across a vma list. It ensures that the caller
1583 * will visit `gate_vma' prior to terminating the search.
1584 */
1585static struct vm_area_struct *next_vma(struct vm_area_struct *this_vma,
1586 struct vm_area_struct *gate_vma)
1587{
1588 struct vm_area_struct *ret;
1589
1590 ret = this_vma->vm_next;
1591 if (ret)
1592 return ret;
1593 if (this_vma == gate_vma)
1594 return NULL;
1595 return gate_vma;
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598/*
1599 * Actual dumper
1600 *
1601 * This is a two-pass process; first we find the offsets of the bits,
1602 * and then they are actually written out. If we run out of core limit
1603 * we just truncate.
1604 */
Neil Horman7dc0b222007-10-16 23:26:34 -07001605static int elf_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607#define NUM_NOTES 6
1608 int has_dumped = 0;
1609 mm_segment_t fs;
1610 int segs;
1611 size_t size = 0;
1612 int i;
Roland McGrathf47aef52007-01-26 00:56:49 -08001613 struct vm_area_struct *vma, *gate_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct elfhdr *elf = NULL;
Andi Kleend025c9d2006-09-30 23:29:28 -07001615 loff_t offset = 0, dataoff, foffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 int numnote;
1617 struct memelfnote *notes = NULL;
1618 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1619 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1620 struct task_struct *g, *p;
1621 LIST_HEAD(thread_list);
1622 struct list_head *t;
1623 elf_fpregset_t *fpu = NULL;
1624#ifdef ELF_CORE_COPY_XFPREGS
1625 elf_fpxregset_t *xfpu = NULL;
1626#endif
1627 int thread_status_size = 0;
1628 elf_addr_t *auxv;
Kawai, Hidehiroa1b59e82007-07-19 01:48:29 -07001629 unsigned long mm_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /*
1632 * We no longer stop all VM operations.
1633 *
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001634 * This is because those proceses that could possibly change map_count
1635 * or the mmap / vma pages are now blocked in do_exit on current
1636 * finishing this core dump.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 *
1638 * Only ptrace can touch these memory addresses, but it doesn't change
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001639 * the map_count or the pages allocated. So no possibility of crashing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * exists while dumping the mm->vm_next areas to the core file.
1641 */
1642
1643 /* alloc memory for large data structures: too large to be on stack */
1644 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1645 if (!elf)
1646 goto cleanup;
1647 prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
1648 if (!prstatus)
1649 goto cleanup;
1650 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1651 if (!psinfo)
1652 goto cleanup;
1653 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1654 if (!notes)
1655 goto cleanup;
1656 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1657 if (!fpu)
1658 goto cleanup;
1659#ifdef ELF_CORE_COPY_XFPREGS
1660 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1661 if (!xfpu)
1662 goto cleanup;
1663#endif
1664
1665 if (signr) {
1666 struct elf_thread_status *tmp;
Oleg Nesterov486ccb02006-09-29 02:00:24 -07001667 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 do_each_thread(g,p)
1669 if (current->mm == p->mm && current != p) {
Oliver Neukum11b0b5a2006-03-25 03:08:13 -08001670 tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (!tmp) {
Oleg Nesterov486ccb02006-09-29 02:00:24 -07001672 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto cleanup;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 tmp->thread = p;
1676 list_add(&tmp->list, &thread_list);
1677 }
1678 while_each_thread(g,p);
Oleg Nesterov486ccb02006-09-29 02:00:24 -07001679 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 list_for_each(t, &thread_list) {
1681 struct elf_thread_status *tmp;
1682 int sz;
1683
1684 tmp = list_entry(t, struct elf_thread_status, list);
1685 sz = elf_dump_thread_status(signr, tmp);
1686 thread_status_size += sz;
1687 }
1688 }
1689 /* now collect the dump for the current */
1690 memset(prstatus, 0, sizeof(*prstatus));
1691 fill_prstatus(prstatus, current, signr);
1692 elf_core_copy_regs(&prstatus->pr_reg, regs);
1693
1694 segs = current->mm->map_count;
1695#ifdef ELF_CORE_EXTRA_PHDRS
1696 segs += ELF_CORE_EXTRA_PHDRS;
1697#endif
1698
Roland McGrathf47aef52007-01-26 00:56:49 -08001699 gate_vma = get_gate_vma(current);
1700 if (gate_vma != NULL)
1701 segs++;
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /* Set up header */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001704 fill_elf_header(elf, segs + 1); /* including notes section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 has_dumped = 1;
1707 current->flags |= PF_DUMPCORE;
1708
1709 /*
1710 * Set up the notes in similar form to SVR4 core dumps made
1711 * with info from their /proc.
1712 */
1713
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001714 fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 fill_psinfo(psinfo, current->group_leader, current->mm);
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001716 fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Eric W. Biedermana9289722005-10-30 15:02:08 -08001718 numnote = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001720 auxv = (elf_addr_t *)current->mm->saved_auxv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 i = 0;
1723 do
1724 i += 2;
1725 while (auxv[i - 2] != AT_NULL);
1726 fill_note(&notes[numnote++], "CORE", NT_AUXV,
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001727 i * sizeof(elf_addr_t), auxv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 /* Try to dump the FPU. */
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001730 if ((prstatus->pr_fpvalid =
1731 elf_core_copy_task_fpregs(current, regs, fpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 fill_note(notes + numnote++,
1733 "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1734#ifdef ELF_CORE_COPY_XFPREGS
1735 if (elf_core_copy_task_xfpregs(current, xfpu))
1736 fill_note(notes + numnote++,
Mark Nelson5b20cd82007-10-16 23:25:39 -07001737 "LINUX", ELF_CORE_XFPREG_TYPE, sizeof(*xfpu), xfpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738#endif
1739
1740 fs = get_fs();
1741 set_fs(KERNEL_DS);
1742
1743 DUMP_WRITE(elf, sizeof(*elf));
1744 offset += sizeof(*elf); /* Elf header */
Petr Vandroveca7a0d862006-10-13 18:42:07 +02001745 offset += (segs + 1) * sizeof(struct elf_phdr); /* Program headers */
1746 foffset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* Write notes phdr entry */
1749 {
1750 struct elf_phdr phdr;
1751 int sz = 0;
1752
1753 for (i = 0; i < numnote; i++)
1754 sz += notesize(notes + i);
1755
1756 sz += thread_status_size;
1757
Michael Ellermane5501492007-09-19 14:38:12 +10001758 sz += elf_coredump_extra_notes_size();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 fill_elf_note_phdr(&phdr, sz, offset);
1761 offset += sz;
1762 DUMP_WRITE(&phdr, sizeof(phdr));
1763 }
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1766
Kawai, Hidehiroa1b59e82007-07-19 01:48:29 -07001767 /*
1768 * We must use the same mm->flags while dumping core to avoid
1769 * inconsistency between the program headers and bodies, otherwise an
1770 * unusable core file can be generated.
1771 */
1772 mm_flags = current->mm->flags;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* Write program headers for segments dump */
Roland McGrathf47aef52007-01-26 00:56:49 -08001775 for (vma = first_vma(current, gate_vma); vma != NULL;
1776 vma = next_vma(vma, gate_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct elf_phdr phdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 phdr.p_type = PT_LOAD;
1780 phdr.p_offset = offset;
1781 phdr.p_vaddr = vma->vm_start;
1782 phdr.p_paddr = 0;
Roland McGrath82df3972007-10-16 23:27:02 -07001783 phdr.p_filesz = vma_dump_size(vma, mm_flags);
1784 phdr.p_memsz = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 offset += phdr.p_filesz;
1786 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001787 if (vma->vm_flags & VM_WRITE)
1788 phdr.p_flags |= PF_W;
1789 if (vma->vm_flags & VM_EXEC)
1790 phdr.p_flags |= PF_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 phdr.p_align = ELF_EXEC_PAGESIZE;
1792
1793 DUMP_WRITE(&phdr, sizeof(phdr));
1794 }
1795
1796#ifdef ELF_CORE_WRITE_EXTRA_PHDRS
1797 ELF_CORE_WRITE_EXTRA_PHDRS;
1798#endif
1799
1800 /* write out the notes section */
1801 for (i = 0; i < numnote; i++)
Andi Kleend025c9d2006-09-30 23:29:28 -07001802 if (!writenote(notes + i, file, &foffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto end_coredump;
1804
Michael Ellermane5501492007-09-19 14:38:12 +10001805 if (elf_coredump_extra_notes_write(file, &foffset))
1806 goto end_coredump;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +01001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /* write out the thread status notes section */
1809 list_for_each(t, &thread_list) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001810 struct elf_thread_status *tmp =
1811 list_entry(t, struct elf_thread_status, list);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 for (i = 0; i < tmp->num_notes; i++)
Andi Kleend025c9d2006-09-30 23:29:28 -07001814 if (!writenote(&tmp->notes[i], file, &foffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 goto end_coredump;
1816 }
Andi Kleend025c9d2006-09-30 23:29:28 -07001817
1818 /* Align to page */
1819 DUMP_SEEK(dataoff - foffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Roland McGrathf47aef52007-01-26 00:56:49 -08001821 for (vma = first_vma(current, gate_vma); vma != NULL;
1822 vma = next_vma(vma, gate_vma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned long addr;
Roland McGrath82df3972007-10-16 23:27:02 -07001824 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Roland McGrath82df3972007-10-16 23:27:02 -07001826 end = vma->vm_start + vma_dump_size(vma, mm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Roland McGrath82df3972007-10-16 23:27:02 -07001828 for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001829 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 struct vm_area_struct *vma;
1831
1832 if (get_user_pages(current, current->mm, addr, 1, 0, 1,
1833 &page, &vma) <= 0) {
Andi Kleend025c9d2006-09-30 23:29:28 -07001834 DUMP_SEEK(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 } else {
Nick Piggin557ed1f2007-10-16 01:24:40 -07001836 if (page == ZERO_PAGE(0)) {
Brian Pomerantz03221702007-04-01 23:49:41 -07001837 if (!dump_seek(file, PAGE_SIZE)) {
1838 page_cache_release(page);
1839 goto end_coredump;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 } else {
1842 void *kaddr;
Jesper Juhlf4e5cc22006-06-23 02:05:35 -07001843 flush_cache_page(vma, addr,
1844 page_to_pfn(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 kaddr = kmap(page);
1846 if ((size += PAGE_SIZE) > limit ||
1847 !dump_write(file, kaddr,
1848 PAGE_SIZE)) {
1849 kunmap(page);
1850 page_cache_release(page);
1851 goto end_coredump;
1852 }
1853 kunmap(page);
1854 }
1855 page_cache_release(page);
1856 }
1857 }
1858 }
1859
1860#ifdef ELF_CORE_WRITE_EXTRA_DATA
1861 ELF_CORE_WRITE_EXTRA_DATA;
1862#endif
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864end_coredump:
1865 set_fs(fs);
1866
1867cleanup:
Jesper Juhl74da6cd2006-01-11 01:51:26 +01001868 while (!list_empty(&thread_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct list_head *tmp = thread_list.next;
1870 list_del(tmp);
1871 kfree(list_entry(tmp, struct elf_thread_status, list));
1872 }
1873
1874 kfree(elf);
1875 kfree(prstatus);
1876 kfree(psinfo);
1877 kfree(notes);
1878 kfree(fpu);
1879#ifdef ELF_CORE_COPY_XFPREGS
1880 kfree(xfpu);
1881#endif
1882 return has_dumped;
1883#undef NUM_NOTES
1884}
1885
1886#endif /* USE_ELF_CORE_DUMP */
1887
1888static int __init init_elf_binfmt(void)
1889{
1890 return register_binfmt(&elf_format);
1891}
1892
1893static void __exit exit_elf_binfmt(void)
1894{
1895 /* Remove the COFF and ELF loaders. */
1896 unregister_binfmt(&elf_format);
1897}
1898
1899core_initcall(init_elf_binfmt);
1900module_exit(exit_elf_binfmt);
1901MODULE_LICENSE("GPL");