linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Liran Alon <liran.alon@oracle.com>
Cc: Alexander Potapenko <glider@google.com>,
	syzbot+ded1696f6b50b615b630@syzkaller.appspotmail.com,
	kvm@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>,
	rkrcmar@redhat.com, syzkaller-bugs@googlegroups.com
Subject: Re: KMSAN: kernel-infoleak in kvm_vcpu_write_guest_page
Date: Wed, 7 Nov 2018 14:37:39 +0100	[thread overview]
Message-ID: <6f79d9be-fa76-3a06-2612-f44f3a18ece7@redhat.com> (raw)
In-Reply-To: <5A114BC0-96E7-41AF-A975-EC3B87A5A60D@oracle.com>

On 07/11/2018 13:58, Liran Alon wrote:
> 
> 
>> On 7 Nov 2018, at 14:47, Paolo Bonzini <pbonzini@redhat.com> wrote:
>>
>> On 07/11/2018 13:10, Alexander Potapenko wrote:
>>> This appears to be a real bug in KVM.
>>> Please see a simplified reproducer attached.
>>
>> Thanks, I agree it's a reael bug.  The basic issue is that the
>> kvm_state->size member is too small (1040) in the KVM_SET_NESTED_STATE
>> ioctl, aka 0x4080aebf.
>>
>> One way to fix it would be to just change kmalloc to kzalloc when
>> allocating cached_vmcs12 and cached_shadow_vmcs12, but really the ioctl
>> is wrong and should be rejected.  And the case where a shadow VMCS has
>> to be loaded is even more wrong, and we have to fix it anyway, so I
>> don't really like the idea of papering over the bug in the allocation.
>>
>> I'll test this patch and submit it formally:
>>
>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>> index c645f777b425..c546f0b1f3e0 100644
>> --- a/arch/x86/kvm/vmx.c
>> +++ b/arch/x86/kvm/vmx.c
>> @@ -14888,10 +14888,13 @@ static int vmx_set_nested_state(struct
>> kvm_vcpu *vcpu,
>> 	if (ret)
>> 		return ret;
>>
>> -	/* Empty 'VMXON' state is permitted */
>> -	if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
>> +	/* Empty 'VMXON' state is permitted.  A partial VMCS12 is not.  */
>> +	if (kvm_state->size == sizeof(kvm_state))
>> 		return 0;
>>
>> +	if (kvm_state->size < sizeof(kvm_state) + VMCS12_SIZE)
>> +		return -EINVAL;
>> +
> 
> I don’t think that this test is sufficient to fully resolve issue.
> What if malicious userspace supplies valid size but pages containing nested_state->vmcs12 is unmapped?
> This will result in vmx_set_nested_state() still calling set_current_vmptr() but failing on copy_from_user()
> which still leaks cached_vmcs12 on next VMPTRLD of guest.

Makes sense; since SET_NESTED_STATE is not a fast path, we can just
memdup_user and pass a kernel pointer to vmx_set_nested_state.

> Therefore, I think that the correct patch should be to change vmx_set_nested_state() to
> first gather all relevant information from userspace and validate it,
> and only then start applying it to KVM’s internal vCPU state.
> 
>> 	if (kvm_state->vmx.vmcs_pa != -1ull) {
>> 		if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
>> 		    !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
>> @@ -14917,6 +14920,7 @@ static int vmx_set_nested_state(struct kvm_vcpu
>> *vcpu,
>> 	}
>>
>> 	vmcs12 = get_vmcs12(vcpu);
>> +	BUILD_BUG_ON(sizeof(*vmcs12) > VMCS12_SIZE);
> 
> Why put this BUILD_BUG_ON() specifically here?
> There are many places which assumes cached_vmcs12 is of size VMCS12_SIZE.
> (Such as nested_release_vmcs12() and handle_vmptrld()).

Unlike those places, here the copy has sizeof(*vmcs12) bytes and an
overflow would cause a userspace write to kernel memory.  Though, that
means there is still a possibility of leaking kernel data when
nested_release_vmcs12 is called.  So it also makes sense to use
VMCS12_SIZE for the memory copies, and kzalloc.

Thanks,

Paolo

>> 	if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
>> 		return -EFAULT;
>>
>> @@ -14932,7 +14936,7 @@ static int vmx_set_nested_state(struct kvm_vcpu
>> *vcpu,
>> 	if (nested_cpu_has_shadow_vmcs(vmcs12) &&
>> 	    vmcs12->vmcs_link_pointer != -1ull) {
>> 		struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
>> -		if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
>> +		if (kvm_state->size < sizeof(kvm_state) + 2 * VMCS12_SIZE)
>> 			return -EINVAL;
>>
>> 		if (copy_from_user(shadow_vmcs12,
>>
>> Paolo
> 
> -Liran
> 
> 


  reply	other threads:[~2018-11-07 13:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-07  1:38 KMSAN: kernel-infoleak in kvm_vcpu_write_guest_page syzbot
2018-11-07 12:10 ` Alexander Potapenko
2018-11-07 12:47   ` Paolo Bonzini
2018-11-07 12:58     ` Liran Alon
2018-11-07 13:37       ` Paolo Bonzini [this message]
2019-01-14 23:47         ` [RFC PATCH] kvm: x86/vmx: Use kzalloc for cached_vmcs12 Tom Roeder
2019-01-15  0:03           ` Jim Mattson
2019-01-15  2:43           ` Sean Christopherson
2019-01-15 10:15             ` Paolo Bonzini
2019-01-23 18:25               ` Tom Roeder
2019-01-24  1:17                 ` Paolo Bonzini
2019-01-15 17:51             ` Tom Roeder
2019-01-23 18:33               ` Tom Roeder
2019-01-24  1:18                 ` Paolo Bonzini
2019-01-24 21:46                   ` Tom Roeder
2018-11-07 12:52   ` KMSAN: kernel-infoleak in kvm_vcpu_write_guest_page Liran Alon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6f79d9be-fa76-3a06-2612-f44f3a18ece7@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=glider@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liran.alon@oracle.com \
    --cc=rkrcmar@redhat.com \
    --cc=syzbot+ded1696f6b50b615b630@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).