LoongArch: KVM: Fix TOCTOU race on pv_features
Subsystems (3)
PATCH v2 1/1 LoongArch: KVM: Fix TOCTOU race on pv_features
From: Tao Cui <[email protected]>
The check-then-set on kvm->arch.pv_features in kvm_loongarch_cpucfg_set_attr() is lockless, so two vCPUs can race past the validation and set different values. Add a spinlock to serialize it.
---
Changes in v2: use a spinlock instead of a cmpxchg loop (Bibo Mao).
arch/loongarch/include/asm/kvm_host.h | 2 ++ arch/loongarch/kvm/vcpu.c | 6 +++++- arch/loongarch/kvm/vm.c | 1 + 3 files changed, 8 insertions(+), 1 deletion(-)
arch/loongarch/include/asm/kvm_host.h +2 -0
@@ -128,6 +128,8 @@ struct kvm_arch {struct kvm_phyid_map *phyid_map;/* Enabled PV features */ unsigned long pv_features; +/* Serializes pv_features updates */ +spinlock_t pv_features_lock;/* Supported KVM features */ unsigned long kvm_features;
arch/loongarch/kvm/vcpu.c +5 -1
@@ -1165,10 +1165,14 @@ static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu,return -EINVAL ;/* All vCPUs need set the same PV features */ +spin_lock (&kvm->arch.pv_features_lock);if ((kvm->arch.pv_features &LOONGARCH_PV_FEAT_UPDATED ) - && ((kvm->arch.pv_features & valid) != val)) + && ((kvm->arch.pv_features & valid) != val)) { +spin_unlock (&kvm->arch.pv_features_lock);return -EINVAL ; + } kvm->arch.pv_features = val |LOONGARCH_PV_FEAT_UPDATED ; +spin_unlock (&kvm->arch.pv_features_lock);return 0 ;default :return -ENXIO ;
arch/loongarch/kvm/vm.c +1 -0
@@ -76,6 +76,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)return -ENOMEM ; }spin_lock_init (&kvm->arch.phyid_map_lock); +spin_lock_init (&kvm->arch.pv_features_lock);kvm_init_vmcs (kvm);kvm_vm_init_features (kvm);
Signature · 1 line
2.43.0
Quoted 57 lines · Tao Cui
From: Tao Cui <[email protected]>
The check-then-set on
kvm->arch.pv_featuresinkvm_loongarch_cpucfg_set_attr()is lockless, so two vCPUs can race past the validation and set different values. Add a spinlock to serialize it.Signed-off-by Tao Cui <[email protected]>---
Changes in v2: use a spinlock instead of a cmpxchg loop (Bibo Mao).
arch/loongarch/include/asm/kvm_host.h | 2 ++ arch/loongarch/kvm/vcpu.c | 6 +++++- arch/loongarch/kvm/vm.c | 1 + 3 files changed, 8 insertions(+), 1 deletion(-)arch/loongarch/include/asm/kvm_host.h +2
-0@@ -128,6 +128,8 @@ struct kvm_arch {struct kvm_phyid_map *phyid_map;/* Enabled PV features */ unsigned long pv_features; +/* Serializes pv_features updates */ +spinlock_t pv_features_lock;/* Supported KVM features */ unsigned long kvm_features;arch/loongarch/kvm/vcpu.c +5
-1@@ -1165,10 +1165,14 @@ static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu,return -EINVAL ;/* All vCPUs need set the same PV features */ +spin_lock (&kvm->arch.pv_features_lock);if ((kvm->arch.pv_features &LOONGARCH_PV_FEAT_UPDATED ) - && ((kvm->arch.pv_features & valid) != val)) + && ((kvm->arch.pv_features & valid) != val)) { +spin_unlock (&kvm->arch.pv_features_lock);return -EINVAL ; + } kvm->arch.pv_features = val |LOONGARCH_PV_FEAT_UPDATED ; +spin_unlock (&kvm->arch.pv_features_lock);return 0 ;default :return -ENXIO ;arch/loongarch/kvm/vm.c +0
-0
arch/loongarch/kvm/vm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 82
Quoted 7 lines
arch/loongarch/kvm/vm.c +1
-0@@ -76,6 +76,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)return -ENOMEM ; }spin_lock_init (&kvm->arch.phyid_map_lock); +spin_lock_init (&kvm->arch.pv_features_lock);kvm_init_vmcs (kvm);kvm_vm_init_features (kvm);
Huacai Chen
maintainerHi, Tao,
Quoted 22 lines · Bibo Mao
Quoted 18 lines · Tao Cui
From: Tao Cui <[email protected]>
The check-then-set on
kvm->arch.pv_featuresinkvm_loongarch_cpucfg_set_attr()is lockless, so two vCPUs can race past the validation and set different values. Add a spinlock to serialize it.Signed-off-by Tao Cui <[email protected]>---
Changes in v2: use a spinlock instead of a cmpxchg loop (Bibo Mao).
arch/loongarch/include/asm/kvm_host.h | 2 ++ arch/loongarch/kvm/vcpu.c | 6 +++++- arch/loongarch/kvm/vm.c | 1 + 3 files changed, 8 insertions(+), 1 deletion(-)
Quoted 10 lines
arch/loongarch/include/asm/kvm_host.h struct kvm_arch { · line 132
Quoted 10 lines
arch/loongarch/include/asm/kvm_host.h +2
-0@@ -128,6 +128,8 @@ struct kvm_arch {struct kvm_phyid_map *phyid_map;/* Enabled PV features */ unsigned long pv_features; +/* Serializes pv_features updates */ +spinlock_t pv_features_lock;
I think it is better to rename it to pv_setting_lock, and put it near phyid_map_lock.
Huacai
Quoted 38 lines
Quoted 29 lines
arch/loongarch/include/asm/kvm_host.h +0
-0/* Supported KVM features */ unsigned long kvm_features;arch/loongarch/kvm/vcpu.c +5
-1@@ -1165,10 +1165,14 @@ static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu,return -EINVAL ;/* All vCPUs need set the same PV features */ +spin_lock (&kvm->arch.pv_features_lock);if ((kvm->arch.pv_features &LOONGARCH_PV_FEAT_UPDATED ) - && ((kvm->arch.pv_features & valid) != val)) + && ((kvm->arch.pv_features & valid) != val)) { +spin_unlock (&kvm->arch.pv_features_lock);return -EINVAL ; + } kvm->arch.pv_features = val |LOONGARCH_PV_FEAT_UPDATED ; +spin_unlock (&kvm->arch.pv_features_lock);return 0 ;default :return -ENXIO ;arch/loongarch/kvm/vm.c +0
-0arch/loongarch/kvm/vm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 82
Quoted 7 lines
arch/loongarch/kvm/vm.c +1
-0@@ -76,6 +76,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)return -ENOMEM ; }spin_lock_init (&kvm->arch.phyid_map_lock); +spin_lock_init (&kvm->arch.pv_features_lock);kvm_init_vmcs (kvm);kvm_vm_init_features (kvm);Reviewed-by Bibo Mao <[email protected]>
Hi Huacai,
Quoted 25 lines · Huacai Chen
Hi, Tao,
Quoted 22 lines · Bibo Mao
Quoted 18 lines · Tao Cui
From: Tao Cui <[email protected]>
The check-then-set on
kvm->arch.pv_featuresinkvm_loongarch_cpucfg_set_attr()is lockless, so two vCPUs can race past the validation and set different values. Add a spinlock to serialize it.Signed-off-by Tao Cui <[email protected]>---
Changes in v2: use a spinlock instead of a cmpxchg loop (Bibo Mao).
arch/loongarch/include/asm/kvm_host.h | 2 ++ arch/loongarch/kvm/vcpu.c | 6 +++++- arch/loongarch/kvm/vm.c | 1 + 3 files changed, 8 insertions(+), 1 deletion(-)
Quoted 13 lines
Quoted 10 lines
arch/loongarch/include/asm/kvm_host.h struct kvm_arch { · line 132
Quoted 10 lines
arch/loongarch/include/asm/kvm_host.h +2
-0@@ -128,6 +128,8 @@ struct kvm_arch {struct kvm_phyid_map *phyid_map;/* Enabled PV features */ unsigned long pv_features; +/* Serializes pv_features updates */ +spinlock_t pv_features_lock;I think it is better to rename it to
pv_setting_lock, and put it nearphyid_map_lock.
Got it, v3 on the way with the rename and the lock moved.
Thanks,
Tao
Quoted 40 lines
Huacai
Quoted 38 lines
Quoted 29 lines
arch/loongarch/include/asm/kvm_host.h +0
-0/* Supported KVM features */ unsigned long kvm_features;arch/loongarch/kvm/vcpu.c +5
-1@@ -1165,10 +1165,14 @@ static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu,return -EINVAL ;/* All vCPUs need set the same PV features */ +spin_lock (&kvm->arch.pv_features_lock);if ((kvm->arch.pv_features &LOONGARCH_PV_FEAT_UPDATED ) - && ((kvm->arch.pv_features & valid) != val)) + && ((kvm->arch.pv_features & valid) != val)) { +spin_unlock (&kvm->arch.pv_features_lock);return -EINVAL ; + } kvm->arch.pv_features = val |LOONGARCH_PV_FEAT_UPDATED ; +spin_unlock (&kvm->arch.pv_features_lock);return 0 ;default :return -ENXIO ;arch/loongarch/kvm/vm.c +0
-0arch/loongarch/kvm/vm.c int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) · line 82
Quoted 7 lines
arch/loongarch/kvm/vm.c +1
-0@@ -76,6 +76,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)return -ENOMEM ; }spin_lock_init (&kvm->arch.phyid_map_lock); +spin_lock_init (&kvm->arch.pv_features_lock);kvm_init_vmcs (kvm);kvm_vm_init_features (kvm);Reviewed-by Bibo Mao <[email protected]>