Vulnerability: arch/x86/kvm/x86.c in the Linux kernel before 4.4 does not reset the PIT counter values during state restoration, which allows guest OS users to cause a denial of service (divide-by-zero error and host OS crash) via a zero value, related to the kvm_vm_ioctl_set_pit and kvm_vm_ioctl_set_pit2 functions.
Vulnerability ID: CVE-2015-7513
CWE: CWE-369
Severity score: 6.9
Project: https://github.com/torvalds/linux
Published: 2016-02-08
File: arch/x86/kvm/x86.c
Commit message: KVM: x86: Reload pit counters for all channels when restoring state

Currently if userspace restores the pit counters with a count of 0
on channels 1 or 2 and the guest attempts to read the count on those
channels, then KVM will perform a mod of 0 and crash.  This will ensure
that 0 values are converted to 65536 as per the spec.

This is CVE-2015-7513.

Signed-off-by: Andy Honig <ahonig@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Commit author: Andrew Honig

Vulnerable version (before fix):
 static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)  static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) { 	mutex_lock(&kvm->arch.vpit->pit_state.lock); 	memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); 	kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0); 	mutex_unlock(&kvm->arch.vpit->pit_state.lock); 	return 0; } static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) { 	int start = 0; 	u32 prev_legacy, cur_legacy; 	mutex_lock(&kvm->arch.vpit->pit_state.lock); 	prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) 	memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels, 	       sizeof(kvm->arch.vpit->pit_state.channels)); 	kvm->arch.vpit->pit_state.flags = ps->flags; 	kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start); 	mutex_unlock(&kvm->arch.vpit->pit_state.lock); 	return 0; }

Fixed version (after fix):
 static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)  static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) { 	int i; 	mutex_lock(&kvm->arch.vpit->pit_state.lock); 	memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state)); 	for (i = 0; i < 3; i++) 		kvm_pit_load_count(kvm, i, ps->channels[i].count, 0); 	mutex_unlock(&kvm->arch.vpit->pit_state.lock); 	return 0; } static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) { 	int start = 0; 	int i; 	u32 prev_legacy, cur_legacy; 	mutex_lock(&kvm->arch.vpit->pit_state.lock); 	prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) 	memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels, 	       sizeof(kvm->arch.vpit->pit_state.channels)); 	kvm->arch.vpit->pit_state.flags = ps->flags; 	for (i = 0; i < 3; i++) 		kvm_pit_load_count(kvm, i, kvm->arch.vpit->pit_state.channels[i].count, start); 	mutex_unlock(&kvm->arch.vpit->pit_state.lock); 	return 0; }

Patch:
patch: @@ -3572,9 +3572,11 @@ static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
 
 static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
 {
+	int i;
 	mutex_lock(&kvm->arch.vpit->pit_state.lock);
 	memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
-	kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
+	for (i = 0; i < 3; i++)
+		kvm_pit_load_count(kvm, i, ps->channels[i].count, 0);
 	mutex_unlock(&kvm->arch.vpit->pit_state.lock);
 	return 0;
 }
@@ -3593,6 +3595,7 @@ static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
 static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
 {
 	int start = 0;
+	int i;
 	u32 prev_legacy, cur_legacy;
 	mutex_lock(&kvm->arch.vpit->pit_state.lock);
 	prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
@@ -3602,7 +3605,8 @@ static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
 	memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
 	       sizeof(kvm->arch.vpit->pit_state.channels));
 	kvm->arch.vpit->pit_state.flags = ps->flags;
-	kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
+	for (i = 0; i < 3; i++)
+		kvm_pit_load_count(kvm, i, kvm->arch.vpit->pit_state.channels[i].count, start);
 	mutex_unlock(&kvm->arch.vpit->pit_state.lock);
 	return 0;
 }
path: arch/x86/kvm/x86.c