Skip to contentSkip to conversation

LoongArch: KVM: Fix TOCTOU race on pv_features

PATCH Applied Tao Cui 3 participants 4 messages Aug 14 – Sep 5

Subsystems (3)
lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH v3 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). Changes in v3: rename the lock to pv_setting_lock and place it next to phyid_map_lock (Huacai Chen).

v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

 arch/loongarch/include/asm/kvm_host.h | 1 +
 arch/loongarch/kvm/vcpu.c             | 6 +++++-
 arch/loongarch/kvm/vm.c               | 1 +
 3 files changed, 7 insertions(+), 1 deletion(-)
arch/loongarch/include/asm/kvm_host.h +1 -0
index 23cfbecebbd7..3538a199aac6 100644
@@ -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;
arch/loongarch/kvm/vcpu.c +5 -1
index 20c207d80e31..e47fa463a8be 100644
@@ -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;
arch/loongarch/kvm/vm.c +1 -0
index 1317c718f896..86ba7d54e81f 100644
@@ -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);
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi, Bibo,

What do you think about this?

Huacai

Quoted 71 lines · Tao Cui

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). Changes in v3: rename the lock to pv_setting_lock and place it next to phyid_map_lock (Huacai Chen).

v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

 arch/loongarch/include/asm/kvm_host.h | 1 +
 arch/loongarch/kvm/vcpu.c             | 6 +++++-
 arch/loongarch/kvm/vm.c               | 1 +
 3 files changed, 7 insertions(+), 1 deletion(-)
arch/loongarch/include/asm/kvm_host.h +1 -0
index 23cfbecebbd7..3538a199aac6 100644
@@ -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;
arch/loongarch/kvm/vcpu.c +5 -1
index 20c207d80e31..e47fa463a8be 100644
@@ -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;
arch/loongarch/kvm/vm.c +1 -1
index 1317c718f896..86ba7d54e81f 100644
@@ -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);
--

2.43.0

maintainer Reviewed-by
#2 lore raw
Quoted 3 lines · Huacai Chen

Hi, Bibo,

What do you think about this?

Thanks for your reminder, I forget this in flooding mailing list :( It looks good to me.

Quoted 76 lines

Huacai

Quoted 71 lines · Tao Cui

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). Changes in v3: rename the lock to pv_setting_lock and place it next to phyid_map_lock (Huacai Chen).

v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

  arch/loongarch/include/asm/kvm_host.h | 1 +
  arch/loongarch/kvm/vcpu.c             | 6 +++++-
  arch/loongarch/kvm/vm.c               | 1 +
  3 files changed, 7 insertions(+), 1 deletion(-)
arch/loongarch/include/asm/kvm_host.h +1 -0
index 23cfbecebbd7..3538a199aac6 100644
@@ -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;
arch/loongarch/kvm/vcpu.c +5 -1
index 20c207d80e31..e47fa463a8be 100644
@@ -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;
arch/loongarch/kvm/vm.c +1 -1
index 1317c718f896..86ba7d54e81f 100644
@@ -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);
--

2.43.0

maintainer Applied
#3 lore raw
In reply to Tao Cui

Applied, thanks.

Huacai

Quoted 71 lines · Tao Cui

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). Changes in v3: rename the lock to pv_setting_lock and place it next to phyid_map_lock (Huacai Chen).

v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

 arch/loongarch/include/asm/kvm_host.h | 1 +
 arch/loongarch/kvm/vcpu.c             | 6 +++++-
 arch/loongarch/kvm/vm.c               | 1 +
 3 files changed, 7 insertions(+), 1 deletion(-)
arch/loongarch/include/asm/kvm_host.h +1 -0
index 23cfbecebbd7..3538a199aac6 100644
@@ -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;
arch/loongarch/kvm/vcpu.c +5 -1
index 20c207d80e31..e47fa463a8be 100644
@@ -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;
arch/loongarch/kvm/vm.c +1 -1
index 1317c718f896..86ba7d54e81f 100644
@@ -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);
--

2.43.0