diff mbox

[2/6] arm/arm64: KVM: fix potential NULL dereference in user_mem_abort()

Message ID 1410990981-665-3-git-send-email-ard.biesheuvel@linaro.org
State New
Headers show

Commit Message

Ard Biesheuvel Sept. 17, 2014, 9:56 p.m. UTC
Handle the potential NULL return value of find_vma_intersection()
before dereferencing it.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 arch/arm/kvm/mmu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Christoffer Dall Sept. 29, 2014, 1:01 p.m. UTC | #1
On Wed, Sep 17, 2014 at 02:56:17PM -0700, Ard Biesheuvel wrote:
> Handle the potential NULL return value of find_vma_intersection()
> before dereferencing it.
> 
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
>  arch/arm/kvm/mmu.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
> index 152e0f896e63..c093e95ff7ef 100644
> --- a/arch/arm/kvm/mmu.c
> +++ b/arch/arm/kvm/mmu.c
> @@ -776,7 +776,11 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
>  	/* Let's check if we will get back a huge page backed by hugetlbfs */
>  	down_read(&current->mm->mmap_sem);
>  	vma = find_vma_intersection(current->mm, hva, hva + 1);
> -	if (is_vm_hugetlb_page(vma)) {
> +	if (unlikely(!vma)) {
> +		kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
> +		up_read(&current->mm->mmap_sem);
> +		return -EFAULT;
> +	} else if (is_vm_hugetlb_page(vma)) {
>  		hugetlb = true;
>  		gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
>  	} else {
> -- 
> 1.8.3.2
> 
Nice catch!

(This would only happen if the guest unmaps an mmap region after
registering the memslot I believe, but still, we should absolutely merge
this.).

Thanks,
-Christoffer
Marc Zyngier Oct. 9, 2014, 1:05 p.m. UTC | #2
On 17/09/14 22:56, Ard Biesheuvel wrote:
> Handle the potential NULL return value of find_vma_intersection()
> before dereferencing it.
> 
> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> ---
>  arch/arm/kvm/mmu.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
> index 152e0f896e63..c093e95ff7ef 100644
> --- a/arch/arm/kvm/mmu.c
> +++ b/arch/arm/kvm/mmu.c
> @@ -776,7 +776,11 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
>  	/* Let's check if we will get back a huge page backed by hugetlbfs */
>  	down_read(&current->mm->mmap_sem);
>  	vma = find_vma_intersection(current->mm, hva, hva + 1);
> -	if (is_vm_hugetlb_page(vma)) {
> +	if (unlikely(!vma)) {
> +		kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
> +		up_read(&current->mm->mmap_sem);
> +		return -EFAULT;
> +	} else if (is_vm_hugetlb_page(vma)) {

I'm not overly fond of this "else if" construct. The !vma case is final,
so what's after cannot be reached.

>  		hugetlb = true;
>  		gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
>  	} else {
> 

Despite the above nit:

Acked-by: Marc Zyngier <marc.zyngier@arm.com>

	M.
Ard Biesheuvel Oct. 9, 2014, 1:10 p.m. UTC | #3
On 9 October 2014 15:05, Marc Zyngier <marc.zyngier@arm.com> wrote:
> On 17/09/14 22:56, Ard Biesheuvel wrote:
>> Handle the potential NULL return value of find_vma_intersection()
>> before dereferencing it.
>>
>> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
>> ---
>>  arch/arm/kvm/mmu.c | 6 +++++-
>>  1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
>> index 152e0f896e63..c093e95ff7ef 100644
>> --- a/arch/arm/kvm/mmu.c
>> +++ b/arch/arm/kvm/mmu.c
>> @@ -776,7 +776,11 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
>>       /* Let's check if we will get back a huge page backed by hugetlbfs */
>>       down_read(&current->mm->mmap_sem);
>>       vma = find_vma_intersection(current->mm, hva, hva + 1);
>> -     if (is_vm_hugetlb_page(vma)) {
>> +     if (unlikely(!vma)) {
>> +             kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
>> +             up_read(&current->mm->mmap_sem);
>> +             return -EFAULT;
>> +     } else if (is_vm_hugetlb_page(vma)) {
>
> I'm not overly fond of this "else if" construct. The !vma case is final,
> so what's after cannot be reached.
>

I don't have a strong preference either way. I can respin but perhaps
Christoffer can fix it up when applying?

>>               hugetlb = true;
>>               gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
>>       } else {
>>
>
> Despite the above nit:
>
> Acked-by: Marc Zyngier <marc.zyngier@arm.com>
>

Thanks.
Christoffer Dall Oct. 9, 2014, 1:11 p.m. UTC | #4
On Thu, Oct 09, 2014 at 03:10:08PM +0200, Ard Biesheuvel wrote:
> On 9 October 2014 15:05, Marc Zyngier <marc.zyngier@arm.com> wrote:
> > On 17/09/14 22:56, Ard Biesheuvel wrote:
> >> Handle the potential NULL return value of find_vma_intersection()
> >> before dereferencing it.
> >>
> >> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
> >> ---
> >>  arch/arm/kvm/mmu.c | 6 +++++-
> >>  1 file changed, 5 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
> >> index 152e0f896e63..c093e95ff7ef 100644
> >> --- a/arch/arm/kvm/mmu.c
> >> +++ b/arch/arm/kvm/mmu.c
> >> @@ -776,7 +776,11 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
> >>       /* Let's check if we will get back a huge page backed by hugetlbfs */
> >>       down_read(&current->mm->mmap_sem);
> >>       vma = find_vma_intersection(current->mm, hva, hva + 1);
> >> -     if (is_vm_hugetlb_page(vma)) {
> >> +     if (unlikely(!vma)) {
> >> +             kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
> >> +             up_read(&current->mm->mmap_sem);
> >> +             return -EFAULT;
> >> +     } else if (is_vm_hugetlb_page(vma)) {
> >
> > I'm not overly fond of this "else if" construct. The !vma case is final,
> > so what's after cannot be reached.
> >
> 
> I don't have a strong preference either way. I can respin but perhaps
> Christoffer can fix it up when applying?
> 
Sure!  Don't respin these just for that.

-Christoffer
Marc Zyngier Oct. 9, 2014, 1:13 p.m. UTC | #5
On 09/10/14 14:11, Christoffer Dall wrote:
> On Thu, Oct 09, 2014 at 03:10:08PM +0200, Ard Biesheuvel wrote:
>> On 9 October 2014 15:05, Marc Zyngier <marc.zyngier@arm.com> wrote:
>>> On 17/09/14 22:56, Ard Biesheuvel wrote:
>>>> Handle the potential NULL return value of find_vma_intersection()
>>>> before dereferencing it.
>>>>
>>>> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
>>>> ---
>>>>  arch/arm/kvm/mmu.c | 6 +++++-
>>>>  1 file changed, 5 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
>>>> index 152e0f896e63..c093e95ff7ef 100644
>>>> --- a/arch/arm/kvm/mmu.c
>>>> +++ b/arch/arm/kvm/mmu.c
>>>> @@ -776,7 +776,11 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
>>>>       /* Let's check if we will get back a huge page backed by hugetlbfs */
>>>>       down_read(&current->mm->mmap_sem);
>>>>       vma = find_vma_intersection(current->mm, hva, hva + 1);
>>>> -     if (is_vm_hugetlb_page(vma)) {
>>>> +     if (unlikely(!vma)) {
>>>> +             kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
>>>> +             up_read(&current->mm->mmap_sem);
>>>> +             return -EFAULT;
>>>> +     } else if (is_vm_hugetlb_page(vma)) {
>>>
>>> I'm not overly fond of this "else if" construct. The !vma case is final,
>>> so what's after cannot be reached.
>>>
>>
>> I don't have a strong preference either way. I can respin but perhaps
>> Christoffer can fix it up when applying?
>>
> Sure!  Don't respin these just for that.

In which case, please add my

Acked-by: Marc Zyngier <marc.zyngier@arm.com>

when you do the fixup.

Thanks,

	M.
diff mbox

Patch

diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
index 152e0f896e63..c093e95ff7ef 100644
--- a/arch/arm/kvm/mmu.c
+++ b/arch/arm/kvm/mmu.c
@@ -776,7 +776,11 @@  static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	/* Let's check if we will get back a huge page backed by hugetlbfs */
 	down_read(&current->mm->mmap_sem);
 	vma = find_vma_intersection(current->mm, hva, hva + 1);
-	if (is_vm_hugetlb_page(vma)) {
+	if (unlikely(!vma)) {
+		kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
+		up_read(&current->mm->mmap_sem);
+		return -EFAULT;
+	} else if (is_vm_hugetlb_page(vma)) {
 		hugetlb = true;
 		gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
 	} else {