Skip to content

Commit 2fbfd50

Browse files
olivier-le-sagecvinayak
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]> (cherry picked from commit 88b468e) Signed-off-by: Vinayak Kariappa Chettimada <[email protected]>
1 parent c81a10b commit 2fbfd50

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
@@ -36,6 +36,14 @@
3636
#include "zephyr/logging/log.h"
3737
LOG_MODULE_REGISTER(bt_sdc_hci_driver);
3838

39+
40+
#if defined(CONFIG_BT_BUF_EVT_DISCARDABLE_COUNT)
41+
#define HCI_RX_BUF_SIZE MAX(BT_BUF_RX_SIZE, \
42+
BT_BUF_EVT_SIZE(CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE))
43+
#else
44+
#define HCI_RX_BUF_SIZE BT_BUF_RX_SIZE
45+
#endif
46+
3947
#if defined(CONFIG_BT_CONN) && defined(CONFIG_BT_CENTRAL)
4048

4149
#if CONFIG_BT_MAX_CONN > 1
@@ -390,6 +398,14 @@ static void data_packet_process(uint8_t *hci_buf)
390398
pb = bt_acl_flags_pb(flags);
391399
bc = bt_acl_flags_bc(flags);
392400

401+
if (len + sizeof(*hdr) > HCI_RX_BUF_SIZE) {
402+
LOG_ERR("Event buffer too small. %u > %u",
403+
len + sizeof(*hdr),
404+
HCI_RX_BUF_SIZE);
405+
k_panic();
406+
return;
407+
}
408+
393409
LOG_DBG("Data: handle (0x%02x), PB(%01d), BC(%01d), len(%u)", handle,
394410
pb, bc, len);
395411

@@ -404,6 +420,14 @@ static void iso_data_packet_process(uint8_t *hci_buf)
404420

405421
uint16_t len = sys_le16_to_cpu(hdr->len);
406422

423+
if (len + sizeof(*hdr) > HCI_RX_BUF_SIZE) {
424+
LOG_ERR("Event buffer too small. %u > %u",
425+
len + sizeof(*hdr),
426+
HCI_RX_BUF_SIZE);
427+
k_panic();
428+
return;
429+
}
430+
407431
net_buf_add_mem(data_buf, &hci_buf[0], len + sizeof(*hdr));
408432

409433
bt_recv(data_buf);
@@ -457,6 +481,14 @@ static void event_packet_process(uint8_t *hci_buf)
457481
struct bt_hci_evt_hdr *hdr = (void *)hci_buf;
458482
struct net_buf *evt_buf;
459483

484+
if (hdr->len + sizeof(*hdr) > HCI_RX_BUF_SIZE) {
485+
LOG_ERR("Event buffer too small. %u > %u",
486+
hdr->len + sizeof(*hdr),
487+
HCI_RX_BUF_SIZE);
488+
k_panic();
489+
return;
490+
}
491+
460492
if (hdr->evt == BT_HCI_EVT_LE_META_EVENT) {
461493
struct bt_hci_evt_le_meta_event *me = (void *)&hci_buf[2];
462494

@@ -530,12 +562,7 @@ static bool fetch_and_process_hci_msg(uint8_t *p_hci_buffer)
530562

531563
void hci_driver_receive_process(void)
532564
{
533-
#if defined(CONFIG_BT_BUF_EVT_DISCARDABLE_COUNT)
534-
static uint8_t hci_buf[MAX(BT_BUF_RX_SIZE,
535-
BT_BUF_EVT_SIZE(CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE))];
536-
#else
537-
static uint8_t hci_buf[BT_BUF_RX_SIZE];
538-
#endif
565+
static uint8_t hci_buf[HCI_RX_BUF_SIZE];
539566

540567
if (fetch_and_process_hci_msg(&hci_buf[0])) {
541568
/* Let other threads of same priority run in between. */

0 commit comments

Comments
 (0)