Enable STRICT_MODULE_RWX
cover Enable STRICT_MODULE_RWX
From: Haoran Jiang <[email protected]>
Enable STRICT_MODULE_RWX on LoongArch and
Move fixmap page tables to BSS segment.
Haoran Jiang (2):
LoongArch: Move fixmap page tables to BSS segment
LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory
permissions
arch/loongarch/Kconfig | 2 + arch/loongarch/include/asm/fixmap.h | 2 + arch/loongarch/kernel/inst.c | 29 +++++++++++- arch/loongarch/kernel/setup.c | 1 + arch/loongarch/mm/init.c | 71 ++++++++++++++++++++++++++--- 5 files changed, 98 insertions(+), 7 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 LoongArch: Move fixmap page tables to BSS segment
From: Haoran Jiang <[email protected]>
__set_fixmap() is not only used during initialization but may also be invoked after the kernel is up. Before this change, fixmap page tables resided in memblock. After the change, they are placed in the BSS segment. Thus, the fixmap becomes usable both during early boot and after kernel startup.
--- arch/loongarch/include/asm/fixmap.h | 1 + arch/loongarch/kernel/setup.c | 1 + arch/loongarch/mm/init.c | 71 ++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 6 deletions(-)
arch/loongarch/include/asm/fixmap.h +1 -0
@@ -31,6 +31,7 @@ enum fixed_addresses {extern void __set_fixmap (enum fixed_addresses idx,phys_addr_t phys,pgprot_t flags); +void __init early_fixmap_init (void );#include <asm-generic/fixmap.h>
arch/loongarch/kernel/setup.c +1 -0
@@ -599,6 +599,7 @@ void __init setup_arch(char **cmdline_p)fdt_setup ();memblock_init ();pagetable_init (); +early_fixmap_init ();bootcmdline_init (cmdline_p);parse_early_param ();reserve_initrd_mem ();
arch/loongarch/mm/init.c +65 -6
89 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 2/2 LoongArch: Enable STRICT_MODULE_RWX for stricter modules memory permissions
In reply to [email protected]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.Add patch_map interface to temporarily map read-only code sections via fixmap, enabling runtime code patching while preserving memory protection after modifications
--- arch/loongarch/Kconfig | 2 ++ arch/loongarch/include/asm/fixmap.h | 1 + arch/loongarch/kernel/inst.c | 29 ++++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-)
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/include/asm/fixmap.h +1 -0
@@ -22,6 +22,7 @@ enum fixed_addresses {FIX_KMAP_END =FIX_KMAP_BEGIN + (KM_MAX_IDX *NR_CPUS ) -1 , #endifFIX_EARLYCON_MEM_BASE , +FIX_TEXT_POKE0 , __end_of_fixed_addresses };
arch/loongarch/kernel/inst.c +28 -1
@@ -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;
Signature · 1 line
2.25.1
Huacai Chen
maintainerHi, Haoran,
Quoted 5 lines · On Sat, Jun 6, 2026 at 9:21 PM <[email protected]> wrote:
From: Haoran Jiang <[email protected]>
Enable
STRICT_MODULE_RWXon LoongArch and
Move fixmap page tables to BSS segment.
As discussed before, we don't need to change fixmap, we can just use set_memory() API, similar as done in larch_insn_text_copy().
Huacai
Quoted 17 lines
Haoran Jiang (2):
LoongArch: Move fixmap page tables to BSS segment
LoongArch: EnableSTRICT_MODULE_RWXfor stricter modules memory
permissionsarch/loongarch/Kconfig | 2 + arch/loongarch/include/asm/fixmap.h | 2 + arch/loongarch/kernel/inst.c | 29 +++++++++++- arch/loongarch/kernel/setup.c | 1 + arch/loongarch/mm/init.c | 71 ++++++++++++++++++++++++++--- 5 files changed, 98 insertions(+), 7 deletions(-)--
2.25.1