arm64: add support for WXN
Subsystems (4)
v6 cover arm64: add support for WXN
This series covers the remaining changes that are needed to enable WXN support on arm64 now that a lot of the prerequisite work has been queued up. WXN support is desirable for robustness, given that writable, executable mappings of memory are too easy to subvert, and in the kernel, we never rely on such mappings anyway.
Setting SCTLR_ELx.WXN makes all writable mappings implicitly non-executable, and when set at EL1, it affects EL0 as well as EL1. This means we need some diligence on the part of user space, but fortunately, most JITs and other user space components that actually need to manipulate the contents of their own executable code use split views on the same memory, or switch between RW- and R-X and back. (One notable exception is V8, which recently switched back to a full RWX mappings based JIT)
So on the user space side, we need a couple of minor tweaks to validate the mmap()/mprotect() arguments when WXN is in effect, and to handle any faults that might occur on such mappings.
On the kernel side, it is mostly about ensuring that we don't rely on writable, executable mappings, even during early boot. So for this reason, the two remaining sequences that create the kernel mapping are merged, moving the more elaborate logic to set the right attributes into a C implementation that executes from the ID map. This also allows us to move the relocation code into C as well, which only lived in asm because it runs before we have a stack.
Finally, some cleanups are provided for the KASLR code, mainly to ensure that the early code's decision to use nG mappings or not is based on the exact same criteria.
(v5 was a subset of v4 without the WXN specific pieces)
Changes since v4: [0]
- don't move __ro_after_init section now that we no longer need to,
- don't complicate the asm kernel mapping routines further, but instead, merge the two existing passes into one implemented in C,
- deal with rodata=off on WXN enabled builds (i.e., turn off WXN as well),
- add some acks from Kees
[0] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Ard Biesheuvel (9):
arm64: kaslr: use an ordinary command line param for nokaslr
arm64: kaslr: don't pretend KASLR is enabled if offset <
MIN_KIMG_ALIGN
arm64: kaslr: drop special case for ThunderX in kaslr_requires_kpti()
arm64: head: allocate more pages for the kernel mapping
arm64: head: move early kernel mapping and relocation code to C code
arm64: mm: avoid fixmap for early swapper_pg_dir updates
arm64: mm: omit redundant remap of kernel image
mm: add arch hook to validate mmap() prot flags
arm64: mm: add support for WXN memory translation attribute
arch/arm64/Kconfig | 11 + arch/arm64/include/asm/cpufeature.h | 9 + arch/arm64/include/asm/kasan.h | 2 - arch/arm64/include/asm/kernel-pgtable.h | 11 +- arch/arm64/include/asm/memory.h | 11 + arch/arm64/include/asm/mman.h | 36 ++ arch/arm64/include/asm/mmu.h | 2 +- arch/arm64/include/asm/mmu_context.h | 30 +- arch/arm64/include/asm/pgtable-prot.h | 2 + arch/arm64/kernel/Makefile | 4 +- arch/arm64/kernel/cpufeature.c | 14 +- arch/arm64/kernel/head.S | 157 +------- arch/arm64/kernel/idreg-override.c | 15 - arch/arm64/kernel/image-vars.h | 17 + arch/arm64/kernel/kaslr.c | 8 +- arch/arm64/kernel/pi/Makefile | 2 +- arch/arm64/kernel/pi/early_map_kernel.c | 381 ++++++++++++++++++++ arch/arm64/kernel/pi/kaslr_early.c | 112 ------ arch/arm64/kernel/vmlinux.lds.S | 13 +- arch/arm64/mm/kasan_init.c | 15 - arch/arm64/mm/mmu.c | 150 +++----- arch/arm64/mm/proc.S | 24 ++ include/linux/mman.h | 15 + mm/mmap.c | 3 + 24 files changed, 630 insertions(+), 414 deletions(-) create mode 100644 arch/arm64/kernel/pi/early_map_kernel.c delete mode 100644 arch/arm64/kernel/pi/kaslr_early.c
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 1/9 arm64: kaslr: use an ordinary command line param for nokaslr
We no longer need to rely on the idreg-override hack to parse the 'nokaslr' command line parameter, given that we now parse it way earlier already, before the kernel is even mapped. So for later access to its value, we can just use core_param() instead.
--- arch/arm64/kernel/idreg-override.c | 15 --------------- arch/arm64/kernel/kaslr.c | 6 ++++-- 2 files changed, 4 insertions(+), 17 deletions(-)
arch/arm64/kernel/idreg-override.c +0 -15
@@ -81,25 +81,11 @@ static const struct ftr_set_desc isar2 __initconst = { }, }; -extern struct arm64_ftr_override kaslr_feature_override; - -static const struct ftr_set_desc kaslr__initconst = { - .name ="kaslr" , -#ifdef CONFIG_RANDOMIZE_BASE - .override = &kaslr_feature_override, -#endif - .fields = { - {"disabled" ,0 }, - {} - }, -}; -static const struct ftr_set_desc *const regs[]__initconst = { &mmfr1, &pfr1, &isar1, &isar2, - &kaslr, }; static const struct {
@@ -114,7 +100,6 @@ static const struct { "id_aa64isar1.api=0 id_aa64isar1.apa=0 " "id_aa64isar2.gpa3=0 id_aa64isar2.apa3=0" }, { "arm64.nomte", "id_aa64pfr1.mte=0" }, - { "nokaslr", "kaslr.disabled=1" }, }; static int __init find_field(const char *cmdline,
arch/arm64/kernel/kaslr.c +4 -2
@@ -8,6 +8,7 @@#include <linux/init.h> #include <linux/libfdt.h> #include <linux/mm_types.h> +#include <linux/moduleparam.h> #include <linux/sched.h> #include <linux/types.h> #include <linux/pgtable.h>
@@ -23,7 +24,8 @@u64 __ro_after_init module_alloc_base;u16 __initdata memstart_offset_seed; -struct arm64_ftr_override kaslr_feature_override__initdata ; +static bool nokaslr; +core_param (nokaslr, nokaslr, bool,0 );static int __init kaslr_init (void ) {
@@ -36,7 +38,7 @@ static int __init kaslr_init(void)*/ module_alloc_base = (u64 )_etext -MODULES_VSIZE ; -if (kaslr_feature_override.val & kaslr_feature_override.mask &0xf ) { +if (nokaslr) {pr_info ("KASLR disabled on command line\n" );return 0 ; }
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v6 1/9] arm64: kaslr: use an ordinary command line param for nokaslr
Quoted 4 lines · Ard Biesheuvel
We no longer need to rely on the idreg-override hack to parse the 'nokaslr' command line parameter, given that we now parse it way earlier already, before the kernel is even mapped. So for later access to its value, we can just use
core_param()instead.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 2/9 arm64: kaslr: don't pretend KASLR is enabled if offset < MIN_KIMG_ALIGN
In reply to Ard BiesheuvelOur virtual KASLR displacement consists of a fully randomized multiple of 2 MiB, combined with an offset that is equal to the physical placement modulo 2 MiB. This arrangement ensures that we can always use 2 MiB block mappings (or contiguous PTE mappings for 16k or 64k pages) to map the kernel.
This means that a KASLR offset of less than 2 MiB is simply the product of this physical displacement, and no randomization has actually taken place. So let's avoid misreporting this case as 'KASLR enabled'.
--- arch/arm64/include/asm/memory.h | 11 +++++++++++ arch/arm64/kernel/cpufeature.c | 2 +- arch/arm64/kernel/kaslr.c | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-)
arch/arm64/include/asm/memory.h +11 -0
@@ -172,6 +172,7 @@#include <linux/compiler.h> #include <linux/mmdebug.h> #include <linux/types.h> +#include <asm/boot.h> #include <asm/bug.h> #if VA_BITS >48
@@ -195,6 +196,16 @@ static inline unsigned long kaslr_offset(void)return kimage_vaddr -KIMAGE_VADDR ; } +static inline bool kaslr_enabled (void ) +{ +/* +* The KASLR offset modulo MIN_KIMG_ALIGN is taken from the physical +* placement of the image rather than from the seed, so a displacement +* of less than MIN_KIMG_ALIGN means that no seed was provided. +*/ +return kaslr_offset () >=MIN_KIMG_ALIGN ; +} +/* * Allow all memory at the discovery stage. We will clip it later. */
arch/arm64/kernel/cpufeature.c +1 -1
@@ -1562,7 +1562,7 @@ bool kaslr_requires_kpti(void)return false; } -return kaslr_offset () >0 ; +return kaslr_enabled (); }static bool __meltdown_safe = true;
arch/arm64/kernel/kaslr.c +1 -1
@@ -43,7 +43,7 @@ static int __init kaslr_init(void)return 0 ; } -if (!kaslr_offset ()) { +if (!kaslr_enabled ()) {pr_warn ("KASLR disabled due to lack of seed\n" );return 0 ; }
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v6 2/9] arm64: kaslr: don't pretend KASLR is enabled if offset < MIN_KIMG_ALIGN
Quoted 9 lines · Ard Biesheuvel
Our virtual KASLR displacement consists of a fully randomized multiple of 2 MiB, combined with an offset that is equal to the physical placement modulo 2 MiB. This arrangement ensures that we can always use 2 MiB block mappings (or contiguous PTE mappings for 16k or 64k pages) to map the kernel.
This means that a KASLR offset of less than 2 MiB is simply the product of this physical displacement, and no randomization has actually taken place. So let's avoid misreporting this case as 'KASLR enabled'.
Might be worth backporting to stable? Though the consequence is just that we might enable KPTI when we don't *need* it which is not the end of the world.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 3/9 arm64: kaslr: drop special case for ThunderX in kaslr_requires_kpti()
In reply to Ard BiesheuvelThunderX is an obsolete platform that shipped without support for the EFI_RNG_PROTOCOL in its firmware. Now that we no longer misidentify small KASLR offsets as randomization being enabled, we can drop the explicit check for ThunderX as well, given that KASLR is known to be unavailable on these systems.
Note that we never enable KPTI on these systems, in spite of what this function returns. The only penalty for getting it wrong (i.e., returning true here on a ThunderX system) is that we will end up using non-global mappings for the kernel pointlessly.
--- arch/arm64/kernel/cpufeature.c | 12 ------------ 1 file changed, 12 deletions(-)
arch/arm64/kernel/cpufeature.c +0 -12
@@ -1550,18 +1550,6 @@ bool kaslr_requires_kpti(void)return false; } -/* -* Systems affected by Cavium erratum 24756 are incompatible -* with KPTI. -*/ -if (IS_ENABLED (CONFIG_CAVIUM_ERRATUM_27456 )) { -extern const struct midr_range cavium_erratum_27456_cpus[]; - -if (is_midr_in_range_list (read_cpuid_id (), - cavium_erratum_27456_cpus)) -return false; - } -return kaslr_enabled (); }
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 4/9 arm64: head: allocate more pages for the kernel mapping
In reply to Ard BiesheuvelIn preparation for switching to an early kernel mapping routine that maps each segment according to its precise boundaries, and with the correct attributes, let's allocate some extra pages for page tables for the 4k page size configuration. This is necessary because the start and end of each segment may not be aligned to the block size, and so we'll need an extra page table at each segment boundary.
--- arch/arm64/include/asm/kernel-pgtable.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kernel-pgtable.h +10 -1
@@ -85,7 +85,7 @@ + EARLY_PGDS((vstart), (vend)) /* each PGDIR needs a next level page table */ \ + EARLY_PUDS((vstart), (vend)) /* each PUD needs a next level page table */ \ + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */ -#define INIT_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR, _end)) +#define INIT_DIR_SIZE (PAGE_SIZE * (EARLY_PAGES(KIMAGE_VADDR, _end) + EARLY_SEGMENT_EXTRA_PAGES))/* the initial ID map may need two extra pages if it needs to be extended */ #if VA_BITS <48
@@ -106,6 +106,15 @@#define SWAPPER_TABLE_SHIFT PMD_SHIFT#endif +/* The number of segments in the kernel image (text, rodata, inittext, initdata, data+bss) */ +#define KERNEL_SEGMENT_COUNT 5 + +#if SWAPPER_BLOCK_SIZE >SEGMENT_ALIGN +#define EARLY_SEGMENT_EXTRA_PAGES (KERNEL_SEGMENT_COUNT + 1) +#else +#define EARLY_SEGMENT_EXTRA_PAGES 0 +#endif +/* * Initial memory map attributes. */
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 5/9 arm64: head: move early kernel mapping and relocation code to C code
In reply to Ard BiesheuvelThe asm version of the kernel mapping code works fine for creating a coarse grained identity map, but for mapping the kernel down to its exact boundaries with the right attributes, it is not suitable. This is why we create a preliminary RWX kernel mapping first, and then rebuild it from scratch later on.
So let's reimplement this, and along with it the relocation routines, in C, in a way that will make it unnecessary to create the kernel page tables yet another time in paging_init().
--- arch/arm64/include/asm/pgtable-prot.h | 2 + arch/arm64/kernel/Makefile | 4 +- arch/arm64/kernel/head.S | 157 +-------- arch/arm64/kernel/image-vars.h | 16 + arch/arm64/kernel/pi/Makefile | 2 +- arch/arm64/kernel/pi/early_map_kernel.c | 368 ++++++++++++++++++++ arch/arm64/kernel/pi/kaslr_early.c | 112 ------ arch/arm64/kernel/vmlinux.lds.S | 13 +- arch/arm64/mm/proc.S | 1 + 9 files changed, 410 insertions(+), 265 deletions(-)
arch/arm64/include/asm/pgtable-prot.h +2 -0
@@ -32,7 +32,9 @@#include <asm/cpufeature.h> #include <asm/pgtable-types.h> +#ifndef arm64_use_ng_mappingsextern bool arm64_use_ng_mappings; +#endif #define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
arch/arm64/kernel/Makefile +2 -2
@@ -29,7 +29,7 @@ obj-y := debug-monitors.o entry.o irq.o fpsimd.o \ cpufeature.o alternative.o cacheinfo.o \ smp.o smp_spin_table.o topology.o smccc-call.o \ syscall.o proton-pack.o idreg-override.o idle.o \ - patching.o + patching.o pi/ targets += efi-entry.o
@@ -59,7 +59,7 @@ obj-$(CONFIG_ACPI) += acpi.o obj-$(CONFIG_ACPI_NUMA) += acpi_numa.o obj-$(CONFIG_ARM64_ACPI_PARKING_PROTOCOL) += acpi_parking_protocol.o obj-$(CONFIG_PARAVIRT) += paravirt.o -obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o pi/ +obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o obj-$(CONFIG_ELF_CORE) += elfcore.o obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \
arch/arm64/kernel/head.S +12 -145
212 lines not on this page · raw message
arch/arm64/kernel/image-vars.h +16 -0
@@ -54,6 +54,22 @@ PROVIDE(__pi___memcpy = __pi_memcpy);PROVIDE (__pi___memmove = __pi_memmove);PROVIDE (__pi___memset = __pi_memset); +/* +* The symbols below are used by the early C kernel mapping code. +*/ +PROVIDE (__pi_init_pg_dir = init_pg_dir); +PROVIDE (__pi_init_pg_end = init_pg_end); + +PROVIDE (__pi__text = _text); +PROVIDE (__pi__stext = _stext); +PROVIDE (__pi__etext = _etext); +PROVIDE (__pi___start_rodata = __start_rodata); +PROVIDE (__pi___inittext_begin = __inittext_begin); +PROVIDE (__pi___inittext_end = __inittext_end); +PROVIDE (__pi___initdata_begin = __initdata_begin); +PROVIDE (__pi___initdata_end = __initdata_end); +PROVIDE (__pi__data = _data); +#ifdef CONFIG_KVM /*
arch/arm64/kernel/pi/Makefile +1 -1
@@ -29,5 +29,5 @@ $(obj)/%.pi.o: $(obj)/%.o FORCE $(obj)/lib-%.o: $(srctree)/lib/%.c FORCE $(call if_changed_rule,cc_o_c) -obj-y := kaslr_early.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o +obj-y := early_map_kernel.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o extra-y := $(patsubst %.pi.o,%.o,$(obj-y))
arch/arm64/kernel/pi/early_map_kernel.c new file +368 -0
368 lines not on this page · raw message
arch/arm64/kernel/pi/kaslr_early.c deleted +0 -112
112 lines not on this page · raw message
arch/arm64/kernel/vmlinux.lds.S +8 -5
@@ -257,15 +257,15 @@ SECTIONS HYPERVISOR_RELOC_SECTION .rela.dyn : ALIGN(8) { - __rela_start = .; + __pi_rela_start = .; *(.rela .rela*) - __rela_end = .; + __pi_rela_end = .; } .relr.dyn : ALIGN(8) { - __relr_start = .; + __pi_relr_start = .; *(.relr.dyn) - __relr_end = .; + __pi_relr_end = .; } . = ALIGN(SEGMENT_ALIGN);
@@ -309,11 +309,14 @@ SECTIONS BSS_SECTION(SBSS_ALIGN, 0, 0) - . = ALIGN(PAGE_SIZE); + . = ALIGN(SEGMENT_ALIGN); init_pg_dir = .; . += INIT_DIR_SIZE; init_pg_end = .; + . += SZ_4K; + primary_init_stack = .; + . = ALIGN(SEGMENT_ALIGN); __pecoff_data_size = ABSOLUTE(. - __initdata_begin); _end = .;
arch/arm64/mm/proc.S +1 -0
@@ -196,6 +196,7 @@ SYM_FUNC_START(idmap_cpu_replace_ttbr1) ret SYM_FUNC_END(idmap_cpu_replace_ttbr1) +SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1) .popsection #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 6/9 arm64: mm: avoid fixmap for early swapper_pg_dir updates
In reply to Ard BiesheuvelEarly in the boot, when .rodata is still writable, we can poke swapper_pg_dir entries directly, and there is no need to go through the fixmap. After a future patch, we will enter the kernel with swapper_pg_dir already active, and early swapper_pg_dir updates for creating the fixmap page table hierarchy itself cannot go through the fixmap for obvious reaons. So let's keep track of whether rodata is writable, and update the descriptor directly in that case.
As the same reasoning applies to early KASAN init, make the function noinstr as well.
--- arch/arm64/mm/mmu.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
arch/arm64/mm/mmu.c +15 -1
@@ -58,6 +58,8 @@ EXPORT_SYMBOL(kimage_voffset);u32 __boot_cpu_mode[] = {BOOT_CPU_MODE_EL2 ,BOOT_CPU_MODE_EL1 }; +static bool rodata_is_rw__ro_after_init = true; +/* * The booting CPU updates the failed status @__early_cpu_boot_status, * with MMU turned off.
@@ -78,10 +80,21 @@ static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;static DEFINE_SPINLOCK (swapper_pgdir_lock);static DEFINE_MUTEX (fixmap_lock); -void set_swapper_pgd (pgd_t *pgdp,pgd_t pgd) +void noinstr set_swapper_pgd(pgd_t *pgdp, pgd_t pgd) {pgd_t *fixmap_pgdp; +/* +* Don't bother with the fixmap if swapper_pg_dir is still mapped +* writable in the kernel mapping. +*/ +if (rodata_is_rw) { +WRITE_ONCE (*pgdp, pgd); +dsb (ishst); +isb (); +return ; + } +spin_lock (&swapper_pgdir_lock); fixmap_pgdp =pgd_set_fixmap (__pa_symbol (pgdp));WRITE_ONCE (*fixmap_pgdp, pgd);
@@ -613,6 +626,7 @@ void mark_rodata_ro(void)* to cover NOTES and EXCEPTION_TABLE. */ section_size = (unsigned long )__init_begin - (unsigned long )__start_rodata; +WRITE_ONCE (rodata_is_rw, false);update_mapping_prot (__pa_symbol (__start_rodata), (unsigned long )__start_rodata, section_size,PAGE_KERNEL_RO );
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 7/9 arm64: mm: omit redundant remap of kernel image
In reply to Ard BiesheuvelNow that the early kernel mapping is created with all the right attributes and segment boundaries, there is no longer a need to recreate it and switch to it. This also means we no longer have to copy the kasan shadow or some parts of the fixmap from one set of page tables to the other.
--- arch/arm64/include/asm/kasan.h | 2 - arch/arm64/include/asm/mmu.h | 2 +- arch/arm64/kernel/image-vars.h | 1 + arch/arm64/kernel/pi/early_map_kernel.c | 4 + arch/arm64/mm/kasan_init.c | 15 --- arch/arm64/mm/mmu.c | 101 +++----------------- 6 files changed, 17 insertions(+), 108 deletions(-)
arch/arm64/include/asm/kasan.h +0 -2
@@ -36,12 +36,10 @@ void kasan_init(void);#define _KASAN_SHADOW_START (va) (KASAN_SHADOW_END - (1UL << ((va) - KASAN_SHADOW_SCALE_SHIFT)))#define KASAN_SHADOW_START _KASAN_SHADOW_START(vabits_actual) -void kasan_copy_shadow (pgd_t *pgdir); asmlinkage void kasan_early_init(void);#else static inline void kasan_init (void ) { } -static inline void kasan_copy_shadow (pgd_t *pgdir) { }#endif #endif
arch/arm64/include/asm/mmu.h +1 -1
@@ -73,7 +73,7 @@ extern void mark_linear_text_alias_ro(void);extern bool kaslr_requires_kpti (void );#define INIT_MM_CONTEXT (name) \ - .pgd = init_pg_dir, + .pgd = swapper_pg_dir,#endif /* !__ASSEMBLY__ */ #endif
arch/arm64/kernel/image-vars.h +1 -0
@@ -59,6 +59,7 @@ PROVIDE(__pi___memset = __pi_memset);*/ PROVIDE (__pi_init_pg_dir = init_pg_dir);PROVIDE (__pi_init_pg_end = init_pg_end); +PROVIDE (__pi_swapper_pg_dir = swapper_pg_dir);PROVIDE (__pi__text = _text);PROVIDE (__pi__stext = _stext);
arch/arm64/kernel/pi/early_map_kernel.c +4 -0
@@ -335,6 +335,10 @@ static void map_kernel(void *fdt, u64 kaslr_offset, u64 va_offset) text_prot, false);dsb (ishst); } + +// Copy the root page table to its final location +memcpy ((void *)swapper_pg_dir + va_offset, init_pg_dir,PGD_SIZE ); +idmap_cpu_replace_ttbr1 (swapper_pg_dir); } asmlinkage u64 early_map_kernel(void *fdt)
arch/arm64/mm/kasan_init.c +0 -15
@@ -184,21 +184,6 @@ static void __init kasan_map_populate(unsigned long start, unsigned long end,kasan_pgd_populate (start &PAGE_MASK ,PAGE_ALIGN (end), node, false); } -/* -* Copy the current shadow region into a new pgdir. -*/ -void __init kasan_copy_shadow (pgd_t *pgdir) -{ -pgd_t *pgdp, *pgdp_new, *pgdp_end; - - pgdp =pgd_offset_k (KASAN_SHADOW_START ); - pgdp_end =pgd_offset_k (KASAN_SHADOW_END ); - pgdp_new =pgd_offset_pgd (pgdir,KASAN_SHADOW_START ); -do { -set_pgd (pgdp_new,READ_ONCE (*pgdp)); - }while (pgdp++, pgdp_new++, pgdp != pgdp_end); -} -static void __init clear_pgds (unsigned long start,unsigned long end) {
arch/arm64/mm/mmu.c +11 -90
140 lines not on this page · raw message
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 8/9 mm: add arch hook to validate mmap() prot flags
In reply to Ard BiesheuvelAdd a hook to permit architectures to perform validation on the prot flags passed to mmap(), like arch_validate_prot() does for mprotect(). This will be used by arm64 to reject PROT_WRITE+PROT_EXEC mappings on configurations that run with WXN enabled.
--- include/linux/mman.h | 15 +++++++++++++++ mm/mmap.c | 3 +++ 2 files changed, 18 insertions(+)
include/linux/mman.h +15 -0
@@ -120,6 +120,21 @@ static inline bool arch_validate_flags(unsigned long flags)#define arch_validate_flags arch_validate_flags#endif +#ifndef arch_validate_mmap_prot +/* +* This is called from mmap(), which ignores unknown prot bits so the default +* is to accept anything. +* +* Returns true if the prot flags are valid +*/ +static inline bool arch_validate_mmap_prot (unsigned long prot, +unsigned long addr) +{ +return true; +} +#define arch_validate_mmap_prot arch_validate_mmap_prot +#endif +/* * Optimisation macro. It is equivalent to: * (x & bit1) ? bit2 : 0
mm/mmap.c +3 -0
@@ -1437,6 +1437,9 @@ unsigned long do_mmap(struct file *file, unsigned long addr,if (!(file &&path_noexec (&file->f_path))) prot |=PROT_EXEC ; +if (!arch_validate_mmap_prot (prot, addr)) +return -EACCES ; +/* force arch specific MAP_FIXED handling in get_unmapped_area */ if (flags &MAP_FIXED_NOREPLACE ) flags |=MAP_FIXED ;
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v6 9/9 arm64: mm: add support for WXN memory translation attribute
In reply to Ard BiesheuvelThe AArch64 virtual memory system supports a global WXN control, which can be enabled to make all writable mappings implicitly no-exec. This is a useful hardening feature, as it prevents mistakes in managing page table permissions from being exploited to attack the system.
When enabled at EL1, the restrictions apply to both EL1 and EL0. EL1 is completely under our control, and has been cleaned up to allow WXN to be enabled from boot onwards. EL0 is not under our control, but given that widely deployed security features such as selinux or PaX already limit the ability of user space to create mappings that are writable and executable at the same time, the impact of enabling this for EL0 is expected to be limited. (For this reason, common user space libraries that have a legitimate need for manipulating executable code already carry fallbacks such as [0].)
If enabled at compile time, the feature can still be disabled at boot if needed, by passing arm64.nowxn on the kernel command line.
[0] https://github.com/libffi/libffi/blob/master/src/closures.c#L440
--- arch/arm64/Kconfig | 11 ++++++ arch/arm64/include/asm/cpufeature.h | 9 +++++ arch/arm64/include/asm/mman.h | 36 ++++++++++++++++++++ arch/arm64/include/asm/mmu_context.h | 30 +++++++++++++++- arch/arm64/kernel/pi/early_map_kernel.c | 11 +++++- arch/arm64/mm/mmu.c | 33 ++++++++++++++---- arch/arm64/mm/proc.S | 23 +++++++++++++ 7 files changed, 144 insertions(+), 9 deletions(-)
arch/arm64/Kconfig +11 -0
@@ -1422,6 +1422,17 @@ config RODATA_FULL_DEFAULT_ENABLED This requires the linear region to be mapped down to pages, which may adversely affect performance in some cases. +config ARM64_WXN + bool "Enable WXN attribute so all writable mappings are non-exec" + help + Set the WXN bit in the SCTLR system register so that all writable + mappings are treated as if the PXN/UXN bit is set as well. + If this is set to Y, it can still be disabled at runtime by + passing 'arm64.nowxn' on the kernel command line. + + This should only be set if no software needs to be supported that + relies on being able to execute from writable mappings. + config ARM64_SW_TTBR0_PAN bool "Emulate Privileged Access Never using TTBR0_EL1 switching" help
arch/arm64/include/asm/cpufeature.h +9 -0
@@ -915,6 +915,15 @@ extern struct arm64_ftr_override id_aa64isar2_override;u32 get_kvm_ipa_limit (void );void dump_cpu_features (void ); +extern int arm64_no_wxn; + +static inline bool arm64_wxn_enabled (void ) +{ +if (!IS_ENABLED (CONFIG_ARM64_WXN )) +return false; +return arm64_no_wxn ==0 ; +} +#endif /* __ASSEMBLY__ */ #endif
arch/arm64/include/asm/mman.h +36 -0
@@ -35,11 +35,40 @@ static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags) }#define arch_calc_vm_flag_bits (flags) arch_calc_vm_flag_bits(flags) +static inline bool arm64_check_wx_prot (unsigned long prot, +struct task_struct *tsk) +{ +/* +* When we are running with SCTLR_ELx.WXN==1, writable mappings are +* implicitly non-executable. This means we should reject such mappings +* when user space attempts to create them using mmap() or mprotect(). +*/ +if (arm64_wxn_enabled () && + ((prot & (PROT_WRITE |PROT_EXEC )) == (PROT_WRITE |PROT_EXEC ))) { +/* +* User space libraries such as libffi carry elaborate +* heuristics to decide whether it is worth it to even attempt +* to create writable executable mappings, as PaX or selinux +* enabled systems will outright reject it. They will usually +* fall back to something else (e.g., two separate shared +* mmap()s of a temporary file) on failure. +*/ +pr_info_ratelimited ( +"process %s (%d) attempted to create PROT_WRITE+PROT_EXEC mapping\n" , + tsk->comm, tsk->pid); +return false; + } +return true; +} +static inline bool arch_validate_prot (unsigned long prot,unsigned long addr__always_unused ) {unsigned long supported =PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM ; +if (!arm64_check_wx_prot (prot, current)) +return false; +if (system_supports_bti ()) supported |=PROT_BTI ;
@@ -50,6 +79,13 @@ static inline bool arch_validate_prot(unsigned long prot, }#define arch_validate_prot (prot, addr) arch_validate_prot(prot, addr) +static inline bool arch_validate_mmap_prot (unsigned long prot, +unsigned long addr) +{ +return arm64_check_wx_prot (prot, current); +} +#define arch_validate_mmap_prot arch_validate_mmap_prot +static inline bool arch_validate_flags (unsigned long vm_flags) {if (!system_supports_mte ())
arch/arm64/include/asm/mmu_context.h +29 -1
@@ -19,13 +19,41 @@#include <asm/cacheflush.h> #include <asm/cpufeature.h> #include <asm/proc-fns.h> -#include <asm-generic/mm_hooks.h> #include <asm/cputype.h> #include <asm/sysreg.h> #include <asm/tlbflush.h> extern bool rodata_full; +static inline int arch_dup_mmap (struct mm_struct *oldmm, +struct mm_struct *mm) +{ +return 0 ; +} + +static inline void arch_exit_mmap (struct mm_struct *mm) +{ +} + +static inline void arch_unmap (struct mm_struct *mm, +unsigned long start,unsigned long end) +{ +} + +static inline bool arch_vma_access_permitted (struct vm_area_struct *vma, +bool write,bool execute,bool foreign) +{ +if (IS_ENABLED (CONFIG_ARM64_WXN ) && execute && + (vma->vm_flags & (VM_WRITE |VM_EXEC )) == (VM_WRITE |VM_EXEC )) { +pr_warn_ratelimited ( +"process %s (%d) attempted to execute from writable memory\n" , + current->comm, current->pid); +/* disallow unless the nowxn override is set */ +return !arm64_wxn_enabled (); + } +return true; +} +static inline void contextidr_thread_switch (struct task_struct *next) {if (!IS_ENABLED (CONFIG_PID_IN_CONTEXTIDR ))
arch/arm64/kernel/pi/early_map_kernel.c +10 -1
@@ -278,15 +278,24 @@ static bool arm64_early_this_cpu_has_e0pd(void) ID_AA64MMFR2_E0PD_SHIFT); } +extern void disable_wxn (void ); +static void map_kernel (void *fdt,u64 kaslr_offset,u64 va_offset) {pgd_t *pgdp = (void *)init_pg_dir +PAGE_SIZE ;pgprot_t text_prot =PAGE_KERNEL_ROX ;pgprot_t data_prot =PAGE_KERNEL ;pgprot_t prot; +bool nowxn = false; -if (cmdline_has (fdt,"rodata=off" )) +if (cmdline_has (fdt,"rodata=off" )) { text_prot =PAGE_KERNEL_EXEC ; + nowxn = true; + } + +if (IS_ENABLED (CONFIG_ARM64_WXN ) && + (nowxn ||cmdline_has (fdt,"arm64.nowxn" ))) +disable_wxn ();// If we have a CPU that supports BTI and a kernel built for // BTI then mark the kernel executable text as guarded pages
arch/arm64/mm/mmu.c +26 -7
@@ -73,6 +73,21 @@ long __section(".mmuoff.data.write") __early_cpu_boot_status; unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;EXPORT_SYMBOL (empty_zero_page); +#ifdef CONFIG_ARM64_WXN +asmlinkage int arm64_no_wxn __ro_after_init; + +static int set_arm64_no_wxn (char *str) +{ + arm64_no_wxn =1 ; + +// Make the value visible to booting secondaries +dcache_clean_inval_poc ((u64)&arm64_no_wxn, + (u64 )&arm64_no_wxn +sizeof (arm64_no_wxn)); +return 1 ; +} +__setup ("arm64.nowxn" , set_arm64_no_wxn); +#endif + static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss; static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused; static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
@@ -660,15 +675,19 @@ static int __init parse_rodata(char *arg)int ret =strtobool (arg, &rodata_enabled);if (!ret) { rodata_full = false; -return 0 ; - } + }else { +/* permit 'full' in addition to boolean options */ +if (strcmp (arg,"full" )) +return -EINVAL ; -/* permit 'full' in addition to boolean options */ -if (strcmp (arg,"full" )) -return -EINVAL ; + rodata_enabled = true; + rodata_full = true; + } - rodata_enabled = true; - rodata_full = true; +#ifdef CONFIG_ARM64_WXN +if (!rodata_enabled) +set_arm64_no_wxn (NULL ); +#endif return 0 ; }early_param ("rodata" , parse_rodata);
arch/arm64/mm/proc.S +23 -0
@@ -498,8 +498,31 @@ SYM_FUNC_START(__cpu_setup) * Prepare SCTLR */ mov_q x0, INIT_SCTLR_EL1_MMU_ON +#ifdef CONFIG_ARM64_WXN + ldr_l w1, arm64_no_wxn, x1 + tst w1, #0x1 // WXN disabled on command line? + orr x1, x0, #SCTLR_ELx_WXN + csel x0, x0, x1, ne +#endif ret // return to head.S .unreq mair .unreq tcr SYM_FUNC_END(__cpu_setup) + +#ifdef CONFIG_ARM64_WXN + .align 2 +SYM_CODE_START(__pi_disable_wxn) + mrs x0, sctlr_el1 + bic x1, x0, #SCTLR_ELx_M + msr sctlr_el1, x1 + isb + tlbi vmalle1 + dsb nsh + isb + bic x0, x0, #SCTLR_ELx_WXN + msr sctlr_el1, x0 + isb + ret +SYM_CODE_END(__pi_disable_wxn) +#endif
Signature · 7 lines
2.35.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel