Skip to content

Commit ee0314a

Browse files
carlescufijhedberg
authored andcommitted
Bluetooth: host: Replace length check assert with if statement
A few of the length checks that deal with HCI packets coming from the controller were using assert statements. But the recommended practice is to drop invalid packets and continue execution whenever a malformed packet arrives from an external source, so replace those assert statements with branches that will drop the packet and return. Signed-off-by: Carles Cufi <[email protected]>
1 parent 8db7be7 commit ee0314a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

subsys/bluetooth/host/hci_core.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,11 @@ static void hci_acl(struct net_buf *buf)
508508
uint8_t flags;
509509

510510
LOG_DBG("buf %p", buf);
511-
512-
BT_ASSERT(buf->len >= sizeof(*hdr));
511+
if (buf->len < sizeof(*hdr)) {
512+
LOG_ERR("Invalid HCI ACL packet size (%u)", buf->len);
513+
net_buf_unref(buf);
514+
return;
515+
}
513516

514517
hdr = net_buf_pull_mem(buf, sizeof(*hdr));
515518
len = sys_le16_to_cpu(hdr->len);
@@ -2650,7 +2653,11 @@ static void hci_event(struct net_buf *buf)
26502653
{
26512654
struct bt_hci_evt_hdr *hdr;
26522655

2653-
BT_ASSERT(buf->len >= sizeof(*hdr));
2656+
if (buf->len < sizeof(*hdr)) {
2657+
LOG_ERR("Invalid HCI event size (%u)", buf->len);
2658+
net_buf_unref(buf);
2659+
return;
2660+
}
26542661

26552662
hdr = net_buf_pull_mem(buf, sizeof(*hdr));
26562663
LOG_DBG("event 0x%02x", hdr->evt);
@@ -3714,7 +3721,11 @@ void hci_event_prio(struct net_buf *buf)
37143721

37153722
net_buf_simple_save(&buf->b, &state);
37163723

3717-
BT_ASSERT(buf->len >= sizeof(*hdr));
3724+
if (buf->len < sizeof(*hdr)) {
3725+
LOG_ERR("Invalid HCI event size (%u)", buf->len);
3726+
net_buf_unref(buf);
3727+
return;
3728+
}
37183729

37193730
hdr = net_buf_pull_mem(buf, sizeof(*hdr));
37203731
evt_flags = bt_hci_evt_get_flags(hdr->evt);

subsys/bluetooth/host/iso.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ void hci_iso(struct net_buf *buf)
105105

106106
BT_ISO_DATA_DBG("buf %p", buf);
107107

108-
BT_ASSERT(buf->len >= sizeof(*hdr));
108+
if (buf->len < sizeof(*hdr)) {
109+
LOG_ERR("Invalid HCI ISO packet size (%u)", buf->len);
110+
net_buf_unref(buf);
111+
return;
112+
}
109113

110114
hdr = net_buf_pull_mem(buf, sizeof(*hdr));
111115
len = bt_iso_hdr_len(sys_le16_to_cpu(hdr->len));

0 commit comments

Comments
 (0)