exec/binfmt_script: Don't modify bprm->buf and then return -ENOEXEC
[linux-2.6-block.git] / fs / binfmt_elf_fdpic.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/* binfmt_elf_fdpic.c: FDPIC ELF binary format
3 *
8a2ab7f5 4 * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
1da177e4
LT
5 * Written by David Howells (dhowells@redhat.com)
6 * Derived from binfmt_elf.c
1da177e4
LT
7 */
8
9#include <linux/module.h>
10
11#include <linux/fs.h>
12#include <linux/stat.h>
13#include <linux/sched.h>
68db0cf1
IM
14#include <linux/sched/coredump.h>
15#include <linux/sched/task_stack.h>
32ef5517 16#include <linux/sched/cputime.h>
1da177e4
LT
17#include <linux/mm.h>
18#include <linux/mman.h>
19#include <linux/errno.h>
20#include <linux/signal.h>
21#include <linux/binfmts.h>
22#include <linux/string.h>
23#include <linux/file.h>
24#include <linux/fcntl.h>
25#include <linux/slab.h>
6d8c4e3b 26#include <linux/pagemap.h>
5edc2a51 27#include <linux/security.h>
1da177e4 28#include <linux/highmem.h>
6d8c4e3b 29#include <linux/highuid.h>
1da177e4
LT
30#include <linux/personality.h>
31#include <linux/ptrace.h>
32#include <linux/init.h>
1da177e4
LT
33#include <linux/elf.h>
34#include <linux/elf-fdpic.h>
35#include <linux/elfcore.h>
088e7af7 36#include <linux/coredump.h>
ab27a8d0 37#include <linux/dax.h>
1da177e4 38
7c0f6ba6 39#include <linux/uaccess.h>
1da177e4
LT
40#include <asm/param.h>
41#include <asm/pgalloc.h>
42
43typedef char *elf_caddr_t;
1da177e4
LT
44
45#if 0
46#define kdebug(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
47#else
48#define kdebug(fmt, ...) do {} while(0)
49#endif
50
6d8c4e3b
DH
51#if 0
52#define kdcore(fmt, ...) printk("FDPIC "fmt"\n" ,##__VA_ARGS__ )
53#else
54#define kdcore(fmt, ...) do {} while(0)
55#endif
56
1da177e4
LT
57MODULE_LICENSE("GPL");
58
71613c3b 59static int load_elf_fdpic_binary(struct linux_binprm *);
8a2ab7f5
DH
60static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
61static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
62 struct mm_struct *, const char *);
1da177e4 63
8a2ab7f5
DH
64static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
65 struct elf_fdpic_params *,
66 struct elf_fdpic_params *);
1da177e4
LT
67
68#ifndef CONFIG_MMU
8a2ab7f5
DH
69static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
70 struct file *,
71 struct mm_struct *);
1da177e4
LT
72#endif
73
8a2ab7f5
DH
74static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
75 struct file *, struct mm_struct *);
1da177e4 76
698ba7b5 77#ifdef CONFIG_ELF_CORE
f6151dfe 78static int elf_fdpic_core_dump(struct coredump_params *cprm);
6d8c4e3b
DH
79#endif
80
1da177e4
LT
81static struct linux_binfmt elf_fdpic_format = {
82 .module = THIS_MODULE,
83 .load_binary = load_elf_fdpic_binary,
698ba7b5 84#ifdef CONFIG_ELF_CORE
6d8c4e3b
DH
85 .core_dump = elf_fdpic_core_dump,
86#endif
1da177e4
LT
87 .min_coredump = ELF_EXEC_PAGESIZE,
88};
89
8a2ab7f5
DH
90static int __init init_elf_fdpic_binfmt(void)
91{
8fc3dc5a
AV
92 register_binfmt(&elf_fdpic_format);
93 return 0;
8a2ab7f5 94}
1da177e4 95
8a2ab7f5
DH
96static void __exit exit_elf_fdpic_binfmt(void)
97{
98 unregister_binfmt(&elf_fdpic_format);
99}
100
6d8c4e3b 101core_initcall(init_elf_fdpic_binfmt);
8a2ab7f5 102module_exit(exit_elf_fdpic_binfmt);
1da177e4 103
1bde925d 104static int is_elf(struct elfhdr *hdr, struct file *file)
1da177e4
LT
105{
106 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
107 return 0;
108 if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
109 return 0;
1bde925d 110 if (!elf_check_arch(hdr))
1da177e4 111 return 0;
72c2d531 112 if (!file->f_op->mmap)
1da177e4
LT
113 return 0;
114 return 1;
115}
116
1bde925d
RF
117#ifndef elf_check_fdpic
118#define elf_check_fdpic(x) 0
119#endif
120
121#ifndef elf_check_const_displacement
122#define elf_check_const_displacement(x) 0
123#endif
124
125static int is_constdisp(struct elfhdr *hdr)
126{
127 if (!elf_check_fdpic(hdr))
128 return 1;
129 if (elf_check_const_displacement(hdr))
130 return 1;
131 return 0;
132}
133
1da177e4
LT
134/*****************************************************************************/
135/*
136 * read the program headers table into memory
137 */
8a2ab7f5
DH
138static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
139 struct file *file)
1da177e4
LT
140{
141 struct elf32_phdr *phdr;
142 unsigned long size;
143 int retval, loop;
bdd1d2d3 144 loff_t pos = params->hdr.e_phoff;
1da177e4
LT
145
146 if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
147 return -ENOMEM;
148 if (params->hdr.e_phnum > 65536U / sizeof(struct elf_phdr))
149 return -ENOMEM;
150
151 size = params->hdr.e_phnum * sizeof(struct elf_phdr);
152 params->phdrs = kmalloc(size, GFP_KERNEL);
153 if (!params->phdrs)
154 return -ENOMEM;
155
bdd1d2d3 156 retval = kernel_read(file, params->phdrs, size, &pos);
e1d2c8b6
DH
157 if (unlikely(retval != size))
158 return retval < 0 ? retval : -ENOEXEC;
1da177e4
LT
159
160 /* determine stack size for this binary */
161 phdr = params->phdrs;
162 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
163 if (phdr->p_type != PT_GNU_STACK)
164 continue;
165
166 if (phdr->p_flags & PF_X)
167 params->flags |= ELF_FDPIC_FLAG_EXEC_STACK;
168 else
169 params->flags |= ELF_FDPIC_FLAG_NOEXEC_STACK;
170
171 params->stack_size = phdr->p_memsz;
172 break;
173 }
174
175 return 0;
8a2ab7f5 176}
1da177e4
LT
177
178/*****************************************************************************/
179/*
180 * load an fdpic binary into various bits of memory
181 */
71613c3b 182static int load_elf_fdpic_binary(struct linux_binprm *bprm)
1da177e4
LT
183{
184 struct elf_fdpic_params exec_params, interp_params;
71613c3b 185 struct pt_regs *regs = current_pt_regs();
1da177e4 186 struct elf_phdr *phdr;
8a2ab7f5 187 unsigned long stack_size, entryaddr;
8a2ab7f5
DH
188#ifdef ELF_FDPIC_PLAT_INIT
189 unsigned long dynaddr;
04e4f2b1
MF
190#endif
191#ifndef CONFIG_MMU
192 unsigned long stack_prot;
8a2ab7f5 193#endif
1da177e4
LT
194 struct file *interpreter = NULL; /* to shut gcc up */
195 char *interpreter_name = NULL;
196 int executable_stack;
197 int retval, i;
bdd1d2d3 198 loff_t pos;
1da177e4 199
aa289b47
DH
200 kdebug("____ LOAD %d ____", current->pid);
201
1da177e4
LT
202 memset(&exec_params, 0, sizeof(exec_params));
203 memset(&interp_params, 0, sizeof(interp_params));
204
205 exec_params.hdr = *(struct elfhdr *) bprm->buf;
206 exec_params.flags = ELF_FDPIC_FLAG_PRESENT | ELF_FDPIC_FLAG_EXECUTABLE;
207
208 /* check that this is a binary we know how to deal with */
209 retval = -ENOEXEC;
1bde925d 210 if (!is_elf(&exec_params.hdr, bprm->file))
1da177e4 211 goto error;
1bde925d
RF
212 if (!elf_check_fdpic(&exec_params.hdr)) {
213#ifdef CONFIG_MMU
214 /* binfmt_elf handles non-fdpic elf except on nommu */
215 goto error;
216#else
217 /* nommu can only load ET_DYN (PIE) ELF */
218 if (exec_params.hdr.e_type != ET_DYN)
219 goto error;
220#endif
221 }
1da177e4
LT
222
223 /* read the program header table */
224 retval = elf_fdpic_fetch_phdrs(&exec_params, bprm->file);
225 if (retval < 0)
226 goto error;
227
228 /* scan for a program header that specifies an interpreter */
229 phdr = exec_params.phdrs;
230
231 for (i = 0; i < exec_params.hdr.e_phnum; i++, phdr++) {
232 switch (phdr->p_type) {
233 case PT_INTERP:
234 retval = -ENOMEM;
235 if (phdr->p_filesz > PATH_MAX)
236 goto error;
237 retval = -ENOENT;
238 if (phdr->p_filesz < 2)
239 goto error;
240
241 /* read the name of the interpreter into memory */
792db3af 242 interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
1da177e4
LT
243 if (!interpreter_name)
244 goto error;
245
bdd1d2d3
CH
246 pos = phdr->p_offset;
247 retval = kernel_read(bprm->file, interpreter_name,
248 phdr->p_filesz, &pos);
e1d2c8b6
DH
249 if (unlikely(retval != phdr->p_filesz)) {
250 if (retval >= 0)
251 retval = -ENOEXEC;
1da177e4 252 goto error;
e1d2c8b6 253 }
1da177e4
LT
254
255 retval = -ENOENT;
256 if (interpreter_name[phdr->p_filesz - 1] != '\0')
257 goto error;
258
259 kdebug("Using ELF interpreter %s", interpreter_name);
260
261 /* replace the program with the interpreter */
262 interpreter = open_exec(interpreter_name);
263 retval = PTR_ERR(interpreter);
264 if (IS_ERR(interpreter)) {
265 interpreter = NULL;
266 goto error;
267 }
268
1fb84496
AD
269 /*
270 * If the binary is not readable then enforce
271 * mm->dumpable = 0 regardless of the interpreter's
272 * permissions.
273 */
1b5d783c 274 would_dump(bprm, interpreter);
1fb84496 275
bdd1d2d3
CH
276 pos = 0;
277 retval = kernel_read(interpreter, bprm->buf,
278 BINPRM_BUF_SIZE, &pos);
e1d2c8b6
DH
279 if (unlikely(retval != BINPRM_BUF_SIZE)) {
280 if (retval >= 0)
281 retval = -ENOEXEC;
1da177e4 282 goto error;
e1d2c8b6 283 }
1da177e4
LT
284
285 interp_params.hdr = *((struct elfhdr *) bprm->buf);
286 break;
287
288 case PT_LOAD:
289#ifdef CONFIG_MMU
290 if (exec_params.load_addr == 0)
291 exec_params.load_addr = phdr->p_vaddr;
292#endif
293 break;
294 }
295
296 }
297
1bde925d 298 if (is_constdisp(&exec_params.hdr))
1da177e4
LT
299 exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
300
301 /* perform insanity checks on the interpreter */
302 if (interpreter_name) {
303 retval = -ELIBBAD;
1bde925d 304 if (!is_elf(&interp_params.hdr, interpreter))
1da177e4
LT
305 goto error;
306
307 interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
308
309 /* read the interpreter's program header table */
310 retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
311 if (retval < 0)
312 goto error;
313 }
314
315 stack_size = exec_params.stack_size;
1da177e4
LT
316 if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
317 executable_stack = EXSTACK_ENABLE_X;
318 else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
319 executable_stack = EXSTACK_DISABLE_X;
1da177e4
LT
320 else
321 executable_stack = EXSTACK_DEFAULT;
322
8e8b63a6
DH
323 if (stack_size == 0) {
324 stack_size = interp_params.stack_size;
325 if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
326 executable_stack = EXSTACK_ENABLE_X;
327 else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
328 executable_stack = EXSTACK_DISABLE_X;
329 else
330 executable_stack = EXSTACK_DEFAULT;
331 }
332
1da177e4
LT
333 retval = -ENOEXEC;
334 if (stack_size == 0)
1bde925d 335 stack_size = 131072UL; /* same as exec.c's default commit */
1da177e4 336
1bde925d 337 if (is_constdisp(&interp_params.hdr))
1da177e4
LT
338 interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
339
340 /* flush all traces of the currently running executable */
2388777a 341 retval = begin_new_exec(bprm);
1da177e4
LT
342 if (retval)
343 goto error;
344
345 /* there's now no turning back... the old userspace image is dead,
19d860a1
AV
346 * defunct, deceased, etc.
347 */
1bde925d
RF
348 if (elf_check_fdpic(&exec_params.hdr))
349 set_personality(PER_LINUX_FDPIC);
350 else
351 set_personality(PER_LINUX);
04e4f2b1
MF
352 if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
353 current->personality |= READ_IMPLIES_EXEC;
221af7f8
LT
354
355 setup_new_exec(bprm);
356
1da177e4
LT
357 set_binfmt(&elf_fdpic_format);
358
359 current->mm->start_code = 0;
360 current->mm->end_code = 0;
361 current->mm->start_stack = 0;
362 current->mm->start_data = 0;
363 current->mm->end_data = 0;
364 current->mm->context.exec_fdpic_loadmap = 0;
365 current->mm->context.interp_fdpic_loadmap = 0;
366
1da177e4
LT
367#ifdef CONFIG_MMU
368 elf_fdpic_arch_lay_out_mm(&exec_params,
369 &interp_params,
370 &current->mm->start_stack,
371 &current->mm->start_brk);
1da177e4 372
8a2ab7f5
DH
373 retval = setup_arg_pages(bprm, current->mm->start_stack,
374 executable_stack);
19d860a1
AV
375 if (retval < 0)
376 goto error;
382e67ae
NP
377#ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
378 retval = arch_setup_additional_pages(bprm, !!interpreter_name);
379 if (retval < 0)
380 goto error;
381#endif
1da177e4
LT
382#endif
383
384 /* load the executable and interpreter into memory */
8a2ab7f5
DH
385 retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
386 "executable");
1da177e4 387 if (retval < 0)
19d860a1 388 goto error;
1da177e4
LT
389
390 if (interpreter_name) {
391 retval = elf_fdpic_map_file(&interp_params, interpreter,
392 current->mm, "interpreter");
393 if (retval < 0) {
394 printk(KERN_ERR "Unable to load interpreter\n");
19d860a1 395 goto error;
1da177e4
LT
396 }
397
398 allow_write_access(interpreter);
399 fput(interpreter);
400 interpreter = NULL;
401 }
402
403#ifdef CONFIG_MMU
404 if (!current->mm->start_brk)
405 current->mm->start_brk = current->mm->end_data;
406
8a2ab7f5
DH
407 current->mm->brk = current->mm->start_brk =
408 PAGE_ALIGN(current->mm->start_brk);
1da177e4
LT
409
410#else
4ac31311 411 /* create a stack area and zero-size brk area */
1da177e4
LT
412 stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
413 if (stack_size < PAGE_SIZE * 2)
414 stack_size = PAGE_SIZE * 2;
415
04e4f2b1
MF
416 stack_prot = PROT_READ | PROT_WRITE;
417 if (executable_stack == EXSTACK_ENABLE_X ||
418 (executable_stack == EXSTACK_DEFAULT && VM_STACK_FLAGS & VM_EXEC))
419 stack_prot |= PROT_EXEC;
420
6be5ceb0 421 current->mm->start_brk = vm_mmap(NULL, 0, stack_size, stack_prot,
ea637639
JZ
422 MAP_PRIVATE | MAP_ANONYMOUS |
423 MAP_UNINITIALIZED | MAP_GROWSDOWN,
1da177e4
LT
424 0);
425
8a2ab7f5 426 if (IS_ERR_VALUE(current->mm->start_brk)) {
1da177e4
LT
427 retval = current->mm->start_brk;
428 current->mm->start_brk = 0;
19d860a1 429 goto error;
1da177e4
LT
430 }
431
1da177e4
LT
432 current->mm->brk = current->mm->start_brk;
433 current->mm->context.end_brk = current->mm->start_brk;
1da177e4
LT
434 current->mm->start_stack = current->mm->start_brk + stack_size;
435#endif
436
8a2ab7f5
DH
437 if (create_elf_fdpic_tables(bprm, current->mm,
438 &exec_params, &interp_params) < 0)
19d860a1 439 goto error;
1da177e4 440
8a2ab7f5
DH
441 kdebug("- start_code %lx", current->mm->start_code);
442 kdebug("- end_code %lx", current->mm->end_code);
443 kdebug("- start_data %lx", current->mm->start_data);
444 kdebug("- end_data %lx", current->mm->end_data);
445 kdebug("- start_brk %lx", current->mm->start_brk);
446 kdebug("- brk %lx", current->mm->brk);
447 kdebug("- start_stack %lx", current->mm->start_stack);
1da177e4
LT
448
449#ifdef ELF_FDPIC_PLAT_INIT
450 /*
451 * The ABI may specify that certain registers be set up in special
452 * ways (on i386 %edx is the address of a DT_FINI function, for
453 * example. This macro performs whatever initialization to
454 * the regs structure is required.
455 */
8a2ab7f5
DH
456 dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
457 ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
458 dynaddr);
1da177e4
LT
459#endif
460
b8383831 461 finalize_exec(bprm);
1da177e4 462 /* everything is now ready... get the userspace context ready to roll */
8a2ab7f5
DH
463 entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
464 start_thread(regs, entryaddr, current->mm->start_stack);
1da177e4 465
1da177e4
LT
466 retval = 0;
467
468error:
469 if (interpreter) {
470 allow_write_access(interpreter);
471 fput(interpreter);
472 }
f99d49ad
JJ
473 kfree(interpreter_name);
474 kfree(exec_params.phdrs);
475 kfree(exec_params.loadmap);
476 kfree(interp_params.phdrs);
477 kfree(interp_params.loadmap);
1da177e4 478 return retval;
8a2ab7f5 479}
1da177e4
LT
480
481/*****************************************************************************/
ec23847d
PM
482
483#ifndef ELF_BASE_PLATFORM
484/*
485 * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
486 * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
487 * will be copied to the user stack in the same manner as AT_PLATFORM.
488 */
489#define ELF_BASE_PLATFORM NULL
490#endif
491
1da177e4 492/*
c7637941
PM
493 * present useful information to the program by shovelling it onto the new
494 * process's stack
1da177e4
LT
495 */
496static int create_elf_fdpic_tables(struct linux_binprm *bprm,
497 struct mm_struct *mm,
498 struct elf_fdpic_params *exec_params,
499 struct elf_fdpic_params *interp_params)
500{
86a264ab 501 const struct cred *cred = current_cred();
1da177e4 502 unsigned long sp, csp, nitems;
530018bf 503 elf_caddr_t __user *argv, *envp;
1da177e4 504 size_t platform_len = 0, len;
ec23847d
PM
505 char *k_platform, *k_base_platform;
506 char __user *u_platform, *u_base_platform, *p;
1da177e4 507 int loop;
9b14ec35 508 int nr; /* reset for each csp adjustment */
1da177e4 509
1da177e4 510#ifdef CONFIG_MMU
c7637941
PM
511 /* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
512 * by the processes running on the same package. One thing we can do is
513 * to shuffle the initial stack for them, so we give the architecture
514 * an opportunity to do so here.
515 */
516 sp = arch_align_stack(bprm->p);
1da177e4
LT
517#else
518 sp = mm->start_stack;
519
520 /* stack the program arguments and environment */
7e7ec6a9 521 if (transfer_args_to_stack(bprm, &sp) < 0)
1da177e4 522 return -EFAULT;
7e7ec6a9 523 sp &= ~15;
1da177e4
LT
524#endif
525
ec23847d
PM
526 /*
527 * If this architecture has a platform capability string, copy it
528 * to userspace. In some cases (Sparc), this info is impossible
529 * for userspace to get any other way, in others (i386) it is
530 * merely difficult.
531 */
1da177e4 532 k_platform = ELF_PLATFORM;
1aeb21d6 533 u_platform = NULL;
1da177e4
LT
534
535 if (k_platform) {
536 platform_len = strlen(k_platform) + 1;
537 sp -= platform_len;
530018bf 538 u_platform = (char __user *) sp;
1da177e4
LT
539 if (__copy_to_user(u_platform, k_platform, platform_len) != 0)
540 return -EFAULT;
541 }
542
ec23847d
PM
543 /*
544 * If this architecture has a "base" platform capability
545 * string, copy it to userspace.
546 */
547 k_base_platform = ELF_BASE_PLATFORM;
548 u_base_platform = NULL;
549
550 if (k_base_platform) {
551 platform_len = strlen(k_base_platform) + 1;
552 sp -= platform_len;
553 u_base_platform = (char __user *) sp;
554 if (__copy_to_user(u_base_platform, k_base_platform, platform_len) != 0)
555 return -EFAULT;
556 }
557
1da177e4
LT
558 sp &= ~7UL;
559
560 /* stack the load map(s) */
561 len = sizeof(struct elf32_fdpic_loadmap);
562 len += sizeof(struct elf32_fdpic_loadseg) * exec_params->loadmap->nsegs;
563 sp = (sp - len) & ~7UL;
564 exec_params->map_addr = sp;
565
530018bf 566 if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
1da177e4
LT
567 return -EFAULT;
568
569 current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
570
571 if (interp_params->loadmap) {
572 len = sizeof(struct elf32_fdpic_loadmap);
8a2ab7f5
DH
573 len += sizeof(struct elf32_fdpic_loadseg) *
574 interp_params->loadmap->nsegs;
1da177e4
LT
575 sp = (sp - len) & ~7UL;
576 interp_params->map_addr = sp;
577
8a2ab7f5
DH
578 if (copy_to_user((void __user *) sp, interp_params->loadmap,
579 len) != 0)
1da177e4
LT
580 return -EFAULT;
581
582 current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
583 }
584
585 /* force 16 byte _final_ alignment here for generality */
5edc2a51 586#define DLINFO_ITEMS 15
1da177e4 587
ec23847d
PM
588 nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0) +
589 (k_base_platform ? 1 : 0) + AT_VECTOR_SIZE_ARCH;
1da177e4 590
5edc2a51
PM
591 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD)
592 nitems++;
593
1da177e4
LT
594 csp = sp;
595 sp -= nitems * 2 * sizeof(unsigned long);
596 sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
597 sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
598 sp -= 1 * sizeof(unsigned long); /* argc */
599
600 csp -= sp & 15UL;
601 sp -= sp & 15UL;
602
603 /* put the ELF interpreter info on the stack */
9b14ec35 604#define NEW_AUX_ENT(id, val) \
8a2ab7f5
DH
605 do { \
606 struct { unsigned long _id, _val; } __user *ent; \
607 \
608 ent = (void __user *) csp; \
609 __put_user((id), &ent[nr]._id); \
610 __put_user((val), &ent[nr]._val); \
9b14ec35 611 nr++; \
1da177e4
LT
612 } while (0)
613
9b14ec35 614 nr = 0;
1da177e4 615 csp -= 2 * sizeof(unsigned long);
9b14ec35 616 NEW_AUX_ENT(AT_NULL, 0);
1da177e4 617 if (k_platform) {
9b14ec35 618 nr = 0;
1da177e4 619 csp -= 2 * sizeof(unsigned long);
9b14ec35 620 NEW_AUX_ENT(AT_PLATFORM,
8a2ab7f5 621 (elf_addr_t) (unsigned long) u_platform);
1da177e4
LT
622 }
623
ec23847d
PM
624 if (k_base_platform) {
625 nr = 0;
626 csp -= 2 * sizeof(unsigned long);
627 NEW_AUX_ENT(AT_BASE_PLATFORM,
628 (elf_addr_t) (unsigned long) u_base_platform);
629 }
630
5edc2a51
PM
631 if (bprm->interp_flags & BINPRM_FLAGS_EXECFD) {
632 nr = 0;
633 csp -= 2 * sizeof(unsigned long);
634 NEW_AUX_ENT(AT_EXECFD, bprm->interp_data);
635 }
636
9b14ec35 637 nr = 0;
1da177e4 638 csp -= DLINFO_ITEMS * 2 * sizeof(unsigned long);
2171364d
MN
639 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
640#ifdef ELF_HWCAP2
641 NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
642#endif
9b14ec35
PM
643 NEW_AUX_ENT(AT_PAGESZ, PAGE_SIZE);
644 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
645 NEW_AUX_ENT(AT_PHDR, exec_params->ph_addr);
646 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
647 NEW_AUX_ENT(AT_PHNUM, exec_params->hdr.e_phnum);
648 NEW_AUX_ENT(AT_BASE, interp_params->elfhdr_addr);
649 NEW_AUX_ENT(AT_FLAGS, 0);
650 NEW_AUX_ENT(AT_ENTRY, exec_params->entry_addr);
ebc887b2
EB
651 NEW_AUX_ENT(AT_UID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->uid));
652 NEW_AUX_ENT(AT_EUID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
653 NEW_AUX_ENT(AT_GID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->gid));
654 NEW_AUX_ENT(AT_EGID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid));
c425e189 655 NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
5edc2a51 656 NEW_AUX_ENT(AT_EXECFN, bprm->exec);
1da177e4
LT
657
658#ifdef ARCH_DLINFO
9b14ec35
PM
659 nr = 0;
660 csp -= AT_VECTOR_SIZE_ARCH * 2 * sizeof(unsigned long);
661
1da177e4
LT
662 /* ARCH_DLINFO must come last so platform specific code can enforce
663 * special alignment requirements on the AUXV if necessary (eg. PPC).
664 */
665 ARCH_DLINFO;
666#endif
667#undef NEW_AUX_ENT
668
669 /* allocate room for argv[] and envv[] */
670 csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
530018bf 671 envp = (elf_caddr_t __user *) csp;
1da177e4 672 csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
530018bf 673 argv = (elf_caddr_t __user *) csp;
1da177e4
LT
674
675 /* stack argc */
676 csp -= sizeof(unsigned long);
530018bf 677 __put_user(bprm->argc, (unsigned long __user *) csp);
1da177e4 678
88bcd512 679 BUG_ON(csp != sp);
1da177e4
LT
680
681 /* fill in the argv[] array */
682#ifdef CONFIG_MMU
683 current->mm->arg_start = bprm->p;
684#else
8a2ab7f5
DH
685 current->mm->arg_start = current->mm->start_stack -
686 (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
1da177e4
LT
687#endif
688
530018bf 689 p = (char __user *) current->mm->arg_start;
1da177e4
LT
690 for (loop = bprm->argc; loop > 0; loop--) {
691 __put_user((elf_caddr_t) p, argv++);
b6a2fea3
OW
692 len = strnlen_user(p, MAX_ARG_STRLEN);
693 if (!len || len > MAX_ARG_STRLEN)
1da177e4
LT
694 return -EINVAL;
695 p += len;
696 }
697 __put_user(NULL, argv);
698 current->mm->arg_end = (unsigned long) p;
699
700 /* fill in the envv[] array */
701 current->mm->env_start = (unsigned long) p;
702 for (loop = bprm->envc; loop > 0; loop--) {
703 __put_user((elf_caddr_t)(unsigned long) p, envp++);
b6a2fea3
OW
704 len = strnlen_user(p, MAX_ARG_STRLEN);
705 if (!len || len > MAX_ARG_STRLEN)
1da177e4
LT
706 return -EINVAL;
707 p += len;
708 }
709 __put_user(NULL, envp);
710 current->mm->env_end = (unsigned long) p;
711
712 mm->start_stack = (unsigned long) sp;
713 return 0;
8a2ab7f5 714}
1da177e4
LT
715
716/*****************************************************************************/
1da177e4
LT
717/*
718 * load the appropriate binary image (executable or interpreter) into memory
719 * - we assume no MMU is available
720 * - if no other PIC bits are set in params->hdr->e_flags
721 * - we assume that the LOADable segments in the binary are independently relocatable
722 * - we assume R/O executable segments are shareable
723 * - else
724 * - we assume the loadable parts of the image to require fixed displacement
725 * - the image is not shareable
726 */
727static int elf_fdpic_map_file(struct elf_fdpic_params *params,
728 struct file *file,
729 struct mm_struct *mm,
730 const char *what)
731{
732 struct elf32_fdpic_loadmap *loadmap;
733#ifdef CONFIG_MMU
734 struct elf32_fdpic_loadseg *mseg;
735#endif
736 struct elf32_fdpic_loadseg *seg;
737 struct elf32_phdr *phdr;
738 unsigned long load_addr, stop;
739 unsigned nloads, tmp;
740 size_t size;
741 int loop, ret;
742
743 /* allocate a load map table */
744 nloads = 0;
745 for (loop = 0; loop < params->hdr.e_phnum; loop++)
746 if (params->phdrs[loop].p_type == PT_LOAD)
747 nloads++;
748
749 if (nloads == 0)
750 return -ELIBBAD;
751
752 size = sizeof(*loadmap) + nloads * sizeof(*seg);
b87576d5 753 loadmap = kzalloc(size, GFP_KERNEL);
1da177e4
LT
754 if (!loadmap)
755 return -ENOMEM;
756
757 params->loadmap = loadmap;
1da177e4
LT
758
759 loadmap->version = ELF32_FDPIC_LOADMAP_VERSION;
760 loadmap->nsegs = nloads;
761
762 load_addr = params->load_addr;
763 seg = loadmap->segs;
764
765 /* map the requested LOADs into the memory space */
766 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
767 case ELF_FDPIC_FLAG_CONSTDISP:
768 case ELF_FDPIC_FLAG_CONTIGUOUS:
769#ifndef CONFIG_MMU
770 ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
771 if (ret < 0)
772 return ret;
773 break;
774#endif
775 default:
776 ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
777 if (ret < 0)
778 return ret;
779 break;
780 }
781
782 /* map the entry point */
783 if (params->hdr.e_entry) {
784 seg = loadmap->segs;
785 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
786 if (params->hdr.e_entry >= seg->p_vaddr &&
8a2ab7f5 787 params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
1da177e4 788 params->entry_addr =
8a2ab7f5
DH
789 (params->hdr.e_entry - seg->p_vaddr) +
790 seg->addr;
1da177e4
LT
791 break;
792 }
793 }
794 }
795
796 /* determine where the program header table has wound up if mapped */
8a2ab7f5
DH
797 stop = params->hdr.e_phoff;
798 stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
1da177e4
LT
799 phdr = params->phdrs;
800
801 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
802 if (phdr->p_type != PT_LOAD)
803 continue;
804
805 if (phdr->p_offset > params->hdr.e_phoff ||
806 phdr->p_offset + phdr->p_filesz < stop)
807 continue;
808
809 seg = loadmap->segs;
810 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
811 if (phdr->p_vaddr >= seg->p_vaddr &&
8a2ab7f5
DH
812 phdr->p_vaddr + phdr->p_filesz <=
813 seg->p_vaddr + seg->p_memsz) {
814 params->ph_addr =
815 (phdr->p_vaddr - seg->p_vaddr) +
816 seg->addr +
1da177e4
LT
817 params->hdr.e_phoff - phdr->p_offset;
818 break;
819 }
820 }
821 break;
822 }
823
824 /* determine where the dynamic section has wound up if there is one */
825 phdr = params->phdrs;
826 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
827 if (phdr->p_type != PT_DYNAMIC)
828 continue;
829
830 seg = loadmap->segs;
831 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
832 if (phdr->p_vaddr >= seg->p_vaddr &&
8a2ab7f5
DH
833 phdr->p_vaddr + phdr->p_memsz <=
834 seg->p_vaddr + seg->p_memsz) {
cdf38888
NP
835 Elf32_Dyn __user *dyn;
836 Elf32_Sword d_tag;
837
8a2ab7f5
DH
838 params->dynamic_addr =
839 (phdr->p_vaddr - seg->p_vaddr) +
840 seg->addr;
841
842 /* check the dynamic section contains at least
843 * one item, and that the last item is a NULL
844 * entry */
1da177e4
LT
845 if (phdr->p_memsz == 0 ||
846 phdr->p_memsz % sizeof(Elf32_Dyn) != 0)
847 goto dynamic_error;
848
849 tmp = phdr->p_memsz / sizeof(Elf32_Dyn);
cdf38888
NP
850 dyn = (Elf32_Dyn __user *)params->dynamic_addr;
851 __get_user(d_tag, &dyn[tmp - 1].d_tag);
852 if (d_tag != 0)
1da177e4
LT
853 goto dynamic_error;
854 break;
855 }
856 }
857 break;
858 }
859
860 /* now elide adjacent segments in the load map on MMU linux
8a2ab7f5
DH
861 * - on uClinux the holes between may actually be filled with system
862 * stuff or stuff from other processes
1da177e4
LT
863 */
864#ifdef CONFIG_MMU
865 nloads = loadmap->nsegs;
866 mseg = loadmap->segs;
867 seg = mseg + 1;
868 for (loop = 1; loop < nloads; loop++) {
869 /* see if we have a candidate for merging */
870 if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
871 load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
872 if (load_addr == (seg->addr & PAGE_MASK)) {
8a2ab7f5
DH
873 mseg->p_memsz +=
874 load_addr -
875 (mseg->addr + mseg->p_memsz);
1da177e4
LT
876 mseg->p_memsz += seg->addr & ~PAGE_MASK;
877 mseg->p_memsz += seg->p_memsz;
878 loadmap->nsegs--;
879 continue;
880 }
881 }
882
883 mseg++;
884 if (mseg != seg)
885 *mseg = *seg;
886 }
887#endif
888
889 kdebug("Mapped Object [%s]:", what);
890 kdebug("- elfhdr : %lx", params->elfhdr_addr);
891 kdebug("- entry : %lx", params->entry_addr);
892 kdebug("- PHDR[] : %lx", params->ph_addr);
893 kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
894 seg = loadmap->segs;
895 for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
896 kdebug("- LOAD[%d] : %08x-%08x [va=%x ms=%x]",
897 loop,
898 seg->addr, seg->addr + seg->p_memsz - 1,
899 seg->p_vaddr, seg->p_memsz);
900
901 return 0;
902
8a2ab7f5 903dynamic_error:
1da177e4 904 printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%lu)\n",
496ad9aa 905 what, file_inode(file)->i_ino);
1da177e4 906 return -ELIBBAD;
8a2ab7f5 907}
1da177e4
LT
908
909/*****************************************************************************/
910/*
911 * map a file with constant displacement under uClinux
912 */
913#ifndef CONFIG_MMU
8a2ab7f5
DH
914static int elf_fdpic_map_file_constdisp_on_uclinux(
915 struct elf_fdpic_params *params,
916 struct file *file,
917 struct mm_struct *mm)
1da177e4
LT
918{
919 struct elf32_fdpic_loadseg *seg;
920 struct elf32_phdr *phdr;
921 unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0, mflags;
1da177e4
LT
922 int loop, ret;
923
924 load_addr = params->load_addr;
925 seg = params->loadmap->segs;
926
8a2ab7f5
DH
927 /* determine the bounds of the contiguous overall allocation we must
928 * make */
1da177e4
LT
929 phdr = params->phdrs;
930 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
931 if (params->phdrs[loop].p_type != PT_LOAD)
932 continue;
933
934 if (base > phdr->p_vaddr)
935 base = phdr->p_vaddr;
936 if (top < phdr->p_vaddr + phdr->p_memsz)
937 top = phdr->p_vaddr + phdr->p_memsz;
938 }
939
940 /* allocate one big anon block for everything */
941 mflags = MAP_PRIVATE;
942 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
943 mflags |= MAP_EXECUTABLE;
944
6be5ceb0 945 maddr = vm_mmap(NULL, load_addr, top - base,
1da177e4 946 PROT_READ | PROT_WRITE | PROT_EXEC, mflags, 0);
8a2ab7f5 947 if (IS_ERR_VALUE(maddr))
1da177e4
LT
948 return (int) maddr;
949
950 if (load_addr != 0)
951 load_addr += PAGE_ALIGN(top - base);
952
953 /* and then load the file segments into it */
954 phdr = params->phdrs;
955 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
956 if (params->phdrs[loop].p_type != PT_LOAD)
957 continue;
958
1da177e4
LT
959 seg->addr = maddr + (phdr->p_vaddr - base);
960 seg->p_vaddr = phdr->p_vaddr;
961 seg->p_memsz = phdr->p_memsz;
962
3dc20cb2
AV
963 ret = read_code(file, seg->addr, phdr->p_offset,
964 phdr->p_filesz);
1da177e4
LT
965 if (ret < 0)
966 return ret;
967
968 /* map the ELF header address if in this segment */
969 if (phdr->p_offset == 0)
970 params->elfhdr_addr = seg->addr;
971
972 /* clear any space allocated but not loaded */
ab4ad555 973 if (phdr->p_filesz < phdr->p_memsz) {
e30c7c3b
TY
974 if (clear_user((void *) (seg->addr + phdr->p_filesz),
975 phdr->p_memsz - phdr->p_filesz))
976 return -EFAULT;
ab4ad555 977 }
1da177e4
LT
978
979 if (mm) {
980 if (phdr->p_flags & PF_X) {
aa289b47
DH
981 if (!mm->start_code) {
982 mm->start_code = seg->addr;
983 mm->end_code = seg->addr +
984 phdr->p_memsz;
985 }
8a2ab7f5 986 } else if (!mm->start_data) {
1da177e4 987 mm->start_data = seg->addr;
1da177e4 988 mm->end_data = seg->addr + phdr->p_memsz;
1da177e4 989 }
1da177e4
LT
990 }
991
992 seg++;
993 }
994
995 return 0;
8a2ab7f5 996}
1da177e4
LT
997#endif
998
999/*****************************************************************************/
1000/*
1001 * map a binary by direct mmap() of the individual PT_LOAD segments
1002 */
1003static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
1004 struct file *file,
1005 struct mm_struct *mm)
1006{
1007 struct elf32_fdpic_loadseg *seg;
1008 struct elf32_phdr *phdr;
1009 unsigned long load_addr, delta_vaddr;
e30c7c3b 1010 int loop, dvset;
1da177e4
LT
1011
1012 load_addr = params->load_addr;
1013 delta_vaddr = 0;
1014 dvset = 0;
1015
1016 seg = params->loadmap->segs;
1017
1018 /* deal with each load segment separately */
1019 phdr = params->phdrs;
1020 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
1021 unsigned long maddr, disp, excess, excess1;
1022 int prot = 0, flags;
1023
1024 if (phdr->p_type != PT_LOAD)
1025 continue;
1026
1027 kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
1028 (unsigned long) phdr->p_vaddr,
1029 (unsigned long) phdr->p_offset,
1030 (unsigned long) phdr->p_filesz,
1031 (unsigned long) phdr->p_memsz);
1032
1033 /* determine the mapping parameters */
1034 if (phdr->p_flags & PF_R) prot |= PROT_READ;
1035 if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
1036 if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
1037
1038 flags = MAP_PRIVATE | MAP_DENYWRITE;
1039 if (params->flags & ELF_FDPIC_FLAG_EXECUTABLE)
1040 flags |= MAP_EXECUTABLE;
1041
1042 maddr = 0;
1043
1044 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
1045 case ELF_FDPIC_FLAG_INDEPENDENT:
1046 /* PT_LOADs are independently locatable */
1047 break;
1048
1049 case ELF_FDPIC_FLAG_HONOURVADDR:
1050 /* the specified virtual address must be honoured */
1051 maddr = phdr->p_vaddr;
1052 flags |= MAP_FIXED;
1053 break;
1054
1055 case ELF_FDPIC_FLAG_CONSTDISP:
1056 /* constant displacement
8a2ab7f5
DH
1057 * - can be mapped anywhere, but must be mapped as a
1058 * unit
1da177e4
LT
1059 */
1060 if (!dvset) {
1061 maddr = load_addr;
1062 delta_vaddr = phdr->p_vaddr;
1063 dvset = 1;
8a2ab7f5 1064 } else {
1da177e4
LT
1065 maddr = load_addr + phdr->p_vaddr - delta_vaddr;
1066 flags |= MAP_FIXED;
1067 }
1068 break;
1069
1070 case ELF_FDPIC_FLAG_CONTIGUOUS:
1071 /* contiguity handled later */
1072 break;
1073
1074 default:
1075 BUG();
1076 }
1077
1078 maddr &= PAGE_MASK;
1079
1080 /* create the mapping */
1081 disp = phdr->p_vaddr & ~PAGE_MASK;
6be5ceb0 1082 maddr = vm_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
1da177e4 1083 phdr->p_offset - disp);
1da177e4
LT
1084
1085 kdebug("mmap[%d] <file> sz=%lx pr=%x fl=%x of=%lx --> %08lx",
8a2ab7f5
DH
1086 loop, phdr->p_memsz + disp, prot, flags,
1087 phdr->p_offset - disp, maddr);
1da177e4 1088
8a2ab7f5 1089 if (IS_ERR_VALUE(maddr))
1da177e4
LT
1090 return (int) maddr;
1091
8a2ab7f5
DH
1092 if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
1093 ELF_FDPIC_FLAG_CONTIGUOUS)
1da177e4
LT
1094 load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
1095
1096 seg->addr = maddr + disp;
1097 seg->p_vaddr = phdr->p_vaddr;
1098 seg->p_memsz = phdr->p_memsz;
1099
1100 /* map the ELF header address if in this segment */
1101 if (phdr->p_offset == 0)
1102 params->elfhdr_addr = seg->addr;
1103
8a2ab7f5
DH
1104 /* clear the bit between beginning of mapping and beginning of
1105 * PT_LOAD */
1da177e4
LT
1106 if (prot & PROT_WRITE && disp > 0) {
1107 kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
e30c7c3b
TY
1108 if (clear_user((void __user *) maddr, disp))
1109 return -EFAULT;
1da177e4
LT
1110 maddr += disp;
1111 }
1112
1113 /* clear any space allocated but not loaded
1114 * - on uClinux we can just clear the lot
1115 * - on MMU linux we'll get a SIGBUS beyond the last page
1116 * extant in the file
1117 */
1118 excess = phdr->p_memsz - phdr->p_filesz;
1119 excess1 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
1120
1121#ifdef CONFIG_MMU
1da177e4
LT
1122 if (excess > excess1) {
1123 unsigned long xaddr = maddr + phdr->p_filesz + excess1;
1124 unsigned long xmaddr;
1125
1126 flags |= MAP_FIXED | MAP_ANONYMOUS;
6be5ceb0 1127 xmaddr = vm_mmap(NULL, xaddr, excess - excess1,
8a2ab7f5 1128 prot, flags, 0);
1da177e4
LT
1129
1130 kdebug("mmap[%d] <anon>"
1131 " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
8a2ab7f5
DH
1132 loop, xaddr, excess - excess1, prot, flags,
1133 xmaddr);
1da177e4
LT
1134
1135 if (xmaddr != xaddr)
1136 return -ENOMEM;
1137 }
1138
1139 if (prot & PROT_WRITE && excess1 > 0) {
1140 kdebug("clear[%d] ad=%lx sz=%lx",
1141 loop, maddr + phdr->p_filesz, excess1);
e30c7c3b
TY
1142 if (clear_user((void __user *) maddr + phdr->p_filesz,
1143 excess1))
1144 return -EFAULT;
1da177e4
LT
1145 }
1146
1147#else
1148 if (excess > 0) {
1149 kdebug("clear[%d] ad=%lx sz=%lx",
1150 loop, maddr + phdr->p_filesz, excess);
e30c7c3b
TY
1151 if (clear_user((void *) maddr + phdr->p_filesz, excess))
1152 return -EFAULT;
1da177e4
LT
1153 }
1154#endif
1155
1156 if (mm) {
1157 if (phdr->p_flags & PF_X) {
aa289b47
DH
1158 if (!mm->start_code) {
1159 mm->start_code = maddr;
1160 mm->end_code = maddr + phdr->p_memsz;
1161 }
8a2ab7f5 1162 } else if (!mm->start_data) {
1da177e4
LT
1163 mm->start_data = maddr;
1164 mm->end_data = maddr + phdr->p_memsz;
1165 }
1166 }
1167
1168 seg++;
1169 }
1170
1171 return 0;
8a2ab7f5 1172}
6d8c4e3b
DH
1173
1174/*****************************************************************************/
1175/*
1176 * ELF-FDPIC core dumper
1177 *
1178 * Modelled on fs/exec.c:aout_core_dump()
1179 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1180 *
1181 * Modelled on fs/binfmt_elf.c core dumper
1182 */
698ba7b5 1183#ifdef CONFIG_ELF_CORE
6d8c4e3b 1184
6d8c4e3b
DH
1185/*
1186 * Decide whether a segment is worth dumping; default is yes to be
1187 * sure (missing info is worse than too much; etc).
1188 * Personally I'd include everything, and use the coredump limit...
1189 *
1190 * I think we should skip something. But I am not sure how. H.J.
1191 */
ee78b0a6 1192static int maydump(struct vm_area_struct *vma, unsigned long mm_flags)
6d8c4e3b 1193{
ee78b0a6
KH
1194 int dump_ok;
1195
6d8c4e3b 1196 /* Do not dump I/O mapped devices or special mappings */
314e51b9 1197 if (vma->vm_flags & VM_IO) {
6d8c4e3b
DH
1198 kdcore("%08lx: %08lx: no (IO)", vma->vm_start, vma->vm_flags);
1199 return 0;
1200 }
1201
1202 /* If we may not read the contents, don't allow us to dump
1203 * them either. "dump_write()" can't handle it anyway.
1204 */
1205 if (!(vma->vm_flags & VM_READ)) {
1206 kdcore("%08lx: %08lx: no (!read)", vma->vm_start, vma->vm_flags);
1207 return 0;
1208 }
1209
ab27a8d0
RZ
1210 /* support for DAX */
1211 if (vma_is_dax(vma)) {
1212 if (vma->vm_flags & VM_SHARED) {
1213 dump_ok = test_bit(MMF_DUMP_DAX_SHARED, &mm_flags);
1214 kdcore("%08lx: %08lx: %s (DAX shared)", vma->vm_start,
1215 vma->vm_flags, dump_ok ? "yes" : "no");
1216 } else {
1217 dump_ok = test_bit(MMF_DUMP_DAX_PRIVATE, &mm_flags);
1218 kdcore("%08lx: %08lx: %s (DAX private)", vma->vm_start,
1219 vma->vm_flags, dump_ok ? "yes" : "no");
1220 }
1221 return dump_ok;
1222 }
1223
ee78b0a6 1224 /* By default, dump shared memory if mapped from an anonymous file. */
6d8c4e3b 1225 if (vma->vm_flags & VM_SHARED) {
496ad9aa 1226 if (file_inode(vma->vm_file)->i_nlink == 0) {
ee78b0a6
KH
1227 dump_ok = test_bit(MMF_DUMP_ANON_SHARED, &mm_flags);
1228 kdcore("%08lx: %08lx: %s (share)", vma->vm_start,
1229 vma->vm_flags, dump_ok ? "yes" : "no");
1230 return dump_ok;
6d8c4e3b
DH
1231 }
1232
ee78b0a6
KH
1233 dump_ok = test_bit(MMF_DUMP_MAPPED_SHARED, &mm_flags);
1234 kdcore("%08lx: %08lx: %s (share)", vma->vm_start,
1235 vma->vm_flags, dump_ok ? "yes" : "no");
1236 return dump_ok;
6d8c4e3b
DH
1237 }
1238
1239#ifdef CONFIG_MMU
ee78b0a6 1240 /* By default, if it hasn't been written to, don't write it out */
6d8c4e3b 1241 if (!vma->anon_vma) {
ee78b0a6
KH
1242 dump_ok = test_bit(MMF_DUMP_MAPPED_PRIVATE, &mm_flags);
1243 kdcore("%08lx: %08lx: %s (!anon)", vma->vm_start,
1244 vma->vm_flags, dump_ok ? "yes" : "no");
1245 return dump_ok;
6d8c4e3b
DH
1246 }
1247#endif
1248
ee78b0a6
KH
1249 dump_ok = test_bit(MMF_DUMP_ANON_PRIVATE, &mm_flags);
1250 kdcore("%08lx: %08lx: %s", vma->vm_start, vma->vm_flags,
1251 dump_ok ? "yes" : "no");
1252 return dump_ok;
6d8c4e3b
DH
1253}
1254
1255/* An ELF note in memory */
1256struct memelfnote
1257{
1258 const char *name;
1259 int type;
1260 unsigned int datasz;
1261 void *data;
1262};
1263
1264static int notesize(struct memelfnote *en)
1265{
1266 int sz;
1267
1268 sz = sizeof(struct elf_note);
1269 sz += roundup(strlen(en->name) + 1, 4);
1270 sz += roundup(en->datasz, 4);
1271
1272 return sz;
1273}
1274
1275/* #define DEBUG */
1276
e6c1baa9 1277static int writenote(struct memelfnote *men, struct coredump_params *cprm)
05f47fda
DH
1278{
1279 struct elf_note en;
6d8c4e3b
DH
1280 en.n_namesz = strlen(men->name) + 1;
1281 en.n_descsz = men->datasz;
1282 en.n_type = men->type;
1283
e6c1baa9 1284 return dump_emit(cprm, &en, sizeof(en)) &&
22a8cb82
AV
1285 dump_emit(cprm, men->name, en.n_namesz) && dump_align(cprm, 4) &&
1286 dump_emit(cprm, men->data, men->datasz) && dump_align(cprm, 4);
6d8c4e3b 1287}
6d8c4e3b 1288
6d8c4e3b
DH
1289static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
1290{
1291 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1292 elf->e_ident[EI_CLASS] = ELF_CLASS;
1293 elf->e_ident[EI_DATA] = ELF_DATA;
1294 elf->e_ident[EI_VERSION] = EV_CURRENT;
1295 elf->e_ident[EI_OSABI] = ELF_OSABI;
1296 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1297
1298 elf->e_type = ET_CORE;
1299 elf->e_machine = ELF_ARCH;
1300 elf->e_version = EV_CURRENT;
1301 elf->e_entry = 0;
1302 elf->e_phoff = sizeof(struct elfhdr);
1303 elf->e_shoff = 0;
1304 elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
1305 elf->e_ehsize = sizeof(struct elfhdr);
1306 elf->e_phentsize = sizeof(struct elf_phdr);
1307 elf->e_phnum = segs;
1308 elf->e_shentsize = 0;
1309 elf->e_shnum = 0;
1310 elf->e_shstrndx = 0;
1311 return;
1312}
1313
1314static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1315{
1316 phdr->p_type = PT_NOTE;
1317 phdr->p_offset = offset;
1318 phdr->p_vaddr = 0;
1319 phdr->p_paddr = 0;
1320 phdr->p_filesz = sz;
1321 phdr->p_memsz = 0;
1322 phdr->p_flags = 0;
1323 phdr->p_align = 0;
1324 return;
1325}
1326
1327static inline void fill_note(struct memelfnote *note, const char *name, int type,
1328 unsigned int sz, void *data)
1329{
1330 note->name = name;
1331 note->type = type;
1332 note->datasz = sz;
1333 note->data = data;
1334 return;
1335}
1336
1337/*
1338 * fill up all the fields in prstatus from the given task struct, except
3ad2f3fb 1339 * registers which need to be filled up separately.
6d8c4e3b
DH
1340 */
1341static void fill_prstatus(struct elf_prstatus *prstatus,
1342 struct task_struct *p, long signr)
1343{
1344 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1345 prstatus->pr_sigpend = p->pending.signal.sig[0];
1346 prstatus->pr_sighold = p->blocked.sig[0];
3b34fc58
ON
1347 rcu_read_lock();
1348 prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1349 rcu_read_unlock();
b488893a 1350 prstatus->pr_pid = task_pid_vnr(p);
b488893a
PE
1351 prstatus->pr_pgrp = task_pgrp_vnr(p);
1352 prstatus->pr_sid = task_session_vnr(p);
6d8c4e3b 1353 if (thread_group_leader(p)) {
cd19c364 1354 struct task_cputime cputime;
2515ddc6 1355
6d8c4e3b 1356 /*
2515ddc6
PM
1357 * This is the record for the group leader. It shows the
1358 * group-wide total, not its individual thread total.
6d8c4e3b 1359 */
cd19c364 1360 thread_group_cputime(p, &cputime);
e2bb80d5
AB
1361 prstatus->pr_utime = ns_to_kernel_old_timeval(cputime.utime);
1362 prstatus->pr_stime = ns_to_kernel_old_timeval(cputime.stime);
6d8c4e3b 1363 } else {
cd19c364 1364 u64 utime, stime;
6fac4829 1365
cd19c364 1366 task_cputime(p, &utime, &stime);
e2bb80d5
AB
1367 prstatus->pr_utime = ns_to_kernel_old_timeval(utime);
1368 prstatus->pr_stime = ns_to_kernel_old_timeval(stime);
6d8c4e3b 1369 }
e2bb80d5
AB
1370 prstatus->pr_cutime = ns_to_kernel_old_timeval(p->signal->cutime);
1371 prstatus->pr_cstime = ns_to_kernel_old_timeval(p->signal->cstime);
6d8c4e3b
DH
1372
1373 prstatus->pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
1374 prstatus->pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
1375}
1376
1377static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1378 struct mm_struct *mm)
1379{
c69e8d9c 1380 const struct cred *cred;
6d8c4e3b
DH
1381 unsigned int i, len;
1382
1383 /* first copy the parameters from user space */
1384 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1385
1386 len = mm->arg_end - mm->arg_start;
1387 if (len >= ELF_PRARGSZ)
1388 len = ELF_PRARGSZ - 1;
1389 if (copy_from_user(&psinfo->pr_psargs,
1390 (const char __user *) mm->arg_start, len))
1391 return -EFAULT;
1392 for (i = 0; i < len; i++)
1393 if (psinfo->pr_psargs[i] == 0)
1394 psinfo->pr_psargs[i] = ' ';
1395 psinfo->pr_psargs[len] = 0;
1396
3b34fc58
ON
1397 rcu_read_lock();
1398 psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1399 rcu_read_unlock();
b488893a 1400 psinfo->pr_pid = task_pid_vnr(p);
b488893a
PE
1401 psinfo->pr_pgrp = task_pgrp_vnr(p);
1402 psinfo->pr_sid = task_session_vnr(p);
6d8c4e3b
DH
1403
1404 i = p->state ? ffz(~p->state) + 1 : 0;
1405 psinfo->pr_state = i;
1406 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1407 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1408 psinfo->pr_nice = task_nice(p);
1409 psinfo->pr_flag = p->flags;
c69e8d9c
DH
1410 rcu_read_lock();
1411 cred = __task_cred(p);
ebc887b2
EB
1412 SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid));
1413 SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
c69e8d9c 1414 rcu_read_unlock();
6d8c4e3b
DH
1415 strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1416
1417 return 0;
1418}
1419
1420/* Here is the structure in which status of each thread is captured. */
1421struct elf_thread_status
1422{
1423 struct list_head list;
1424 struct elf_prstatus prstatus; /* NT_PRSTATUS */
1425 elf_fpregset_t fpu; /* NT_PRFPREG */
1426 struct task_struct *thread;
1427#ifdef ELF_CORE_COPY_XFPREGS
5b20cd80 1428 elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
6d8c4e3b
DH
1429#endif
1430 struct memelfnote notes[3];
1431 int num_notes;
1432};
1433
1434/*
1435 * In order to add the specific thread information for the elf file format,
1436 * we need to keep a linked list of every thread's pr_status and then create
1437 * a single section for them in the final core file.
1438 */
1439static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1440{
1441 struct task_struct *p = t->thread;
1442 int sz = 0;
1443
1444 t->num_notes = 0;
1445
1446 fill_prstatus(&t->prstatus, p, signr);
1447 elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1448
1449 fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1450 &t->prstatus);
1451 t->num_notes++;
1452 sz += notesize(&t->notes[0]);
1453
1454 t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL, &t->fpu);
1455 if (t->prstatus.pr_fpvalid) {
1456 fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1457 &t->fpu);
1458 t->num_notes++;
1459 sz += notesize(&t->notes[1]);
1460 }
1461
1462#ifdef ELF_CORE_COPY_XFPREGS
1463 if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
5b20cd80
MN
1464 fill_note(&t->notes[2], "LINUX", ELF_CORE_XFPREG_TYPE,
1465 sizeof(t->xfpu), &t->xfpu);
6d8c4e3b
DH
1466 t->num_notes++;
1467 sz += notesize(&t->notes[2]);
1468 }
1469#endif
1470 return sz;
1471}
1472
8d9032bb
DH
1473static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
1474 elf_addr_t e_shoff, int segs)
1475{
1476 elf->e_shoff = e_shoff;
1477 elf->e_shentsize = sizeof(*shdr4extnum);
1478 elf->e_shnum = 1;
1479 elf->e_shstrndx = SHN_UNDEF;
1480
1481 memset(shdr4extnum, 0, sizeof(*shdr4extnum));
1482
1483 shdr4extnum->sh_type = SHT_NULL;
1484 shdr4extnum->sh_size = elf->e_shnum;
1485 shdr4extnum->sh_link = elf->e_shstrndx;
1486 shdr4extnum->sh_info = segs;
1487}
1488
6d8c4e3b
DH
1489/*
1490 * dump the segments for an MMU process
1491 */
e6c1baa9 1492static bool elf_fdpic_dump_segments(struct coredump_params *cprm)
6d8c4e3b
DH
1493{
1494 struct vm_area_struct *vma;
1495
1496 for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
11e3e8d6 1497#ifdef CONFIG_MMU
6d8c4e3b 1498 unsigned long addr;
11e3e8d6 1499#endif
6d8c4e3b 1500
e6c1baa9 1501 if (!maydump(vma, cprm->mm_flags))
6d8c4e3b
DH
1502 continue;
1503
e6c1baa9 1504#ifdef CONFIG_MMU
f3e8fccd
HD
1505 for (addr = vma->vm_start; addr < vma->vm_end;
1506 addr += PAGE_SIZE) {
e6c1baa9 1507 bool res;
f3e8fccd
HD
1508 struct page *page = get_dump_page(addr);
1509 if (page) {
1510 void *kaddr = kmap(page);
e6c1baa9 1511 res = dump_emit(cprm, kaddr, PAGE_SIZE);
6d8c4e3b 1512 kunmap(page);
09cbfeaf 1513 put_page(page);
e6c1baa9 1514 } else {
9b56d543 1515 res = dump_skip(cprm, PAGE_SIZE);
e6c1baa9
AV
1516 }
1517 if (!res)
1518 return false;
6d8c4e3b 1519 }
e6c1baa9
AV
1520#else
1521 if (!dump_emit(cprm, (void *) vma->vm_start,
6d8c4e3b 1522 vma->vm_end - vma->vm_start))
e6c1baa9
AV
1523 return false;
1524#endif
6d8c4e3b 1525 }
e6c1baa9 1526 return true;
6d8c4e3b 1527}
6d8c4e3b 1528
8d9032bb
DH
1529static size_t elf_core_vma_data_size(unsigned long mm_flags)
1530{
1531 struct vm_area_struct *vma;
1532 size_t size = 0;
1533
47568d4c 1534 for (vma = current->mm->mmap; vma; vma = vma->vm_next)
8d9032bb
DH
1535 if (maydump(vma, mm_flags))
1536 size += vma->vm_end - vma->vm_start;
1537 return size;
1538}
1539
6d8c4e3b
DH
1540/*
1541 * Actual dumper
1542 *
1543 * This is a two-pass process; first we find the offsets of the bits,
1544 * and then they are actually written out. If we run out of core limit
1545 * we just truncate.
1546 */
f6151dfe 1547static int elf_fdpic_core_dump(struct coredump_params *cprm)
6d8c4e3b
DH
1548{
1549#define NUM_NOTES 6
1550 int has_dumped = 0;
1551 mm_segment_t fs;
1552 int segs;
6d8c4e3b
DH
1553 int i;
1554 struct vm_area_struct *vma;
1555 struct elfhdr *elf = NULL;
9b56d543 1556 loff_t offset = 0, dataoff;
6d8c4e3b
DH
1557 int numnote;
1558 struct memelfnote *notes = NULL;
1559 struct elf_prstatus *prstatus = NULL; /* NT_PRSTATUS */
1560 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
6d8c4e3b
DH
1561 LIST_HEAD(thread_list);
1562 struct list_head *t;
1563 elf_fpregset_t *fpu = NULL;
1564#ifdef ELF_CORE_COPY_XFPREGS
1565 elf_fpxregset_t *xfpu = NULL;
1566#endif
1567 int thread_status_size = 0;
6d8c4e3b 1568 elf_addr_t *auxv;
93eb211e 1569 struct elf_phdr *phdr4note = NULL;
8d9032bb
DH
1570 struct elf_shdr *shdr4extnum = NULL;
1571 Elf_Half e_phnum;
1572 elf_addr_t e_shoff;
afabada9
AV
1573 struct core_thread *ct;
1574 struct elf_thread_status *tmp;
6d8c4e3b
DH
1575
1576 /*
1577 * We no longer stop all VM operations.
1578 *
1579 * This is because those proceses that could possibly change map_count
1580 * or the mmap / vma pages are now blocked in do_exit on current
1581 * finishing this core dump.
1582 *
1583 * Only ptrace can touch these memory addresses, but it doesn't change
1584 * the map_count or the pages allocated. So no possibility of crashing
1585 * exists while dumping the mm->vm_next areas to the core file.
1586 */
1587
1588 /* alloc memory for large data structures: too large to be on stack */
1589 elf = kmalloc(sizeof(*elf), GFP_KERNEL);
1590 if (!elf)
1591 goto cleanup;
1592 prstatus = kzalloc(sizeof(*prstatus), GFP_KERNEL);
1593 if (!prstatus)
1594 goto cleanup;
1595 psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1596 if (!psinfo)
1597 goto cleanup;
6da2ec56
KC
1598 notes = kmalloc_array(NUM_NOTES, sizeof(struct memelfnote),
1599 GFP_KERNEL);
6d8c4e3b
DH
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
afabada9
AV
1611 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;
182c515f 1616
afabada9
AV
1617 tmp->thread = ct->task;
1618 list_add(&tmp->list, &thread_list);
1619 }
182c515f 1620
afabada9
AV
1621 list_for_each(t, &thread_list) {
1622 struct elf_thread_status *tmp;
1623 int sz;
6d8c4e3b 1624
afabada9
AV
1625 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;
6d8c4e3b
DH
1628 }
1629
1630 /* now collect the dump for the current */
5ab1c309 1631 fill_prstatus(prstatus, current, cprm->siginfo->si_signo);
f6151dfe 1632 elf_core_copy_regs(&prstatus->pr_reg, cprm->regs);
6d8c4e3b 1633
6d8c4e3b 1634 segs = current->mm->map_count;
1fcccbac 1635 segs += elf_core_extra_phdrs();
6d8c4e3b 1636
8d9032bb
DH
1637 /* 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
6d8c4e3b 1645 /* Set up header */
8d9032bb 1646 fill_elf_fdpic_header(elf, e_phnum);
6d8c4e3b
DH
1647
1648 has_dumped = 1;
6d8c4e3b
DH
1649 /*
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 =
f6151dfe 1671 elf_core_copy_task_fpregs(current, cprm->regs, fpu)))
6d8c4e3b
DH
1672 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++,
5b20cd80 1677 "LINUX", ELF_CORE_XFPREG_TYPE, sizeof(*xfpu), xfpu);
6d8c4e3b
DH
1678#endif
1679
1680 fs = get_fs();
1681 set_fs(KERNEL_DS);
1682
6d8c4e3b 1683 offset += sizeof(*elf); /* Elf header */
8d9032bb 1684 offset += segs * sizeof(struct elf_phdr); /* Program headers */
6d8c4e3b
DH
1685
1686 /* Write notes phdr entry */
1687 {
6d8c4e3b
DH
1688 int sz = 0;
1689
1690 for (i = 0; i < numnote; i++)
1691 sz += notesize(notes + i);
1692
1693 sz += thread_status_size;
1694
93eb211e
DH
1695 phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
1696 if (!phdr4note)
088e7af7 1697 goto end_coredump;
93eb211e
DH
1698
1699 fill_elf_note_phdr(phdr4note, sz, offset);
1700 offset += sz;
6d8c4e3b
DH
1701 }
1702
1703 /* Page-align dumped data */
1704 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1705
30736a4d 1706 offset += elf_core_vma_data_size(cprm->mm_flags);
8d9032bb
DH
1707 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
e6c1baa9 1719 if (!dump_emit(cprm, elf, sizeof(*elf)))
93eb211e
DH
1720 goto end_coredump;
1721
e6c1baa9 1722 if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note)))
93eb211e
DH
1723 goto end_coredump;
1724
6d8c4e3b 1725 /* write program headers for segments dump */
8feae131 1726 for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
6d8c4e3b
DH
1727 struct elf_phdr phdr;
1728 size_t sz;
1729
6d8c4e3b
DH
1730 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;
30736a4d 1736 phdr.p_filesz = maydump(vma, cprm->mm_flags) ? sz : 0;
6d8c4e3b
DH
1737 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
e6c1baa9 1746 if (!dump_emit(cprm, &phdr, sizeof(phdr)))
088e7af7 1747 goto end_coredump;
6d8c4e3b
DH
1748 }
1749
506f21c5 1750 if (!elf_core_write_extra_phdrs(cprm, offset))
1fcccbac 1751 goto end_coredump;
6d8c4e3b
DH
1752
1753 /* write out the notes section */
1754 for (i = 0; i < numnote; i++)
e6c1baa9 1755 if (!writenote(notes + i, cprm))
6d8c4e3b
DH
1756 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++)
e6c1baa9 1764 if (!writenote(&tmp->notes[i], cprm))
6d8c4e3b
DH
1765 goto end_coredump;
1766 }
1767
1607f09c 1768 if (!dump_skip(cprm, dataoff - cprm->pos))
f3e8fccd 1769 goto end_coredump;
6d8c4e3b 1770
e6c1baa9 1771 if (!elf_fdpic_dump_segments(cprm))
6d8c4e3b
DH
1772 goto end_coredump;
1773
aa3e7eaf 1774 if (!elf_core_write_extra_data(cprm))
1fcccbac 1775 goto end_coredump;
6d8c4e3b 1776
8d9032bb 1777 if (e_phnum == PN_XNUM) {
e6c1baa9 1778 if (!dump_emit(cprm, shdr4extnum, sizeof(*shdr4extnum)))
8d9032bb
DH
1779 goto end_coredump;
1780 }
1781
2f48912d 1782 if (cprm->file->f_pos != offset) {
6d8c4e3b
DH
1783 /* Sanity check */
1784 printk(KERN_WARNING
1785 "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
2f48912d 1786 cprm->file->f_pos, offset);
6d8c4e3b
DH
1787 }
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 }
93eb211e 1798 kfree(phdr4note);
6d8c4e3b
DH
1799 kfree(elf);
1800 kfree(prstatus);
1801 kfree(psinfo);
1802 kfree(notes);
1803 kfree(fpu);
bcb65a79 1804 kfree(shdr4extnum);
6d8c4e3b
DH
1805#ifdef ELF_CORE_COPY_XFPREGS
1806 kfree(xfpu);
1807#endif
1808 return has_dumped;
1809#undef NUM_NOTES
1810}
1811
698ba7b5 1812#endif /* CONFIG_ELF_CORE */