[PATCH 21/45] KVM: Take memory protections into account for __kvm_vcpu_map

From: Paolo Bonzini

Date: Mon Nov 02 2026 - 12:59:06 EST


Take into account memory attributes when establishing a long term map
through kvm_vcpu_map()/kvm_vcpu_map_readonly().

Unlike the gfn-to-pfn cache, changes to the attributes will not be
propagated, so that it will be possible to place a nested VMCS, VMCB,
or APIC access page on a non-readable/writable page. This will be
fixed separately by using a gfn-to-pfn cache for nested virtualization
data structures, in the meanwhile apply a stopgap fix.

Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Link: https://patch.msgid.link/20260918135030.171564-18-pbonzini@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
virt/kvm/kvm_main.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index de54f52f2158..c7b8d1bcf893 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3302,6 +3302,11 @@ int __kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map,
if (WARN_ON_ONCE(map->hva))
kvm_vcpu_unmap(vcpu, map);

+ if (!kvm_mem_attributes_may_read_gfn(vcpu->kvm, gfn))
+ return -EFAULT;
+ if (writable && !kvm_mem_attributes_may_write_gfn(vcpu->kvm, gfn))
+ return -EFAULT;
+
map->pinned_page = NULL;
map->page = NULL;
map->hva = NULL;
--
2.52.0