Re: Re: [PATCH] VMCI: Fix NULL pointer dereference on context ptr

From: Greg Kroah-Hartman
Date: Mon Mar 23 2020 - 02:55:11 EST


On Mon, Mar 23, 2020 at 12:53:02PM +0800, Xiyu Yang wrote:
> Hi Greg,
>
> On Wed, Mar 18, 2020 at 12:02:04PM +0100, Greg Kroah-Hartman wrote:
> > On Tue, Mar 17, 2020 at 02:29:57PM +0800, Xiyu Yang wrote:
> > > The refcount wrapper function vmci_ctx_get() may return NULL
> > > context ptr. Thus, we need to add a NULL pointer check
> > > before its dereference.
> > >
> > > Signed-off-by: Xiyu Yang <xiyuyang19@xxxxxxxxxxxx>
> > > Signed-off-by: Xin Tan <tanxin.ctf@xxxxxxxxx>
> > > ---
> > > drivers/misc/vmw_vmci/vmci_context.c | 2 ++
> > > drivers/misc/vmw_vmci/vmci_queue_pair.c | 17 +++++++++++------
> > > 2 files changed, 13 insertions(+), 6 deletions(-)
> > >
> > > diff --git a/drivers/misc/vmw_vmci/vmci_context.c b/drivers/misc/vmw_vmci/vmci_context.c
> > > index 16695366ec92..a20878fba374 100644
> > > --- a/drivers/misc/vmw_vmci/vmci_context.c
> > > +++ b/drivers/misc/vmw_vmci/vmci_context.c
> > > @@ -898,6 +898,8 @@ void vmci_ctx_rcv_notifications_release(u32 context_id,
> > > bool success)
> > > {
> > > struct vmci_ctx *context = vmci_ctx_get(context_id);
> > > + if (context == NULL)
> > > + return;
> >
> > Same comment as on your other patch.
> >
> > And is this a v2?
>
> Thanks! Yes, this is a v2.
>
> According to our observation, vmci_ctx_rcv_notifications_release()
> currently is only called by vmci_host_do_recv_notifications() which
> guarantees a valid context object can be acquired with this context_id.
>
> However, we argue that a NULL-check here is still necessary because
> this function may be called by other functions in the future who may
> fail/forget to provide such guarantee.

No, that's not how we write code in the kernel, if it does not need to
be checked for because this can not happen, then do not check for it.

Don't try to plan for random users of your code in the future when you
control those users directly :)

thanks,

greg k-h