Skip to content

Commit 09cce60

Browse files
willdeaconMarc Zyngier
authored andcommitted
KVM: arm64: Prevent unconditional donation of unmapped regions from the host
Since host stage-2 mappings are created lazily, we cannot rely solely on the pte in order to recover the target physical address when checking a host-initiated memory transition as this permits donation of unmapped regions corresponding to MMIO or "no-map" memory. Instead of inspecting the pte, move the addr_is_allowed_memory() check into the host callback function where it is passed the physical address directly from the walker. Cc: Quentin Perret <qperret@google.com> Fixes: e82edcc ("KVM: arm64: Implement do_share() helper for sharing memory") Signed-off-by: Will Deacon <will@kernel.org> Signed-off-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20230518095844.1178-1-will@kernel.org
1 parent 6254873 commit 09cce60

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

arch/arm64/kvm/hyp/nvhe/mem_protect.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -575,18 +575,15 @@ struct pkvm_mem_donation {
575575

576576
struct check_walk_data {
577577
enum pkvm_page_state desired;
578-
enum pkvm_page_state (*get_page_state)(kvm_pte_t pte);
578+
enum pkvm_page_state (*get_page_state)(kvm_pte_t pte, u64 addr);
579579
};
580580

581581
static int __check_page_state_visitor(const struct kvm_pgtable_visit_ctx *ctx,
582582
enum kvm_pgtable_walk_flags visit)
583583
{
584584
struct check_walk_data *d = ctx->arg;
585585

586-
if (kvm_pte_valid(ctx->old) && !addr_is_allowed_memory(kvm_pte_to_phys(ctx->old)))
587-
return -EINVAL;
588-
589-
return d->get_page_state(ctx->old) == d->desired ? 0 : -EPERM;
586+
return d->get_page_state(ctx->old, ctx->addr) == d->desired ? 0 : -EPERM;
590587
}
591588

592589
static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size,
@@ -601,8 +598,11 @@ static int check_page_state_range(struct kvm_pgtable *pgt, u64 addr, u64 size,
601598
return kvm_pgtable_walk(pgt, addr, size, &walker);
602599
}
603600

604-
static enum pkvm_page_state host_get_page_state(kvm_pte_t pte)
601+
static enum pkvm_page_state host_get_page_state(kvm_pte_t pte, u64 addr)
605602
{
603+
if (!addr_is_allowed_memory(addr))
604+
return PKVM_NOPAGE;
605+
606606
if (!kvm_pte_valid(pte) && pte)
607607
return PKVM_NOPAGE;
608608

@@ -709,7 +709,7 @@ static int host_complete_donation(u64 addr, const struct pkvm_mem_transition *tx
709709
return host_stage2_set_owner_locked(addr, size, host_id);
710710
}
711711

712-
static enum pkvm_page_state hyp_get_page_state(kvm_pte_t pte)
712+
static enum pkvm_page_state hyp_get_page_state(kvm_pte_t pte, u64 addr)
713713
{
714714
if (!kvm_pte_valid(pte))
715715
return PKVM_NOPAGE;

0 commit comments

Comments
 (0)