* [Patch net] bluetooth: validate HCI_EVENT_PKT packet carefully
@ 2019-03-18 20:45 Cong Wang
2019-03-19 1:32 ` Cong Wang
0 siblings, 1 reply; 2+ messages in thread
From: Cong Wang @ 2019-03-18 20:45 UTC (permalink / raw)
To: netdev
Cc: Cong Wang, syzbot+cec7a50c412a2c03f8f5,
syzbot+660883c56e2fa65d4497, Marcel Holtmann, Johan Hedberg
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+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>
Signed-off-by: Cong Wang <xiyou.wangcong@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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Patch net] bluetooth: validate HCI_EVENT_PKT packet carefully
2019-03-18 20:45 [Patch net] bluetooth: validate HCI_EVENT_PKT packet carefully Cong Wang
@ 2019-03-19 1:32 ` Cong Wang
0 siblings, 0 replies; 2+ messages in thread
From: Cong Wang @ 2019-03-19 1:32 UTC (permalink / raw)
To: Linux Kernel Network Developers
Cc: syzbot, syzbot, Marcel Holtmann, Johan Hedberg
On Mon, Mar 18, 2019 at 1:45 PM Cong Wang <xiyou.wangcong@gmail.com> wrote:
> @@ -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;
Here it should be 1 + num_rsp * sizeof(...).
I will send v2 after getting other feedbacks.
Thanks!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-03-19 1:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-18 20:45 [Patch net] bluetooth: validate HCI_EVENT_PKT packet carefully Cong Wang
2019-03-19 1:32 ` Cong Wang
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.