mirror of
https://github.com/torvalds/linux.git
synced 2026-09-09 09:22:02 +02:00
LoongArch: KVM: Fix TOCTOU race on pv_features
In kvm_loongarch_cpucfg_set_attr() the check-then-set on kvm->arch.pv_features is lockless, so two vCPUs can race past the validation and set different values. Add a spinlock to protect it. Cc: stable@vger.kernel.org Reviewed-by: Bibo Mao <maobibo@loongson.cn> Signed-off-by: Tao Cui <cuitao@kylinos.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
This commit is contained in:
parent
501514d6eb
commit
9296375902
|
|
@ -125,6 +125,7 @@ struct kvm_arch {
|
|||
unsigned int pte_shifts[MAX_PGTABLE_LEVELS];
|
||||
unsigned int root_level;
|
||||
spinlock_t phyid_map_lock;
|
||||
spinlock_t pv_setting_lock;
|
||||
struct kvm_phyid_map *phyid_map;
|
||||
/* Enabled PV features */
|
||||
unsigned long pv_features;
|
||||
|
|
|
|||
|
|
@ -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_setting_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_setting_lock);
|
||||
return -EINVAL;
|
||||
}
|
||||
kvm->arch.pv_features = val | LOONGARCH_PV_FEAT_UPDATED;
|
||||
spin_unlock(&kvm->arch.pv_setting_lock);
|
||||
return 0;
|
||||
default:
|
||||
return -ENXIO;
|
||||
|
|
|
|||
|
|
@ -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_setting_lock);
|
||||
|
||||
kvm_init_vmcs(kvm);
|
||||
kvm_vm_init_features(kvm);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user