KVM: Reject vCPU IDs above 2^32
v3 cover KVM: Reject vCPU IDs above 2^32
This small series evolved from a single vCPU ID limit check to multiple ones, including sanity checks among them and enhanced selftests.
Please apply!
Thanks,
Mathias
v2: https://lore.kernel.org/kvm/[email protected]/
changes v2->v3:
- test max_vcpu_ids on bsp_vcpu_id limit check if already set (Sean)
- add patch to check max_vcpu_ids to not exclude bsp_vcpu_id (Sean)
- reorder series to have selftests at the end
- minor commit log cosmetics
changes v1->v2:
- add comment and build bug to make truncation check more obvious (Sean)
- handle KVM_SET_BOOT_CPU_ID similar
Mathias Krause (4):
KVM: Reject overly excessive IDs in KVM_CREATE_VCPU
KVM: x86: Limit check IDs for KVM_SET_BOOT_CPU_ID
KVM: selftests: Test max vCPU IDs corner cases
KVM: selftests: Test vCPU boot IDs above 2^32
Sean Christopherson (1):
KVM: x86: Prevent excluding the BSP on setting max_vcpu_ids
arch/x86/kvm/x86.c | 7 +++++- .../kvm/x86_64/max_vcpuid_cap_test.c | 22 +++++++++++++++++-- .../selftests/kvm/x86_64/set_boot_cpu_id.c | 11 ++++++++++ virt/kvm/kvm_main.c | 10 ++++++++- 4 files changed, 46 insertions(+), 4 deletions(-)
Signature · 1 line
2.30.2
PATCH v3 1/5 KVM: Reject overly excessive IDs in KVM_CREATE_VCPU
If, on a 64 bit system, a vCPU ID is provided that has the upper 32 bits set to a non-zero value, it may get accepted if the truncated to 32 bits integer value is below KVM_MAX_VCPU_IDS and 'max_vcpus'. This feels very wrong and triggered the reporting logic of PaX's SIZE_OVERFLOW plugin.
Instead of silently truncating and accepting such values, pass the full value to kvm_vm_ioctl_create_vcpu() and make the existing limit checks return an error.
Even if this is a userland ABI breaking change, no sane userland could have ever relied on that behaviour.
--- virt/kvm/kvm_main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +9 -1
@@ -4200,12 +4200,20 @@ static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)/* * Creates some virtual cpus. Good luck creating more than one. */ -static int kvm_vm_ioctl_create_vcpu (struct kvm *kvm,u32 id) +static int kvm_vm_ioctl_create_vcpu (struct kvm *kvm,unsigned long id) {int r;struct kvm_vcpu *vcpu;struct page *page; +/* +* KVM tracks vCPU IDs as 'int', be kind to userspace and reject +* too-large values instead of silently truncating. +* +* Also ensure we're not breaking this assumption by accidentally +* pushing KVM_MAX_VCPU_IDS above INT_MAX. +*/ +BUILD_BUG_ON (KVM_MAX_VCPU_IDS >INT_MAX );if (id >=KVM_MAX_VCPU_IDS )return -EINVAL ;
Signature · 1 line
2.30.2
Sean Christopherson
maintainerRe: [PATCH v3 1/5] KVM: Reject overly excessive IDs in KVM_CREATE_VCPU
Quoted 11 lines · Mathias Krause
virt/kvm/kvm_main.c +1
-1@@ -4200,12 +4200,20 @@ static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)/* * Creates some virtual cpus. Good luck creating more than one. */ -static int kvm_vm_ioctl_create_vcpu (struct kvm *kvm,u32 id) +static int kvm_vm_ioctl_create_vcpu (struct kvm *kvm,unsigned long id) {
virt/kvm/kvm_main.c static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu) · line 4214
Quoted 10 lines
virt/kvm/kvm_main.c +6
-0int r;struct kvm_vcpu *vcpu;struct page *page; +/* +* KVM tracks vCPU IDs as 'int', be kind to userspace and reject +* too-large values instead of silently truncating. +* +* Also ensure we're not breaking this assumption by accidentally +* pushing KVM_MAX_VCPU_IDS above INT_MAX.
I tweaked this slightly because it's not just accidental changes we need to guard against, and to "hint" that vcpu_id really should be an "unsigned int".
/*
* KVM tracks vCPU IDs as 'int', be kind to userspace and reject
* too-large values instead of silently truncating.
*
* Ensure KVM_MAX_VCPU_IDS isn't pushed above INT_MAX without first
* changing the storage type (at the very least, IDs should be tracked
* as unsigned ints).
*/
Quoted 8 lines
virt/kvm/kvm_main.c +2
-0+*/ +BUILD_BUG_ON (KVM_MAX_VCPU_IDS >INT_MAX );if (id >=KVM_MAX_VCPU_IDS )return -EINVAL ;Signature · 1 line
2.30.2
PATCH v3 2/5 KVM: x86: Limit check IDs for KVM_SET_BOOT_CPU_ID
In reply to Mathias KrauseDo not accept IDs which are definitely invalid by limit checking the passed value against KVM_MAX_VCPU_IDS and 'max_vcpu_ids' if it was already set.
This ensures invalid values, especially on 64-bit systems, don't go unnoticed and lead to a valid id by chance when truncated by the final assignment.
---
v3:
- check against arch.max_vcpu_ids too, if already set (Sean)
- switch to else-if instead of goto (Sean)
arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
@@ -7222,6 +7222,9 @@ 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)) + r = -EINVAL ;else kvm->arch.bsp_vcpu_id = arg;mutex_unlock (&kvm->lock);
Signature · 1 line
2.30.2
PATCH v3 3/5 KVM: x86: Prevent excluding the BSP on setting max_vcpu_ids
In reply to Mathias KrauseFrom: Sean Christopherson <[email protected]>
If the BSP vCPU ID was already set, ensure it doesn't get excluded when limiting vCPU IDs via KVM_CAP_MAX_VCPU_ID.
--- arch/x86/kvm/x86.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +3 -1
@@ -6701,7 +6701,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;mutex_lock (&kvm->lock); -if (kvm->arch.max_vcpu_ids == cap->args[0 ]) { +if (kvm->arch.bsp_vcpu_id > cap->args[0 ]) { + ; + }else if (kvm->arch.max_vcpu_ids == cap->args[0 ]) { r =0 ; }else if (!kvm->arch.max_vcpu_ids) { kvm->arch.max_vcpu_ids = cap->args[0 ];
Signature · 1 line
2.30.2
PATCH v3 4/5 KVM: selftests: Test max vCPU IDs corner cases
In reply to Mathias KrauseThe KVM_CREATE_VCPU ioctl ABI had an implicit integer truncation bug, allowing 2^32 aliases for a vCPU ID by setting the upper 32 bits of a 64 bit ioctl() argument.
It also allowed excluding a once set boot CPU ID.
Verify this no longer works and gets rejected with an error.
---
v3:
- test BOOT_CPU_ID interaction too
.../kvm/x86_64/max_vcpuid_cap_test.c | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c +20 -2
@@ -26,19 +26,37 @@ int main(int argc, char *argv[])TEST_ASSERT (ret <0 ,"Setting KVM_CAP_MAX_VCPU_ID beyond KVM cap should fail" ); +/* Test BOOT_CPU_ID interaction (MAX_VCPU_ID cannot be lower) */ +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 ); +TEST_ASSERT (ret <0 , +"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 ); -/* Try to set KVM_CAP_MAX_VCPU_ID again */ ret =__vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +1 );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*/ +/* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap */ ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)MAX_VCPU_ID );TEST_ASSERT (ret <0 ,"Creating vCPU with ID > MAX_VCPU_ID should fail" ); +/* Create vCPU with id beyond UINT_MAX */ + ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)(1L <<32 )); +TEST_ASSERT (ret <0 ,"Creating vCPU with ID > UINT_MAX should fail" ); + +/* Create vCPU with id within bounds */ + ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)0 ); +TEST_ASSERT (ret >=0 ,"Creating vCPU with ID 0 should succeed" ); + +close (ret);kvm_vm_free (vm);return 0 ; }
Signature · 1 line
2.30.2
Sean Christopherson
maintainerRe: [PATCH v3 4/5] KVM: selftests: Test max vCPU IDs corner cases
Quoted 39 lines · Mathias Krause
The
KVM_CREATE_VCPUioctl ABI had an implicit integer truncation bug, allowing 2^32 aliases for a vCPU ID by setting the upper 32 bits of a 64 bitioctl()argument.It also allowed excluding a once set boot CPU ID.
Verify this no longer works and gets rejected with an error.
Signed-off-by Mathias Krause <[email protected]>---
v3:
- testBOOT_CPU_IDinteraction too.../kvm/x86_64/max_vcpuid_cap_test.c | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c +10
-1@@ -26,19 +26,37 @@ int main(int argc, char *argv[])TEST_ASSERT (ret <0 ,"Setting KVM_CAP_MAX_VCPU_ID beyond KVM cap should fail" ); +/* Test BOOT_CPU_ID interaction (MAX_VCPU_ID cannot be lower) */ +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 ); +TEST_ASSERT (ret <0 , +"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 ); -/* Try to set KVM_CAP_MAX_VCPU_ID again */
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c int main(int argc, char *argv[]) · line 51
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c +2
-1ret =__vm_enable_cap (vm,KVM_CAP_MAX_VCPU_ID ,MAX_VCPU_ID +1 );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*/ +/* Create vCPU with id beyond KVM_CAP_MAX_VCPU_ID cap */ ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)MAX_VCPU_ID );TEST_ASSERT (ret <0 ,"Creating vCPU with ID > MAX_VCPU_ID should fail" ); +/* Create vCPU with id beyond UINT_MAX */
I changed this comment to
/* Create vCPU with bits 63:32 != 0, but an otherwise valid id */
mostly because it's specifically testing the bad truncation of the upper bits, but also because I initially misinterpreted the intent and confused it with the INT_MAX BUILD_BUG_ON().
Quoted 14 lines
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c +8
-0+ ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)(1L <<32 )); +TEST_ASSERT (ret <0 ,"Creating vCPU with ID > UINT_MAX should fail" ); + +/* Create vCPU with id within bounds */ + ret =__vm_ioctl (vm,KVM_CREATE_VCPU , (void *)0 ); +TEST_ASSERT (ret >=0 ,"Creating vCPU with ID 0 should succeed" ); + +close (ret);kvm_vm_free (vm);return 0 ; }Signature · 1 line
2.30.2
PATCH v3 5/5 KVM: selftests: Test vCPU boot IDs above 2^32
In reply to Mathias KrauseThe KVM_SET_BOOT_CPU_ID ioctl missed to reject invalid vCPU IDs. Verify this no longer works and gets rejected with an appropriate error code.
--- tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +11 -0
@@ -33,6 +33,13 @@ static void guest_not_bsp_vcpu(void *arg)GUEST_DONE (); } +static void test_set_invalid_bsp (struct kvm_vm *vm) +{ +int r =__vm_ioctl (vm,KVM_SET_BOOT_CPU_ID , (void *)(1L <<32 )); + +TEST_ASSERT (r ==-1 && errno ==EINVAL ,"invalid KVM_SET_BOOT_CPU_ID set" ); +} +static void test_set_bsp_busy (struct kvm_vcpu *vcpu,const char *msg) { int r = __vm_ioctl(vcpu->vm, KVM_SET_BOOT_CPU_ID,
@@ -75,11 +82,15 @@ static void run_vcpu(struct kvm_vcpu *vcpu)static struct kvm_vm *create_vm (uint32_t nr_vcpus,uint32_t bsp_vcpu_id,struct kvm_vcpu *vcpus[]) { +static int invalid_bsp_tested;struct kvm_vm *vm;uint32_t i; vm =vm_create (nr_vcpus); +if (!invalid_bsp_tested++) +test_set_invalid_bsp (vm); +vm_ioctl (vm,KVM_SET_BOOT_CPU_ID , (void *)(unsigned long )bsp_vcpu_id);for (i =0 ; i < nr_vcpus; i++)
Signature · 1 line
2.30.2
Sean Christopherson
maintainerRe: [PATCH v3 5/5] KVM: selftests: Test vCPU boot IDs above 2^32
Quoted 13 lines · Mathias Krause
The
KVM_SET_BOOT_CPU_IDioctl missed to reject invalid vCPU IDs. Verify this no longer works and gets rejected with an appropriate error code.Signed-off-by Mathias Krause <[email protected]>--- tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c | 11 +++++++++++ 1 file changed, 11 insertions(+)tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +0
-0
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c static void guest_not_bsp_vcpu(void *arg) · line 39
Quoted 7 lines
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +4
-0@@ -33,6 +33,13 @@ static void guest_not_bsp_vcpu(void *arg)GUEST_DONE (); } +static void test_set_invalid_bsp (struct kvm_vm *vm) +{ +int r =__vm_ioctl (vm,KVM_SET_BOOT_CPU_ID , (void *)(1L <<32 )); +
I also added a test to verify KVM_CAP_MAX_VCPU_ID+1 also fails, because why not.
unsigned long max_vcpu_id = vm_check_cap(vm, KVM_CAP_MAX_VCPU_ID);
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 *)(1L << 32));
TEST_ASSERT(r == -1 && errno == EINVAL, "BSP with ID[63:32]!=0 should fail");Quoted 7 lines
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +3
-0+TEST_ASSERT (r ==-1 && errno ==EINVAL ,"invalid KVM_SET_BOOT_CPU_ID set" ); +} +static void test_set_bsp_busy (struct kvm_vcpu *vcpu,const char *msg) { int r = __vm_ioctl(vcpu->vm, KVM_SET_BOOT_CPU_ID,
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c static void run_vcpu(struct kvm_vcpu *vcpu) · line 91
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +2
-0@@ -75,11 +82,15 @@ static void run_vcpu(struct kvm_vcpu *vcpu)static struct kvm_vm *create_vm (uint32_t nr_vcpus,uint32_t bsp_vcpu_id,struct kvm_vcpu *vcpus[]) { +static int invalid_bsp_tested;struct kvm_vm *vm;uint32_t i; vm =vm_create (nr_vcpus); +if (!invalid_bsp_tested++)
I dropped this and just had every VM run the negative test. There's zero chance anyone will ever notice an extra failed ioctl() or three, whereas it took me a second to realize this is just a somewhat lazy way of writing a one-off negative test.
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +2
-0+test_set_invalid_bsp (vm); +vm_ioctl (vm,KVM_SET_BOOT_CPU_ID , (void *)(unsigned long )bsp_vcpu_id);for (i =0 ; i < nr_vcpus; i++)Signature · 1 line
2.30.2
Quoted 3 lines · Mathias Krause
This small series evolved from a single vCPU ID limit check to multiple ones, including sanity checks among them and enhanced selftests.
Applied to kvm-x86 generic, with a few tweaks (emails incoming). Thanks!
[1/5] KVM: Reject overly excessive IDs in KVM_CREATE_VCPU
https://github.com/kvm-x86/linux/commit/8b8e57e5096e [2/5] KVM: x86: Limit check IDs for KVM_SET_BOOT_CPU_ID https://github.com/kvm-x86/linux/commit/7c305d5118e6 [3/5] KVM: x86: Prevent excluding the BSP on setting max_vcpu_ids https://github.com/kvm-x86/linux/commit/d29bf2ca1404 [4/5] KVM: selftests: Test max vCPU IDs corner cases https://github.com/kvm-x86/linux/commit/4b451a57809c [5/5] KVM: selftests: Test vCPU boot IDs above 2^32 https://github.com/kvm-x86/linux/commit/438a496b9041
Quoted 8 lines · Sean Christopherson
Quoted 3 lines · Mathias Krause
This small series evolved from a single vCPU ID limit check to multiple ones, including sanity checks among them and enhanced selftests.
Applied to kvm-x86 generic, with a few tweaks (emails incoming). Thanks!
[1/5] KVM: Reject overly excessive IDs in
KVM_CREATE_VCPU
Quoted 9 lines
https://github.com/kvm-x86/linux/commit/8b8e57e5096e [2/5] KVM: x86: Limit check IDs for KVM_SET_BOOT_CPU_ID https://github.com/kvm-x86/linux/commit/7c305d5118e6 [3/5] KVM: x86: Prevent excluding the BSP on setting max_vcpu_ids https://github.com/kvm-x86/linux/commit/d29bf2ca1404 [4/5] KVM: selftests: Test max vCPU IDs corner cases https://github.com/kvm-x86/linux/commit/4b451a57809c [5/5] KVM: selftests: Test vCPU boot IDs above 2^32 https://github.com/kvm-x86/linux/commit/438a496b9041
Looking good, thanks Sean!
Mathias