linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kvm: fix page struct leak in handle_vmon
@ 2017-01-24 10:56 Paolo Bonzini
  2017-01-25  9:31 ` David Hildenbrand
  2017-01-25 10:11 ` David Hildenbrand
  0 siblings, 2 replies; 7+ messages in thread
From: Paolo Bonzini @ 2017-01-24 10:56 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: dvyukov

handle_vmon gets a reference on VMXON region page,
but does not release it. Release the reference.

Found by syzkaller; based on a patch by Dmitry.

Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/vmx.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 42cc3d6f4d20..0f7345035210 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -7085,13 +7085,18 @@ static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
 		}
 
 		page = nested_get_page(vcpu, vmptr);
-		if (page == NULL ||
-		    *(u32 *)kmap(page) != VMCS12_REVISION) {
+		if (page == NULL) {
 			nested_vmx_failInvalid(vcpu);
+			return kvm_skip_emulated_instruction(vcpu);
+		}
+		if (*(u32 *)kmap(page) != VMCS12_REVISION) {
 			kunmap(page);
+			nested_release_page_clean(page);
+			nested_vmx_failInvalid(vcpu);
 			return kvm_skip_emulated_instruction(vcpu);
 		}
 		kunmap(page);
+		nested_release_page_clean(page);
 		vmx->nested.vmxon_ptr = vmptr;
 		break;
 	case EXIT_REASON_VMCLEAR:
-- 
1.8.3.1

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

* Re: [PATCH] kvm: fix page struct leak in handle_vmon
  2017-01-24 10:56 [PATCH] kvm: fix page struct leak in handle_vmon Paolo Bonzini
@ 2017-01-25  9:31 ` David Hildenbrand
  2017-01-25  9:52   ` Paolo Bonzini
  2017-01-25 10:11 ` David Hildenbrand
  1 sibling, 1 reply; 7+ messages in thread
From: David Hildenbrand @ 2017-01-25  9:31 UTC (permalink / raw)
  To: Paolo Bonzini, linux-kernel, kvm; +Cc: dvyukov

Am 24.01.2017 um 11:56 schrieb Paolo Bonzini:
> handle_vmon gets a reference on VMXON region page,
> but does not release it. Release the reference.
> 
> Found by syzkaller; based on a patch by Dmitry.
> 
> Reported-by: Dmitry Vyukov <dvyukov@google.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  arch/x86/kvm/vmx.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 42cc3d6f4d20..0f7345035210 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -7085,13 +7085,18 @@ static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
>  		}
>  
>  		page = nested_get_page(vcpu, vmptr);
> -		if (page == NULL ||
> -		    *(u32 *)kmap(page) != VMCS12_REVISION) {
> +		if (page == NULL) {
>  			nested_vmx_failInvalid(vcpu);
> +			return kvm_skip_emulated_instruction(vcpu);
> +		}
> +		if (*(u32 *)kmap(page) != VMCS12_REVISION) {

shouldn't we also check if kmap even returned a valid pointer before
dereferencing it?

David thinks so.

>  			kunmap(page);
> +			nested_release_page_clean(page);
> +			nested_vmx_failInvalid(vcpu);
>  			return kvm_skip_emulated_instruction(vcpu);
>  		}
>  		kunmap(page);
> +		nested_release_page_clean(page);
>  		vmx->nested.vmxon_ptr = vmptr;
>  		break;
>  	case EXIT_REASON_VMCLEAR:
> 

-- 

David

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

* Re: [PATCH] kvm: fix page struct leak in handle_vmon
  2017-01-25  9:31 ` David Hildenbrand
@ 2017-01-25  9:52   ` Paolo Bonzini
  2017-01-25  9:57     ` David Hildenbrand
  0 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2017-01-25  9:52 UTC (permalink / raw)
  To: David Hildenbrand; +Cc: linux-kernel, kvm, dvyukov



----- Original Message -----
> From: "David Hildenbrand" <david@redhat.com>
> To: "Paolo Bonzini" <pbonzini@redhat.com>, linux-kernel@vger.kernel.org, kvm@vger.kernel.org
> Cc: dvyukov@google.com
> Sent: Wednesday, January 25, 2017 10:31:13 AM
> Subject: Re: [PATCH] kvm: fix page struct leak in handle_vmon
> 
> Am 24.01.2017 um 11:56 schrieb Paolo Bonzini:
> > handle_vmon gets a reference on VMXON region page,
> > but does not release it. Release the reference.
> > 
> > Found by syzkaller; based on a patch by Dmitry.
> > 
> > Reported-by: Dmitry Vyukov <dvyukov@google.com>
> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> > ---
> >  arch/x86/kvm/vmx.c | 9 +++++++--
> >  1 file changed, 7 insertions(+), 2 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> > index 42cc3d6f4d20..0f7345035210 100644
> > --- a/arch/x86/kvm/vmx.c
> > +++ b/arch/x86/kvm/vmx.c
> > @@ -7085,13 +7085,18 @@ static int nested_vmx_check_vmptr(struct kvm_vcpu
> > *vcpu, int exit_reason,
> >  		}
> >  
> >  		page = nested_get_page(vcpu, vmptr);
> > -		if (page == NULL ||
> > -		    *(u32 *)kmap(page) != VMCS12_REVISION) {
> > +		if (page == NULL) {
> >  			nested_vmx_failInvalid(vcpu);
> > +			return kvm_skip_emulated_instruction(vcpu);
> > +		}
> > +		if (*(u32 *)kmap(page) != VMCS12_REVISION) {
> 
> shouldn't we also check if kmap even returned a valid pointer before
> dereferencing it?

I don't think kmap can fail (page_address can)?

Paolo

> >  			kunmap(page);
> > +			nested_release_page_clean(page);
> > +			nested_vmx_failInvalid(vcpu);
> >  			return kvm_skip_emulated_instruction(vcpu);
> >  		}
> >  		kunmap(page);
> > +		nested_release_page_clean(page);
> >  		vmx->nested.vmxon_ptr = vmptr;
> >  		break;
> >  	case EXIT_REASON_VMCLEAR:
> > 
> 
> --
> 
> David
> 

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

* Re: [PATCH] kvm: fix page struct leak in handle_vmon
  2017-01-25  9:52   ` Paolo Bonzini
@ 2017-01-25  9:57     ` David Hildenbrand
  2017-01-25 10:11       ` David Hildenbrand
  0 siblings, 1 reply; 7+ messages in thread
From: David Hildenbrand @ 2017-01-25  9:57 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: linux-kernel, kvm, dvyukov

Am 25.01.2017 um 10:52 schrieb Paolo Bonzini:
> 
> 
> ----- Original Message -----
>> From: "David Hildenbrand" <david@redhat.com>
>> To: "Paolo Bonzini" <pbonzini@redhat.com>, linux-kernel@vger.kernel.org, kvm@vger.kernel.org
>> Cc: dvyukov@google.com
>> Sent: Wednesday, January 25, 2017 10:31:13 AM
>> Subject: Re: [PATCH] kvm: fix page struct leak in handle_vmon
>>
>> Am 24.01.2017 um 11:56 schrieb Paolo Bonzini:
>>> handle_vmon gets a reference on VMXON region page,
>>> but does not release it. Release the reference.
>>>
>>> Found by syzkaller; based on a patch by Dmitry.
>>>
>>> Reported-by: Dmitry Vyukov <dvyukov@google.com>
>>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>>> ---
>>>  arch/x86/kvm/vmx.c | 9 +++++++--
>>>  1 file changed, 7 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>>> index 42cc3d6f4d20..0f7345035210 100644
>>> --- a/arch/x86/kvm/vmx.c
>>> +++ b/arch/x86/kvm/vmx.c
>>> @@ -7085,13 +7085,18 @@ static int nested_vmx_check_vmptr(struct kvm_vcpu
>>> *vcpu, int exit_reason,
>>>  		}
>>>  
>>>  		page = nested_get_page(vcpu, vmptr);
>>> -		if (page == NULL ||
>>> -		    *(u32 *)kmap(page) != VMCS12_REVISION) {
>>> +		if (page == NULL) {
>>>  			nested_vmx_failInvalid(vcpu);
>>> +			return kvm_skip_emulated_instruction(vcpu);
>>> +		}
>>> +		if (*(u32 *)kmap(page) != VMCS12_REVISION) {
>>
>> shouldn't we also check if kmap even returned a valid pointer before
>> dereferencing it?
> 
> I don't think kmap can fail (page_address can)?

Then I wonder why there are some checks:

e.g. nested_vmx_merge_msr_bitmap()

msr_bitmap_l1 = (unsigned long *)kmap(page);
if (!msr_bitmap_l1) {
	// no unmap
	...
	return false;

or vmx_complete_nested_posted_interrupt()

vapic_page = kmap(vmx->nested.virtual_apic_page);
if (!vapic_page) {
	// no unmap
	...
	return -ENOMEM;


But there is also no check in handle_vmptrld() for example.


> 
> Paolo


-- 

David

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

* Re: [PATCH] kvm: fix page struct leak in handle_vmon
  2017-01-25  9:57     ` David Hildenbrand
@ 2017-01-25 10:11       ` David Hildenbrand
  2017-01-25 10:35         ` Paolo Bonzini
  0 siblings, 1 reply; 7+ messages in thread
From: David Hildenbrand @ 2017-01-25 10:11 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: linux-kernel, kvm, dvyukov

Am 25.01.2017 um 10:57 schrieb David Hildenbrand:
> Am 25.01.2017 um 10:52 schrieb Paolo Bonzini:
>>
>>
>> ----- Original Message -----
>>> From: "David Hildenbrand" <david@redhat.com>
>>> To: "Paolo Bonzini" <pbonzini@redhat.com>, linux-kernel@vger.kernel.org, kvm@vger.kernel.org
>>> Cc: dvyukov@google.com
>>> Sent: Wednesday, January 25, 2017 10:31:13 AM
>>> Subject: Re: [PATCH] kvm: fix page struct leak in handle_vmon
>>>
>>> Am 24.01.2017 um 11:56 schrieb Paolo Bonzini:
>>>> handle_vmon gets a reference on VMXON region page,
>>>> but does not release it. Release the reference.
>>>>
>>>> Found by syzkaller; based on a patch by Dmitry.
>>>>
>>>> Reported-by: Dmitry Vyukov <dvyukov@google.com>
>>>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>>>> ---
>>>>  arch/x86/kvm/vmx.c | 9 +++++++--
>>>>  1 file changed, 7 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>>>> index 42cc3d6f4d20..0f7345035210 100644
>>>> --- a/arch/x86/kvm/vmx.c
>>>> +++ b/arch/x86/kvm/vmx.c
>>>> @@ -7085,13 +7085,18 @@ static int nested_vmx_check_vmptr(struct kvm_vcpu
>>>> *vcpu, int exit_reason,
>>>>  		}
>>>>  
>>>>  		page = nested_get_page(vcpu, vmptr);
>>>> -		if (page == NULL ||
>>>> -		    *(u32 *)kmap(page) != VMCS12_REVISION) {
>>>> +		if (page == NULL) {
>>>>  			nested_vmx_failInvalid(vcpu);
>>>> +			return kvm_skip_emulated_instruction(vcpu);
>>>> +		}
>>>> +		if (*(u32 *)kmap(page) != VMCS12_REVISION) {
>>>
>>> shouldn't we also check if kmap even returned a valid pointer before
>>> dereferencing it?
>>
>> I don't think kmap can fail (page_address can)?
> 
> Then I wonder why there are some checks:
> 
> e.g. nested_vmx_merge_msr_bitmap()
> 
> msr_bitmap_l1 = (unsigned long *)kmap(page);
> if (!msr_bitmap_l1) {
> 	// no unmap
> 	...
> 	return false;
> 
> or vmx_complete_nested_posted_interrupt()
> 
> vapic_page = kmap(vmx->nested.virtual_apic_page);
> if (!vapic_page) {
> 	// no unmap
> 	...
> 	return -ENOMEM;
> 
> 
> But there is also no check in handle_vmptrld() for example.
> 
> 
>>
>> Paolo
> 
> 

Think you're right it can't fail.

So something like that could most likely be done

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index a236dec..a9be221 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -4973,10 +4973,6 @@ static int
vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
                        return 0;

                vapic_page = kmap(vmx->nested.virtual_apic_page);
-               if (!vapic_page) {
-                       WARN_ON(1);
-                       return -ENOMEM;
-               }
                __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
                kunmap(vmx->nested.virtual_apic_page);

@@ -9730,12 +9726,6 @@ static inline bool
nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
                return false;
        }
        msr_bitmap_l1 = (unsigned long *)kmap(page);
-       if (!msr_bitmap_l1) {
-               nested_release_page_clean(page);
-               WARN_ON(1);
-               return false;
-       }
-
        memset(msr_bitmap_l0, 0xff, PAGE_SIZE);

        if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {

-- 

David

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

* Re: [PATCH] kvm: fix page struct leak in handle_vmon
  2017-01-24 10:56 [PATCH] kvm: fix page struct leak in handle_vmon Paolo Bonzini
  2017-01-25  9:31 ` David Hildenbrand
@ 2017-01-25 10:11 ` David Hildenbrand
  1 sibling, 0 replies; 7+ messages in thread
From: David Hildenbrand @ 2017-01-25 10:11 UTC (permalink / raw)
  To: Paolo Bonzini, linux-kernel, kvm; +Cc: dvyukov

Am 24.01.2017 um 11:56 schrieb Paolo Bonzini:
> handle_vmon gets a reference on VMXON region page,
> but does not release it. Release the reference.
> 
> Found by syzkaller; based on a patch by Dmitry.
> 
> Reported-by: Dmitry Vyukov <dvyukov@google.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  arch/x86/kvm/vmx.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 42cc3d6f4d20..0f7345035210 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -7085,13 +7085,18 @@ static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
>  		}
>  
>  		page = nested_get_page(vcpu, vmptr);
> -		if (page == NULL ||
> -		    *(u32 *)kmap(page) != VMCS12_REVISION) {
> +		if (page == NULL) {
>  			nested_vmx_failInvalid(vcpu);
> +			return kvm_skip_emulated_instruction(vcpu);
> +		}
> +		if (*(u32 *)kmap(page) != VMCS12_REVISION) {
>  			kunmap(page);
> +			nested_release_page_clean(page);
> +			nested_vmx_failInvalid(vcpu);
>  			return kvm_skip_emulated_instruction(vcpu);
>  		}
>  		kunmap(page);
> +		nested_release_page_clean(page);
>  		vmx->nested.vmxon_ptr = vmptr;
>  		break;
>  	case EXIT_REASON_VMCLEAR:
> 

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 

David

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

* Re: [PATCH] kvm: fix page struct leak in handle_vmon
  2017-01-25 10:11       ` David Hildenbrand
@ 2017-01-25 10:35         ` Paolo Bonzini
  0 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2017-01-25 10:35 UTC (permalink / raw)
  To: David Hildenbrand; +Cc: linux-kernel, kvm, dvyukov



On 25/01/2017 11:11, David Hildenbrand wrote:
> Am 25.01.2017 um 10:57 schrieb David Hildenbrand:
>> Am 25.01.2017 um 10:52 schrieb Paolo Bonzini:
>>>
>>>
>>> ----- Original Message -----
>>>> From: "David Hildenbrand" <david@redhat.com>
>>>> To: "Paolo Bonzini" <pbonzini@redhat.com>, linux-kernel@vger.kernel.org, kvm@vger.kernel.org
>>>> Cc: dvyukov@google.com
>>>> Sent: Wednesday, January 25, 2017 10:31:13 AM
>>>> Subject: Re: [PATCH] kvm: fix page struct leak in handle_vmon
>>>>
>>>> Am 24.01.2017 um 11:56 schrieb Paolo Bonzini:
>>>>> handle_vmon gets a reference on VMXON region page,
>>>>> but does not release it. Release the reference.
>>>>>
>>>>> Found by syzkaller; based on a patch by Dmitry.
>>>>>
>>>>> Reported-by: Dmitry Vyukov <dvyukov@google.com>
>>>>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>>>>> ---
>>>>>  arch/x86/kvm/vmx.c | 9 +++++++--
>>>>>  1 file changed, 7 insertions(+), 2 deletions(-)
>>>>>
>>>>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>>>>> index 42cc3d6f4d20..0f7345035210 100644
>>>>> --- a/arch/x86/kvm/vmx.c
>>>>> +++ b/arch/x86/kvm/vmx.c
>>>>> @@ -7085,13 +7085,18 @@ static int nested_vmx_check_vmptr(struct kvm_vcpu
>>>>> *vcpu, int exit_reason,
>>>>>  		}
>>>>>  
>>>>>  		page = nested_get_page(vcpu, vmptr);
>>>>> -		if (page == NULL ||
>>>>> -		    *(u32 *)kmap(page) != VMCS12_REVISION) {
>>>>> +		if (page == NULL) {
>>>>>  			nested_vmx_failInvalid(vcpu);
>>>>> +			return kvm_skip_emulated_instruction(vcpu);
>>>>> +		}
>>>>> +		if (*(u32 *)kmap(page) != VMCS12_REVISION) {
>>>>
>>>> shouldn't we also check if kmap even returned a valid pointer before
>>>> dereferencing it?
>>>
>>> I don't think kmap can fail (page_address can)?
>>
>> Then I wonder why there are some checks:
>>
>> e.g. nested_vmx_merge_msr_bitmap()
>>
>> msr_bitmap_l1 = (unsigned long *)kmap(page);
>> if (!msr_bitmap_l1) {
>> 	// no unmap
>> 	...
>> 	return false;
>>
>> or vmx_complete_nested_posted_interrupt()
>>
>> vapic_page = kmap(vmx->nested.virtual_apic_page);
>> if (!vapic_page) {
>> 	// no unmap
>> 	...
>> 	return -ENOMEM;
>>
>>
>> But there is also no check in handle_vmptrld() for example.
>>
>>
>>>
>>> Paolo
>>
>>
> 
> Think you're right it can't fail.
> 
> So something like that could most likely be done
> 
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index a236dec..a9be221 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -4973,10 +4973,6 @@ static int
> vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
>                         return 0;
> 
>                 vapic_page = kmap(vmx->nested.virtual_apic_page);
> -               if (!vapic_page) {
> -                       WARN_ON(1);
> -                       return -ENOMEM;
> -               }
>                 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
>                 kunmap(vmx->nested.virtual_apic_page);
> 
> @@ -9730,12 +9726,6 @@ static inline bool
> nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
>                 return false;
>         }
>         msr_bitmap_l1 = (unsigned long *)kmap(page);
> -       if (!msr_bitmap_l1) {
> -               nested_release_page_clean(page);
> -               WARN_ON(1);
> -               return false;
> -       }
> -
>         memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
> 
>         if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {

Yes, definitely.  Want to send a patch?

Paolo

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

end of thread, other threads:[~2017-01-25 10:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-24 10:56 [PATCH] kvm: fix page struct leak in handle_vmon Paolo Bonzini
2017-01-25  9:31 ` David Hildenbrand
2017-01-25  9:52   ` Paolo Bonzini
2017-01-25  9:57     ` David Hildenbrand
2017-01-25 10:11       ` David Hildenbrand
2017-01-25 10:35         ` Paolo Bonzini
2017-01-25 10:11 ` David Hildenbrand

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).