Skip to content

Commit 88b468e

Browse files
olivier-le-sagenordicjm
authored andcommitted
bluetooth: controller: add k_panic() if the hci packet is too big
Log an error and fail if an HCI packet was too big for the static HCI buffer. If for instance CONFIG_BT_BUF_RX_SIZE is too small, you get buffer overruns. Hopefully this can save some time for poor saps like me who spend all day trying to figure out why their code seems to produce random bus faults and other memory corruption symptoms Signed-off-by: Olivier Lesage <[email protected]>
1 parent cf2cc23 commit 88b468e

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

subsys/bluetooth/controller/hci_driver.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838
#include "zephyr/logging/log.h"
3939
LOG_MODULE_REGISTER(bt_sdc_hci_driver);
4040

41+
42+
#if defined(CONFIG_BT_BUF_EVT_DISCARDABLE_COUNT)
43+
#define HCI_RX_BUF_SIZE MAX(BT_BUF_RX_SIZE, \
44+
BT_BUF_EVT_SIZE(CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE))
45+
#else
46+
#define HCI_RX_BUF_SIZE BT_BUF_RX_SIZE
47+
#endif
48+
4149
#if defined(CONFIG_BT_CONN) && defined(CONFIG_BT_CENTRAL)
4250

4351
#if CONFIG_BT_MAX_CONN > 1
@@ -431,6 +439,14 @@ static void data_packet_process(const struct device *dev, uint8_t *hci_buf)
431439
pb = bt_acl_flags_pb(flags);
432440
bc = bt_acl_flags_bc(flags);
433441

442+
if (len + sizeof(*hdr) > HCI_RX_BUF_SIZE) {
443+
LOG_ERR("Event buffer too small. %u > %u",
444+
len + sizeof(*hdr),
445+
HCI_RX_BUF_SIZE);
446+
k_panic();
447+
return;
448+
}
449+
434450
LOG_DBG("Data: handle (0x%02x), PB(%01d), BC(%01d), len(%u)", handle,
435451
pb, bc, len);
436452

@@ -448,6 +464,14 @@ static void iso_data_packet_process(const struct device *dev, uint8_t *hci_buf)
448464

449465
uint16_t len = sys_le16_to_cpu(hdr->len);
450466

467+
if (len + sizeof(*hdr) > HCI_RX_BUF_SIZE) {
468+
LOG_ERR("Event buffer too small. %u > %u",
469+
len + sizeof(*hdr),
470+
HCI_RX_BUF_SIZE);
471+
k_panic();
472+
return;
473+
}
474+
451475
net_buf_add_mem(data_buf, &hci_buf[0], len + sizeof(*hdr));
452476

453477
struct hci_driver_data *driver_data = dev->data;
@@ -503,6 +527,14 @@ static void event_packet_process(const struct device *dev, uint8_t *hci_buf)
503527
struct bt_hci_evt_hdr *hdr = (void *)hci_buf;
504528
struct net_buf *evt_buf;
505529

530+
if (hdr->len + sizeof(*hdr) > HCI_RX_BUF_SIZE) {
531+
LOG_ERR("Event buffer too small. %u > %u",
532+
hdr->len + sizeof(*hdr),
533+
HCI_RX_BUF_SIZE);
534+
k_panic();
535+
return;
536+
}
537+
506538
if (hdr->evt == BT_HCI_EVT_LE_META_EVENT) {
507539
struct bt_hci_evt_le_meta_event *me = (void *)&hci_buf[2];
508540

@@ -579,12 +611,7 @@ static bool fetch_and_process_hci_msg(const struct device *dev, uint8_t *p_hci_b
579611

580612
void hci_driver_receive_process(void)
581613
{
582-
#if defined(CONFIG_BT_BUF_EVT_DISCARDABLE_COUNT)
583-
static uint8_t hci_buf[MAX(BT_BUF_RX_SIZE,
584-
BT_BUF_EVT_SIZE(CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE))];
585-
#else
586-
static uint8_t hci_buf[BT_BUF_RX_SIZE];
587-
#endif
614+
static uint8_t hci_buf[HCI_RX_BUF_SIZE];
588615

589616
const struct device *dev = DEVICE_DT_GET(DT_DRV_INST(0));
590617

0 commit comments

Comments
 (0)