Skip to contentSkip to conversation

KVM: arm64: Fix memory check in host_stage2_set_owner_locked()

PATCH Applied Mostafa Saleh 3 participants 4 messages May 1, 2025 – May 7, 2025

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

PATCH 1/1 KVM: arm64: Fix memory check in host_stage2_set_owner_locked()

I found this simple bug while preparing some patches for pKVM. AFAICT, it should be harmless (besides crashing the kernel if it was misbehaving)

Fixes e94a7dea2972 ("KVM: arm64: Move host page ownership tracking to the hyp vmemmap")
---
 arch/arm64/kvm/hyp/nvhe/mem_protect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1 -1
index 2a5284f749b4..e80f3ebd3e2a 100644
@@ -503,7 +503,7 @@ int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
 {
 	int ret;
 
-	if (!addr_is_memory(addr))
+	if (!range_is_memory(addr, addr + size))
 		return -EPERM;
 
 	ret = host_stage2_try(kvm_pgtable_stage2_set_owner, &host_mmu.pgt,
Signature · 1 line
2.49.0.906.g1f30a19c02-goog
maintainer
#1 lore raw
Quoted 16 lines · Mostafa Saleh

I found this simple bug while preparing some patches for pKVM. AFAICT, it should be harmless (besides crashing the kernel if it was misbehaving)

Fixes e94a7dea2972 ("KVM: arm64: Move host page ownership tracking to the hyp vmemmap")
---
 arch/arm64/kvm/hyp/nvhe/mem_protect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
index 2a5284f749b4..e80f3ebd3e2a 100644

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id) · line 509

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1 -1
@@ -503,7 +503,7 @@ int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
 {
 	int ret;
 
-	if (!addr_is_memory(addr))
+	if (!range_is_memory(addr, addr + size))
 		return -EPERM;
 
 	ret = host_stage2_try(kvm_pgtable_stage2_set_owner, &host_mmu.pgt,

I vaguely seem to remember that there was an assumption around addr/size representing a single page, and therefore addr_is_memory() was doing the right thing.

Has this assumption changed? Or is this only a figment of my imagination?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#2 lore raw
Quoted 25 lines · Marc Zyngier
Quoted 23 lines · Mostafa Saleh

I found this simple bug while preparing some patches for pKVM. AFAICT, it should be harmless (besides crashing the kernel if it was misbehaving)

Fixes e94a7dea2972 ("KVM: arm64: Move host page ownership tracking to the hyp vmemmap")
---
 arch/arm64/kvm/hyp/nvhe/mem_protect.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +1 -1
index 2a5284f749b4..e80f3ebd3e2a 100644
@@ -503,7 +503,7 @@ int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id)
 {
 	int ret;
 
-	if (!addr_is_memory(addr))
+	if (!range_is_memory(addr, addr + size))
 		return -EPERM;
 
Quoted 7 lines

arch/arm64/kvm/hyp/nvhe/mem_protect.c int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id) · line 509

Quoted 1 line
arch/arm64/kvm/hyp/nvhe/mem_protect.c +0 -0
 	ret = host_stage2_try(kvm_pgtable_stage2_set_owner, &host_mmu.pgt,

I vaguely seem to remember that there was an assumption around addr/size representing a single page, and therefore addr_is_memory() was doing the right thing.

Has this assumption changed? Or is this only a figment of my imagination?

From what I see, this might have size exceeding page size in the following cases:
1- When creating a VM with 4K kernel as the struct pkvm_hyp_vm size
is at least 0x1130 (without vCPUs)
2- When creating a VM and PGD size is bigger than one page (for cases
with concatenation as 52 bits and 4K would have 64K PGD)

Thanks,
Mostafa

Quoted 7 lines

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.