mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory permissions
@ 2026-06-08  6:38 haoran.jiang
  2026-06-09  8:33 ` Huacai Chen
  0 siblings, 1 reply; 3+ messages in thread
From: haoran.jiang @ 2026-06-08  6:38 UTC (permalink / raw)
  To: loongarch
  Cc: linux-kernel, chenhuacai, kernel, akpm, jbohac, kees, yangtiezhu,
	Haoran Jiang

From: Haoran Jiang <jianghaoran@kylinos.cn>

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.

Signed-off-by: Haoran Jiang <jianghaoran@kylinos.cn>
---
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(+)

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 606597da46b8..40d748a13c50 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -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
diff --git a/arch/loongarch/kernel/inst.c b/arch/loongarch/kernel/inst.c
index 0b9228b7c13a..877890c77d0c 100644
--- a/arch/loongarch/kernel/inst.c
+++ b/arch/loongarch/kernel/inst.c
@@ -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;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-06-11  6:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-08  6:38 [PATCH v3] LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory permissions haoran.jiang
2026-06-09  8:33 ` Huacai Chen
2026-06-11  6:27   ` Tiezhu Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome