xref: /OK3568_Linux_fs/kernel/fs/binfmt_elf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * linux/fs/binfmt_elf.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * These are the functions used to load ELF format executables as used
6*4882a593Smuzhiyun  * on SVr4 machines.  Information on the format may be found in the book
7*4882a593Smuzhiyun  * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
8*4882a593Smuzhiyun  * Tools".
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/fs.h>
16*4882a593Smuzhiyun #include <linux/log2.h>
17*4882a593Smuzhiyun #include <linux/mm.h>
18*4882a593Smuzhiyun #include <linux/mman.h>
19*4882a593Smuzhiyun #include <linux/errno.h>
20*4882a593Smuzhiyun #include <linux/signal.h>
21*4882a593Smuzhiyun #include <linux/binfmts.h>
22*4882a593Smuzhiyun #include <linux/string.h>
23*4882a593Smuzhiyun #include <linux/file.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun #include <linux/personality.h>
26*4882a593Smuzhiyun #include <linux/elfcore.h>
27*4882a593Smuzhiyun #include <linux/init.h>
28*4882a593Smuzhiyun #include <linux/highuid.h>
29*4882a593Smuzhiyun #include <linux/compiler.h>
30*4882a593Smuzhiyun #include <linux/highmem.h>
31*4882a593Smuzhiyun #include <linux/hugetlb.h>
32*4882a593Smuzhiyun #include <linux/pagemap.h>
33*4882a593Smuzhiyun #include <linux/vmalloc.h>
34*4882a593Smuzhiyun #include <linux/security.h>
35*4882a593Smuzhiyun #include <linux/random.h>
36*4882a593Smuzhiyun #include <linux/elf.h>
37*4882a593Smuzhiyun #include <linux/elf-randomize.h>
38*4882a593Smuzhiyun #include <linux/utsname.h>
39*4882a593Smuzhiyun #include <linux/coredump.h>
40*4882a593Smuzhiyun #include <linux/sched.h>
41*4882a593Smuzhiyun #include <linux/sched/coredump.h>
42*4882a593Smuzhiyun #include <linux/sched/task_stack.h>
43*4882a593Smuzhiyun #include <linux/sched/cputime.h>
44*4882a593Smuzhiyun #include <linux/sizes.h>
45*4882a593Smuzhiyun #include <linux/types.h>
46*4882a593Smuzhiyun #include <linux/cred.h>
47*4882a593Smuzhiyun #include <linux/dax.h>
48*4882a593Smuzhiyun #include <linux/uaccess.h>
49*4882a593Smuzhiyun #include <asm/param.h>
50*4882a593Smuzhiyun #include <asm/page.h>
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #ifndef ELF_COMPAT
53*4882a593Smuzhiyun #define ELF_COMPAT 0
54*4882a593Smuzhiyun #endif
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #ifndef user_long_t
57*4882a593Smuzhiyun #define user_long_t long
58*4882a593Smuzhiyun #endif
59*4882a593Smuzhiyun #ifndef user_siginfo_t
60*4882a593Smuzhiyun #define user_siginfo_t siginfo_t
61*4882a593Smuzhiyun #endif
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /* That's for binfmt_elf_fdpic to deal with */
64*4882a593Smuzhiyun #ifndef elf_check_fdpic
65*4882a593Smuzhiyun #define elf_check_fdpic(ex) false
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun static int load_elf_binary(struct linux_binprm *bprm);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #ifdef CONFIG_USELIB
71*4882a593Smuzhiyun static int load_elf_library(struct file *);
72*4882a593Smuzhiyun #else
73*4882a593Smuzhiyun #define load_elf_library NULL
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun  * If we don't support core dumping, then supply a NULL so we
78*4882a593Smuzhiyun  * don't even try.
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun #ifdef CONFIG_ELF_CORE
81*4882a593Smuzhiyun static int elf_core_dump(struct coredump_params *cprm);
82*4882a593Smuzhiyun #else
83*4882a593Smuzhiyun #define elf_core_dump	NULL
84*4882a593Smuzhiyun #endif
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun #if ELF_EXEC_PAGESIZE > PAGE_SIZE
87*4882a593Smuzhiyun #define ELF_MIN_ALIGN	ELF_EXEC_PAGESIZE
88*4882a593Smuzhiyun #else
89*4882a593Smuzhiyun #define ELF_MIN_ALIGN	PAGE_SIZE
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #ifndef ELF_CORE_EFLAGS
93*4882a593Smuzhiyun #define ELF_CORE_EFLAGS	0
94*4882a593Smuzhiyun #endif
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun #define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
97*4882a593Smuzhiyun #define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
98*4882a593Smuzhiyun #define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun static struct linux_binfmt elf_format = {
101*4882a593Smuzhiyun 	.module		= THIS_MODULE,
102*4882a593Smuzhiyun 	.load_binary	= load_elf_binary,
103*4882a593Smuzhiyun 	.load_shlib	= load_elf_library,
104*4882a593Smuzhiyun 	.core_dump	= elf_core_dump,
105*4882a593Smuzhiyun 	.min_coredump	= ELF_EXEC_PAGESIZE,
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun #define BAD_ADDR(x) (unlikely((unsigned long)(x) >= TASK_SIZE))
109*4882a593Smuzhiyun 
set_brk(unsigned long start,unsigned long end,int prot)110*4882a593Smuzhiyun static int set_brk(unsigned long start, unsigned long end, int prot)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	start = ELF_PAGEALIGN(start);
113*4882a593Smuzhiyun 	end = ELF_PAGEALIGN(end);
114*4882a593Smuzhiyun 	if (end > start) {
115*4882a593Smuzhiyun 		/*
116*4882a593Smuzhiyun 		 * Map the last of the bss segment.
117*4882a593Smuzhiyun 		 * If the header is requesting these pages to be
118*4882a593Smuzhiyun 		 * executable, honour that (ppc32 needs this).
119*4882a593Smuzhiyun 		 */
120*4882a593Smuzhiyun 		int error = vm_brk_flags(start, end - start,
121*4882a593Smuzhiyun 				prot & PROT_EXEC ? VM_EXEC : 0);
122*4882a593Smuzhiyun 		if (error)
123*4882a593Smuzhiyun 			return error;
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 	current->mm->start_brk = current->mm->brk = end;
126*4882a593Smuzhiyun 	return 0;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /* We need to explicitly zero any fractional pages
130*4882a593Smuzhiyun    after the data section (i.e. bss).  This would
131*4882a593Smuzhiyun    contain the junk from the file that should not
132*4882a593Smuzhiyun    be in memory
133*4882a593Smuzhiyun  */
padzero(unsigned long elf_bss)134*4882a593Smuzhiyun static int padzero(unsigned long elf_bss)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	unsigned long nbyte;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	nbyte = ELF_PAGEOFFSET(elf_bss);
139*4882a593Smuzhiyun 	if (nbyte) {
140*4882a593Smuzhiyun 		nbyte = ELF_MIN_ALIGN - nbyte;
141*4882a593Smuzhiyun 		if (clear_user((void __user *) elf_bss, nbyte))
142*4882a593Smuzhiyun 			return -EFAULT;
143*4882a593Smuzhiyun 	}
144*4882a593Smuzhiyun 	return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /* Let's use some macros to make this stack manipulation a little clearer */
148*4882a593Smuzhiyun #ifdef CONFIG_STACK_GROWSUP
149*4882a593Smuzhiyun #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) + (items))
150*4882a593Smuzhiyun #define STACK_ROUND(sp, items) \
151*4882a593Smuzhiyun 	((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
152*4882a593Smuzhiyun #define STACK_ALLOC(sp, len) ({ \
153*4882a593Smuzhiyun 	elf_addr_t __user *old_sp = (elf_addr_t __user *)sp; sp += len; \
154*4882a593Smuzhiyun 	old_sp; })
155*4882a593Smuzhiyun #else
156*4882a593Smuzhiyun #define STACK_ADD(sp, items) ((elf_addr_t __user *)(sp) - (items))
157*4882a593Smuzhiyun #define STACK_ROUND(sp, items) \
158*4882a593Smuzhiyun 	(((unsigned long) (sp - items)) &~ 15UL)
159*4882a593Smuzhiyun #define STACK_ALLOC(sp, len) ({ sp -= len ; sp; })
160*4882a593Smuzhiyun #endif
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun #ifndef ELF_BASE_PLATFORM
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun  * AT_BASE_PLATFORM indicates the "real" hardware/microarchitecture.
165*4882a593Smuzhiyun  * If the arch defines ELF_BASE_PLATFORM (in asm/elf.h), the value
166*4882a593Smuzhiyun  * will be copied to the user stack in the same manner as AT_PLATFORM.
167*4882a593Smuzhiyun  */
168*4882a593Smuzhiyun #define ELF_BASE_PLATFORM NULL
169*4882a593Smuzhiyun #endif
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun static int
create_elf_tables(struct linux_binprm * bprm,const struct elfhdr * exec,unsigned long interp_load_addr,unsigned long e_entry,unsigned long phdr_addr)172*4882a593Smuzhiyun create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
173*4882a593Smuzhiyun 		unsigned long interp_load_addr,
174*4882a593Smuzhiyun 		unsigned long e_entry, unsigned long phdr_addr)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct mm_struct *mm = current->mm;
177*4882a593Smuzhiyun 	unsigned long p = bprm->p;
178*4882a593Smuzhiyun 	int argc = bprm->argc;
179*4882a593Smuzhiyun 	int envc = bprm->envc;
180*4882a593Smuzhiyun 	elf_addr_t __user *sp;
181*4882a593Smuzhiyun 	elf_addr_t __user *u_platform;
182*4882a593Smuzhiyun 	elf_addr_t __user *u_base_platform;
183*4882a593Smuzhiyun 	elf_addr_t __user *u_rand_bytes;
184*4882a593Smuzhiyun 	const char *k_platform = ELF_PLATFORM;
185*4882a593Smuzhiyun 	const char *k_base_platform = ELF_BASE_PLATFORM;
186*4882a593Smuzhiyun 	unsigned char k_rand_bytes[16];
187*4882a593Smuzhiyun 	int items;
188*4882a593Smuzhiyun 	elf_addr_t *elf_info;
189*4882a593Smuzhiyun 	int ei_index;
190*4882a593Smuzhiyun 	const struct cred *cred = current_cred();
191*4882a593Smuzhiyun 	struct vm_area_struct *vma;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/*
194*4882a593Smuzhiyun 	 * In some cases (e.g. Hyper-Threading), we want to avoid L1
195*4882a593Smuzhiyun 	 * evictions by the processes running on the same package. One
196*4882a593Smuzhiyun 	 * thing we can do is to shuffle the initial stack for them.
197*4882a593Smuzhiyun 	 */
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	p = arch_align_stack(p);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	/*
202*4882a593Smuzhiyun 	 * If this architecture has a platform capability string, copy it
203*4882a593Smuzhiyun 	 * to userspace.  In some cases (Sparc), this info is impossible
204*4882a593Smuzhiyun 	 * for userspace to get any other way, in others (i386) it is
205*4882a593Smuzhiyun 	 * merely difficult.
206*4882a593Smuzhiyun 	 */
207*4882a593Smuzhiyun 	u_platform = NULL;
208*4882a593Smuzhiyun 	if (k_platform) {
209*4882a593Smuzhiyun 		size_t len = strlen(k_platform) + 1;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 		u_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
212*4882a593Smuzhiyun 		if (copy_to_user(u_platform, k_platform, len))
213*4882a593Smuzhiyun 			return -EFAULT;
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	/*
217*4882a593Smuzhiyun 	 * If this architecture has a "base" platform capability
218*4882a593Smuzhiyun 	 * string, copy it to userspace.
219*4882a593Smuzhiyun 	 */
220*4882a593Smuzhiyun 	u_base_platform = NULL;
221*4882a593Smuzhiyun 	if (k_base_platform) {
222*4882a593Smuzhiyun 		size_t len = strlen(k_base_platform) + 1;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		u_base_platform = (elf_addr_t __user *)STACK_ALLOC(p, len);
225*4882a593Smuzhiyun 		if (copy_to_user(u_base_platform, k_base_platform, len))
226*4882a593Smuzhiyun 			return -EFAULT;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	/*
230*4882a593Smuzhiyun 	 * Generate 16 random bytes for userspace PRNG seeding.
231*4882a593Smuzhiyun 	 */
232*4882a593Smuzhiyun 	get_random_bytes(k_rand_bytes, sizeof(k_rand_bytes));
233*4882a593Smuzhiyun 	u_rand_bytes = (elf_addr_t __user *)
234*4882a593Smuzhiyun 		       STACK_ALLOC(p, sizeof(k_rand_bytes));
235*4882a593Smuzhiyun 	if (copy_to_user(u_rand_bytes, k_rand_bytes, sizeof(k_rand_bytes)))
236*4882a593Smuzhiyun 		return -EFAULT;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	/* Create the ELF interpreter info */
239*4882a593Smuzhiyun 	elf_info = (elf_addr_t *)mm->saved_auxv;
240*4882a593Smuzhiyun 	/* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
241*4882a593Smuzhiyun #define NEW_AUX_ENT(id, val) \
242*4882a593Smuzhiyun 	do { \
243*4882a593Smuzhiyun 		*elf_info++ = id; \
244*4882a593Smuzhiyun 		*elf_info++ = val; \
245*4882a593Smuzhiyun 	} while (0)
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun #ifdef ARCH_DLINFO
248*4882a593Smuzhiyun 	/*
249*4882a593Smuzhiyun 	 * ARCH_DLINFO must come first so PPC can do its special alignment of
250*4882a593Smuzhiyun 	 * AUXV.
251*4882a593Smuzhiyun 	 * update AT_VECTOR_SIZE_ARCH if the number of NEW_AUX_ENT() in
252*4882a593Smuzhiyun 	 * ARCH_DLINFO changes
253*4882a593Smuzhiyun 	 */
254*4882a593Smuzhiyun 	ARCH_DLINFO;
255*4882a593Smuzhiyun #endif
256*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
257*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
258*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
259*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_PHDR, phdr_addr);
260*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr));
261*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
262*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_BASE, interp_load_addr);
263*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_FLAGS, 0);
264*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_ENTRY, e_entry);
265*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_UID, from_kuid_munged(cred->user_ns, cred->uid));
266*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_EUID, from_kuid_munged(cred->user_ns, cred->euid));
267*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_GID, from_kgid_munged(cred->user_ns, cred->gid));
268*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_EGID, from_kgid_munged(cred->user_ns, cred->egid));
269*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_SECURE, bprm->secureexec);
270*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_RANDOM, (elf_addr_t)(unsigned long)u_rand_bytes);
271*4882a593Smuzhiyun #ifdef ELF_HWCAP2
272*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_HWCAP2, ELF_HWCAP2);
273*4882a593Smuzhiyun #endif
274*4882a593Smuzhiyun 	NEW_AUX_ENT(AT_EXECFN, bprm->exec);
275*4882a593Smuzhiyun 	if (k_platform) {
276*4882a593Smuzhiyun 		NEW_AUX_ENT(AT_PLATFORM,
277*4882a593Smuzhiyun 			    (elf_addr_t)(unsigned long)u_platform);
278*4882a593Smuzhiyun 	}
279*4882a593Smuzhiyun 	if (k_base_platform) {
280*4882a593Smuzhiyun 		NEW_AUX_ENT(AT_BASE_PLATFORM,
281*4882a593Smuzhiyun 			    (elf_addr_t)(unsigned long)u_base_platform);
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 	if (bprm->have_execfd) {
284*4882a593Smuzhiyun 		NEW_AUX_ENT(AT_EXECFD, bprm->execfd);
285*4882a593Smuzhiyun 	}
286*4882a593Smuzhiyun #undef NEW_AUX_ENT
287*4882a593Smuzhiyun 	/* AT_NULL is zero; clear the rest too */
288*4882a593Smuzhiyun 	memset(elf_info, 0, (char *)mm->saved_auxv +
289*4882a593Smuzhiyun 			sizeof(mm->saved_auxv) - (char *)elf_info);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	/* And advance past the AT_NULL entry.  */
292*4882a593Smuzhiyun 	elf_info += 2;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	ei_index = elf_info - (elf_addr_t *)mm->saved_auxv;
295*4882a593Smuzhiyun 	sp = STACK_ADD(p, ei_index);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	items = (argc + 1) + (envc + 1) + 1;
298*4882a593Smuzhiyun 	bprm->p = STACK_ROUND(sp, items);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* Point sp at the lowest address on the stack */
301*4882a593Smuzhiyun #ifdef CONFIG_STACK_GROWSUP
302*4882a593Smuzhiyun 	sp = (elf_addr_t __user *)bprm->p - items - ei_index;
303*4882a593Smuzhiyun 	bprm->exec = (unsigned long)sp; /* XXX: PARISC HACK */
304*4882a593Smuzhiyun #else
305*4882a593Smuzhiyun 	sp = (elf_addr_t __user *)bprm->p;
306*4882a593Smuzhiyun #endif
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	/*
310*4882a593Smuzhiyun 	 * Grow the stack manually; some architectures have a limit on how
311*4882a593Smuzhiyun 	 * far ahead a user-space access may be in order to grow the stack.
312*4882a593Smuzhiyun 	 */
313*4882a593Smuzhiyun 	if (mmap_read_lock_killable(mm))
314*4882a593Smuzhiyun 		return -EINTR;
315*4882a593Smuzhiyun 	vma = find_extend_vma(mm, bprm->p);
316*4882a593Smuzhiyun 	mmap_read_unlock(mm);
317*4882a593Smuzhiyun 	if (!vma)
318*4882a593Smuzhiyun 		return -EFAULT;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/* Now, let's put argc (and argv, envp if appropriate) on the stack */
321*4882a593Smuzhiyun 	if (put_user(argc, sp++))
322*4882a593Smuzhiyun 		return -EFAULT;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/* Populate list of argv pointers back to argv strings. */
325*4882a593Smuzhiyun 	p = mm->arg_end = mm->arg_start;
326*4882a593Smuzhiyun 	while (argc-- > 0) {
327*4882a593Smuzhiyun 		size_t len;
328*4882a593Smuzhiyun 		if (put_user((elf_addr_t)p, sp++))
329*4882a593Smuzhiyun 			return -EFAULT;
330*4882a593Smuzhiyun 		len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
331*4882a593Smuzhiyun 		if (!len || len > MAX_ARG_STRLEN)
332*4882a593Smuzhiyun 			return -EINVAL;
333*4882a593Smuzhiyun 		p += len;
334*4882a593Smuzhiyun 	}
335*4882a593Smuzhiyun 	if (put_user(0, sp++))
336*4882a593Smuzhiyun 		return -EFAULT;
337*4882a593Smuzhiyun 	mm->arg_end = p;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	/* Populate list of envp pointers back to envp strings. */
340*4882a593Smuzhiyun 	mm->env_end = mm->env_start = p;
341*4882a593Smuzhiyun 	while (envc-- > 0) {
342*4882a593Smuzhiyun 		size_t len;
343*4882a593Smuzhiyun 		if (put_user((elf_addr_t)p, sp++))
344*4882a593Smuzhiyun 			return -EFAULT;
345*4882a593Smuzhiyun 		len = strnlen_user((void __user *)p, MAX_ARG_STRLEN);
346*4882a593Smuzhiyun 		if (!len || len > MAX_ARG_STRLEN)
347*4882a593Smuzhiyun 			return -EINVAL;
348*4882a593Smuzhiyun 		p += len;
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 	if (put_user(0, sp++))
351*4882a593Smuzhiyun 		return -EFAULT;
352*4882a593Smuzhiyun 	mm->env_end = p;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/* Put the elf_info on the stack in the right place.  */
355*4882a593Smuzhiyun 	if (copy_to_user(sp, mm->saved_auxv, ei_index * sizeof(elf_addr_t)))
356*4882a593Smuzhiyun 		return -EFAULT;
357*4882a593Smuzhiyun 	return 0;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
elf_map(struct file * filep,unsigned long addr,const struct elf_phdr * eppnt,int prot,int type,unsigned long total_size)360*4882a593Smuzhiyun static unsigned long elf_map(struct file *filep, unsigned long addr,
361*4882a593Smuzhiyun 		const struct elf_phdr *eppnt, int prot, int type,
362*4882a593Smuzhiyun 		unsigned long total_size)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	unsigned long map_addr;
365*4882a593Smuzhiyun 	unsigned long size = eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr);
366*4882a593Smuzhiyun 	unsigned long off = eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr);
367*4882a593Smuzhiyun 	addr = ELF_PAGESTART(addr);
368*4882a593Smuzhiyun 	size = ELF_PAGEALIGN(size);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	/* mmap() will return -EINVAL if given a zero size, but a
371*4882a593Smuzhiyun 	 * segment with zero filesize is perfectly valid */
372*4882a593Smuzhiyun 	if (!size)
373*4882a593Smuzhiyun 		return addr;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	/*
376*4882a593Smuzhiyun 	* total_size is the size of the ELF (interpreter) image.
377*4882a593Smuzhiyun 	* The _first_ mmap needs to know the full size, otherwise
378*4882a593Smuzhiyun 	* randomization might put this image into an overlapping
379*4882a593Smuzhiyun 	* position with the ELF binary image. (since size < total_size)
380*4882a593Smuzhiyun 	* So we first map the 'big' image - and unmap the remainder at
381*4882a593Smuzhiyun 	* the end. (which unmap is needed for ELF images with holes.)
382*4882a593Smuzhiyun 	*/
383*4882a593Smuzhiyun 	if (total_size) {
384*4882a593Smuzhiyun 		total_size = ELF_PAGEALIGN(total_size);
385*4882a593Smuzhiyun 		map_addr = vm_mmap(filep, addr, total_size, prot, type, off);
386*4882a593Smuzhiyun 		if (!BAD_ADDR(map_addr))
387*4882a593Smuzhiyun 			vm_munmap(map_addr+size, total_size-size);
388*4882a593Smuzhiyun 	} else
389*4882a593Smuzhiyun 		map_addr = vm_mmap(filep, addr, size, prot, type, off);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	if ((type & MAP_FIXED_NOREPLACE) &&
392*4882a593Smuzhiyun 	    PTR_ERR((void *)map_addr) == -EEXIST)
393*4882a593Smuzhiyun 		pr_info("%d (%s): Uhuuh, elf segment at %px requested but the memory is mapped already\n",
394*4882a593Smuzhiyun 			task_pid_nr(current), current->comm, (void *)addr);
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	return(map_addr);
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
total_mapping_size(const struct elf_phdr * cmds,int nr)399*4882a593Smuzhiyun static unsigned long total_mapping_size(const struct elf_phdr *cmds, int nr)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	int i, first_idx = -1, last_idx = -1;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	for (i = 0; i < nr; i++) {
404*4882a593Smuzhiyun 		if (cmds[i].p_type == PT_LOAD) {
405*4882a593Smuzhiyun 			last_idx = i;
406*4882a593Smuzhiyun 			if (first_idx == -1)
407*4882a593Smuzhiyun 				first_idx = i;
408*4882a593Smuzhiyun 		}
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 	if (first_idx == -1)
411*4882a593Smuzhiyun 		return 0;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz -
414*4882a593Smuzhiyun 				ELF_PAGESTART(cmds[first_idx].p_vaddr);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
elf_read(struct file * file,void * buf,size_t len,loff_t pos)417*4882a593Smuzhiyun static int elf_read(struct file *file, void *buf, size_t len, loff_t pos)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	ssize_t rv;
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	rv = kernel_read(file, buf, len, &pos);
422*4882a593Smuzhiyun 	if (unlikely(rv != len)) {
423*4882a593Smuzhiyun 		return (rv < 0) ? rv : -EIO;
424*4882a593Smuzhiyun 	}
425*4882a593Smuzhiyun 	return 0;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
maximum_alignment(struct elf_phdr * cmds,int nr)428*4882a593Smuzhiyun static unsigned long maximum_alignment(struct elf_phdr *cmds, int nr)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	unsigned long alignment = 0;
431*4882a593Smuzhiyun 	int i;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	for (i = 0; i < nr; i++) {
434*4882a593Smuzhiyun 		if (cmds[i].p_type == PT_LOAD) {
435*4882a593Smuzhiyun 			unsigned long p_align = cmds[i].p_align;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 			/* skip non-power of two alignments as invalid */
438*4882a593Smuzhiyun 			if (!is_power_of_2(p_align))
439*4882a593Smuzhiyun 				continue;
440*4882a593Smuzhiyun 			alignment = max(alignment, p_align);
441*4882a593Smuzhiyun 		}
442*4882a593Smuzhiyun 	}
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	/* ensure we align to at least one page */
445*4882a593Smuzhiyun 	return ELF_PAGEALIGN(alignment);
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun /**
449*4882a593Smuzhiyun  * load_elf_phdrs() - load ELF program headers
450*4882a593Smuzhiyun  * @elf_ex:   ELF header of the binary whose program headers should be loaded
451*4882a593Smuzhiyun  * @elf_file: the opened ELF binary file
452*4882a593Smuzhiyun  *
453*4882a593Smuzhiyun  * Loads ELF program headers from the binary file elf_file, which has the ELF
454*4882a593Smuzhiyun  * header pointed to by elf_ex, into a newly allocated array. The caller is
455*4882a593Smuzhiyun  * responsible for freeing the allocated data. Returns an ERR_PTR upon failure.
456*4882a593Smuzhiyun  */
load_elf_phdrs(const struct elfhdr * elf_ex,struct file * elf_file)457*4882a593Smuzhiyun static struct elf_phdr *load_elf_phdrs(const struct elfhdr *elf_ex,
458*4882a593Smuzhiyun 				       struct file *elf_file)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	struct elf_phdr *elf_phdata = NULL;
461*4882a593Smuzhiyun 	int retval, err = -1;
462*4882a593Smuzhiyun 	unsigned int size;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	/*
465*4882a593Smuzhiyun 	 * If the size of this structure has changed, then punt, since
466*4882a593Smuzhiyun 	 * we will be doing the wrong thing.
467*4882a593Smuzhiyun 	 */
468*4882a593Smuzhiyun 	if (elf_ex->e_phentsize != sizeof(struct elf_phdr))
469*4882a593Smuzhiyun 		goto out;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	/* Sanity check the number of program headers... */
472*4882a593Smuzhiyun 	/* ...and their total size. */
473*4882a593Smuzhiyun 	size = sizeof(struct elf_phdr) * elf_ex->e_phnum;
474*4882a593Smuzhiyun 	if (size == 0 || size > 65536 || size > ELF_MIN_ALIGN)
475*4882a593Smuzhiyun 		goto out;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	elf_phdata = kmalloc(size, GFP_KERNEL);
478*4882a593Smuzhiyun 	if (!elf_phdata)
479*4882a593Smuzhiyun 		goto out;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	/* Read in the program headers */
482*4882a593Smuzhiyun 	retval = elf_read(elf_file, elf_phdata, size, elf_ex->e_phoff);
483*4882a593Smuzhiyun 	if (retval < 0) {
484*4882a593Smuzhiyun 		err = retval;
485*4882a593Smuzhiyun 		goto out;
486*4882a593Smuzhiyun 	}
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	/* Success! */
489*4882a593Smuzhiyun 	err = 0;
490*4882a593Smuzhiyun out:
491*4882a593Smuzhiyun 	if (err) {
492*4882a593Smuzhiyun 		kfree(elf_phdata);
493*4882a593Smuzhiyun 		elf_phdata = NULL;
494*4882a593Smuzhiyun 	}
495*4882a593Smuzhiyun 	return elf_phdata;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun #ifndef CONFIG_ARCH_BINFMT_ELF_STATE
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /**
501*4882a593Smuzhiyun  * struct arch_elf_state - arch-specific ELF loading state
502*4882a593Smuzhiyun  *
503*4882a593Smuzhiyun  * This structure is used to preserve architecture specific data during
504*4882a593Smuzhiyun  * the loading of an ELF file, throughout the checking of architecture
505*4882a593Smuzhiyun  * specific ELF headers & through to the point where the ELF load is
506*4882a593Smuzhiyun  * known to be proceeding (ie. SET_PERSONALITY).
507*4882a593Smuzhiyun  *
508*4882a593Smuzhiyun  * This implementation is a dummy for architectures which require no
509*4882a593Smuzhiyun  * specific state.
510*4882a593Smuzhiyun  */
511*4882a593Smuzhiyun struct arch_elf_state {
512*4882a593Smuzhiyun };
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun #define INIT_ARCH_ELF_STATE {}
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun /**
517*4882a593Smuzhiyun  * arch_elf_pt_proc() - check a PT_LOPROC..PT_HIPROC ELF program header
518*4882a593Smuzhiyun  * @ehdr:	The main ELF header
519*4882a593Smuzhiyun  * @phdr:	The program header to check
520*4882a593Smuzhiyun  * @elf:	The open ELF file
521*4882a593Smuzhiyun  * @is_interp:	True if the phdr is from the interpreter of the ELF being
522*4882a593Smuzhiyun  *		loaded, else false.
523*4882a593Smuzhiyun  * @state:	Architecture-specific state preserved throughout the process
524*4882a593Smuzhiyun  *		of loading the ELF.
525*4882a593Smuzhiyun  *
526*4882a593Smuzhiyun  * Inspects the program header phdr to validate its correctness and/or
527*4882a593Smuzhiyun  * suitability for the system. Called once per ELF program header in the
528*4882a593Smuzhiyun  * range PT_LOPROC to PT_HIPROC, for both the ELF being loaded and its
529*4882a593Smuzhiyun  * interpreter.
530*4882a593Smuzhiyun  *
531*4882a593Smuzhiyun  * Return: Zero to proceed with the ELF load, non-zero to fail the ELF load
532*4882a593Smuzhiyun  *         with that return code.
533*4882a593Smuzhiyun  */
arch_elf_pt_proc(struct elfhdr * ehdr,struct elf_phdr * phdr,struct file * elf,bool is_interp,struct arch_elf_state * state)534*4882a593Smuzhiyun static inline int arch_elf_pt_proc(struct elfhdr *ehdr,
535*4882a593Smuzhiyun 				   struct elf_phdr *phdr,
536*4882a593Smuzhiyun 				   struct file *elf, bool is_interp,
537*4882a593Smuzhiyun 				   struct arch_elf_state *state)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	/* Dummy implementation, always proceed */
540*4882a593Smuzhiyun 	return 0;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /**
544*4882a593Smuzhiyun  * arch_check_elf() - check an ELF executable
545*4882a593Smuzhiyun  * @ehdr:	The main ELF header
546*4882a593Smuzhiyun  * @has_interp:	True if the ELF has an interpreter, else false.
547*4882a593Smuzhiyun  * @interp_ehdr: The interpreter's ELF header
548*4882a593Smuzhiyun  * @state:	Architecture-specific state preserved throughout the process
549*4882a593Smuzhiyun  *		of loading the ELF.
550*4882a593Smuzhiyun  *
551*4882a593Smuzhiyun  * Provides a final opportunity for architecture code to reject the loading
552*4882a593Smuzhiyun  * of the ELF & cause an exec syscall to return an error. This is called after
553*4882a593Smuzhiyun  * all program headers to be checked by arch_elf_pt_proc have been.
554*4882a593Smuzhiyun  *
555*4882a593Smuzhiyun  * Return: Zero to proceed with the ELF load, non-zero to fail the ELF load
556*4882a593Smuzhiyun  *         with that return code.
557*4882a593Smuzhiyun  */
arch_check_elf(struct elfhdr * ehdr,bool has_interp,struct elfhdr * interp_ehdr,struct arch_elf_state * state)558*4882a593Smuzhiyun static inline int arch_check_elf(struct elfhdr *ehdr, bool has_interp,
559*4882a593Smuzhiyun 				 struct elfhdr *interp_ehdr,
560*4882a593Smuzhiyun 				 struct arch_elf_state *state)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	/* Dummy implementation, always proceed */
563*4882a593Smuzhiyun 	return 0;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun #endif /* !CONFIG_ARCH_BINFMT_ELF_STATE */
567*4882a593Smuzhiyun 
make_prot(u32 p_flags,struct arch_elf_state * arch_state,bool has_interp,bool is_interp)568*4882a593Smuzhiyun static inline int make_prot(u32 p_flags, struct arch_elf_state *arch_state,
569*4882a593Smuzhiyun 			    bool has_interp, bool is_interp)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	int prot = 0;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	if (p_flags & PF_R)
574*4882a593Smuzhiyun 		prot |= PROT_READ;
575*4882a593Smuzhiyun 	if (p_flags & PF_W)
576*4882a593Smuzhiyun 		prot |= PROT_WRITE;
577*4882a593Smuzhiyun 	if (p_flags & PF_X)
578*4882a593Smuzhiyun 		prot |= PROT_EXEC;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	return arch_elf_adjust_prot(prot, arch_state, has_interp, is_interp);
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /* This is much more generalized than the library routine read function,
584*4882a593Smuzhiyun    so we keep this separate.  Technically the library read function
585*4882a593Smuzhiyun    is only provided so that we can read a.out libraries that have
586*4882a593Smuzhiyun    an ELF header */
587*4882a593Smuzhiyun 
load_elf_interp(struct elfhdr * interp_elf_ex,struct file * interpreter,unsigned long no_base,struct elf_phdr * interp_elf_phdata,struct arch_elf_state * arch_state)588*4882a593Smuzhiyun static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
589*4882a593Smuzhiyun 		struct file *interpreter,
590*4882a593Smuzhiyun 		unsigned long no_base, struct elf_phdr *interp_elf_phdata,
591*4882a593Smuzhiyun 		struct arch_elf_state *arch_state)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun 	struct elf_phdr *eppnt;
594*4882a593Smuzhiyun 	unsigned long load_addr = 0;
595*4882a593Smuzhiyun 	int load_addr_set = 0;
596*4882a593Smuzhiyun 	unsigned long last_bss = 0, elf_bss = 0;
597*4882a593Smuzhiyun 	int bss_prot = 0;
598*4882a593Smuzhiyun 	unsigned long error = ~0UL;
599*4882a593Smuzhiyun 	unsigned long total_size;
600*4882a593Smuzhiyun 	int i;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	/* First of all, some simple consistency checks */
603*4882a593Smuzhiyun 	if (interp_elf_ex->e_type != ET_EXEC &&
604*4882a593Smuzhiyun 	    interp_elf_ex->e_type != ET_DYN)
605*4882a593Smuzhiyun 		goto out;
606*4882a593Smuzhiyun 	if (!elf_check_arch(interp_elf_ex) ||
607*4882a593Smuzhiyun 	    elf_check_fdpic(interp_elf_ex))
608*4882a593Smuzhiyun 		goto out;
609*4882a593Smuzhiyun 	if (!interpreter->f_op->mmap)
610*4882a593Smuzhiyun 		goto out;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	total_size = total_mapping_size(interp_elf_phdata,
613*4882a593Smuzhiyun 					interp_elf_ex->e_phnum);
614*4882a593Smuzhiyun 	if (!total_size) {
615*4882a593Smuzhiyun 		error = -EINVAL;
616*4882a593Smuzhiyun 		goto out;
617*4882a593Smuzhiyun 	}
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	eppnt = interp_elf_phdata;
620*4882a593Smuzhiyun 	for (i = 0; i < interp_elf_ex->e_phnum; i++, eppnt++) {
621*4882a593Smuzhiyun 		if (eppnt->p_type == PT_LOAD) {
622*4882a593Smuzhiyun 			int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
623*4882a593Smuzhiyun 			int elf_prot = make_prot(eppnt->p_flags, arch_state,
624*4882a593Smuzhiyun 						 true, true);
625*4882a593Smuzhiyun 			unsigned long vaddr = 0;
626*4882a593Smuzhiyun 			unsigned long k, map_addr;
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 			vaddr = eppnt->p_vaddr;
629*4882a593Smuzhiyun 			if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
630*4882a593Smuzhiyun 				elf_type |= MAP_FIXED;
631*4882a593Smuzhiyun 			else if (no_base && interp_elf_ex->e_type == ET_DYN)
632*4882a593Smuzhiyun 				load_addr = -vaddr;
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 			map_addr = elf_map(interpreter, load_addr + vaddr,
635*4882a593Smuzhiyun 					eppnt, elf_prot, elf_type, total_size);
636*4882a593Smuzhiyun 			total_size = 0;
637*4882a593Smuzhiyun 			error = map_addr;
638*4882a593Smuzhiyun 			if (BAD_ADDR(map_addr))
639*4882a593Smuzhiyun 				goto out;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 			if (!load_addr_set &&
642*4882a593Smuzhiyun 			    interp_elf_ex->e_type == ET_DYN) {
643*4882a593Smuzhiyun 				load_addr = map_addr - ELF_PAGESTART(vaddr);
644*4882a593Smuzhiyun 				load_addr_set = 1;
645*4882a593Smuzhiyun 			}
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 			/*
648*4882a593Smuzhiyun 			 * Check to see if the section's size will overflow the
649*4882a593Smuzhiyun 			 * allowed task size. Note that p_filesz must always be
650*4882a593Smuzhiyun 			 * <= p_memsize so it's only necessary to check p_memsz.
651*4882a593Smuzhiyun 			 */
652*4882a593Smuzhiyun 			k = load_addr + eppnt->p_vaddr;
653*4882a593Smuzhiyun 			if (BAD_ADDR(k) ||
654*4882a593Smuzhiyun 			    eppnt->p_filesz > eppnt->p_memsz ||
655*4882a593Smuzhiyun 			    eppnt->p_memsz > TASK_SIZE ||
656*4882a593Smuzhiyun 			    TASK_SIZE - eppnt->p_memsz < k) {
657*4882a593Smuzhiyun 				error = -ENOMEM;
658*4882a593Smuzhiyun 				goto out;
659*4882a593Smuzhiyun 			}
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 			/*
662*4882a593Smuzhiyun 			 * Find the end of the file mapping for this phdr, and
663*4882a593Smuzhiyun 			 * keep track of the largest address we see for this.
664*4882a593Smuzhiyun 			 */
665*4882a593Smuzhiyun 			k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
666*4882a593Smuzhiyun 			if (k > elf_bss)
667*4882a593Smuzhiyun 				elf_bss = k;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 			/*
670*4882a593Smuzhiyun 			 * Do the same thing for the memory mapping - between
671*4882a593Smuzhiyun 			 * elf_bss and last_bss is the bss section.
672*4882a593Smuzhiyun 			 */
673*4882a593Smuzhiyun 			k = load_addr + eppnt->p_vaddr + eppnt->p_memsz;
674*4882a593Smuzhiyun 			if (k > last_bss) {
675*4882a593Smuzhiyun 				last_bss = k;
676*4882a593Smuzhiyun 				bss_prot = elf_prot;
677*4882a593Smuzhiyun 			}
678*4882a593Smuzhiyun 		}
679*4882a593Smuzhiyun 	}
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 	/*
682*4882a593Smuzhiyun 	 * Now fill out the bss section: first pad the last page from
683*4882a593Smuzhiyun 	 * the file up to the page boundary, and zero it from elf_bss
684*4882a593Smuzhiyun 	 * up to the end of the page.
685*4882a593Smuzhiyun 	 */
686*4882a593Smuzhiyun 	if (padzero(elf_bss)) {
687*4882a593Smuzhiyun 		error = -EFAULT;
688*4882a593Smuzhiyun 		goto out;
689*4882a593Smuzhiyun 	}
690*4882a593Smuzhiyun 	/*
691*4882a593Smuzhiyun 	 * Next, align both the file and mem bss up to the page size,
692*4882a593Smuzhiyun 	 * since this is where elf_bss was just zeroed up to, and where
693*4882a593Smuzhiyun 	 * last_bss will end after the vm_brk_flags() below.
694*4882a593Smuzhiyun 	 */
695*4882a593Smuzhiyun 	elf_bss = ELF_PAGEALIGN(elf_bss);
696*4882a593Smuzhiyun 	last_bss = ELF_PAGEALIGN(last_bss);
697*4882a593Smuzhiyun 	/* Finally, if there is still more bss to allocate, do it. */
698*4882a593Smuzhiyun 	if (last_bss > elf_bss) {
699*4882a593Smuzhiyun 		error = vm_brk_flags(elf_bss, last_bss - elf_bss,
700*4882a593Smuzhiyun 				bss_prot & PROT_EXEC ? VM_EXEC : 0);
701*4882a593Smuzhiyun 		if (error)
702*4882a593Smuzhiyun 			goto out;
703*4882a593Smuzhiyun 	}
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	error = load_addr;
706*4882a593Smuzhiyun out:
707*4882a593Smuzhiyun 	return error;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun  * These are the functions used to load ELF style executables and shared
712*4882a593Smuzhiyun  * libraries.  There is no binary dependent code anywhere else.
713*4882a593Smuzhiyun  */
714*4882a593Smuzhiyun 
parse_elf_property(const char * data,size_t * off,size_t datasz,struct arch_elf_state * arch,bool have_prev_type,u32 * prev_type)715*4882a593Smuzhiyun static int parse_elf_property(const char *data, size_t *off, size_t datasz,
716*4882a593Smuzhiyun 			      struct arch_elf_state *arch,
717*4882a593Smuzhiyun 			      bool have_prev_type, u32 *prev_type)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	size_t o, step;
720*4882a593Smuzhiyun 	const struct gnu_property *pr;
721*4882a593Smuzhiyun 	int ret;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	if (*off == datasz)
724*4882a593Smuzhiyun 		return -ENOENT;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	if (WARN_ON_ONCE(*off > datasz || *off % ELF_GNU_PROPERTY_ALIGN))
727*4882a593Smuzhiyun 		return -EIO;
728*4882a593Smuzhiyun 	o = *off;
729*4882a593Smuzhiyun 	datasz -= *off;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if (datasz < sizeof(*pr))
732*4882a593Smuzhiyun 		return -ENOEXEC;
733*4882a593Smuzhiyun 	pr = (const struct gnu_property *)(data + o);
734*4882a593Smuzhiyun 	o += sizeof(*pr);
735*4882a593Smuzhiyun 	datasz -= sizeof(*pr);
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	if (pr->pr_datasz > datasz)
738*4882a593Smuzhiyun 		return -ENOEXEC;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	WARN_ON_ONCE(o % ELF_GNU_PROPERTY_ALIGN);
741*4882a593Smuzhiyun 	step = round_up(pr->pr_datasz, ELF_GNU_PROPERTY_ALIGN);
742*4882a593Smuzhiyun 	if (step > datasz)
743*4882a593Smuzhiyun 		return -ENOEXEC;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	/* Properties are supposed to be unique and sorted on pr_type: */
746*4882a593Smuzhiyun 	if (have_prev_type && pr->pr_type <= *prev_type)
747*4882a593Smuzhiyun 		return -ENOEXEC;
748*4882a593Smuzhiyun 	*prev_type = pr->pr_type;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	ret = arch_parse_elf_property(pr->pr_type, data + o,
751*4882a593Smuzhiyun 				      pr->pr_datasz, ELF_COMPAT, arch);
752*4882a593Smuzhiyun 	if (ret)
753*4882a593Smuzhiyun 		return ret;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	*off = o + step;
756*4882a593Smuzhiyun 	return 0;
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun #define NOTE_DATA_SZ SZ_1K
760*4882a593Smuzhiyun #define GNU_PROPERTY_TYPE_0_NAME "GNU"
761*4882a593Smuzhiyun #define NOTE_NAME_SZ (sizeof(GNU_PROPERTY_TYPE_0_NAME))
762*4882a593Smuzhiyun 
parse_elf_properties(struct file * f,const struct elf_phdr * phdr,struct arch_elf_state * arch)763*4882a593Smuzhiyun static int parse_elf_properties(struct file *f, const struct elf_phdr *phdr,
764*4882a593Smuzhiyun 				struct arch_elf_state *arch)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun 	union {
767*4882a593Smuzhiyun 		struct elf_note nhdr;
768*4882a593Smuzhiyun 		char data[NOTE_DATA_SZ];
769*4882a593Smuzhiyun 	} note;
770*4882a593Smuzhiyun 	loff_t pos;
771*4882a593Smuzhiyun 	ssize_t n;
772*4882a593Smuzhiyun 	size_t off, datasz;
773*4882a593Smuzhiyun 	int ret;
774*4882a593Smuzhiyun 	bool have_prev_type;
775*4882a593Smuzhiyun 	u32 prev_type;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_ARCH_USE_GNU_PROPERTY) || !phdr)
778*4882a593Smuzhiyun 		return 0;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	/* load_elf_binary() shouldn't call us unless this is true... */
781*4882a593Smuzhiyun 	if (WARN_ON_ONCE(phdr->p_type != PT_GNU_PROPERTY))
782*4882a593Smuzhiyun 		return -ENOEXEC;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	/* If the properties are crazy large, that's too bad (for now): */
785*4882a593Smuzhiyun 	if (phdr->p_filesz > sizeof(note))
786*4882a593Smuzhiyun 		return -ENOEXEC;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	pos = phdr->p_offset;
789*4882a593Smuzhiyun 	n = kernel_read(f, &note, phdr->p_filesz, &pos);
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(note) < sizeof(note.nhdr) + NOTE_NAME_SZ);
792*4882a593Smuzhiyun 	if (n < 0 || n < sizeof(note.nhdr) + NOTE_NAME_SZ)
793*4882a593Smuzhiyun 		return -EIO;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	if (note.nhdr.n_type != NT_GNU_PROPERTY_TYPE_0 ||
796*4882a593Smuzhiyun 	    note.nhdr.n_namesz != NOTE_NAME_SZ ||
797*4882a593Smuzhiyun 	    strncmp(note.data + sizeof(note.nhdr),
798*4882a593Smuzhiyun 		    GNU_PROPERTY_TYPE_0_NAME, n - sizeof(note.nhdr)))
799*4882a593Smuzhiyun 		return -ENOEXEC;
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	off = round_up(sizeof(note.nhdr) + NOTE_NAME_SZ,
802*4882a593Smuzhiyun 		       ELF_GNU_PROPERTY_ALIGN);
803*4882a593Smuzhiyun 	if (off > n)
804*4882a593Smuzhiyun 		return -ENOEXEC;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	if (note.nhdr.n_descsz > n - off)
807*4882a593Smuzhiyun 		return -ENOEXEC;
808*4882a593Smuzhiyun 	datasz = off + note.nhdr.n_descsz;
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 	have_prev_type = false;
811*4882a593Smuzhiyun 	do {
812*4882a593Smuzhiyun 		ret = parse_elf_property(note.data, &off, datasz, arch,
813*4882a593Smuzhiyun 					 have_prev_type, &prev_type);
814*4882a593Smuzhiyun 		have_prev_type = true;
815*4882a593Smuzhiyun 	} while (!ret);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	return ret == -ENOENT ? 0 : ret;
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun 
load_elf_binary(struct linux_binprm * bprm)820*4882a593Smuzhiyun static int load_elf_binary(struct linux_binprm *bprm)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun 	struct file *interpreter = NULL; /* to shut gcc up */
823*4882a593Smuzhiyun 	unsigned long load_addr, load_bias = 0, phdr_addr = 0;
824*4882a593Smuzhiyun 	int load_addr_set = 0;
825*4882a593Smuzhiyun 	unsigned long error;
826*4882a593Smuzhiyun 	struct elf_phdr *elf_ppnt, *elf_phdata, *interp_elf_phdata = NULL;
827*4882a593Smuzhiyun 	struct elf_phdr *elf_property_phdata = NULL;
828*4882a593Smuzhiyun 	unsigned long elf_bss, elf_brk;
829*4882a593Smuzhiyun 	int bss_prot = 0;
830*4882a593Smuzhiyun 	int retval, i;
831*4882a593Smuzhiyun 	unsigned long elf_entry;
832*4882a593Smuzhiyun 	unsigned long e_entry;
833*4882a593Smuzhiyun 	unsigned long interp_load_addr = 0;
834*4882a593Smuzhiyun 	unsigned long start_code, end_code, start_data, end_data;
835*4882a593Smuzhiyun 	unsigned long reloc_func_desc __maybe_unused = 0;
836*4882a593Smuzhiyun 	int executable_stack = EXSTACK_DEFAULT;
837*4882a593Smuzhiyun 	struct elfhdr *elf_ex = (struct elfhdr *)bprm->buf;
838*4882a593Smuzhiyun 	struct elfhdr *interp_elf_ex = NULL;
839*4882a593Smuzhiyun 	struct arch_elf_state arch_state = INIT_ARCH_ELF_STATE;
840*4882a593Smuzhiyun 	struct mm_struct *mm;
841*4882a593Smuzhiyun 	struct pt_regs *regs;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	retval = -ENOEXEC;
844*4882a593Smuzhiyun 	/* First of all, some simple consistency checks */
845*4882a593Smuzhiyun 	if (memcmp(elf_ex->e_ident, ELFMAG, SELFMAG) != 0)
846*4882a593Smuzhiyun 		goto out;
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	if (elf_ex->e_type != ET_EXEC && elf_ex->e_type != ET_DYN)
849*4882a593Smuzhiyun 		goto out;
850*4882a593Smuzhiyun 	if (!elf_check_arch(elf_ex))
851*4882a593Smuzhiyun 		goto out;
852*4882a593Smuzhiyun 	if (elf_check_fdpic(elf_ex))
853*4882a593Smuzhiyun 		goto out;
854*4882a593Smuzhiyun 	if (!bprm->file->f_op->mmap)
855*4882a593Smuzhiyun 		goto out;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	elf_phdata = load_elf_phdrs(elf_ex, bprm->file);
858*4882a593Smuzhiyun 	if (!elf_phdata)
859*4882a593Smuzhiyun 		goto out;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	elf_ppnt = elf_phdata;
862*4882a593Smuzhiyun 	for (i = 0; i < elf_ex->e_phnum; i++, elf_ppnt++) {
863*4882a593Smuzhiyun 		char *elf_interpreter;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 		if (elf_ppnt->p_type == PT_GNU_PROPERTY) {
866*4882a593Smuzhiyun 			elf_property_phdata = elf_ppnt;
867*4882a593Smuzhiyun 			continue;
868*4882a593Smuzhiyun 		}
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 		if (elf_ppnt->p_type != PT_INTERP)
871*4882a593Smuzhiyun 			continue;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 		/*
874*4882a593Smuzhiyun 		 * This is the program interpreter used for shared libraries -
875*4882a593Smuzhiyun 		 * for now assume that this is an a.out format binary.
876*4882a593Smuzhiyun 		 */
877*4882a593Smuzhiyun 		retval = -ENOEXEC;
878*4882a593Smuzhiyun 		if (elf_ppnt->p_filesz > PATH_MAX || elf_ppnt->p_filesz < 2)
879*4882a593Smuzhiyun 			goto out_free_ph;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 		retval = -ENOMEM;
882*4882a593Smuzhiyun 		elf_interpreter = kmalloc(elf_ppnt->p_filesz, GFP_KERNEL);
883*4882a593Smuzhiyun 		if (!elf_interpreter)
884*4882a593Smuzhiyun 			goto out_free_ph;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 		retval = elf_read(bprm->file, elf_interpreter, elf_ppnt->p_filesz,
887*4882a593Smuzhiyun 				  elf_ppnt->p_offset);
888*4882a593Smuzhiyun 		if (retval < 0)
889*4882a593Smuzhiyun 			goto out_free_interp;
890*4882a593Smuzhiyun 		/* make sure path is NULL terminated */
891*4882a593Smuzhiyun 		retval = -ENOEXEC;
892*4882a593Smuzhiyun 		if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
893*4882a593Smuzhiyun 			goto out_free_interp;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 		interpreter = open_exec(elf_interpreter);
896*4882a593Smuzhiyun 		kfree(elf_interpreter);
897*4882a593Smuzhiyun 		retval = PTR_ERR(interpreter);
898*4882a593Smuzhiyun 		if (IS_ERR(interpreter))
899*4882a593Smuzhiyun 			goto out_free_ph;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 		/*
902*4882a593Smuzhiyun 		 * If the binary is not readable then enforce mm->dumpable = 0
903*4882a593Smuzhiyun 		 * regardless of the interpreter's permissions.
904*4882a593Smuzhiyun 		 */
905*4882a593Smuzhiyun 		would_dump(bprm, interpreter);
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 		interp_elf_ex = kmalloc(sizeof(*interp_elf_ex), GFP_KERNEL);
908*4882a593Smuzhiyun 		if (!interp_elf_ex) {
909*4882a593Smuzhiyun 			retval = -ENOMEM;
910*4882a593Smuzhiyun 			goto out_free_file;
911*4882a593Smuzhiyun 		}
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 		/* Get the exec headers */
914*4882a593Smuzhiyun 		retval = elf_read(interpreter, interp_elf_ex,
915*4882a593Smuzhiyun 				  sizeof(*interp_elf_ex), 0);
916*4882a593Smuzhiyun 		if (retval < 0)
917*4882a593Smuzhiyun 			goto out_free_dentry;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 		break;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun out_free_interp:
922*4882a593Smuzhiyun 		kfree(elf_interpreter);
923*4882a593Smuzhiyun 		goto out_free_ph;
924*4882a593Smuzhiyun 	}
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	elf_ppnt = elf_phdata;
927*4882a593Smuzhiyun 	for (i = 0; i < elf_ex->e_phnum; i++, elf_ppnt++)
928*4882a593Smuzhiyun 		switch (elf_ppnt->p_type) {
929*4882a593Smuzhiyun 		case PT_GNU_STACK:
930*4882a593Smuzhiyun 			if (elf_ppnt->p_flags & PF_X)
931*4882a593Smuzhiyun 				executable_stack = EXSTACK_ENABLE_X;
932*4882a593Smuzhiyun 			else
933*4882a593Smuzhiyun 				executable_stack = EXSTACK_DISABLE_X;
934*4882a593Smuzhiyun 			break;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 		case PT_LOPROC ... PT_HIPROC:
937*4882a593Smuzhiyun 			retval = arch_elf_pt_proc(elf_ex, elf_ppnt,
938*4882a593Smuzhiyun 						  bprm->file, false,
939*4882a593Smuzhiyun 						  &arch_state);
940*4882a593Smuzhiyun 			if (retval)
941*4882a593Smuzhiyun 				goto out_free_dentry;
942*4882a593Smuzhiyun 			break;
943*4882a593Smuzhiyun 		}
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/* Some simple consistency checks for the interpreter */
946*4882a593Smuzhiyun 	if (interpreter) {
947*4882a593Smuzhiyun 		retval = -ELIBBAD;
948*4882a593Smuzhiyun 		/* Not an ELF interpreter */
949*4882a593Smuzhiyun 		if (memcmp(interp_elf_ex->e_ident, ELFMAG, SELFMAG) != 0)
950*4882a593Smuzhiyun 			goto out_free_dentry;
951*4882a593Smuzhiyun 		/* Verify the interpreter has a valid arch */
952*4882a593Smuzhiyun 		if (!elf_check_arch(interp_elf_ex) ||
953*4882a593Smuzhiyun 		    elf_check_fdpic(interp_elf_ex))
954*4882a593Smuzhiyun 			goto out_free_dentry;
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 		/* Load the interpreter program headers */
957*4882a593Smuzhiyun 		interp_elf_phdata = load_elf_phdrs(interp_elf_ex,
958*4882a593Smuzhiyun 						   interpreter);
959*4882a593Smuzhiyun 		if (!interp_elf_phdata)
960*4882a593Smuzhiyun 			goto out_free_dentry;
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 		/* Pass PT_LOPROC..PT_HIPROC headers to arch code */
963*4882a593Smuzhiyun 		elf_property_phdata = NULL;
964*4882a593Smuzhiyun 		elf_ppnt = interp_elf_phdata;
965*4882a593Smuzhiyun 		for (i = 0; i < interp_elf_ex->e_phnum; i++, elf_ppnt++)
966*4882a593Smuzhiyun 			switch (elf_ppnt->p_type) {
967*4882a593Smuzhiyun 			case PT_GNU_PROPERTY:
968*4882a593Smuzhiyun 				elf_property_phdata = elf_ppnt;
969*4882a593Smuzhiyun 				break;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 			case PT_LOPROC ... PT_HIPROC:
972*4882a593Smuzhiyun 				retval = arch_elf_pt_proc(interp_elf_ex,
973*4882a593Smuzhiyun 							  elf_ppnt, interpreter,
974*4882a593Smuzhiyun 							  true, &arch_state);
975*4882a593Smuzhiyun 				if (retval)
976*4882a593Smuzhiyun 					goto out_free_dentry;
977*4882a593Smuzhiyun 				break;
978*4882a593Smuzhiyun 			}
979*4882a593Smuzhiyun 	}
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	retval = parse_elf_properties(interpreter ?: bprm->file,
982*4882a593Smuzhiyun 				      elf_property_phdata, &arch_state);
983*4882a593Smuzhiyun 	if (retval)
984*4882a593Smuzhiyun 		goto out_free_dentry;
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun 	/*
987*4882a593Smuzhiyun 	 * Allow arch code to reject the ELF at this point, whilst it's
988*4882a593Smuzhiyun 	 * still possible to return an error to the code that invoked
989*4882a593Smuzhiyun 	 * the exec syscall.
990*4882a593Smuzhiyun 	 */
991*4882a593Smuzhiyun 	retval = arch_check_elf(elf_ex,
992*4882a593Smuzhiyun 				!!interpreter, interp_elf_ex,
993*4882a593Smuzhiyun 				&arch_state);
994*4882a593Smuzhiyun 	if (retval)
995*4882a593Smuzhiyun 		goto out_free_dentry;
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	/* Flush all traces of the currently running executable */
998*4882a593Smuzhiyun 	retval = begin_new_exec(bprm);
999*4882a593Smuzhiyun 	if (retval)
1000*4882a593Smuzhiyun 		goto out_free_dentry;
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	/* Do this immediately, since STACK_TOP as used in setup_arg_pages
1003*4882a593Smuzhiyun 	   may depend on the personality.  */
1004*4882a593Smuzhiyun 	SET_PERSONALITY2(*elf_ex, &arch_state);
1005*4882a593Smuzhiyun 	if (elf_read_implies_exec(*elf_ex, executable_stack))
1006*4882a593Smuzhiyun 		current->personality |= READ_IMPLIES_EXEC;
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
1009*4882a593Smuzhiyun 		current->flags |= PF_RANDOMIZE;
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	setup_new_exec(bprm);
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 	/* Do this so that we can load the interpreter, if need be.  We will
1014*4882a593Smuzhiyun 	   change some of these later */
1015*4882a593Smuzhiyun 	retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
1016*4882a593Smuzhiyun 				 executable_stack);
1017*4882a593Smuzhiyun 	if (retval < 0)
1018*4882a593Smuzhiyun 		goto out_free_dentry;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	elf_bss = 0;
1021*4882a593Smuzhiyun 	elf_brk = 0;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	start_code = ~0UL;
1024*4882a593Smuzhiyun 	end_code = 0;
1025*4882a593Smuzhiyun 	start_data = 0;
1026*4882a593Smuzhiyun 	end_data = 0;
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 	/* Now we do a little grungy work by mmapping the ELF image into
1029*4882a593Smuzhiyun 	   the correct location in memory. */
1030*4882a593Smuzhiyun 	for(i = 0, elf_ppnt = elf_phdata;
1031*4882a593Smuzhiyun 	    i < elf_ex->e_phnum; i++, elf_ppnt++) {
1032*4882a593Smuzhiyun 		int elf_prot, elf_flags;
1033*4882a593Smuzhiyun 		unsigned long k, vaddr;
1034*4882a593Smuzhiyun 		unsigned long total_size = 0;
1035*4882a593Smuzhiyun 		unsigned long alignment;
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 		if (elf_ppnt->p_type != PT_LOAD)
1038*4882a593Smuzhiyun 			continue;
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 		if (unlikely (elf_brk > elf_bss)) {
1041*4882a593Smuzhiyun 			unsigned long nbyte;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 			/* There was a PT_LOAD segment with p_memsz > p_filesz
1044*4882a593Smuzhiyun 			   before this one. Map anonymous pages, if needed,
1045*4882a593Smuzhiyun 			   and clear the area.  */
1046*4882a593Smuzhiyun 			retval = set_brk(elf_bss + load_bias,
1047*4882a593Smuzhiyun 					 elf_brk + load_bias,
1048*4882a593Smuzhiyun 					 bss_prot);
1049*4882a593Smuzhiyun 			if (retval)
1050*4882a593Smuzhiyun 				goto out_free_dentry;
1051*4882a593Smuzhiyun 			nbyte = ELF_PAGEOFFSET(elf_bss);
1052*4882a593Smuzhiyun 			if (nbyte) {
1053*4882a593Smuzhiyun 				nbyte = ELF_MIN_ALIGN - nbyte;
1054*4882a593Smuzhiyun 				if (nbyte > elf_brk - elf_bss)
1055*4882a593Smuzhiyun 					nbyte = elf_brk - elf_bss;
1056*4882a593Smuzhiyun 				if (clear_user((void __user *)elf_bss +
1057*4882a593Smuzhiyun 							load_bias, nbyte)) {
1058*4882a593Smuzhiyun 					/*
1059*4882a593Smuzhiyun 					 * This bss-zeroing can fail if the ELF
1060*4882a593Smuzhiyun 					 * file specifies odd protections. So
1061*4882a593Smuzhiyun 					 * we don't check the return value
1062*4882a593Smuzhiyun 					 */
1063*4882a593Smuzhiyun 				}
1064*4882a593Smuzhiyun 			}
1065*4882a593Smuzhiyun 		}
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 		elf_prot = make_prot(elf_ppnt->p_flags, &arch_state,
1068*4882a593Smuzhiyun 				     !!interpreter, false);
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 		elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 		vaddr = elf_ppnt->p_vaddr;
1073*4882a593Smuzhiyun 		/*
1074*4882a593Smuzhiyun 		 * If we are loading ET_EXEC or we have already performed
1075*4882a593Smuzhiyun 		 * the ET_DYN load_addr calculations, proceed normally.
1076*4882a593Smuzhiyun 		 */
1077*4882a593Smuzhiyun 		if (elf_ex->e_type == ET_EXEC || load_addr_set) {
1078*4882a593Smuzhiyun 			elf_flags |= MAP_FIXED;
1079*4882a593Smuzhiyun 		} else if (elf_ex->e_type == ET_DYN) {
1080*4882a593Smuzhiyun 			/*
1081*4882a593Smuzhiyun 			 * This logic is run once for the first LOAD Program
1082*4882a593Smuzhiyun 			 * Header for ET_DYN binaries to calculate the
1083*4882a593Smuzhiyun 			 * randomization (load_bias) for all the LOAD
1084*4882a593Smuzhiyun 			 * Program Headers, and to calculate the entire
1085*4882a593Smuzhiyun 			 * size of the ELF mapping (total_size). (Note that
1086*4882a593Smuzhiyun 			 * load_addr_set is set to true later once the
1087*4882a593Smuzhiyun 			 * initial mapping is performed.)
1088*4882a593Smuzhiyun 			 *
1089*4882a593Smuzhiyun 			 * There are effectively two types of ET_DYN
1090*4882a593Smuzhiyun 			 * binaries: programs (i.e. PIE: ET_DYN with INTERP)
1091*4882a593Smuzhiyun 			 * and loaders (ET_DYN without INTERP, since they
1092*4882a593Smuzhiyun 			 * _are_ the ELF interpreter). The loaders must
1093*4882a593Smuzhiyun 			 * be loaded away from programs since the program
1094*4882a593Smuzhiyun 			 * may otherwise collide with the loader (especially
1095*4882a593Smuzhiyun 			 * for ET_EXEC which does not have a randomized
1096*4882a593Smuzhiyun 			 * position). For example to handle invocations of
1097*4882a593Smuzhiyun 			 * "./ld.so someprog" to test out a new version of
1098*4882a593Smuzhiyun 			 * the loader, the subsequent program that the
1099*4882a593Smuzhiyun 			 * loader loads must avoid the loader itself, so
1100*4882a593Smuzhiyun 			 * they cannot share the same load range. Sufficient
1101*4882a593Smuzhiyun 			 * room for the brk must be allocated with the
1102*4882a593Smuzhiyun 			 * loader as well, since brk must be available with
1103*4882a593Smuzhiyun 			 * the loader.
1104*4882a593Smuzhiyun 			 *
1105*4882a593Smuzhiyun 			 * Therefore, programs are loaded offset from
1106*4882a593Smuzhiyun 			 * ELF_ET_DYN_BASE and loaders are loaded into the
1107*4882a593Smuzhiyun 			 * independently randomized mmap region (0 load_bias
1108*4882a593Smuzhiyun 			 * without MAP_FIXED).
1109*4882a593Smuzhiyun 			 */
1110*4882a593Smuzhiyun 			if (interpreter) {
1111*4882a593Smuzhiyun 				load_bias = ELF_ET_DYN_BASE;
1112*4882a593Smuzhiyun 				if (current->flags & PF_RANDOMIZE)
1113*4882a593Smuzhiyun 					load_bias += arch_mmap_rnd();
1114*4882a593Smuzhiyun 				alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum);
1115*4882a593Smuzhiyun 				if (alignment)
1116*4882a593Smuzhiyun 					load_bias &= ~(alignment - 1);
1117*4882a593Smuzhiyun 				elf_flags |= MAP_FIXED;
1118*4882a593Smuzhiyun 			} else
1119*4882a593Smuzhiyun 				load_bias = 0;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 			/*
1122*4882a593Smuzhiyun 			 * Since load_bias is used for all subsequent loading
1123*4882a593Smuzhiyun 			 * calculations, we must lower it by the first vaddr
1124*4882a593Smuzhiyun 			 * so that the remaining calculations based on the
1125*4882a593Smuzhiyun 			 * ELF vaddrs will be correctly offset. The result
1126*4882a593Smuzhiyun 			 * is then page aligned.
1127*4882a593Smuzhiyun 			 */
1128*4882a593Smuzhiyun 			load_bias = ELF_PAGESTART(load_bias - vaddr);
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 			total_size = total_mapping_size(elf_phdata,
1131*4882a593Smuzhiyun 							elf_ex->e_phnum);
1132*4882a593Smuzhiyun 			if (!total_size) {
1133*4882a593Smuzhiyun 				retval = -EINVAL;
1134*4882a593Smuzhiyun 				goto out_free_dentry;
1135*4882a593Smuzhiyun 			}
1136*4882a593Smuzhiyun 		}
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 		error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
1139*4882a593Smuzhiyun 				elf_prot, elf_flags, total_size);
1140*4882a593Smuzhiyun 		if (BAD_ADDR(error)) {
1141*4882a593Smuzhiyun 			retval = IS_ERR((void *)error) ?
1142*4882a593Smuzhiyun 				PTR_ERR((void*)error) : -EINVAL;
1143*4882a593Smuzhiyun 			goto out_free_dentry;
1144*4882a593Smuzhiyun 		}
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 		if (!load_addr_set) {
1147*4882a593Smuzhiyun 			load_addr_set = 1;
1148*4882a593Smuzhiyun 			load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
1149*4882a593Smuzhiyun 			if (elf_ex->e_type == ET_DYN) {
1150*4882a593Smuzhiyun 				load_bias += error -
1151*4882a593Smuzhiyun 				             ELF_PAGESTART(load_bias + vaddr);
1152*4882a593Smuzhiyun 				load_addr += load_bias;
1153*4882a593Smuzhiyun 				reloc_func_desc = load_bias;
1154*4882a593Smuzhiyun 			}
1155*4882a593Smuzhiyun 		}
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 		/*
1158*4882a593Smuzhiyun 		 * Figure out which segment in the file contains the Program
1159*4882a593Smuzhiyun 		 * Header table, and map to the associated memory address.
1160*4882a593Smuzhiyun 		 */
1161*4882a593Smuzhiyun 		if (elf_ppnt->p_offset <= elf_ex->e_phoff &&
1162*4882a593Smuzhiyun 		    elf_ex->e_phoff < elf_ppnt->p_offset + elf_ppnt->p_filesz) {
1163*4882a593Smuzhiyun 			phdr_addr = elf_ex->e_phoff - elf_ppnt->p_offset +
1164*4882a593Smuzhiyun 				    elf_ppnt->p_vaddr;
1165*4882a593Smuzhiyun 		}
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 		k = elf_ppnt->p_vaddr;
1168*4882a593Smuzhiyun 		if ((elf_ppnt->p_flags & PF_X) && k < start_code)
1169*4882a593Smuzhiyun 			start_code = k;
1170*4882a593Smuzhiyun 		if (start_data < k)
1171*4882a593Smuzhiyun 			start_data = k;
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 		/*
1174*4882a593Smuzhiyun 		 * Check to see if the section's size will overflow the
1175*4882a593Smuzhiyun 		 * allowed task size. Note that p_filesz must always be
1176*4882a593Smuzhiyun 		 * <= p_memsz so it is only necessary to check p_memsz.
1177*4882a593Smuzhiyun 		 */
1178*4882a593Smuzhiyun 		if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
1179*4882a593Smuzhiyun 		    elf_ppnt->p_memsz > TASK_SIZE ||
1180*4882a593Smuzhiyun 		    TASK_SIZE - elf_ppnt->p_memsz < k) {
1181*4882a593Smuzhiyun 			/* set_brk can never work. Avoid overflows. */
1182*4882a593Smuzhiyun 			retval = -EINVAL;
1183*4882a593Smuzhiyun 			goto out_free_dentry;
1184*4882a593Smuzhiyun 		}
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 		k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 		if (k > elf_bss)
1189*4882a593Smuzhiyun 			elf_bss = k;
1190*4882a593Smuzhiyun 		if ((elf_ppnt->p_flags & PF_X) && end_code < k)
1191*4882a593Smuzhiyun 			end_code = k;
1192*4882a593Smuzhiyun 		if (end_data < k)
1193*4882a593Smuzhiyun 			end_data = k;
1194*4882a593Smuzhiyun 		k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
1195*4882a593Smuzhiyun 		if (k > elf_brk) {
1196*4882a593Smuzhiyun 			bss_prot = elf_prot;
1197*4882a593Smuzhiyun 			elf_brk = k;
1198*4882a593Smuzhiyun 		}
1199*4882a593Smuzhiyun 	}
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	e_entry = elf_ex->e_entry + load_bias;
1202*4882a593Smuzhiyun 	phdr_addr += load_bias;
1203*4882a593Smuzhiyun 	elf_bss += load_bias;
1204*4882a593Smuzhiyun 	elf_brk += load_bias;
1205*4882a593Smuzhiyun 	start_code += load_bias;
1206*4882a593Smuzhiyun 	end_code += load_bias;
1207*4882a593Smuzhiyun 	start_data += load_bias;
1208*4882a593Smuzhiyun 	end_data += load_bias;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	/* Calling set_brk effectively mmaps the pages that we need
1211*4882a593Smuzhiyun 	 * for the bss and break sections.  We must do this before
1212*4882a593Smuzhiyun 	 * mapping in the interpreter, to make sure it doesn't wind
1213*4882a593Smuzhiyun 	 * up getting placed where the bss needs to go.
1214*4882a593Smuzhiyun 	 */
1215*4882a593Smuzhiyun 	retval = set_brk(elf_bss, elf_brk, bss_prot);
1216*4882a593Smuzhiyun 	if (retval)
1217*4882a593Smuzhiyun 		goto out_free_dentry;
1218*4882a593Smuzhiyun 	if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
1219*4882a593Smuzhiyun 		retval = -EFAULT; /* Nobody gets to see this, but.. */
1220*4882a593Smuzhiyun 		goto out_free_dentry;
1221*4882a593Smuzhiyun 	}
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 	if (interpreter) {
1224*4882a593Smuzhiyun 		elf_entry = load_elf_interp(interp_elf_ex,
1225*4882a593Smuzhiyun 					    interpreter,
1226*4882a593Smuzhiyun 					    load_bias, interp_elf_phdata,
1227*4882a593Smuzhiyun 					    &arch_state);
1228*4882a593Smuzhiyun 		if (!IS_ERR((void *)elf_entry)) {
1229*4882a593Smuzhiyun 			/*
1230*4882a593Smuzhiyun 			 * load_elf_interp() returns relocation
1231*4882a593Smuzhiyun 			 * adjustment
1232*4882a593Smuzhiyun 			 */
1233*4882a593Smuzhiyun 			interp_load_addr = elf_entry;
1234*4882a593Smuzhiyun 			elf_entry += interp_elf_ex->e_entry;
1235*4882a593Smuzhiyun 		}
1236*4882a593Smuzhiyun 		if (BAD_ADDR(elf_entry)) {
1237*4882a593Smuzhiyun 			retval = IS_ERR((void *)elf_entry) ?
1238*4882a593Smuzhiyun 					(int)elf_entry : -EINVAL;
1239*4882a593Smuzhiyun 			goto out_free_dentry;
1240*4882a593Smuzhiyun 		}
1241*4882a593Smuzhiyun 		reloc_func_desc = interp_load_addr;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 		allow_write_access(interpreter);
1244*4882a593Smuzhiyun 		fput(interpreter);
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 		kfree(interp_elf_ex);
1247*4882a593Smuzhiyun 		kfree(interp_elf_phdata);
1248*4882a593Smuzhiyun 	} else {
1249*4882a593Smuzhiyun 		elf_entry = e_entry;
1250*4882a593Smuzhiyun 		if (BAD_ADDR(elf_entry)) {
1251*4882a593Smuzhiyun 			retval = -EINVAL;
1252*4882a593Smuzhiyun 			goto out_free_dentry;
1253*4882a593Smuzhiyun 		}
1254*4882a593Smuzhiyun 	}
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	kfree(elf_phdata);
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 	set_binfmt(&elf_format);
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
1261*4882a593Smuzhiyun 	retval = arch_setup_additional_pages(bprm, !!interpreter);
1262*4882a593Smuzhiyun 	if (retval < 0)
1263*4882a593Smuzhiyun 		goto out;
1264*4882a593Smuzhiyun #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	retval = create_elf_tables(bprm, elf_ex, interp_load_addr,
1267*4882a593Smuzhiyun 				   e_entry, phdr_addr);
1268*4882a593Smuzhiyun 	if (retval < 0)
1269*4882a593Smuzhiyun 		goto out;
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 	mm = current->mm;
1272*4882a593Smuzhiyun 	mm->end_code = end_code;
1273*4882a593Smuzhiyun 	mm->start_code = start_code;
1274*4882a593Smuzhiyun 	mm->start_data = start_data;
1275*4882a593Smuzhiyun 	mm->end_data = end_data;
1276*4882a593Smuzhiyun 	mm->start_stack = bprm->p;
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) {
1279*4882a593Smuzhiyun 		/*
1280*4882a593Smuzhiyun 		 * For architectures with ELF randomization, when executing
1281*4882a593Smuzhiyun 		 * a loader directly (i.e. no interpreter listed in ELF
1282*4882a593Smuzhiyun 		 * headers), move the brk area out of the mmap region
1283*4882a593Smuzhiyun 		 * (since it grows up, and may collide early with the stack
1284*4882a593Smuzhiyun 		 * growing down), and into the unused ELF_ET_DYN_BASE region.
1285*4882a593Smuzhiyun 		 */
1286*4882a593Smuzhiyun 		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
1287*4882a593Smuzhiyun 		    elf_ex->e_type == ET_DYN && !interpreter) {
1288*4882a593Smuzhiyun 			mm->brk = mm->start_brk = ELF_ET_DYN_BASE;
1289*4882a593Smuzhiyun 		}
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 		mm->brk = mm->start_brk = arch_randomize_brk(mm);
1292*4882a593Smuzhiyun #ifdef compat_brk_randomized
1293*4882a593Smuzhiyun 		current->brk_randomized = 1;
1294*4882a593Smuzhiyun #endif
1295*4882a593Smuzhiyun 	}
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	if (current->personality & MMAP_PAGE_ZERO) {
1298*4882a593Smuzhiyun 		/* Why this, you ask???  Well SVr4 maps page 0 as read-only,
1299*4882a593Smuzhiyun 		   and some applications "depend" upon this behavior.
1300*4882a593Smuzhiyun 		   Since we do not have the power to recompile these, we
1301*4882a593Smuzhiyun 		   emulate the SVr4 behavior. Sigh. */
1302*4882a593Smuzhiyun 		error = vm_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
1303*4882a593Smuzhiyun 				MAP_FIXED | MAP_PRIVATE, 0);
1304*4882a593Smuzhiyun 	}
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	regs = current_pt_regs();
1307*4882a593Smuzhiyun #ifdef ELF_PLAT_INIT
1308*4882a593Smuzhiyun 	/*
1309*4882a593Smuzhiyun 	 * The ABI may specify that certain registers be set up in special
1310*4882a593Smuzhiyun 	 * ways (on i386 %edx is the address of a DT_FINI function, for
1311*4882a593Smuzhiyun 	 * example.  In addition, it may also specify (eg, PowerPC64 ELF)
1312*4882a593Smuzhiyun 	 * that the e_entry field is the address of the function descriptor
1313*4882a593Smuzhiyun 	 * for the startup routine, rather than the address of the startup
1314*4882a593Smuzhiyun 	 * routine itself.  This macro performs whatever initialization to
1315*4882a593Smuzhiyun 	 * the regs structure is required as well as any relocations to the
1316*4882a593Smuzhiyun 	 * function descriptor entries when executing dynamically links apps.
1317*4882a593Smuzhiyun 	 */
1318*4882a593Smuzhiyun 	ELF_PLAT_INIT(regs, reloc_func_desc);
1319*4882a593Smuzhiyun #endif
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	finalize_exec(bprm);
1322*4882a593Smuzhiyun 	start_thread(regs, elf_entry, bprm->p);
1323*4882a593Smuzhiyun 	retval = 0;
1324*4882a593Smuzhiyun out:
1325*4882a593Smuzhiyun 	return retval;
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	/* error cleanup */
1328*4882a593Smuzhiyun out_free_dentry:
1329*4882a593Smuzhiyun 	kfree(interp_elf_ex);
1330*4882a593Smuzhiyun 	kfree(interp_elf_phdata);
1331*4882a593Smuzhiyun out_free_file:
1332*4882a593Smuzhiyun 	allow_write_access(interpreter);
1333*4882a593Smuzhiyun 	if (interpreter)
1334*4882a593Smuzhiyun 		fput(interpreter);
1335*4882a593Smuzhiyun out_free_ph:
1336*4882a593Smuzhiyun 	kfree(elf_phdata);
1337*4882a593Smuzhiyun 	goto out;
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun #ifdef CONFIG_USELIB
1341*4882a593Smuzhiyun /* This is really simpleminded and specialized - we are loading an
1342*4882a593Smuzhiyun    a.out library that is given an ELF header. */
load_elf_library(struct file * file)1343*4882a593Smuzhiyun static int load_elf_library(struct file *file)
1344*4882a593Smuzhiyun {
1345*4882a593Smuzhiyun 	struct elf_phdr *elf_phdata;
1346*4882a593Smuzhiyun 	struct elf_phdr *eppnt;
1347*4882a593Smuzhiyun 	unsigned long elf_bss, bss, len;
1348*4882a593Smuzhiyun 	int retval, error, i, j;
1349*4882a593Smuzhiyun 	struct elfhdr elf_ex;
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	error = -ENOEXEC;
1352*4882a593Smuzhiyun 	retval = elf_read(file, &elf_ex, sizeof(elf_ex), 0);
1353*4882a593Smuzhiyun 	if (retval < 0)
1354*4882a593Smuzhiyun 		goto out;
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
1357*4882a593Smuzhiyun 		goto out;
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	/* First of all, some simple consistency checks */
1360*4882a593Smuzhiyun 	if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
1361*4882a593Smuzhiyun 	    !elf_check_arch(&elf_ex) || !file->f_op->mmap)
1362*4882a593Smuzhiyun 		goto out;
1363*4882a593Smuzhiyun 	if (elf_check_fdpic(&elf_ex))
1364*4882a593Smuzhiyun 		goto out;
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 	/* Now read in all of the header information */
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 	j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
1369*4882a593Smuzhiyun 	/* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun 	error = -ENOMEM;
1372*4882a593Smuzhiyun 	elf_phdata = kmalloc(j, GFP_KERNEL);
1373*4882a593Smuzhiyun 	if (!elf_phdata)
1374*4882a593Smuzhiyun 		goto out;
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	eppnt = elf_phdata;
1377*4882a593Smuzhiyun 	error = -ENOEXEC;
1378*4882a593Smuzhiyun 	retval = elf_read(file, eppnt, j, elf_ex.e_phoff);
1379*4882a593Smuzhiyun 	if (retval < 0)
1380*4882a593Smuzhiyun 		goto out_free_ph;
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
1383*4882a593Smuzhiyun 		if ((eppnt + i)->p_type == PT_LOAD)
1384*4882a593Smuzhiyun 			j++;
1385*4882a593Smuzhiyun 	if (j != 1)
1386*4882a593Smuzhiyun 		goto out_free_ph;
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	while (eppnt->p_type != PT_LOAD)
1389*4882a593Smuzhiyun 		eppnt++;
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	/* Now use mmap to map the library into memory. */
1392*4882a593Smuzhiyun 	error = vm_mmap(file,
1393*4882a593Smuzhiyun 			ELF_PAGESTART(eppnt->p_vaddr),
1394*4882a593Smuzhiyun 			(eppnt->p_filesz +
1395*4882a593Smuzhiyun 			 ELF_PAGEOFFSET(eppnt->p_vaddr)),
1396*4882a593Smuzhiyun 			PROT_READ | PROT_WRITE | PROT_EXEC,
1397*4882a593Smuzhiyun 			MAP_FIXED_NOREPLACE | MAP_PRIVATE | MAP_DENYWRITE,
1398*4882a593Smuzhiyun 			(eppnt->p_offset -
1399*4882a593Smuzhiyun 			 ELF_PAGEOFFSET(eppnt->p_vaddr)));
1400*4882a593Smuzhiyun 	if (error != ELF_PAGESTART(eppnt->p_vaddr))
1401*4882a593Smuzhiyun 		goto out_free_ph;
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	elf_bss = eppnt->p_vaddr + eppnt->p_filesz;
1404*4882a593Smuzhiyun 	if (padzero(elf_bss)) {
1405*4882a593Smuzhiyun 		error = -EFAULT;
1406*4882a593Smuzhiyun 		goto out_free_ph;
1407*4882a593Smuzhiyun 	}
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	len = ELF_PAGEALIGN(eppnt->p_filesz + eppnt->p_vaddr);
1410*4882a593Smuzhiyun 	bss = ELF_PAGEALIGN(eppnt->p_memsz + eppnt->p_vaddr);
1411*4882a593Smuzhiyun 	if (bss > len) {
1412*4882a593Smuzhiyun 		error = vm_brk(len, bss - len);
1413*4882a593Smuzhiyun 		if (error)
1414*4882a593Smuzhiyun 			goto out_free_ph;
1415*4882a593Smuzhiyun 	}
1416*4882a593Smuzhiyun 	error = 0;
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun out_free_ph:
1419*4882a593Smuzhiyun 	kfree(elf_phdata);
1420*4882a593Smuzhiyun out:
1421*4882a593Smuzhiyun 	return error;
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun #endif /* #ifdef CONFIG_USELIB */
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun #ifdef CONFIG_ELF_CORE
1426*4882a593Smuzhiyun /*
1427*4882a593Smuzhiyun  * ELF core dumper
1428*4882a593Smuzhiyun  *
1429*4882a593Smuzhiyun  * Modelled on fs/exec.c:aout_core_dump()
1430*4882a593Smuzhiyun  * Jeremy Fitzhardinge <jeremy@sw.oz.au>
1431*4882a593Smuzhiyun  */
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun /* An ELF note in memory */
1434*4882a593Smuzhiyun struct memelfnote
1435*4882a593Smuzhiyun {
1436*4882a593Smuzhiyun 	const char *name;
1437*4882a593Smuzhiyun 	int type;
1438*4882a593Smuzhiyun 	unsigned int datasz;
1439*4882a593Smuzhiyun 	void *data;
1440*4882a593Smuzhiyun };
1441*4882a593Smuzhiyun 
notesize(struct memelfnote * en)1442*4882a593Smuzhiyun static int notesize(struct memelfnote *en)
1443*4882a593Smuzhiyun {
1444*4882a593Smuzhiyun 	int sz;
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun 	sz = sizeof(struct elf_note);
1447*4882a593Smuzhiyun 	sz += roundup(strlen(en->name) + 1, 4);
1448*4882a593Smuzhiyun 	sz += roundup(en->datasz, 4);
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 	return sz;
1451*4882a593Smuzhiyun }
1452*4882a593Smuzhiyun 
writenote(struct memelfnote * men,struct coredump_params * cprm)1453*4882a593Smuzhiyun static int writenote(struct memelfnote *men, struct coredump_params *cprm)
1454*4882a593Smuzhiyun {
1455*4882a593Smuzhiyun 	struct elf_note en;
1456*4882a593Smuzhiyun 	en.n_namesz = strlen(men->name) + 1;
1457*4882a593Smuzhiyun 	en.n_descsz = men->datasz;
1458*4882a593Smuzhiyun 	en.n_type = men->type;
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 	return dump_emit(cprm, &en, sizeof(en)) &&
1461*4882a593Smuzhiyun 	    dump_emit(cprm, men->name, en.n_namesz) && dump_align(cprm, 4) &&
1462*4882a593Smuzhiyun 	    dump_emit(cprm, men->data, men->datasz) && dump_align(cprm, 4);
1463*4882a593Smuzhiyun }
1464*4882a593Smuzhiyun 
fill_elf_header(struct elfhdr * elf,int segs,u16 machine,u32 flags)1465*4882a593Smuzhiyun static void fill_elf_header(struct elfhdr *elf, int segs,
1466*4882a593Smuzhiyun 			    u16 machine, u32 flags)
1467*4882a593Smuzhiyun {
1468*4882a593Smuzhiyun 	memset(elf, 0, sizeof(*elf));
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 	memcpy(elf->e_ident, ELFMAG, SELFMAG);
1471*4882a593Smuzhiyun 	elf->e_ident[EI_CLASS] = ELF_CLASS;
1472*4882a593Smuzhiyun 	elf->e_ident[EI_DATA] = ELF_DATA;
1473*4882a593Smuzhiyun 	elf->e_ident[EI_VERSION] = EV_CURRENT;
1474*4882a593Smuzhiyun 	elf->e_ident[EI_OSABI] = ELF_OSABI;
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	elf->e_type = ET_CORE;
1477*4882a593Smuzhiyun 	elf->e_machine = machine;
1478*4882a593Smuzhiyun 	elf->e_version = EV_CURRENT;
1479*4882a593Smuzhiyun 	elf->e_phoff = sizeof(struct elfhdr);
1480*4882a593Smuzhiyun 	elf->e_flags = flags;
1481*4882a593Smuzhiyun 	elf->e_ehsize = sizeof(struct elfhdr);
1482*4882a593Smuzhiyun 	elf->e_phentsize = sizeof(struct elf_phdr);
1483*4882a593Smuzhiyun 	elf->e_phnum = segs;
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun 
fill_elf_note_phdr(struct elf_phdr * phdr,int sz,loff_t offset)1486*4882a593Smuzhiyun static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, loff_t offset)
1487*4882a593Smuzhiyun {
1488*4882a593Smuzhiyun 	phdr->p_type = PT_NOTE;
1489*4882a593Smuzhiyun 	phdr->p_offset = offset;
1490*4882a593Smuzhiyun 	phdr->p_vaddr = 0;
1491*4882a593Smuzhiyun 	phdr->p_paddr = 0;
1492*4882a593Smuzhiyun 	phdr->p_filesz = sz;
1493*4882a593Smuzhiyun 	phdr->p_memsz = 0;
1494*4882a593Smuzhiyun 	phdr->p_flags = 0;
1495*4882a593Smuzhiyun 	phdr->p_align = 0;
1496*4882a593Smuzhiyun }
1497*4882a593Smuzhiyun 
fill_note(struct memelfnote * note,const char * name,int type,unsigned int sz,void * data)1498*4882a593Smuzhiyun static void fill_note(struct memelfnote *note, const char *name, int type,
1499*4882a593Smuzhiyun 		unsigned int sz, void *data)
1500*4882a593Smuzhiyun {
1501*4882a593Smuzhiyun 	note->name = name;
1502*4882a593Smuzhiyun 	note->type = type;
1503*4882a593Smuzhiyun 	note->datasz = sz;
1504*4882a593Smuzhiyun 	note->data = data;
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun /*
1508*4882a593Smuzhiyun  * fill up all the fields in prstatus from the given task struct, except
1509*4882a593Smuzhiyun  * registers which need to be filled up separately.
1510*4882a593Smuzhiyun  */
fill_prstatus(struct elf_prstatus * prstatus,struct task_struct * p,long signr)1511*4882a593Smuzhiyun static void fill_prstatus(struct elf_prstatus *prstatus,
1512*4882a593Smuzhiyun 		struct task_struct *p, long signr)
1513*4882a593Smuzhiyun {
1514*4882a593Smuzhiyun 	prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
1515*4882a593Smuzhiyun 	prstatus->pr_sigpend = p->pending.signal.sig[0];
1516*4882a593Smuzhiyun 	prstatus->pr_sighold = p->blocked.sig[0];
1517*4882a593Smuzhiyun 	rcu_read_lock();
1518*4882a593Smuzhiyun 	prstatus->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1519*4882a593Smuzhiyun 	rcu_read_unlock();
1520*4882a593Smuzhiyun 	prstatus->pr_pid = task_pid_vnr(p);
1521*4882a593Smuzhiyun 	prstatus->pr_pgrp = task_pgrp_vnr(p);
1522*4882a593Smuzhiyun 	prstatus->pr_sid = task_session_vnr(p);
1523*4882a593Smuzhiyun 	if (thread_group_leader(p)) {
1524*4882a593Smuzhiyun 		struct task_cputime cputime;
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun 		/*
1527*4882a593Smuzhiyun 		 * This is the record for the group leader.  It shows the
1528*4882a593Smuzhiyun 		 * group-wide total, not its individual thread total.
1529*4882a593Smuzhiyun 		 */
1530*4882a593Smuzhiyun 		thread_group_cputime(p, &cputime);
1531*4882a593Smuzhiyun 		prstatus->pr_utime = ns_to_kernel_old_timeval(cputime.utime);
1532*4882a593Smuzhiyun 		prstatus->pr_stime = ns_to_kernel_old_timeval(cputime.stime);
1533*4882a593Smuzhiyun 	} else {
1534*4882a593Smuzhiyun 		u64 utime, stime;
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 		task_cputime(p, &utime, &stime);
1537*4882a593Smuzhiyun 		prstatus->pr_utime = ns_to_kernel_old_timeval(utime);
1538*4882a593Smuzhiyun 		prstatus->pr_stime = ns_to_kernel_old_timeval(stime);
1539*4882a593Smuzhiyun 	}
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	prstatus->pr_cutime = ns_to_kernel_old_timeval(p->signal->cutime);
1542*4882a593Smuzhiyun 	prstatus->pr_cstime = ns_to_kernel_old_timeval(p->signal->cstime);
1543*4882a593Smuzhiyun }
1544*4882a593Smuzhiyun 
fill_psinfo(struct elf_prpsinfo * psinfo,struct task_struct * p,struct mm_struct * mm)1545*4882a593Smuzhiyun static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
1546*4882a593Smuzhiyun 		       struct mm_struct *mm)
1547*4882a593Smuzhiyun {
1548*4882a593Smuzhiyun 	const struct cred *cred;
1549*4882a593Smuzhiyun 	unsigned int i, len;
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	/* first copy the parameters from user space */
1552*4882a593Smuzhiyun 	memset(psinfo, 0, sizeof(struct elf_prpsinfo));
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 	len = mm->arg_end - mm->arg_start;
1555*4882a593Smuzhiyun 	if (len >= ELF_PRARGSZ)
1556*4882a593Smuzhiyun 		len = ELF_PRARGSZ-1;
1557*4882a593Smuzhiyun 	if (copy_from_user(&psinfo->pr_psargs,
1558*4882a593Smuzhiyun 		           (const char __user *)mm->arg_start, len))
1559*4882a593Smuzhiyun 		return -EFAULT;
1560*4882a593Smuzhiyun 	for(i = 0; i < len; i++)
1561*4882a593Smuzhiyun 		if (psinfo->pr_psargs[i] == 0)
1562*4882a593Smuzhiyun 			psinfo->pr_psargs[i] = ' ';
1563*4882a593Smuzhiyun 	psinfo->pr_psargs[len] = 0;
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	rcu_read_lock();
1566*4882a593Smuzhiyun 	psinfo->pr_ppid = task_pid_vnr(rcu_dereference(p->real_parent));
1567*4882a593Smuzhiyun 	rcu_read_unlock();
1568*4882a593Smuzhiyun 	psinfo->pr_pid = task_pid_vnr(p);
1569*4882a593Smuzhiyun 	psinfo->pr_pgrp = task_pgrp_vnr(p);
1570*4882a593Smuzhiyun 	psinfo->pr_sid = task_session_vnr(p);
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 	i = p->state ? ffz(~p->state) + 1 : 0;
1573*4882a593Smuzhiyun 	psinfo->pr_state = i;
1574*4882a593Smuzhiyun 	psinfo->pr_sname = (i > 5) ? '.' : "RSDTZW"[i];
1575*4882a593Smuzhiyun 	psinfo->pr_zomb = psinfo->pr_sname == 'Z';
1576*4882a593Smuzhiyun 	psinfo->pr_nice = task_nice(p);
1577*4882a593Smuzhiyun 	psinfo->pr_flag = p->flags;
1578*4882a593Smuzhiyun 	rcu_read_lock();
1579*4882a593Smuzhiyun 	cred = __task_cred(p);
1580*4882a593Smuzhiyun 	SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid));
1581*4882a593Smuzhiyun 	SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
1582*4882a593Smuzhiyun 	rcu_read_unlock();
1583*4882a593Smuzhiyun 	strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun 	return 0;
1586*4882a593Smuzhiyun }
1587*4882a593Smuzhiyun 
fill_auxv_note(struct memelfnote * note,struct mm_struct * mm)1588*4882a593Smuzhiyun static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)
1589*4882a593Smuzhiyun {
1590*4882a593Smuzhiyun 	elf_addr_t *auxv = (elf_addr_t *) mm->saved_auxv;
1591*4882a593Smuzhiyun 	int i = 0;
1592*4882a593Smuzhiyun 	do
1593*4882a593Smuzhiyun 		i += 2;
1594*4882a593Smuzhiyun 	while (auxv[i - 2] != AT_NULL);
1595*4882a593Smuzhiyun 	fill_note(note, "CORE", NT_AUXV, i * sizeof(elf_addr_t), auxv);
1596*4882a593Smuzhiyun }
1597*4882a593Smuzhiyun 
fill_siginfo_note(struct memelfnote * note,user_siginfo_t * csigdata,const kernel_siginfo_t * siginfo)1598*4882a593Smuzhiyun static void fill_siginfo_note(struct memelfnote *note, user_siginfo_t *csigdata,
1599*4882a593Smuzhiyun 		const kernel_siginfo_t *siginfo)
1600*4882a593Smuzhiyun {
1601*4882a593Smuzhiyun 	copy_siginfo_to_external(csigdata, siginfo);
1602*4882a593Smuzhiyun 	fill_note(note, "CORE", NT_SIGINFO, sizeof(*csigdata), csigdata);
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun #define MAX_FILE_NOTE_SIZE (4*1024*1024)
1606*4882a593Smuzhiyun /*
1607*4882a593Smuzhiyun  * Format of NT_FILE note:
1608*4882a593Smuzhiyun  *
1609*4882a593Smuzhiyun  * long count     -- how many files are mapped
1610*4882a593Smuzhiyun  * long page_size -- units for file_ofs
1611*4882a593Smuzhiyun  * array of [COUNT] elements of
1612*4882a593Smuzhiyun  *   long start
1613*4882a593Smuzhiyun  *   long end
1614*4882a593Smuzhiyun  *   long file_ofs
1615*4882a593Smuzhiyun  * followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
1616*4882a593Smuzhiyun  */
fill_files_note(struct memelfnote * note)1617*4882a593Smuzhiyun static int fill_files_note(struct memelfnote *note)
1618*4882a593Smuzhiyun {
1619*4882a593Smuzhiyun 	struct mm_struct *mm = current->mm;
1620*4882a593Smuzhiyun 	struct vm_area_struct *vma;
1621*4882a593Smuzhiyun 	unsigned count, size, names_ofs, remaining, n;
1622*4882a593Smuzhiyun 	user_long_t *data;
1623*4882a593Smuzhiyun 	user_long_t *start_end_ofs;
1624*4882a593Smuzhiyun 	char *name_base, *name_curpos;
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	/* *Estimated* file count and total data size needed */
1627*4882a593Smuzhiyun 	count = mm->map_count;
1628*4882a593Smuzhiyun 	if (count > UINT_MAX / 64)
1629*4882a593Smuzhiyun 		return -EINVAL;
1630*4882a593Smuzhiyun 	size = count * 64;
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 	names_ofs = (2 + 3 * count) * sizeof(data[0]);
1633*4882a593Smuzhiyun  alloc:
1634*4882a593Smuzhiyun 	if (size >= MAX_FILE_NOTE_SIZE) /* paranoia check */
1635*4882a593Smuzhiyun 		return -EINVAL;
1636*4882a593Smuzhiyun 	size = round_up(size, PAGE_SIZE);
1637*4882a593Smuzhiyun 	/*
1638*4882a593Smuzhiyun 	 * "size" can be 0 here legitimately.
1639*4882a593Smuzhiyun 	 * Let it ENOMEM and omit NT_FILE section which will be empty anyway.
1640*4882a593Smuzhiyun 	 */
1641*4882a593Smuzhiyun 	data = kvmalloc(size, GFP_KERNEL);
1642*4882a593Smuzhiyun 	if (ZERO_OR_NULL_PTR(data))
1643*4882a593Smuzhiyun 		return -ENOMEM;
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun 	start_end_ofs = data + 2;
1646*4882a593Smuzhiyun 	name_base = name_curpos = ((char *)data) + names_ofs;
1647*4882a593Smuzhiyun 	remaining = size - names_ofs;
1648*4882a593Smuzhiyun 	count = 0;
1649*4882a593Smuzhiyun 	for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
1650*4882a593Smuzhiyun 		struct file *file;
1651*4882a593Smuzhiyun 		const char *filename;
1652*4882a593Smuzhiyun 
1653*4882a593Smuzhiyun 		file = vma->vm_file;
1654*4882a593Smuzhiyun 		if (!file)
1655*4882a593Smuzhiyun 			continue;
1656*4882a593Smuzhiyun 		filename = file_path(file, name_curpos, remaining);
1657*4882a593Smuzhiyun 		if (IS_ERR(filename)) {
1658*4882a593Smuzhiyun 			if (PTR_ERR(filename) == -ENAMETOOLONG) {
1659*4882a593Smuzhiyun 				kvfree(data);
1660*4882a593Smuzhiyun 				size = size * 5 / 4;
1661*4882a593Smuzhiyun 				goto alloc;
1662*4882a593Smuzhiyun 			}
1663*4882a593Smuzhiyun 			continue;
1664*4882a593Smuzhiyun 		}
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun 		/* file_path() fills at the end, move name down */
1667*4882a593Smuzhiyun 		/* n = strlen(filename) + 1: */
1668*4882a593Smuzhiyun 		n = (name_curpos + remaining) - filename;
1669*4882a593Smuzhiyun 		remaining = filename - name_curpos;
1670*4882a593Smuzhiyun 		memmove(name_curpos, filename, n);
1671*4882a593Smuzhiyun 		name_curpos += n;
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 		*start_end_ofs++ = vma->vm_start;
1674*4882a593Smuzhiyun 		*start_end_ofs++ = vma->vm_end;
1675*4882a593Smuzhiyun 		*start_end_ofs++ = vma->vm_pgoff;
1676*4882a593Smuzhiyun 		count++;
1677*4882a593Smuzhiyun 	}
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun 	/* Now we know exact count of files, can store it */
1680*4882a593Smuzhiyun 	data[0] = count;
1681*4882a593Smuzhiyun 	data[1] = PAGE_SIZE;
1682*4882a593Smuzhiyun 	/*
1683*4882a593Smuzhiyun 	 * Count usually is less than mm->map_count,
1684*4882a593Smuzhiyun 	 * we need to move filenames down.
1685*4882a593Smuzhiyun 	 */
1686*4882a593Smuzhiyun 	n = mm->map_count - count;
1687*4882a593Smuzhiyun 	if (n != 0) {
1688*4882a593Smuzhiyun 		unsigned shift_bytes = n * 3 * sizeof(data[0]);
1689*4882a593Smuzhiyun 		memmove(name_base - shift_bytes, name_base,
1690*4882a593Smuzhiyun 			name_curpos - name_base);
1691*4882a593Smuzhiyun 		name_curpos -= shift_bytes;
1692*4882a593Smuzhiyun 	}
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 	size = name_curpos - (char *)data;
1695*4882a593Smuzhiyun 	fill_note(note, "CORE", NT_FILE, size, data);
1696*4882a593Smuzhiyun 	return 0;
1697*4882a593Smuzhiyun }
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun #ifdef CORE_DUMP_USE_REGSET
1700*4882a593Smuzhiyun #include <linux/regset.h>
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun struct elf_thread_core_info {
1703*4882a593Smuzhiyun 	struct elf_thread_core_info *next;
1704*4882a593Smuzhiyun 	struct task_struct *task;
1705*4882a593Smuzhiyun 	struct elf_prstatus prstatus;
1706*4882a593Smuzhiyun 	struct memelfnote notes[];
1707*4882a593Smuzhiyun };
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun struct elf_note_info {
1710*4882a593Smuzhiyun 	struct elf_thread_core_info *thread;
1711*4882a593Smuzhiyun 	struct memelfnote psinfo;
1712*4882a593Smuzhiyun 	struct memelfnote signote;
1713*4882a593Smuzhiyun 	struct memelfnote auxv;
1714*4882a593Smuzhiyun 	struct memelfnote files;
1715*4882a593Smuzhiyun 	user_siginfo_t csigdata;
1716*4882a593Smuzhiyun 	size_t size;
1717*4882a593Smuzhiyun 	int thread_notes;
1718*4882a593Smuzhiyun };
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun /*
1721*4882a593Smuzhiyun  * When a regset has a writeback hook, we call it on each thread before
1722*4882a593Smuzhiyun  * dumping user memory.  On register window machines, this makes sure the
1723*4882a593Smuzhiyun  * user memory backing the register data is up to date before we read it.
1724*4882a593Smuzhiyun  */
do_thread_regset_writeback(struct task_struct * task,const struct user_regset * regset)1725*4882a593Smuzhiyun static void do_thread_regset_writeback(struct task_struct *task,
1726*4882a593Smuzhiyun 				       const struct user_regset *regset)
1727*4882a593Smuzhiyun {
1728*4882a593Smuzhiyun 	if (regset->writeback)
1729*4882a593Smuzhiyun 		regset->writeback(task, regset, 1);
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun #ifndef PRSTATUS_SIZE
1733*4882a593Smuzhiyun #define PRSTATUS_SIZE(S, R) sizeof(S)
1734*4882a593Smuzhiyun #endif
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun #ifndef SET_PR_FPVALID
1737*4882a593Smuzhiyun #define SET_PR_FPVALID(S, V, R) ((S)->pr_fpvalid = (V))
1738*4882a593Smuzhiyun #endif
1739*4882a593Smuzhiyun 
fill_thread_core_info(struct elf_thread_core_info * t,const struct user_regset_view * view,long signr,size_t * total)1740*4882a593Smuzhiyun static int fill_thread_core_info(struct elf_thread_core_info *t,
1741*4882a593Smuzhiyun 				 const struct user_regset_view *view,
1742*4882a593Smuzhiyun 				 long signr, size_t *total)
1743*4882a593Smuzhiyun {
1744*4882a593Smuzhiyun 	unsigned int i;
1745*4882a593Smuzhiyun 	int regset0_size;
1746*4882a593Smuzhiyun 
1747*4882a593Smuzhiyun 	/*
1748*4882a593Smuzhiyun 	 * NT_PRSTATUS is the one special case, because the regset data
1749*4882a593Smuzhiyun 	 * goes into the pr_reg field inside the note contents, rather
1750*4882a593Smuzhiyun 	 * than being the whole note contents.  We fill the reset in here.
1751*4882a593Smuzhiyun 	 * We assume that regset 0 is NT_PRSTATUS.
1752*4882a593Smuzhiyun 	 */
1753*4882a593Smuzhiyun 	fill_prstatus(&t->prstatus, t->task, signr);
1754*4882a593Smuzhiyun 	regset0_size = regset_get(t->task, &view->regsets[0],
1755*4882a593Smuzhiyun 		   sizeof(t->prstatus.pr_reg), &t->prstatus.pr_reg);
1756*4882a593Smuzhiyun 	if (regset0_size < 0)
1757*4882a593Smuzhiyun 		return 0;
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	fill_note(&t->notes[0], "CORE", NT_PRSTATUS,
1760*4882a593Smuzhiyun 		  PRSTATUS_SIZE(t->prstatus, regset0_size), &t->prstatus);
1761*4882a593Smuzhiyun 	*total += notesize(&t->notes[0]);
1762*4882a593Smuzhiyun 
1763*4882a593Smuzhiyun 	do_thread_regset_writeback(t->task, &view->regsets[0]);
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	/*
1766*4882a593Smuzhiyun 	 * Each other regset might generate a note too.  For each regset
1767*4882a593Smuzhiyun 	 * that has no core_note_type or is inactive, we leave t->notes[i]
1768*4882a593Smuzhiyun 	 * all zero and we'll know to skip writing it later.
1769*4882a593Smuzhiyun 	 */
1770*4882a593Smuzhiyun 	for (i = 1; i < view->n; ++i) {
1771*4882a593Smuzhiyun 		const struct user_regset *regset = &view->regsets[i];
1772*4882a593Smuzhiyun 		int note_type = regset->core_note_type;
1773*4882a593Smuzhiyun 		bool is_fpreg = note_type == NT_PRFPREG;
1774*4882a593Smuzhiyun 		void *data;
1775*4882a593Smuzhiyun 		int ret;
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 		do_thread_regset_writeback(t->task, regset);
1778*4882a593Smuzhiyun 		if (!note_type) // not for coredumps
1779*4882a593Smuzhiyun 			continue;
1780*4882a593Smuzhiyun 		if (regset->active && regset->active(t->task, regset) <= 0)
1781*4882a593Smuzhiyun 			continue;
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 		ret = regset_get_alloc(t->task, regset, ~0U, &data);
1784*4882a593Smuzhiyun 		if (ret < 0)
1785*4882a593Smuzhiyun 			continue;
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun 		if (is_fpreg)
1788*4882a593Smuzhiyun 			SET_PR_FPVALID(&t->prstatus, 1, regset0_size);
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun 		fill_note(&t->notes[i], is_fpreg ? "CORE" : "LINUX",
1791*4882a593Smuzhiyun 			  note_type, ret, data);
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 		*total += notesize(&t->notes[i]);
1794*4882a593Smuzhiyun 	}
1795*4882a593Smuzhiyun 
1796*4882a593Smuzhiyun 	return 1;
1797*4882a593Smuzhiyun }
1798*4882a593Smuzhiyun 
fill_note_info(struct elfhdr * elf,int phdrs,struct elf_note_info * info,struct coredump_params * cprm)1799*4882a593Smuzhiyun static int fill_note_info(struct elfhdr *elf, int phdrs,
1800*4882a593Smuzhiyun 			  struct elf_note_info *info,
1801*4882a593Smuzhiyun 			  struct coredump_params *cprm)
1802*4882a593Smuzhiyun {
1803*4882a593Smuzhiyun 	struct task_struct *dump_task = current;
1804*4882a593Smuzhiyun 	const struct user_regset_view *view = task_user_regset_view(dump_task);
1805*4882a593Smuzhiyun 	struct elf_thread_core_info *t;
1806*4882a593Smuzhiyun 	struct elf_prpsinfo *psinfo;
1807*4882a593Smuzhiyun 	struct core_thread *ct;
1808*4882a593Smuzhiyun 	unsigned int i;
1809*4882a593Smuzhiyun 
1810*4882a593Smuzhiyun 	info->size = 0;
1811*4882a593Smuzhiyun 	info->thread = NULL;
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun 	psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
1814*4882a593Smuzhiyun 	if (psinfo == NULL) {
1815*4882a593Smuzhiyun 		info->psinfo.data = NULL; /* So we don't free this wrongly */
1816*4882a593Smuzhiyun 		return 0;
1817*4882a593Smuzhiyun 	}
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	fill_note(&info->psinfo, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun 	/*
1822*4882a593Smuzhiyun 	 * Figure out how many notes we're going to need for each thread.
1823*4882a593Smuzhiyun 	 */
1824*4882a593Smuzhiyun 	info->thread_notes = 0;
1825*4882a593Smuzhiyun 	for (i = 0; i < view->n; ++i)
1826*4882a593Smuzhiyun 		if (view->regsets[i].core_note_type != 0)
1827*4882a593Smuzhiyun 			++info->thread_notes;
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun 	/*
1830*4882a593Smuzhiyun 	 * Sanity check.  We rely on regset 0 being in NT_PRSTATUS,
1831*4882a593Smuzhiyun 	 * since it is our one special case.
1832*4882a593Smuzhiyun 	 */
1833*4882a593Smuzhiyun 	if (unlikely(info->thread_notes == 0) ||
1834*4882a593Smuzhiyun 	    unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) {
1835*4882a593Smuzhiyun 		WARN_ON(1);
1836*4882a593Smuzhiyun 		return 0;
1837*4882a593Smuzhiyun 	}
1838*4882a593Smuzhiyun 
1839*4882a593Smuzhiyun 	/*
1840*4882a593Smuzhiyun 	 * Initialize the ELF file header.
1841*4882a593Smuzhiyun 	 */
1842*4882a593Smuzhiyun 	fill_elf_header(elf, phdrs,
1843*4882a593Smuzhiyun 			view->e_machine, view->e_flags);
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 	/*
1846*4882a593Smuzhiyun 	 * Allocate a structure for each thread.
1847*4882a593Smuzhiyun 	 */
1848*4882a593Smuzhiyun 	for (ct = &dump_task->mm->core_state->dumper; ct; ct = ct->next) {
1849*4882a593Smuzhiyun 		t = kzalloc(offsetof(struct elf_thread_core_info,
1850*4882a593Smuzhiyun 				     notes[info->thread_notes]),
1851*4882a593Smuzhiyun 			    GFP_KERNEL);
1852*4882a593Smuzhiyun 		if (unlikely(!t))
1853*4882a593Smuzhiyun 			return 0;
1854*4882a593Smuzhiyun 
1855*4882a593Smuzhiyun 		t->task = ct->task;
1856*4882a593Smuzhiyun 		if (ct->task == dump_task || !info->thread) {
1857*4882a593Smuzhiyun 			t->next = info->thread;
1858*4882a593Smuzhiyun 			info->thread = t;
1859*4882a593Smuzhiyun 		} else {
1860*4882a593Smuzhiyun 			/*
1861*4882a593Smuzhiyun 			 * Make sure to keep the original task at
1862*4882a593Smuzhiyun 			 * the head of the list.
1863*4882a593Smuzhiyun 			 */
1864*4882a593Smuzhiyun 			t->next = info->thread->next;
1865*4882a593Smuzhiyun 			info->thread->next = t;
1866*4882a593Smuzhiyun 		}
1867*4882a593Smuzhiyun 	}
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	/*
1870*4882a593Smuzhiyun 	 * Now fill in each thread's information.
1871*4882a593Smuzhiyun 	 */
1872*4882a593Smuzhiyun 	for (t = info->thread; t != NULL; t = t->next)
1873*4882a593Smuzhiyun 		if (!fill_thread_core_info(t, view, cprm->siginfo->si_signo, &info->size))
1874*4882a593Smuzhiyun 			return 0;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	/*
1877*4882a593Smuzhiyun 	 * Fill in the two process-wide notes.
1878*4882a593Smuzhiyun 	 */
1879*4882a593Smuzhiyun 	fill_psinfo(psinfo, dump_task->group_leader, dump_task->mm);
1880*4882a593Smuzhiyun 	info->size += notesize(&info->psinfo);
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 	fill_siginfo_note(&info->signote, &info->csigdata, cprm->siginfo);
1883*4882a593Smuzhiyun 	info->size += notesize(&info->signote);
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun 	fill_auxv_note(&info->auxv, current->mm);
1886*4882a593Smuzhiyun 	info->size += notesize(&info->auxv);
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 	if (fill_files_note(&info->files) == 0)
1889*4882a593Smuzhiyun 		info->size += notesize(&info->files);
1890*4882a593Smuzhiyun 
1891*4882a593Smuzhiyun 	return 1;
1892*4882a593Smuzhiyun }
1893*4882a593Smuzhiyun 
get_note_info_size(struct elf_note_info * info)1894*4882a593Smuzhiyun static size_t get_note_info_size(struct elf_note_info *info)
1895*4882a593Smuzhiyun {
1896*4882a593Smuzhiyun 	return info->size;
1897*4882a593Smuzhiyun }
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun /*
1900*4882a593Smuzhiyun  * Write all the notes for each thread.  When writing the first thread, the
1901*4882a593Smuzhiyun  * process-wide notes are interleaved after the first thread-specific note.
1902*4882a593Smuzhiyun  */
write_note_info(struct elf_note_info * info,struct coredump_params * cprm)1903*4882a593Smuzhiyun static int write_note_info(struct elf_note_info *info,
1904*4882a593Smuzhiyun 			   struct coredump_params *cprm)
1905*4882a593Smuzhiyun {
1906*4882a593Smuzhiyun 	bool first = true;
1907*4882a593Smuzhiyun 	struct elf_thread_core_info *t = info->thread;
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 	do {
1910*4882a593Smuzhiyun 		int i;
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun 		if (!writenote(&t->notes[0], cprm))
1913*4882a593Smuzhiyun 			return 0;
1914*4882a593Smuzhiyun 
1915*4882a593Smuzhiyun 		if (first && !writenote(&info->psinfo, cprm))
1916*4882a593Smuzhiyun 			return 0;
1917*4882a593Smuzhiyun 		if (first && !writenote(&info->signote, cprm))
1918*4882a593Smuzhiyun 			return 0;
1919*4882a593Smuzhiyun 		if (first && !writenote(&info->auxv, cprm))
1920*4882a593Smuzhiyun 			return 0;
1921*4882a593Smuzhiyun 		if (first && info->files.data &&
1922*4882a593Smuzhiyun 				!writenote(&info->files, cprm))
1923*4882a593Smuzhiyun 			return 0;
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun 		for (i = 1; i < info->thread_notes; ++i)
1926*4882a593Smuzhiyun 			if (t->notes[i].data &&
1927*4882a593Smuzhiyun 			    !writenote(&t->notes[i], cprm))
1928*4882a593Smuzhiyun 				return 0;
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 		first = false;
1931*4882a593Smuzhiyun 		t = t->next;
1932*4882a593Smuzhiyun 	} while (t);
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun 	return 1;
1935*4882a593Smuzhiyun }
1936*4882a593Smuzhiyun 
free_note_info(struct elf_note_info * info)1937*4882a593Smuzhiyun static void free_note_info(struct elf_note_info *info)
1938*4882a593Smuzhiyun {
1939*4882a593Smuzhiyun 	struct elf_thread_core_info *threads = info->thread;
1940*4882a593Smuzhiyun 	while (threads) {
1941*4882a593Smuzhiyun 		unsigned int i;
1942*4882a593Smuzhiyun 		struct elf_thread_core_info *t = threads;
1943*4882a593Smuzhiyun 		threads = t->next;
1944*4882a593Smuzhiyun 		WARN_ON(t->notes[0].data && t->notes[0].data != &t->prstatus);
1945*4882a593Smuzhiyun 		for (i = 1; i < info->thread_notes; ++i)
1946*4882a593Smuzhiyun 			kfree(t->notes[i].data);
1947*4882a593Smuzhiyun 		kfree(t);
1948*4882a593Smuzhiyun 	}
1949*4882a593Smuzhiyun 	kfree(info->psinfo.data);
1950*4882a593Smuzhiyun 	kvfree(info->files.data);
1951*4882a593Smuzhiyun }
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun #else
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun /* Here is the structure in which status of each thread is captured. */
1956*4882a593Smuzhiyun struct elf_thread_status
1957*4882a593Smuzhiyun {
1958*4882a593Smuzhiyun 	struct list_head list;
1959*4882a593Smuzhiyun 	struct elf_prstatus prstatus;	/* NT_PRSTATUS */
1960*4882a593Smuzhiyun 	elf_fpregset_t fpu;		/* NT_PRFPREG */
1961*4882a593Smuzhiyun 	struct task_struct *thread;
1962*4882a593Smuzhiyun 	struct memelfnote notes[3];
1963*4882a593Smuzhiyun 	int num_notes;
1964*4882a593Smuzhiyun };
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun /*
1967*4882a593Smuzhiyun  * In order to add the specific thread information for the elf file format,
1968*4882a593Smuzhiyun  * we need to keep a linked list of every threads pr_status and then create
1969*4882a593Smuzhiyun  * a single section for them in the final core file.
1970*4882a593Smuzhiyun  */
elf_dump_thread_status(long signr,struct elf_thread_status * t)1971*4882a593Smuzhiyun static int elf_dump_thread_status(long signr, struct elf_thread_status *t)
1972*4882a593Smuzhiyun {
1973*4882a593Smuzhiyun 	int sz = 0;
1974*4882a593Smuzhiyun 	struct task_struct *p = t->thread;
1975*4882a593Smuzhiyun 	t->num_notes = 0;
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun 	fill_prstatus(&t->prstatus, p, signr);
1978*4882a593Smuzhiyun 	elf_core_copy_task_regs(p, &t->prstatus.pr_reg);
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun 	fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus),
1981*4882a593Smuzhiyun 		  &(t->prstatus));
1982*4882a593Smuzhiyun 	t->num_notes++;
1983*4882a593Smuzhiyun 	sz += notesize(&t->notes[0]);
1984*4882a593Smuzhiyun 
1985*4882a593Smuzhiyun 	if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, NULL,
1986*4882a593Smuzhiyun 								&t->fpu))) {
1987*4882a593Smuzhiyun 		fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu),
1988*4882a593Smuzhiyun 			  &(t->fpu));
1989*4882a593Smuzhiyun 		t->num_notes++;
1990*4882a593Smuzhiyun 		sz += notesize(&t->notes[1]);
1991*4882a593Smuzhiyun 	}
1992*4882a593Smuzhiyun 	return sz;
1993*4882a593Smuzhiyun }
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun struct elf_note_info {
1996*4882a593Smuzhiyun 	struct memelfnote *notes;
1997*4882a593Smuzhiyun 	struct memelfnote *notes_files;
1998*4882a593Smuzhiyun 	struct elf_prstatus *prstatus;	/* NT_PRSTATUS */
1999*4882a593Smuzhiyun 	struct elf_prpsinfo *psinfo;	/* NT_PRPSINFO */
2000*4882a593Smuzhiyun 	struct list_head thread_list;
2001*4882a593Smuzhiyun 	elf_fpregset_t *fpu;
2002*4882a593Smuzhiyun 	user_siginfo_t csigdata;
2003*4882a593Smuzhiyun 	int thread_status_size;
2004*4882a593Smuzhiyun 	int numnote;
2005*4882a593Smuzhiyun };
2006*4882a593Smuzhiyun 
elf_note_info_init(struct elf_note_info * info)2007*4882a593Smuzhiyun static int elf_note_info_init(struct elf_note_info *info)
2008*4882a593Smuzhiyun {
2009*4882a593Smuzhiyun 	memset(info, 0, sizeof(*info));
2010*4882a593Smuzhiyun 	INIT_LIST_HEAD(&info->thread_list);
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun 	/* Allocate space for ELF notes */
2013*4882a593Smuzhiyun 	info->notes = kmalloc_array(8, sizeof(struct memelfnote), GFP_KERNEL);
2014*4882a593Smuzhiyun 	if (!info->notes)
2015*4882a593Smuzhiyun 		return 0;
2016*4882a593Smuzhiyun 	info->psinfo = kmalloc(sizeof(*info->psinfo), GFP_KERNEL);
2017*4882a593Smuzhiyun 	if (!info->psinfo)
2018*4882a593Smuzhiyun 		return 0;
2019*4882a593Smuzhiyun 	info->prstatus = kmalloc(sizeof(*info->prstatus), GFP_KERNEL);
2020*4882a593Smuzhiyun 	if (!info->prstatus)
2021*4882a593Smuzhiyun 		return 0;
2022*4882a593Smuzhiyun 	info->fpu = kmalloc(sizeof(*info->fpu), GFP_KERNEL);
2023*4882a593Smuzhiyun 	if (!info->fpu)
2024*4882a593Smuzhiyun 		return 0;
2025*4882a593Smuzhiyun 	return 1;
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun 
fill_note_info(struct elfhdr * elf,int phdrs,struct elf_note_info * info,struct coredump_params * cprm)2028*4882a593Smuzhiyun static int fill_note_info(struct elfhdr *elf, int phdrs,
2029*4882a593Smuzhiyun 			  struct elf_note_info *info,
2030*4882a593Smuzhiyun 			  struct coredump_params *cprm)
2031*4882a593Smuzhiyun {
2032*4882a593Smuzhiyun 	struct core_thread *ct;
2033*4882a593Smuzhiyun 	struct elf_thread_status *ets;
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 	if (!elf_note_info_init(info))
2036*4882a593Smuzhiyun 		return 0;
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	for (ct = current->mm->core_state->dumper.next;
2039*4882a593Smuzhiyun 					ct; ct = ct->next) {
2040*4882a593Smuzhiyun 		ets = kzalloc(sizeof(*ets), GFP_KERNEL);
2041*4882a593Smuzhiyun 		if (!ets)
2042*4882a593Smuzhiyun 			return 0;
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 		ets->thread = ct->task;
2045*4882a593Smuzhiyun 		list_add(&ets->list, &info->thread_list);
2046*4882a593Smuzhiyun 	}
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun 	list_for_each_entry(ets, &info->thread_list, list) {
2049*4882a593Smuzhiyun 		int sz;
2050*4882a593Smuzhiyun 
2051*4882a593Smuzhiyun 		sz = elf_dump_thread_status(cprm->siginfo->si_signo, ets);
2052*4882a593Smuzhiyun 		info->thread_status_size += sz;
2053*4882a593Smuzhiyun 	}
2054*4882a593Smuzhiyun 	/* now collect the dump for the current */
2055*4882a593Smuzhiyun 	memset(info->prstatus, 0, sizeof(*info->prstatus));
2056*4882a593Smuzhiyun 	fill_prstatus(info->prstatus, current, cprm->siginfo->si_signo);
2057*4882a593Smuzhiyun 	elf_core_copy_regs(&info->prstatus->pr_reg, cprm->regs);
2058*4882a593Smuzhiyun 
2059*4882a593Smuzhiyun 	/* Set up header */
2060*4882a593Smuzhiyun 	fill_elf_header(elf, phdrs, ELF_ARCH, ELF_CORE_EFLAGS);
2061*4882a593Smuzhiyun 
2062*4882a593Smuzhiyun 	/*
2063*4882a593Smuzhiyun 	 * Set up the notes in similar form to SVR4 core dumps made
2064*4882a593Smuzhiyun 	 * with info from their /proc.
2065*4882a593Smuzhiyun 	 */
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun 	fill_note(info->notes + 0, "CORE", NT_PRSTATUS,
2068*4882a593Smuzhiyun 		  sizeof(*info->prstatus), info->prstatus);
2069*4882a593Smuzhiyun 	fill_psinfo(info->psinfo, current->group_leader, current->mm);
2070*4882a593Smuzhiyun 	fill_note(info->notes + 1, "CORE", NT_PRPSINFO,
2071*4882a593Smuzhiyun 		  sizeof(*info->psinfo), info->psinfo);
2072*4882a593Smuzhiyun 
2073*4882a593Smuzhiyun 	fill_siginfo_note(info->notes + 2, &info->csigdata, cprm->siginfo);
2074*4882a593Smuzhiyun 	fill_auxv_note(info->notes + 3, current->mm);
2075*4882a593Smuzhiyun 	info->numnote = 4;
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun 	if (fill_files_note(info->notes + info->numnote) == 0) {
2078*4882a593Smuzhiyun 		info->notes_files = info->notes + info->numnote;
2079*4882a593Smuzhiyun 		info->numnote++;
2080*4882a593Smuzhiyun 	}
2081*4882a593Smuzhiyun 
2082*4882a593Smuzhiyun 	/* Try to dump the FPU. */
2083*4882a593Smuzhiyun 	info->prstatus->pr_fpvalid =
2084*4882a593Smuzhiyun 		elf_core_copy_task_fpregs(current, cprm->regs, info->fpu);
2085*4882a593Smuzhiyun 	if (info->prstatus->pr_fpvalid)
2086*4882a593Smuzhiyun 		fill_note(info->notes + info->numnote++,
2087*4882a593Smuzhiyun 			  "CORE", NT_PRFPREG, sizeof(*info->fpu), info->fpu);
2088*4882a593Smuzhiyun 	return 1;
2089*4882a593Smuzhiyun }
2090*4882a593Smuzhiyun 
get_note_info_size(struct elf_note_info * info)2091*4882a593Smuzhiyun static size_t get_note_info_size(struct elf_note_info *info)
2092*4882a593Smuzhiyun {
2093*4882a593Smuzhiyun 	int sz = 0;
2094*4882a593Smuzhiyun 	int i;
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun 	for (i = 0; i < info->numnote; i++)
2097*4882a593Smuzhiyun 		sz += notesize(info->notes + i);
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	sz += info->thread_status_size;
2100*4882a593Smuzhiyun 
2101*4882a593Smuzhiyun 	return sz;
2102*4882a593Smuzhiyun }
2103*4882a593Smuzhiyun 
write_note_info(struct elf_note_info * info,struct coredump_params * cprm)2104*4882a593Smuzhiyun static int write_note_info(struct elf_note_info *info,
2105*4882a593Smuzhiyun 			   struct coredump_params *cprm)
2106*4882a593Smuzhiyun {
2107*4882a593Smuzhiyun 	struct elf_thread_status *ets;
2108*4882a593Smuzhiyun 	int i;
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	for (i = 0; i < info->numnote; i++)
2111*4882a593Smuzhiyun 		if (!writenote(info->notes + i, cprm))
2112*4882a593Smuzhiyun 			return 0;
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun 	/* write out the thread status notes section */
2115*4882a593Smuzhiyun 	list_for_each_entry(ets, &info->thread_list, list) {
2116*4882a593Smuzhiyun 		for (i = 0; i < ets->num_notes; i++)
2117*4882a593Smuzhiyun 			if (!writenote(&ets->notes[i], cprm))
2118*4882a593Smuzhiyun 				return 0;
2119*4882a593Smuzhiyun 	}
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 	return 1;
2122*4882a593Smuzhiyun }
2123*4882a593Smuzhiyun 
free_note_info(struct elf_note_info * info)2124*4882a593Smuzhiyun static void free_note_info(struct elf_note_info *info)
2125*4882a593Smuzhiyun {
2126*4882a593Smuzhiyun 	while (!list_empty(&info->thread_list)) {
2127*4882a593Smuzhiyun 		struct list_head *tmp = info->thread_list.next;
2128*4882a593Smuzhiyun 		list_del(tmp);
2129*4882a593Smuzhiyun 		kfree(list_entry(tmp, struct elf_thread_status, list));
2130*4882a593Smuzhiyun 	}
2131*4882a593Smuzhiyun 
2132*4882a593Smuzhiyun 	/* Free data possibly allocated by fill_files_note(): */
2133*4882a593Smuzhiyun 	if (info->notes_files)
2134*4882a593Smuzhiyun 		kvfree(info->notes_files->data);
2135*4882a593Smuzhiyun 
2136*4882a593Smuzhiyun 	kfree(info->prstatus);
2137*4882a593Smuzhiyun 	kfree(info->psinfo);
2138*4882a593Smuzhiyun 	kfree(info->notes);
2139*4882a593Smuzhiyun 	kfree(info->fpu);
2140*4882a593Smuzhiyun }
2141*4882a593Smuzhiyun 
2142*4882a593Smuzhiyun #endif
2143*4882a593Smuzhiyun 
fill_extnum_info(struct elfhdr * elf,struct elf_shdr * shdr4extnum,elf_addr_t e_shoff,int segs)2144*4882a593Smuzhiyun static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
2145*4882a593Smuzhiyun 			     elf_addr_t e_shoff, int segs)
2146*4882a593Smuzhiyun {
2147*4882a593Smuzhiyun 	elf->e_shoff = e_shoff;
2148*4882a593Smuzhiyun 	elf->e_shentsize = sizeof(*shdr4extnum);
2149*4882a593Smuzhiyun 	elf->e_shnum = 1;
2150*4882a593Smuzhiyun 	elf->e_shstrndx = SHN_UNDEF;
2151*4882a593Smuzhiyun 
2152*4882a593Smuzhiyun 	memset(shdr4extnum, 0, sizeof(*shdr4extnum));
2153*4882a593Smuzhiyun 
2154*4882a593Smuzhiyun 	shdr4extnum->sh_type = SHT_NULL;
2155*4882a593Smuzhiyun 	shdr4extnum->sh_size = elf->e_shnum;
2156*4882a593Smuzhiyun 	shdr4extnum->sh_link = elf->e_shstrndx;
2157*4882a593Smuzhiyun 	shdr4extnum->sh_info = segs;
2158*4882a593Smuzhiyun }
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun /*
2161*4882a593Smuzhiyun  * Actual dumper
2162*4882a593Smuzhiyun  *
2163*4882a593Smuzhiyun  * This is a two-pass process; first we find the offsets of the bits,
2164*4882a593Smuzhiyun  * and then they are actually written out.  If we run out of core limit
2165*4882a593Smuzhiyun  * we just truncate.
2166*4882a593Smuzhiyun  */
elf_core_dump(struct coredump_params * cprm)2167*4882a593Smuzhiyun static int elf_core_dump(struct coredump_params *cprm)
2168*4882a593Smuzhiyun {
2169*4882a593Smuzhiyun 	int has_dumped = 0;
2170*4882a593Smuzhiyun 	int vma_count, segs, i;
2171*4882a593Smuzhiyun 	size_t vma_data_size;
2172*4882a593Smuzhiyun 	struct elfhdr elf;
2173*4882a593Smuzhiyun 	loff_t offset = 0, dataoff;
2174*4882a593Smuzhiyun 	struct elf_note_info info = { };
2175*4882a593Smuzhiyun 	struct elf_phdr *phdr4note = NULL;
2176*4882a593Smuzhiyun 	struct elf_shdr *shdr4extnum = NULL;
2177*4882a593Smuzhiyun 	Elf_Half e_phnum;
2178*4882a593Smuzhiyun 	elf_addr_t e_shoff;
2179*4882a593Smuzhiyun 	struct core_vma_metadata *vma_meta;
2180*4882a593Smuzhiyun 
2181*4882a593Smuzhiyun 	if (dump_vma_snapshot(cprm, &vma_count, &vma_meta, &vma_data_size))
2182*4882a593Smuzhiyun 		return 0;
2183*4882a593Smuzhiyun 
2184*4882a593Smuzhiyun 	/*
2185*4882a593Smuzhiyun 	 * The number of segs are recored into ELF header as 16bit value.
2186*4882a593Smuzhiyun 	 * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here.
2187*4882a593Smuzhiyun 	 */
2188*4882a593Smuzhiyun 	segs = vma_count + elf_core_extra_phdrs();
2189*4882a593Smuzhiyun 
2190*4882a593Smuzhiyun 	/* for notes section */
2191*4882a593Smuzhiyun 	segs++;
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun 	/* If segs > PN_XNUM(0xffff), then e_phnum overflows. To avoid
2194*4882a593Smuzhiyun 	 * this, kernel supports extended numbering. Have a look at
2195*4882a593Smuzhiyun 	 * include/linux/elf.h for further information. */
2196*4882a593Smuzhiyun 	e_phnum = segs > PN_XNUM ? PN_XNUM : segs;
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun 	/*
2199*4882a593Smuzhiyun 	 * Collect all the non-memory information about the process for the
2200*4882a593Smuzhiyun 	 * notes.  This also sets up the file header.
2201*4882a593Smuzhiyun 	 */
2202*4882a593Smuzhiyun 	if (!fill_note_info(&elf, e_phnum, &info, cprm))
2203*4882a593Smuzhiyun 		goto end_coredump;
2204*4882a593Smuzhiyun 
2205*4882a593Smuzhiyun 	has_dumped = 1;
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun 	offset += sizeof(elf);				/* Elf header */
2208*4882a593Smuzhiyun 	offset += segs * sizeof(struct elf_phdr);	/* Program headers */
2209*4882a593Smuzhiyun 
2210*4882a593Smuzhiyun 	/* Write notes phdr entry */
2211*4882a593Smuzhiyun 	{
2212*4882a593Smuzhiyun 		size_t sz = get_note_info_size(&info);
2213*4882a593Smuzhiyun 
2214*4882a593Smuzhiyun 		sz += elf_coredump_extra_notes_size();
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 		phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
2217*4882a593Smuzhiyun 		if (!phdr4note)
2218*4882a593Smuzhiyun 			goto end_coredump;
2219*4882a593Smuzhiyun 
2220*4882a593Smuzhiyun 		fill_elf_note_phdr(phdr4note, sz, offset);
2221*4882a593Smuzhiyun 		offset += sz;
2222*4882a593Smuzhiyun 	}
2223*4882a593Smuzhiyun 
2224*4882a593Smuzhiyun 	dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun 	offset += vma_data_size;
2227*4882a593Smuzhiyun 	offset += elf_core_extra_data_size();
2228*4882a593Smuzhiyun 	e_shoff = offset;
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun 	if (e_phnum == PN_XNUM) {
2231*4882a593Smuzhiyun 		shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
2232*4882a593Smuzhiyun 		if (!shdr4extnum)
2233*4882a593Smuzhiyun 			goto end_coredump;
2234*4882a593Smuzhiyun 		fill_extnum_info(&elf, shdr4extnum, e_shoff, segs);
2235*4882a593Smuzhiyun 	}
2236*4882a593Smuzhiyun 
2237*4882a593Smuzhiyun 	offset = dataoff;
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun 	if (!dump_emit(cprm, &elf, sizeof(elf)))
2240*4882a593Smuzhiyun 		goto end_coredump;
2241*4882a593Smuzhiyun 
2242*4882a593Smuzhiyun 	if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note)))
2243*4882a593Smuzhiyun 		goto end_coredump;
2244*4882a593Smuzhiyun 
2245*4882a593Smuzhiyun 	/* Write program headers for segments dump */
2246*4882a593Smuzhiyun 	for (i = 0; i < vma_count; i++) {
2247*4882a593Smuzhiyun 		struct core_vma_metadata *meta = vma_meta + i;
2248*4882a593Smuzhiyun 		struct elf_phdr phdr;
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 		phdr.p_type = PT_LOAD;
2251*4882a593Smuzhiyun 		phdr.p_offset = offset;
2252*4882a593Smuzhiyun 		phdr.p_vaddr = meta->start;
2253*4882a593Smuzhiyun 		phdr.p_paddr = 0;
2254*4882a593Smuzhiyun 		phdr.p_filesz = meta->dump_size;
2255*4882a593Smuzhiyun 		phdr.p_memsz = meta->end - meta->start;
2256*4882a593Smuzhiyun 		offset += phdr.p_filesz;
2257*4882a593Smuzhiyun 		phdr.p_flags = 0;
2258*4882a593Smuzhiyun 		if (meta->flags & VM_READ)
2259*4882a593Smuzhiyun 			phdr.p_flags |= PF_R;
2260*4882a593Smuzhiyun 		if (meta->flags & VM_WRITE)
2261*4882a593Smuzhiyun 			phdr.p_flags |= PF_W;
2262*4882a593Smuzhiyun 		if (meta->flags & VM_EXEC)
2263*4882a593Smuzhiyun 			phdr.p_flags |= PF_X;
2264*4882a593Smuzhiyun 		phdr.p_align = ELF_EXEC_PAGESIZE;
2265*4882a593Smuzhiyun 
2266*4882a593Smuzhiyun 		if (!dump_emit(cprm, &phdr, sizeof(phdr)))
2267*4882a593Smuzhiyun 			goto end_coredump;
2268*4882a593Smuzhiyun 	}
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun 	if (!elf_core_write_extra_phdrs(cprm, offset))
2271*4882a593Smuzhiyun 		goto end_coredump;
2272*4882a593Smuzhiyun 
2273*4882a593Smuzhiyun  	/* write out the notes section */
2274*4882a593Smuzhiyun 	if (!write_note_info(&info, cprm))
2275*4882a593Smuzhiyun 		goto end_coredump;
2276*4882a593Smuzhiyun 
2277*4882a593Smuzhiyun 	if (elf_coredump_extra_notes_write(cprm))
2278*4882a593Smuzhiyun 		goto end_coredump;
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 	/* Align to page */
2281*4882a593Smuzhiyun 	if (!dump_skip(cprm, dataoff - cprm->pos))
2282*4882a593Smuzhiyun 		goto end_coredump;
2283*4882a593Smuzhiyun 
2284*4882a593Smuzhiyun 	for (i = 0; i < vma_count; i++) {
2285*4882a593Smuzhiyun 		struct core_vma_metadata *meta = vma_meta + i;
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun 		if (!dump_user_range(cprm, meta->start, meta->dump_size))
2288*4882a593Smuzhiyun 			goto end_coredump;
2289*4882a593Smuzhiyun 	}
2290*4882a593Smuzhiyun 	dump_truncate(cprm);
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun 	if (!elf_core_write_extra_data(cprm))
2293*4882a593Smuzhiyun 		goto end_coredump;
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 	if (e_phnum == PN_XNUM) {
2296*4882a593Smuzhiyun 		if (!dump_emit(cprm, shdr4extnum, sizeof(*shdr4extnum)))
2297*4882a593Smuzhiyun 			goto end_coredump;
2298*4882a593Smuzhiyun 	}
2299*4882a593Smuzhiyun 
2300*4882a593Smuzhiyun end_coredump:
2301*4882a593Smuzhiyun 	free_note_info(&info);
2302*4882a593Smuzhiyun 	kfree(shdr4extnum);
2303*4882a593Smuzhiyun 	kvfree(vma_meta);
2304*4882a593Smuzhiyun 	kfree(phdr4note);
2305*4882a593Smuzhiyun 	return has_dumped;
2306*4882a593Smuzhiyun }
2307*4882a593Smuzhiyun 
2308*4882a593Smuzhiyun #endif		/* CONFIG_ELF_CORE */
2309*4882a593Smuzhiyun 
init_elf_binfmt(void)2310*4882a593Smuzhiyun static int __init init_elf_binfmt(void)
2311*4882a593Smuzhiyun {
2312*4882a593Smuzhiyun 	register_binfmt(&elf_format);
2313*4882a593Smuzhiyun 	return 0;
2314*4882a593Smuzhiyun }
2315*4882a593Smuzhiyun 
exit_elf_binfmt(void)2316*4882a593Smuzhiyun static void __exit exit_elf_binfmt(void)
2317*4882a593Smuzhiyun {
2318*4882a593Smuzhiyun 	/* Remove the COFF and ELF loaders. */
2319*4882a593Smuzhiyun 	unregister_binfmt(&elf_format);
2320*4882a593Smuzhiyun }
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun core_initcall(init_elf_binfmt);
2323*4882a593Smuzhiyun module_exit(exit_elf_binfmt);
2324*4882a593Smuzhiyun MODULE_LICENSE("GPL");
2325