KVM: Fix wallclock version writing race
authorAvi Kivity <avi@redhat.com>
Tue, 4 May 2010 12:00:37 +0000 (15:00 +0300)
committerAvi Kivity <avi@redhat.com>
Wed, 19 May 2010 08:36:36 +0000 (11:36 +0300)
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.

Acked-by: Glauber Costa <glommer@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
arch/x86/kvm/x86.c

index dff08e527ec734056e28d49f3b4b4adbe305ff64..54f73b6a006ba2811acdb2f606070d2a40228b13 100644 (file)
@@ -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));