Skip to contentSkip to conversation

KVM: Fix wallclock version writing race

PATCH In review Avi Kivity 2 participants 3 messages May 4, 2010

Subsystems (3)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

PATCH 1/1 KVM: Fix wallclock version writing race

Wallclock writing uses an unprotected global variable to hold the version; this can cause one guest to interfere with another if both write their wallclock at the same time.

---
 arch/x86/kvm/x86.c |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +10 -2
index f6f8dad..c3152d7 100644
@@ -754,14 +754,22 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
 
 static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
 {
-	static int version;
+	int version;
+	int r;
 	struct pvclock_wall_clock wc;
 	struct timespec boot;
 
 	if (!wall_clock)
 		return;
 
-	version++;
+	r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
+	if (r)
+		return;
+
+	if (version & 1)
+		++version;  /* first time write, random junk */
+
+	++version;
 
 	kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
 
Signature · 1 line
1.7.0.4
author
#1 lore raw
Quoted 6 lines · Avi Kivity

Wallclock writing uses an unprotected global variable to hold the version; this can cause one guest to interfere with another if both write their wallclock at the same time.

This was pointed out by Naphtali.

Quoted 30 lines
arch/x86/kvm/x86.c +10 -2
index f6f8dad..c3152d7 100644
@@ -754,14 +754,22 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
 
  static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  {
-	static int version;
+	int version;
+	int r;
  	struct pvclock_wall_clock wc;
  	struct timespec boot;
 
  	if (!wall_clock)
  		return;
 
-	version++;
+	r = kvm_read_guest(kvm, wall_clock,&version, sizeof(version));
+	if (r)
+		return;
+
+	if (version&  1)
+		++version;  /* first time write, random junk */
+
+	++version;
 
  	kvm_write_guest(kvm, wall_clock,&version, sizeof(version));
 
Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
In reply to Avi Kivity
Quoted 4 lines · Avi Kivity

Wallclock writing uses an unprotected global variable to hold the version; this can cause one guest to interfere with another if both write their wallclock at the same time.

makes sense to me.

ACK.