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