Skip to contentSkip to conversation

KVM: arm64: nv: Fix NULL dereference in kvm_inject_nested()

PATCH Superseded Dan Carpenter 3 participants 5 messages Nov 16 – Nov 18

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: nv: Fix NULL dereference in kvm_inject_nested()

There is a NULL 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)

KVM_SET_VCPU_EVENTS does not load the vcpu, but it 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. vcpu->arch.hw_mmu is NULL when the vcpu is not loaded, so kvm_vcpu_put_hw_mmu() dereferences a NULL pointer.

To fix this bug, only put and load the vcpu when it is loaded, similar to what kvm_reset_vcpu() does. When the vcpu is not loaded there is nothing to switch: the next kvm_arch_vcpu_load() loads L1's context.

Fixes 9aba641b9ec2 ("KVM: arm64: nv: Respect exception routing rules for SEAs")
Fixes 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking")

---
This was found through Pereq's formal analysis but the stack trace comes from real life testing.

x86 implements kvm_arch_vcpu_ioctl() differently and puts vcpu_load() at the start of the ioctl. But the way arm64 does it is deliberately and just something that people working with the code need to be aware of, I guess.

 arch/arm64/kvm/emulate-nested.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/kvm/emulate-nested.c +6 -3
index 2bec81668361..22ca735a34f6 100644
@@ -2862,7 +2862,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 			     enum exception_type type)
 {
 	u64 pstate, mode;
-	bool direct_inject;
+	bool direct_inject, loaded;
 
 	if (!vcpu_has_nv(vcpu)) {
 		kvm_err("Unexpected call to %s for the non-nesting configuration\n",
@@ -2891,6 +2891,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	}
 
 	preempt_disable();
+	loaded = (vcpu->cpu != -1);
 
 	vcpu_set_flag(vcpu, IN_NESTED_EXCEPTION);
 
@@ -2900,7 +2901,8 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	 */
 	__kvm_adjust_pc(vcpu);
 
-	kvm_arch_vcpu_put(vcpu);
+	if (loaded)
+		kvm_arch_vcpu_put(vcpu);
 
 	kvm_inject_el2_exception(vcpu, esr_el2, type);
 
@@ -2915,7 +2917,8 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	 */
 	__kvm_adjust_pc(vcpu);
 
-	kvm_arch_vcpu_load(vcpu, smp_processor_id());
+	if (loaded)
+		kvm_arch_vcpu_load(vcpu, smp_processor_id());
 	vcpu_clear_flag(vcpu, IN_NESTED_EXCEPTION);
 
 	preempt_enable();
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: arm64: nv: unguarded kvm_pmu_nested_transition() in kvm_inject_nested() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: arm64: nv: unguarded kvm_pmu_nested_transition() in kvm_inject_nested()
--

commit 7702078604222808ef62a9f6855a5c6c2add9d59
Author: Dan Carpenter <[email protected]>

KVM: arm64: nv: Fix NULL dereference in kvm_inject_nested()

This commit guards the kvm_arch_vcpu_put() and kvm_arch_vcpu_load() calls in kvm_inject_nested() with a check that the vCPU is currently loaded.

Quoted 32 lines
arch/arm64/kvm/emulate-nested.c +4 -2
index 2bec816683614..22ca735a34f69 100644
@@ -2862,7 +2862,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 			     enum exception_type type)
 {
 	u64 pstate, mode;
-	bool direct_inject;
+	bool direct_inject, loaded;
 
 	if (!vcpu_has_nv(vcpu)) {
 		kvm_err("Unexpected call to %s for the non-nesting configuration\n",
@@ -2891,6 +2891,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	}
 
 	preempt_disable();
+	loaded = (vcpu->cpu != -1);
 
 	vcpu_set_flag(vcpu, IN_NESTED_EXCEPTION);
 
@@ -2900,7 +2901,8 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	 */
 	__kvm_adjust_pc(vcpu);
 
-	kvm_arch_vcpu_put(vcpu);
+	if (loaded)
+		kvm_arch_vcpu_put(vcpu);
 
 	kvm_inject_el2_exception(vcpu, esr_el2, type);
 

arch/arm64/kvm/emulate-nested.c static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2, · line 2924

Quoted 9 lines
arch/arm64/kvm/emulate-nested.c +2 -1
@@ -2915,7 +2917,8 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	 */
 	__kvm_adjust_pc(vcpu);
 
-	kvm_arch_vcpu_load(vcpu, smp_processor_id());
+	if (loaded)
+		kvm_arch_vcpu_load(vcpu, smp_processor_id());
 	vcpu_clear_flag(vcpu, IN_NESTED_EXCEPTION);
 
 	preempt_enable();

[Severity: High]
Can kvm_pmu_nested_transition() at the end of kvm_inject_nested() still run when the vCPU is not loaded?

Right below preempt_enable(), kvm_inject_nested() unconditionally calls kvm_pmu_nested_transition():

arch/arm64/kvm/emulate-nested.c:kvm_inject_nested() {
    ...
	preempt_enable();

	if (kvm_vcpu_has_pmu(vcpu))
		kvm_pmu_nested_transition(vcpu);
}

When KVM_SET_VCPU_EVENTS reaches kvm_inject_nested() on an unloaded vCPU, kvm_pmu_nested_transition() calls kvm_pmu_create_perf_event() and kvm_vcpu_pmu_restore_guest(). If the vCPU was created with KVM_ARM_VCPU_PMU_V3_STRICT before KVM_ARM_VCPU_PMU_V3_SET_PMU is called, kvm_pmu_create_perf_event() -> kvm_map_pmu_event() dereferences a NULL vcpu->kvm->arch.arm_pmu. If called before KVM_ARM_VCPU_PMU_V3_INIT, it can also queue an uninitialized vcpu->arch.pmu.overflow_work.

Even on an initialized PMU, running kvm_pmu_nested_transition() while unloaded binds the new perf_event to current (which may be a VMM control thread rather than the vCPU thread) and programs the physical CPU's PMU EL0 filters via kvm_vcpu_pmu_restore_guest() without a matching kvm_vcpu_pmu_restore_host() before returning to userspace.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 18 lines · On Mon, Nov 16, 2026 at 02:06:38PM +0000, [email protected] wrote:

arch/arm64/kvm/emulate-nested.c static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2, · line 2924

Quoted 10 lines
arch/arm64/kvm/emulate-nested.c +2 -1
@@ -2915,7 +2917,8 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	 */
 	__kvm_adjust_pc(vcpu);
 
-	kvm_arch_vcpu_load(vcpu, smp_processor_id());
+	if (loaded)
+		kvm_arch_vcpu_load(vcpu, smp_processor_id());
 	vcpu_clear_flag(vcpu, IN_NESTED_EXCEPTION);
 
 	preempt_enable();

[Severity: High]
Can kvm_pmu_nested_transition() at the end of kvm_inject_nested() still run when the vCPU is not loaded?

Right below preempt_enable(), kvm_inject_nested() unconditionally calls kvm_pmu_nested_transition():

Quoted 7 lines
arch/arm64/kvm/emulate-nested.c:kvm_inject_nested() {
    ...
	preempt_enable();

	if (kvm_vcpu_has_pmu(vcpu))
		kvm_pmu_nested_transition(vcpu);
}

Sigh... Sashiko is right again. I will resend.

I need to figure out a better way to test patches so this doesn't happen again...

regards,
dan carpenter

maintainer
#3 lore raw
In reply to Dan Carpenter
Quoted 72 lines · Dan Carpenter

There is a NULL 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)

KVM_SET_VCPU_EVENTS does not load the vcpu, but it 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. vcpu->arch.hw_mmu is NULL when the vcpu is not loaded, so kvm_vcpu_put_hw_mmu() dereferences a NULL pointer.

To fix this bug, only put and load the vcpu when it is loaded, similar to what kvm_reset_vcpu() does. When the vcpu is not loaded there is nothing to switch: the next kvm_arch_vcpu_load() loads L1's context.

Fixes 9aba641b9ec2 ("KVM: arm64: nv: Respect exception routing rules for SEAs")
Fixes 77ee70a07357 ("KVM: arm64: nv: Honor SError exception routing / masking")

---
This was found through Pereq's formal analysis but the stack trace comes from real life testing.

x86 implements kvm_arch_vcpu_ioctl() differently and puts vcpu_load() at the start of the ioctl. But the way arm64 does it is deliberately and just something that people working with the code need to be aware of, I guess.

 arch/arm64/kvm/emulate-nested.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/kvm/emulate-nested.c +4 -2
index 2bec81668361..22ca735a34f6 100644
@@ -2862,7 +2862,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 			     enum exception_type type)
 {
 	u64 pstate, mode;
-	bool direct_inject;
+	bool direct_inject, loaded;
 
 	if (!vcpu_has_nv(vcpu)) {
 		kvm_err("Unexpected call to %s for the non-nesting configuration\n",
@@ -2891,6 +2891,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	}
 
 	preempt_disable();
+	loaded = (vcpu->cpu != -1);
 
 	vcpu_set_flag(vcpu, IN_NESTED_EXCEPTION);
 
@@ -2900,7 +2901,8 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	 */
 	__kvm_adjust_pc(vcpu);
 
-	kvm_arch_vcpu_put(vcpu);
+	if (loaded)
+		kvm_arch_vcpu_put(vcpu);
 
 	kvm_inject_el2_exception(vcpu, esr_el2, type);
 

arch/arm64/kvm/emulate-nested.c static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2, · line 2924

Quoted 9 lines
arch/arm64/kvm/emulate-nested.c +2 -1
@@ -2915,7 +2917,8 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2,
 	 */
 	__kvm_adjust_pc(vcpu);
 
-	kvm_arch_vcpu_load(vcpu, smp_processor_id());
+	if (loaded)
+		kvm_arch_vcpu_load(vcpu, smp_processor_id());
 	vcpu_clear_flag(vcpu, IN_NESTED_EXCEPTION);
 
 	preempt_enable();

I wonder if a marginally better approach is to add an unconditional vcpu_load() in the event injection ioctl:

arch/arm64/kvm/arm.c +6 -2
index 5959bbbfa026f..21cd872ea394b 100644
@@ -2007,7 +2007,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++)
@@ -2019,7 +2019,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;
 }
 
 /*

and be done with it, avoiding the fast-path bloating altogether. Unless you can spot another path where the vcpu is not loaded, but still requires the injection of an exception?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#4 lore raw
Quoted 27 lines · Marc Zyngier
arch/arm64/kvm/arm.c +6 -2
index 5959bbbfa026f..21cd872ea394b 100644
@@ -2007,7 +2007,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++)
@@ -2019,7 +2019,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;
 }
 
 /*
Quoted 3 lines

and be done with it, avoiding the fast-path bloating altogether. Unless you can spot another path where the vcpu is not loaded, but still requires the injection of an exception?

I feel like this approach brings arm64 slightly more in line with x86 so I like it. I'll send that.

I'm still redoing the TCR2 changes so I won't send those today.

regards,
dan carpenter