* [PATCH v2 0/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks
@ 2026-07-31 15:08 Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive Dmytro Maluka
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Dmytro Maluka @ 2026-07-31 15:08 UTC (permalink / raw)
To: Sean Christopherson
Cc: Paolo Bonzini, Dave Hansen, Mathias Krause, kvm, linux-doc,
linux-kernel, linux-kselftest, Vineeth Pillai, Chuanxiao Dong,
Aashish Sharma, Grzegorz Jaszczyk, 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.
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/20240614202859.3597745-1-minipli@grsecurity.net/
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(-)
--
2.55.0.508.g3f0d502094-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
2026-07-31 15:08 [PATCH v2 0/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks Dmytro Maluka
@ 2026-07-31 15:08 ` Dmytro Maluka
2026-07-31 15:24 ` sashiko-bot
2026-07-31 15:08 ` [PATCH v2 2/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks Dmytro Maluka
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Dmytro Maluka @ 2026-07-31 15:08 UTC (permalink / raw)
To: Sean Christopherson
Cc: Paolo Bonzini, Dave Hansen, Mathias Krause, kvm, linux-doc,
linux-kernel, linux-kselftest, Vineeth Pillai, Chuanxiao Dong,
Aashish Sharma, Grzegorz Jaszczyk, Dmytro Maluka
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")
Signed-off-by: Dmytro Maluka <dmaluka@chromium.org>
---
tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
index 7e2bfb3c3f3b..47ee14967873 100644
--- a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
+++ b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
@@ -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 */
--
2.55.0.508.g3f0d502094-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 2/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks
2026-07-31 15:08 [PATCH v2 0/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive Dmytro Maluka
@ 2026-07-31 15:08 ` Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 3/4] KVM: selftests: Improve bsp_vcpu_id and max_vcpu_ids out-of-bound tests Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 4/4] Documentation: KVM: Clarify that KVM_CAP_MAX_VCPU_ID is exclusive Dmytro Maluka
3 siblings, 0 replies; 7+ messages in thread
From: Dmytro Maluka @ 2026-07-31 15:08 UTC (permalink / raw)
To: Sean Christopherson
Cc: Paolo Bonzini, Dave Hansen, Mathias Krause, kvm, linux-doc,
linux-kernel, linux-kselftest, Vineeth Pillai, Chuanxiao Dong,
Aashish Sharma, Grzegorz Jaszczyk, 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")
Signed-off-by: Dmytro Maluka <dmaluka@chromium.org>
---
arch/x86/kvm/x86.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 47cb9eba113b..b576b46869d2 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -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;
--
2.55.0.508.g3f0d502094-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 3/4] KVM: selftests: Improve bsp_vcpu_id and max_vcpu_ids out-of-bound tests
2026-07-31 15:08 [PATCH v2 0/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 2/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks Dmytro Maluka
@ 2026-07-31 15:08 ` Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 4/4] Documentation: KVM: Clarify that KVM_CAP_MAX_VCPU_ID is exclusive Dmytro Maluka
3 siblings, 0 replies; 7+ messages in thread
From: Dmytro Maluka @ 2026-07-31 15:08 UTC (permalink / raw)
To: Sean Christopherson
Cc: Paolo Bonzini, Dave Hansen, Mathias Krause, kvm, linux-doc,
linux-kernel, linux-kselftest, Vineeth Pillai, Chuanxiao Dong,
Aashish Sharma, Grzegorz Jaszczyk, 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.
Signed-off-by: Dmytro Maluka <dmaluka@chromium.org>
---
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(-)
diff --git a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
index 47ee14967873..43f16c052de4 100644
--- a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
+++ b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
@@ -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");
}
/*
diff --git a/tools/testing/selftests/kvm/x86/set_boot_cpu_id.c b/tools/testing/selftests/kvm/x86/set_boot_cpu_id.c
index 8e3898646c69..1c4ebdf8e82c 100644
--- a/tools/testing/selftests/kvm/x86/set_boot_cpu_id.c
+++ b/tools/testing/selftests/kvm/x86/set_boot_cpu_id.c
@@ -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));
--
2.55.0.508.g3f0d502094-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 4/4] Documentation: KVM: Clarify that KVM_CAP_MAX_VCPU_ID is exclusive
2026-07-31 15:08 [PATCH v2 0/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks Dmytro Maluka
` (2 preceding siblings ...)
2026-07-31 15:08 ` [PATCH v2 3/4] KVM: selftests: Improve bsp_vcpu_id and max_vcpu_ids out-of-bound tests Dmytro Maluka
@ 2026-07-31 15:08 ` Dmytro Maluka
3 siblings, 0 replies; 7+ messages in thread
From: Dmytro Maluka @ 2026-07-31 15:08 UTC (permalink / raw)
To: Sean Christopherson
Cc: Paolo Bonzini, Dave Hansen, Mathias Krause, kvm, linux-doc,
linux-kernel, linux-kselftest, Vineeth Pillai, Chuanxiao Dong,
Aashish Sharma, Grzegorz Jaszczyk, 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.
Signed-off-by: Dmytro Maluka <dmaluka@chromium.org>
---
Documentation/virt/kvm/api.rst | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index e3003a241d5b..dab9a28c0fb5 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -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
------------------------------
--
2.55.0.508.g3f0d502094-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
2026-07-31 15:08 ` [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive Dmytro Maluka
@ 2026-07-31 15:24 ` sashiko-bot
2026-07-31 15:44 ` Dmytro Maluka
0 siblings, 1 reply; 7+ messages in thread
From: sashiko-bot @ 2026-07-31 15:24 UTC (permalink / raw)
To: Dmytro Maluka; +Cc: kvm
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 <dmaluka@chromium.org>
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.
> diff --git a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
> index 7e2bfb3c3f3ba..47ee14967873f 100644
> --- a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
> +++ b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
[ ... ]
> @@ -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?
>
> /* 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");
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260731150846.1728164-1-dmaluka@chromium.org?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive
2026-07-31 15:24 ` sashiko-bot
@ 2026-07-31 15:44 ` Dmytro Maluka
0 siblings, 0 replies; 7+ messages in thread
From: Dmytro Maluka @ 2026-07-31 15:44 UTC (permalink / raw)
To: sashiko-reviews; +Cc: kvm
On Fri, Jul 31, 2026 at 03:24:07PM +0000, sashiko-bot@kernel.org 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 <dmaluka@chromium.org>
>
> 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.
>
> > diff --git a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
> > index 7e2bfb3c3f3ba..47ee14967873f 100644
> > --- a/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
> > +++ b/tools/testing/selftests/kvm/x86/max_vcpuid_cap_test.c
> [ ... ]
> > @@ -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,
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.
> and the
> selftest be updated with a negative test to ensure it actually fails?
That is done in patch 3 in this series.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-07-31 15:44 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-31 15:08 [PATCH v2 0/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 1/4] KVM: selftests: Fix treating KVM_CAP_MAX_VCPU_ID as inclusive Dmytro Maluka
2026-07-31 15:24 ` sashiko-bot
2026-07-31 15:44 ` Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 2/4] KVM: x86: Fix off-by-one in max_vcpu_ids bounds checks Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 3/4] KVM: selftests: Improve bsp_vcpu_id and max_vcpu_ids out-of-bound tests Dmytro Maluka
2026-07-31 15:08 ` [PATCH v2 4/4] Documentation: KVM: Clarify that KVM_CAP_MAX_VCPU_ID is exclusive Dmytro Maluka
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox