1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* binfmt_elf_fdpic.c: FDPIC ELF binary format
3 *
4 * Copyright (C) 2003, 2004, 2006 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 * Derived from binfmt_elf.c
7 */
8
9 #include <linux/module.h>
10
11 #include <linux/fs.h>
12 #include <linux/stat.h>
13 #include <linux/sched.h>
14 #include <linux/sched/coredump.h>
15 #include <linux/sched/task_stack.h>
16 #include <linux/sched/cputime.h>
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>
26 #include <linux/pagemap.h>
27 #include <linux/security.h>
28 #include <linux/highmem.h>
29 #include <linux/highuid.h>
30 #include <linux/personality.h>
31 #include <linux/ptrace.h>
32 #include <linux/init.h>
33 #include <linux/elf.h>
34 #include <linux/elf-fdpic.h>
35 #include <linux/elfcore.h>
36 #include <linux/coredump.h>
37 #include <linux/dax.h>
38 #include <linux/regset.h>
39
40 #include <linux/uaccess.h>
41 #include <asm/param.h>
42
43 typedef char *elf_caddr_t;
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
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
57 MODULE_LICENSE("GPL");
58
59 static int load_elf_fdpic_binary(struct linux_binprm *);
60 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *, struct file *);
61 static int elf_fdpic_map_file(struct elf_fdpic_params *, struct file *,
62 struct mm_struct *, const char *);
63
64 static int create_elf_fdpic_tables(struct linux_binprm *, struct mm_struct *,
65 struct elf_fdpic_params *,
66 struct elf_fdpic_params *);
67
68 #ifndef CONFIG_MMU
69 static int elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params *,
70 struct file *,
71 struct mm_struct *);
72 #endif
73
74 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *,
75 struct file *, struct mm_struct *);
76
77 #ifdef CONFIG_ELF_CORE
78 static int elf_fdpic_core_dump(struct coredump_params *cprm);
79 #endif
80
81 static struct linux_binfmt elf_fdpic_format = {
82 .module = THIS_MODULE,
83 .load_binary = load_elf_fdpic_binary,
84 #ifdef CONFIG_ELF_CORE
85 .core_dump = elf_fdpic_core_dump,
86 .min_coredump = ELF_EXEC_PAGESIZE,
87 #endif
88 };
89
init_elf_fdpic_binfmt(void)90 static int __init init_elf_fdpic_binfmt(void)
91 {
92 register_binfmt(&elf_fdpic_format);
93 return 0;
94 }
95
exit_elf_fdpic_binfmt(void)96 static void __exit exit_elf_fdpic_binfmt(void)
97 {
98 unregister_binfmt(&elf_fdpic_format);
99 }
100
101 core_initcall(init_elf_fdpic_binfmt);
102 module_exit(exit_elf_fdpic_binfmt);
103
is_elf(struct elfhdr * hdr,struct file * file)104 static int is_elf(struct elfhdr *hdr, struct file *file)
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;
110 if (!elf_check_arch(hdr))
111 return 0;
112 if (!can_mmap_file(file))
113 return 0;
114 return 1;
115 }
116
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
is_constdisp(struct elfhdr * hdr)125 static 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
134 /*****************************************************************************/
135 /*
136 * read the program headers table into memory
137 */
elf_fdpic_fetch_phdrs(struct elf_fdpic_params * params,struct file * file)138 static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
139 struct file *file)
140 {
141 struct elf_phdr *phdr;
142 unsigned long size;
143 int retval, loop;
144 loff_t pos = params->hdr.e_phoff;
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
156 retval = kernel_read(file, params->phdrs, size, &pos);
157 if (unlikely(retval != size))
158 return retval < 0 ? retval : -ENOEXEC;
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;
176 }
177
178 /*****************************************************************************/
179 /*
180 * load an fdpic binary into various bits of memory
181 */
load_elf_fdpic_binary(struct linux_binprm * bprm)182 static int load_elf_fdpic_binary(struct linux_binprm *bprm)
183 {
184 struct elf_fdpic_params exec_params, interp_params;
185 struct pt_regs *regs = current_pt_regs();
186 struct elf_phdr *phdr;
187 unsigned long stack_size, entryaddr;
188 #ifdef ELF_FDPIC_PLAT_INIT
189 unsigned long dynaddr;
190 #endif
191 #ifndef CONFIG_MMU
192 unsigned long stack_prot;
193 #endif
194 struct file *interpreter = NULL; /* to shut gcc up */
195 char *interpreter_name = NULL;
196 int executable_stack;
197 int retval, i;
198 loff_t pos;
199
200 kdebug("____ LOAD %d ____", current->pid);
201
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;
210 if (!is_elf(&exec_params.hdr, bprm->file))
211 goto error;
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 }
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 /* elf ABI allows only one interpreter */
235 if (interpreter_name)
236 continue;
237
238 retval = -ENOMEM;
239 if (phdr->p_filesz > PATH_MAX)
240 goto error;
241 retval = -ENOENT;
242 if (phdr->p_filesz < 2)
243 goto error;
244
245 /* read the name of the interpreter into memory */
246 interpreter_name = kmalloc(phdr->p_filesz, GFP_KERNEL);
247 if (!interpreter_name)
248 goto error;
249
250 pos = phdr->p_offset;
251 retval = kernel_read(bprm->file, interpreter_name,
252 phdr->p_filesz, &pos);
253 if (unlikely(retval != phdr->p_filesz)) {
254 if (retval >= 0)
255 retval = -ENOEXEC;
256 goto error;
257 }
258
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 = bprm_open_interpreter(bprm,
267 interpreter_name);
268 retval = PTR_ERR(interpreter);
269 if (IS_ERR(interpreter)) {
270 interpreter = NULL;
271 goto error;
272 }
273
274 /*
275 * If the binary is not readable then enforce
276 * mm->dumpable = 0 regardless of the interpreter's
277 * permissions.
278 */
279 would_dump(bprm, interpreter);
280
281 pos = 0;
282 retval = kernel_read(interpreter, bprm->buf,
283 BINPRM_BUF_SIZE, &pos);
284 if (unlikely(retval != BINPRM_BUF_SIZE)) {
285 if (retval >= 0)
286 retval = -ENOEXEC;
287 goto error;
288 }
289
290 interp_params.hdr = *((struct elfhdr *) bprm->buf);
291 break;
292
293 case PT_LOAD:
294 #ifdef CONFIG_MMU
295 if (exec_params.load_addr == 0)
296 exec_params.load_addr = phdr->p_vaddr;
297 #endif
298 break;
299 }
300
301 }
302
303 /* No PT_INTERP to substitute for: the override does not apply. */
304 bprm_drop_loader(bprm);
305
306 if (is_constdisp(&exec_params.hdr))
307 exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
308
309 /* perform insanity checks on the interpreter */
310 if (interpreter_name) {
311 retval = -ELIBBAD;
312 if (!is_elf(&interp_params.hdr, interpreter))
313 goto error;
314
315 interp_params.flags = ELF_FDPIC_FLAG_PRESENT;
316
317 /* read the interpreter's program header table */
318 retval = elf_fdpic_fetch_phdrs(&interp_params, interpreter);
319 if (retval < 0)
320 goto error;
321 }
322
323 stack_size = exec_params.stack_size;
324 if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
325 executable_stack = EXSTACK_ENABLE_X;
326 else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
327 executable_stack = EXSTACK_DISABLE_X;
328 else
329 executable_stack = EXSTACK_DEFAULT;
330
331 if (stack_size == 0 && interp_params.flags & ELF_FDPIC_FLAG_PRESENT) {
332 stack_size = interp_params.stack_size;
333 if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
334 executable_stack = EXSTACK_ENABLE_X;
335 else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
336 executable_stack = EXSTACK_DISABLE_X;
337 else
338 executable_stack = EXSTACK_DEFAULT;
339 }
340
341 retval = -ENOEXEC;
342 if (stack_size == 0)
343 stack_size = 131072UL; /* same as exec.c's default commit */
344
345 if (is_constdisp(&interp_params.hdr))
346 interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
347
348 /* flush all traces of the currently running executable */
349 retval = begin_new_exec(bprm);
350 if (retval)
351 goto error;
352
353 /* there's now no turning back... the old userspace image is dead,
354 * defunct, deceased, etc.
355 */
356 SET_PERSONALITY(exec_params.hdr);
357 if (elf_check_fdpic(&exec_params.hdr))
358 current->personality |= PER_LINUX_FDPIC;
359 if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
360 current->personality |= READ_IMPLIES_EXEC;
361
362 setup_new_exec(bprm);
363
364 set_binfmt(&elf_fdpic_format);
365
366 current->mm->start_code = 0;
367 current->mm->end_code = 0;
368 current->mm->start_stack = 0;
369 current->mm->start_data = 0;
370 current->mm->end_data = 0;
371 current->mm->context.exec_fdpic_loadmap = 0;
372 current->mm->context.interp_fdpic_loadmap = 0;
373
374 #ifdef CONFIG_MMU
375 elf_fdpic_arch_lay_out_mm(&exec_params,
376 &interp_params,
377 ¤t->mm->start_stack,
378 ¤t->mm->start_brk);
379
380 retval = setup_arg_pages(bprm, current->mm->start_stack,
381 executable_stack);
382 if (retval < 0)
383 goto error;
384 #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
385 retval = arch_setup_additional_pages(bprm, !!interpreter_name);
386 if (retval < 0)
387 goto error;
388 #endif
389 #endif
390
391 /* load the executable and interpreter into memory */
392 retval = elf_fdpic_map_file(&exec_params, bprm->file, current->mm,
393 "executable");
394 if (retval < 0)
395 goto error;
396
397 if (interpreter_name) {
398 retval = elf_fdpic_map_file(&interp_params, interpreter,
399 current->mm, "interpreter");
400 if (retval < 0) {
401 printk(KERN_ERR "Unable to load interpreter\n");
402 goto error;
403 }
404
405 exe_file_allow_write_access(interpreter);
406 fput(interpreter);
407 interpreter = NULL;
408 }
409
410 #ifdef CONFIG_MMU
411 if (!current->mm->start_brk)
412 current->mm->start_brk = current->mm->end_data;
413
414 current->mm->brk = current->mm->start_brk =
415 PAGE_ALIGN(current->mm->start_brk);
416
417 #else
418 /* create a stack area and zero-size brk area */
419 stack_size = (stack_size + PAGE_SIZE - 1) & PAGE_MASK;
420 if (stack_size < PAGE_SIZE * 2)
421 stack_size = PAGE_SIZE * 2;
422
423 stack_prot = PROT_READ | PROT_WRITE;
424 if (executable_stack == EXSTACK_ENABLE_X ||
425 (executable_stack == EXSTACK_DEFAULT && VM_STACK_FLAGS & VM_EXEC))
426 stack_prot |= PROT_EXEC;
427
428 current->mm->start_brk = vm_mmap(NULL, 0, stack_size, stack_prot,
429 MAP_PRIVATE | MAP_ANONYMOUS |
430 MAP_UNINITIALIZED | MAP_GROWSDOWN,
431 0);
432
433 if (IS_ERR_VALUE(current->mm->start_brk)) {
434 retval = current->mm->start_brk;
435 current->mm->start_brk = 0;
436 goto error;
437 }
438
439 current->mm->brk = current->mm->start_brk;
440 current->mm->context.end_brk = current->mm->start_brk;
441 current->mm->start_stack = current->mm->start_brk + stack_size;
442 #endif
443
444 retval = create_elf_fdpic_tables(bprm, current->mm, &exec_params,
445 &interp_params);
446 if (retval < 0)
447 goto error;
448
449 kdebug("- start_code %lx", current->mm->start_code);
450 kdebug("- end_code %lx", current->mm->end_code);
451 kdebug("- start_data %lx", current->mm->start_data);
452 kdebug("- end_data %lx", current->mm->end_data);
453 kdebug("- start_brk %lx", current->mm->start_brk);
454 kdebug("- brk %lx", current->mm->brk);
455 kdebug("- start_stack %lx", current->mm->start_stack);
456
457 #ifdef ELF_FDPIC_PLAT_INIT
458 /*
459 * The ABI may specify that certain registers be set up in special
460 * ways (on i386 %edx is the address of a DT_FINI function, for
461 * example. This macro performs whatever initialization to
462 * the regs structure is required.
463 */
464 dynaddr = interp_params.dynamic_addr ?: exec_params.dynamic_addr;
465 ELF_FDPIC_PLAT_INIT(regs, exec_params.map_addr, interp_params.map_addr,
466 dynaddr);
467 #endif
468
469 finalize_exec(bprm);
470 /* everything is now ready... get the userspace context ready to roll */
471 entryaddr = interp_params.entry_addr ?: exec_params.entry_addr;
472 start_thread(regs, entryaddr, current->mm->start_stack);
473
474 retval = 0;
475
476 error:
477 if (interpreter) {
478 exe_file_allow_write_access(interpreter);
479 fput(interpreter);
480 }
481 kfree(interpreter_name);
482 kfree(exec_params.phdrs);
483 kfree(exec_params.loadmap);
484 kfree(interp_params.phdrs);
485 kfree(interp_params.loadmap);
486 return retval;
487 }
488
489 /*****************************************************************************/
490
491 #ifndef ELF_BASE_PLATFORM
492 /*
493 * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
494 * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
495 * will be copied to the user stack in the same manner as AT_PLATFORM.
496 */
497 #define ELF_BASE_PLATFORM NULL
498 #endif
499
500 /*
501 * present useful information to the program by shovelling it onto the new
502 * process's stack
503 */
create_elf_fdpic_tables(struct linux_binprm * bprm,struct mm_struct * mm,struct elf_fdpic_params * exec_params,struct elf_fdpic_params * interp_params)504 static int create_elf_fdpic_tables(struct linux_binprm *bprm,
505 struct mm_struct *mm,
506 struct elf_fdpic_params *exec_params,
507 struct elf_fdpic_params *interp_params)
508 {
509 const struct cred *cred = current_cred();
510 unsigned long sp, csp, nitems;
511 elf_caddr_t __user *argv, *envp;
512 size_t platform_len = 0, len;
513 char *k_platform, *k_base_platform;
514 char __user *u_platform, *u_base_platform, *p;
515 int loop;
516 int ei_index;
517 elf_addr_t *elf_info;
518
519 #ifdef CONFIG_MMU
520 /* In some cases (e.g. Hyper-Threading), we want to avoid L1 evictions
521 * by the processes running on the same package. One thing we can do is
522 * to shuffle the initial stack for them, so we give the architecture
523 * an opportunity to do so here.
524 */
525 sp = arch_align_stack(bprm->p);
526 #else
527 sp = mm->start_stack;
528
529 /* stack the program arguments and environment */
530 if (transfer_args_to_stack(bprm, &sp) < 0)
531 return -EFAULT;
532 sp &= ~15;
533 #endif
534
535 /*
536 * If this architecture has a platform capability string, copy it
537 * to userspace. In some cases (Sparc), this info is impossible
538 * for userspace to get any other way, in others (i386) it is
539 * merely difficult.
540 */
541 k_platform = ELF_PLATFORM;
542 u_platform = NULL;
543
544 if (k_platform) {
545 platform_len = strlen(k_platform) + 1;
546 sp -= platform_len;
547 u_platform = (char __user *) sp;
548 if (copy_to_user(u_platform, k_platform, platform_len) != 0)
549 return -EFAULT;
550 }
551
552 /*
553 * If this architecture has a "base" platform capability
554 * string, copy it to userspace.
555 */
556 k_base_platform = ELF_BASE_PLATFORM;
557 u_base_platform = NULL;
558
559 if (k_base_platform) {
560 platform_len = strlen(k_base_platform) + 1;
561 sp -= platform_len;
562 u_base_platform = (char __user *) sp;
563 if (copy_to_user(u_base_platform, k_base_platform, platform_len) != 0)
564 return -EFAULT;
565 }
566
567 sp &= ~7UL;
568
569 /* stack the load map(s) */
570 len = sizeof(struct elf_fdpic_loadmap);
571 len += sizeof(struct elf_fdpic_loadseg) * exec_params->loadmap->nsegs;
572 sp = (sp - len) & ~7UL;
573 exec_params->map_addr = sp;
574
575 if (copy_to_user((void __user *) sp, exec_params->loadmap, len) != 0)
576 return -EFAULT;
577
578 current->mm->context.exec_fdpic_loadmap = (unsigned long) sp;
579
580 if (interp_params->loadmap) {
581 len = sizeof(struct elf_fdpic_loadmap);
582 len += sizeof(struct elf_fdpic_loadseg) *
583 interp_params->loadmap->nsegs;
584 sp = (sp - len) & ~7UL;
585 interp_params->map_addr = sp;
586
587 if (copy_to_user((void __user *) sp, interp_params->loadmap,
588 len) != 0)
589 return -EFAULT;
590
591 current->mm->context.interp_fdpic_loadmap = (unsigned long) sp;
592 }
593
594 /* force 16 byte _final_ alignment here for generality */
595 #define DLINFO_ITEMS 15
596
597 nitems = 1 + DLINFO_ITEMS + (k_platform ? 1 : 0) +
598 (k_base_platform ? 1 : 0) + AT_VECTOR_SIZE_ARCH;
599
600 if (bprm->have_execfd)
601 nitems++;
602 #ifdef ELF_HWCAP2
603 nitems++;
604 #endif
605 #ifdef ELF_HWCAP3
606 nitems++;
607 #endif
608 #ifdef ELF_HWCAP4
609 nitems++;
610 #endif
611
612 csp = sp;
613 sp -= nitems * 2 * sizeof(unsigned long);
614 sp -= (bprm->envc + 1) * sizeof(char *); /* envv[] */
615 sp -= (bprm->argc + 1) * sizeof(char *); /* argv[] */
616 sp -= 1 * sizeof(unsigned long); /* argc */
617
618 csp -= sp & 15UL;
619 sp -= sp & 15UL;
620
621 /* Create the ELF interpreter info */
622 elf_info = (elf_addr_t *)mm->saved_auxv;
623 /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
624 #define NEW_AUX_ENT(id, val) \
625 do { \
626 *elf_info++ = id; \
627 *elf_info++ = val; \
628 } while (0)
629
630 #ifdef ARCH_DLINFO
631 /*
632 * ARCH_DLINFO must come first so PPC can do its special alignment of
633 * AUXV.
634 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
635 * ARCH_DLINFO changes
636 */
637 ARCH_DLINFO;
638 #endif
639 NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
640 #ifdef ELF_HWCAP2
641 NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
642 #endif
643 #ifdef ELF_HWCAP3
644 NEW_AUX_ENT(AT_HWCAP3, ELF_HWCAP3);
645 #endif
646 #ifdef ELF_HWCAP4
647 NEW_AUX_ENT(AT_HWCAP4, ELF_HWCAP4);
648 #endif
649 NEW_AUX_ENT(AT_PAGESZ, PAGE_SIZE);
650 NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
651 NEW_AUX_ENT(AT_PHDR, exec_params->ph_addr);
652 NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
653 NEW_AUX_ENT(AT_PHNUM, exec_params->hdr.e_phnum);
654 NEW_AUX_ENT(AT_BASE, interp_params->elfhdr_addr);
655 NEW_AUX_ENT(AT_FLAGS, bprm_at_flags(bprm));
656 NEW_AUX_ENT(AT_ENTRY, exec_params->entry_addr);
657 NEW_AUX_ENT(AT_UID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->uid));
658 NEW_AUX_ENT(AT_EUID, (elf_addr_t) from_kuid_munged(cred->user_ns, cred->euid));
659 NEW_AUX_ENT(AT_GID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->gid));
660 NEW_AUX_ENT(AT_EGID, (elf_addr_t) from_kgid_munged(cred->user_ns, cred->egid));
661 NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
662 NEW_AUX_ENT(AT_EXECFN, bprm->exec);
663 if (k_platform)
664 NEW_AUX_ENT(AT_PLATFORM,
665 (elf_addr_t)(unsigned long)u_platform);
666 if (k_base_platform)
667 NEW_AUX_ENT(AT_BASE_PLATFORM,
668 (elf_addr_t)(unsigned long)u_base_platform);
669 if (bprm->have_execfd)
670 NEW_AUX_ENT(AT_EXECFD, bprm->execfd);
671 #undef NEW_AUX_ENT
672 /* AT_NULL is zero; clear the rest too */
673 memset(elf_info, 0, (char *)mm->saved_auxv +
674 sizeof(mm->saved_auxv) - (char *)elf_info);
675
676 /* And advance past the AT_NULL entry. */
677 elf_info += 2;
678
679 ei_index = elf_info - (elf_addr_t *)mm->saved_auxv;
680 csp -= ei_index * sizeof(elf_addr_t);
681
682 /* Put the elf_info on the stack in the right place. */
683 if (copy_to_user((void __user *)csp, mm->saved_auxv,
684 ei_index * sizeof(elf_addr_t)))
685 return -EFAULT;
686
687 /* allocate room for argv[] and envv[] */
688 csp -= (bprm->envc + 1) * sizeof(elf_caddr_t);
689 envp = (elf_caddr_t __user *) csp;
690 csp -= (bprm->argc + 1) * sizeof(elf_caddr_t);
691 argv = (elf_caddr_t __user *) csp;
692
693 /* stack argc */
694 csp -= sizeof(unsigned long);
695 if (put_user(bprm->argc, (unsigned long __user *) csp))
696 return -EFAULT;
697
698 BUG_ON(csp != sp);
699
700 /* fill in the argv[] array */
701 #ifdef CONFIG_MMU
702 current->mm->arg_start = bprm->p;
703 #else
704 current->mm->arg_start = current->mm->start_stack -
705 (MAX_ARG_PAGES * PAGE_SIZE - bprm->p);
706 #endif
707
708 p = (char __user *) current->mm->arg_start;
709 for (loop = bprm->argc; loop > 0; loop--) {
710 if (put_user((elf_caddr_t) p, argv++))
711 return -EFAULT;
712 len = strnlen_user(p, MAX_ARG_STRLEN);
713 if (!len || len > MAX_ARG_STRLEN)
714 return -EINVAL;
715 p += len;
716 }
717 if (put_user(NULL, argv))
718 return -EFAULT;
719 current->mm->arg_end = (unsigned long) p;
720
721 /* fill in the envv[] array */
722 current->mm->env_start = (unsigned long) p;
723 for (loop = bprm->envc; loop > 0; loop--) {
724 if (put_user((elf_caddr_t)(unsigned long) p, envp++))
725 return -EFAULT;
726 len = strnlen_user(p, MAX_ARG_STRLEN);
727 if (!len || len > MAX_ARG_STRLEN)
728 return -EINVAL;
729 p += len;
730 }
731 if (put_user(NULL, envp))
732 return -EFAULT;
733 current->mm->env_end = (unsigned long) p;
734
735 mm->start_stack = (unsigned long) sp;
736 return 0;
737 }
738
739 /*****************************************************************************/
740 /*
741 * load the appropriate binary image (executable or interpreter) into memory
742 * - we assume no MMU is available
743 * - if no other PIC bits are set in params->hdr->e_flags
744 * - we assume that the LOADable segments in the binary are independently relocatable
745 * - we assume R/O executable segments are shareable
746 * - else
747 * - we assume the loadable parts of the image to require fixed displacement
748 * - the image is not shareable
749 */
elf_fdpic_map_file(struct elf_fdpic_params * params,struct file * file,struct mm_struct * mm,const char * what)750 static int elf_fdpic_map_file(struct elf_fdpic_params *params,
751 struct file *file,
752 struct mm_struct *mm,
753 const char *what)
754 {
755 struct elf_fdpic_loadmap *loadmap;
756 #ifdef CONFIG_MMU
757 struct elf_fdpic_loadseg *mseg;
758 unsigned long load_addr;
759 #endif
760 struct elf_fdpic_loadseg *seg;
761 struct elf_phdr *phdr;
762 unsigned nloads, tmp;
763 unsigned long stop;
764 int loop, ret;
765
766 /* allocate a load map table */
767 nloads = 0;
768 for (loop = 0; loop < params->hdr.e_phnum; loop++)
769 if (params->phdrs[loop].p_type == PT_LOAD)
770 nloads++;
771
772 if (nloads == 0)
773 return -ELIBBAD;
774
775 loadmap = kzalloc_flex(*loadmap, segs, nloads);
776 if (!loadmap)
777 return -ENOMEM;
778
779 params->loadmap = loadmap;
780
781 loadmap->version = ELF_FDPIC_LOADMAP_VERSION;
782 loadmap->nsegs = nloads;
783
784 /* map the requested LOADs into the memory space */
785 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
786 case ELF_FDPIC_FLAG_CONSTDISP:
787 case ELF_FDPIC_FLAG_CONTIGUOUS:
788 #ifndef CONFIG_MMU
789 ret = elf_fdpic_map_file_constdisp_on_uclinux(params, file, mm);
790 if (ret < 0)
791 return ret;
792 break;
793 #endif
794 default:
795 ret = elf_fdpic_map_file_by_direct_mmap(params, file, mm);
796 if (ret < 0)
797 return ret;
798 break;
799 }
800
801 /* map the entry point */
802 if (params->hdr.e_entry) {
803 seg = loadmap->segs;
804 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
805 if (params->hdr.e_entry >= seg->p_vaddr &&
806 params->hdr.e_entry < seg->p_vaddr + seg->p_memsz) {
807 params->entry_addr =
808 (params->hdr.e_entry - seg->p_vaddr) +
809 seg->addr;
810 break;
811 }
812 }
813 }
814
815 /* determine where the program header table has wound up if mapped */
816 stop = params->hdr.e_phoff;
817 stop += params->hdr.e_phnum * sizeof (struct elf_phdr);
818 phdr = params->phdrs;
819
820 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
821 if (phdr->p_type != PT_LOAD)
822 continue;
823
824 if (phdr->p_offset > params->hdr.e_phoff ||
825 phdr->p_offset + phdr->p_filesz < stop)
826 continue;
827
828 seg = loadmap->segs;
829 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
830 if (phdr->p_vaddr >= seg->p_vaddr &&
831 phdr->p_vaddr + phdr->p_filesz <=
832 seg->p_vaddr + seg->p_memsz) {
833 params->ph_addr =
834 (phdr->p_vaddr - seg->p_vaddr) +
835 seg->addr +
836 params->hdr.e_phoff - phdr->p_offset;
837 break;
838 }
839 }
840 break;
841 }
842
843 /* determine where the dynamic section has wound up if there is one */
844 phdr = params->phdrs;
845 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
846 if (phdr->p_type != PT_DYNAMIC)
847 continue;
848
849 seg = loadmap->segs;
850 for (loop = loadmap->nsegs; loop > 0; loop--, seg++) {
851 if (phdr->p_vaddr >= seg->p_vaddr &&
852 phdr->p_vaddr + phdr->p_memsz <=
853 seg->p_vaddr + seg->p_memsz) {
854 Elf_Dyn __user *dyn;
855 Elf_Sword d_tag;
856
857 params->dynamic_addr =
858 (phdr->p_vaddr - seg->p_vaddr) +
859 seg->addr;
860
861 /* check the dynamic section contains at least
862 * one item, and that the last item is a NULL
863 * entry */
864 if (phdr->p_memsz == 0 ||
865 phdr->p_memsz % sizeof(Elf_Dyn) != 0)
866 goto dynamic_error;
867
868 tmp = phdr->p_memsz / sizeof(Elf_Dyn);
869 dyn = (Elf_Dyn __user *)params->dynamic_addr;
870 if (get_user(d_tag, &dyn[tmp - 1].d_tag) ||
871 d_tag != 0)
872 goto dynamic_error;
873 break;
874 }
875 }
876 break;
877 }
878
879 /* now elide adjacent segments in the load map on MMU linux
880 * - on uClinux the holes between may actually be filled with system
881 * stuff or stuff from other processes
882 */
883 #ifdef CONFIG_MMU
884 nloads = loadmap->nsegs;
885 mseg = loadmap->segs;
886 seg = mseg + 1;
887 for (loop = 1; loop < nloads; loop++) {
888 /* see if we have a candidate for merging */
889 if (seg->p_vaddr - mseg->p_vaddr == seg->addr - mseg->addr) {
890 load_addr = PAGE_ALIGN(mseg->addr + mseg->p_memsz);
891 if (load_addr == (seg->addr & PAGE_MASK)) {
892 mseg->p_memsz +=
893 load_addr -
894 (mseg->addr + mseg->p_memsz);
895 mseg->p_memsz += seg->addr & ~PAGE_MASK;
896 mseg->p_memsz += seg->p_memsz;
897 loadmap->nsegs--;
898 continue;
899 }
900 }
901
902 mseg++;
903 if (mseg != seg)
904 *mseg = *seg;
905 }
906 #endif
907
908 kdebug("Mapped Object [%s]:", what);
909 kdebug("- elfhdr : %lx", params->elfhdr_addr);
910 kdebug("- entry : %lx", params->entry_addr);
911 kdebug("- PHDR[] : %lx", params->ph_addr);
912 kdebug("- DYNAMIC[]: %lx", params->dynamic_addr);
913 seg = loadmap->segs;
914 for (loop = 0; loop < loadmap->nsegs; loop++, seg++)
915 kdebug("- LOAD[%d] : %08llx-%08llx [va=%llx ms=%llx]",
916 loop,
917 (unsigned long long) seg->addr,
918 (unsigned long long) seg->addr + seg->p_memsz - 1,
919 (unsigned long long) seg->p_vaddr,
920 (unsigned long long) seg->p_memsz);
921
922 return 0;
923
924 dynamic_error:
925 printk("ELF FDPIC %s with invalid DYNAMIC section (inode=%llu)\n",
926 what, file_inode(file)->i_ino);
927 return -ELIBBAD;
928 }
929
930 /*****************************************************************************/
931 /*
932 * map a file with constant displacement under uClinux
933 */
934 #ifndef CONFIG_MMU
elf_fdpic_map_file_constdisp_on_uclinux(struct elf_fdpic_params * params,struct file * file,struct mm_struct * mm)935 static int elf_fdpic_map_file_constdisp_on_uclinux(
936 struct elf_fdpic_params *params,
937 struct file *file,
938 struct mm_struct *mm)
939 {
940 struct elf_fdpic_loadseg *seg;
941 struct elf_phdr *phdr;
942 unsigned long load_addr, base = ULONG_MAX, top = 0, maddr = 0;
943 int loop, ret;
944
945 load_addr = params->load_addr;
946 seg = params->loadmap->segs;
947
948 /* determine the bounds of the contiguous overall allocation we must
949 * make */
950 phdr = params->phdrs;
951 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
952 if (params->phdrs[loop].p_type != PT_LOAD)
953 continue;
954
955 if (base > phdr->p_vaddr)
956 base = phdr->p_vaddr;
957 if (top < phdr->p_vaddr + phdr->p_memsz)
958 top = phdr->p_vaddr + phdr->p_memsz;
959 }
960
961 /* allocate one big anon block for everything */
962 maddr = vm_mmap(NULL, load_addr, top - base,
963 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, 0);
964 if (IS_ERR_VALUE(maddr))
965 return (int) maddr;
966
967 if (load_addr != 0)
968 load_addr += PAGE_ALIGN(top - base);
969
970 /* and then load the file segments into it */
971 phdr = params->phdrs;
972 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
973 if (params->phdrs[loop].p_type != PT_LOAD)
974 continue;
975
976 seg->addr = maddr + (phdr->p_vaddr - base);
977 seg->p_vaddr = phdr->p_vaddr;
978 seg->p_memsz = phdr->p_memsz;
979
980 ret = read_code(file, seg->addr, phdr->p_offset,
981 phdr->p_filesz);
982 if (ret < 0)
983 return ret;
984
985 /* map the ELF header address if in this segment */
986 if (phdr->p_offset == 0)
987 params->elfhdr_addr = seg->addr;
988
989 /* clear any space allocated but not loaded */
990 if (phdr->p_filesz < phdr->p_memsz) {
991 if (clear_user((void *) (seg->addr + phdr->p_filesz),
992 phdr->p_memsz - phdr->p_filesz))
993 return -EFAULT;
994 }
995
996 if (mm) {
997 if (phdr->p_flags & PF_X) {
998 if (!mm->start_code) {
999 mm->start_code = seg->addr;
1000 mm->end_code = seg->addr +
1001 phdr->p_memsz;
1002 }
1003 } else if (!mm->start_data) {
1004 mm->start_data = seg->addr;
1005 mm->end_data = seg->addr + phdr->p_memsz;
1006 }
1007 }
1008
1009 seg++;
1010 }
1011
1012 return 0;
1013 }
1014 #endif
1015
1016 /*****************************************************************************/
1017 /*
1018 * map a binary by direct mmap() of the individual PT_LOAD segments
1019 */
elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params * params,struct file * file,struct mm_struct * mm)1020 static int elf_fdpic_map_file_by_direct_mmap(struct elf_fdpic_params *params,
1021 struct file *file,
1022 struct mm_struct *mm)
1023 {
1024 struct elf_fdpic_loadseg *seg;
1025 struct elf_phdr *phdr;
1026 unsigned long load_addr, delta_vaddr;
1027 int loop, dvset;
1028
1029 load_addr = params->load_addr;
1030 delta_vaddr = 0;
1031 dvset = 0;
1032
1033 seg = params->loadmap->segs;
1034
1035 /* deal with each load segment separately */
1036 phdr = params->phdrs;
1037 for (loop = 0; loop < params->hdr.e_phnum; loop++, phdr++) {
1038 unsigned long maddr, disp, excess;
1039 int prot = 0, flags;
1040
1041 if (phdr->p_type != PT_LOAD)
1042 continue;
1043
1044 kdebug("[LOAD] va=%lx of=%lx fs=%lx ms=%lx",
1045 (unsigned long) phdr->p_vaddr,
1046 (unsigned long) phdr->p_offset,
1047 (unsigned long) phdr->p_filesz,
1048 (unsigned long) phdr->p_memsz);
1049
1050 /* determine the mapping parameters */
1051 if (phdr->p_flags & PF_R) prot |= PROT_READ;
1052 if (phdr->p_flags & PF_W) prot |= PROT_WRITE;
1053 if (phdr->p_flags & PF_X) prot |= PROT_EXEC;
1054
1055 flags = MAP_PRIVATE;
1056 maddr = 0;
1057
1058 switch (params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) {
1059 case ELF_FDPIC_FLAG_INDEPENDENT:
1060 /* PT_LOADs are independently locatable */
1061 break;
1062
1063 case ELF_FDPIC_FLAG_HONOURVADDR:
1064 /* the specified virtual address must be honoured */
1065 maddr = phdr->p_vaddr;
1066 flags |= MAP_FIXED;
1067 break;
1068
1069 case ELF_FDPIC_FLAG_CONSTDISP:
1070 /* constant displacement
1071 * - can be mapped anywhere, but must be mapped as a
1072 * unit
1073 */
1074 if (!dvset) {
1075 maddr = load_addr;
1076 delta_vaddr = phdr->p_vaddr;
1077 dvset = 1;
1078 } else {
1079 maddr = load_addr + phdr->p_vaddr - delta_vaddr;
1080 flags |= MAP_FIXED;
1081 }
1082 break;
1083
1084 case ELF_FDPIC_FLAG_CONTIGUOUS:
1085 /* contiguity handled later */
1086 break;
1087
1088 default:
1089 BUG();
1090 }
1091
1092 maddr &= PAGE_MASK;
1093
1094 /* create the mapping */
1095 disp = phdr->p_vaddr & ~PAGE_MASK;
1096 maddr = vm_mmap(file, maddr, phdr->p_memsz + disp, prot, flags,
1097 phdr->p_offset - disp);
1098
1099 kdebug("mmap[%d] <file> sz=%llx pr=%x fl=%x of=%llx --> %08lx",
1100 loop, (unsigned long long) phdr->p_memsz + disp,
1101 prot, flags, (unsigned long long) phdr->p_offset - disp,
1102 maddr);
1103
1104 if (IS_ERR_VALUE(maddr))
1105 return (int) maddr;
1106
1107 if ((params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
1108 ELF_FDPIC_FLAG_CONTIGUOUS)
1109 load_addr += PAGE_ALIGN(phdr->p_memsz + disp);
1110
1111 seg->addr = maddr + disp;
1112 seg->p_vaddr = phdr->p_vaddr;
1113 seg->p_memsz = phdr->p_memsz;
1114
1115 /* map the ELF header address if in this segment */
1116 if (phdr->p_offset == 0)
1117 params->elfhdr_addr = seg->addr;
1118
1119 /* clear the bit between beginning of mapping and beginning of
1120 * PT_LOAD */
1121 if (prot & PROT_WRITE && disp > 0) {
1122 kdebug("clear[%d] ad=%lx sz=%lx", loop, maddr, disp);
1123 if (clear_user((void __user *) maddr, disp))
1124 return -EFAULT;
1125 maddr += disp;
1126 }
1127
1128 /* clear any space allocated but not loaded
1129 * - on uClinux we can just clear the lot
1130 * - on MMU linux we'll get a SIGBUS beyond the last page
1131 * extant in the file
1132 */
1133 excess = phdr->p_memsz - phdr->p_filesz;
1134
1135 #ifdef CONFIG_MMU
1136 unsigned long excess1
1137 = PAGE_SIZE - ((maddr + phdr->p_filesz) & ~PAGE_MASK);
1138 if (excess > excess1) {
1139 unsigned long xaddr = maddr + phdr->p_filesz + excess1;
1140 unsigned long xmaddr;
1141
1142 flags |= MAP_FIXED | MAP_ANONYMOUS;
1143 xmaddr = vm_mmap(NULL, xaddr, excess - excess1,
1144 prot, flags, 0);
1145
1146 kdebug("mmap[%d] <anon>"
1147 " ad=%lx sz=%lx pr=%x fl=%x of=0 --> %08lx",
1148 loop, xaddr, excess - excess1, prot, flags,
1149 xmaddr);
1150
1151 if (xmaddr != xaddr)
1152 return -ENOMEM;
1153 }
1154
1155 if (prot & PROT_WRITE && excess1 > 0) {
1156 kdebug("clear[%d] ad=%lx sz=%lx",
1157 loop, maddr + phdr->p_filesz, excess1);
1158 if (clear_user((void __user *) maddr + phdr->p_filesz,
1159 excess1))
1160 return -EFAULT;
1161 }
1162
1163 #else
1164 if (excess > 0) {
1165 kdebug("clear[%d] ad=%llx sz=%lx", loop,
1166 (unsigned long long) maddr + phdr->p_filesz,
1167 excess);
1168 if (clear_user((void *) maddr + phdr->p_filesz, excess))
1169 return -EFAULT;
1170 }
1171 #endif
1172
1173 if (mm) {
1174 if (phdr->p_flags & PF_X) {
1175 if (!mm->start_code) {
1176 mm->start_code = maddr;
1177 mm->end_code = maddr + phdr->p_memsz;
1178 }
1179 } else if (!mm->start_data) {
1180 mm->start_data = maddr;
1181 mm->end_data = maddr + phdr->p_memsz;
1182 }
1183 }
1184
1185 seg++;
1186 }
1187
1188 return 0;
1189 }
1190
1191 /*****************************************************************************/
1192 /*
1193 * ELF-FDPIC core dumper
1194 *
1195 * Modelled on fs/exec.c:aout_core_dump()
1196 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1197 *
1198 * Modelled on fs/binfmt_elf.c core dumper
1199 */
1200 #ifdef CONFIG_ELF_CORE
1201
1202 struct elf_prstatus_fdpic
1203 {
1204 struct elf_prstatus_common common;
1205 elf_gregset_t pr_reg; /* GP registers */
1206 /* When using FDPIC, the loadmap addresses need to be communicated
1207 * to GDB in order for GDB to do the necessary relocations. The
1208 * fields (below) used to communicate this information are placed
1209 * immediately after ``pr_reg'', so that the loadmap addresses may
1210 * be viewed as part of the register set if so desired.
1211 */
1212 unsigned long pr_exec_fdpic_loadmap;
1213 unsigned long pr_interp_fdpic_loadmap;
1214 int pr_fpvalid; /* True if math co-processor being used. */
1215 };
1216
1217 /* An ELF note in memory */
1218 struct memelfnote
1219 {
1220 const char *name;
1221 int type;
1222 unsigned int datasz;
1223 void *data;
1224 };
1225
notesize(struct memelfnote * en)1226 static int notesize(struct memelfnote *en)
1227 {
1228 int sz;
1229
1230 sz = sizeof(struct elf_note);
1231 sz += roundup(strlen(en->name) + 1, 4);
1232 sz += roundup(en->datasz, 4);
1233
1234 return sz;
1235 }
1236
1237 /* #define DEBUG */
1238
writenote(struct memelfnote * men,struct coredump_params * cprm)1239 static int writenote(struct memelfnote *men, struct coredump_params *cprm)
1240 {
1241 struct elf_note en;
1242 en.n_namesz = strlen(men->name) + 1;
1243 en.n_descsz = men->datasz;
1244 en.n_type = men->type;
1245
1246 return dump_emit(cprm, &en, sizeof(en)) &&
1247 dump_emit(cprm, men->name, en.n_namesz) && dump_align(cprm, 4) &&
1248 dump_emit(cprm, men->data, men->datasz) && dump_align(cprm, 4);
1249 }
1250
fill_elf_fdpic_header(struct elfhdr * elf,int segs)1251 static inline void fill_elf_fdpic_header(struct elfhdr *elf, int segs)
1252 {
1253 memcpy(elf->e_ident, ELFMAG, SELFMAG);
1254 elf->e_ident[EI_CLASS] = ELF_CLASS;
1255 elf->e_ident[EI_DATA] = ELF_DATA;
1256 elf->e_ident[EI_VERSION] = EV_CURRENT;
1257 elf->e_ident[EI_OSABI] = ELF_OSABI;
1258 memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);
1259
1260 elf->e_type = ET_CORE;
1261 elf->e_machine = ELF_ARCH;
1262 elf->e_version = EV_CURRENT;
1263 elf->e_entry = 0;
1264 elf->e_phoff = sizeof(struct elfhdr);
1265 elf->e_shoff = 0;
1266 elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
1267 elf->e_ehsize = sizeof(struct elfhdr);
1268 elf->e_phentsize = sizeof(struct elf_phdr);
1269 elf->e_phnum = segs;
1270 elf->e_shentsize = 0;
1271 elf->e_shnum = 0;
1272 elf->e_shstrndx = 0;
1273 return;
1274 }
1275
fill_elf_note_phdr(struct elf_phdr * phdr,int sz,loff_t offset)1276 static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1277 {
1278 phdr->p_type = PT_NOTE;
1279 phdr->p_offset = offset;
1280 phdr->p_vaddr = 0;
1281 phdr->p_paddr = 0;
1282 phdr->p_filesz = sz;
1283 phdr->p_memsz = 0;
1284 phdr->p_flags = 0;
1285 phdr->p_align = 4;
1286 return;
1287 }
1288
__fill_note(struct memelfnote * note,const char * name,int type,unsigned int sz,void * data)1289 static inline void __fill_note(struct memelfnote *note, const char *name, int type,
1290 unsigned int sz, void *data)
1291 {
1292 note->name = name;
1293 note->type = type;
1294 note->datasz = sz;
1295 note->data = data;
1296 return;
1297 }
1298
1299 #define fill_note(note, type, sz, data) \
1300 __fill_note(note, NN_ ## type, NT_ ## type, sz, data)
1301
1302 /*
1303 * fill up all the fields in prstatus from the given task struct, except
1304 * registers which need to be filled up separately.
1305 */
fill_prstatus(struct elf_prstatus_common * prstatus,struct task_struct * p,long signr)1306 static void fill_prstatus(struct elf_prstatus_common *prstatus,
1307 struct task_struct *p, long signr)
1308 {
1309 prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1310 prstatus->pr_sigpend = p->pending.signal.sig[0];
1311 prstatus->pr_sighold = p->blocked.sig[0];
1312 rcu_read_lock();
1313 prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1314 rcu_read_unlock();
1315 prstatus->pr_pid = task_pid_vnr(p);
1316 prstatus->pr_pgrp = task_pgrp_vnr(p);
1317 prstatus->pr_sid = task_session_vnr(p);
1318 if (thread_group_leader(p)) {
1319 struct task_cputime cputime;
1320
1321 /*
1322 * This is the record for the group leader. It shows the
1323 * group-wide total, not its individual thread total.
1324 */
1325 thread_group_cputime(p, &cputime);
1326 prstatus->pr_utime = ns_to_kernel_old_timeval(cputime.utime);
1327 prstatus->pr_stime = ns_to_kernel_old_timeval(cputime.stime);
1328 } else {
1329 u64 utime, stime;
1330
1331 task_cputime(p, &utime, &stime);
1332 prstatus->pr_utime = ns_to_kernel_old_timeval(utime);
1333 prstatus->pr_stime = ns_to_kernel_old_timeval(stime);
1334 }
1335 prstatus->pr_cutime = ns_to_kernel_old_timeval(p->signal->cutime);
1336 prstatus->pr_cstime = ns_to_kernel_old_timeval(p->signal->cstime);
1337 }
1338
fill_psinfo(struct elf_prpsinfo * psinfo,struct task_struct * p,struct mm_struct * mm)1339 static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1340 struct mm_struct *mm)
1341 {
1342 const struct cred *cred;
1343 unsigned int i, len;
1344 unsigned int state;
1345
1346 /* first copy the parameters from user space */
1347 memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1348
1349 len = mm->arg_end - mm->arg_start;
1350 if (len >= ELF_PRARGSZ)
1351 len = ELF_PRARGSZ - 1;
1352 if (copy_from_user(&psinfo->pr_psargs,
1353 (const char __user *) mm->arg_start, len))
1354 return -EFAULT;
1355 for (i = 0; i < len; i++)
1356 if (psinfo->pr_psargs[i] == 0)
1357 psinfo->pr_psargs[i] = ' ';
1358 psinfo->pr_psargs[len] = 0;
1359
1360 rcu_read_lock();
1361 psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1362 rcu_read_unlock();
1363 psinfo->pr_pid = task_pid_vnr(p);
1364 psinfo->pr_pgrp = task_pgrp_vnr(p);
1365 psinfo->pr_sid = task_session_vnr(p);
1366
1367 state = READ_ONCE(p->__state);
1368 i = state ? ffz(~state) + 1 : 0;
1369 psinfo->pr_state = i;
1370 psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1371 psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1372 psinfo->pr_nice = task_nice(p);
1373 psinfo->pr_flag = p->flags;
1374 rcu_read_lock();
1375 cred = __task_cred(p);
1376 SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid));
1377 SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
1378 rcu_read_unlock();
1379 get_task_comm(psinfo->pr_fname, p);
1380
1381 return 0;
1382 }
1383
1384 /* Here is the structure in which status of each thread is captured. */
1385 struct elf_thread_status
1386 {
1387 struct elf_thread_status *next;
1388 struct elf_prstatus_fdpic prstatus; /* NT_PRSTATUS */
1389 elf_fpregset_t fpu; /* NT_PRFPREG */
1390 struct memelfnote notes[2];
1391 int num_notes;
1392 };
1393
1394 /*
1395 * In order to add the specific thread information for the elf file format,
1396 * we need to keep a linked list of every thread's pr_status and then create
1397 * a single section for them in the final core file.
1398 */
elf_dump_thread_status(long signr,struct task_struct * p,int * sz)1399 static struct elf_thread_status *elf_dump_thread_status(long signr, struct task_struct *p, int *sz)
1400 {
1401 const struct user_regset_view *view = task_user_regset_view(p);
1402 struct elf_thread_status *t;
1403 int i, ret;
1404
1405 t = kzalloc_obj(struct elf_thread_status);
1406 if (!t)
1407 return t;
1408
1409 fill_prstatus(&t->prstatus.common, p, signr);
1410 t->prstatus.pr_exec_fdpic_loadmap = p->mm->context.exec_fdpic_loadmap;
1411 t->prstatus.pr_interp_fdpic_loadmap = p->mm->context.interp_fdpic_loadmap;
1412 regset_get(p, &view->regsets[0],
1413 sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg);
1414
1415 fill_note(&t->notes[0], PRSTATUS, sizeof(t->prstatus), &t->prstatus);
1416 t->num_notes++;
1417 *sz += notesize(&t->notes[0]);
1418
1419 for (i = 1; i < view->n; ++i) {
1420 const struct user_regset *regset = &view->regsets[i];
1421 if (regset->core_note_type != NT_PRFPREG)
1422 continue;
1423 if (regset->active && regset->active(p, regset) <= 0)
1424 continue;
1425 ret = regset_get(p, regset, sizeof(t->fpu), &t->fpu);
1426 if (ret >= 0)
1427 t->prstatus.pr_fpvalid = 1;
1428 break;
1429 }
1430
1431 if (t->prstatus.pr_fpvalid) {
1432 fill_note(&t->notes[1], PRFPREG, sizeof(t->fpu), &t->fpu);
1433 t->num_notes++;
1434 *sz += notesize(&t->notes[1]);
1435 }
1436 return t;
1437 }
1438
fill_extnum_info(struct elfhdr * elf,struct elf_shdr * shdr4extnum,elf_addr_t e_shoff,int segs)1439 static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
1440 elf_addr_t e_shoff, int segs)
1441 {
1442 elf->e_shoff = e_shoff;
1443 elf->e_shentsize = sizeof(*shdr4extnum);
1444 elf->e_shnum = 1;
1445 elf->e_shstrndx = SHN_UNDEF;
1446
1447 memset(shdr4extnum, 0, sizeof(*shdr4extnum));
1448
1449 shdr4extnum->sh_type = SHT_NULL;
1450 shdr4extnum->sh_size = elf->e_shnum;
1451 shdr4extnum->sh_link = elf->e_shstrndx;
1452 shdr4extnum->sh_info = segs;
1453 }
1454
1455 /*
1456 * dump the segments for an MMU process
1457 */
elf_fdpic_dump_segments(struct coredump_params * cprm,struct core_vma_metadata * vma_meta,int vma_count)1458 static bool elf_fdpic_dump_segments(struct coredump_params *cprm,
1459 struct core_vma_metadata *vma_meta,
1460 int vma_count)
1461 {
1462 int i;
1463
1464 for (i = 0; i < vma_count; i++) {
1465 struct core_vma_metadata *meta = vma_meta + i;
1466
1467 if (!dump_user_range(cprm, meta->start, meta->dump_size))
1468 return false;
1469 }
1470 return true;
1471 }
1472
1473 /*
1474 * Actual dumper
1475 *
1476 * This is a two-pass process; first we find the offsets of the bits,
1477 * and then they are actually written out. If we run out of core limit
1478 * we just truncate.
1479 */
elf_fdpic_core_dump(struct coredump_params * cprm)1480 static int elf_fdpic_core_dump(struct coredump_params *cprm)
1481 {
1482 int has_dumped = 0;
1483 int segs;
1484 int i;
1485 struct elfhdr *elf = NULL;
1486 loff_t offset = 0, dataoff;
1487 struct memelfnote psinfo_note, auxv_note;
1488 struct elf_prpsinfo *psinfo = NULL; /* NT_PRPSINFO */
1489 struct elf_thread_status *thread_list = NULL;
1490 int thread_status_size = 0;
1491 elf_addr_t *auxv;
1492 struct elf_phdr *phdr4note = NULL;
1493 struct elf_shdr *shdr4extnum = NULL;
1494 Elf_Half e_phnum;
1495 elf_addr_t e_shoff;
1496 struct core_thread *ct;
1497 struct elf_thread_status *tmp;
1498
1499 /* alloc memory for large data structures: too large to be on stack */
1500 elf = kmalloc_obj(*elf);
1501 if (!elf)
1502 goto end_coredump;
1503 psinfo = kmalloc_obj(*psinfo);
1504 if (!psinfo)
1505 goto end_coredump;
1506
1507 for (ct = current->signal->core_state->dumper.next;
1508 ct; ct = ct->next) {
1509 tmp = elf_dump_thread_status(cprm->siginfo->si_signo,
1510 ct->task, &thread_status_size);
1511 if (!tmp)
1512 goto end_coredump;
1513
1514 tmp->next = thread_list;
1515 thread_list = tmp;
1516 }
1517
1518 /* now collect the dump for the current */
1519 tmp = elf_dump_thread_status(cprm->siginfo->si_signo,
1520 current, &thread_status_size);
1521 if (!tmp)
1522 goto end_coredump;
1523 tmp->next = thread_list;
1524 thread_list = tmp;
1525
1526 segs = cprm->vma_count + elf_core_extra_phdrs(cprm);
1527
1528 /* for notes section */
1529 segs++;
1530
1531 /* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
1532 * this, kernel supports extended numbering. Have a look at
1533 * include/linux/elf.h for further information. */
1534 e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
1535
1536 /* Set up header */
1537 fill_elf_fdpic_header(elf, e_phnum);
1538
1539 has_dumped = 1;
1540 /*
1541 * Set up the notes in similar form to SVR4 core dumps made
1542 * with info from their /proc.
1543 */
1544
1545 fill_psinfo(psinfo, current->group_leader, current->mm);
1546 fill_note(&psinfo_note, PRPSINFO, sizeof(*psinfo), psinfo);
1547 thread_status_size += notesize(&psinfo_note);
1548
1549 auxv = (elf_addr_t *) current->mm->saved_auxv;
1550 i = 0;
1551 do
1552 i += 2;
1553 while (auxv[i - 2] != AT_NULL);
1554 fill_note(&auxv_note, AUXV, i * sizeof(elf_addr_t), auxv);
1555 thread_status_size += notesize(&auxv_note);
1556
1557 offset = sizeof(*elf); /* ELF header */
1558 offset += segs * sizeof(struct elf_phdr); /* Program headers */
1559
1560 /* Write notes phdr entry */
1561 phdr4note = kmalloc_obj(*phdr4note);
1562 if (!phdr4note)
1563 goto end_coredump;
1564
1565 fill_elf_note_phdr(phdr4note, thread_status_size, offset);
1566 offset += thread_status_size;
1567
1568 /* Page-align dumped data */
1569 dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
1570
1571 offset += cprm->vma_data_size;
1572 offset += elf_core_extra_data_size(cprm);
1573 e_shoff = offset;
1574
1575 if (e_phnum == PN_XNUM) {
1576 shdr4extnum = kmalloc_obj(*shdr4extnum);
1577 if (!shdr4extnum)
1578 goto end_coredump;
1579 fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
1580 }
1581
1582 offset = dataoff;
1583
1584 if (!dump_emit(cprm, elf, sizeof(*elf)))
1585 goto end_coredump;
1586
1587 if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note)))
1588 goto end_coredump;
1589
1590 /* write program headers for segments dump */
1591 for (i = 0; i < cprm->vma_count; i++) {
1592 struct core_vma_metadata *meta = cprm->vma_meta + i;
1593 struct elf_phdr phdr;
1594 size_t sz;
1595
1596 sz = meta->end - meta->start;
1597
1598 phdr.p_type = PT_LOAD;
1599 phdr.p_offset = offset;
1600 phdr.p_vaddr = meta->start;
1601 phdr.p_paddr = 0;
1602 phdr.p_filesz = meta->dump_size;
1603 phdr.p_memsz = sz;
1604 offset += phdr.p_filesz;
1605 phdr.p_flags = 0;
1606 if (meta->flags & VM_READ)
1607 phdr.p_flags |= PF_R;
1608 if (meta->flags & VM_WRITE)
1609 phdr.p_flags |= PF_W;
1610 if (meta->flags & VM_EXEC)
1611 phdr.p_flags |= PF_X;
1612 phdr.p_align = ELF_EXEC_PAGESIZE;
1613
1614 if (!dump_emit(cprm, &phdr, sizeof(phdr)))
1615 goto end_coredump;
1616 }
1617
1618 if (!elf_core_write_extra_phdrs(cprm, offset))
1619 goto end_coredump;
1620
1621 /* write out the notes section */
1622 if (!writenote(thread_list->notes, cprm))
1623 goto end_coredump;
1624 if (!writenote(&psinfo_note, cprm))
1625 goto end_coredump;
1626 if (!writenote(&auxv_note, cprm))
1627 goto end_coredump;
1628 for (i = 1; i < thread_list->num_notes; i++)
1629 if (!writenote(thread_list->notes + i, cprm))
1630 goto end_coredump;
1631
1632 /* write out the thread status notes section */
1633 for (tmp = thread_list->next; tmp; tmp = tmp->next) {
1634 for (i = 0; i < tmp->num_notes; i++)
1635 if (!writenote(&tmp->notes[i], cprm))
1636 goto end_coredump;
1637 }
1638
1639 dump_skip_to(cprm, dataoff);
1640
1641 if (!elf_fdpic_dump_segments(cprm, cprm->vma_meta, cprm->vma_count))
1642 goto end_coredump;
1643
1644 if (!elf_core_write_extra_data(cprm))
1645 goto end_coredump;
1646
1647 if (e_phnum == PN_XNUM) {
1648 if (!dump_emit(cprm, shdr4extnum, sizeof(*shdr4extnum)))
1649 goto end_coredump;
1650 }
1651
1652 if (cprm->file->f_pos != offset) {
1653 /* Sanity check */
1654 printk(KERN_WARNING
1655 "elf_core_dump: file->f_pos (%lld) != offset (%lld)\n",
1656 cprm->file->f_pos, offset);
1657 }
1658
1659 end_coredump:
1660 while (thread_list) {
1661 tmp = thread_list;
1662 thread_list = thread_list->next;
1663 kfree(tmp);
1664 }
1665 kfree(phdr4note);
1666 kfree(elf);
1667 kfree(psinfo);
1668 kfree(shdr4extnum);
1669 return has_dumped;
1670 }
1671
1672 #endif /* CONFIG_ELF_CORE */
1673