[v3,0/3] KVM: x86: Out-of-bounds access in kvm_recalculate_phys_map()

Message ID 20230602233250.1014316-1-seanjc@google.com
Headers
Series KVM: x86: Out-of-bounds access in kvm_recalculate_phys_map() |

Message

Sean Christopherson June 2, 2023, 11:32 p.m. UTC
  In Michal's words...

kvm_recalculate_apic_map() creates the APIC map iterating over the list of
vCPUs twice. First to find the max APIC ID and allocate a max-sized buffer,
then again, calling kvm_recalculate_phys_map() for each vCPU. This opens a
race window: value of max APIC ID can increase _after_ the buffer was
allocated.

v3:
 - s/race/test for the new test file
 - Use kvm_vm_free() instead of kvm_vm_release() in the test
 - Fix a few typos in the test

v2: https://lore.kernel.org/all/20230526235048.2842761-1-seanjc@google.com

v1: https://lore.kernel.org/all/20230525183347.2562472-1-mhal@rbox.co

Michal Luczaj (1):
  KVM: selftests: Add test for race in kvm_recalculate_apic_map()

Sean Christopherson (2):
  KVM: x86: Bail from kvm_recalculate_phys_map() if x2APIC ID is
    out-of-bounds
  KVM: x86: Retry APIC optimized map recalc if vCPU is added/enabled

 arch/x86/kvm/lapic.c                          | 49 ++++++++++--
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../kvm/x86_64/recalc_apic_map_test.c         | 74 +++++++++++++++++++
 3 files changed, 118 insertions(+), 6 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86_64/recalc_apic_map_test.c


base-commit: 39428f6ea9eace95011681628717062ff7f5eb5f
  

Comments

Sean Christopherson June 3, 2023, 12:52 a.m. UTC | #1
On Fri, 02 Jun 2023 16:32:47 -0700, Sean Christopherson wrote:
> In Michal's words...
> 
> kvm_recalculate_apic_map() creates the APIC map iterating over the list of
> vCPUs twice. First to find the max APIC ID and allocate a max-sized buffer,
> then again, calling kvm_recalculate_phys_map() for each vCPU. This opens a
> race window: value of max APIC ID can increase _after_ the buffer was
> allocated.
> 
> [...]

Applied 1 and 3 to kvm-x86 fixes (for 6.4), figuring out how to deal with the
dependencies get #2 into 6.5 is a future problem.

[1/3] KVM: x86: Bail from kvm_recalculate_phys_map() if x2APIC ID is out-of-bounds
      https://github.com/kvm-x86/linux/commit/4364b287982b
[2/3] ...
[3/3] KVM: selftests: Add test for race in kvm_recalculate_apic_map()
      https://github.com/kvm-x86/linux/commit/47d2804bc99c

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes
  
Sean Christopherson Aug. 3, 2023, 12:04 a.m. UTC | #2
On Fri, 02 Jun 2023 16:32:47 -0700, Sean Christopherson wrote:
> In Michal's words...
> 
> kvm_recalculate_apic_map() creates the APIC map iterating over the list of
> vCPUs twice. First to find the max APIC ID and allocate a max-sized buffer,
> then again, calling kvm_recalculate_phys_map() for each vCPU. This opens a
> race window: value of max APIC ID can increase _after_ the buffer was
> allocated.
> 
> [...]

Applied patch 2 with fixup for the typo to kvm-x86 misc.  Note, patches 1 and 3
went into 6.5.

[2/3] KVM: x86: Retry APIC optimized map recalc if vCPU is added/enabled
      https://github.com/kvm-x86/linux/commit/41e90a69a49b

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes