linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Cong Wang <xiyou.wangcong@gmail.com>
To: Marcel Holtmann <marcel@holtmann.org>
Cc: Linux Kernel Network Developers <netdev@vger.kernel.org>,
	linux-bluetooth <linux-bluetooth@vger.kernel.org>,
	syzbot <syzbot+cec7a50c412a2c03f8f5@syzkaller.appspotmail.com>,
	syzbot <syzbot+660883c56e2fa65d4497@syzkaller.appspotmail.com>,
	Johan Hedberg <johan.hedberg@gmail.com>,
	Dan Carpenter <dan.carpenter@oracle.com>,
	Tomas Bortoli <tomasbortoli@gmail.com>
Subject: Re: [Patch net v2 1/3] bluetooth: validate HCI_EVENT_PKT packet carefully
Date: Tue, 23 Apr 2019 18:36:39 -0700	[thread overview]
Message-ID: <CAM_iQpVyFCAa8rbCJY-PNgY9XwBRjV4N2WAwtV1d4z=UG0afCg@mail.gmail.com> (raw)
In-Reply-To: <5F6E057E-4897-4F74-9FA4-0CB41222DB5F@holtmann.org>

On Tue, Apr 23, 2019 at 12:42 PM Marcel Holtmann <marcel@holtmann.org> wrote:
>
> Hi Cong,
>
> > hci_event_packet() blindly assumes all packets are sane, at least
> > for packets allocated via vhci_get_user() path this is not true.
> > We have to check if we access skb data out-of-bound with
> > pskb_may_pull() before each skb->data dereference on RX path.
> >
> > Reported-and-tested-by: syzbot+cec7a50c412a2c03f8f5@syzkaller.appspotmail.com
> > Reported-and-tested-by: syzbot+660883c56e2fa65d4497@syzkaller.appspotmail.com
> > Cc: Marcel Holtmann <marcel@holtmann.org>
> > Cc: Johan Hedberg <johan.hedberg@gmail.com>
> > Cc: Dan Carpenter <dan.carpenter@oracle.com>
> > Reviewed-by: Tomas Bortoli <tomasbortoli@gmail.com>
> > Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
> > ---
> > net/bluetooth/hci_event.c | 262 +++++++++++++++++++++++++++++++-------
> > 1 file changed, 218 insertions(+), 44 deletions(-)
> >
> > diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
> > index 609fd6871c5a..2fef70c0bffe 100644
> > --- a/net/bluetooth/hci_event.c
> > +++ b/net/bluetooth/hci_event.c
> > @@ -2331,10 +2331,13 @@ static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
> >
> > static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
> > {
> > -     __u8 status = *((__u8 *) skb->data);
> >       struct discovery_state *discov = &hdev->discovery;
> >       struct inquiry_entry *e;
> > +     __u8 status;
> >
> > +     if (unlikely(!pskb_may_pull(skb, 1)))
> > +             return;
> > +     status = *((__u8 *)skb->data);
> >       BT_DBG("%s status 0x%2.2x", hdev->name, status);
> >
> >       hci_conn_check_pending(hdev);
> > @@ -2391,14 +2394,21 @@ static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
> > static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
> > {
> >       struct inquiry_data data;
> > -     struct inquiry_info *info = (void *) (skb->data + 1);
> > -     int num_rsp = *((__u8 *) skb->data);
> > +     struct inquiry_info *info;
> > +     int num_rsp;
> >
> >       BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
> >
> > +     if (unlikely(!pskb_may_pull(skb, 1)))
> > +             return;
> > +     num_rsp = *((__u8 *)skb->data);
> >       if (!num_rsp)
> >               return;
> >
> > +     if (unlikely(!pskb_may_pull(skb, 1 + num_rsp * sizeof(*info))))
> > +             return;
> > +     info = (void *)(skb->data + 1);
> > +
> >       if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
> >               return;
>
> this really looks like we better create a macro for this. It is repetitive code that can be turned into just a macro usage.

Hmm, I have no idea on how to make a macro for this, any hints?

By the way, we use the similar pattern in networking code, I am not
aware of any macro there.

Thanks.

  reply	other threads:[~2019-04-24  1:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-03 23:08 [Patch net v2 0/3] bluetooth: validate packet boundary carefully Cong Wang
2019-04-03 23:08 ` [Patch net v2 1/3] bluetooth: validate HCI_EVENT_PKT packet carefully Cong Wang
2019-04-23 19:42   ` Marcel Holtmann
2019-04-24  1:36     ` Cong Wang [this message]
2019-04-03 23:08 ` [Patch net v2 2/3] bluetooth: validate HCI_EV_LE_META " Cong Wang
2019-04-04  8:34   ` Dan Carpenter
2019-04-05 17:26     ` Cong Wang
2019-04-05 20:09       ` Dan Carpenter
2019-04-03 23:08 ` [Patch net v2 3/3] bluetooth: validate HCI_EV_CMD_COMPLETE " Cong Wang

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='CAM_iQpVyFCAa8rbCJY-PNgY9XwBRjV4N2WAwtV1d4z=UG0afCg@mail.gmail.com' \
    --to=xiyou.wangcong@gmail.com \
    --cc=dan.carpenter@oracle.com \
    --cc=johan.hedberg@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=marcel@holtmann.org \
    --cc=netdev@vger.kernel.org \
    --cc=syzbot+660883c56e2fa65d4497@syzkaller.appspotmail.com \
    --cc=syzbot+cec7a50c412a2c03f8f5@syzkaller.appspotmail.com \
    --cc=tomasbortoli@gmail.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;
as well as URLs for NNTP newsgroup(s).