Skip to contentSkip to conversation

arm64/efi: Clear PTE_WRITE when applying EFI_MEMORY_RO

PATCH Applied Ard Biesheuvel 2 participants 2 messages Oct 9 – Oct 12

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/efi: Clear PTE_WRITE when applying EFI_MEMORY_RO

From: Ard Biesheuvel <[email protected]>

set_permissions() sets PTE_RDONLY for regions that the Memory Attributes Table describes as read-only, but leaves PTE_WRITE set. PTE_WRITE is the DBM bit, so when hardware dirty state management is enabled (TCR_EL1.HD), such a descriptor is writable-clean rather than read-only, and a write by the firmware will be permitted by the hardware (which marks the entry dirty) instead of faulting.

Clear PTE_WRITE as well, as pte_wrprotect() does.

---
 arch/arm64/kernel/efi.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/arm64/kernel/efi.c +7 -1
index 0ec90fd1754e..6f34aef3d04e 100644
@@ -117,8 +117,14 @@ static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
 	const efi_memory_desc_t *md = spd->md;
 	pte_t pte = __ptep_get(ptep);
 
+	/*
+	 * Clear PTE_WRITE as well: it doubles as the DBM bit, and leaving it
+	 * set would permit the hardware to make the mapping writable again
+	 * when TCR_EL1.HD is set.
+	 */
 	if (md->attribute & EFI_MEMORY_RO)
-		pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
+		pte = set_pte_bit(clear_pte_bit(pte, __pgprot(PTE_WRITE)),
+				  __pgprot(PTE_RDONLY));
 	if (md->attribute & EFI_MEMORY_XP)
 		pte = set_pte_bit(pte, __pgprot(PTE_PXN));
 	else if (system_supports_bti_kernel() && spd->has_bti)
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
maintainer Applied
#1 lore raw
Quoted 8 lines · Ard Biesheuvel

set_permissions() sets PTE_RDONLY for regions that the Memory Attributes Table describes as read-only, but leaves PTE_WRITE set. PTE_WRITE is the DBM bit, so when hardware dirty state management is enabled (TCR_EL1.HD), such a descriptor is writable-clean rather than read-only, and a write by the firmware will be permitted by the hardware (which marks the entry dirty) instead of faulting.

[...]

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

[1/1] arm64/efi: Clear PTE_WRITE when applying EFI_MEMORY_RO

      https://git.kernel.org/arm64/c/215d7b662273