Skip to content

Commit 7305e10

Browse files
sylvioalveskartben
authored andcommitted
bluetooth: esp32: make LE discardability length-safe
Determine event discardability after parsing the HCI header and require at least one byte before reading the LE subevent. This removes a possible OOB read on malformed/short LE Meta events. Also mark LE Extended Advertising Report as discardable, matching legacy Advertising Report to reduce RX pool pressure during heavy scanning. Signed-off-by: Sylvio Alves <[email protected]>
1 parent 089df60 commit 7305e10

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

drivers/bluetooth/hci/hci_esp32.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,24 @@ struct bt_esp32_data {
2828
/* VHCI notifies when exactly one more HCI packet can be sent */
2929
static K_SEM_DEFINE(hci_send_sem, 0, 1);
3030

31-
static bool is_hci_event_discardable(const uint8_t *evt_data)
31+
static bool is_hci_event_discardable(uint8_t evt_code, const uint8_t *payload, size_t plen)
3232
{
33-
uint8_t evt_type = evt_data[0];
34-
35-
switch (evt_type) {
33+
switch (evt_code) {
3634
#if defined(CONFIG_BT_CLASSIC)
3735
case BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI:
3836
case BT_HCI_EVT_EXTENDED_INQUIRY_RESULT:
3937
return true;
4038
#endif
4139
case BT_HCI_EVT_LE_META_EVENT: {
42-
uint8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)];
40+
/* Need at least 1 byte to read LE subevent safely */
41+
if (plen < 1U) {
42+
return false;
43+
}
44+
uint8_t subevt_type = payload[0];
4345

4446
switch (subevt_type) {
4547
case BT_HCI_EVT_LE_ADVERTISING_REPORT:
48+
case BT_HCI_EVT_LE_EXT_ADVERTISING_REPORT:
4649
return true;
4750
default:
4851
return false;
@@ -65,8 +68,6 @@ static struct net_buf *bt_esp_evt_recv(uint8_t *data, size_t remaining)
6568
return NULL;
6669
}
6770

68-
discardable = is_hci_event_discardable(data, remaining);
69-
7071
memcpy((void *)&hdr, data, sizeof(hdr));
7172
data += sizeof(hdr);
7273
remaining -= sizeof(hdr);
@@ -77,6 +78,8 @@ static struct net_buf *bt_esp_evt_recv(uint8_t *data, size_t remaining)
7778
}
7879
LOG_DBG("len %u", hdr.len);
7980

81+
discardable = is_hci_event_discardable(hdr.evt, data, remaining);
82+
8083
buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT);
8184
if (!buf) {
8285
if (discardable) {

0 commit comments

Comments
 (0)