Skip to contentSkip to conversation

LoongArch: KVM: Fix TOCTOU race on pv_features

PATCH Superseded Tao Cui 3 participants 4 messages Aug 13 – Aug 14

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

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
index 23cfbecebbd7..cdbbd90e0584 100644
@@ -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
index 20c207d80e31..551ed39b2d1f 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_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
index 1317c718f896..b984cc54f305 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_features_lock);
 
 	kvm_init_vmcs(kvm);
 	kvm_vm_init_features(kvm);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 57 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).

  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
index 23cfbecebbd7..cdbbd90e0584 100644
@@ -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
index 20c207d80e31..551ed39b2d1f 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_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
index 1317c718f896..b984cc54f305 100644

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);
maintainer
#2 lore raw

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_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(-)
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
index 23cfbecebbd7..cdbbd90e0584 100644
@@ -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
index 20c207d80e31..551ed39b2d1f 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_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
index 1317c718f896..b984cc54f305 100644

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);
author
#3 lore raw

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_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(-)
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
index 23cfbecebbd7..cdbbd90e0584 100644
@@ -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.

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
index 20c207d80e31..551ed39b2d1f 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_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
index 1317c718f896..b984cc54f305 100644

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);