Skip to contentSkip to conversation

Bluetooth: Fix slab-out-of-bounds read in hci_le_direct_adv_report_evt()

PATCH Superseded Peilin Ye 2 participants 4 messages Aug 5, 2020 – Nov 11, 2020

Subsystems (4)
lore
Share & tools
lore

Peilin Ye

author
#0 lore raw

PATCH 1/1 Bluetooth: Fix slab-out-of-bounds read in hci_le_direct_adv_report_evt()

num_reports is not being properly checked. A malformed event packet with a large num_reports number makes hci_le_direct_adv_report_evt() read out of bounds. Fix it.

Reported-and-tested-by [email protected]
Fixes 2f010b55884e ("Bluetooth: Add support for handling LE Direct Advertising Report events")

---
I moved the initialization of ev out of the loop and restructured the function a bit, since otherwise the check would look like:

	if (!num_reports || skb->len < num_reports * sizeof(struct hci_ev_le_direct_adv_info) + 1)
		return;

Therefore I used the similar structure with hci_inquiry_result_evt() etc.

hci_le_adv_report_evt() and hci_le_ext_adv_report_evt() also have the same issue with num_reports, but I'm not sure how to perform the check for them, since they use variable-length reports. Should we do something like this? (take hci_le_adv_report_evt() as example:)

	if (!num_reports ||
	    skb->len < num_reports * (sizeof(*ev) + HCI_MAX_AD_LENGTH + 1) + 1)
		return;

Then how about hci_le_ext_adv_report_evt()? There is no such HCI_MAX_AD_LENGTH restrictions on ev->length for it, I assume?

Would like to hear your opinion. Thank you!

 net/bluetooth/hci_event.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/bluetooth/hci_event.c +5 -7
index 4b7fc430793c..aec43ae488d1 100644
@@ -5863,21 +5863,19 @@ static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
 					 struct sk_buff *skb)
 {
 	u8 num_reports = skb->data[0];
-	void *ptr = &skb->data[1];
+	struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
 
-	hci_dev_lock(hdev);
+	if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
+		return;
 
-	while (num_reports--) {
-		struct hci_ev_le_direct_adv_info *ev = ptr;
+	hci_dev_lock(hdev);
 
+	for (; num_reports; num_reports--, ev++)
 		process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
 				   ev->bdaddr_type, &ev->direct_addr,
 				   ev->direct_addr_type, ev->rssi, NULL, 0,
 				   false);
 
-		ptr += sizeof(*ev);
-	}
-
 	hci_dev_unlock(hdev);
 }
 
Signature · 6 lines
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
author
#1 lore raw

PATCH v2 1/1 Bluetooth: Fix slab-out-of-bounds read in hci_le_direct_adv_report_evt()

num_reports is not being properly checked. A malformed event packet with a large num_reports number makes hci_le_direct_adv_report_evt() read out of bounds. Fix it.

Fixes 2f010b55884e ("Bluetooth: Add support for handling LE Direct Advertising Report events")
Reported-and-tested-by [email protected]

---
Change in v2:
- add "Cc: stable@" tag.

 net/bluetooth/hci_event.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/bluetooth/hci_event.c +5 -7
index 4b7fc430793c..aec43ae488d1 100644
@@ -5863,21 +5863,19 @@ static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
 					 struct sk_buff *skb)
 {
 	u8 num_reports = skb->data[0];
-	void *ptr = &skb->data[1];
+	struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
 
-	hci_dev_lock(hdev);
+	if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
+		return;
 
-	while (num_reports--) {
-		struct hci_ev_le_direct_adv_info *ev = ptr;
+	hci_dev_lock(hdev);
 
+	for (; num_reports; num_reports--, ev++)
 		process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
 				   ev->bdaddr_type, &ev->direct_addr,
 				   ev->direct_addr_type, ev->rssi, NULL, 0,
 				   false);
 
-		ptr += sizeof(*ev);
-	}
-
 	hci_dev_unlock(hdev);
 }
 
Signature · 6 lines
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
maintainer
#2 lore raw

Hi Peilin,

Quoted 36 lines

num_reports is not being properly checked. A malformed event packet with a large num_reports number makes hci_le_direct_adv_report_evt() read out of bounds. Fix it.

Fixes 2f010b55884e ("Bluetooth: Add support for handling LE Direct Advertising Report events")
Reported-and-tested-by [email protected]

---
Change in v2:
- add "Cc: stable@" tag.

net/bluetooth/hci_event.c | 12 +++++-------

1 file changed, 5 insertions(+), 7 deletions(-)
net/bluetooth/hci_event.c +5 -4
index 4b7fc430793c..aec43ae488d1 100644
@@ -5863,21 +5863,19 @@ static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
 					 struct sk_buff *skb)
 {
 	u8 num_reports = skb->data[0];
-	void *ptr = &skb->data[1];
+	struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
 
-	hci_dev_lock(hdev);
+	if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
+		return;
 
-	while (num_reports--) {
-		struct hci_ev_le_direct_adv_info *ev = ptr;
+	hci_dev_lock(hdev);
 
+	for (; num_reports; num_reports--, ev++)

net/bluetooth/hci_event.c static void hci_le_direct_adv_report_evt(struct hci_dev *hdev, · line 5880

Quoted 10 lines
net/bluetooth/hci_event.c +0 -3
 		process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
 				   ev->bdaddr_type, &ev->direct_addr,
 				   ev->direct_addr_type, ev->rssi, NULL, 0,
 				   false);
 
-		ptr += sizeof(*ev);
-	}
-
 	hci_dev_unlock(hdev);
 }

patch has been applied to bluetooth-next tree.

Regards

Marcel

author
#3 lore raw
Quoted 43 lines · Marcel Holtmann

Hi Peilin,

Quoted 41 lines

num_reports is not being properly checked. A malformed event packet with a large num_reports number makes hci_le_direct_adv_report_evt() read out of bounds. Fix it.

Fixes 2f010b55884e ("Bluetooth: Add support for handling LE Direct Advertising Report events")
Reported-and-tested-by [email protected]

---
Change in v2:
- add "Cc: stable@" tag.

net/bluetooth/hci_event.c | 12 +++++-------

1 file changed, 5 insertions(+), 7 deletions(-)
net/bluetooth/hci_event.c +5 -4
index 4b7fc430793c..aec43ae488d1 100644
@@ -5863,21 +5863,19 @@ static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
 					 struct sk_buff *skb)
 {
 	u8 num_reports = skb->data[0];
-	void *ptr = &skb->data[1];
+	struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
 
-	hci_dev_lock(hdev);
+	if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
+		return;
 
-	while (num_reports--) {
-		struct hci_ev_le_direct_adv_info *ev = ptr;
+	hci_dev_lock(hdev);
 
+	for (; num_reports; num_reports--, ev++)
 		process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
 				   ev->bdaddr_type, &ev->direct_addr,
 				   ev->direct_addr_type, ev->rssi, NULL, 0,
 				   false);
 
Quoted 7 lines

net/bluetooth/hci_event.c static void hci_le_direct_adv_report_evt(struct hci_dev *hdev, · line 5880

Quoted 5 lines
net/bluetooth/hci_event.c +0 -3
-		ptr += sizeof(*ev);
-	}
-
 	hci_dev_unlock(hdev);
 }

patch has been applied to bluetooth-next tree.

Thank you for reviewing it,

Peilin Ye

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees