Skip to contentSkip to conversation

arm64: mm: Defer read-only remap of data/bss linear alias

PATCH Applied Ard Biesheuvel 3 participants 3 messages Jun 23 – Jun 24 arm64

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 arm64: mm: Defer read-only remap of data/bss linear alias

From: Ard Biesheuvel <[email protected]>

Since commit

f2ba877402e5 ("arm64: mm: Map the kernel data/bss read-only in the linear map")

the linear alias of the .data and .bss regions is remapped read-only early during the boot. (Note that a subsequent patch to unmap this region entirely was reverted just before the v7.2 merge window, and will be brought back in an improved form for the v7.3 cycle)

Fuad reports that in some cases, the KVM init code may apply relocations to variables that reside in .data, and does so via the linear map. This means that remapping .data read-only beforehand is a bad idea, and results in an early boot crash.

These variables in .data are only present when CONFIG_NVHE_EL2_DEBUG or CONFIG_NVHE_EL2_TRACING are enabled, which is why it was not spotted in testing.

So move the remap to mark_rodata_ro(), which is a reasonable place to put this, and ensures that it happens much later during the boot. It also means that rodata=off is now taken into account, and so the linear alias will remain writable in that case.

Fixes f2ba877402e5 ("arm64: mm: Map the kernel data/bss read-only in the linear map")

---
v2: improve changelog, add Fuad's R-b

 arch/arm64/mm/mmu.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/arm64/mm/mmu.c +6 -5
index 9f354971b7e4..1f7eca86b5c1 100644
@@ -1198,11 +1198,6 @@ static void __init map_mem(void)
 		__map_memblock(start, end, pgprot_tagged(PAGE_KERNEL),
 			       flags);
 	}
-
-	/* Map the kernel data/bss read-only in the linear map */
-	__map_memblock(init_end, kernel_end, PAGE_KERNEL_RO, flags);
-	flush_tlb_kernel_range((unsigned long)lm_alias(__init_end),
-			       (unsigned long)lm_alias(__bss_stop));
 }
 
 void mark_rodata_ro(void)
@@ -1221,6 +1216,12 @@ void mark_rodata_ro(void)
 	update_mapping_prot(__pa_symbol(_text), (unsigned long)_text,
 			    (unsigned long)_stext - (unsigned long)_text,
 			    PAGE_KERNEL_RO);
+
+	/* Map the kernel data/bss read-only in the linear map */
+	update_mapping_prot(__pa_symbol(__init_end),
+			    (unsigned long)lm_alias(__init_end),
+			    (unsigned long)__bss_stop - (unsigned long)__init_end,
+			    PAGE_KERNEL_RO);
 }
 
 static void __init declare_vma(struct vm_struct *vma,
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 25 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Since commit

f2ba877402e5 ("arm64: mm: Map the kernel data/bss read-only in the linear map")

the linear alias of the .data and .bss regions is remapped read-only early during the boot. (Note that a subsequent patch to unmap this region entirely was reverted just before the v7.2 merge window, and will be brought back in an improved form for the v7.3 cycle)

Fuad reports that in some cases, the KVM init code may apply relocations to variables that reside in .data, and does so via the linear map. This means that remapping .data read-only beforehand is a bad idea, and results in an early boot crash.

These variables in .data are only present when CONFIG_NVHE_EL2_DEBUG or CONFIG_NVHE_EL2_TRACING are enabled, which is why it was not spotted in testing.

So move the remap to mark_rodata_ro(), which is a reasonable place to put this, and ensures that it happens much later during the boot. It also means that rodata=off is now taken into account, and so the linear alias will remain writable in that case.

Quoted 4 lines
Fixes f2ba877402e5 ("arm64: mm: Map the kernel data/bss read-only in the linear map")

Thanks for expanding the commit message!

Quoted 35 lines

---
v2: improve changelog, add Fuad's R-b

 arch/arm64/mm/mmu.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/arm64/mm/mmu.c +6 -5
index 9f354971b7e4..1f7eca86b5c1 100644
@@ -1198,11 +1198,6 @@ static void __init map_mem(void)
 		__map_memblock(start, end, pgprot_tagged(PAGE_KERNEL),
 			       flags);
 	}
-
-	/* Map the kernel data/bss read-only in the linear map */
-	__map_memblock(init_end, kernel_end, PAGE_KERNEL_RO, flags);
-	flush_tlb_kernel_range((unsigned long)lm_alias(__init_end),
-			       (unsigned long)lm_alias(__bss_stop));
 }
 
 void mark_rodata_ro(void)
@@ -1221,6 +1216,12 @@ void mark_rodata_ro(void)
 	update_mapping_prot(__pa_symbol(_text), (unsigned long)_text,
 			    (unsigned long)_stext - (unsigned long)_text,
 			    PAGE_KERNEL_RO);
+
+	/* Map the kernel data/bss read-only in the linear map */
+	update_mapping_prot(__pa_symbol(__init_end),
+			    (unsigned long)lm_alias(__init_end),
+			    (unsigned long)__bss_stop - (unsigned long)__init_end,
+			    PAGE_KERNEL_RO);
 }
 
 static void __init declare_vma(struct vm_struct *vma,
maintainer Applied
#2 lore raw
In reply to Ard Biesheuvel
Quoted 10 lines · Ard Biesheuvel

Since commit

f2ba877402e5 ("arm64: mm: Map the kernel data/bss read-only in the linear map")

the linear alias of the .data and .bss regions is remapped read-only early during the boot. (Note that a subsequent patch to unmap this region entirely was reverted just before the v7.2 merge window, and will be brought back in an improved form for the v7.3 cycle)

[...]

Applied to arm64 (for-next/core), thanks!

[1/1] arm64: mm: Defer read-only remap of data/bss linear alias

      https://git.kernel.org/arm64/c/36fa5ffa6034

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev