[PATCH v2 1/2] KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS

Dan Carpenter dan at pereq.eu
Wed Nov 18 05:05:44 PST 2026


Nested virtualization can trigger a NULL pointer dereference of
vcpu->arch.hw_mmu in kvm_inject_nested():

Unable to handle kernel NULL pointer dereference at virtual address 0000000000000084
  kvm_vcpu_put_hw_mmu (arch/arm64/kvm/nested.c:1046) (P)
  vhe_vcpu_put (arch/arm64/kvm/arm.c:827)
  kvm_arch_vcpu_put (arch/arm64/kvm/arm.c:860)
  kvm_inject_nested (arch/arm64/kvm/emulate-nested.c:2903)
  kvm_inject_nested_serror (arch/arm64/kvm/emulate-nested.c:2976)
  kvm_inject_serror_esr (arch/arm64/kvm/inject_fault.c:365)
  __kvm_arm_vcpu_set_events (arch/arm64/kvm/guest.c:827)
  kvm_arch_vcpu_ioctl (arch/arm64/kvm/arm.c:2017 arch/arm64/kvm/arm.c:2157)
  kvm_vcpu_ioctl (arch/arm64/kvm/../../../virt/kvm/kvm_main.c:4772)

The KVM_SET_VCPU_EVENTS ioctl does not load the vcpu, it just injects a
pending SError or external abort straight away.  If the vcpu is in L2
context and L1 has asked for the exception, kvm_inject_nested() puts and
loads a vcpu that is not loaded which leads to a NULL pointer dereference.

Load the vcpu in kvm_arm_vcpu_set_events() so that kvm_inject_nested()
always finds a loaded vcpu.

Fixes: 9aba641b9ec2 ("KVM: arm64: nv: Respect exception routing rules for SEAs")
Fixes: 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking")
Cc: stable at vger.kernel.org
Signed-off-by: Dan Carpenter <dan at pereq.eu>
---
In v1, I added checks for if the vcpu was loaded.  This version calls
vcpu_load() in the caller instead.  That's more like what x86 does.

Also Sashiko found an additional bug in v1 and I have added a fix for
that as patch 2/2.

 arch/arm64/kvm/arm.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 84a52e27ed85..a148e80d0aef 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -2002,7 +2002,7 @@ static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
 				   struct kvm_vcpu_events *events)
 {
-	int i;
+	int i, ret;
 
 	/* check whether the reserved field is zero */
 	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
@@ -2014,7 +2014,11 @@ static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
 		if (events->exception.pad[i])
 			return -EINVAL;
 
-	return __kvm_arm_vcpu_set_events(vcpu, events);
+	vcpu_load(vcpu);
+	ret = __kvm_arm_vcpu_set_events(vcpu, events);
+	vcpu_put(vcpu);
+
+	return ret;
 }
 
 /*
-- 
2.53.0




More information about the linux-arm-kernel mailing list