KVM: nVMX: backport virtual-APIC host NULL-deref fix
Subsystems (2)
v2 cover KVM: nVMX: backport virtual-APIC host NULL-deref fix
This series backports the fix for a guest-triggerable host NULL pointer dereference in nested-VMX virtual-APIC handling. The bug is present in 6.1.y and fixed in 6.6.y and later.
vmx_guest_apic_has_interrupt() tests vmx->nested.virtual_apic_map.gfn to decide the virtual-APIC page is mapped, then reads through vmx->nested.virtual_apic_map.hva. kvm_vcpu_unmap() clears .hva but not .gfn, so after the page is unmapped an L1 guest using virtual-interrupt delivery passes the .gfn check with .hva == NULL and the host faults reading NULL + APIC_PROCPRI (CR2 = 0xa0). 96c66e87deee introduced the .gfn check. The function is still present at 6.1.176.
The upstream fix, 321ef62b0c5f, deletes the function. It carries a stable tag but did not apply to 6.1.y. The failed-apply notice [1] lists the SEV-SNP series as a dependency, but that series is not required, and the backport builds fine without it.
321ef62b0c5f edits the vmx_has_nested_events() body that 27c4fa42b11a adds, and 27c4fa42b11a calls pi_find_highest_vector() from d83c36d822be. The series is those three commits in that order. The rest of the June 2024 series is already in 6.1.y.
Sean asked for the series to be backported rather than kvm_vcpu_unmap() patched [2].
6.1.y predates the vmx main.c / x86_ops.h split, so patch 3 removes the hook from vmx_x86_ops in vmx.c. The resulting vmx_has_nested_events() matches 6.6.y. The injection path is unchanged; only the wake path that reached the dereference changes.
Reported by Taeyang Lee.
[1] https://lore.kernel.org/all/2024072925-straw-mashing-54f6@gregkh
[2] https://lore.kernel.org/all/CAH-2XvJo_JiyAnb21_LYxSz8xZ96oUVF25eTnDyALF+wnZw8ww@mail.gmail.com/
Changes since v1:
- v1 reached stable@ without this cover letter and with an incomplete Cc, due to a git send-email error on my end. No code changed.
Sean Christopherson (3):
KVM: nVMX: Add a helper to get highest pending from Posted Interrupt
vector
KVM: nVMX: Check for pending posted interrupts when looking for nested
events
KVM: nVMX: Fold requested virtual interrupt check into
has_nested_events()
arch/x86/include/asm/kvm-x86-ops.h | 1 - arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/vmx/nested.c | 45 +++++++++++++++++++++++++++--- arch/x86/kvm/vmx/posted_intr.h | 10 +++++++ arch/x86/kvm/vmx/vmx.c | 21 -------------- arch/x86/kvm/x86.c | 10 +------ 6 files changed, 52 insertions(+), 36 deletions(-)
--
2.34.1
PATCH v2 1/3 KVM: nVMX: Add a helper to get highest pending from Posted Interrupt vector
From: Sean Christopherson <[email protected]>
commit d83c36d822be44db4bad0c43bea99c8908f54117 upstream.
Add a helper to retrieve the highest pending vector given a Posted Interrupt descriptor. While the actual operation is straightforward, it's surprisingly easy to mess up, e.g. if one tries to reuse lapic.c's find_highest_vector(), which doesn't work with PID.PIR due to the APIC's IRR and ISR component registers being physically discontiguous (they're 4-byte registers aligned at 16-byte intervals).
To make PIR handling more consistent with respect to IRR and ISR handling, return -1 to indicate "no interrupt pending".
[ Nicholas Dudar: backport to 6.1.y. 6.1.y defines struct pi_desc in
posted_intr.h and predates the move to <asm/posted_intr.h>, so the helper and the <linux/find.h> include go in posted_intr.h. ]
--- arch/x86/kvm/vmx/nested.c | 5 +++-- arch/x86/kvm/vmx/posted_intr.h | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +3 -2
@@ -12,6 +12,7 @@#include "mmu.h" #include "nested.h" #include "pmu.h" +#include "posted_intr.h" #include "sgx.h" #include "trace.h" #include "vmx.h"
@@ -3818,8 +3819,8 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)if (!pi_test_and_clear_on (vmx->nested.pi_desc))return 0 ; - max_irr =find_last_bit ((unsigned long *)vmx->nested.pi_desc->pir,256 ); -if (max_irr !=256 ) { + max_irr =pi_find_highest_vector (vmx->nested.pi_desc); +if (max_irr >0 ) { vapic_page = vmx->nested.virtual_apic_map.hva;if (!vapic_page) goto mmio_needed;
arch/x86/kvm/vmx/posted_intr.h +10 -0
@@ -2,6 +2,8 @@#ifndef __KVM_X86_VMX_POSTED_INTR_H#define __KVM_X86_VMX_POSTED_INTR_H +#include <linux/find.h> +#define POSTED_INTR_ON 0#define POSTED_INTR_SN 1
@@ -103,4 +105,12 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, bool set);void vmx_pi_start_assignment (struct kvm *kvm); +static inline int pi_find_highest_vector (struct pi_desc *pi_desc) +{ +int vec; + + vec =find_last_bit ((unsigned long *)pi_desc->pir,256 ); +return vec <256 ? vec :-1 ; +} +#endif /* __KVM_X86_VMX_POSTED_INTR_H */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2 6.1.y 1/3] KVM: nVMX: Add a helper to get highest pending from Posted Interrupt vector
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The condition `if (max_irr > 0)` incorrectly ignores vector 0, preventing it from being synced…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The condition if (max_irr > 0) incorrectly ignores vector 0, preventing it from being synced to the guest's virtual APIC and leaving the PIR state corrupted. --
commit 5c23c313090006d2eae5e26f892baf83bf9030f1
Author: Sean Christopherson <[email protected]>
KVM: nVMX: Add a helper to get highest pending from Posted Interrupt vector
This commit adds a helper to retrieve the highest pending vector given a Posted Interrupt descriptor. The helper returns -1 to indicate no interrupt is pending, improving consistency with IRR and ISR handling.
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0
[ ... ]
arch/x86/kvm/vmx/nested.c static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) · line 3823
Quoted 8 lines
arch/x86/kvm/vmx/nested.c +2
-2@@ -3818,8 +3819,8 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)if (!pi_test_and_clear_on (vmx->nested.pi_desc))return 0 ; - max_irr =find_last_bit ((unsigned long *)vmx->nested.pi_desc->pir,256 ); -if (max_irr !=256 ) { + max_irr =pi_find_highest_vector (vmx->nested.pi_desc); +if (max_irr >0 ) {
[Severity: Medium]
Does this condition inadvertently skip vector 0?
The commit message states that pi_find_highest_vector() returns -1 to indicate no interrupt pending, so max_irr can be -1 or 0-255. By checking max_irr > 0, the code skips processing if vector 0 is the highest pending interrupt.
While vector 0 is an invalid APIC interrupt that hardware typically ignores, skipping it here means KVM does not call __kvm_apic_update_irr(). As a result, vector 0 could remain stuck in the PIR array instead of being cleared, which diverges from the lockless PIR/ON protocol.
Should this condition check for max_irr >= 0 or max_irr != -1 instead?
Quoted 3 lines
arch/x86/kvm/vmx/nested.c +0
-0vapic_page = vmx->nested.virtual_apic_map.hva;if (!vapic_page) goto mmio_needed;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sean Christopherson
reviewerRe: [PATCH v2 6.1.y 1/3] KVM: nVMX: Add a helper to get highest pending from Posted Interrupt vector
Quoted 9 lines · On Fri, Jun 19, 2026, [email protected] wrote:
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0[ ... ]
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0@@ -3818,8 +3819,8 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)if (!pi_test_and_clear_on (vmx->nested.pi_desc))return 0 ;
Quoted 7 lines
arch/x86/kvm/vmx/nested.c static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) · line 3823
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +2
-2- max_irr =find_last_bit ((unsigned long *)vmx->nested.pi_desc->pir,256 ); -if (max_irr !=256 ) { + max_irr =pi_find_highest_vector (vmx->nested.pi_desc); +if (max_irr >0 ) {[Severity: Medium]
Does this condition inadvertently skip vector 0?
No, it deliberately skips vector 0.
Quoted 10 lines
The commit message states that
pi_find_highest_vector()returns -1 to indicate no interrupt pending, somax_irrcan be -1 or 0-255. By checkingmax_irr> 0, the code skips processing if vector 0 is the highest pending interrupt.While vector 0 is an invalid APIC interrupt that hardware typically ignores, skipping it here means KVM does not call
__kvm_apic_update_irr(). As a result, vector 0 could remain stuck in the PIR array instead of being cleared, which diverges from the lockless PIR/ON protocol.Should this condition check for
max_irr>= 0 ormax_irr!= -1 instead?
Nah, because vmcs.GUEST_INTR_STATUS's RVI will never satisfy:
RVI[7:4] > VPPR[7:4]
and so the interrupt (that shouldn't/can't exist) will never be delivered, even if the KVM check were "fixed". If anything KVM could do "max_irr > 15", probably with a #define of some kind instead of an open coded literally.
Regardless, that's something to tweak in upstream, not in LTS backports.
Sean Christopherson
reviewerRe: [PATCH v2 6.1.y 1/3] KVM: nVMX: Add a helper to get highest pending from Posted Interrupt vector
In reply to Nicholas DudarQuoted 14 lines · Nicholas Dudar
From: Sean Christopherson <[email protected]>
commit
d83c36d822be44db4bad0c43bea99c8908f54117upstream.Add a helper to retrieve the highest pending vector given a Posted Interrupt descriptor. While the actual operation is straightforward, it's surprisingly easy to mess up, e.g. if one tries to reuse lapic.c's
find_highest_vector(), which doesn't work with PID.PIR due to the APIC's IRR and ISR component registers being physically discontiguous (they're 4-byte registers aligned at 16-byte intervals).To make PIR handling more consistent with respect to IRR and ISR handling, return -1 to indicate "no interrupt pending".
Quoted 6 lines
Signed-off-by Sean Christopherson <[email protected]>[ Nicholas Dudar: backport to 6.1.y. 6.1.y defines struct
pi_descin
posted_intr.hand predates the move to <asm/posted_intr.h>, so the helper and the <linux/find.h> include go inposted_intr.h. ]
This is misleading. The helper is in arch/x86/kvm/vmx/posted_intr.h, even in upstream. I don't know if I *intentionally* put the helper in KVM code, but for for whatever reason, I did.
Commit 699f67512f04 caused a conflict that needed to be resolved in the 6.1 backport, but that didn't have anything to do with needing to re-home the helper.
That only matters because I was going to ask if we'd be better off backporting asm/posted_intr.h so that the helper would live in it's "proper" location, but the answer on that front is "no", because it's already there.
FWIW, I got the same conflict resolution, it's just the blurb that's confusing.
Quoted 58 lines
Signed-off-by Nicholas Dudar <[email protected]>--- arch/x86/kvm/vmx/nested.c | 5 +++-- arch/x86/kvm/vmx/posted_intr.h | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/nested.c +3
-2@@ -12,6 +12,7 @@#include "mmu.h" #include "nested.h" #include "pmu.h" +#include "posted_intr.h" #include "sgx.h" #include "trace.h" #include "vmx.h" @@ -3818,8 +3819,8 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)if (!pi_test_and_clear_on (vmx->nested.pi_desc))return 0 ; - max_irr =find_last_bit ((unsigned long *)vmx->nested.pi_desc->pir,256 ); -if (max_irr !=256 ) { + max_irr =pi_find_highest_vector (vmx->nested.pi_desc); +if (max_irr >0 ) { vapic_page = vmx->nested.virtual_apic_map.hva;if (!vapic_page) goto mmio_needed;arch/x86/kvm/vmx/posted_intr.h +10
-0@@ -2,6 +2,8 @@#ifndef __KVM_X86_VMX_POSTED_INTR_H#define __KVM_X86_VMX_POSTED_INTR_H +#include <linux/find.h> +#define POSTED_INTR_ON 0#define POSTED_INTR_SN 1@@ -103,4 +105,12 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, bool set);void vmx_pi_start_assignment (struct kvm *kvm); +static inline int pi_find_highest_vector (struct pi_desc *pi_desc) +{ +int vec; + + vec =find_last_bit ((unsigned long *)pi_desc->pir,256 ); +return vec <256 ? vec :-1 ; +} +#endif /* __KVM_X86_VMX_POSTED_INTR_H */ Signature · 1 line
2.34.1
Re: [PATCH v2 6.1.y 1/3] KVM: nVMX: Add a helper to get highest pending from Posted Interrupt vector
Quoted 4 lines
This is misleading. The helper is in
arch/x86/kvm/vmx/posted_intr.h, even in upstream. I don't know if I *intentionally* put the helper in KVM code, but for for whatever reason, I did.
Quoted 8 lines
Commit
699f67512f04caused a conflict that needed to be resolved in the 6.1 backport, but that didn't have anything to do with needing to re-home the helper.That only matters because I was going to ask if we'd be better off backporting
asm/posted_intr.hso that the helper would live in it's "proper" location, but the answer on that front is "no", because it's already there.FWIW, I got the same conflict resolution, it's just the blurb that's confusing.
Thanks, Sean. Yes I see it now, the conflict was 699f67512f04 not being in 6.1.y, not the helper's location. Apologies for the misattribution.
PATCH v2 2/3 KVM: nVMX: Check for pending posted interrupts when looking for nested events
In reply to Nicholas DudarFrom: Sean Christopherson <[email protected]>
commit 27c4fa42b11af780d49ce704f7fa67b3c2544df4 upstream.
Check for pending (and notified!) posted interrupts when checking if L2 has a pending wake event, as fully posted/notified virtual interrupt is a valid wake event for HLT.
Note that KVM must check vmx->nested.pi_pending to avoid prematurely waking L2, e.g. even if KVM sees a non-zero PID.PIR and PID.0N=1, the virtual interrupt won't actually be recognized until a notification IRQ is received by the vCPU or the vCPU does (nested) VM-Enter.
[ Nicholas Dudar: backport to 6.1.y. Prerequisite for the next patch, which
folds its check into the vmx_has_nested_events() body this patch builds. Applies cleanly. The for_injection path still returns preemption_timer || mtf, as the previous 6.1.y body did. ]
--- arch/x86/kvm/vmx/nested.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +34 -2
@@ -3953,8 +3953,40 @@ static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu)static bool vmx_has_nested_events (struct kvm_vcpu *vcpu,bool for_injection) { -return nested_vmx_preemption_timer_pending (vcpu) || -to_vmx (vcpu)->nested.mtf_pending; +struct vcpu_vmx *vmx =to_vmx (vcpu); +void *vapic = vmx->nested.virtual_apic_map.hva; +int max_irr, vppr; + +if (nested_vmx_preemption_timer_pending (vcpu) || + vmx->nested.mtf_pending) +return true; + +/* +* Virtual Interrupt Delivery doesn't require manual injection. Either +* the interrupt is already in GUEST_RVI and will be recognized by CPU +* at VM-Entry, or there is a KVM_REQ_EVENT pending and KVM will move +* the interrupt from the PIR to RVI prior to entering the guest. +*/ +if (for_injection) +return false; + +if (!nested_cpu_has_vid (get_vmcs12 (vcpu)) || +__vmx_interrupt_blocked (vcpu)) +return false; + +if (!vapic) +return false; + + vppr = *((u32 *)(vapic +APIC_PROCPRI )); + +if (vmx->nested.pi_pending && vmx->nested.pi_desc && +pi_test_on (vmx->nested.pi_desc)) { + max_irr =pi_find_highest_vector (vmx->nested.pi_desc); +if (max_irr >0 && (max_irr &0xf0 ) > (vppr &0xf0 )) +return true; + } + +return false; }/*
Signature · 1 line
2.34.1
PATCH v2 3/3 KVM: nVMX: Fold requested virtual interrupt check into has_nested_events()
In reply to Nicholas DudarFrom: Sean Christopherson <[email protected]>
commit 321ef62b0c5f6f57bb8500a2ca5986052675abbf upstream.
Check for a Requested Virtual Interrupt, i.e. a virtual interrupt that is pending delivery, in vmx_has_nested_events() and drop the one-off kvm_x86_ops.guest_apic_has_interrupt() hook.
In addition to dropping a superfluous hook, this fixes a bug where KVM would incorrectly treat virtual interrupts _for L2_ as always enabled due to kvm_arch_interrupt_allowed(), by way of vmx_interrupt_blocked(), treating IRQs as enabled if L2 is active and vmcs12 is configured to exit on IRQs, i.e. KVM would treat a virtual interrupt for L2 as a valid wake event based on L1's IRQ blocking status.
[ Nicholas Dudar: backport to 6.1.y. 6.1.y predates the vmx main.c /
x86_ops.h split, so drop .guest_apic_has_interrupt from vmx_x86_ops in vmx.c rather than vt_x86_ops in main.c. The function is static in vmx.c, so upstream's x86_ops.h prototype removal does not apply. 6.1.y keeps the current hwapic_isr_update signature. ]
--- arch/x86/include/asm/kvm-x86-ops.h | 1 - arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/vmx/nested.c | 4 ++++ arch/x86/kvm/vmx/vmx.c | 21 --------------------- arch/x86/kvm/x86.c | 10 +--------- 5 files changed, 5 insertions(+), 32 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +0 -1
@@ -81,7 +81,6 @@ KVM_X86_OP(check_apicv_inhibit_reasons) KVM_X86_OP(refresh_apicv_exec_ctrl) KVM_X86_OP_OPTIONAL(hwapic_irr_update) KVM_X86_OP_OPTIONAL(hwapic_isr_update) -KVM_X86_OP_OPTIONAL_RET0(guest_apic_has_interrupt) KVM_X86_OP_OPTIONAL(load_eoi_exitmap) KVM_X86_OP_OPTIONAL(set_virtual_apic_mode) KVM_X86_OP_OPTIONAL(set_apic_access_page_addr)
arch/x86/include/asm/kvm_host.h +0 -1
@@ -1549,7 +1549,6 @@ struct kvm_x86_ops {void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu);void (*hwapic_irr_update)(struct kvm_vcpu *vcpu,int max_irr);void (*hwapic_isr_update)(struct kvm_vcpu *vcpu,int isr); -bool (*guest_apic_has_interrupt)(struct kvm_vcpu *vcpu);void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu,u64 *eoi_exit_bitmap);void (*set_virtual_apic_mode)(struct kvm_vcpu *vcpu);void (*set_apic_access_page_addr)(struct kvm_vcpu *vcpu);
arch/x86/kvm/vmx/nested.c +4 -0
@@ -3979,6 +3979,10 @@ static bool vmx_has_nested_events(struct kvm_vcpu *vcpu, bool for_injection) vppr = *((u32 *)(vapic +APIC_PROCPRI )); + max_irr =vmx_get_rvi (); +if ((max_irr &0xf0 ) > (vppr &0xf0 )) +return true; +if (vmx->nested.pi_pending && vmx->nested.pi_desc &&pi_test_on (vmx->nested.pi_desc)) { max_irr =pi_find_highest_vector (vmx->nested.pi_desc);
arch/x86/kvm/vmx/vmx.c +0 -21
@@ -4063,26 +4063,6 @@ void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu) } } -static bool vmx_guest_apic_has_interrupt (struct kvm_vcpu *vcpu) -{ -struct vcpu_vmx *vmx =to_vmx (vcpu); -void *vapic_page; -u32 vppr; -int rvi; - -if (WARN_ON_ONCE (!is_guest_mode (vcpu)) || - !nested_cpu_has_vid (get_vmcs12 (vcpu)) || -WARN_ON_ONCE (!vmx->nested.virtual_apic_map.gfn)) -return false; - - rvi =vmx_get_rvi (); - - vapic_page = vmx->nested.virtual_apic_map.hva; - vppr = *((u32 *)(vapic_page +APIC_PROCPRI )); - -return ((rvi &0xf0 ) > (vppr &0xf0 )); -} -static void vmx_msr_filter_changed (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);
@@ -8266,7 +8246,6 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons, .hwapic_irr_update = vmx_hwapic_irr_update, .hwapic_isr_update = vmx_hwapic_isr_update, - .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt, .sync_pir_to_irr = vmx_sync_pir_to_irr, .deliver_interrupt = vmx_deliver_interrupt, .dy_apicv_has_pending_interrupt = pi_has_pending_interrupt,
arch/x86/kvm/x86.c +1 -9
@@ -13046,12 +13046,6 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kvm,kvm_page_track_flush_slot (kvm, slot); } -static inline bool kvm_guest_apic_has_interrupt (struct kvm_vcpu *vcpu) -{ -return (is_guest_mode (vcpu) && -static_call (kvm_x86_guest_apic_has_interrupt)(vcpu)); -} -static inline bool kvm_vcpu_has_events (struct kvm_vcpu *vcpu) {if (!list_empty_careful (&vcpu->async_pf.done))
@@ -13077,9 +13071,7 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) static_call(kvm_x86_smi_allowed)(vcpu, false)))return true; -if (kvm_arch_interrupt_allowed (vcpu) && - (kvm_cpu_has_interrupt (vcpu) || -kvm_guest_apic_has_interrupt (vcpu))) +if (kvm_arch_interrupt_allowed (vcpu) &&kvm_cpu_has_interrupt (vcpu))return true;if (kvm_hv_has_stimer_pending (vcpu))
Signature · 1 line
2.34.1
Quoted 3 lines
This series backports the fix for a guest-triggerable host NULL pointer dereference in nested-VMX virtual-APIC handling. The bug is present in 6.1.y and fixed in 6.6.y and later.
Queued the 3-patch series for 6.1, thanks.
Signature · 2 lines
Thanks, Sasha
Sean Christopherson
reviewerQuoted 5 lines · Sasha Levin
Quoted 3 lines
This series backports the fix for a guest-triggerable host NULL pointer dereference in nested-VMX virtual-APIC handling. The bug is present in 6.1.y and fixed in 6.6.y and later.
Queued the 3-patch series for 6.1, thanks.
Backports LGTM, thanks Nicholas!