Skip to content

Commit fe08e36

Browse files
committed
Merge branch 'kvm-dwmw2-fixes' into HEAD
This brings in a few important fixes for Xen emulation. While nobody should be enabling it, the bug effectively allows userspace to read arbitrary memory. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2 parents 47b0c2e + 8332f0e commit fe08e36

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

arch/x86/kvm/xen.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,14 @@ static int kvm_xen_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
954954
return kvm_xen_hypercall_set_result(vcpu, run->xen.u.hcall.result);
955955
}
956956

957+
static inline int max_evtchn_port(struct kvm *kvm)
958+
{
959+
if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode)
960+
return EVTCHN_2L_NR_CHANNELS;
961+
else
962+
return COMPAT_EVTCHN_2L_NR_CHANNELS;
963+
}
964+
957965
static bool wait_pending_event(struct kvm_vcpu *vcpu, int nr_ports,
958966
evtchn_port_t *ports)
959967
{
@@ -1042,6 +1050,10 @@ static bool kvm_xen_schedop_poll(struct kvm_vcpu *vcpu, bool longmode,
10421050
*r = -EFAULT;
10431051
goto out;
10441052
}
1053+
if (ports[i] >= max_evtchn_port(vcpu->kvm)) {
1054+
*r = -EINVAL;
1055+
goto out;
1056+
}
10451057
}
10461058

10471059
if (sched_poll.nr_ports == 1)
@@ -1215,6 +1227,7 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
12151227
bool longmode;
12161228
u64 input, params[6], r = -ENOSYS;
12171229
bool handled = false;
1230+
u8 cpl;
12181231

12191232
input = (u64)kvm_register_read(vcpu, VCPU_REGS_RAX);
12201233

@@ -1242,9 +1255,17 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
12421255
params[5] = (u64)kvm_r9_read(vcpu);
12431256
}
12441257
#endif
1258+
cpl = static_call(kvm_x86_get_cpl)(vcpu);
12451259
trace_kvm_xen_hypercall(input, params[0], params[1], params[2],
12461260
params[3], params[4], params[5]);
12471261

1262+
/*
1263+
* Only allow hypercall acceleration for CPL0. The rare hypercalls that
1264+
* are permitted in guest userspace can be handled by the VMM.
1265+
*/
1266+
if (unlikely(cpl > 0))
1267+
goto handle_in_userspace;
1268+
12481269
switch (input) {
12491270
case __HYPERVISOR_xen_version:
12501271
if (params[0] == XENVER_version && vcpu->kvm->arch.xen.xen_version) {
@@ -1279,10 +1300,11 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
12791300
if (handled)
12801301
return kvm_xen_hypercall_set_result(vcpu, r);
12811302

1303+
handle_in_userspace:
12821304
vcpu->run->exit_reason = KVM_EXIT_XEN;
12831305
vcpu->run->xen.type = KVM_EXIT_XEN_HCALL;
12841306
vcpu->run->xen.u.hcall.longmode = longmode;
1285-
vcpu->run->xen.u.hcall.cpl = static_call(kvm_x86_get_cpl)(vcpu);
1307+
vcpu->run->xen.u.hcall.cpl = cpl;
12861308
vcpu->run->xen.u.hcall.input = input;
12871309
vcpu->run->xen.u.hcall.params[0] = params[0];
12881310
vcpu->run->xen.u.hcall.params[1] = params[1];
@@ -1297,14 +1319,6 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
12971319
return 0;
12981320
}
12991321

1300-
static inline int max_evtchn_port(struct kvm *kvm)
1301-
{
1302-
if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode)
1303-
return EVTCHN_2L_NR_CHANNELS;
1304-
else
1305-
return COMPAT_EVTCHN_2L_NR_CHANNELS;
1306-
}
1307-
13081322
static void kvm_xen_check_poller(struct kvm_vcpu *vcpu, int port)
13091323
{
13101324
int poll_evtchn = vcpu->arch.xen.poll_evtchn;

virt/kvm/pfncache.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,12 @@ int kvm_gfn_to_pfn_cache_refresh(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,
297297
if (!gpc->valid || old_uhva != gpc->uhva) {
298298
ret = hva_to_pfn_retry(kvm, gpc);
299299
} else {
300-
/* If the HVA→PFN mapping was already valid, don't unmap it. */
300+
/*
301+
* If the HVA→PFN mapping was already valid, don't unmap it.
302+
* But do update gpc->khva because the offset within the page
303+
* may have changed.
304+
*/
305+
gpc->khva = old_khva + page_offset;
301306
old_pfn = KVM_PFN_ERR_FAULT;
302307
old_khva = NULL;
303308
ret = 0;

0 commit comments

Comments
 (0)