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.

Probably we need to same check for other hci_event_packet() paths
too, this patch only addresses HCI_EVENT_PKT packet as it is the
only case reported so far.

Reported-and-tested-by: syzbot+cec7a50c412a2c03f...@syzkaller.appspotmail.com
Reported-and-tested-by: syzbot+660883c56e2fa65d4...@syzkaller.appspotmail.com
Cc: Marcel Holtmann <mar...@holtmann.org>
Cc: Johan Hedberg <johan.hedb...@gmail.com>
Signed-off-by: Cong Wang <xiyou.wangc...@gmail.com>
---
 net/bluetooth/hci_event.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 609fd6871c5a..c403a76c81f7 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -3964,8 +3964,12 @@ static void hci_inquiry_result_with_rssi_evt(struct 
hci_dev *hdev,
                                             struct sk_buff *skb)
 {
        struct inquiry_data data;
-       int num_rsp = *((__u8 *) skb->data);
+       int num_rsp;
+
+       if (unlikely(!pskb_may_pull(skb, 1)))
+               return;
 
+       num_rsp = *((__u8 *)skb->data);
        BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
 
        if (!num_rsp)
@@ -3978,6 +3982,9 @@ static void hci_inquiry_result_with_rssi_evt(struct 
hci_dev *hdev,
 
        if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
                struct inquiry_info_with_rssi_and_pscan_mode *info;
+
+               if (unlikely(!pskb_may_pull(skb, num_rsp * sizeof(*info))))
+                       goto unlock;
                info = (void *) (skb->data + 1);
 
                for (; num_rsp; num_rsp--, info++) {
@@ -3999,7 +4006,11 @@ static void hci_inquiry_result_with_rssi_evt(struct 
hci_dev *hdev,
                                          flags, NULL, 0, NULL, 0);
                }
        } else {
-               struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
+               struct inquiry_info_with_rssi *info;
+
+               if (unlikely(!pskb_may_pull(skb, num_rsp * sizeof(*info))))
+                       goto unlock;
+               info = (void *)(skb->data + 1);
 
                for (; num_rsp; num_rsp--, info++) {
                        u32 flags;
@@ -4021,6 +4032,7 @@ static void hci_inquiry_result_with_rssi_evt(struct 
hci_dev *hdev,
                }
        }
 
+unlock:
        hci_dev_unlock(hdev);
 }
 
@@ -5742,13 +5754,18 @@ static bool hci_get_cmd_complete(struct hci_dev *hdev, 
u16 opcode,
 
 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
 {
-       struct hci_event_hdr *hdr = (void *) skb->data;
+       struct hci_event_hdr *hdr;
        hci_req_complete_t req_complete = NULL;
        hci_req_complete_skb_t req_complete_skb = NULL;
        struct sk_buff *orig_skb = NULL;
-       u8 status = 0, event = hdr->evt, req_evt = 0;
+       u8 status = 0, event, req_evt = 0;
        u16 opcode = HCI_OP_NOP;
 
+       if (unlikely(!pskb_may_pull(skb,  HCI_EVENT_HDR_SIZE)))
+               goto free;
+       hdr = (void *)skb->data;
+       event = hdr->evt;
+
        if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
                struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
                opcode = __le16_to_cpu(cmd_hdr->opcode);
@@ -5960,6 +5977,7 @@ void hci_event_packet(struct hci_dev *hdev, struct 
sk_buff *skb)
                req_complete_skb(hdev, status, opcode, orig_skb);
        }
 
+free:
        kfree_skb(orig_skb);
        kfree_skb(skb);
        hdev->stat.evt_rx++;
-- 
2.20.1

Reply via email to