Skip to contentSkip to conversation

powerpc: Further Strict RWX support

PATCH Superseded Jordan Niethe 3 participants 19 messages May 17, 2021 – May 20, 2021

Subsystems (3)

v14 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v14 cover powerpc: Further Strict RWX support

Adding more Strict RWX support on powerpc, in particular Strict Module RWX. Thanks for all of the feedback everyone.
It is now rebased on linux-next + powerpc/64s/radix: Enable huge vmalloc mappings
(https://lore.kernel.org/linuxppc-dev/[email protected]/)

For reference the previous revision is available here:
https://lore.kernel.org/linuxppc-dev/[email protected]/

The changes in v14 for each patch:

Christophe Leroy (2):
powerpc/mm: implement set_memory_attr()
powerpc/32: use set_memory_attr()

Jordan Niethe (4):
powerpc/lib/code-patching: Set up Strict RWX patching earlier powerpc/modules: Make module_alloc() Strict Module RWX aware
v14: - Split out from powerpc: Set ARCH_HAS_STRICT_MODULE_RW
- Add and use strict_module_rwx_enabled() helper
powerpc/bpf: Remove bpf_jit_free()
powerpc/bpf: Write protect JIT code

Russell Currey (3):
powerpc/mm: Implement set_memory() routines
v14: - only check is_vm_area_hugepages() for virtual memory
powerpc/kprobes: Mark newly allocated probes as ROX
v14: - Use strict_module_rwx_enabled()
powerpc: Set ARCH_HAS_STRICT_MODULE_RWX
v14: - Make changes to module_alloc() its own commit

 arch/powerpc/Kconfig                  |   2 +
 arch/powerpc/include/asm/mmu.h        |   5 +
 arch/powerpc/include/asm/set_memory.h |  34 +++++++
 arch/powerpc/kernel/kprobes.c         |  17 ++++
 arch/powerpc/kernel/module.c          |   4 +-
 arch/powerpc/lib/code-patching.c      |  12 +--
 arch/powerpc/mm/Makefile              |   2 +-
 arch/powerpc/mm/pageattr.c            | 134 ++++++++++++++++++++++++++
 arch/powerpc/mm/pgtable_32.c          |  60 ++----------
 arch/powerpc/net/bpf_jit_comp.c       |  13 +--
 10 files changed, 211 insertions(+), 72 deletions(-)
 create mode 100644 arch/powerpc/include/asm/set_memory.h
 create mode 100644 arch/powerpc/mm/pageattr.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v14 1/9 powerpc/mm: Implement set_memory() routines

From: Russell Currey <[email protected]>

The set_memory_{ro/rw/nx/x}() functions are required for STRICT_MODULE_RWX, and are generally useful primitives to have. This implementation is designed to be generic across powerpc's many MMUs. It's possible that this could be optimised to be faster for specific MMUs.

This implementation does not handle cases where the caller is attempting to change the mapping of the page it is executing from, or if another CPU is concurrently using the page being altered. These cases likely shouldn't happen, but a more complex implementation with MMU-specific code could safely handle them.

On hash, the linear mapping is not kept in the linux pagetable, so this will not change the protection if used on that range. Currently these functions are not used on the linear map so just WARN for now.

apply_to_existing_page_range() does not work on huge pages so for now disallow changing the protection of huge pages.

[jpn: - Allow set memory functions to be used without Strict RWX
- Hash: Disallow certain regions
- Have change_page_attr() take function pointers to manipulate ptes
- Radix: Add ptesync after set_pte_at()]

---
v10: WARN if trying to change the hash linear map
v11: - Update copywrite dates
- Allow set memory functions to be used without Strict RWX
- Hash: Disallow certain regions and add comment explaining why
- Have change_page_attr() take function pointers to manipulate ptes
- Clarify change_page_attr()'s comment
- Radix: Add ptesync after set_pte_at()
v12: - change_page_attr() back to taking an action value
- disallow operating on huge pages
v14: - only check is_vm_area_hugepages() for virtual memory

---
 arch/powerpc/Kconfig                  |   1 +
 arch/powerpc/include/asm/set_memory.h |  32 ++++++++
 arch/powerpc/mm/Makefile              |   2 +-
 arch/powerpc/mm/pageattr.c            | 101 ++++++++++++++++++++++++++
 4 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 arch/powerpc/include/asm/set_memory.h
 create mode 100644 arch/powerpc/mm/pageattr.c
arch/powerpc/Kconfig +1 -0
index 3f863dd21374..cce0a137b046 100644
@@ -138,6 +138,7 @@ config PPC
 	select ARCH_HAS_MEMBARRIER_CALLBACKS
 	select ARCH_HAS_MEMBARRIER_SYNC_CORE
 	select ARCH_HAS_SCALED_CPUTIME		if VIRT_CPU_ACCOUNTING_NATIVE && PPC_BOOK3S_64
+	select ARCH_HAS_SET_MEMORY
 	select ARCH_HAS_STRICT_KERNEL_RWX	if ((PPC_BOOK3S_64 || PPC32) && !HIBERNATION)
 	select ARCH_HAS_TICK_BROADCAST		if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_HAS_UACCESS_FLUSHCACHE
arch/powerpc/include/asm/set_memory.h new file +32 -0
new file mode 100644
index 000000000000..64011ea444b4
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_POWERPC_SET_MEMORY_H
+#define _ASM_POWERPC_SET_MEMORY_H
+
+#define SET_MEMORY_RO	0
+#define SET_MEMORY_RW	1
+#define SET_MEMORY_NX	2
+#define SET_MEMORY_X	3
+
+int change_memory_attr(unsigned long addr, int numpages, long action);
+
+static inline int set_memory_ro(unsigned long addr, int numpages)
+{
+	return change_memory_attr(addr, numpages, SET_MEMORY_RO);
+}
+
+static inline int set_memory_rw(unsigned long addr, int numpages)
+{
+	return change_memory_attr(addr, numpages, SET_MEMORY_RW);
+}
+
+static inline int set_memory_nx(unsigned long addr, int numpages)
+{
+	return change_memory_attr(addr, numpages, SET_MEMORY_NX);
+}
+
+static inline int set_memory_x(unsigned long addr, int numpages)
+{
+	return change_memory_attr(addr, numpages, SET_MEMORY_X);
+}
+
+#endif
arch/powerpc/mm/Makefile +1 -1
index c3df3a8501d4..9142cf1fb0d5 100644
@@ -5,7 +5,7 @@
 
 ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 
-obj-y				:= fault.o mem.o pgtable.o mmap.o maccess.o \
+obj-y				:= fault.o mem.o pgtable.o mmap.o maccess.o pageattr.o \
 				   init_$(BITS).o pgtable_$(BITS).o \
 				   pgtable-frag.o ioremap.o ioremap_$(BITS).o \
 				   init-common.o mmu_context.o drmem.o \
arch/powerpc/mm/pageattr.c new file +101 -0

101 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

PATCH v14 2/9 powerpc/lib/code-patching: Set up Strict RWX patching earlier

In reply to Jordan Niethe

setup_text_poke_area() is a late init call so it runs before mark_rodata_ro() and after the init calls. This lets all the init code patching simply write to their locations. In the future, kprobes is going to allocate its instruction pages RO which means they will need setup_text__poke_area() to have been already called for their code patching. However, init_kprobes() (which allocates and patches some instruction pages) is an early init call so it happens before setup_text__poke_area().

start_kernel() calls poking_init() before any of the init calls. On powerpc, poking_init() is currently a nop. setup_text_poke_area() relies on kernel virtual memory, cpu hotplug and per_cpu_areas being setup. setup_per_cpu_areas(), boot_cpu_hotplug_init() and mm_init() are called before poking_init().

Turn setup_text_poke_area() into poking_init().

---
v9: New to series

---
 arch/powerpc/lib/code-patching.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
arch/powerpc/lib/code-patching.c +4 -8
index 870b30d9be2f..15296207e1ba 100644
@@ -70,14 +70,11 @@ static int text_area_cpu_down(unsigned int cpu)
 }
 
 /*
- * Run as a late init call. This allows all the boot time patching to be done
- * simply by patching the code, and then we're called here prior to
- * mark_rodata_ro(), which happens after all init calls are run. Although
- * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
- * it as being preferable to a kernel that will crash later when someone tries
- * to use patch_instruction().
+ * Although BUG_ON() is rude, in this case it should only happen if ENOMEM, and
+ * we judge it as being preferable to a kernel that will crash later when
+ * someone tries to use patch_instruction().
  */
-static int __init setup_text_poke_area(void)
+int __init poking_init(void)
 {
 	BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
 		"powerpc/text_poke:online", text_area_cpu_up,
@@ -85,7 +82,6 @@ static int __init setup_text_poke_area(void)
 
 	return 0;
 }
-late_initcall(setup_text_poke_area);
 
 /*
  * This can be called for kernel text or a module.
Signature · 1 line
2.25.1
#3 lore raw

PATCH v14 3/9 powerpc/modules: Make module_alloc() Strict Module RWX aware

In reply to Jordan Niethe

Make module_alloc() use PAGE_KERNEL protections instead of PAGE_KERNEL_EXEX if Strict Module RWX is enabled.

---
v14: - Split out from powerpc: Set ARCH_HAS_STRICT_MODULE_RWX
- Add and use strict_module_rwx_enabled() helper

---
 arch/powerpc/include/asm/mmu.h | 5 +++++
 arch/powerpc/kernel/module.c   | 4 +++-
 2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/mmu.h +5 -0
index 607168b1aef4..7710bf0cbf8a 100644
@@ -357,6 +357,11 @@ static inline bool strict_kernel_rwx_enabled(void)
 	return false;
 }
 #endif
+
+static inline bool strict_module_rwx_enabled(void)
+{
+	return IS_ENABLED(CONFIG_STRICT_MODULE_RWX) && strict_kernel_rwx_enabled();
+}
 #endif /* !__ASSEMBLY__ */
 
 /* The kernel use the constants below to index in the page sizes array.
arch/powerpc/kernel/module.c +3 -1
index 3f35c8d20be7..ed04a3ba66fe 100644
@@ -92,12 +92,14 @@ int module_finalize(const Elf_Ehdr *hdr,
 static __always_inline void *
 __module_alloc(unsigned long size, unsigned long start, unsigned long end)
 {
+	pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC;
+
 	/*
 	 * Don't do huge page allocations for modules yet until more testing
 	 * is done. STRICT_MODULE_RWX may require extra work to support this
 	 * too.
 	 */
-	return __vmalloc_node_range(size, 1, start, end, GFP_KERNEL, PAGE_KERNEL_EXEC,
+	return __vmalloc_node_range(size, 1, start, end, GFP_KERNEL, prot,
 				    VM_FLUSH_RESET_PERMS | VM_NO_HUGE_VMAP,
 				    NUMA_NO_NODE, __builtin_return_address(0));
 }
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v14 3/9] powerpc/modules: Make module_alloc() Strict Module RWX aware

Le 17/05/2021 à 05:28, Jordan Niethe a écrit :

Quoted 17 lines

Make module_alloc() use PAGE_KERNEL protections instead of PAGE_KERNEL_EXEX if Strict Module RWX is enabled.

---
v14: - Split out from powerpc: Set ARCH_HAS_STRICT_MODULE_RWX
- Add and use strict_module_rwx_enabled() helper

---
  arch/powerpc/include/asm/mmu.h | 5 +++++
  arch/powerpc/kernel/module.c   | 4 +++-
  2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/mmu.h +0 -0
index 607168b1aef4..7710bf0cbf8a 100644

arch/powerpc/include/asm/mmu.h static inline bool strict_kernel_rwx_enabled(void) · line 364

Quoted 8 lines
arch/powerpc/include/asm/mmu.h +5 -0
@@ -357,6 +357,11 @@ static inline bool strict_kernel_rwx_enabled(void)
  	return false;
  }
  #endif
+
+static inline bool strict_module_rwx_enabled(void)
+{
+	return IS_ENABLED(CONFIG_STRICT_MODULE_RWX) && strict_kernel_rwx_enabled();
+}

Looking at arch/Kconfig, I have the feeling that it is possible to select CONFIG_STRICT_MODULE_RWX without selecting CONFIG_STRICT_KERNEL_RWX.

In that case, strict_kernel_rwx_enabled() will return false.

Quoted 24 lines
arch/powerpc/include/asm/mmu.h +0 -0
  #endif /* !__ASSEMBLY__ */
 
  /* The kernel use the constants below to index in the page sizes array.
arch/powerpc/kernel/module.c +3 -1
index 3f35c8d20be7..ed04a3ba66fe 100644
@@ -92,12 +92,14 @@ int module_finalize(const Elf_Ehdr *hdr,
  static __always_inline void *
  __module_alloc(unsigned long size, unsigned long start, unsigned long end)
  {
+	pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC;
+
  	/*
  	 * Don't do huge page allocations for modules yet until more testing
  	 * is done. STRICT_MODULE_RWX may require extra work to support this
  	 * too.
  	 */
-	return __vmalloc_node_range(size, 1, start, end, GFP_KERNEL, PAGE_KERNEL_EXEC,
+	return __vmalloc_node_range(size, 1, start, end, GFP_KERNEL, prot,
  				    VM_FLUSH_RESET_PERMS | VM_NO_HUGE_VMAP,
  				    NUMA_NO_NODE, __builtin_return_address(0));
  }
#5 lore raw

Re: [PATCH v14 3/9] powerpc/modules: Make module_alloc() Strict Module RWX aware

Quoted 33 lines · Christophe Leroy
Quoted 17 lines · Jordan Niethe

Make module_alloc() use PAGE_KERNEL protections instead of PAGE_KERNEL_EXEX if Strict Module RWX is enabled.

---
v14: - Split out from powerpc: Set ARCH_HAS_STRICT_MODULE_RWX
- Add and use strict_module_rwx_enabled() helper

---
  arch/powerpc/include/asm/mmu.h | 5 +++++
  arch/powerpc/kernel/module.c   | 4 +++-
  2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/mmu.h +0 -0
index 607168b1aef4..7710bf0cbf8a 100644

arch/powerpc/include/asm/mmu.h static inline bool strict_kernel_rwx_enabled(void) · line 364

Quoted 8 lines
arch/powerpc/include/asm/mmu.h +5 -0
@@ -357,6 +357,11 @@ static inline bool strict_kernel_rwx_enabled(void)
      return false;
  }
  #endif
+
+static inline bool strict_module_rwx_enabled(void)
+{
+     return IS_ENABLED(CONFIG_STRICT_MODULE_RWX) && strict_kernel_rwx_enabled();
+}

Looking at arch/Kconfig, I have the feeling that it is possible to select CONFIG_STRICT_MODULE_RWX without selecting CONFIG_STRICT_KERNEL_RWX.

Quoted 1 line

In that case, strict_kernel_rwx_enabled() will return false.

Ok, if someone did that currently it would break things, e.g. code patching. I think it should it be made impossible to CONFIG_STRICT_MODULE_RWX without CONFIG_STRICT_KERNEL_RWX?

Quoted 25 lines
Quoted 25 lines
arch/powerpc/include/asm/mmu.h +0 -0
  #endif /* !__ASSEMBLY__ */
 
  /* The kernel use the constants below to index in the page sizes array.
arch/powerpc/kernel/module.c +3 -1
index 3f35c8d20be7..ed04a3ba66fe 100644
@@ -92,12 +92,14 @@ int module_finalize(const Elf_Ehdr *hdr,
  static __always_inline void *
  __module_alloc(unsigned long size, unsigned long start, unsigned long end)
  {
+     pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC;
+
      /*
       * Don't do huge page allocations for modules yet until more testing
       * is done. STRICT_MODULE_RWX may require extra work to support this
       * too.
       */
-     return __vmalloc_node_range(size, 1, start, end, GFP_KERNEL, PAGE_KERNEL_EXEC,
+     return __vmalloc_node_range(size, 1, start, end, GFP_KERNEL, prot,
                                  VM_FLUSH_RESET_PERMS | VM_NO_HUGE_VMAP,
                                  NUMA_NO_NODE, __builtin_return_address(0));
  }
#6 lore raw

Re: [PATCH v14 3/9] powerpc/modules: Make module_alloc() Strict Module RWX aware

Quoted 32 lines · Jordan Niethe
Quoted 30 lines · Christophe Leroy
Quoted 17 lines · Jordan Niethe

Make module_alloc() use PAGE_KERNEL protections instead of PAGE_KERNEL_EXEX if Strict Module RWX is enabled.

---
v14: - Split out from powerpc: Set ARCH_HAS_STRICT_MODULE_RWX
- Add and use strict_module_rwx_enabled() helper

---
  arch/powerpc/include/asm/mmu.h | 5 +++++
  arch/powerpc/kernel/module.c   | 4 +++-
  2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/mmu.h +0 -0
index 607168b1aef4..7710bf0cbf8a 100644

arch/powerpc/include/asm/mmu.h static inline bool strict_kernel_rwx_enabled(void) · line 364

Quoted 8 lines
arch/powerpc/include/asm/mmu.h +5 -0
@@ -357,6 +357,11 @@ static inline bool strict_kernel_rwx_enabled(void)
      return false;
  }
  #endif
+
+static inline bool strict_module_rwx_enabled(void)
+{
+     return IS_ENABLED(CONFIG_STRICT_MODULE_RWX) && strict_kernel_rwx_enabled();
+}

Looking at arch/Kconfig, I have the feeling that it is possible to select CONFIG_STRICT_MODULE_RWX without selecting CONFIG_STRICT_KERNEL_RWX.

Quoted 5 lines
Quoted 1 line

In that case, strict_kernel_rwx_enabled() will return false.

Ok, if someone did that currently it would break things, e.g. code patching. I think it should it be made impossible to CONFIG_STRICT_MODULE_RWX without CONFIG_STRICT_KERNEL_RWX?

Yeah I don't see any reason to support that combination.

We should be moving to a world where both are on by default, or in fact are always enabled.

cheers

#7 lore raw

Re: [PATCH v14 3/9] powerpc/modules: Make module_alloc() Strict Module RWX aware

Le 17/05/2021 à 13:01, Michael Ellerman a écrit :

Quoted 35 lines
Quoted 33 lines · Jordan Niethe
Quoted 31 lines · Christophe Leroy
Quoted 17 lines · Jordan Niethe

Make module_alloc() use PAGE_KERNEL protections instead of PAGE_KERNEL_EXEX if Strict Module RWX is enabled.

---
v14: - Split out from powerpc: Set ARCH_HAS_STRICT_MODULE_RWX
- Add and use strict_module_rwx_enabled() helper

---
   arch/powerpc/include/asm/mmu.h | 5 +++++
   arch/powerpc/kernel/module.c   | 4 +++-
   2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/mmu.h +0 -0
index 607168b1aef4..7710bf0cbf8a 100644

arch/powerpc/include/asm/mmu.h static inline bool strict_kernel_rwx_enabled(void) · line 364

Quoted 8 lines
arch/powerpc/include/asm/mmu.h +5 -0
@@ -357,6 +357,11 @@ static inline bool strict_kernel_rwx_enabled(void)
       return false;
   }
   #endif
+
+static inline bool strict_module_rwx_enabled(void)
+{
+     return IS_ENABLED(CONFIG_STRICT_MODULE_RWX) && strict_kernel_rwx_enabled();
+}

Looking at arch/Kconfig, I have the feeling that it is possible to select CONFIG_STRICT_MODULE_RWX without selecting CONFIG_STRICT_KERNEL_RWX.

In that case, strict_kernel_rwx_enabled() will return false.

Quoted 9 lines
Quoted 3 lines

Ok, if someone did that currently it would break things, e.g. code patching. I think it should it be made impossible to CONFIG_STRICT_MODULE_RWX without CONFIG_STRICT_KERNEL_RWX?

Yeah I don't see any reason to support that combination.

We should be moving to a world where both are on by default, or in fact are always enabled.

Would it work if we add the following in arch/powerpc/Kconfig ? :

select STRICT_KERNEL_RWX if STRICT_MODULE_RWX

There should be no dependency issue as powerpc only selects ARCH_HAS_STRICT_MODULE_RWX when ARCH_HAS_STRICT_KERNEL_RWX is also selected.

Christophe

#8 lore raw

Re: [PATCH v14 3/9] powerpc/modules: Make module_alloc() Strict Module RWX aware

Quoted 45 lines · Christophe Leroy

Le 17/05/2021 à 13:01, Michael Ellerman a écrit :

Quoted 35 lines
Quoted 33 lines · Jordan Niethe
Quoted 31 lines · Christophe Leroy
Quoted 17 lines · Jordan Niethe

Make module_alloc() use PAGE_KERNEL protections instead of PAGE_KERNEL_EXEX if Strict Module RWX is enabled.

---
v14: - Split out from powerpc: Set ARCH_HAS_STRICT_MODULE_RWX
- Add and use strict_module_rwx_enabled() helper

---
   arch/powerpc/include/asm/mmu.h | 5 +++++
   arch/powerpc/kernel/module.c   | 4 +++-
   2 files changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/mmu.h +0 -0
index 607168b1aef4..7710bf0cbf8a 100644

arch/powerpc/include/asm/mmu.h static inline bool strict_kernel_rwx_enabled(void) · line 364

Quoted 8 lines
arch/powerpc/include/asm/mmu.h +5 -0
@@ -357,6 +357,11 @@ static inline bool strict_kernel_rwx_enabled(void)
       return false;
   }
   #endif
+
+static inline bool strict_module_rwx_enabled(void)
+{
+     return IS_ENABLED(CONFIG_STRICT_MODULE_RWX) && strict_kernel_rwx_enabled();
+}

Looking at arch/Kconfig, I have the feeling that it is possible to select CONFIG_STRICT_MODULE_RWX without selecting CONFIG_STRICT_KERNEL_RWX.

In that case, strict_kernel_rwx_enabled() will return false.

Quoted 8 lines
Quoted 3 lines

Ok, if someone did that currently it would break things, e.g. code patching. I think it should it be made impossible to CONFIG_STRICT_MODULE_RWX without CONFIG_STRICT_KERNEL_RWX?

Yeah I don't see any reason to support that combination.

We should be moving to a world where both are on by default, or in fact are always enabled.

Quoted 6 lines

Would it work if we add the following in arch/powerpc/Kconfig ? :

select STRICT_KERNEL_RWX if STRICT_MODULE_RWX

There should be no dependency issue as powerpc only selects ARCH_HAS_STRICT_MODULE_RWX when ARCH_HAS_STRICT_KERNEL_RWX is also selected.

I think it will work. It's slightly rude to select things like that, but I think it's OK for something like this.

Medium term we can possibly just have the generic STRICT_MODULE_RWX depend on STRICT_KERNEL_RWX.

cheers

#9 lore raw

PATCH v14 4/9 powerpc/kprobes: Mark newly allocated probes as ROX

In reply to Jordan Niethe

From: Russell Currey <[email protected]>

Add the arch specific insn page allocator for powerpc. This allocates ROX pages if STRICT_KERNEL_RWX is enabled. These pages are only written to with patch_instruction() which is able to write RO pages.

[jpn: Reword commit message, switch to __vmalloc_node_range()]

---
v9: - vmalloc_exec() no longer exists
- Set the page to RW before freeing it
v10: - use __vmalloc_node_range()
v11: - Neaten up
v12: - Switch from __vmalloc_node_range() to module_alloc()
v13: Use strict_kernel_rwx_enabled()
v14: Use strict_module_rwx_enabled()

---
 arch/powerpc/kernel/kprobes.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/powerpc/kernel/kprobes.c +17 -0
index 01ab2163659e..937e338053ff 100644
@@ -19,11 +19,13 @@
 #include <linux/extable.h>
 #include <linux/kdebug.h>
 #include <linux/slab.h>
+#include <linux/moduleloader.h>
 #include <asm/code-patching.h>
 #include <asm/cacheflush.h>
 #include <asm/sstep.h>
 #include <asm/sections.h>
 #include <asm/inst.h>
+#include <asm/set_memory.h>
 #include <linux/uaccess.h>
 
 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
@@ -103,6 +105,21 @@ kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
 	return addr;
 }
 
+void *alloc_insn_page(void)
+{
+	void *page;
+
+	page = module_alloc(PAGE_SIZE);
+	if (!page)
+		return NULL;
+
+	if (strict_module_rwx_enabled()) {
+		set_memory_ro((unsigned long)page, 1);
+		set_memory_x((unsigned long)page, 1);
+	}
+	return page;
+}
+
 int arch_prepare_kprobe(struct kprobe *p)
 {
 	int ret = 0;
Signature · 1 line
2.25.1
#10 lore raw

PATCH v14 5/9 powerpc/bpf: Remove bpf_jit_free()

In reply to Jordan Niethe

Commit 74451e66d516 ("bpf: make jited programs visible in traces") added a default bpf_jit_free() implementation. Powerpc did not use the default bpf_jit_free() as powerpc did not set the images read-only. The default bpf_jit_free() called bpf_jit_binary_unlock_ro() is why it could not be used for powerpc.

Commit d53d2f78cead ("bpf: Use vmalloc special flag") moved keeping track of read-only memory to vmalloc. This included removing bpf_jit_binary_unlock_ro(). Therefore there is no reason powerpc needs its own bpf_jit_free(). Remove it.

---
v11: New to series

---
 arch/powerpc/net/bpf_jit_comp.c | 12 ------------
 1 file changed, 12 deletions(-)
arch/powerpc/net/bpf_jit_comp.c +0 -12
index 798ac4350a82..6c8c268e4fe8 100644
@@ -257,15 +257,3 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
 
 	return fp;
 }
-
-/* Overriding bpf_jit_free() as we don't set images read-only. */
-void bpf_jit_free(struct bpf_prog *fp)
-{
-	unsigned long addr = (unsigned long)fp->bpf_func & PAGE_MASK;
-	struct bpf_binary_header *bpf_hdr = (void *)addr;
-
-	if (fp->jited)
-		bpf_jit_binary_free(bpf_hdr);
-
-	bpf_prog_unlock_free(fp);
-}
Signature · 1 line
2.25.1
#11 lore raw

PATCH v14 6/9 powerpc/bpf: Write protect JIT code

In reply to Jordan Niethe

Add the necessary call to bpf_jit_binary_lock_ro() to remove write and add exec permissions to the JIT image after it has finished being written.

Without CONFIG_STRICT_MODULE_RWX the image will be writable and executable until the call to bpf_jit_binary_lock_ro().

---
v10: New to series
v11: Remove CONFIG_STRICT_MODULE_RWX conditional

---
 arch/powerpc/net/bpf_jit_comp.c | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/net/bpf_jit_comp.c +1 -0
index 6c8c268e4fe8..53aefee3fe70 100644
@@ -237,6 +237,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
 	fp->jited_len = alloclen;
 
 	bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + (bpf_hdr->pages * PAGE_SIZE));
+	bpf_jit_binary_lock_ro(bpf_hdr);
 	if (!fp->is_func || extra_pass) {
 		bpf_prog_fill_jited_linfo(fp, addrs);
 out_addrs:
Signature · 1 line
2.25.1
#12 lore raw

Re: [PATCH v14 6/9] powerpc/bpf: Write protect JIT code

Le 17/05/2021 à 05:28, Jordan Niethe a écrit :

Quoted 6 lines

Add the necessary call to bpf_jit_binary_lock_ro() to remove write and add exec permissions to the JIT image after it has finished being written.

Without CONFIG_STRICT_MODULE_RWX the image will be writable and executable until the call to bpf_jit_binary_lock_ro().

And _with_ CONFIG_STRICT_MODULE_RWX what will happen ? It will be _writable_ but not _executable_ ?

Quoted 23 lines

---
v10: New to series
v11: Remove CONFIG_STRICT_MODULE_RWX conditional

---
  arch/powerpc/net/bpf_jit_comp.c | 1 +
  1 file changed, 1 insertion(+)
arch/powerpc/net/bpf_jit_comp.c +1 -0
index 6c8c268e4fe8..53aefee3fe70 100644
@@ -237,6 +237,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
  	fp->jited_len = alloclen;
 
  	bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + (bpf_hdr->pages * PAGE_SIZE));
+	bpf_jit_binary_lock_ro(bpf_hdr);
  	if (!fp->is_func || extra_pass) {
  		bpf_prog_fill_jited_linfo(fp, addrs);
  out_addrs:
#13 lore raw

Re: [PATCH v14 6/9] powerpc/bpf: Write protect JIT code

Quoted 4 lines · Christophe Leroy

Le 17/05/2021 à 05:28, Jordan Niethe a écrit :

Quoted 8 lines
Quoted 6 lines

Add the necessary call to bpf_jit_binary_lock_ro() to remove write and add exec permissions to the JIT image after it has finished being written.

Without CONFIG_STRICT_MODULE_RWX the image will be writable and executable until the call to bpf_jit_binary_lock_ro().

And _with_ CONFIG_STRICT_MODULE_RWX what will happen ? It will be _writable_ but not _executable_ ?

That's right.
With CONFIG_STRICT_MODULE_RWX the image will initially be PAGE_KERNEL from bpf_jit_alloc_exec() calling module_alloc(). So not executable. bpf_jit_binary_lock_ro() will then remove write and add executable.

Without CONFIG_STRICT_MODULE_RWX the image will initially be PAGE_KERNEL_EXEC from module_alloc(). bpf_jit_binary_lock_ro() will remove write, but until that point it will have been write + exec.

Quoted 24 lines
Quoted 24 lines

---
v10: New to series
v11: Remove CONFIG_STRICT_MODULE_RWX conditional

---
  arch/powerpc/net/bpf_jit_comp.c | 1 +
  1 file changed, 1 insertion(+)
arch/powerpc/net/bpf_jit_comp.c +1 -0
index 6c8c268e4fe8..53aefee3fe70 100644
@@ -237,6 +237,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
      fp->jited_len = alloclen;
 
      bpf_flush_icache(bpf_hdr, (u8 *)bpf_hdr + (bpf_hdr->pages * PAGE_SIZE));
+     bpf_jit_binary_lock_ro(bpf_hdr);
      if (!fp->is_func || extra_pass) {
              bpf_prog_fill_jited_linfo(fp, addrs);
  out_addrs:
#14 lore raw

PATCH v14 7/9 powerpc: Set ARCH_HAS_STRICT_MODULE_RWX

In reply to Jordan Niethe

From: Russell Currey <[email protected]>

To enable strict module RWX on powerpc, set:

CONFIG_STRICT_MODULE_RWX=y

You should also have CONFIG_STRICT_KERNEL_RWX=y set to have any real security benefit.

ARCH_HAS_STRICT_MODULE_RWX is set to require ARCH_HAS_STRICT_KERNEL_RWX. This is due to a quirk in arch/Kconfig and arch/powerpc/Kconfig that makes STRICT_MODULE_RWX *on by default* in configurations where STRICT_KERNEL_RWX is *unavailable*.

Since this doesn't make much sense, and module RWX without kernel RWX doesn't make much sense, having the same dependencies as kernel RWX works around this problem.

Book32s/32 processors with a hash mmu (i.e. 604 core) can not set memory protection on a page by page basis so do not enable.

[jpn: - predicate on !PPC_BOOK3S_604
- make module_alloc() use PAGE_KERNEL protection]

---
v10: - Predicate on !PPC_BOOK3S_604
- Make module_alloc() use PAGE_KERNEL protection
v11: - Neaten up
v13: Use strict_kernel_rwx_enabled()
v14: Make changes to module_alloc() its own commit

---
 arch/powerpc/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index cce0a137b046..cb5d9d862c35 100644
@@ -140,6 +140,7 @@ config PPC
 	select ARCH_HAS_SCALED_CPUTIME		if VIRT_CPU_ACCOUNTING_NATIVE && PPC_BOOK3S_64
 	select ARCH_HAS_SET_MEMORY
 	select ARCH_HAS_STRICT_KERNEL_RWX	if ((PPC_BOOK3S_64 || PPC32) && !HIBERNATION)
+	select ARCH_HAS_STRICT_MODULE_RWX	if ARCH_HAS_STRICT_KERNEL_RWX && !PPC_BOOK3S_604
 	select ARCH_HAS_TICK_BROADCAST		if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_HAS_UACCESS_FLUSHCACHE
 	select ARCH_HAS_COPY_MC			if PPC64
Signature · 1 line
2.25.1
Reviewed-by on v14 7/9
#15 lore raw

Re: [PATCH v14 7/9] powerpc: Set ARCH_HAS_STRICT_MODULE_RWX

Le 17/05/2021 à 05:28, Jordan Niethe a écrit :

Quoted 14 lines

From: Russell Currey <[email protected]>

To enable strict module RWX on powerpc, set:

CONFIG_STRICT_MODULE_RWX=y

You should also have CONFIG_STRICT_KERNEL_RWX=y set to have any real security benefit.

ARCH_HAS_STRICT_MODULE_RWX is set to require ARCH_HAS_STRICT_KERNEL_RWX. This is due to a quirk in arch/Kconfig and arch/powerpc/Kconfig that makes STRICT_MODULE_RWX *on by default* in configurations where STRICT_KERNEL_RWX is *unavailable*.

Quoted 5 lines

Since this doesn't make much sense, and module RWX without kernel RWX doesn't make much sense, having the same dependencies as kernel RWX works around this problem.

Book32s/32 processors with a hash mmu (i.e. 604 core) can not set memory

^^^^^^

Book32s ==> Book3s

Quoted 1 line

protection on a page by page basis so do not enable.

It is not exactly that. The problem on 604 is for _exec_ protection.

Note that on book3s/32, on both 603 and 604 core, it is not possible to write protect kernel pages. So maybe it would make sense to disable ARCH_HAS_STRICT_MODULE_RWX on CONFIG_PPC_BOOK3S_32 completely, I'm not sure.

Quoted 5 lines

[jpn: - predicate on !PPC_BOOK3S_604
- make module_alloc() use PAGE_KERNEL protection]

Quoted 23 lines

---
v10: - Predicate on !PPC_BOOK3S_604
- Make module_alloc() use PAGE_KERNEL protection
v11: - Neaten up
v13: Use strict_kernel_rwx_enabled()
v14: Make changes to module_alloc() its own commit

---
  arch/powerpc/Kconfig | 1 +
  1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index cce0a137b046..cb5d9d862c35 100644
@@ -140,6 +140,7 @@ config PPC
  	select ARCH_HAS_SCALED_CPUTIME		if VIRT_CPU_ACCOUNTING_NATIVE && PPC_BOOK3S_64
  	select ARCH_HAS_SET_MEMORY
  	select ARCH_HAS_STRICT_KERNEL_RWX	if ((PPC_BOOK3S_64 || PPC32) && !HIBERNATION)
+	select ARCH_HAS_STRICT_MODULE_RWX	if ARCH_HAS_STRICT_KERNEL_RWX && !PPC_BOOK3S_604
  	select ARCH_HAS_TICK_BROADCAST		if GENERIC_CLOCKEVENTS_BROADCAST
  	select ARCH_HAS_UACCESS_FLUSHCACHE
  	select ARCH_HAS_COPY_MC			if PPC64
#16 lore raw

Re: [PATCH v14 7/9] powerpc: Set ARCH_HAS_STRICT_MODULE_RWX

Quoted 18 lines · Christophe Leroy
Quoted 14 lines · Jordan Niethe

From: Russell Currey <[email protected]>

To enable strict module RWX on powerpc, set:

CONFIG_STRICT_MODULE_RWX=y

You should also have CONFIG_STRICT_KERNEL_RWX=y set to have any real security benefit.

ARCH_HAS_STRICT_MODULE_RWX is set to require ARCH_HAS_STRICT_KERNEL_RWX. This is due to a quirk in arch/Kconfig and arch/powerpc/Kconfig that makes STRICT_MODULE_RWX *on by default* in configurations where STRICT_KERNEL_RWX is *unavailable*.

Quoted 8 lines
Quoted 5 lines

Since this doesn't make much sense, and module RWX without kernel RWX doesn't make much sense, having the same dependencies as kernel RWX works around this problem.

Book32s/32 processors with a hash mmu (i.e. 604 core) can not set memory

^^^^^^

Book32s ==> Book3s

Thanks.

Quoted 4 lines
Quoted 2 lines

protection on a page by page basis so do not enable.

It is not exactly that. The problem on 604 is for _exec_ protection.

Right.

Quoted 4 lines

Note that on book3s/32, on both 603 and 604 core, it is not possible to write protect kernel pages. So maybe it would make sense to disable ARCH_HAS_STRICT_MODULE_RWX on CONFIG_PPC_BOOK3S_32 completely, I'm not sure.

Yeah, that does seem like it would make sense to disable it.

Quoted 33 lines
Quoted 7 lines

[jpn: - predicate on !PPC_BOOK3S_604
- make module_alloc() use PAGE_KERNEL protection]

Quoted 23 lines

---
v10: - Predicate on !PPC_BOOK3S_604
- Make module_alloc() use PAGE_KERNEL protection
v11: - Neaten up
v13: Use strict_kernel_rwx_enabled()
v14: Make changes to module_alloc() its own commit

---
  arch/powerpc/Kconfig | 1 +
  1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index cce0a137b046..cb5d9d862c35 100644
@@ -140,6 +140,7 @@ config PPC
      select ARCH_HAS_SCALED_CPUTIME          if VIRT_CPU_ACCOUNTING_NATIVE && PPC_BOOK3S_64
      select ARCH_HAS_SET_MEMORY
      select ARCH_HAS_STRICT_KERNEL_RWX       if ((PPC_BOOK3S_64 || PPC32) && !HIBERNATION)
+     select ARCH_HAS_STRICT_MODULE_RWX       if ARCH_HAS_STRICT_KERNEL_RWX && !PPC_BOOK3S_604
      select ARCH_HAS_TICK_BROADCAST          if GENERIC_CLOCKEVENTS_BROADCAST
      select ARCH_HAS_UACCESS_FLUSHCACHE
      select ARCH_HAS_COPY_MC                 if PPC64
#17 lore raw

PATCH v14 8/9 powerpc/mm: implement set_memory_attr()

In reply to Jordan Niethe

From: Christophe Leroy <[email protected]>

In addition to the set_memory_xx() functions which allows to change the memory attributes of not (yet) used memory regions, implement a set_memory_attr() function to:
- set the final memory protection after init on currently used kernel regions.
- enable/disable kernel memory regions in the scope of DEBUG_PAGEALLOC.

Unlike the set_memory_xx() which can act in three step as the regions are unused, this function must modify 'on the fly' as the kernel is executing from them. At the moment only PPC32 will use it and changing page attributes on the fly is not an issue.

[ruscur: cast "data" to unsigned long instead of int]
---
 arch/powerpc/include/asm/set_memory.h |  2 ++
 arch/powerpc/mm/pageattr.c            | 33 +++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
arch/powerpc/include/asm/set_memory.h +2 -0
index 64011ea444b4..b040094f7920 100644
@@ -29,4 +29,6 @@ static inline int set_memory_x(unsigned long addr, int numpages)
 	return change_memory_attr(addr, numpages, SET_MEMORY_X);
 }
 
+int set_memory_attr(unsigned long addr, int numpages, pgprot_t prot);
+
 #endif
arch/powerpc/mm/pageattr.c +33 -0
index 5e5ae50a7f23..0876216ceee6 100644
@@ -99,3 +99,36 @@ int change_memory_attr(unsigned long addr, int numpages, long action)
 	return apply_to_existing_page_range(&init_mm, start, size,
 					    change_page_attr, (void *)action);
 }
+
+/*
+ * Set the attributes of a page:
+ *
+ * This function is used by PPC32 at the end of init to set final kernel memory
+ * protection. It includes changing the maping of the page it is executing from
+ * and data pages it is using.
+ */
+static int set_page_attr(pte_t *ptep, unsigned long addr, void *data)
+{
+	pgprot_t prot = __pgprot((unsigned long)data);
+
+	spin_lock(&init_mm.page_table_lock);
+
+	set_pte_at(&init_mm, addr, ptep, pte_modify(*ptep, prot));
+	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
+
+	spin_unlock(&init_mm.page_table_lock);
+
+	return 0;
+}
+
+int set_memory_attr(unsigned long addr, int numpages, pgprot_t prot)
+{
+	unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
+	unsigned long sz = numpages * PAGE_SIZE;
+
+	if (numpages <= 0)
+		return 0;
+
+	return apply_to_existing_page_range(&init_mm, start, sz, set_page_attr,
+					    (void *)pgprot_val(prot));
+}
Signature · 1 line
2.25.1
#18 lore raw

PATCH v14 9/9 powerpc/32: use set_memory_attr()

In reply to Jordan Niethe

From: Christophe Leroy <[email protected]>

Use set_memory_attr() instead of the PPC32 specific change_page_attr()

change_page_attr() was checking that the address was not mapped by blocks and was handling highmem, but that's unneeded because the affected pages can't be in highmem and block mapping verification is already done by the callers.

[ruscur: rebase on powerpc/merge with Christophe's new patches]
---
 arch/powerpc/mm/pgtable_32.c | 60 ++++++------------------------------
 1 file changed, 10 insertions(+), 50 deletions(-)
arch/powerpc/mm/pgtable_32.c +10 -50
index e0ec67a16887..dcf5ecca19d9 100644
@@ -23,6 +23,7 @@
 #include <linux/highmem.h>
 #include <linux/memblock.h>
 #include <linux/slab.h>
+#include <linux/set_memory.h>
 
 #include <asm/pgalloc.h>
 #include <asm/fixmap.h>
@@ -132,64 +133,20 @@ void __init mapin_ram(void)
 	}
 }
 
-static int __change_page_attr_noflush(struct page *page, pgprot_t prot)
-{
-	pte_t *kpte;
-	unsigned long address;
-
-	BUG_ON(PageHighMem(page));
-	address = (unsigned long)page_address(page);
-
-	if (v_block_mapped(address))
-		return 0;
-	kpte = virt_to_kpte(address);
-	if (!kpte)
-		return -EINVAL;
-	__set_pte_at(&init_mm, address, kpte, mk_pte(page, prot), 0);
-
-	return 0;
-}
-
-/*
- * Change the page attributes of an page in the linear mapping.
- *
- * THIS DOES NOTHING WITH BAT MAPPINGS, DEBUG USE ONLY
- */
-static int change_page_attr(struct page *page, int numpages, pgprot_t prot)
-{
-	int i, err = 0;
-	unsigned long flags;
-	struct page *start = page;
-
-	local_irq_save(flags);
-	for (i = 0; i < numpages; i++, page++) {
-		err = __change_page_attr_noflush(page, prot);
-		if (err)
-			break;
-	}
-	wmb();
-	local_irq_restore(flags);
-	flush_tlb_kernel_range((unsigned long)page_address(start),
-			       (unsigned long)page_address(page));
-	return err;
-}
-
 void mark_initmem_nx(void)
 {
-	struct page *page = virt_to_page(_sinittext);
 	unsigned long numpages = PFN_UP((unsigned long)_einittext) -
 				 PFN_DOWN((unsigned long)_sinittext);
 
 	if (v_block_mapped((unsigned long)_sinittext))
 		mmu_mark_initmem_nx();
 	else
-		change_page_attr(page, numpages, PAGE_KERNEL);
+		set_memory_attr((unsigned long)_sinittext, numpages, PAGE_KERNEL);
 }
 
 #ifdef CONFIG_STRICT_KERNEL_RWX
 void mark_rodata_ro(void)
 {
-	struct page *page;
 	unsigned long numpages;
 
 	if (v_block_mapped((unsigned long)_stext + 1)) {
@@ -198,20 +155,18 @@ void mark_rodata_ro(void)
 		return;
 	}
 
-	page = virt_to_page(_stext);
 	numpages = PFN_UP((unsigned long)_etext) -
 		   PFN_DOWN((unsigned long)_stext);
 
-	change_page_attr(page, numpages, PAGE_KERNEL_ROX);
+	set_memory_attr((unsigned long)_stext, numpages, PAGE_KERNEL_ROX);
 	/*
 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
 	 * to cover NOTES and EXCEPTION_TABLE.
 	 */
-	page = virt_to_page(__start_rodata);
 	numpages = PFN_UP((unsigned long)__init_begin) -
 		   PFN_DOWN((unsigned long)__start_rodata);
 
-	change_page_attr(page, numpages, PAGE_KERNEL_RO);
+	set_memory_attr((unsigned long)__start_rodata, numpages, PAGE_KERNEL_RO);
 
 	// mark_initmem_nx() should have already run by now
 	ptdump_check_wx();
@@ -221,9 +176,14 @@ void mark_rodata_ro(void)
 #ifdef CONFIG_DEBUG_PAGEALLOC
 void __kernel_map_pages(struct page *page, int numpages, int enable)
 {
+	unsigned long addr = (unsigned long)page_address(page);
+
 	if (PageHighMem(page))
 		return;
 
-	change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0));
+	if (enable)
+		set_memory_attr(addr, numpages, PAGE_KERNEL);
+	else
+		set_memory_attr(addr, numpages, __pgprot(0));
 }
 #endif /* CONFIG_DEBUG_PAGEALLOC */
Signature · 1 line
2.25.1