Skip to contentSkip to conversation

KVM: arm64: Only free fully unmapped tables in stage2_free_walker()

PATCH Applied Oliver Upton 3 participants 4 messages Sep 4, 2025 – Sep 5, 2025

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

PATCH 1/1 KVM: arm64: Only free fully unmapped tables in stage2_free_walker()

syzkaller caught a terminal use-after-free in the free walker resulting from a premature put_page() on a partially unmapped page table. Previously KVM performed a single walk covering the entire IPA space, but now that the range of the walk is up to KVM_PGTABLE_MIN_BLOCK_LEVEL worth of memory it is possibly to only partially free a table.

Fix it by only dropping the table reference if the page count of the table is 1 (i.e. no longer contains valid PTEs).

Fixes e9abe311f356 ("KVM: arm64: Reschedule as needed when destroying the stage-2 page-tables")
---
 arch/arm64/kvm/hyp/pgtable.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +27 -6
index c36f282a175d..50b8fb7cc59f 100644
@@ -1535,20 +1535,41 @@ size_t kvm_pgtable_stage2_pgd_size(u64 vtcr)
 	return kvm_pgd_pages(ia_bits, start_level) * PAGE_SIZE;
 }
 
-static int stage2_free_walker(const struct kvm_pgtable_visit_ctx *ctx,
-			      enum kvm_pgtable_walk_flags visit)
+static int stage2_free_leaf(const struct kvm_pgtable_visit_ctx *ctx)
 {
 	struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
 
-	if (!stage2_pte_is_counted(ctx->old))
+	mm_ops->put_page(ctx->ptep);
+	return 0;
+}
+
+static int stage2_free_table_post(const struct kvm_pgtable_visit_ctx *ctx)
+{
+	struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops;
+	kvm_pte_t *childp = kvm_pte_follow(ctx->old, mm_ops);
+
+	if (mm_ops->page_count(childp) != 1)
 		return 0;
 
 	mm_ops->put_page(ctx->ptep);
+	mm_ops->put_page(childp);
+	return 0;
+}
 
-	if (kvm_pte_table(ctx->old, ctx->level))
-		mm_ops->put_page(kvm_pte_follow(ctx->old, mm_ops));
+static int stage2_free_walker(const struct kvm_pgtable_visit_ctx *ctx,
+			      enum kvm_pgtable_walk_flags visit)
+{
+	if (!stage2_pte_is_counted(ctx->old))
+		return 0;
 
-	return 0;
+	switch (visit) {
+	case KVM_PGTABLE_WALK_LEAF:
+		return stage2_free_leaf(ctx);
+	case KVM_PGTABLE_WALK_TABLE_POST:
+		return stage2_free_table_post(ctx);
+	default:
+		return -EINVAL;
+	}
 }
 
 void kvm_pgtable_stage2_destroy_range(struct kvm_pgtable *pgt,
Signature · 1 line
2.39.5
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Oliver Upton

syzkaller caught a terminal use-after-free in the free walker resulting from a premature put_page() on a partially unmapped page table. Previously KVM performed a single walk covering the entire IPA space, but now that the range of the walk is up to KVM_PGTABLE_MIN_BLOCK_LEVEL worth of memory it is possibly to only partially free a table.

Quoted 5 lines

Fix it by only dropping the table reference if the page count of the table is 1 (i.e. no longer contains valid PTEs).

Fixes e9abe311f356 ("KVM: arm64: Reschedule as needed when destroying the stage-2 page-tables")
	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#2 lore raw
Quoted 10 lines · Marc Zyngier
Quoted 7 lines · Oliver Upton

syzkaller caught a terminal use-after-free in the free walker resulting from a premature put_page() on a partially unmapped page table. Previously KVM performed a single walk covering the entire IPA space, but now that the range of the walk is up to KVM_PGTABLE_MIN_BLOCK_LEVEL worth of memory it is possibly to only partially free a table.

Quoted 7 lines
Quoted 5 lines

Fix it by only dropping the table reference if the page count of the table is 1 (i.e. no longer contains valid PTEs).

Fixes e9abe311f356 ("KVM: arm64: Reschedule as needed when destroying the stage-2 page-tables")
author Applied
#3 lore raw
In reply to Oliver Upton
Quoted 10 lines · Oliver Upton

syzkaller caught a terminal use-after-free in the free walker resulting from a premature put_page() on a partially unmapped page table. Previously KVM performed a single walk covering the entire IPA space, but now that the range of the walk is up to KVM_PGTABLE_MIN_BLOCK_LEVEL worth of memory it is possibly to only partially free a table.

Fix it by only dropping the table reference if the page count of the table is 1 (i.e. no longer contains valid PTEs).

[...]

Applied to fixes, thanks!

[1/1] KVM: arm64: Only free fully unmapped tables in stage2_free_walker()

      https://git.kernel.org/kvmarm/kvmarm/c/3095864771d8

--
Best,
Oliver