Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] KVM: arm64: nv: Fix a couple NULL dereferences in kvm_inject_nested()
@ 2026-11-18 13:05 Dan Carpenter
  2026-11-18 13:05 ` [PATCH v2 1/2] KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS Dan Carpenter
  2026-11-18 13:05 ` [PATCH v2 2/2] KVM: arm64: nv: Fix NULL dereference in kvm_pmu_nested_transition() Dan Carpenter
  0 siblings, 2 replies; 3+ messages in thread
From: Dan Carpenter @ 2026-11-18 13:05 UTC (permalink / raw)
  To: Congkai Tan
  Cc: Catalin Marinas, Fuad Tabba, Haris Okanovic, Joey Gouly, kvmarm,
	linux-arm-kernel, Marc Zyngier, Mark Rutland, Oliver Upton,
	Stanislav Spassov, Steffen Eiden, Suzuki K Poulose, Will Deacon,
	Zenghui Yu, Karthik Bhargavan, Clément Blaudeau

This is part of our formal verification work at Pereq.  It's a couple
of NULL dereference bugs that affect nested virt.

The first version added checks for if the vcpu was loaded but the
new version loads it unconditionally instead.  Also add a fix for
the related bug that Sashiko found.

Dan Carpenter (2):
  KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS
  KVM: arm64: nv: Fix NULL dereference in kvm_pmu_nested_transition()

 arch/arm64/kvm/arm.c      | 8 ++++++--
 arch/arm64/kvm/pmu-emul.c | 3 +++
 2 files changed, 9 insertions(+), 2 deletions(-)

-- 
2.53.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS
  2026-11-18 13:05 [PATCH v2 0/2] KVM: arm64: nv: Fix a couple NULL dereferences in kvm_inject_nested() Dan Carpenter
@ 2026-11-18 13:05 ` Dan Carpenter
  2026-11-18 13:05 ` [PATCH v2 2/2] KVM: arm64: nv: Fix NULL dereference in kvm_pmu_nested_transition() Dan Carpenter
  1 sibling, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2026-11-18 13:05 UTC (permalink / raw)
  To: Oliver Upton
  Cc: Marc Zyngier, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, linux-arm-kernel, kvmarm, Karthik Bhargavan,
	Clément Blaudeau

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@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@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



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] KVM: arm64: nv: Fix NULL dereference in kvm_pmu_nested_transition()
  2026-11-18 13:05 [PATCH v2 0/2] KVM: arm64: nv: Fix a couple NULL dereferences in kvm_inject_nested() Dan Carpenter
  2026-11-18 13:05 ` [PATCH v2 1/2] KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS Dan Carpenter
@ 2026-11-18 13:05 ` Dan Carpenter
  1 sibling, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2026-11-18 13:05 UTC (permalink / raw)
  To: Congkai Tan
  Cc: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Haris Okanovic, Stanislav Spassov, linux-arm-kernel,
	kvmarm, Karthik Bhargavan, Clément Blaudeau

With the KVM_ARM_VCPU_PMU_V3_STRICT feature, kvm->arch.arm_pmu stays
NULL until userspace selects a PMU with the KVM_ARM_VCPU_PMU_V3_SET_PMU
attribute.  The KVM_SET_VCPU_EVENTS ioctl can inject an SError or
external abort before that which leads to a NULL pointer dereference.

Unable to handle kernel NULL pointer dereference at virtual address 00000000000001e8
  kvm_pmu_create_perf_event (arch/arm64/kvm/pmu-emul.c:710) (P)
  kvm_pmu_nested_transition (arch/arm64/kvm/pmu-emul.c:1357)
  kvm_inject_nested (arch/arm64/kvm/emulate-nested.c:2927)
  kvm_inject_nested_serror (arch/arm64/kvm/emulate-nested.c:2979)
  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:2161)
  kvm_vcpu_ioctl (arch/arm64/kvm/../../../virt/kvm/kvm_main.c:4772)

Return early if the PMU hasn't been initialized to fix this.  After all,
we can't transition a PMU that doesn't exist.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-217134@kernel.org/
Fixes: ad220e275c39 ("KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: This is a new patch.

 arch/arm64/kvm/pmu-emul.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c
index e2b28dbc0451..388a6244e7b2 100644
--- a/arch/arm64/kvm/pmu-emul.c
+++ b/arch/arm64/kvm/pmu-emul.c
@@ -1342,6 +1342,9 @@ void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu)
 	unsigned long mask;
 	int i;
 
+	if (!vcpu->arch.pmu.created)
+		return;
+
 	mask = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
 	for_each_set_bit(i, &mask, 32) {
 		struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-18 13:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 13:05 [PATCH v2 0/2] KVM: arm64: nv: Fix a couple NULL dereferences in kvm_inject_nested() Dan Carpenter
2026-11-18 13:05 ` [PATCH v2 1/2] KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS Dan Carpenter
2026-11-18 13:05 ` [PATCH v2 2/2] KVM: arm64: nv: Fix NULL dereference in kvm_pmu_nested_transition() Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox