Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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