Skip to content

Commit 7cc7f67

Browse files
Wanpeng Libwhacks
authored andcommitted
KVM: Fix stack-out-of-bounds read in write_mmio
commit e39d200 upstream. Reported by syzkaller: BUG: KASAN: stack-out-of-bounds in write_mmio+0x11e/0x270 [kvm] Read of size 8 at addr ffff8803259df7f8 by task syz-executor/32298 CPU: 6 PID: 32298 Comm: syz-executor Tainted: G OE 4.15.0-rc2+ #18 Hardware name: LENOVO ThinkCentre M8500t-N000/SHARKBAY, BIOS FBKTC1AUS 02/16/2016 Call Trace: dump_stack+0xab/0xe1 print_address_description+0x6b/0x290 kasan_report+0x28a/0x370 write_mmio+0x11e/0x270 [kvm] emulator_read_write_onepage+0x311/0x600 [kvm] emulator_read_write+0xef/0x240 [kvm] emulator_fix_hypercall+0x105/0x150 [kvm] em_hypercall+0x2b/0x80 [kvm] x86_emulate_insn+0x2b1/0x1640 [kvm] x86_emulate_instruction+0x39a/0xb90 [kvm] handle_exception+0x1b4/0x4d0 [kvm_intel] vcpu_enter_guest+0x15a0/0x2640 [kvm] kvm_arch_vcpu_ioctl_run+0x549/0x7d0 [kvm] kvm_vcpu_ioctl+0x479/0x880 [kvm] do_vfs_ioctl+0x142/0x9a0 SyS_ioctl+0x74/0x80 entry_SYSCALL_64_fastpath+0x23/0x9a The path of patched vmmcall will patch 3 bytes opcode 0F 01 C1(vmcall) to the guest memory, however, write_mmio tracepoint always prints 8 bytes through *(u64 *)val since kvm splits the mmio access into 8 bytes. This leaks 5 bytes from the kernel stack (CVE-2017-17741). This patch fixes it by just accessing the bytes which we operate on. Before patch: syz-executor-5567 [007] .... 51370.561696: kvm_mmio: mmio write len 3 gpa 0x10 val 0x1ffff10077c1010f After patch: syz-executor-13416 [002] .... 51302.299573: kvm_mmio: mmio write len 3 gpa 0x10 val 0xc1010f Reported-by: Dmitry Vyukov <[email protected]> Reviewed-by: Darren Kenny <[email protected]> Reviewed-by: Marc Zyngier <[email protected]> Tested-by: Marc Zyngier <[email protected]> Cc: Paolo Bonzini <[email protected]> Cc: Radim Krčmář <[email protected]> Cc: Marc Zyngier <[email protected]> Cc: Christoffer Dall <[email protected]> Signed-off-by: Wanpeng Li <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> [bwh: Backported to 3.16: - ARM implementation combines the KVM_TRACE_MMIO_WRITE and KVM_TRACE_MMIO_READ_UNSATISFIED cases - Adjust filename] Signed-off-by: Ben Hutchings <[email protected]>
1 parent 7dd8d19 commit 7cc7f67

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

arch/arm/kvm/mmio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run)
113113
}
114114

115115
trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
116-
data);
116+
&data);
117117
data = vcpu_data_host_to_guest(vcpu, data, len);
118118
*vcpu_reg(vcpu, vcpu->arch.mmio_decode.rt) = data;
119119
}
@@ -192,7 +192,7 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,
192192
trace_kvm_mmio((mmio.is_write) ? KVM_TRACE_MMIO_WRITE :
193193
KVM_TRACE_MMIO_READ_UNSATISFIED,
194194
mmio.len, fault_ipa,
195-
(mmio.is_write) ? data : 0);
195+
(mmio.is_write) ? &data : NULL);
196196

197197
if (mmio.is_write)
198198
mmio_write_buf(mmio.data, mmio.len, data);

arch/x86/kvm/x86.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,7 +4052,7 @@ static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
40524052
!kvm_iodevice_read(&vcpu->arch.apic->dev, addr, n, v))
40534053
&& kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, n, v))
40544054
break;
4055-
trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, *(u64 *)v);
4055+
trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
40564056
handled += n;
40574057
addr += n;
40584058
len -= n;
@@ -4276,7 +4276,7 @@ static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
42764276
{
42774277
if (vcpu->mmio_read_completed) {
42784278
trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
4279-
vcpu->mmio_fragments[0].gpa, *(u64 *)val);
4279+
vcpu->mmio_fragments[0].gpa, val);
42804280
vcpu->mmio_read_completed = 0;
42814281
return 1;
42824282
}
@@ -4298,14 +4298,14 @@ static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
42984298

42994299
static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
43004300
{
4301-
trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
4301+
trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
43024302
return vcpu_mmio_write(vcpu, gpa, bytes, val);
43034303
}
43044304

43054305
static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
43064306
void *val, int bytes)
43074307
{
4308-
trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
4308+
trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
43094309
return X86EMUL_IO_NEEDED;
43104310
}
43114311

include/trace/events/kvm.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ TRACE_EVENT(kvm_ack_irq,
163163
{ KVM_TRACE_MMIO_WRITE, "write" }
164164

165165
TRACE_EVENT(kvm_mmio,
166-
TP_PROTO(int type, int len, u64 gpa, u64 val),
166+
TP_PROTO(int type, int len, u64 gpa, void *val),
167167
TP_ARGS(type, len, gpa, val),
168168

169169
TP_STRUCT__entry(
@@ -177,7 +177,10 @@ TRACE_EVENT(kvm_mmio,
177177
__entry->type = type;
178178
__entry->len = len;
179179
__entry->gpa = gpa;
180-
__entry->val = val;
180+
__entry->val = 0;
181+
if (val)
182+
memcpy(&__entry->val, val,
183+
min_t(u32, sizeof(__entry->val), len));
181184
),
182185

183186
TP_printk("mmio %s len %u gpa 0x%llx val 0x%llx",

0 commit comments

Comments
 (0)