blob: 6a56dea1387661d420903591b4be2c508c72c76d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* binfmt_elf_fdpic.c: FDPIC ELF binary format
2 *
David Howells8a2ab7f2006-07-10 04:44:53 -07003 * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 * Derived from binfmt_elf.c
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/module.h>
14
15#include <linux/fs.h>
16#include <linux/stat.h>
17#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010018#include <linux/sched/coredump.h>
19#include <linux/sched/task_stack.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010020#include <linux/sched/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/mman.h>
23#include <linux/errno.h>
24#include <linux/signal.h>
25#include <linux/binfmts.h>
26#include <linux/string.h>
27#include <linux/file.h>
28#include <linux/fcntl.h>
29#include <linux/slab.h>
David Howells6d8c4e32006-07-10 04:44:55 -070030#include <linux/pagemap.h>
Paul Mundt5edc2a52008-10-15 22:04:16 -070031#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/highmem.h>
David Howells6d8c4e32006-07-10 04:44:55 -070033#include <linux/highuid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/personality.h>
35#include <linux/ptrace.h>
36#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/elf.h>
38#include <linux/elf-fdpic.h>
39#include <linux/elfcore.h>
Daisuke HATAYAMA088e7af2010-03-05 13:44:06 -080040#include <linux/coredump.h>
Ross Zwislerab27a8d2015-10-05 16:33:37 -060041#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080043#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/param.h>
45#include <asm/pgalloc.h>
46
47typedef char *elf_caddr_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#if 0
50#define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
51#else
52#define kdebug(fmt, ...) do {} while(0)
53#endif
54
David Howells6d8c4e32006-07-10 04:44:55 -070055#if 0
56#define kdcore(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
57#else
58#define kdcore(fmt, ...) do {} while(0)
59#endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061MODULE_LICENSE("GPL");
62
Al Viro71613c32012-10-20 22:00:48 -040063static int load_elf_fdpic_binary(struct linux_binprm *);
David Howells8a2ab7f2006-07-10 04:44:53 -070064static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
65static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
66 struct mm_struct *, const char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
David Howells8a2ab7f2006-07-10 04:44:53 -070068static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
69 struct elf_fdpic_params *,
70 struct elf_fdpic_params *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#ifndef CONFIG_MMU
David Howells8a2ab7f2006-07-10 04:44:53 -070073static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
74 struct file *,
75 struct mm_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77
David Howells8a2ab7f2006-07-10 04:44:53 -070078static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
79 struct file *, struct mm_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Christoph Hellwig698ba7b2009-12-15 16:47:37 -080081#ifdef CONFIG_ELF_CORE
Masami Hiramatsuf6151df2009-12-17 15:27:16 -080082static int elf_fdpic_core_dump(struct coredump_params *cprm);
David Howells6d8c4e32006-07-10 04:44:55 -070083#endif
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static struct linux_binfmt elf_fdpic_format = {
86 .module = THIS_MODULE,
87 .load_binary = load_elf_fdpic_binary,
Christoph Hellwig698ba7b2009-12-15 16:47:37 -080088#ifdef CONFIG_ELF_CORE
David Howells6d8c4e32006-07-10 04:44:55 -070089 .core_dump = elf_fdpic_core_dump,
90#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .min_coredump = ELF_EXEC_PAGESIZE,
92};
93
David Howells8a2ab7f2006-07-10 04:44:53 -070094static int __init init_elf_fdpic_binfmt(void)
95{
Al Viro8fc3dc52012-03-17 03:05:16 -040096 register_binfmt(&elf_fdpic_format);
97 return 0;
David Howells8a2ab7f2006-07-10 04:44:53 -070098}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
David Howells8a2ab7f2006-07-10 04:44:53 -0700100static void __exit exit_elf_fdpic_binfmt(void)
101{
102 unregister_binfmt(&elf_fdpic_format);
103}
104
David Howells6d8c4e32006-07-10 04:44:55 -0700105core_initcall(init_elf_fdpic_binfmt);
David Howells8a2ab7f2006-07-10 04:44:53 -0700106module_exit(exit_elf_fdpic_binfmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Rich Felker1bde9252015-11-09 14:59:01 -0800108static int is_elf(struct elfhdr *hdr, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
111 return 0;
112 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
113 return 0;
Rich Felker1bde9252015-11-09 14:59:01 -0800114 if (!elf_check_arch(hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
Al Viro72c2d532013-09-22 16:27:52 -0400116 if (!file->f_op->mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118 return 1;
119}
120
Rich Felker1bde9252015-11-09 14:59:01 -0800121#ifndef elf_check_fdpic
122#define elf_check_fdpic(x) 0
123#endif
124
125#ifndef elf_check_const_displacement
126#define elf_check_const_displacement(x) 0
127#endif
128
129static int is_constdisp(struct elfhdr *hdr)
130{
131 if (!elf_check_fdpic(hdr))
132 return 1;
133 if (elf_check_const_displacement(hdr))
134 return 1;
135 return 0;
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*****************************************************************************/
139/*
140 * read the program headers table into memory
141 */
David Howells8a2ab7f2006-07-10 04:44:53 -0700142static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
143 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct elf32_phdr *phdr;
146 unsigned long size;
147 int retval, loop;
148
149 if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
150 return -ENOMEM;
151 if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
152 return -ENOMEM;
153
154 size = params->hdr.e_phnum * sizeof(struct elf_phdr);
155 params->phdrs = kmalloc(size, GFP_KERNEL);
156 if (!params->phdrs)
157 return -ENOMEM;
158
David Howells8a2ab7f2006-07-10 04:44:53 -0700159 retval = kernel_read(file, params->hdr.e_phoff,
160 (char *) params->phdrs, size);
David Howellse1d2c8b2008-04-29 00:59:34 -0700161 if (unlikely(retval != size))
162 return retval < 0 ? retval : -ENOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /* determine stack size for this binary */
165 phdr = params->phdrs;
166 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
167 if (phdr->p_type != PT_GNU_STACK)
168 continue;
169
170 if (phdr->p_flags & PF_X)
171 params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
172 else
173 params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
174
175 params->stack_size = phdr->p_memsz;
176 break;
177 }
178
179 return 0;
David Howells8a2ab7f2006-07-10 04:44:53 -0700180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/*****************************************************************************/
183/*
184 * load an fdpic binary into various bits of memory
185 */
Al Viro71613c32012-10-20 22:00:48 -0400186static int load_elf_fdpic_binary(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct elf_fdpic_params exec_params, interp_params;
Al Viro71613c32012-10-20 22:00:48 -0400189 struct pt_regs *regs = current_pt_regs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct elf_phdr *phdr;
David Howells8a2ab7f2006-07-10 04:44:53 -0700191 unsigned long stack_size, entryaddr;
David Howells8a2ab7f2006-07-10 04:44:53 -0700192#ifdef ELF_FDPIC_PLAT_INIT
193 unsigned long dynaddr;
194#endif
Mike Frysinger04e4f2b2010-01-06 17:23:17 +0000195#ifndef CONFIG_MMU
196 unsigned long stack_prot;
197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct file *interpreter = NULL; /* to shut gcc up */
199 char *interpreter_name = NULL;
200 int executable_stack;
201 int retval, i;
202
David Howellsaa289b42007-03-23 00:10:00 -0700203 kdebug("____ LOAD %d ____", current->pid);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 memset(&exec_params, 0, sizeof(exec_params));
206 memset(&interp_params, 0, sizeof(interp_params));
207
208 exec_params.hdr = *(struct elfhdr *) bprm->buf;
209 exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
210
211 /* check that this is a binary we know how to deal with */
212 retval = -ENOEXEC;
Rich Felker1bde9252015-11-09 14:59:01 -0800213 if (!is_elf(&exec_params.hdr, bprm->file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 goto error;
Rich Felker1bde9252015-11-09 14:59:01 -0800215 if (!elf_check_fdpic(&exec_params.hdr)) {
216#ifdef CONFIG_MMU
217 /* binfmt_elf handles non-fdpic elf except on nommu */
218 goto error;
219#else
220 /* nommu can only load ET_DYN (PIE) ELF */
221 if (exec_params.hdr.e_type != ET_DYN)
222 goto error;
223#endif
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* read the program header table */
227 retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
228 if (retval < 0)
229 goto error;
230
231 /* scan for a program header that specifies an interpreter */
232 phdr = exec_params.phdrs;
233
234 for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
235 switch (phdr->p_type) {
236 case PT_INTERP:
237 retval = -ENOMEM;
238 if (phdr->p_filesz > PATH_MAX)
239 goto error;
240 retval = -ENOENT;
241 if (phdr->p_filesz < 2)
242 goto error;
243
244 /* read the name of the interpreter into memory */
Jesper Juhl792db3a2006-01-09 20:54:45 -0800245 interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!interpreter_name)
247 goto error;
248
249 retval = kernel_read(bprm->file,
250 phdr->p_offset,
251 interpreter_name,
252 phdr->p_filesz);
David Howellse1d2c8b2008-04-29 00:59:34 -0700253 if (unlikely(retval != phdr->p_filesz)) {
254 if (retval >= 0)
255 retval = -ENOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 goto error;
David Howellse1d2c8b2008-04-29 00:59:34 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 retval = -ENOENT;
260 if (interpreter_name[phdr->p_filesz - 1] != '\0')
261 goto error;
262
263 kdebug("Using ELF interpreter %s", interpreter_name);
264
265 /* replace the program with the interpreter */
266 interpreter = open_exec(interpreter_name);
267 retval = PTR_ERR(interpreter);
268 if (IS_ERR(interpreter)) {
269 interpreter = NULL;
270 goto error;
271 }
272
Alexey Dobriyan1fb84492007-01-26 00:57:16 -0800273 /*
274 * If the binary is not readable then enforce
275 * mm->dumpable = 0 regardless of the interpreter's
276 * permissions.
277 */
Al Viro1b5d7832011-06-19 12:49:47 -0400278 would_dump(bprm, interpreter);
Alexey Dobriyan1fb84492007-01-26 00:57:16 -0800279
David Howells8a2ab7f2006-07-10 04:44:53 -0700280 retval = kernel_read(interpreter, 0, bprm->buf,
281 BINPRM_BUF_SIZE);
David Howellse1d2c8b2008-04-29 00:59:34 -0700282 if (unlikely(retval != BINPRM_BUF_SIZE)) {
283 if (retval >= 0)
284 retval = -ENOEXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto error;
David Howellse1d2c8b2008-04-29 00:59:34 -0700286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 interp_params.hdr = *((struct elfhdr *) bprm->buf);
289 break;
290
291 case PT_LOAD:
292#ifdef CONFIG_MMU
293 if (exec_params.load_addr == 0)
294 exec_params.load_addr = phdr->p_vaddr;
295#endif
296 break;
297 }
298
299 }
300
Rich Felker1bde9252015-11-09 14:59:01 -0800301 if (is_constdisp(&exec_params.hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
303
304 /* perform insanity checks on the interpreter */
305 if (interpreter_name) {
306 retval = -ELIBBAD;
Rich Felker1bde9252015-11-09 14:59:01 -0800307 if (!is_elf(&interp_params.hdr, interpreter))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto error;
309
310 interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
311
312 /* read the interpreter's program header table */
313 retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
314 if (retval < 0)
315 goto error;
316 }
317
318 stack_size = exec_params.stack_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
320 executable_stack = EXSTACK_ENABLE_X;
321 else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
322 executable_stack = EXSTACK_DISABLE_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 else
324 executable_stack = EXSTACK_DEFAULT;
325
David Howells8e8b63a2009-09-23 15:57:06 -0700326 if (stack_size == 0) {
327 stack_size = interp_params.stack_size;
328 if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
329 executable_stack = EXSTACK_ENABLE_X;
330 else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
331 executable_stack = EXSTACK_DISABLE_X;
332 else
333 executable_stack = EXSTACK_DEFAULT;
334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 retval = -ENOEXEC;
337 if (stack_size == 0)
Rich Felker1bde9252015-11-09 14:59:01 -0800338 stack_size = 131072UL; /* same as exec.c's default commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Rich Felker1bde9252015-11-09 14:59:01 -0800340 if (is_constdisp(&interp_params.hdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
342
343 /* flush all traces of the currently running executable */
344 retval = flush_old_exec(bprm);
345 if (retval)
346 goto error;
347
348 /* there's now no turning back... the old userspace image is dead,
Al Viro19d860a2014-05-04 20:11:36 -0400349 * defunct, deceased, etc.
350 */
Rich Felker1bde9252015-11-09 14:59:01 -0800351 if (elf_check_fdpic(&exec_params.hdr))
352 set_personality(PER_LINUX_FDPIC);
353 else
354 set_personality(PER_LINUX);
Mike Frysinger04e4f2b2010-01-06 17:23:17 +0000355 if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
356 current->personality |= READ_IMPLIES_EXEC;
Linus Torvalds221af7f2010-01-28 22:14:42 -0800357
358 setup_new_exec(bprm);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 set_binfmt(&elf_fdpic_format);
361
362 current->mm->start_code = 0;
363 current->mm->end_code = 0;
364 current->mm->start_stack = 0;
365 current->mm->start_data = 0;
366 current->mm->end_data = 0;
367 current->mm->context.exec_fdpic_loadmap = 0;
368 current->mm->context.interp_fdpic_loadmap = 0;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#ifdef CONFIG_MMU
371 elf_fdpic_arch_lay_out_mm(&exec_params,
372 &interp_params,
373 &current->mm->start_stack,
374 &current->mm->start_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David Howells8a2ab7f2006-07-10 04:44:53 -0700376 retval = setup_arg_pages(bprm, current->mm->start_stack,
377 executable_stack);
Al Viro19d860a2014-05-04 20:11:36 -0400378 if (retval < 0)
379 goto error;
Nicolas Pitre382e67a2017-08-11 00:53:39 -0400380#ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
381 retval = arch_setup_additional_pages(bprm, !!interpreter_name);
382 if (retval < 0)
383 goto error;
384#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#endif
386
387 /* load the executable and interpreter into memory */
David Howells8a2ab7f2006-07-10 04:44:53 -0700388 retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
389 "executable");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (retval < 0)
Al Viro19d860a2014-05-04 20:11:36 -0400391 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (interpreter_name) {
394 retval = elf_fdpic_map_file(&interp_params, interpreter,
395 current->mm, "interpreter");
396 if (retval < 0) {
397 printk(KERN_ERR "Unable to load interpreter\n");
Al Viro19d860a2014-05-04 20:11:36 -0400398 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 allow_write_access(interpreter);
402 fput(interpreter);
403 interpreter = NULL;
404 }
405
406#ifdef CONFIG_MMU
407 if (!current->mm->start_brk)
408 current->mm->start_brk = current->mm->end_data;
409
David Howells8a2ab7f2006-07-10 04:44:53 -0700410 current->mm->brk = current->mm->start_brk =
411 PAGE_ALIGN(current->mm->start_brk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413#else
Rich Felker4ac31312015-08-20 15:11:06 -0400414 /* create a stack area and zero-size brk area */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
416 if (stack_size < PAGE_SIZE * 2)
417 stack_size = PAGE_SIZE * 2;
418
Mike Frysinger04e4f2b2010-01-06 17:23:17 +0000419 stack_prot = PROT_READ | PROT_WRITE;
420 if (executable_stack == EXSTACK_ENABLE_X ||
421 (executable_stack == EXSTACK_DEFAULT && VM_STACK_FLAGS & VM_EXEC))
422 stack_prot |= PROT_EXEC;
423
Linus Torvalds6be5ceb2012-04-20 17:13:58 -0700424 current->mm->start_brk = vm_mmap(NULL, 0, stack_size, stack_prot,
Jie Zhangea637632009-12-14 18:00:02 -0800425 MAP_PRIVATE | MAP_ANONYMOUS |
426 MAP_UNINITIALIZED | MAP_GROWSDOWN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 0);
428
David Howells8a2ab7f2006-07-10 04:44:53 -0700429 if (IS_ERR_VALUE(current->mm->start_brk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 retval = current->mm->start_brk;
431 current->mm->start_brk = 0;
Al Viro19d860a2014-05-04 20:11:36 -0400432 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 current->mm->brk = current->mm->start_brk;
436 current->mm->context.end_brk = current->mm->start_brk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 current->mm->start_stack = current->mm->start_brk + stack_size;
438#endif
439
David Howellsa6f76f22008-11-14 10:39:24 +1100440 install_exec_creds(bprm);
David Howells8a2ab7f2006-07-10 04:44:53 -0700441 if (create_elf_fdpic_tables(bprm, current->mm,
442 &exec_params, &interp_params) < 0)
Al Viro19d860a2014-05-04 20:11:36 -0400443 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
David Howells8a2ab7f2006-07-10 04:44:53 -0700445 kdebug("- start_code %lx", current->mm->start_code);
446 kdebug("- end_code %lx", current->mm->end_code);
447 kdebug("- start_data %lx", current->mm->start_data);
448 kdebug("- end_data %lx", current->mm->end_data);
449 kdebug("- start_brk %lx", current->mm->start_brk);
450 kdebug("- brk %lx", current->mm->brk);
451 kdebug("- start_stack %lx", current->mm->start_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453#ifdef ELF_FDPIC_PLAT_INIT
454 /*
455 * The ABI may specify that certain registers be set up in special
456 * ways (on i386 %edx is the address of a DT_FINI function, for
457 * example. This macro performs whatever initialization to
458 * the regs structure is required.
459 */
David Howells8a2ab7f2006-07-10 04:44:53 -0700460 dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
461 ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
462 dynaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#endif
464
465 /* everything is now ready... get the userspace context ready to roll */
David Howells8a2ab7f2006-07-10 04:44:53 -0700466 entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
467 start_thread(regs, entryaddr, current->mm->start_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 retval = 0;
470
471error:
472 if (interpreter) {
473 allow_write_access(interpreter);
474 fput(interpreter);
475 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800476 kfree(interpreter_name);
477 kfree(exec_params.phdrs);
478 kfree(exec_params.loadmap);
479 kfree(interp_params.phdrs);
480 kfree(interp_params.loadmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return retval;
David Howells8a2ab7f2006-07-10 04:44:53 -0700482}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484/*****************************************************************************/
Paul Mundtec238472008-10-15 22:04:15 -0700485
486#ifndef ELF_BASE_PLATFORM
487/*
488 * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
489 * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
490 * will be copied to the user stack in the same manner as AT_PLATFORM.
491 */
492#define ELF_BASE_PLATFORM NULL
493#endif
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/*
Paul Mundtc7637942008-10-15 22:04:15 -0700496 * present useful information to the program by shovelling it onto the new
497 * process's stack
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
499static int create_elf_fdpic_tables(struct linux_binprm *bprm,
500 struct mm_struct *mm,
501 struct elf_fdpic_params *exec_params,
502 struct elf_fdpic_params *interp_params)
503{
David Howells86a264a2008-11-14 10:39:18 +1100504 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned long sp, csp, nitems;
Al Viro530018b2006-06-23 02:04:05 -0700506 elf_caddr_t __user *argv, *envp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 size_t platform_len = 0, len;
Paul Mundtec238472008-10-15 22:04:15 -0700508 char *k_platform, *k_base_platform;
509 char __user *u_platform, *u_base_platform, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int loop;
Paul Mundt9b14ec32008-05-19 13:34:45 +0900511 int nr; /* reset for each csp adjustment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#ifdef CONFIG_MMU
Paul Mundtc7637942008-10-15 22:04:15 -0700514 /* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
515 * by the processes running on the same package. One thing we can do is
516 * to shuffle the initial stack for them, so we give the architecture
517 * an opportunity to do so here.
518 */
519 sp = arch_align_stack(bprm->p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#else
521 sp = mm->start_stack;
522
523 /* stack the program arguments and environment */
Nicolas Pitre7e7ec6a2016-07-24 11:30:18 -0400524 if (transfer_args_to_stack(bprm, &sp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EFAULT;
Nicolas Pitre7e7ec6a2016-07-24 11:30:18 -0400526 sp &= ~15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#endif
528
Paul Mundtec238472008-10-15 22:04:15 -0700529 /*
530 * If this architecture has a platform capability string, copy it
531 * to userspace. In some cases (Sparc), this info is impossible
532 * for userspace to get any other way, in others (i386) it is
533 * merely difficult.
534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 k_platform = ELF_PLATFORM;
David Howells1aeb21d2006-07-10 04:44:50 -0700536 u_platform = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (k_platform) {
539 platform_len = strlen(k_platform) + 1;
540 sp -= platform_len;
Al Viro530018b2006-06-23 02:04:05 -0700541 u_platform = (char __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
543 return -EFAULT;
544 }
545
Paul Mundtec238472008-10-15 22:04:15 -0700546 /*
547 * If this architecture has a "base" platform capability
548 * string, copy it to userspace.
549 */
550 k_base_platform = ELF_BASE_PLATFORM;
551 u_base_platform = NULL;
552
553 if (k_base_platform) {
554 platform_len = strlen(k_base_platform) + 1;
555 sp -= platform_len;
556 u_base_platform = (char __user *) sp;
557 if (__copy_to_user(u_base_platform, k_base_platform, platform_len) != 0)
558 return -EFAULT;
559 }
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 sp &= ~7UL;
562
563 /* stack the load map(s) */
564 len = sizeof(struct elf32_fdpic_loadmap);
565 len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
566 sp = (sp - len) & ~7UL;
567 exec_params->map_addr = sp;
568
Al Viro530018b2006-06-23 02:04:05 -0700569 if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return -EFAULT;
571
572 current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
573
574 if (interp_params->loadmap) {
575 len = sizeof(struct elf32_fdpic_loadmap);
David Howells8a2ab7f2006-07-10 04:44:53 -0700576 len += sizeof(struct elf32_fdpic_loadseg) *
577 interp_params->loadmap->nsegs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 sp = (sp - len) & ~7UL;
579 interp_params->map_addr = sp;
580
David Howells8a2ab7f2006-07-10 04:44:53 -0700581 if (copy_to_user((void __user *) sp, interp_params->loadmap,
582 len) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EFAULT;
584
585 current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
586 }
587
588 /* force 16 byte _final_ alignment here for generality */
Paul Mundt5edc2a52008-10-15 22:04:16 -0700589#define DLINFO_ITEMS 15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Paul Mundtec238472008-10-15 22:04:15 -0700591 nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0) +
592 (k_base_platform ? 1 : 0) + AT_VECTOR_SIZE_ARCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Paul Mundt5edc2a52008-10-15 22:04:16 -0700594 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD)
595 nitems++;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 csp = sp;
598 sp -= nitems * 2 * sizeof(unsigned long);
599 sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
600 sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
601 sp -= 1 * sizeof(unsigned long); /* argc */
602
603 csp -= sp & 15UL;
604 sp -= sp & 15UL;
605
606 /* put the ELF interpreter info on the stack */
Paul Mundt9b14ec32008-05-19 13:34:45 +0900607#define NEW_AUX_ENT(id, val) \
David Howells8a2ab7f2006-07-10 04:44:53 -0700608 do { \
609 struct { unsigned long _id, _val; } __user *ent; \
610 \
611 ent = (void __user *) csp; \
612 __put_user((id), &ent[nr]._id); \
613 __put_user((val), &ent[nr]._val); \
Paul Mundt9b14ec32008-05-19 13:34:45 +0900614 nr++; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 } while (0)
616
Paul Mundt9b14ec32008-05-19 13:34:45 +0900617 nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 csp -= 2 * sizeof(unsigned long);
Paul Mundt9b14ec32008-05-19 13:34:45 +0900619 NEW_AUX_ENT(AT_NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (k_platform) {
Paul Mundt9b14ec32008-05-19 13:34:45 +0900621 nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 csp -= 2 * sizeof(unsigned long);
Paul Mundt9b14ec32008-05-19 13:34:45 +0900623 NEW_AUX_ENT(AT_PLATFORM,
David Howells8a2ab7f2006-07-10 04:44:53 -0700624 (elf_addr_t) (unsigned long) u_platform);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Paul Mundtec238472008-10-15 22:04:15 -0700627 if (k_base_platform) {
628 nr = 0;
629 csp -= 2 * sizeof(unsigned long);
630 NEW_AUX_ENT(AT_BASE_PLATFORM,
631 (elf_addr_t) (unsigned long) u_base_platform);
632 }
633
Paul Mundt5edc2a52008-10-15 22:04:16 -0700634 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
635 nr = 0;
636 csp -= 2 * sizeof(unsigned long);
637 NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
638 }
639
Paul Mundt9b14ec32008-05-19 13:34:45 +0900640 nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
Michael Neuling21713642013-04-17 17:33:11 +0000642 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
643#ifdef ELF_HWCAP2
644 NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
645#endif
Paul Mundt9b14ec32008-05-19 13:34:45 +0900646 NEW_AUX_ENT(AT_PAGESZ, PAGE_SIZE);
647 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
648 NEW_AUX_ENT(AT_PHDR, exec_params->ph_addr);
649 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
650 NEW_AUX_ENT(AT_PHNUM, exec_params->hdr.e_phnum);
651 NEW_AUX_ENT(AT_BASE, interp_params->elfhdr_addr);
652 NEW_AUX_ENT(AT_FLAGS, 0);
653 NEW_AUX_ENT(AT_ENTRY, exec_params->entry_addr);
Eric W. Biedermanebc887b2012-02-07 18:36:10 -0800654 NEW_AUX_ENT(AT_UID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->uid));
655 NEW_AUX_ENT(AT_EUID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
656 NEW_AUX_ENT(AT_GID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->gid));
657 NEW_AUX_ENT(AT_EGID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid));
Paul Mundt5edc2a52008-10-15 22:04:16 -0700658 NEW_AUX_ENT(AT_SECURE, security_bprm_secureexec(bprm));
659 NEW_AUX_ENT(AT_EXECFN, bprm->exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661#ifdef ARCH_DLINFO
Paul Mundt9b14ec32008-05-19 13:34:45 +0900662 nr = 0;
663 csp -= AT_VECTOR_SIZE_ARCH * 2 * sizeof(unsigned long);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* ARCH_DLINFO must come last so platform specific code can enforce
666 * special alignment requirements on the AUXV if necessary (eg. PPC).
667 */
668 ARCH_DLINFO;
669#endif
670#undef NEW_AUX_ENT
671
672 /* allocate room for argv[] and envv[] */
673 csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
Al Viro530018b2006-06-23 02:04:05 -0700674 envp = (elf_caddr_t __user *) csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
Al Viro530018b2006-06-23 02:04:05 -0700676 argv = (elf_caddr_t __user *) csp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /* stack argc */
679 csp -= sizeof(unsigned long);
Al Viro530018b2006-06-23 02:04:05 -0700680 __put_user(bprm->argc, (unsigned long __user *) csp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Eric Sesterhenn88bcd512006-03-24 18:38:48 +0100682 BUG_ON(csp != sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /* fill in the argv[] array */
685#ifdef CONFIG_MMU
686 current->mm->arg_start = bprm->p;
687#else
David Howells8a2ab7f2006-07-10 04:44:53 -0700688 current->mm->arg_start = current->mm->start_stack -
689 (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690#endif
691
Al Viro530018b2006-06-23 02:04:05 -0700692 p = (char __user *) current->mm->arg_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 for (loop = bprm->argc; loop > 0; loop--) {
694 __put_user((elf_caddr_t) p, argv++);
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700695 len = strnlen_user(p, MAX_ARG_STRLEN);
696 if (!len || len > MAX_ARG_STRLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -EINVAL;
698 p += len;
699 }
700 __put_user(NULL, argv);
701 current->mm->arg_end = (unsigned long) p;
702
703 /* fill in the envv[] array */
704 current->mm->env_start = (unsigned long) p;
705 for (loop = bprm->envc; loop > 0; loop--) {
706 __put_user((elf_caddr_t)(unsigned long) p, envp++);
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700707 len = strnlen_user(p, MAX_ARG_STRLEN);
708 if (!len || len > MAX_ARG_STRLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return -EINVAL;
710 p += len;
711 }
712 __put_user(NULL, envp);
713 current->mm->env_end = (unsigned long) p;
714
715 mm->start_stack = (unsigned long) sp;
716 return 0;
David Howells8a2ab7f2006-07-10 04:44:53 -0700717}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719/*****************************************************************************/
720/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * load the appropriate binary image (executable or interpreter) into memory
722 * - we assume no MMU is available
723 * - if no other PIC bits are set in params->hdr->e_flags
724 * - we assume that the LOADable segments in the binary are independently relocatable
725 * - we assume R/O executable segments are shareable
726 * - else
727 * - we assume the loadable parts of the image to require fixed displacement
728 * - the image is not shareable
729 */
730static int elf_fdpic_map_file(struct elf_fdpic_params *params,
731 struct file *file,
732 struct mm_struct *mm,
733 const char *what)
734{
735 struct elf32_fdpic_loadmap *loadmap;
736#ifdef CONFIG_MMU
737 struct elf32_fdpic_loadseg *mseg;
738#endif
739 struct elf32_fdpic_loadseg *seg;
740 struct elf32_phdr *phdr;
741 unsigned long load_addr, stop;
742 unsigned nloads, tmp;
743 size_t size;
744 int loop, ret;
745
746 /* allocate a load map table */
747 nloads = 0;
748 for (loop = 0; loop < params->hdr.e_phnum; loop++)
749 if (params->phdrs[loop].p_type == PT_LOAD)
750 nloads++;
751
752 if (nloads == 0)
753 return -ELIBBAD;
754
755 size = sizeof(*loadmap) + nloads * sizeof(*seg);
Robert P. J. Dayb87576d2006-12-12 20:07:35 +0100756 loadmap = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!loadmap)
758 return -ENOMEM;
759
760 params->loadmap = loadmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
763 loadmap->nsegs = nloads;
764
765 load_addr = params->load_addr;
766 seg = loadmap->segs;
767
768 /* map the requested LOADs into the memory space */
769 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
770 case ELF_FDPIC_FLAG_CONSTDISP:
771 case ELF_FDPIC_FLAG_CONTIGUOUS:
772#ifndef CONFIG_MMU
773 ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
774 if (ret < 0)
775 return ret;
776 break;
777#endif
778 default:
779 ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
780 if (ret < 0)
781 return ret;
782 break;
783 }
784
785 /* map the entry point */
786 if (params->hdr.e_entry) {
787 seg = loadmap->segs;
788 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
789 if (params->hdr.e_entry >= seg->p_vaddr &&
David Howells8a2ab7f2006-07-10 04:44:53 -0700790 params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 params->entry_addr =
David Howells8a2ab7f2006-07-10 04:44:53 -0700792 (params->hdr.e_entry - seg->p_vaddr) +
793 seg->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 break;
795 }
796 }
797 }
798
799 /* determine where the program header table has wound up if mapped */
David Howells8a2ab7f2006-07-10 04:44:53 -0700800 stop = params->hdr.e_phoff;
801 stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 phdr = params->phdrs;
803
804 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
805 if (phdr->p_type != PT_LOAD)
806 continue;
807
808 if (phdr->p_offset > params->hdr.e_phoff ||
809 phdr->p_offset + phdr->p_filesz < stop)
810 continue;
811
812 seg = loadmap->segs;
813 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
814 if (phdr->p_vaddr >= seg->p_vaddr &&
David Howells8a2ab7f2006-07-10 04:44:53 -0700815 phdr->p_vaddr + phdr->p_filesz <=
816 seg->p_vaddr + seg->p_memsz) {
817 params->ph_addr =
818 (phdr->p_vaddr - seg->p_vaddr) +
819 seg->addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 params->hdr.e_phoff - phdr->p_offset;
821 break;
822 }
823 }
824 break;
825 }
826
827 /* determine where the dynamic section has wound up if there is one */
828 phdr = params->phdrs;
829 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
830 if (phdr->p_type != PT_DYNAMIC)
831 continue;
832
833 seg = loadmap->segs;
834 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
835 if (phdr->p_vaddr >= seg->p_vaddr &&
David Howells8a2ab7f2006-07-10 04:44:53 -0700836 phdr->p_vaddr + phdr->p_memsz <=
837 seg->p_vaddr + seg->p_memsz) {
Nicolas Pitrecdf38882017-08-16 18:56:14 -0400838 Elf32_Dyn __user *dyn;
839 Elf32_Sword d_tag;
840
David Howells8a2ab7f2006-07-10 04:44:53 -0700841 params->dynamic_addr =
842 (phdr->p_vaddr - seg->p_vaddr) +
843 seg->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
David Howells8a2ab7f2006-07-10 04:44:53 -0700845 /* check the dynamic section contains at least
846 * one item, and that the last item is a NULL
847 * entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (phdr->p_memsz == 0 ||
849 phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
850 goto dynamic_error;
851
852 tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
Nicolas Pitrecdf38882017-08-16 18:56:14 -0400853 dyn = (Elf32_Dyn __user *)params->dynamic_addr;
854 __get_user(d_tag, &dyn[tmp - 1].d_tag);
855 if (d_tag != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto dynamic_error;
857 break;
858 }
859 }
860 break;
861 }
862
863 /* now elide adjacent segments in the load map on MMU linux
David Howells8a2ab7f2006-07-10 04:44:53 -0700864 * - on uClinux the holes between may actually be filled with system
865 * stuff or stuff from other processes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
867#ifdef CONFIG_MMU
868 nloads = loadmap->nsegs;
869 mseg = loadmap->segs;
870 seg = mseg + 1;
871 for (loop = 1; loop < nloads; loop++) {
872 /* see if we have a candidate for merging */
873 if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
874 load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
875 if (load_addr == (seg->addr & PAGE_MASK)) {
David Howells8a2ab7f2006-07-10 04:44:53 -0700876 mseg->p_memsz +=
877 load_addr -
878 (mseg->addr + mseg->p_memsz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 mseg->p_memsz += seg->addr & ~PAGE_MASK;
880 mseg->p_memsz += seg->p_memsz;
881 loadmap->nsegs--;
882 continue;
883 }
884 }
885
886 mseg++;
887 if (mseg != seg)
888 *mseg = *seg;
889 }
890#endif
891
892 kdebug("Mapped Object [%s]:", what);
893 kdebug("- elfhdr : %lx", params->elfhdr_addr);
894 kdebug("- entry : %lx", params->entry_addr);
895 kdebug("- PHDR[] : %lx", params->ph_addr);
896 kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
897 seg = loadmap->segs;
898 for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
899 kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
900 loop,
901 seg->addr, seg->addr + seg->p_memsz - 1,
902 seg->p_vaddr, seg->p_memsz);
903
904 return 0;
905
David Howells8a2ab7f2006-07-10 04:44:53 -0700906dynamic_error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
Al Viro496ad9a2013-01-23 17:07:38 -0500908 what, file_inode(file)->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -ELIBBAD;
David Howells8a2ab7f2006-07-10 04:44:53 -0700910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912/*****************************************************************************/
913/*
914 * map a file with constant displacement under uClinux
915 */
916#ifndef CONFIG_MMU
David Howells8a2ab7f2006-07-10 04:44:53 -0700917static int elf_fdpic_map_file_constdisp_on_uclinux(
918 struct elf_fdpic_params *params,
919 struct file *file,
920 struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct elf32_fdpic_loadseg *seg;
923 struct elf32_phdr *phdr;
924 unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int loop, ret;
926
927 load_addr = params->load_addr;
928 seg = params->loadmap->segs;
929
David Howells8a2ab7f2006-07-10 04:44:53 -0700930 /* determine the bounds of the contiguous overall allocation we must
931 * make */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 phdr = params->phdrs;
933 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
934 if (params->phdrs[loop].p_type != PT_LOAD)
935 continue;
936
937 if (base > phdr->p_vaddr)
938 base = phdr->p_vaddr;
939 if (top < phdr->p_vaddr + phdr->p_memsz)
940 top = phdr->p_vaddr + phdr->p_memsz;
941 }
942
943 /* allocate one big anon block for everything */
944 mflags = MAP_PRIVATE;
945 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
946 mflags |= MAP_EXECUTABLE;
947
Linus Torvalds6be5ceb2012-04-20 17:13:58 -0700948 maddr = vm_mmap(NULL, load_addr, top - base,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
David Howells8a2ab7f2006-07-10 04:44:53 -0700950 if (IS_ERR_VALUE(maddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return (int) maddr;
952
953 if (load_addr != 0)
954 load_addr += PAGE_ALIGN(top - base);
955
956 /* and then load the file segments into it */
957 phdr = params->phdrs;
958 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
959 if (params->phdrs[loop].p_type != PT_LOAD)
960 continue;
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 seg->addr = maddr + (phdr->p_vaddr - base);
963 seg->p_vaddr = phdr->p_vaddr;
964 seg->p_memsz = phdr->p_memsz;
965
Al Viro3dc20cb2013-04-13 20:31:37 -0400966 ret = read_code(file, seg->addr, phdr->p_offset,
967 phdr->p_filesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (ret < 0)
969 return ret;
970
971 /* map the ELF header address if in this segment */
972 if (phdr->p_offset == 0)
973 params->elfhdr_addr = seg->addr;
974
975 /* clear any space allocated but not loaded */
Mike Frysingerab4ad552009-04-02 16:58:28 -0700976 if (phdr->p_filesz < phdr->p_memsz) {
Takuya Yoshikawae30c7c32010-06-01 14:10:47 +0100977 if (clear_user((void *) (seg->addr + phdr->p_filesz),
978 phdr->p_memsz - phdr->p_filesz))
979 return -EFAULT;
Mike Frysingerab4ad552009-04-02 16:58:28 -0700980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (mm) {
983 if (phdr->p_flags & PF_X) {
David Howellsaa289b42007-03-23 00:10:00 -0700984 if (!mm->start_code) {
985 mm->start_code = seg->addr;
986 mm->end_code = seg->addr +
987 phdr->p_memsz;
988 }
David Howells8a2ab7f2006-07-10 04:44:53 -0700989 } else if (!mm->start_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 mm->start_data = seg->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 mm->end_data = seg->addr + phdr->p_memsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
995 seg++;
996 }
997
998 return 0;
David Howells8a2ab7f2006-07-10 04:44:53 -0700999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#endif
1001
1002/*****************************************************************************/
1003/*
1004 * map a binary by direct mmap() of the individual PT_LOAD segments
1005 */
1006static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
1007 struct file *file,
1008 struct mm_struct *mm)
1009{
1010 struct elf32_fdpic_loadseg *seg;
1011 struct elf32_phdr *phdr;
1012 unsigned long load_addr, delta_vaddr;
Takuya Yoshikawae30c7c32010-06-01 14:10:47 +01001013 int loop, dvset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 load_addr = params->load_addr;
1016 delta_vaddr = 0;
1017 dvset = 0;
1018
1019 seg = params->loadmap->segs;
1020
1021 /* deal with each load segment separately */
1022 phdr = params->phdrs;
1023 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
1024 unsigned long maddr, disp, excess, excess1;
1025 int prot = 0, flags;
1026
1027 if (phdr->p_type != PT_LOAD)
1028 continue;
1029
1030 kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
1031 (unsigned long) phdr->p_vaddr,
1032 (unsigned long) phdr->p_offset,
1033 (unsigned long) phdr->p_filesz,
1034 (unsigned long) phdr->p_memsz);
1035
1036 /* determine the mapping parameters */
1037 if (phdr->p_flags & PF_R) prot |= PROT_READ;
1038 if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
1039 if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
1040
1041 flags = MAP_PRIVATE | MAP_DENYWRITE;
1042 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
1043 flags |= MAP_EXECUTABLE;
1044
1045 maddr = 0;
1046
1047 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
1048 case ELF_FDPIC_FLAG_INDEPENDENT:
1049 /* PT_LOADs are independently locatable */
1050 break;
1051
1052 case ELF_FDPIC_FLAG_HONOURVADDR:
1053 /* the specified virtual address must be honoured */
1054 maddr = phdr->p_vaddr;
1055 flags |= MAP_FIXED;
1056 break;
1057
1058 case ELF_FDPIC_FLAG_CONSTDISP:
1059 /* constant displacement
David Howells8a2ab7f2006-07-10 04:44:53 -07001060 * - can be mapped anywhere, but must be mapped as a
1061 * unit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
1063 if (!dvset) {
1064 maddr = load_addr;
1065 delta_vaddr = phdr->p_vaddr;
1066 dvset = 1;
David Howells8a2ab7f2006-07-10 04:44:53 -07001067 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 maddr = load_addr + phdr->p_vaddr - delta_vaddr;
1069 flags |= MAP_FIXED;
1070 }
1071 break;
1072
1073 case ELF_FDPIC_FLAG_CONTIGUOUS:
1074 /* contiguity handled later */
1075 break;
1076
1077 default:
1078 BUG();
1079 }
1080
1081 maddr &= PAGE_MASK;
1082
1083 /* create the mapping */
1084 disp = phdr->p_vaddr & ~PAGE_MASK;
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001085 maddr = vm_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 phdr->p_offset - disp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
David Howells8a2ab7f2006-07-10 04:44:53 -07001089 loop, phdr->p_memsz + disp, prot, flags,
1090 phdr->p_offset - disp, maddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
David Howells8a2ab7f2006-07-10 04:44:53 -07001092 if (IS_ERR_VALUE(maddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return (int) maddr;
1094
David Howells8a2ab7f2006-07-10 04:44:53 -07001095 if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
1096 ELF_FDPIC_FLAG_CONTIGUOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
1098
1099 seg->addr = maddr + disp;
1100 seg->p_vaddr = phdr->p_vaddr;
1101 seg->p_memsz = phdr->p_memsz;
1102
1103 /* map the ELF header address if in this segment */
1104 if (phdr->p_offset == 0)
1105 params->elfhdr_addr = seg->addr;
1106
David Howells8a2ab7f2006-07-10 04:44:53 -07001107 /* clear the bit between beginning of mapping and beginning of
1108 * PT_LOAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (prot & PROT_WRITE && disp > 0) {
1110 kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
Takuya Yoshikawae30c7c32010-06-01 14:10:47 +01001111 if (clear_user((void __user *) maddr, disp))
1112 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 maddr += disp;
1114 }
1115
1116 /* clear any space allocated but not loaded
1117 * - on uClinux we can just clear the lot
1118 * - on MMU linux we'll get a SIGBUS beyond the last page
1119 * extant in the file
1120 */
1121 excess = phdr->p_memsz - phdr->p_filesz;
1122 excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
1123
1124#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (excess > excess1) {
1126 unsigned long xaddr = maddr + phdr->p_filesz + excess1;
1127 unsigned long xmaddr;
1128
1129 flags |= MAP_FIXED | MAP_ANONYMOUS;
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001130 xmaddr = vm_mmap(NULL, xaddr, excess - excess1,
David Howells8a2ab7f2006-07-10 04:44:53 -07001131 prot, flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 kdebug("mmap[%d] <anon>"
1134 " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
David Howells8a2ab7f2006-07-10 04:44:53 -07001135 loop, xaddr, excess - excess1, prot, flags,
1136 xmaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (xmaddr != xaddr)
1139 return -ENOMEM;
1140 }
1141
1142 if (prot & PROT_WRITE && excess1 > 0) {
1143 kdebug("clear[%d] ad=%lx sz=%lx",
1144 loop, maddr + phdr->p_filesz, excess1);
Takuya Yoshikawae30c7c32010-06-01 14:10:47 +01001145 if (clear_user((void __user *) maddr + phdr->p_filesz,
1146 excess1))
1147 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
1150#else
1151 if (excess > 0) {
1152 kdebug("clear[%d] ad=%lx sz=%lx",
1153 loop, maddr + phdr->p_filesz, excess);
Takuya Yoshikawae30c7c32010-06-01 14:10:47 +01001154 if (clear_user((void *) maddr + phdr->p_filesz, excess))
1155 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157#endif
1158
1159 if (mm) {
1160 if (phdr->p_flags & PF_X) {
David Howellsaa289b42007-03-23 00:10:00 -07001161 if (!mm->start_code) {
1162 mm->start_code = maddr;
1163 mm->end_code = maddr + phdr->p_memsz;
1164 }
David Howells8a2ab7f2006-07-10 04:44:53 -07001165 } else if (!mm->start_data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 mm->start_data = maddr;
1167 mm->end_data = maddr + phdr->p_memsz;
1168 }
1169 }
1170
1171 seg++;
1172 }
1173
1174 return 0;
David Howells8a2ab7f2006-07-10 04:44:53 -07001175}
David Howells6d8c4e32006-07-10 04:44:55 -07001176
1177/*****************************************************************************/
1178/*
1179 * ELF-FDPIC core dumper
1180 *
1181 * Modelled on fs/exec.c:aout_core_dump()
1182 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1183 *
1184 * Modelled on fs/binfmt_elf.c core dumper
1185 */
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08001186#ifdef CONFIG_ELF_CORE
David Howells6d8c4e32006-07-10 04:44:55 -07001187
1188/*
David Howells6d8c4e32006-07-10 04:44:55 -07001189 * Decide whether a segment is worth dumping; default is yes to be
1190 * sure (missing info is worse than too much; etc).
1191 * Personally I'd include everything, and use the coredump limit...
1192 *
1193 * I think we should skip something. But I am not sure how. H.J.
1194 */
Kawai, Hidehiroee78b0a2007-07-19 01:48:30 -07001195static int maydump(struct vm_area_struct *vma, unsigned long mm_flags)
David Howells6d8c4e32006-07-10 04:44:55 -07001196{
Kawai, Hidehiroee78b0a2007-07-19 01:48:30 -07001197 int dump_ok;
1198
David Howells6d8c4e32006-07-10 04:44:55 -07001199 /* Do not dump I/O mapped devices or special mappings */
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001200 if (vma->vm_flags & VM_IO) {
David Howells6d8c4e32006-07-10 04:44:55 -07001201 kdcore("%08lx: %08lx: no (IO)", vma->vm_start, vma->vm_flags);
1202 return 0;
1203 }
1204
1205 /* If we may not read the contents, don't allow us to dump
1206 * them either. "dump_write()" can't handle it anyway.
1207 */
1208 if (!(vma->vm_flags & VM_READ)) {
1209 kdcore("%08lx: %08lx: no (!read)", vma->vm_start, vma->vm_flags);
1210 return 0;
1211 }
1212
Ross Zwislerab27a8d2015-10-05 16:33:37 -06001213 /* support for DAX */
1214 if (vma_is_dax(vma)) {
1215 if (vma->vm_flags & VM_SHARED) {
1216 dump_ok = test_bit(MMF_DUMP_DAX_SHARED, &mm_flags);
1217 kdcore("%08lx: %08lx: %s (DAX shared)", vma->vm_start,
1218 vma->vm_flags, dump_ok ? "yes" : "no");
1219 } else {
1220 dump_ok = test_bit(MMF_DUMP_DAX_PRIVATE, &mm_flags);
1221 kdcore("%08lx: %08lx: %s (DAX private)", vma->vm_start,
1222 vma->vm_flags, dump_ok ? "yes" : "no");
1223 }
1224 return dump_ok;
1225 }
1226
Kawai, Hidehiroee78b0a2007-07-19 01:48:30 -07001227 /* By default, dump shared memory if mapped from an anonymous file. */
David Howells6d8c4e32006-07-10 04:44:55 -07001228 if (vma->vm_flags & VM_SHARED) {
Al Viro496ad9a2013-01-23 17:07:38 -05001229 if (file_inode(vma->vm_file)->i_nlink == 0) {
Kawai, Hidehiroee78b0a2007-07-19 01:48:30 -07001230 dump_ok = test_bit(MMF_DUMP_ANON_SHARED, &mm_flags);
1231 kdcore("%08lx: %08lx: %s (share)", vma->vm_start,
1232 vma->vm_flags, dump_ok ? "yes" : "no");
1233 return dump_ok;
David Howells6d8c4e32006-07-10 04:44:55 -07001234 }
1235
Kawai, Hidehiroee78b0a2007-07-19 01:48:30 -07001236 dump_ok = test_bit(MMF_DUMP_MAPPED_SHARED, &mm_flags);
1237 kdcore("%08lx: %08lx: %s (share)", vma->vm_start,
1238 vma->vm_flags, dump_ok ? "yes" : "no");
1239 return dump_ok;
David Howells6d8c4e32006-07-10 04:44:55 -07001240 }
1241
1242#ifdef CONFIG_MMU
Kawai, Hidehiroee78b0a2007-07-19 01:48:30 -07001243 /* By default, if it hasn't been written to, don't write it out */
David Howells6d8c4e32006-07-10 04:44:55 -07001244 if (!vma->anon_vma) {
Kawai, Hidehiroee78b0a2007-07-19 01:48:30 -07001245 dump_ok = test_bit(MMF_DUMP_MAPPED_PRIVATE, &mm_flags);
1246 kdcore("%08lx: %08lx: %s (!anon)", vma->vm_start,
1247 vma->vm_flags, dump_ok ? "yes" : "no");
1248 return dump_ok;
David Howells6d8c4e32006-07-10 04:44:55 -07001249 }
1250#endif
1251
Kawai, Hidehiroee78b0a2007-07-19 01:48:30 -07001252 dump_ok = test_bit(MMF_DUMP_ANON_PRIVATE, &mm_flags);
1253 kdcore("%08lx: %08lx: %s", vma->vm_start, vma->vm_flags,
1254 dump_ok ? "yes" : "no");
1255 return dump_ok;
David Howells6d8c4e32006-07-10 04:44:55 -07001256}
1257
1258/* An ELF note in memory */
1259struct memelfnote
1260{
1261 const char *name;
1262 int type;
1263 unsigned int datasz;
1264 void *data;
1265};
1266
1267static int notesize(struct memelfnote *en)
1268{
1269 int sz;
1270
1271 sz = sizeof(struct elf_note);
1272 sz += roundup(strlen(en->name) + 1, 4);
1273 sz += roundup(en->datasz, 4);
1274
1275 return sz;
1276}
1277
1278/* #define DEBUG */
1279
Al Viroe6c1baa2013-10-05 18:58:47 -04001280static int writenote(struct memelfnote *men, struct coredump_params *cprm)
David Howells6d8c4e32006-07-10 04:44:55 -07001281{
1282 struct elf_note en;
David Howells6d8c4e32006-07-10 04:44:55 -07001283 en.n_namesz = strlen(men->name) + 1;
1284 en.n_descsz = men->datasz;
1285 en.n_type = men->type;
1286
Al Viroe6c1baa2013-10-05 18:58:47 -04001287 return dump_emit(cprm, &en, sizeof(en)) &&
Al Viro22a8cb82013-10-08 11:05:01 -04001288 dump_emit(cprm, men->name, en.n_namesz) && dump_align(cprm, 4) &&
1289 dump_emit(cprm, men->data, men->datasz) && dump_align(cprm, 4);
David Howells6d8c4e32006-07-10 04:44:55 -07001290}
David Howells6d8c4e32006-07-10 04:44:55 -07001291
David Howells6d8c4e32006-07-10 04:44:55 -07001292static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
1293{
1294 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1295 elf->e_ident[EI_CLASS] = ELF_CLASS;
1296 elf->e_ident[EI_DATA] = ELF_DATA;
1297 elf->e_ident[EI_VERSION] = EV_CURRENT;
1298 elf->e_ident[EI_OSABI] = ELF_OSABI;
1299 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1300
1301 elf->e_type = ET_CORE;
1302 elf->e_machine = ELF_ARCH;
1303 elf->e_version = EV_CURRENT;
1304 elf->e_entry = 0;
1305 elf->e_phoff = sizeof(struct elfhdr);
1306 elf->e_shoff = 0;
1307 elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
1308 elf->e_ehsize = sizeof(struct elfhdr);
1309 elf->e_phentsize = sizeof(struct elf_phdr);
1310 elf->e_phnum = segs;
1311 elf->e_shentsize = 0;
1312 elf->e_shnum = 0;
1313 elf->e_shstrndx = 0;
1314 return;
1315}
1316
1317static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1318{
1319 phdr->p_type = PT_NOTE;
1320 phdr->p_offset = offset;
1321 phdr->p_vaddr = 0;
1322 phdr->p_paddr = 0;
1323 phdr->p_filesz = sz;
1324 phdr->p_memsz = 0;
1325 phdr->p_flags = 0;
1326 phdr->p_align = 0;
1327 return;
1328}
1329
1330static inline void fill_note(struct memelfnote *note, const char *name, int type,
1331 unsigned int sz, void *data)
1332{
1333 note->name = name;
1334 note->type = type;
1335 note->datasz = sz;
1336 note->data = data;
1337 return;
1338}
1339
1340/*
1341 * fill up all the fields in prstatus from the given task struct, except
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001342 * registers which need to be filled up separately.
David Howells6d8c4e32006-07-10 04:44:55 -07001343 */
1344static void fill_prstatus(struct elf_prstatus *prstatus,
1345 struct task_struct *p, long signr)
1346{
1347 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1348 prstatus->pr_sigpend = p->pending.signal.sig[0];
1349 prstatus->pr_sighold = p->blocked.sig[0];
Oleg Nesterov3b34fc52009-06-17 16:27:38 -07001350 rcu_read_lock();
1351 prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1352 rcu_read_unlock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001353 prstatus->pr_pid = task_pid_vnr(p);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001354 prstatus->pr_pgrp = task_pgrp_vnr(p);
1355 prstatus->pr_sid = task_session_vnr(p);
David Howells6d8c4e32006-07-10 04:44:55 -07001356 if (thread_group_leader(p)) {
Frederic Weisbeckercd19c362017-01-31 04:09:27 +01001357 struct task_cputime cputime;
Paul Mundt2515ddc2008-10-21 12:07:40 +09001358
David Howells6d8c4e32006-07-10 04:44:55 -07001359 /*
Paul Mundt2515ddc2008-10-21 12:07:40 +09001360 * This is the record for the group leader. It shows the
1361 * group-wide total, not its individual thread total.
David Howells6d8c4e32006-07-10 04:44:55 -07001362 */
Frederic Weisbeckercd19c362017-01-31 04:09:27 +01001363 thread_group_cputime(p, &cputime);
1364 prstatus->pr_utime = ns_to_timeval(cputime.utime);
1365 prstatus->pr_stime = ns_to_timeval(cputime.stime);
David Howells6d8c4e32006-07-10 04:44:55 -07001366 } else {
Frederic Weisbeckercd19c362017-01-31 04:09:27 +01001367 u64 utime, stime;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001368
Frederic Weisbeckercd19c362017-01-31 04:09:27 +01001369 task_cputime(p, &utime, &stime);
1370 prstatus->pr_utime = ns_to_timeval(utime);
1371 prstatus->pr_stime = ns_to_timeval(stime);
David Howells6d8c4e32006-07-10 04:44:55 -07001372 }
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001373 prstatus->pr_cutime = ns_to_timeval(p->signal->cutime);
1374 prstatus->pr_cstime = ns_to_timeval(p->signal->cstime);
David Howells6d8c4e32006-07-10 04:44:55 -07001375
1376 prstatus->pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
1377 prstatus->pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
1378}
1379
1380static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1381 struct mm_struct *mm)
1382{
David Howellsc69e8d92008-11-14 10:39:19 +11001383 const struct cred *cred;
David Howells6d8c4e32006-07-10 04:44:55 -07001384 unsigned int i, len;
1385
1386 /* first copy the parameters from user space */
1387 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1388
1389 len = mm->arg_end - mm->arg_start;
1390 if (len >= ELF_PRARGSZ)
1391 len = ELF_PRARGSZ - 1;
1392 if (copy_from_user(&psinfo->pr_psargs,
1393 (const char __user *) mm->arg_start, len))
1394 return -EFAULT;
1395 for (i = 0; i < len; i++)
1396 if (psinfo->pr_psargs[i] == 0)
1397 psinfo->pr_psargs[i] = ' ';
1398 psinfo->pr_psargs[len] = 0;
1399
Oleg Nesterov3b34fc52009-06-17 16:27:38 -07001400 rcu_read_lock();
1401 psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1402 rcu_read_unlock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001403 psinfo->pr_pid = task_pid_vnr(p);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001404 psinfo->pr_pgrp = task_pgrp_vnr(p);
1405 psinfo->pr_sid = task_session_vnr(p);
David Howells6d8c4e32006-07-10 04:44:55 -07001406
1407 i = p->state ? ffz(~p->state) + 1 : 0;
1408 psinfo->pr_state = i;
1409 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1410 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1411 psinfo->pr_nice = task_nice(p);
1412 psinfo->pr_flag = p->flags;
David Howellsc69e8d92008-11-14 10:39:19 +11001413 rcu_read_lock();
1414 cred = __task_cred(p);
Eric W. Biedermanebc887b2012-02-07 18:36:10 -08001415 SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid));
1416 SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
David Howellsc69e8d92008-11-14 10:39:19 +11001417 rcu_read_unlock();
David Howells6d8c4e32006-07-10 04:44:55 -07001418 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1419
1420 return 0;
1421}
1422
1423/* Here is the structure in which status of each thread is captured. */
1424struct elf_thread_status
1425{
1426 struct list_head list;
1427 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1428 elf_fpregset_t fpu; /* NT_PRFPREG */
1429 struct task_struct *thread;
1430#ifdef ELF_CORE_COPY_XFPREGS
Mark Nelson5b20cd82007-10-16 23:25:39 -07001431 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
David Howells6d8c4e32006-07-10 04:44:55 -07001432#endif
1433 struct memelfnote notes[3];
1434 int num_notes;
1435};
1436
1437/*
1438 * In order to add the specific thread information for the elf file format,
1439 * we need to keep a linked list of every thread's pr_status and then create
1440 * a single section for them in the final core file.
1441 */
1442static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1443{
1444 struct task_struct *p = t->thread;
1445 int sz = 0;
1446
1447 t->num_notes = 0;
1448
1449 fill_prstatus(&t->prstatus, p, signr);
1450 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1451
1452 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1453 &t->prstatus);
1454 t->num_notes++;
1455 sz += notesize(&t->notes[0]);
1456
1457 t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu);
1458 if (t->prstatus.pr_fpvalid) {
1459 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1460 &t->fpu);
1461 t->num_notes++;
1462 sz += notesize(&t->notes[1]);
1463 }
1464
1465#ifdef ELF_CORE_COPY_XFPREGS
1466 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
Mark Nelson5b20cd82007-10-16 23:25:39 -07001467 fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
1468 sizeof(t->xfpu), &t->xfpu);
David Howells6d8c4e32006-07-10 04:44:55 -07001469 t->num_notes++;
1470 sz += notesize(&t->notes[2]);
1471 }
1472#endif
1473 return sz;
1474}
1475
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001476static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
1477 elf_addr_t e_shoff, int segs)
1478{
1479 elf->e_shoff = e_shoff;
1480 elf->e_shentsize = sizeof(*shdr4extnum);
1481 elf->e_shnum = 1;
1482 elf->e_shstrndx = SHN_UNDEF;
1483
1484 memset(shdr4extnum, 0, sizeof(*shdr4extnum));
1485
1486 shdr4extnum->sh_type = SHT_NULL;
1487 shdr4extnum->sh_size = elf->e_shnum;
1488 shdr4extnum->sh_link = elf->e_shstrndx;
1489 shdr4extnum->sh_info = segs;
1490}
1491
David Howells6d8c4e32006-07-10 04:44:55 -07001492/*
1493 * dump the segments for an MMU process
1494 */
Al Viroe6c1baa2013-10-05 18:58:47 -04001495static bool elf_fdpic_dump_segments(struct coredump_params *cprm)
David Howells6d8c4e32006-07-10 04:44:55 -07001496{
1497 struct vm_area_struct *vma;
1498
1499 for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
1500 unsigned long addr;
1501
Al Viroe6c1baa2013-10-05 18:58:47 -04001502 if (!maydump(vma, cprm->mm_flags))
David Howells6d8c4e32006-07-10 04:44:55 -07001503 continue;
1504
Al Viroe6c1baa2013-10-05 18:58:47 -04001505#ifdef CONFIG_MMU
Hugh Dickinsf3e8fcc2009-09-21 17:03:25 -07001506 for (addr = vma->vm_start; addr < vma->vm_end;
1507 addr += PAGE_SIZE) {
Al Viroe6c1baa2013-10-05 18:58:47 -04001508 bool res;
Hugh Dickinsf3e8fcc2009-09-21 17:03:25 -07001509 struct page *page = get_dump_page(addr);
1510 if (page) {
1511 void *kaddr = kmap(page);
Al Viroe6c1baa2013-10-05 18:58:47 -04001512 res = dump_emit(cprm, kaddr, PAGE_SIZE);
David Howells6d8c4e32006-07-10 04:44:55 -07001513 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001514 put_page(page);
Al Viroe6c1baa2013-10-05 18:58:47 -04001515 } else {
Al Viro9b56d542013-10-08 09:26:08 -04001516 res = dump_skip(cprm, PAGE_SIZE);
Al Viroe6c1baa2013-10-05 18:58:47 -04001517 }
1518 if (!res)
1519 return false;
David Howells6d8c4e32006-07-10 04:44:55 -07001520 }
Al Viroe6c1baa2013-10-05 18:58:47 -04001521#else
1522 if (!dump_emit(cprm, (void *) vma->vm_start,
David Howells6d8c4e32006-07-10 04:44:55 -07001523 vma->vm_end - vma->vm_start))
Al Viroe6c1baa2013-10-05 18:58:47 -04001524 return false;
David Howells6d8c4e32006-07-10 04:44:55 -07001525#endif
Al Viroe6c1baa2013-10-05 18:58:47 -04001526 }
1527 return true;
1528}
David Howells6d8c4e32006-07-10 04:44:55 -07001529
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001530static size_t elf_core_vma_data_size(unsigned long mm_flags)
1531{
1532 struct vm_area_struct *vma;
1533 size_t size = 0;
1534
David Howells47568d42010-03-24 17:02:28 +00001535 for (vma = current->mm->mmap; vma; vma = vma->vm_next)
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001536 if (maydump(vma, mm_flags))
1537 size += vma->vm_end - vma->vm_start;
1538 return size;
1539}
1540
David Howells6d8c4e32006-07-10 04:44:55 -07001541/*
1542 * Actual dumper
1543 *
1544 * This is a two-pass process; first we find the offsets of the bits,
1545 * and then they are actually written out. If we run out of core limit
1546 * we just truncate.
1547 */
Masami Hiramatsuf6151df2009-12-17 15:27:16 -08001548static int elf_fdpic_core_dump(struct coredump_params *cprm)
David Howells6d8c4e32006-07-10 04:44:55 -07001549{
1550#define NUM_NOTES 6
1551 int has_dumped = 0;
1552 mm_segment_t fs;
1553 int segs;
David Howells6d8c4e32006-07-10 04:44:55 -07001554 int i;
1555 struct vm_area_struct *vma;
1556 struct elfhdr *elf = NULL;
Al Viro9b56d542013-10-08 09:26:08 -04001557 loff_t offset = 0, dataoff;
David Howells6d8c4e32006-07-10 04:44:55 -07001558 int numnote;
1559 struct memelfnote *notes = NULL;
1560 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1561 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
David Howells6d8c4e32006-07-10 04:44:55 -07001562 LIST_HEAD(thread_list);
1563 struct list_head *t;
1564 elf_fpregset_t *fpu = NULL;
1565#ifdef ELF_CORE_COPY_XFPREGS
1566 elf_fpxregset_t *xfpu = NULL;
1567#endif
1568 int thread_status_size = 0;
David Howells6d8c4e32006-07-10 04:44:55 -07001569 elf_addr_t *auxv;
Daisuke HATAYAMA93eb2112010-03-05 13:44:09 -08001570 struct elf_phdr *phdr4note = NULL;
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001571 struct elf_shdr *shdr4extnum = NULL;
1572 Elf_Half e_phnum;
1573 elf_addr_t e_shoff;
Al Viroafabada2013-10-14 07:39:56 -04001574 struct core_thread *ct;
1575 struct elf_thread_status *tmp;
David Howells6d8c4e32006-07-10 04:44:55 -07001576
1577 /*
1578 * We no longer stop all VM operations.
1579 *
1580 * This is because those proceses that could possibly change map_count
1581 * or the mmap / vma pages are now blocked in do_exit on current
1582 * finishing this core dump.
1583 *
1584 * Only ptrace can touch these memory addresses, but it doesn't change
1585 * the map_count or the pages allocated. So no possibility of crashing
1586 * exists while dumping the mm->vm_next areas to the core file.
1587 */
1588
1589 /* alloc memory for large data structures: too large to be on stack */
1590 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1591 if (!elf)
1592 goto cleanup;
1593 prstatus = kzalloc(sizeof(*prstatus), GFP_KERNEL);
1594 if (!prstatus)
1595 goto cleanup;
1596 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1597 if (!psinfo)
1598 goto cleanup;
1599 notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
1600 if (!notes)
1601 goto cleanup;
1602 fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
1603 if (!fpu)
1604 goto cleanup;
1605#ifdef ELF_CORE_COPY_XFPREGS
1606 xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
1607 if (!xfpu)
1608 goto cleanup;
1609#endif
1610
Al Viroafabada2013-10-14 07:39:56 -04001611 for (ct = current->mm->core_state->dumper.next;
1612 ct; ct = ct->next) {
1613 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
1614 if (!tmp)
1615 goto cleanup;
1616
1617 tmp->thread = ct->task;
1618 list_add(&tmp->list, &thread_list);
1619 }
1620
1621 list_for_each(t, &thread_list) {
David Howells6d8c4e32006-07-10 04:44:55 -07001622 struct elf_thread_status *tmp;
Al Viroafabada2013-10-14 07:39:56 -04001623 int sz;
Oleg Nesterov24d52882008-07-25 01:47:40 -07001624
Al Viroafabada2013-10-14 07:39:56 -04001625 tmp = list_entry(t, struct elf_thread_status, list);
1626 sz = elf_dump_thread_status(cprm->siginfo->si_signo, tmp);
1627 thread_status_size += sz;
David Howells6d8c4e32006-07-10 04:44:55 -07001628 }
1629
1630 /* now collect the dump for the current */
Denys Vlasenko5ab1c302012-10-04 17:15:29 -07001631 fill_prstatus(prstatus, current, cprm->siginfo->si_signo);
Masami Hiramatsuf6151df2009-12-17 15:27:16 -08001632 elf_core_copy_regs(&prstatus->pr_reg, cprm->regs);
David Howells6d8c4e32006-07-10 04:44:55 -07001633
David Howells6d8c4e32006-07-10 04:44:55 -07001634 segs = current->mm->map_count;
Daisuke HATAYAMA1fcccba2010-03-05 13:44:07 -08001635 segs += elf_core_extra_phdrs();
David Howells6d8c4e32006-07-10 04:44:55 -07001636
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001637 /* for notes section */
1638 segs++;
1639
1640 /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
1641 * this, kernel supports extended numbering. Have a look at
1642 * include/linux/elf.h for further information. */
1643 e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
1644
David Howells6d8c4e32006-07-10 04:44:55 -07001645 /* Set up header */
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001646 fill_elf_fdpic_header(elf, e_phnum);
David Howells6d8c4e32006-07-10 04:44:55 -07001647
1648 has_dumped = 1;
David Howells6d8c4e32006-07-10 04:44:55 -07001649 /*
1650 * Set up the notes in similar form to SVR4 core dumps made
1651 * with info from their /proc.
1652 */
1653
1654 fill_note(notes + 0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1655 fill_psinfo(psinfo, current->group_leader, current->mm);
1656 fill_note(notes + 1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1657
1658 numnote = 2;
1659
1660 auxv = (elf_addr_t *) current->mm->saved_auxv;
1661
1662 i = 0;
1663 do
1664 i += 2;
1665 while (auxv[i - 2] != AT_NULL);
1666 fill_note(&notes[numnote++], "CORE", NT_AUXV,
1667 i * sizeof(elf_addr_t), auxv);
1668
1669 /* Try to dump the FPU. */
1670 if ((prstatus->pr_fpvalid =
Masami Hiramatsuf6151df2009-12-17 15:27:16 -08001671 elf_core_copy_task_fpregs(current, cprm->regs, fpu)))
David Howells6d8c4e32006-07-10 04:44:55 -07001672 fill_note(notes + numnote++,
1673 "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1674#ifdef ELF_CORE_COPY_XFPREGS
1675 if (elf_core_copy_task_xfpregs(current, xfpu))
1676 fill_note(notes + numnote++,
Mark Nelson5b20cd82007-10-16 23:25:39 -07001677 "LINUX", ELF_CORE_XFPREG_TYPE, sizeof(*xfpu), xfpu);
David Howells6d8c4e32006-07-10 04:44:55 -07001678#endif
1679
1680 fs = get_fs();
1681 set_fs(KERNEL_DS);
1682
David Howells6d8c4e32006-07-10 04:44:55 -07001683 offset += sizeof(*elf); /* Elf header */
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001684 offset += segs * sizeof(struct elf_phdr); /* Program headers */
David Howells6d8c4e32006-07-10 04:44:55 -07001685
1686 /* Write notes phdr entry */
1687 {
David Howells6d8c4e32006-07-10 04:44:55 -07001688 int sz = 0;
1689
1690 for (i = 0; i < numnote; i++)
1691 sz += notesize(notes + i);
1692
1693 sz += thread_status_size;
1694
Daisuke HATAYAMA93eb2112010-03-05 13:44:09 -08001695 phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
1696 if (!phdr4note)
Daisuke HATAYAMA088e7af2010-03-05 13:44:06 -08001697 goto end_coredump;
Daisuke HATAYAMA93eb2112010-03-05 13:44:09 -08001698
1699 fill_elf_note_phdr(phdr4note, sz, offset);
1700 offset += sz;
David Howells6d8c4e32006-07-10 04:44:55 -07001701 }
1702
1703 /* Page-align dumped data */
1704 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1705
Masami Hiramatsu30736a42010-03-05 13:44:12 -08001706 offset += elf_core_vma_data_size(cprm->mm_flags);
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001707 offset += elf_core_extra_data_size();
1708 e_shoff = offset;
1709
1710 if (e_phnum == PN_XNUM) {
1711 shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
1712 if (!shdr4extnum)
1713 goto end_coredump;
1714 fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
1715 }
1716
1717 offset = dataoff;
1718
Al Viroe6c1baa2013-10-05 18:58:47 -04001719 if (!dump_emit(cprm, elf, sizeof(*elf)))
Daisuke HATAYAMA93eb2112010-03-05 13:44:09 -08001720 goto end_coredump;
1721
Al Viroe6c1baa2013-10-05 18:58:47 -04001722 if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note)))
Daisuke HATAYAMA93eb2112010-03-05 13:44:09 -08001723 goto end_coredump;
1724
David Howells6d8c4e32006-07-10 04:44:55 -07001725 /* write program headers for segments dump */
David Howells8feae132009-01-08 12:04:47 +00001726 for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
David Howells6d8c4e32006-07-10 04:44:55 -07001727 struct elf_phdr phdr;
1728 size_t sz;
1729
David Howells6d8c4e32006-07-10 04:44:55 -07001730 sz = vma->vm_end - vma->vm_start;
1731
1732 phdr.p_type = PT_LOAD;
1733 phdr.p_offset = offset;
1734 phdr.p_vaddr = vma->vm_start;
1735 phdr.p_paddr = 0;
Masami Hiramatsu30736a42010-03-05 13:44:12 -08001736 phdr.p_filesz = maydump(vma, cprm->mm_flags) ? sz : 0;
David Howells6d8c4e32006-07-10 04:44:55 -07001737 phdr.p_memsz = sz;
1738 offset += phdr.p_filesz;
1739 phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
1740 if (vma->vm_flags & VM_WRITE)
1741 phdr.p_flags |= PF_W;
1742 if (vma->vm_flags & VM_EXEC)
1743 phdr.p_flags |= PF_X;
1744 phdr.p_align = ELF_EXEC_PAGESIZE;
1745
Al Viroe6c1baa2013-10-05 18:58:47 -04001746 if (!dump_emit(cprm, &phdr, sizeof(phdr)))
Daisuke HATAYAMA088e7af2010-03-05 13:44:06 -08001747 goto end_coredump;
David Howells6d8c4e32006-07-10 04:44:55 -07001748 }
1749
Al Viro506f21c2013-10-05 17:22:57 -04001750 if (!elf_core_write_extra_phdrs(cprm, offset))
Daisuke HATAYAMA1fcccba2010-03-05 13:44:07 -08001751 goto end_coredump;
David Howells6d8c4e32006-07-10 04:44:55 -07001752
1753 /* write out the notes section */
1754 for (i = 0; i < numnote; i++)
Al Viroe6c1baa2013-10-05 18:58:47 -04001755 if (!writenote(notes + i, cprm))
David Howells6d8c4e32006-07-10 04:44:55 -07001756 goto end_coredump;
1757
1758 /* write out the thread status notes section */
1759 list_for_each(t, &thread_list) {
1760 struct elf_thread_status *tmp =
1761 list_entry(t, struct elf_thread_status, list);
1762
1763 for (i = 0; i < tmp->num_notes; i++)
Al Viroe6c1baa2013-10-05 18:58:47 -04001764 if (!writenote(&tmp->notes[i], cprm))
David Howells6d8c4e32006-07-10 04:44:55 -07001765 goto end_coredump;
1766 }
1767
Mateusz Guzik1607f092016-06-05 23:14:14 +02001768 if (!dump_skip(cprm, dataoff - cprm->pos))
David Howells6d8c4e32006-07-10 04:44:55 -07001769 goto end_coredump;
1770
Al Viroe6c1baa2013-10-05 18:58:47 -04001771 if (!elf_fdpic_dump_segments(cprm))
1772 goto end_coredump;
1773
Al Viroaa3e7ea2013-10-05 17:50:15 -04001774 if (!elf_core_write_extra_data(cprm))
Daisuke HATAYAMA1fcccba2010-03-05 13:44:07 -08001775 goto end_coredump;
David Howells6d8c4e32006-07-10 04:44:55 -07001776
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001777 if (e_phnum == PN_XNUM) {
Al Viroe6c1baa2013-10-05 18:58:47 -04001778 if (!dump_emit(cprm, shdr4extnum, sizeof(*shdr4extnum)))
Daisuke HATAYAMA8d9032b2010-03-05 13:44:10 -08001779 goto end_coredump;
1780 }
1781
Daisuke HATAYAMA2f489122010-01-04 15:42:14 +09001782 if (cprm->file->f_pos != offset) {
David Howells6d8c4e32006-07-10 04:44:55 -07001783 /* Sanity check */
1784 printk(KERN_WARNING
1785 "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
Daisuke HATAYAMA2f489122010-01-04 15:42:14 +09001786 cprm->file->f_pos, offset);
David Howells6d8c4e32006-07-10 04:44:55 -07001787 }
1788
1789end_coredump:
1790 set_fs(fs);
1791
1792cleanup:
1793 while (!list_empty(&thread_list)) {
1794 struct list_head *tmp = thread_list.next;
1795 list_del(tmp);
1796 kfree(list_entry(tmp, struct elf_thread_status, list));
1797 }
Daisuke HATAYAMA93eb2112010-03-05 13:44:09 -08001798 kfree(phdr4note);
David Howells6d8c4e32006-07-10 04:44:55 -07001799 kfree(elf);
1800 kfree(prstatus);
1801 kfree(psinfo);
1802 kfree(notes);
1803 kfree(fpu);
Davidlohr Buesobcb65a72011-07-06 12:26:05 +01001804 kfree(shdr4extnum);
David Howells6d8c4e32006-07-10 04:44:55 -07001805#ifdef ELF_CORE_COPY_XFPREGS
1806 kfree(xfpu);
1807#endif
1808 return has_dumped;
1809#undef NUM_NOTES
1810}
1811
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08001812#endif /* CONFIG_ELF_CORE */