LoongArch architecture development
 help / color / mirror / Atom feed
From: haoran.jiang@linux.dev
To: loongarch@lists.linux.dev
Cc: linux-kernel@vger.kernel.org, chenhuacai@kernel.org,
	kernel@xen0n.name, akpm@linux-foundation.org, jbohac@suse.cz,
	kees@kernel.org, yangtiezhu@loongson.cn,
	Haoran Jiang <jianghaoran@kylinos.cn>
Subject: [PATCH 2/2] LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory permissions
Date: Sat,  6 Jun 2026 21:21:26 +0800	[thread overview]
Message-ID: <20260606132126.562034-3-haoran.jiang@linux.dev> (raw)
In-Reply-To: <20260606132126.562034-1-haoran.jiang@linux.dev>

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.Add patch_map interface to temporarily map
read-only code sections via fixmap, enabling runtime code patching
while preserving memory protection after modifications

Signed-off-by: Haoran Jiang <jianghaoran@kylinos.cn>
---
 arch/loongarch/Kconfig              |  2 ++
 arch/loongarch/include/asm/fixmap.h |  1 +
 arch/loongarch/kernel/inst.c        | 29 ++++++++++++++++++++++++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)

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/include/asm/fixmap.h b/arch/loongarch/include/asm/fixmap.h
index 5a9b04c720bf..7d174eafdc21 100644
--- a/arch/loongarch/include/asm/fixmap.h
+++ b/arch/loongarch/include/asm/fixmap.h
@@ -22,6 +22,7 @@ enum fixed_addresses {
 	FIX_KMAP_END = FIX_KMAP_BEGIN + (KM_MAX_IDX * NR_CPUS) - 1,
 #endif
 	FIX_EARLYCON_MEM_BASE,
+	FIX_TEXT_POKE0,
 	__end_of_fixed_addresses
 };
 
diff --git a/arch/loongarch/kernel/inst.c b/arch/loongarch/kernel/inst.c
index 0b9228b7c13a..7c4f60a7e892 100644
--- a/arch/loongarch/kernel/inst.c
+++ b/arch/loongarch/kernel/inst.c
@@ -9,9 +9,33 @@
 
 #include <asm/cacheflush.h>
 #include <asm/inst.h>
+#include <linux/kprobes.h>
 
 static DEFINE_RAW_SPINLOCK(patch_lock);
 
+static void __kprobes *patch_map(void *addr, const unsigned int fixmap)
+{
+	phys_addr_t phys;
+
+	if ((unsigned long)addr < vm_map_base) {
+		return addr;
+	} else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) {
+		struct page *page = vmalloc_to_page(addr);
+
+		BUG_ON(!page);
+		phys = page_to_phys(page) + offset_in_page(addr);
+	} else {
+		return addr;
+	}
+
+	return (void *)set_fixmap_offset(fixmap, phys);
+}
+
+static void __kprobes patch_unmap(int fixmap)
+{
+	clear_fixmap(fixmap);
+}
+
 void simu_pc(struct pt_regs *regs, union loongarch_instruction insn)
 {
 	unsigned long pc = regs->csr_era;
@@ -208,12 +232,15 @@ int larch_insn_write(void *addr, u32 insn)
 {
 	int ret;
 	unsigned long flags = 0;
+	void *waddr = addr;
 
 	if ((unsigned long)addr & 3)
 		return -EINVAL;
 
 	raw_spin_lock_irqsave(&patch_lock, flags);
-	ret = copy_to_kernel_nofault(addr, &insn, LOONGARCH_INSN_SIZE);
+	waddr = patch_map(addr, FIX_TEXT_POKE0);
+	ret = copy_to_kernel_nofault(waddr, &insn, LOONGARCH_INSN_SIZE);
+	patch_unmap(FIX_TEXT_POKE0);
 	raw_spin_unlock_irqrestore(&patch_lock, flags);
 
 	return ret;
-- 
2.25.1


  parent reply	other threads:[~2026-06-06 13:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-06 13:21 [PATCH 0/2] Enable STRICT_MODULE_RWX haoran.jiang
2026-06-06 13:21 ` [PATCH 1/2] LoongArch: Move fixmap page tables to BSS segment haoran.jiang
2026-06-06 13:21 ` haoran.jiang [this message]
2026-06-06 14:04 ` [PATCH 0/2] Enable STRICT_MODULE_RWX Huacai Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260606132126.562034-3-haoran.jiang@linux.dev \
    --to=haoran.jiang@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=chenhuacai@kernel.org \
    --cc=jbohac@suse.cz \
    --cc=jianghaoran@kylinos.cn \
    --cc=kees@kernel.org \
    --cc=kernel@xen0n.name \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=yangtiezhu@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox