|
|
8d49642 |
From 0b79459b482e85cb7426aa7da683a9f2c97aeae1 Mon Sep 17 00:00:00 2001
|
|
|
8d49642 |
From: Andy Honig <ahonig@google.com>
|
|
|
8d49642 |
Date: Wed, 20 Feb 2013 14:48:10 -0800
|
|
|
8d49642 |
Subject: [PATCH 3/3] KVM: x86: Convert MSR_KVM_SYSTEM_TIME to use
|
|
|
8d49642 |
gfn_to_hva_cache functions (CVE-2013-1797)
|
|
|
8d49642 |
|
|
|
8d49642 |
There is a potential use after free issue with the handling of
|
|
|
8d49642 |
MSR_KVM_SYSTEM_TIME. If the guest specifies a GPA in a movable or removable
|
|
|
8d49642 |
memory such as frame buffers then KVM might continue to write to that
|
|
|
8d49642 |
address even after it's removed via KVM_SET_USER_MEMORY_REGION. KVM pins
|
|
|
8d49642 |
the page in memory so it's unlikely to cause an issue, but if the user
|
|
|
8d49642 |
space component re-purposes the memory previously used for the guest, then
|
|
|
8d49642 |
the guest will be able to corrupt that memory.
|
|
|
8d49642 |
|
|
|
8d49642 |
Tested: Tested against kvmclock unit test
|
|
|
8d49642 |
|
|
|
8d49642 |
Signed-off-by: Andrew Honig <ahonig@google.com>
|
|
|
8d49642 |
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
|
|
|
8d49642 |
---
|
|
|
8d49642 |
arch/x86/include/asm/kvm_host.h | 4 ++--
|
|
|
8d49642 |
arch/x86/kvm/x86.c | 47 ++++++++++++++++++-----------------------
|
|
|
8d49642 |
2 files changed, 22 insertions(+), 29 deletions(-)
|
|
|
8d49642 |
|
|
|
8d49642 |
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
|
|
|
8d49642 |
index 635a74d..4979778 100644
|
|
|
8d49642 |
--- a/arch/x86/include/asm/kvm_host.h
|
|
|
8d49642 |
+++ b/arch/x86/include/asm/kvm_host.h
|
|
|
8d49642 |
@@ -414,8 +414,8 @@ struct kvm_vcpu_arch {
|
|
|
8d49642 |
gpa_t time;
|
|
|
8d49642 |
struct pvclock_vcpu_time_info hv_clock;
|
|
|
8d49642 |
unsigned int hw_tsc_khz;
|
|
|
8d49642 |
- unsigned int time_offset;
|
|
|
8d49642 |
- struct page *time_page;
|
|
|
8d49642 |
+ struct gfn_to_hva_cache pv_time;
|
|
|
8d49642 |
+ bool pv_time_enabled;
|
|
|
8d49642 |
/* set guest stopped flag in pvclock flags field */
|
|
|
8d49642 |
bool pvclock_set_guest_stopped_request;
|
|
|
8d49642 |
|
|
|
8d49642 |
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
|
|
|
8d49642 |
index 2ade60c..f19ac0a 100644
|
|
|
8d49642 |
--- a/arch/x86/kvm/x86.c
|
|
|
8d49642 |
+++ b/arch/x86/kvm/x86.c
|
|
|
8d49642 |
@@ -1406,10 +1406,9 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
|
|
|
8d49642 |
unsigned long flags, this_tsc_khz;
|
|
|
8d49642 |
struct kvm_vcpu_arch *vcpu = &v->arch;
|
|
|
8d49642 |
struct kvm_arch *ka = &v->kvm->arch;
|
|
|
8d49642 |
- void *shared_kaddr;
|
|
|
8d49642 |
s64 kernel_ns, max_kernel_ns;
|
|
|
8d49642 |
u64 tsc_timestamp, host_tsc;
|
|
|
8d49642 |
- struct pvclock_vcpu_time_info *guest_hv_clock;
|
|
|
8d49642 |
+ struct pvclock_vcpu_time_info guest_hv_clock;
|
|
|
8d49642 |
u8 pvclock_flags;
|
|
|
8d49642 |
bool use_master_clock;
|
|
|
8d49642 |
|
|
|
8d49642 |
@@ -1463,7 +1462,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
|
|
|
8d49642 |
|
|
|
8d49642 |
local_irq_restore(flags);
|
|
|
8d49642 |
|
|
|
8d49642 |
- if (!vcpu->time_page)
|
|
|
8d49642 |
+ if (!vcpu->pv_time_enabled)
|
|
|
8d49642 |
return 0;
|
|
|
8d49642 |
|
|
|
8d49642 |
/*
|
|
|
8d49642 |
@@ -1525,12 +1524,12 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
|
|
|
8d49642 |
*/
|
|
|
8d49642 |
vcpu->hv_clock.version += 2;
|
|
|
8d49642 |
|
|
|
8d49642 |
- shared_kaddr = kmap_atomic(vcpu->time_page);
|
|
|
8d49642 |
-
|
|
|
8d49642 |
- guest_hv_clock = shared_kaddr + vcpu->time_offset;
|
|
|
8d49642 |
+ if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
|
|
|
8d49642 |
+ &guest_hv_clock, sizeof(guest_hv_clock))))
|
|
|
8d49642 |
+ return 0;
|
|
|
8d49642 |
|
|
|
8d49642 |
/* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
|
|
|
8d49642 |
- pvclock_flags = (guest_hv_clock->flags & PVCLOCK_GUEST_STOPPED);
|
|
|
8d49642 |
+ pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
|
|
|
8d49642 |
|
|
|
8d49642 |
if (vcpu->pvclock_set_guest_stopped_request) {
|
|
|
8d49642 |
pvclock_flags |= PVCLOCK_GUEST_STOPPED;
|
|
|
8d49642 |
@@ -1543,12 +1542,9 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
|
|
|
8d49642 |
|
|
|
8d49642 |
vcpu->hv_clock.flags = pvclock_flags;
|
|
|
8d49642 |
|
|
|
8d49642 |
- memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
|
|
|
8d49642 |
- sizeof(vcpu->hv_clock));
|
|
|
8d49642 |
-
|
|
|
8d49642 |
- kunmap_atomic(shared_kaddr);
|
|
|
8d49642 |
-
|
|
|
8d49642 |
- mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
|
|
|
8d49642 |
+ kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
|
|
|
8d49642 |
+ &vcpu->hv_clock,
|
|
|
8d49642 |
+ sizeof(vcpu->hv_clock));
|
|
|
8d49642 |
return 0;
|
|
|
8d49642 |
}
|
|
|
8d49642 |
|
|
|
8d49642 |
@@ -1837,10 +1833,7 @@ static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
|
|
|
8d49642 |
|
|
|
8d49642 |
static void kvmclock_reset(struct kvm_vcpu *vcpu)
|
|
|
8d49642 |
{
|
|
|
8d49642 |
- if (vcpu->arch.time_page) {
|
|
|
8d49642 |
- kvm_release_page_dirty(vcpu->arch.time_page);
|
|
|
8d49642 |
- vcpu->arch.time_page = NULL;
|
|
|
8d49642 |
- }
|
|
|
8d49642 |
+ vcpu->arch.pv_time_enabled = false;
|
|
|
8d49642 |
}
|
|
|
8d49642 |
|
|
|
8d49642 |
static void accumulate_steal_time(struct kvm_vcpu *vcpu)
|
|
|
8d49642 |
@@ -1947,6 +1940,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
|
|
|
8d49642 |
break;
|
|
|
8d49642 |
case MSR_KVM_SYSTEM_TIME_NEW:
|
|
|
8d49642 |
case MSR_KVM_SYSTEM_TIME: {
|
|
|
8d49642 |
+ u64 gpa_offset;
|
|
|
8d49642 |
kvmclock_reset(vcpu);
|
|
|
8d49642 |
|
|
|
8d49642 |
vcpu->arch.time = data;
|
|
|
8d49642 |
@@ -1956,19 +1950,17 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
|
|
|
8d49642 |
if (!(data & 1))
|
|
|
8d49642 |
break;
|
|
|
8d49642 |
|
|
|
8d49642 |
- /* ...but clean it before doing the actual write */
|
|
|
8d49642 |
- vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
|
|
|
8d49642 |
+ gpa_offset = data & ~(PAGE_MASK | 1);
|
|
|
8d49642 |
|
|
|
8d49642 |
/* Check that the address is 32-byte aligned. */
|
|
|
8d49642 |
- if (vcpu->arch.time_offset &
|
|
|
8d49642 |
- (sizeof(struct pvclock_vcpu_time_info) - 1))
|
|
|
8d49642 |
+ if (gpa_offset & (sizeof(struct pvclock_vcpu_time_info) - 1))
|
|
|
8d49642 |
break;
|
|
|
8d49642 |
|
|
|
8d49642 |
- vcpu->arch.time_page =
|
|
|
8d49642 |
- gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
|
|
|
8d49642 |
-
|
|
|
8d49642 |
- if (is_error_page(vcpu->arch.time_page))
|
|
|
8d49642 |
- vcpu->arch.time_page = NULL;
|
|
|
8d49642 |
+ if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
|
|
|
8d49642 |
+ &vcpu->arch.pv_time, data & ~1ULL))
|
|
|
8d49642 |
+ vcpu->arch.pv_time_enabled = false;
|
|
|
8d49642 |
+ else
|
|
|
8d49642 |
+ vcpu->arch.pv_time_enabled = true;
|
|
|
8d49642 |
|
|
|
8d49642 |
break;
|
|
|
8d49642 |
}
|
|
|
8d49642 |
@@ -2972,7 +2964,7 @@ static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
|
|
|
8d49642 |
*/
|
|
|
8d49642 |
static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
|
|
|
8d49642 |
{
|
|
|
8d49642 |
- if (!vcpu->arch.time_page)
|
|
|
8d49642 |
+ if (!vcpu->arch.pv_time_enabled)
|
|
|
8d49642 |
return -EINVAL;
|
|
|
8d49642 |
vcpu->arch.pvclock_set_guest_stopped_request = true;
|
|
|
8d49642 |
kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
|
|
|
8d49642 |
@@ -6723,6 +6715,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
|
|
|
8d49642 |
goto fail_free_wbinvd_dirty_mask;
|
|
|
8d49642 |
|
|
|
8d49642 |
vcpu->arch.ia32_tsc_adjust_msr = 0x0;
|
|
|
8d49642 |
+ vcpu->arch.pv_time_enabled = false;
|
|
|
8d49642 |
kvm_async_pf_hash_reset(vcpu);
|
|
|
8d49642 |
kvm_pmu_init(vcpu);
|
|
|
8d49642 |
|
|
|
8d49642 |
--
|
|
|
8d49642 |
1.8.1.4
|
|
|
8d49642 |
|