linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND] KVM: X86: Fix stack-out-of-bounds read in write_mmio
@ 2017-12-12  8:57 Wanpeng Li
  2017-12-12 16:07 ` Paolo Bonzini
  0 siblings, 1 reply; 4+ messages in thread
From: Wanpeng Li @ 2017-12-12  8:57 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Paolo Bonzini, Radim Krčmář, Wanpeng Li

From: Wanpeng Li <wanpeng.li@hotmail.com>

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 
can result in stack-out-of-bounds read due to access the extra 5 bytes. 
This patch fixes it by just accessing the bytes which we operates 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 <dvyukov@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
---
 arch/x86/kvm/x86.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index bc5d853..51e7932 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4690,7 +4690,10 @@ static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
 
 static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
 {
-	trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
+	u64 data = 0;
+
+	memcpy(&data, val, min(8, bytes));
+	trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, data);
 	return vcpu_mmio_write(vcpu, gpa, bytes, val);
 }
 
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH RESEND] KVM: X86: Fix stack-out-of-bounds read in write_mmio
  2017-12-12  8:57 [PATCH RESEND] KVM: X86: Fix stack-out-of-bounds read in write_mmio Wanpeng Li
@ 2017-12-12 16:07 ` Paolo Bonzini
  2017-12-13  3:05   ` Wanpeng Li
  0 siblings, 1 reply; 4+ messages in thread
From: Paolo Bonzini @ 2017-12-12 16:07 UTC (permalink / raw)
  To: Wanpeng Li, linux-kernel, kvm; +Cc: Radim Krčmář, Wanpeng Li

On 12/12/2017 09:57, Wanpeng Li wrote:
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index bc5d853..51e7932 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -4690,7 +4690,10 @@ static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
>  
>  static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
>  {
> -	trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
> +	u64 data = 0;
> +
> +	memcpy(&data, val, min(8, bytes));
> +	trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, data);
>  	return vcpu_mmio_write(vcpu, gpa, bytes, val);
>  }
>  
> 

Please do the memcpy in TRACE_EVENT(kvm_mmio)'s TP_fast_assign block.
That is done only when the trace event is active.

Thanks,

Paolo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH RESEND] KVM: X86: Fix stack-out-of-bounds read in write_mmio
  2017-12-12 16:07 ` Paolo Bonzini
@ 2017-12-13  3:05   ` Wanpeng Li
  2017-12-13  9:56     ` Paolo Bonzini
  0 siblings, 1 reply; 4+ messages in thread
From: Wanpeng Li @ 2017-12-13  3:05 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: linux-kernel, kvm, Radim Krčmář, Wanpeng Li

2017-12-13 0:07 GMT+08:00 Paolo Bonzini <pbonzini@redhat.com>:
> On 12/12/2017 09:57, Wanpeng Li wrote:
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index bc5d853..51e7932 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -4690,7 +4690,10 @@ static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
>>
>>  static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
>>  {
>> -     trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
>> +     u64 data = 0;
>> +
>> +     memcpy(&data, val, min(8, bytes));
>> +     trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, data);
>>       return vcpu_mmio_write(vcpu, gpa, bytes, val);
>>  }
>>
>>
>
> Please do the memcpy in TRACE_EVENT(kvm_mmio)'s TP_fast_assign block.
> That is done only when the trace event is active.

I still can observe the stack out-of-bounds read warning if keep *(u64
*)val as the parameter since it has already been dereferenced. So
maybe we should change the parameter of trace_kvm_mmio() to void *val,
however, I'm not sure whether it will break the tracepoint ABI which
this article https://lwn.net/Articles/734039/ "Workload analysis with
tracing" section pointed out.

Regards,
Wanpeng Li

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH RESEND] KVM: X86: Fix stack-out-of-bounds read in write_mmio
  2017-12-13  3:05   ` Wanpeng Li
@ 2017-12-13  9:56     ` Paolo Bonzini
  0 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2017-12-13  9:56 UTC (permalink / raw)
  To: Wanpeng Li; +Cc: linux-kernel, kvm, Radim Krčmář, Wanpeng Li

On 13/12/2017 04:05, Wanpeng Li wrote:
> 2017-12-13 0:07 GMT+08:00 Paolo Bonzini <pbonzini@redhat.com>:
>> On 12/12/2017 09:57, Wanpeng Li wrote:
>>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>>> index bc5d853..51e7932 100644
>>> --- a/arch/x86/kvm/x86.c
>>> +++ b/arch/x86/kvm/x86.c
>>> @@ -4690,7 +4690,10 @@ static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
>>>
>>>  static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
>>>  {
>>> -     trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
>>> +     u64 data = 0;
>>> +
>>> +     memcpy(&data, val, min(8, bytes));
>>> +     trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, data);
>>>       return vcpu_mmio_write(vcpu, gpa, bytes, val);
>>>  }
>>>
>>>
>>
>> Please do the memcpy in TRACE_EVENT(kvm_mmio)'s TP_fast_assign block.
>> That is done only when the trace event is active.
> 
> I still can observe the stack out-of-bounds read warning if keep *(u64
> *)val as the parameter since it has already been dereferenced. So
> maybe we should change the parameter of trace_kvm_mmio() to void *val,

Yes, exactly.

> however, I'm not sure whether it will break the tracepoint ABI which
> this article https://lwn.net/Articles/734039/ "Workload analysis with
> tracing" section pointed out.

No, the tracepoint ABI refers to TP_PROTO, not TP_STRUCT and
TP_fast_assign.  Good that you thought about it, though!

Thanks,

Paolo

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-12-13  9:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-12  8:57 [PATCH RESEND] KVM: X86: Fix stack-out-of-bounds read in write_mmio Wanpeng Li
2017-12-12 16:07 ` Paolo Bonzini
2017-12-13  3:05   ` Wanpeng Li
2017-12-13  9:56     ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).