From: Joe Moriarty Subject: Re: [PATCH v1 1/1] KVM: X86: NULL pointer dereference [null-pointer-deref] (CWE 476) problem Date: Wed, 14 Feb 2018 11:55:54 -0500 Message-ID: <6e222b28-5c6e-5a57-1eb4-a9d8cc2b7dbe@oracle.com> References: <20180213182640.10646-1-joe.moriarty@oracle.com> <20180213182640.10646-2-joe.moriarty@oracle.com> <3c022ee7-5a91-6c4a-91a3-ce424bb6c6ee@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit To: Paolo Bonzini , rkrcmar@redhat.com, kvm@vger.kernel.org Return-path: Received: from userp2130.oracle.com ([156.151.31.86]:41204 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1032907AbeBNQ4D (ORCPT ); Wed, 14 Feb 2018 11:56:03 -0500 In-Reply-To: <3c022ee7-5a91-6c4a-91a3-ce424bb6c6ee@redhat.com> Content-Language: en-US Sender: kvm-owner@vger.kernel.org List-ID: On 2/14/2018 11:28 AM, Paolo Bonzini wrote: > On 13/02/2018 19:26, Joe Moriarty wrote: >> The Parfait (version 2.1.0) static code analysis tool found the >> following NULL pointer dereference problem. >> >> - arch/x86/kvm/mmu.c >> There is a possibility that the call to __gfn_to_rmap() can happen >> with a NULL pointer given for the slot argument. This can happen >> if the slot information cannot be determined from a previous call >> to __gfn_to_memslot(). The base_gfn will be passed in as 0 to >> gfn_to_index if no valid slot information can be obtained from a >> call to __gfn_to_memslot(). > > How does it justify the possible NULL pointer dereference? > > The fix below is incorrect anyway, since it would just get a random page > out of guest memory; please report the issue without providing a patch > if you do not understand the code well. > > I can't exclude there is a race condition here that can lead to a NULL > pointer dereference. However, it should be handled by passing the > struct kvm_memory_slot all the way down to __gfn_to_rmap, i.e. adding a > new argument to gfn_to_rmap, rmap_add, mmu_set_spte, __direct_map, > try_async_pf and many more functions. That's not as easy as the patch > you posted. :) > > Thanks, > > Paolo > >> Signed-off-by: Joe Moriarty >> --- >> arch/x86/kvm/mmu.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c >> index 8eca1d04aeb8..69d41b5d0948 100644 >> --- a/arch/x86/kvm/mmu.c >> +++ b/arch/x86/kvm/mmu.c >> @@ -1239,7 +1239,7 @@ static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level, >> { >> unsigned long idx; >> >> - idx = gfn_to_index(gfn, slot->base_gfn, level); >> + idx = gfn_to_index(gfn, slot ? slot->base_gfn : 0, level); >> return &slot->arch.rmap[level - PT_PAGE_TABLE_LEVEL][idx]; >> } >> >> > Hi Paolo, Thank you for the review. I wasn't sure if the change I proposed was correct or not. I will take your suggestion of posting to the mailing list instead of as a patch the next time I encounter a situation like this again. In the meantime, I will look at doing your suggestion of passing kvm_memory_slot down to gfn_to_rmap() and the other functions you pointed out above for the next version of the patch. Joe