mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Chris Lu (陸稚泓)" <Chris.Lu@mediatek.com>
To: "pmenzel@molgen.mpg.de" <pmenzel@molgen.mpg.de>
Cc: "marcel@holtmann.org" <marcel@holtmann.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-mediatek@lists.infradead.org"
	<linux-mediatek@lists.infradead.org>,
	"linux-bluetooth@vger.kernel.org"
	<linux-bluetooth@vger.kernel.org>,
	"johan.hedberg@gmail.com" <johan.hedberg@gmail.com>,
	"Steve Lee (李視誠)" <steve.lee@mediatek.com>,
	"Sean Wang" <Sean.Wang@mediatek.com>,
	"Aaron Hou (侯俊仰)" <Aaron.Hou@mediatek.com>,
	"luiz.dentz@gmail.com" <luiz.dentz@gmail.com>
Subject: Re: [PATCH v3] Bluetooth: btmtk: Fix null pointer when processing coredump
Date: Wed, 12 Jul 2023 08:53:14 +0000	[thread overview]
Message-ID: <d8f82d97496c73c01521dbbce5455ad23521036c.camel@mediatek.com> (raw)
In-Reply-To: <0cb29d27-a76f-47f2-86c3-f39ba25e8bc2@molgen.mpg.de>

On Wed, 2023-07-12 at 08:11 +0200, Paul Menzel wrote:
>  	 
> External email : Please do not click links or open attachments until
> you have verified the sender or the content.
>  Dear Chris,
> 
Dear Paul,

Thanks for your review and feedback to Mediatek's Bluetooth driver
code.

> 
> Am 12.07.23 um 07:18 schrieb Chris Lu:
> > There may be a potential null pointer risk if offset value is
> > less than 0 when doing memcmp in btmtk_process_coredump().
> > Checking offset is valid before doing memcmp.
> 
> Use imperative mood: Check offset …
> 
> > Signed-off-by: Chris Lu <chris.lu@mediatek.com>
> > Co-developed-by: Sean Wang <sean.wang@mediatek.com>
> > Signed-off-by: Sean Wang <sean.wang@mediatek.com>
> > ---
> > v2: fix typo
> > v3: fix bot checking error
> > ---
> >   drivers/bluetooth/btmtk.c | 16 ++++++++--------
> >   1 file changed, 8 insertions(+), 8 deletions(-)
> > 
> > diff --git a/drivers/bluetooth/btmtk.c b/drivers/bluetooth/btmtk.c
> > index 786f775196ae..0f290430ae0e 100644
> > --- a/drivers/bluetooth/btmtk.c
> > +++ b/drivers/bluetooth/btmtk.c
> > @@ -370,7 +370,7 @@ EXPORT_SYMBOL_GPL(btmtk_register_coredump);
> >   int btmtk_process_coredump(struct hci_dev *hdev, struct sk_buff
> *skb)
> >   {
> >   struct btmediatek_data *data = hci_get_priv(hdev);
> > -int err;
> > +int err, offset;
> >   
> >   if (!IS_ENABLED(CONFIG_DEV_COREDUMP))
> >   return 0;
> > @@ -392,15 +392,15 @@ int btmtk_process_coredump(struct hci_dev
> *hdev, struct sk_buff *skb)
> >   if (err < 0)
> >   break;
> >   data->cd_info.cnt++;
> > +offset = skb->len - sizeof(MTK_COREDUMP_END);
> 
> For `sizeof()` shouldn’t you use `size_t`? But that is unsigned of 
> course. Maybe ssize_t then?
> 
yes, it's better to use ssize_t or size_t, I'll change declaratins of
offset from int to ssize_t.

> >   
> >   /* Mediatek coredump data would be more than MTK_COREDUMP_NUM */
> > -if (data->cd_info.cnt > MTK_COREDUMP_NUM &&
> > -    skb->len > sizeof(MTK_COREDUMP_END) &&
> > -    !memcmp((char *)&skb->data[skb->len -
> sizeof(MTK_COREDUMP_END)],
> > -    MTK_COREDUMP_END, sizeof(MTK_COREDUMP_END) - 1)) {
> > -bt_dev_info(hdev, "Mediatek coredump end");
> > -hci_devcd_complete(hdev);
> > -}
> > +if (data->cd_info.cnt > MTK_COREDUMP_NUM && offset > 0)
> 
> Why not keep it like before, and just add the condition `skb->len < 
> sizeof(MTK_COREDUMP_END)`? The compiler is probably going to optimize
> so 
> the value is not calculated twice.
> 
> 
> Kind regards,
> 
> Paul
> 
> 
The reason why I send this patch is when I backport devcoredump feature
to specific project with older kernel version, the compiler might not
so optimized that it would cause kernel panic when run into memcmp.
As a result, make sure `skb->len > sizeof(MTK_COREDUMP_END) ` before
doing memcmp part can avoid null pointer issue.
Besides, only in condition 'data->cd_info.cnt > MTK_COREDUMP_NUM &&
offset > 0' need to do memcmp to check the end of coredump. Driver do
noting with condition `skb->len < sizeof(MTK_COREDUMP_END) ` that
additional condiction is not really necessary.

> > +if (!memcmp((char *)&skb->data[offset], MTK_COREDUMP_END,
> > +    sizeof(MTK_COREDUMP_END) - 1)) {
> > +bt_dev_info(hdev, "Mediatek coredump end");
> > +hci_devcd_complete(hdev);
> > +}
> >   
> >   break;
> >   }

  reply	other threads:[~2023-07-12  8:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-12  5:18 Chris Lu
2023-07-12  6:11 ` Paul Menzel
2023-07-12  8:53   ` Chris Lu (陸稚泓) [this message]
2023-07-12  9:25     ` Paul Menzel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d8f82d97496c73c01521dbbce5455ad23521036c.camel@mediatek.com \
    --to=chris.lu@mediatek.com \
    --cc=Aaron.Hou@mediatek.com \
    --cc=Sean.Wang@mediatek.com \
    --cc=johan.hedberg@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    --cc=pmenzel@molgen.mpg.de \
    --cc=steve.lee@mediatek.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®