KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks
Subsystems (5)
v2 cover KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks
max_vcpu_ids and KVM_CAP_MAX_VCPU_ID are exclusive, i.e. the maximum allowed vcpu_id value is max_vcpu_ids minus one, not max_vcpu_ids.
Fix the sanity checks for KVM_SET_BOOT_CPU_ID and KVM_CAP_MAX_VCPU_ID and the selftests for them (added in series [1]) to fail when bsp_vcpu_id is incorrectly set to max_vcpu_ids, not to a value below max_vcpu_ids.
Also clarify that in the documentation of KVM_CAP_MAX_VCPU_ID, in an attempt to make this a bit less confusing.
v1 -> v2:
Split selftest fix into two patches, to avoid interim breakage of tests.
[1] https://lore.kernel.org/all/[email protected]/
Dmytro Maluka (4):
KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks
KVM: selftests: Improve bsp_vcpu_id and max_vcpu_ids out-of-bound
tests
Documentation: KVM: Clarify that KVM_CAP_MAX_VCPU_ID is exclusive
Documentation/virt/kvm/api.rst | 13 ++++++++----- arch/x86/kvm/x86.c | 6 +++--- .../selftests/kvm/x86/max_vcpuid_cap_test.c | 17 ++++++++++------- .../testing/selftests/kvm/x86/set_boot_cpu_id.c | 4 ++-- 4 files changed, 23 insertions(+), 17 deletions(-)
Signature · 1 line
2.55.0.508.g3f0d502094-goog
PATCH v2 1/4 KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
KVM_CAP_MAX_VCPU_ID is (confusingly) exclusive, i.e. the maximum allowed vcpu_id value is KVM_CAP_MAX_VCPU_ID minus one. So in particular, setting bsp_vcpu_id to the value _equal_ to KVM_CAP_MAX_VCPU_ID is invalid, it must be strictly below KVM_CAP_MAX_VCPU_ID. Whereas the corresponding test in x86/max_vcpuid_cap_test mistakenly assumes that it is valid and expects the ioctl to return success.
Fix this, by changing the "semantics" of the selftest's internal MAX_VCPU_ID constant: let it represent the actual maximum vcpu_id, i.e. the KVM_CAP_MAX_VCPU_ID value minus one.
--- tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c +7 -4
@@ -36,16 +36,19 @@ int main(int argc, char *argv[]) "Setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID should fail"); } -/* Set KVM_CAP_MAX_VCPU_ID */ -vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID ); +/* +* Set KVM_CAP_MAX_VCPU_ID. Note: KVM_CAP_MAX_VCPU_ID is a misnomer, +* it actually represents maximum vcpu_id plus one. +*/ +vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +1 );/* Try to set KVM_CAP_MAX_VCPU_ID again */ - ret =__vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +1 ); + ret =__vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +2 );TEST_ASSERT (ret <0 ,"Setting KVM_CAP_MAX_VCPU_ID multiple times should fail" );/* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap */ - ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)MAX_VCPU_ID ); + ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)(MAX_VCPU_ID +1 ));TEST_ASSERT (ret <0 ,"Creating vCPU with ID > MAX_VCPU_ID should fail" );/* Create vCPU with bits 63:32 != 0, but an otherwise valid id */
Signature · 1 line
2.55.0.508.g3f0d502094-goog
[email protected]
AI reviewRe: [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The patch modifies a KVM selftest to avoid an invalid configuration but sweeps an underlying…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch modifies a KVM selftest to avoid an invalid configuration but sweeps an underlying kernel bug under the rug by failing to fix the kernel and masking the issue in the test suite. --
--- Patch [1]: [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive --- commit 5e2afeaca4e83d31005f9aa5caf94dbbc9f20b8c
Author: Dmytro Maluka <[email protected]>
KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
This patch modifies the max_vcpuid_cap_test selftest to account for KVM_CAP_MAX_VCPU_ID being exclusive (maximum allowed vcpu_id plus one). It changes the test to use MAX_VCPU_ID + 1 when enabling the capability, avoiding the boundary condition.
Quoted 4 lines
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c +0
-0
[ ... ]
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c int main(int argc, char *argv[]) · line 43
Quoted 11 lines
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c +5
-2@@ -36,16 +36,19 @@ int main(int argc, char *argv[]) "Setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID should fail"); } -/* Set KVM_CAP_MAX_VCPU_ID */ -vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID ); +/* +* Set KVM_CAP_MAX_VCPU_ID. Note: KVM_CAP_MAX_VCPU_ID is a misnomer, +* it actually represents maximum vcpu_id plus one. +*/ +vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +1 );
[Severity: Medium]
Does this patch mask an underlying kernel bug rather than fixing it?
The commit message notes that setting bsp_vcpu_id equal to KVM_CAP_MAX_VCPU_ID is invalid. However, by changing the test to avoid this exact boundary condition, it seems we are leaving the kernel's off-by-one bug unfixed.
Looking at the kernel side in arch/x86/kvm/x86.c:kvm_vm_ioctl_enable_cap():
if (kvm->arch.bsp_vcpu_id > cap->args[0]) {This bounds check uses ">" instead of ">=", which appears to incorrectly allow bsp_vcpu_id to equal max_vcpu_ids.
Should the kernel be fixed to reject this invalid configuration, and the selftest be updated with a negative test to ensure it actually fails?
Quoted 11 lines
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c +2
-2/* Try to set KVM_CAP_MAX_VCPU_ID again */ - ret =__vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +1 ); + ret =__vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +2 );TEST_ASSERT (ret <0 ,"Setting KVM_CAP_MAX_VCPU_ID multiple times should fail" );/* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap */ - ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)MAX_VCPU_ID ); + ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)(MAX_VCPU_ID +1 ));TEST_ASSERT (ret <0 ,"Creating vCPU with ID > MAX_VCPU_ID should fail" );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
Quoted 39 lines · On Fri, Jul 31, 2026 at 03:24:07PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch modifies a KVM selftest to avoid an invalid configuration but sweeps an underlying kernel bug under the rug by failing to fix the kernel and masking the issue in the test suite. ----- Patch [1]: [PATCH v2 1/4] KVM: selftests: Fix treating
KVM_CAP_MAX_VCPU_IDas inclusive --- commit5e2afeaca4e83d31005f9aa5caf94dbbc9f20b8c
Author: Dmytro Maluka <[email protected]>KVM: selftests: Fix treating
KVM_CAP_MAX_VCPU_IDas inclusiveThis patch modifies the
max_vcpuid_cap_testselftest to account forKVM_CAP_MAX_VCPU_IDbeing exclusive (maximum allowedvcpu_idplus one). It changes the test to useMAX_VCPU_ID+ 1 when enabling the capability, avoiding the boundary condition.Quoted 4 lines
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c +0
-0[ ... ]
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c int main(int argc, char *argv[]) · line 43
Quoted 11 lines
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c +5
-2@@ -36,16 +36,19 @@ int main(int argc, char *argv[]) "Setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID should fail"); } -/* Set KVM_CAP_MAX_VCPU_ID */ -vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID ); +/* +* Set KVM_CAP_MAX_VCPU_ID. Note: KVM_CAP_MAX_VCPU_ID is a misnomer, +* it actually represents maximum vcpu_id plus one. +*/ +vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +1 );[Severity: Medium]
Does this patch mask an underlying kernel bug rather than fixing it?The commit message notes that setting
bsp_vcpu_idequal toKVM_CAP_MAX_VCPU_IDis invalid. However, by changing the test to avoid this exact boundary condition, it seems we are leaving the kernel's off-by-one bug unfixed.
Quoted 8 lines
Looking at the kernel side in arch/x86/kvm/x86.c:kvm_vm_ioctl_enable_cap():
if (kvm->arch.bsp_vcpu_id > cap->args[0]) {This bounds check uses ">" instead of ">=", which appears to incorrectly allow
bsp_vcpu_idto equalmax_vcpu_ids.Should the kernel be fixed to reject this invalid configuration,
Yes, it is fixed in patch 2 in this series.
The reason for this selftest patch is not to "mask" the KVM bug but to prevent the test from incorrectly failing once the KVM bug is fixed.
Quoted 2 lines
and the
selftest be updated with a negative test to ensure it actually fails?
That is done in patch 3 in this series.
PATCH v2 2/4 KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks
In reply to Dmytro Malukamax_vcpu_ids and KVM_CAP_MAX_VCPU_ID are exclusive, i.e. the maximum allowed vcpu_id value is max_vcpu_ids minus one, not max_vcpu_ids.
Fix sanity checks for KVM_SET_BOOT_CPU_ID and KVM_CAP_MAX_VCPU_ID to correctly fail when bsp_vcpu_id is set to max_vcpu_ids, not to a value below max_vcpu_ids.
--- arch/x86/kvm/x86.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +3 -3
@@ -6908,7 +6908,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;mutex_lock (&kvm->lock); -if (kvm->arch.bsp_vcpu_id > cap->args[0 ]) { +if (kvm->arch.bsp_vcpu_id >= cap->args[0 ]) { ; }else if (kvm->arch.max_vcpu_ids == cap->args[0 ]) { r =0 ;
@@ -7478,8 +7478,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)mutex_lock (&kvm->lock);if (kvm->created_vcpus) r = -EBUSY ; -else if (arg >KVM_MAX_VCPU_IDS || - (kvm->arch.max_vcpu_ids && arg > kvm->arch.max_vcpu_ids)) +else if (arg >=KVM_MAX_VCPU_IDS || + (kvm->arch.max_vcpu_ids && arg >= kvm->arch.max_vcpu_ids)) r = -EINVAL ;else kvm->arch.bsp_vcpu_id = arg;
Signature · 1 line
2.55.0.508.g3f0d502094-goog
PATCH v2 3/4 KVM: selftests: Improve bsp_vcpu_id and max_vcpu_ids out-of-bound tests
In reply to Dmytro MalukaNow that KVM correctly fails with -EINVAL when attempting to set bsp_vcpu_id to the value of max_vcpu_ids (not just to a greater value) and vice versa, improve corresponding tests to cover this corner case as well.
--- tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c | 6 +++--- tools/testing/selftests/kvm/x86/set_boot_cpu_id.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c +3 -3
@@ -30,10 +30,10 @@ int main(int argc, char *argv[])if (kvm_has_cap (KVM_CAP_SET_BOOT_CPU_ID )) {vm_ioctl (vm,KVM_SET_BOOT_CPU_ID , (void *)MAX_VCPU_ID ); -/* Try setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID */ - ret =__vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID -1 ); +/* Try setting KVM_CAP_MAX_VCPU_ID below or equal to BOOT_CPU_ID */ + ret =__vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID );TEST_ASSERT (ret <0 , -"Setting KVM_CAP_MAX_VCPU_ID below BOOT_CPU_ID should fail" ); +"Setting KVM_CAP_MAX_VCPU_ID <= BOOT_CPU_ID should fail" ); }/*
tools/testing/selftests/kvm/x86/set_boot_cpu_id.c +2 -2
@@ -39,8 +39,8 @@ static void test_set_invalid_bsp(struct kvm_vm *vm)int r;if (max_vcpu_id) { - r =__vm_ioctl (vm,KVM_SET_BOOT_CPU_ID , (void *)(max_vcpu_id +1 )); -TEST_ASSERT (r ==-1 && errno ==EINVAL ,"BSP with ID > MAX should fail" ); + r =__vm_ioctl (vm,KVM_SET_BOOT_CPU_ID , (void *)max_vcpu_id); +TEST_ASSERT (r ==-1 && errno ==EINVAL ,"BSP with ID >= MAX should fail" ); } r =__vm_ioctl (vm,KVM_SET_BOOT_CPU_ID , (void *)(1L <<32 ));
Signature · 1 line
2.55.0.508.g3f0d502094-goog
PATCH v2 4/4 Documentation: KVM: Clarify that KVM_CAP_MAX_VCPU_ID is exclusive
In reply to Dmytro MalukaThe KVM_CAP_MAX_VCPU_ID capability value is exclusive, i.e. valid APIC ID values are only values below this value, not including it. Its documentation doesn't make that clear, and even suggests otherwise. Fix it and explicitly state that it is exclusive.
--- Documentation/virt/kvm/api.rst | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
Documentation/virt/kvm/api.rst +8 -5
@@ -8593,21 +8593,24 @@ KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT By default, KVM for nested SVM guests :Architectures: x86 :Target: VM -:Parameters: args[0] - maximum APIC ID value set for current VM +:Parameters: args[0] - maximum APIC ID value (plus one) set for current VM :Returns: 0 on success, -EINVAL if args[0] is beyond KVM_MAX_VCPU_IDS supported in KVM or if it has been set. -This capability allows userspace to specify maximum possible APIC ID +This capability allows userspace to specify a limit on APIC ID values assigned for current VM session prior to the creation of vCPUs, saving memory for data structures indexed by the APIC ID. Userspace is able to calculate the limit to APIC ID values from designated CPU topology. -The value can be changed only until KVM_ENABLE_CAP is set to a nonzero +The limit is not inclusive, i.e. the maximum possible APIC ID value is +args[0] - 1. + +The capability value can only be changed until it is set to a nonzero value or until a vCPU is created. Upon creation of the first vCPU, if the value was set to zero or KVM_ENABLE_CAP was not invoked, KVM -uses the return value of KVM_CHECK_EXTENSION(KVM_CAP_MAX_VCPU_ID) as -the maximum APIC ID. +uses the return value of KVM_CHECK_EXTENSION(KVM_CAP_MAX_VCPU_ID) - 1 +as the maximum APIC ID. 7.33 KVM_CAP_X86_NOTIFY_VMEXIT ------------------------------
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Quoted 9 lines · Dmytro Maluka
max_vcpu_idsandKVM_CAP_MAX_VCPU_IDare exclusive, i.e. the maximum allowedvcpu_idvalue ismax_vcpu_idsminus one, notmax_vcpu_ids.Fix the sanity checks for
KVM_SET_BOOT_CPU_IDandKVM_CAP_MAX_VCPU_IDand the selftests for them (added in series [1]) to fail whenbsp_vcpu_idis incorrectly set tomax_vcpu_ids, not to a value belowmax_vcpu_ids.[...]
Applied to kvm-x86 misc, thanks!
[1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
https://github.com/kvm-x86/linux/commit/9b04ebe5c085 [2/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks https://github.com/kvm-x86/linux/commit/c36d436e7335 [3/4] KVM: selftests: Improve bsp_vcpu_id and max_vcpu_ids out-of-bound tests https://github.com/kvm-x86/linux/commit/201aaeb1df8f [4/4] Documentation: KVM: Clarify that KVM_CAP_MAX_VCPU_ID is exclusive https://github.com/kvm-x86/linux/commit/ea6d489ae6c2