Subject: Re: [Linux-kernel-mentees] [PATCH] net: bluetooth: Fix null pointer dereference in hci_event_packet()

From: Dan Carpenter <[email-masked]>
Date: Mon, 14 Sep 2020 18:44:05 +0300

On Sat, Sep 12, 2020 at 02:40:28PM +0530, Anmol Karn wrote:
> On Thu, Sep 10, 2020 at 01:49:18PM +0300, Dan Carpenter wrote:
> > On Thu, Sep 10, 2020 at 10:04:24AM +0530, Anmol Karn wrote:
> > > Prevent hci_phy_link_complete_evt() from dereferencing 'hcon->amp_mgr'
> > > as NULL. Fix it by adding pointer check for it.
> > > 
> > > Reported-and-tested-by: [email-masked]
> > > Link: https://syzkaller.appspot.com/bug?extid=0bef568258653cff272f 
> > > Signed-off-by: Anmol Karn <[email-masked]>
> > > ---
> > >  net/bluetooth/hci_event.c | 5 +++++
> > >  1 file changed, 5 insertions(+)
> > > 
> > > diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
> > > index 4b7fc430793c..871e16804433 100644
> > > --- a/net/bluetooth/hci_event.c
> > > +++ b/net/bluetooth/hci_event.c
> > > @@ -4936,6 +4936,11 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
> > >  		return;
> > >  	}
> > >  
> > > +	if (IS_ERR_OR_NULL(hcon->amp_mgr)) {
> > 
> > It can't be an error pointer.  Shouldn't we call hci_conn_del() on this
> > path?  Try to find the Fixes tag to explain how this bug was introduced.
> > 
> > (Don't rush to send a v2.  The patch requires quite a bit more digging
> > and detective work before it is ready).
> > 
> > > +		hci_dev_unlock(hdev);
> > > +		return;
> > > +	}
> > > +
> > >  	if (ev->status) {
> > >  		hci_conn_del(hcon);
> > >  		hci_dev_unlock(hdev);
> > 
> > regards,
> > dan carpenter
> > 
> 
> Sir,
> 
> I need little advice in continuing with this Patch,
> 
> I have looked into the Bisected logs and the problem occurs from this commit:
> 
> 941992d29447 ("ethernet: amd: use IS_ENABLED() instead of checking for built-in or module")
> 

That's just the patch which made the code testable by syzbot.  It didn't
introduce the bug.

> 
> Here is a diff of patch which i modified from last patch,
> 
> diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
> index 4b7fc430793c..6ce435064e0b 100644
> --- a/net/bluetooth/hci_event.c
> +++ b/net/bluetooth/hci_event.c
> @@ -4936,6 +4936,12 @@ static void hci_phy_link_complete_evt(struct hci_dev *hdev,
>                 return;
>         }
> 
> +       if (!hcon->amp_mgr) {
> +               hci_conn_del(hcon);
> +               hci_dev_unlock(hdev);

I have no idea if calling hci_conn_del() is really the correct, thing.
I don't know the code at all.  Anyway, do some research and figure out
for sure what the correct thing is.

Also look for similar bugs in other places where hcon->amp_mgr is
dereferenced.  For example, amp_read_loc_assoc_final_data() seems to
have a similar bug.

regards,
dan carpenter