KVM: x86: Out-of-bounds access in kvm_recalculate_phys_map()
Sean Christopherson
maintainerv3 cover KVM: x86: Out-of-bounds access in kvm_recalculate_phys_map()
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/[email protected]
v1: https://lore.kernel.org/all/[email protected]
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
Signature · 1 line
2.41.0.rc2.161.g9c6817b8e7-goog
Sean Christopherson
maintainerPATCH v3 1/3 KVM: x86: Bail from kvm_recalculate_phys_map() if x2APIC ID is out-of-bounds
Bail from kvm_recalculate_phys_map() and disable the optimized map if the target vCPU's x2APIC ID is out-of-bounds, i.e. if the vCPU was added and/or enabled its local APIC after the map was allocated. This fixes an out-of-bounds access bug in the !x2apic_format path where KVM would write beyond the end of phys_map.
Check the x2APIC ID regardless of whether or not x2APIC is enabled, as KVM's hardcodes x2APIC ID to be the vCPU ID, i.e. it can't change, and the map allocation in kvm_recalculate_apic_map() doesn't check for x2APIC being enabled, i.e. the check won't get false postivies.
Note, this also affects the x2apic_format path, which previously just ignored the "x2apic_id > new->max_apic_id" case. That too is arguably a bug fix, as ignoring the vCPU meant that KVM would not send interrupts to the vCPU until the next map recalculation. In practice, that "bug" is likely benign as a newly present vCPU/APIC would immediately trigger a recalc. But, there's no functional downside to disabling the map, and a future patch will gracefully handle the -E2BIG case by retrying instead of simply disabling the optimized map.
Opportunistically add a sanity check on the xAPIC ID size, along with a comment explaining why the xAPIC ID is guaranteed to be "good".
--- arch/x86/kvm/lapic.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
arch/x86/kvm/lapic.c +18 -2
@@ -228,6 +228,23 @@ static int kvm_recalculate_phys_map(struct kvm_apic_map *new,u32 xapic_id =kvm_xapic_id (apic);u32 physical_id; +/* +* For simplicity, KVM always allocates enough space for all possible +* xAPIC IDs. Yell, but don't kill the VM, as KVM can continue on +* without the optimized map. +*/ +if (WARN_ON_ONCE (xapic_id > new->max_apic_id)) +return -EINVAL ; + +/* +* Bail if a vCPU was added and/or enabled its APIC between allocating +* the map and doing the actual calculations for the map. Note, KVM +* hardcodes the x2APIC ID to vcpu_id, i.e. there's no TOCTOU bug if +* the compiler decides to reload x2apic_id after this check. +*/ +if (x2apic_id > new->max_apic_id) +return -E2BIG ; +/* * Deliberately truncate the vCPU ID when detecting a mismatched APIC * ID to avoid false positives if the vCPU ID, i.e. x2APIC ID, is a
@@ -253,8 +270,7 @@ static int kvm_recalculate_phys_map(struct kvm_apic_map *new,*/ if (vcpu->kvm->arch.x2apic_format) {/* See also kvm_apic_match_physical_addr(). */ -if ((apic_x2apic_mode (apic) || x2apic_id >0xff ) && - x2apic_id <= new->max_apic_id) +if (apic_x2apic_mode (apic) || x2apic_id >0xff ) new->phys_map[x2apic_id] = apic;if (!apic_x2apic_mode (apic) && !new->phys_map[xapic_id])
Signature · 1 line
2.41.0.rc2.161.g9c6817b8e7-goog
Sean Christopherson
maintainerRe: [PATCH v3 1/3] KVM: x86: Bail from kvm_recalculate_phys_map() if x2APIC ID is out-of-bounds
Quoted 20 lines · Sean Christopherson
Bail from
kvm_recalculate_phys_map()and disable the optimized map if the target vCPU's x2APIC ID is out-of-bounds, i.e. if the vCPU was added and/or enabled its local APIC after the map was allocated. This fixes an out-of-bounds access bug in the !x2apic_format path where KVM would write beyond the end ofphys_map.Check the x2APIC ID regardless of whether or not x2APIC is enabled, as KVM's hardcodes x2APIC ID to be the vCPU ID, i.e. it can't change, and the map allocation in
kvm_recalculate_apic_map()doesn't check for x2APIC being enabled, i.e. the check won't get false postivies.Note, this also affects the
x2apic_formatpath, which previously just ignored the "x2apic_id>new->max_apic_id" case. That too is arguably a bug fix, as ignoring the vCPU meant that KVM would not send interrupts to the vCPU until the next map recalculation. In practice, that "bug" is likely benign as a newly present vCPU/APIC would immediately trigger a recalc. But, there's no functional downside to disabling the map, and a future patch will gracefully handle the-E2BIGcase by retrying instead of simply disabling the optimized map.
Quoted 4 lines
Opportunistically add a sanity check on the xAPIC ID size, along with a comment explaining why the xAPIC ID is guaranteed to be "good".
Reported-by Michal Luczaj <[email protected]>
Quoted 2 lines
Signed-off-by Sean Christopherson <[email protected]>---
Sean Christopherson
maintainerPATCH v3 2/3 KVM: x86: Retry APIC optimized map recalc if vCPU is added/enabled
In reply to Sean ChristophersonRetry the optimized APIC map recalculation if an APIC-enabled vCPU shows up between allocating the map and filling in the map data. Conditionally reschedule before retrying even though the number of vCPUs that can be created is bounded by KVM. Retrying a few thousand times isn't so slow as to be hugely problematic, but it's not blazing fast either.
Reset xapic_id_mistach on each retry as a vCPU could change its xAPIC ID between loops, but do NOT reset max_id. The map size also factors in whether or not a vCPU's local APIC is hardware-enabled, i.e. userspace and/or the guest can theoretically keep KVM retrying indefinitely. The only downside is that KVM will allocate more memory than is strictly necessary if the vCPU with the highest x2APIC ID disabled its APIC while the recalculation was in-progress.
Refresh kvm->arch.apic_map_dirty to opportunistically change it from DIRTY => UPDATE_IN_PROGRESS to avoid an unnecessary recalc from a different task, i.e. if another task is waiting to attempt an update (which is likely since a retry happens if and only if an update is required).
--- arch/x86/kvm/lapic.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)
arch/x86/kvm/lapic.c +25 -4
@@ -381,7 +381,8 @@ void kvm_recalculate_apic_map(struct kvm *kvm)struct kvm_vcpu *vcpu;unsigned long i;u32 max_id =255 ;/* enough space for any xAPIC ID */ -bool xapic_id_mismatch = false; +bool xapic_id_mismatch; +int r;/* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */ if (atomic_read_acquire (&kvm->arch.apic_map_dirty) ==CLEAN )
@@ -391,9 +392,14 @@ void kvm_recalculate_apic_map(struct kvm *kvm) "Dirty APIC map without an in-kernel local APIC");mutex_lock (&kvm->arch.apic_map_lock); + +retry:/* -* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map -* (if clean) or the APIC registers (if dirty). +* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map (if clean) +* or the APIC registers (if dirty). Note, on retry the map may have +* not yet been marked dirty by whatever task changed a vCPU's x2APIC +* ID, i.e. the map may still show up as in-progress. In that case +* this task still needs to retry and copmlete its calculation. */ if (atomic_cmpxchg_acquire (&kvm->arch.apic_map_dirty,DIRTY ,UPDATE_IN_PROGRESS ) ==CLEAN ) {
@@ -402,6 +408,15 @@ void kvm_recalculate_apic_map(struct kvm *kvm)return ; } +/* +* Reset the mismatch flag between attempts so that KVM does the right +* thing if a vCPU changes its xAPIC ID, but do NOT reset max_id, i.e. +* keep max_id strictly increasing. Disallowing max_id from shrinking +* ensures KVM won't get stuck in an infinite loop, e.g. if the vCPU +* with the highest x2APIC ID is toggling its APIC on and off. +*/ + xapic_id_mismatch = false; +kvm_for_each_vcpu (i, vcpu, kvm)if (kvm_apic_present (vcpu)) max_id =max (max_id,kvm_x2apic_id (vcpu->arch.apic));
@@ -420,9 +435,15 @@ void kvm_recalculate_apic_map(struct kvm *kvm)if (!kvm_apic_present (vcpu))continue ; -if (kvm_recalculate_phys_map (new, vcpu, &xapic_id_mismatch)) { + r =kvm_recalculate_phys_map (new, vcpu, &xapic_id_mismatch); +if (r) {kvfree (new); new =NULL ; +if (r == -E2BIG ) { +cond_resched (); + goto retry; + } + goto out; }
Signature · 1 line
2.41.0.rc2.161.g9c6817b8e7-goog
Re: [PATCH v3 2/3] KVM: x86: Retry APIC optimized map recalc if vCPU is added/enabled
arch/x86/kvm/lapic.c void kvm_recalculate_apic_map(struct kvm *kvm) · line 402
Quoted 5 lines · Sean Christopherson
arch/x86/kvm/lapic.c +5
-0+* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map (if clean) +* or the APIC registers (if dirty). Note, on retry the map may have +* not yet been marked dirty by whatever task changed a vCPU's x2APIC +* ID, i.e. the map may still show up as in-progress. In that case +* this task still needs to retry and copmlete its calculation.
s/copmlete/complete ?
Speaking of nits, if you're planning to do some more work around kvm_recalculate_phys_map(), there's that old comment typo I've failed to notice earlier:
"Apply KVM's hotplug hack if userspace has enable 32-bit APIC IDs."
enabled?
Sean Christopherson
maintainerPATCH v3 3/3 KVM: selftests: Add test for race in kvm_recalculate_apic_map()
In reply to Sean ChristophersonFrom: Michal Luczaj <[email protected]>
Keep switching between LAPIC_MODE_X2APIC and LAPIC_MODE_DISABLED during APIC map construction to hunt for TOCTOU bugs in KVM. KVM's optimized map recalc makes multiple passes over the list of vCPUs, and the calculations ignore vCPU's whose APIC is hardware-disabled, i.e. there's a window where toggling LAPIC_MODE_DISABLED is quite interesting.
--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/recalc_apic_map_test.c | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/recalc_apic_map_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test +TEST_GEN_PROGS_x86_64 += x86_64/recalc_apic_map_test TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test
tools/testing/selftests/kvm/x86_64/recalc_apic_map_test.c new file +74 -0
74 lines not on this page · raw message
Signature · 1 line
2.41.0.rc2.161.g9c6817b8e7-goog
Sean Christopherson
maintainerQuoted 9 lines · Sean Christopherson
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, callingkvm_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
Quoted 9 lines · Sean Christopherson
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, callingkvm_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