Skip to contentSkip to conversation

riscv: fix text patching on minimal kernel configs

PATCH In review Xiaofeng Yuan 4 participants 7 messages Aug 14 RISC-V

lore
Share & tools
lore
#0 lore raw

v5 cover riscv: fix text patching on minimal kernel configs

This series fixes two related issues that break kprobes and kernel text patching on RISC-V configurations where CONFIG_STRICT_MODULE_RWX is not available (e.g. CONFIG_MODULES=n).

Patch 1 makes the EXECMEM_KPROBES range writable when CONFIG_STRICT_MODULE_RWX is disabled, so kprobe instruction slot writes do not fault on read-only executable pages.

Patch 2 makes patch_map() skip the temporary fixmap mapping when the kernel text is already writable, i.e. when CONFIG_STRICT_KERNEL_RWX is disabled. The module text path is still gated on CONFIG_STRICT_MODULE_RWX.

Changes since v4:
- 2/2: gate the kernel text fixmap on CONFIG_STRICT_KERNEL_RWX instead of returning early on !CONFIG_STRICT_MODULE_RWX. The v4 early return broke configs with CONFIG_MODULES=n, where CONFIG_STRICT_MODULE_RWX is unavailable but CONFIG_STRICT_KERNEL_RWX is still enabled and the kernel text is read-only, causing a boot-time kernel panic on BPI-F3 and RZ/Five (reported by Klara Modin and Lad Prabhakar).

Xiaofeng Yuan (2):
riscv: mm: make EXECMEM_KPROBES writable without
CONFIG_STRICT_MODULE_RWX
riscv: patch: skip fixmap mapping when kernel text is already writable

 arch/riscv/kernel/patch.c | 2 ++
 arch/riscv/mm/init.c      | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/2 riscv: mm: make EXECMEM_KPROBES writable without CONFIG_STRICT_MODULE_RWX

When CONFIG_STRICT_MODULE_RWX is not set, execmem cannot create temporary writable mappings for read-only executable pages. In this case, the execmem ranges must already have writable permissions.

Currently EXECMEM_KPROBES unconditionally uses PAGE_KERNEL_READ_EXEC, which causes kprobe instruction slot writes to trigger page faults on systems where CONFIG_STRICT_MODULE_RWX is not enabled.

Fix this by using PAGE_KERNEL_EXEC when CONFIG_STRICT_MODULE_RWX is not available.

---
 arch/riscv/mm/init.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/mm/init.c +3 -1
index 85bdcce63a..b084a72e5d 100644
@@ -1462,7 +1462,9 @@ struct execmem_info __init *execmem_arch_setup(void)
 			[EXECMEM_KPROBES] = {
 				.start	= VMALLOC_START,
 				.end	= VMALLOC_END,
-				.pgprot	= PAGE_KERNEL_READ_EXEC,
+				.pgprot	= IS_ENABLED(CONFIG_STRICT_MODULE_RWX) ?
+					  PAGE_KERNEL_READ_EXEC :
+					  PAGE_KERNEL_EXEC,
 				.alignment = 1,
 			},
 			[EXECMEM_BPF] = {
Signature · 1 line
2.43.0
Tested-by Lad Prabhakar on v5 1/2
#2 lore raw

Re: [PATCH v5 1/2] riscv: mm: make EXECMEM_KPROBES writable without CONFIG_STRICT_MODULE_RWX

Quoted 9 lines · Xiaofeng Yuan

When CONFIG_STRICT_MODULE_RWX is not set, execmem cannot create temporary writable mappings for read-only executable pages. In this case, the execmem ranges must already have writable permissions.

Currently EXECMEM_KPROBES unconditionally uses PAGE_KERNEL_READ_EXEC, which causes kprobe instruction slot writes to trigger page faults on systems where CONFIG_STRICT_MODULE_RWX is not enabled.

Quoted 8 lines

Fix this by using PAGE_KERNEL_EXEC when CONFIG_STRICT_MODULE_RWX is not available.

---
 arch/riscv/mm/init.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Cheers,
Prabhakar

Quoted 18 lines
arch/riscv/mm/init.c +3 -2
index 85bdcce63a..b084a72e5d 100644
@@ -1462,7 +1462,9 @@ struct execmem_info __init *execmem_arch_setup(void)
                        [EXECMEM_KPROBES] = {
                                .start  = VMALLOC_START,
                                .end    = VMALLOC_END,
-                               .pgprot = PAGE_KERNEL_READ_EXEC,
+                               .pgprot = IS_ENABLED(CONFIG_STRICT_MODULE_RWX) ?
+                                         PAGE_KERNEL_READ_EXEC :
+                                         PAGE_KERNEL_EXEC,
                                .alignment = 1,
                        },
                        [EXECMEM_BPF] = {
--

2.43.0

#3 lore raw

PATCH v5 2/2 riscv: patch: skip fixmap mapping when kernel text is already writable

In reply to Xiaofeng Yuan

patch_map() always creates a temporary writable mapping via fixmap for kernel text addresses, even when CONFIG_STRICT_KERNEL_RWX is disabled and the kernel text is already mapped with _PAGE_WRITE.

This is unnecessary overhead at best, and on minimal configurations it can cause page faults.

Skip the fixmap path for kernel text when CONFIG_STRICT_KERNEL_RWX is not enabled, since the text pages are already writable in that case. The module text path is already gated on CONFIG_STRICT_MODULE_RWX and is kept unchanged.

---
v5: fix the fixmap gating for kernel text. The v4 early return on
CONFIG_STRICT_MODULE_RWX wrongly skipped the fixmap for kernel text too, which panics on configs with CONFIG_MODULES=n where CONFIG_STRICT_MODULE_RWX is unavailable but CONFIG_STRICT_KERNEL_RWX is still enabled and kernel text is read-only.
v3: early return when !CONFIG_STRICT_MODULE_RWX (per Nam Cao's suggestion)
v2: add commit description

---
 arch/riscv/kernel/patch.c | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/kernel/patch.c +2 -0
index 16b243376f..2239c28981 100644
@@ -45,6 +45,8 @@ static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
 	phys_addr_t phys;
 
 	if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr)) {
+		if (!IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
+			return addr;
 		phys = __pa_symbol(addr);
 	} else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) {
 		struct page *page = vmalloc_to_page(addr);
Signature · 1 line
2.43.0
Tested-by on v5 2/2
#4 lore raw

Re: [PATCH v5 2/2] riscv: patch: skip fixmap mapping when kernel text is already writable

Quoted 25 lines · Xiaofeng Yuan

patch_map() always creates a temporary writable mapping via fixmap for kernel text addresses, even when CONFIG_STRICT_KERNEL_RWX is disabled and the kernel text is already mapped with _PAGE_WRITE.

This is unnecessary overhead at best, and on minimal configurations it can cause page faults.

Skip the fixmap path for kernel text when CONFIG_STRICT_KERNEL_RWX is not enabled, since the text pages are already writable in that case. The module text path is already gated on CONFIG_STRICT_MODULE_RWX and is kept unchanged.

---
v5: fix the fixmap gating for kernel text. The v4 early return on
CONFIG_STRICT_MODULE_RWX wrongly skipped the fixmap for kernel text too, which panics on configs with CONFIG_MODULES=n where CONFIG_STRICT_MODULE_RWX is unavailable but CONFIG_STRICT_KERNEL_RWX is still enabled and kernel text is read-only.
v3: early return when !CONFIG_STRICT_MODULE_RWX (per Nam Cao's suggestion)
v2: add commit description

Quoted 1 line

This version works fine for me.

Thanks,

Quoted 20 lines
---
 arch/riscv/kernel/patch.c | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/kernel/patch.c +2 -0
index 16b243376f..2239c28981 100644
@@ -45,6 +45,8 @@ static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
 	phys_addr_t phys;
 
 	if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr)) {
+		if (!IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
+			return addr;
 		phys = __pa_symbol(addr);
 	} else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) {
 		struct page *page = vmalloc_to_page(addr);
Signature · 1 line
2.43.0
Tested-by Lad Prabhakar on v5 2/2
#5 lore raw

Re: [PATCH v5 2/2] riscv: patch: skip fixmap mapping when kernel text is already writable

In reply to Xiaofeng Yuan
Quoted 26 lines · Xiaofeng Yuan

patch_map() always creates a temporary writable mapping via fixmap for kernel text addresses, even when CONFIG_STRICT_KERNEL_RWX is disabled and the kernel text is already mapped with _PAGE_WRITE.

This is unnecessary overhead at best, and on minimal configurations it can cause page faults.

Skip the fixmap path for kernel text when CONFIG_STRICT_KERNEL_RWX is not enabled, since the text pages are already writable in that case. The module text path is already gated on CONFIG_STRICT_MODULE_RWX and is kept unchanged.

---
v5: fix the fixmap gating for kernel text. The v4 early return on
CONFIG_STRICT_MODULE_RWX wrongly skipped the fixmap for kernel text too, which panics on configs with CONFIG_MODULES=n where CONFIG_STRICT_MODULE_RWX is unavailable but CONFIG_STRICT_KERNEL_RWX is still enabled and kernel text is read-only.
v3: early return when !CONFIG_STRICT_MODULE_RWX (per Nam Cao's suggestion)
v2: add commit description

Quoted 5 lines
---
 arch/riscv/kernel/patch.c | 2 ++
 1 file changed, 2 insertions(+)

Cheers,
Prabhakar

Quoted 16 lines
arch/riscv/kernel/patch.c +2 -1
index 16b243376f..2239c28981 100644
@@ -45,6 +45,8 @@ static __always_inline void *patch_map(void *addr, const unsigned int fixmap)
        phys_addr_t phys;
 
        if (core_kernel_text(uintaddr) || is_kernel_exittext(uintaddr)) {
+               if (!IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
+                       return addr;
                phys = __pa_symbol(addr);
        } else if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) {
                struct page *page = vmalloc_to_page(addr);
--

2.43.0

maintainer
#6 lore raw
In reply to Xiaofeng Yuan
Quoted 11 lines · Xiaofeng Yuan

This series fixes two related issues that break kprobes and kernel text patching on RISC-V configurations where CONFIG_STRICT_MODULE_RWX is not available (e.g. CONFIG_MODULES=n).

Patch 1 makes the EXECMEM_KPROBES range writable when CONFIG_STRICT_MODULE_RWX is disabled, so kprobe instruction slot writes do not fault on read-only executable pages.

Patch 2 makes patch_map() skip the temporary fixmap mapping when the kernel text is already writable, i.e. when CONFIG_STRICT_KERNEL_RWX is disabled. The module text path is still gated on CONFIG_STRICT_MODULE_RWX.

Thanks, queued for v7.3-rc.

- Paul