xref: /openbmc/linux/arch/arm/kernel/elf.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2ecea4ab6SPaul Gortmaker #include <linux/export.h>
38ec53663SRussell King #include <linux/sched.h>
48ec53663SRussell King #include <linux/personality.h>
58ec53663SRussell King #include <linux/binfmts.h>
68ec53663SRussell King #include <linux/elf.h>
7382e67aeSNicolas Pitre #include <linux/elf-fdpic.h>
89f97da78SDavid Howells #include <asm/system_info.h>
98ec53663SRussell King 
elf_check_arch(const struct elf32_hdr * x)108ec53663SRussell King int elf_check_arch(const struct elf32_hdr *x)
118ec53663SRussell King {
128ec53663SRussell King 	unsigned int eflags;
138ec53663SRussell King 
148ec53663SRussell King 	/* Make sure it's an ARM executable */
158ec53663SRussell King 	if (x->e_machine != EM_ARM)
168ec53663SRussell King 		return 0;
178ec53663SRussell King 
188ec53663SRussell King 	/* Make sure the entry address is reasonable */
198ec53663SRussell King 	if (x->e_entry & 1) {
208ec53663SRussell King 		if (!(elf_hwcap & HWCAP_THUMB))
218ec53663SRussell King 			return 0;
228ec53663SRussell King 	} else if (x->e_entry & 3)
238ec53663SRussell King 		return 0;
248ec53663SRussell King 
258ec53663SRussell King 	eflags = x->e_flags;
268ec53663SRussell King 	if ((eflags & EF_ARM_EABI_MASK) == EF_ARM_EABI_UNKNOWN) {
27d2ed5cb8SRussell King 		unsigned int flt_fmt;
28d2ed5cb8SRussell King 
298ec53663SRussell King 		/* APCS26 is only allowed if the CPU supports it */
308ec53663SRussell King 		if ((eflags & EF_ARM_APCS_26) && !(elf_hwcap & HWCAP_26BIT))
318ec53663SRussell King 			return 0;
328ec53663SRussell King 
33d2ed5cb8SRussell King 		flt_fmt = eflags & (EF_ARM_VFP_FLOAT | EF_ARM_SOFT_FLOAT);
34d2ed5cb8SRussell King 
358ec53663SRussell King 		/* VFP requires the supporting code */
36d2ed5cb8SRussell King 		if (flt_fmt == EF_ARM_VFP_FLOAT && !(elf_hwcap & HWCAP_VFP))
378ec53663SRussell King 			return 0;
388ec53663SRussell King 	}
398ec53663SRussell King 	return 1;
408ec53663SRussell King }
418ec53663SRussell King EXPORT_SYMBOL(elf_check_arch);
428ec53663SRussell King 
elf_set_personality(const struct elf32_hdr * x)438ec53663SRussell King void elf_set_personality(const struct elf32_hdr *x)
448ec53663SRussell King {
458ec53663SRussell King 	unsigned int eflags = x->e_flags;
465e143436SNicolas Pitre 	unsigned int personality = current->personality & ~PER_MASK;
475e143436SNicolas Pitre 
485e143436SNicolas Pitre 	/*
495e143436SNicolas Pitre 	 * We only support Linux ELF executables, so always set the
505e143436SNicolas Pitre 	 * personality to LINUX.
515e143436SNicolas Pitre 	 */
525e143436SNicolas Pitre 	personality |= PER_LINUX;
538ec53663SRussell King 
548ec53663SRussell King 	/*
558ec53663SRussell King 	 * APCS-26 is only valid for OABI executables
568ec53663SRussell King 	 */
575e143436SNicolas Pitre 	if ((eflags & EF_ARM_EABI_MASK) == EF_ARM_EABI_UNKNOWN &&
585e143436SNicolas Pitre 	    (eflags & EF_ARM_APCS_26))
595e143436SNicolas Pitre 		personality &= ~ADDR_LIMIT_32BIT;
605e143436SNicolas Pitre 	else
615e143436SNicolas Pitre 		personality |= ADDR_LIMIT_32BIT;
628ec53663SRussell King 
638ec53663SRussell King 	set_personality(personality);
648ec53663SRussell King 
658ec53663SRussell King 	/*
668ec53663SRussell King 	 * Since the FPA coprocessor uses CP1 and CP2, and iWMMXt uses CP0
678ec53663SRussell King 	 * and CP1, we only enable access to the iWMMXt coprocessor if the
688ec53663SRussell King 	 * binary is EABI or softfloat (and thus, guaranteed not to use
698ec53663SRussell King 	 * FPA instructions.)
708ec53663SRussell King 	 */
718ec53663SRussell King 	if (elf_hwcap & HWCAP_IWMMXT &&
728ec53663SRussell King 	    eflags & (EF_ARM_EABI_MASK | EF_ARM_SOFT_FLOAT)) {
738ec53663SRussell King 		set_thread_flag(TIF_USING_IWMMXT);
748ec53663SRussell King 	} else {
758ec53663SRussell King 		clear_thread_flag(TIF_USING_IWMMXT);
768ec53663SRussell King 	}
778ec53663SRussell King }
788ec53663SRussell King EXPORT_SYMBOL(elf_set_personality);
798ec53663SRussell King 
808ec53663SRussell King /*
8178066055SKees Cook  * An executable for which elf_read_implies_exec() returns TRUE will
8278066055SKees Cook  * have the READ_IMPLIES_EXEC personality flag set automatically.
8378066055SKees Cook  *
8478066055SKees Cook  * The decision process for determining the results are:
8578066055SKees Cook  *
8678066055SKees Cook  *                 CPU: | lacks NX*  | has NX     |
8778066055SKees Cook  * ELF:                 |            |            |
8878066055SKees Cook  * ---------------------|------------|------------|
8978066055SKees Cook  * missing PT_GNU_STACK | exec-all   | exec-all   |
90*eaf3f9e6SKees Cook  * PT_GNU_STACK == RWX  | exec-all   | exec-stack |
9178066055SKees Cook  * PT_GNU_STACK == RW   | exec-all   | exec-none  |
9278066055SKees Cook  *
9378066055SKees Cook  *  exec-all  : all PROT_READ user mappings are executable, except when
9478066055SKees Cook  *              backed by files on a noexec-filesystem.
9578066055SKees Cook  *  exec-none : only PROT_EXEC user mappings are executable.
96*eaf3f9e6SKees Cook  *  exec-stack: only the stack and PROT_EXEC user mappings are executable.
9778066055SKees Cook  *
9878066055SKees Cook  *  *this column has no architectural effect: NX markings are ignored by
9978066055SKees Cook  *   hardware, but may have behavioral effects when "wants X" collides with
10078066055SKees Cook  *   "cannot be X" constraints in memory permission flags, as in
10178066055SKees Cook  *   https://lkml.kernel.org/r/20190418055759.GA3155@mellanox.com
10278066055SKees Cook  *
1038ec53663SRussell King  */
arm_elf_read_implies_exec(int executable_stack)104e71fd631SNicolas Pitre int arm_elf_read_implies_exec(int executable_stack)
1058ec53663SRussell King {
106*eaf3f9e6SKees Cook 	if (executable_stack == EXSTACK_DEFAULT)
1078ec53663SRussell King 		return 1;
1089da616fbSMakito SHIOKAWA 	if (cpu_architecture() < CPU_ARCH_ARMv6)
1098ec53663SRussell King 		return 1;
1108ec53663SRussell King 	return 0;
1118ec53663SRussell King }
1128ec53663SRussell King EXPORT_SYMBOL(arm_elf_read_implies_exec);
113382e67aeSNicolas Pitre 
114382e67aeSNicolas Pitre #if defined(CONFIG_MMU) && defined(CONFIG_BINFMT_ELF_FDPIC)
115382e67aeSNicolas Pitre 
elf_fdpic_arch_lay_out_mm(struct elf_fdpic_params * exec_params,struct elf_fdpic_params * interp_params,unsigned long * start_stack,unsigned long * start_brk)116382e67aeSNicolas Pitre void elf_fdpic_arch_lay_out_mm(struct elf_fdpic_params *exec_params,
117382e67aeSNicolas Pitre 			       struct elf_fdpic_params *interp_params,
118382e67aeSNicolas Pitre 			       unsigned long *start_stack,
119382e67aeSNicolas Pitre 			       unsigned long *start_brk)
120382e67aeSNicolas Pitre {
121382e67aeSNicolas Pitre 	elf_set_personality(&exec_params->hdr);
122382e67aeSNicolas Pitre 
123382e67aeSNicolas Pitre 	exec_params->load_addr = 0x8000;
124382e67aeSNicolas Pitre 	interp_params->load_addr = ELF_ET_DYN_BASE;
125382e67aeSNicolas Pitre 	*start_stack = TASK_SIZE - SZ_16M;
126382e67aeSNicolas Pitre 
127382e67aeSNicolas Pitre 	if ((exec_params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) == ELF_FDPIC_FLAG_INDEPENDENT) {
128382e67aeSNicolas Pitre 		exec_params->flags &= ~ELF_FDPIC_FLAG_ARRANGEMENT;
129382e67aeSNicolas Pitre 		exec_params->flags |= ELF_FDPIC_FLAG_CONSTDISP;
130382e67aeSNicolas Pitre 	}
131382e67aeSNicolas Pitre }
132382e67aeSNicolas Pitre 
133382e67aeSNicolas Pitre #endif
134