LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory permissions
PATCH v3 1/1 LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory permissions
From: Haoran Jiang <[email protected]>
Enable STRICT_MODULE_RWX to enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions via set_memory() API.
---
v2:
Change the method of modifying page table permissions from patch_map to set_memory() API.
v3:
Modify commit description.
--- arch/loongarch/Kconfig | 2 ++ arch/loongarch/kernel/inst.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+)
arch/loongarch/Kconfig +2 -0
@@ -27,6 +27,7 @@ config LOONGARCH select ARCH_HAS_PTE_SPECIAL if 64BIT select ARCH_HAS_SET_MEMORY select ARCH_HAS_SET_DIRECT_MAP + select ARCH_HAS_STRICT_MODULE_RWX select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN select ARCH_HAS_VDSO_ARCH_DATA
@@ -197,6 +198,7 @@ config LOONGARCH select NUMA_MEMBLKS if NUMA select OF select OF_EARLY_FLATTREE + select ARCH_OPTIONAL_KERNEL_RWX select PCI select PCI_DOMAINS_GENERIC select PCI_ECAM if ACPI
arch/loongarch/kernel/inst.c +18 -0
@@ -207,13 +207,31 @@ int larch_insn_read(void *addr, u32 *insnp)int larch_insn_write (void *addr,u32 insn) {int ret; +int err =0 ; +size_t start;unsigned long flags =0 ;if ((unsigned long )addr &3 )return -EINVAL ; + start =round_down ((size_t )addr,PAGE_SIZE ); +raw_spin_lock_irqsave (&patch_lock, flags); + + err =set_memory_rw (start,1 ); +if (err) { +pr_info ("%s: set_memory_rw() failed\n" , __func__); +return err; + } + ret =copy_to_kernel_nofault (addr, &insn,LOONGARCH_INSN_SIZE ); + + err =set_memory_rox (start,1 ); +if (err) { +pr_info ("%s: set_memory_rox() failed\n" , __func__); +return err; + } +raw_spin_unlock_irqrestore (&patch_lock, flags);return ret;
Signature · 1 line
2.25.1
Huacai Chen
maintainerHi, Haoran,
Quoted 35 lines · On Mon, Jun 8, 2026 at 2:39 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
Enable
STRICT_MODULE_RWXto enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions viaset_memory()API.Signed-off-by Haoran Jiang <[email protected]>---
v2:
Change the method of modifying page table permissions frompatch_maptoset_memory()API.v3:
Modify commit description.--- arch/loongarch/Kconfig | 2 ++ arch/loongarch/kernel/inst.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+)arch/loongarch/Kconfig +1
-0@@ -27,6 +27,7 @@ config LOONGARCH select ARCH_HAS_PTE_SPECIAL if 64BIT select ARCH_HAS_SET_MEMORY select ARCH_HAS_SET_DIRECT_MAP + select ARCH_HAS_STRICT_MODULE_RWX select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN select ARCH_HAS_VDSO_ARCH_DATA
arch/loongarch/Kconfig config LOONGARCH · line 201
Quoted 4 lines
arch/loongarch/Kconfig +1
-0@@ -197,6 +198,7 @@ config LOONGARCH select NUMA_MEMBLKS if NUMA select OF select OF_EARLY_FLATTREE + select ARCH_OPTIONAL_KERNEL_RWX
Have you compared the performance with and without STRICT_MODULE_RWX? If no performance decreases, I suggest not select ARCH_OPTIONAL_KERNEL_RWX, this will cause STRICT_MODULE_RWX to be set always.
Quoted 17 lines
arch/loongarch/Kconfig +0
-0select PCI select PCI_DOMAINS_GENERIC select PCI_ECAM if ACPIarch/loongarch/kernel/inst.c +2
-0@@ -207,13 +207,31 @@ int larch_insn_read(void *addr, u32 *insnp)int larch_insn_write (void *addr,u32 insn) {int ret; +int err =0 ; +size_t start;unsigned long flags =0 ;if ((unsigned long )addr &3 )return -EINVAL ;
arch/loongarch/kernel/inst.c int larch_insn_read(void *addr, u32 *insnp) · line 225
Quoted 10 lines
arch/loongarch/kernel/inst.c +8
-0+ start =round_down ((size_t )addr,PAGE_SIZE ); +raw_spin_lock_irqsave (&patch_lock, flags); + + err =set_memory_rw (start,1 ); +if (err) { +pr_info ("%s: set_memory_rw() failed\n" , __func__); +return err; + }
I don't know how you verified the function, but it is obvious this should be out of spinlock and irq disabled.
Yes, move it out will cause concurrent problem, so we should use mutex, and the final state of this function will be like this:
err = set_memory_rw(start, 1);
local_irq_save(flags);
ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
local_irq_restore(flags);
err = set_memory_rox(start, 1);Then the callers should use text_mutex to protect.
For example:
1. For ftrace, define ftrace_arch_code_modify_prepare() and ftrace_arch_code_modify_post_process() as RISC-V.
2. For jump labels, protect larch_insn_write() with mutex_lock(&text_mutex) and mutex_unlock(&text_mutex).
3. For kprobes,integrate this patch, and then also protect larch_insn_patch_text() with mutex_lock(&text_mutex) and mutex_unlock(&text_mutex).
In addition, it seems that text_mutex and patch_lock are for the same purpose, so patch_lock can be removed here.
@Tiezhu, what's your opinion?
Huacai
Quoted 15 lines
arch/loongarch/kernel/inst.c +8
-1+ ret =copy_to_kernel_nofault (addr, &insn,LOONGARCH_INSN_SIZE ); + + err =set_memory_rox (start,1 ); +if (err) { +pr_info ("%s: set_memory_rox() failed\n" , __func__); +return err; + } +raw_spin_unlock_irqrestore (&patch_lock, flags);return ret; --2.25.1
Quoted 11 lines · Huacai Chen
Hi, Haoran,
Quoted 8 lines · On Mon, Jun 8, 2026 at 2:39 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
Enable
STRICT_MODULE_RWXto enforce strict memory permissions on modules,making the code region non-writable, the data region non-executable, and the read-only data region both non-writable and non-executable.Temporarily modify code section read/write permissions viaset_memory()API.
...
Quoted 4 lines
In addition, it seems that
text_mutexandpatch_lockare for the same purpose, sopatch_lockcan be removed here.@Tiezhu, what's your opinion?
Agreed. If all callers properly switch to text_mutex, patch_lock can be removed entirely.
Thanks,
Tiezhu