Skip to contentSkip to conversation

KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks

PATCH Applied Dmytro Maluka 3 participants 8 messages Jul 31 – Nov 13

Subsystems (5)
Documentation x86 KVM Kselftest +1 more subsystems
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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.

Fixes 4b451a57809c ("KVM: selftests: Test max vCPU IDs corner cases")
---
 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
index 7e2bfb3c3f3b..47ee14967873 100644
@@ -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
#2 lore raw

Re: [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
index 7e2bfb3c3f3ba..47ee14967873f 100644

[ ... ]

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
#3 lore raw

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_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
index 7e2bfb3c3f3ba..47ee14967873f 100644

[ ... ]

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.

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_id to equal max_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.

#4 lore raw

PATCH v2 2/4 KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks

In reply to Dmytro Maluka

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 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.

Fixes 7c305d5118e6 ("KVM: x86: Limit check IDs for KVM_SET_BOOT_CPU_ID")
Fixes d29bf2ca1404 ("KVM: x86: Prevent excluding the BSP on setting max_vcpu_ids")
---
 arch/x86/kvm/x86.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +3 -3
index 47cb9eba113b..b576b46869d2 100644
@@ -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
#5 lore raw

PATCH v2 3/4 KVM: selftests: Improve bsp_vcpu_id and max_vcpu_ids out-of-bound tests

In reply to Dmytro Maluka

Now 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
index 47ee14967873..43f16c052de4 100644
@@ -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
index 8e3898646c69..1c4ebdf8e82c 100644
@@ -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
#6 lore raw

PATCH v2 4/4 Documentation: KVM: Clarify that KVM_CAP_MAX_VCPU_ID is exclusive

In reply to Dmytro Maluka

The 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
index e3003a241d5b..dab9a28c0fb5 100644
@@ -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
maintainer Applied
#7 lore raw
In reply to Dmytro Maluka
Quoted 9 lines · Dmytro Maluka

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.

[...]

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

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