Page protections for arm64
cover Page protections for arm64
Hi,
These are a couple of semi-related patches to set memory on arm64 to something other than read/write/execute everywhere. The CONFIG_DEBUG_SET_MODULE_RONX is actually v2 of the previous patch[1] but it seemed reasonable to put this with the other work to map regular memory with better protections. The direction for the arm64 work is roughly based on arm.
Thanks,
Laura
[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2014-March/238246.html
Laura Abbott (3):
arm64: Add CONFIG_DEBUG_SET_MODULE_RONX support
arm64: Treat handle_arch_irq as a function pointer
arm64: WIP: add better page protections to arm64
arch/arm64/Kconfig.debug | 21 +++++ arch/arm64/include/asm/cacheflush.h | 4 + arch/arm64/kernel/entry.S | 4 +- arch/arm64/mm/Makefile | 2 +- arch/arm64/mm/init.c | 1 + arch/arm64/mm/mm.h | 2 + arch/arm64/mm/mmu.c | 173 ++++++++++++++++++++++++++++++++---- arch/arm64/mm/pageattr.c | 120 +++++++++++++++++++++++++ 8 files changed, 309 insertions(+), 18 deletions(-) create mode 100644 arch/arm64/mm/pageattr.c
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
PATCH 1/3 arm64: Add CONFIG_DEBUG_SET_MODULE_RONX support
In a similar fashion to other architecture, add the infrastructure and Kconfig to enable DEBUG_SET_MODULE_RONX support. When enabled, module ranges will be marked read-only/no-execute as appropriate.
--- arch/arm64/Kconfig.debug | 11 ++++ arch/arm64/include/asm/cacheflush.h | 4 ++ arch/arm64/mm/Makefile | 2 +- arch/arm64/mm/pageattr.c | 120 ++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/mm/pageattr.c
arch/arm64/Kconfig.debug +11 -0
@@ -37,4 +37,15 @@ config PID_IN_CONTEXTIDR instructions during context switch. Say Y here only if you are planning to use hardware trace tools with this kernel. +config DEBUG_SET_MODULE_RONX + bool "Set loadable kernel module data as NX and text as RO" + depends on MODULES + help + This option helps catch unintended modifications to loadable + kernel module's text and read-only data. It also prevents execution + of module data. Such protection may interfere with run-time code + patching and dynamic kernel tracing - and they might also protect + against certain classes of kernel exploits. + If in doubt, say "N". + endmenu
arch/arm64/include/asm/cacheflush.h +4 -0
@@ -157,4 +157,8 @@ static inline void flush_cache_vunmap(unsigned long start, unsigned long end) { } +int set_memory_ro (unsigned long addr,int numpages); +int set_memory_rw (unsigned long addr,int numpages); +int set_memory_x (unsigned long addr,int numpages); +int set_memory_nx (unsigned long addr,int numpages);#endif
arch/arm64/mm/Makefile +1 -1
@@ -1,5 +1,5 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ cache.o copypage.o flush.o \ ioremap.o mmap.o pgd.o mmu.o \ - context.o tlb.o proc.o + context.o tlb.o proc.o pageattr.o obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o
arch/arm64/mm/pageattr.c new file +120 -0
120 lines not on this page · raw message
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
PATCH 1/3 arm64: Add CONFIG_DEBUG_SET_MODULE_RONX support
Quoted 8 lines · Laura Abbott
In a similar fashion to other architecture, add the infrastructure and
Kconfigto enableDEBUG_SET_MODULE_RONXsupport. When enabled, module ranges will be marked read-only/no-execute as appropriate.Change-Id I4251a0929b1fe6f43f84b14f0a64fed30769700eSigned-off-by Laura Abbott <[email protected]>---
[ ... ]
Quoted 5 lines
arch/arm64/mm/pageattr.c new file +0
-0
[ ... ]
Quoted 12 lines
arch/arm64/mm/pageattr.c new file +12
-0+static int change_memory_common (unsigned long addr,int numpages, +pgprot_t prot,bool set) +{ +unsigned long start = addr; +unsigned long size =PAGE_SIZE *numpages; +unsigned long end = start + size; +int ret; + +if (start <MODULES_VADDR || start >=MODULES_END ) +return -EINVAL ; + +if (end <MODULES_VADDR || end >=MODULES_END )
arch/arm64/mm/pageattr.c line 82
Quoted 10 lines
arch/arm64/mm/pageattr.c new file +10
-0+return -EINVAL ; + +if (set) + ret =apply_to_page_range (&init_mm, start, size, + set_page_range, (void *)prot); +else + ret =apply_to_page_range (&init_mm, start, size, + clear_page_range, (void *)prot); + +flush_tlb_kernel_range (start, end);
Could you please add an isb() here? (We're about to nuke the one in flush_tlb_kernel_range).
Cheers,
Signature · 1 line
Steve
PATCH 1/3 arm64: Add CONFIG_DEBUG_SET_MODULE_RONX support
Quoted 37 lines · Steve Capper
Quoted 8 lines · Laura Abbott
In a similar fashion to other architecture, add the infrastructure and
Kconfigto enableDEBUG_SET_MODULE_RONXsupport. When enabled, module ranges will be marked read-only/no-execute as appropriate.Change-Id I4251a0929b1fe6f43f84b14f0a64fed30769700eSigned-off-by Laura Abbott <[email protected]>---
[ ... ]
Quoted 5 lines
arch/arm64/mm/pageattr.c new file +0
-0[ ... ]
Quoted 17 lines
arch/arm64/mm/pageattr.c new file +17
-0+static int change_memory_common (unsigned long addr,int numpages, +pgprot_t prot,bool set) +{ +unsigned long start = addr; +unsigned long size =PAGE_SIZE *numpages; +unsigned long end = start + size; +int ret; + +if (start <MODULES_VADDR || start >=MODULES_END ) +return -EINVAL ; + +if (end <MODULES_VADDR || end >=MODULES_END ) +return -EINVAL ; + +if (set) + ret =apply_to_page_range (&init_mm, start, size, + set_page_range, (void *)prot);
Quoted 8 lines
arch/arm64/mm/pageattr.c line 82
Quoted 5 lines
arch/arm64/mm/pageattr.c new file +5
-0+else + ret =apply_to_page_range (&init_mm, start, size, + clear_page_range, (void *)prot); + +flush_tlb_kernel_range (start, end);Could you please add an
isb()here? (We're about to nuke the one inflush_tlb_kernel_range).
Thinking about this even more (too much?), how does this work with SMP anyway? You need each CPU to execute an isb(), so this just a race that is dealt with already (probably treated as benign)?
Will
PATCH 1/3 arm64: Add CONFIG_DEBUG_SET_MODULE_RONX support
Quoted 47 lines · Will Deacon
Quoted 37 lines · Steve Capper
Quoted 8 lines · Laura Abbott
In a similar fashion to other architecture, add the infrastructure and
Kconfigto enableDEBUG_SET_MODULE_RONXsupport. When enabled, module ranges will be marked read-only/no-execute as appropriate.Change-Id I4251a0929b1fe6f43f84b14f0a64fed30769700eSigned-off-by Laura Abbott <[email protected]>---
[ ... ]
Quoted 5 lines
arch/arm64/mm/pageattr.c new file +0
-0[ ... ]
Quoted 17 lines
arch/arm64/mm/pageattr.c new file +17
-0+static int change_memory_common (unsigned long addr,int numpages, +pgprot_t prot,bool set) +{ +unsigned long start = addr; +unsigned long size =PAGE_SIZE *numpages; +unsigned long end = start + size; +int ret; + +if (start <MODULES_VADDR || start >=MODULES_END ) +return -EINVAL ; + +if (end <MODULES_VADDR || end >=MODULES_END ) +return -EINVAL ; + +if (set) + ret =apply_to_page_range (&init_mm, start, size, + set_page_range, (void *)prot);Quoted 8 lines
arch/arm64/mm/pageattr.c line 82
Quoted 5 lines
arch/arm64/mm/pageattr.c new file +5
-0+else + ret =apply_to_page_range (&init_mm, start, size, + clear_page_range, (void *)prot); + +flush_tlb_kernel_range (start, end);Could you please add an
isb()here? (We're about to nuke the one inflush_tlb_kernel_range).
Quoted 4 lines
Thinking about this even more (too much?), how does this work with SMP anyway? You need each CPU to execute an
isb(), so this just a race that is dealt with already (probably treated as benign)?
Yes unless we want to IPI an isb I think this should be a mostly benign race. I say 'mostly' only because this is a security/debug feature so there could be a hole to take advantage of. Then again, because we map and then set permissions later there is always a chance of a race.
I'll add the isb for v2 based on Will's patch set.
Laura
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
PATCH 2/3 arm64: Treat handle_arch_irq as a function pointer
In reply to Laura Abbotthandle_arch_irq isn't actually text, it's just a function pointer. It doesn't need to be stored in the text section and doing so causes problems if we ever want to make the kernel text readonly. Move it to the data section.
--- arch/arm64/kernel/entry.S | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/kernel/entry.S +3 -1
@@ -139,7 +139,8 @@ tsk .req x28 // current thread_info * Interrupt handling. */ .macro irq_handler - ldr x1, handle_arch_irq + ldr x1, =handle_arch_irq + ldr x1, [x1] mov x0, sp blr x1 .endm
@@ -678,5 +679,6 @@ ENTRY(sys_rt_sigreturn_wrapper) b sys_rt_sigreturn ENDPROC(sys_rt_sigreturn_wrapper) +.data ENTRY(handle_arch_irq) .quad 0
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
PATCH 2/3 arm64: Treat handle_arch_irq as a function pointer
Quoted 26 lines · Laura Abbott
handle_arch_irqisn't actually text, it's just a function pointer. It doesn't need to be stored in the text section and doing so causes problems if we ever want to make the kernel text readonly. Move it to the data section.Change-Id I682ff82f5c8c429129dd81bbcc91571f278c62e2Signed-off-by Laura Abbott <[email protected]>--- arch/arm64/kernel/entry.S | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)arch/arm64/kernel/entry.S +2
-1@@ -139,7 +139,8 @@ tsk .req x28 // current thread_info * Interrupt handling. */ .macro irq_handler - ldr x1, handle_arch_irq + ldr x1, =handle_arch_irq + ldr x1, [x1] mov x0, sp blr x1 .endm
arch/arm64/kernel/entry.S ENTRY(sys_rt_sigreturn_wrapper) · line 683
Quoted 5 lines
arch/arm64/kernel/entry.S +1
-0@@ -678,5 +679,6 @@ ENTRY(sys_rt_sigreturn_wrapper) b sys_rt_sigreturn ENDPROC(sys_rt_sigreturn_wrapper) +.data ENTRY(handle_arch_irq)
Probably worth dropping the ENTRY directive if we're putting this in .data and using an explicit .align instead.
Will
PATCH 3/3 arm64: WIP: add better page protections to arm64
In reply to Laura AbbottAdd page protections for arm64 similar to those in arm or in progress for arm. This is for security reasons. The flow is currently:
- Map all memory as either RWX or RW. We round to the nearest section to avoid creating page tables before everything is mapped
- Once everything is mapped, if either end of the RWX section should not be X, we split the PMD and remap as necessary
- When initmem is to be freed, we change the permissions back to RW (using stop machine if necessary to flush the TLB)
- If CONFIG_DEBUG_RODATA is set, the read only sections are set read only.
TODO:
- actually make init rodata ro?
- Kconfig option to align up to section boundary (ran into relocation truncation errors, need to debug more)
- Anything to do with ftrace/kprobes
--- arch/arm64/Kconfig.debug | 10 +++ arch/arm64/mm/init.c | 1 + arch/arm64/mm/mm.h | 2 + arch/arm64/mm/mmu.c | 173 ++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 170 insertions(+), 16 deletions(-)
arch/arm64/Kconfig.debug +10 -0
@@ -48,4 +48,14 @@ config DEBUG_SET_MODULE_RONX against certain classes of kernel exploits. If in doubt, say "N". +config DEBUG_RODATA + bool "Make kernel text and rodata read-only" + help + If this is set, kernel text and rodata will be made read-only. This + is to help catch accidental or malicious attempts to change the + kernel's executable code. Additionally splits rodata from kernel + text so it can be made explicitly non-executable. + + If in doubt, say Y + endmenu
arch/arm64/mm/init.c +1 -0
@@ -325,6 +325,7 @@ void __init mem_init(void)void free_initmem (void ) { +fixup_init ();free_initmem_default (0 ); }
arch/arm64/mm/mm.h +2 -0
@@ -1,2 +1,4 @@extern void __init bootmem_init (void );extern void __init arm64_swiotlb_init (void ); + +void fixup_init (void );
arch/arm64/mm/mmu.c +157 -16
259 lines not on this page · raw message
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation