Skip to contentSkip to conversation

riscv: mm: Proper page permissions after initmem free

PATCH Superseded Björn Töpel 3 participants 4 messages Nov 12, 2022 – Nov 14, 2022 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: mm: Proper page permissions after initmem free

From: Björn Töpel <[email protected]>

64-bit RISC-V kernels have the kernel image mapped separately, and in addition to the linear map. When the kernel is loaded, the linear map of kernel image is set to PAGE_READ permission, and the kernel map is set to PAGE_READ and PAGE_EXEC.

When the initmem is freed, the corresponding pages in the linear map should be restored to PAGE_READ and PAGE_WRITE. The corresponding pages in the kernel map should also be restored to PAGE_READ and PAGE_WRITE, by removing the PAGE_EXEC permission, and adding PAGE_WRITE.

This is not the case. For 64-bit kernels, only the linear map is restored to its proper page permissions at initmem free, and not the kernelmap.

In practise this results in that the kernel can potentially jump to dead __init code, and start executing invalid 0xcc instructions, without getting an exception.

Restore the freed initmem properly, by setting both the alias (kernel map) and the linear map to the correct permissions.

Fixes e5c35fa04019 ("riscv: Map the kernel with correct permissions the first time")
---
 arch/riscv/kernel/setup.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
arch/riscv/kernel/setup.c +6 -4
index ad76bb59b059..361e635070fe 100644
@@ -321,10 +321,12 @@ subsys_initcall(topology_init);
 
 void free_initmem(void)
 {
-	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
-		set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end),
-				  IS_ENABLED(CONFIG_64BIT) ?
-					set_memory_rw : set_memory_rw_nx);
+	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
+		if (IS_ENABLED(CONFIG_64BIT))
+			set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end),
+					  set_memory_rw);
+		set_kernel_memory(__init_begin, __init_end, set_memory_rw_nx);
+	}
 
 	free_initmem_default(POISON_FREE_INITMEM);
 }

base-commit: 442bcbfd2c5401587b983e34bed0b407214735c3

Signature · 1 line
2.37.2
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 21 lines · Björn Töpel

From: Björn Töpel <[email protected]>

64-bit RISC-V kernels have the kernel image mapped separately, and in addition to the linear map. When the kernel is loaded, the linear map of kernel image is set to PAGE_READ permission, and the kernel map is set to PAGE_READ and PAGE_EXEC.

When the initmem is freed, the corresponding pages in the linear map should be restored to PAGE_READ and PAGE_WRITE. The corresponding pages in the kernel map should also be restored to PAGE_READ and PAGE_WRITE, by removing the PAGE_EXEC permission, and adding PAGE_WRITE.

This is not the case. For 64-bit kernels, only the linear map is restored to its proper page permissions at initmem free, and not the kernelmap.

In practise this results in that the kernel can potentially jump to dead __init code, and start executing invalid 0xcc instructions, without getting an exception.

Quoted 8 lines

Restore the freed initmem properly, by setting both the alias (kernel map) and the linear map to the correct permissions.

Fixes e5c35fa04019 ("riscv: Map the kernel with correct permissions the first time")
---
 arch/riscv/kernel/setup.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
#2 lore raw
In reply to Björn Töpel

Hi Björn,

Quoted 37 lines · Björn Töpel

From: Björn Töpel <[email protected]>

64-bit RISC-V kernels have the kernel image mapped separately, and in addition to the linear map. When the kernel is loaded, the linear map of kernel image is set to PAGE_READ permission, and the kernel map is set to PAGE_READ and PAGE_EXEC.

When the initmem is freed, the corresponding pages in the linear map should be restored to PAGE_READ and PAGE_WRITE. The corresponding pages in the kernel map should also be restored to PAGE_READ and PAGE_WRITE, by removing the PAGE_EXEC permission, and adding PAGE_WRITE.

This is not the case. For 64-bit kernels, only the linear map is restored to its proper page permissions at initmem free, and not the kernelmap.

In practise this results in that the kernel can potentially jump to dead __init code, and start executing invalid 0xcc instructions, without getting an exception.

Restore the freed initmem properly, by setting both the alias (kernel map) and the linear map to the correct permissions.

Fixes e5c35fa04019 ("riscv: Map the kernel with correct permissions the first time")
---
  arch/riscv/kernel/setup.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
arch/riscv/kernel/setup.c +0 -0
index ad76bb59b059..361e635070fe 100644
@@ -321,10 +321,12 @@ subsys_initcall(topology_init);
 
  void free_initmem(void)

arch/riscv/kernel/setup.c subsys_initcall(topology_init); · line 328

Quoted 10 lines
arch/riscv/kernel/setup.c +5 -4
  {
-	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
-		set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end),
-				  IS_ENABLED(CONFIG_64BIT) ?
-					set_memory_rw : set_memory_rw_nx);
+	if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
+		if (IS_ENABLED(CONFIG_64BIT))
+			set_kernel_memory(lm_alias(__init_begin), lm_alias(__init_end),
+					  set_memory_rw);
+		set_kernel_memory(__init_begin, __init_end, set_memory_rw_nx);

That's nits but for 64-bit kernels, do we really want to make the kernel mapping writable? I think catching a write access here would be better because IMO that should not happen.

Thanks,

Alex

Quoted 6 lines
arch/riscv/kernel/setup.c +1 -0
+	}
 
  	free_initmem_default(POISON_FREE_INITMEM);
  }

base-commit: 442bcbfd2c5401587b983e34bed0b407214735c3

#3 lore raw
Quoted 6 lines · Alexandre Ghiti

arch/riscv/kernel/setup.c subsys_initcall(topology_init); · line 328

Quoted 1 line
arch/riscv/kernel/setup.c +1 -0
+		set_kernel_memory(__init_begin, __init_end, set_memory_rw_nx);

That's nits but for 64-bit kernels, do we really want to make the kernel mapping writable? I think catching a write access here would be better because IMO that should not happen.

Thanks for having a look!

Hmm, so what you're suggesting is a "set_memory_r_nx" for CONFIG_64BIT? What do other archs do? Is it worth the extra ifdef clutter?

Björn