Skip to content

Commit 4c9479b

Browse files
ivaniushkovrlubos
authored andcommitted
[nrf fromtree] Bluetooth: Fixing UBSAN warning in CTE field parsing in adv.c/scan.c
during local testling, UBSAN reported the following warnings: - bluetooth/host/adv.c:2067:19: runtime error: shift exponent 255 is too large for 32-bit type 'long unsigned int' - bluetooth/host/scan.c:828:18: runtime error: shift exponent 255 is too large for 32-bit type 'long unsigned int' It turned out that we can't use BIT() macro directly on bt_hci_evt_le_per_advertising_report::cte_type field. According to Core Spec, `cte_type = 0xFF` corresponds to `No contstant tone extension`. Added separate function to convert CTE bit field from HCI format to bt_df_cte_type Signed-off-by: Ivan Iushkov <[email protected]> (cherry picked from commit b1e9f86) Signed-off-by: Ivan Iushkov <[email protected]>
1 parent 7e22d68 commit 4c9479b

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

subsys/bluetooth/host/adv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ void bt_hci_le_per_adv_response_report(struct net_buf *buf)
20642064
response = net_buf_pull_mem(buf, sizeof(struct bt_hci_evt_le_per_adv_response));
20652065
info.tx_power = response->tx_power;
20662066
info.rssi = response->rssi;
2067-
info.cte_type = BIT(response->cte_type);
2067+
info.cte_type = bt_get_df_cte_type(response->cte_type);
20682068
info.response_slot = response->response_slot;
20692069

20702070
if (buf->len < response->data_length) {

subsys/bluetooth/host/hci_core.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,22 @@ uint8_t bt_get_phy(uint8_t hci_phy)
422422
}
423423
}
424424

425+
int bt_get_df_cte_type(uint8_t hci_cte_type)
426+
{
427+
switch (hci_cte_type) {
428+
case BT_HCI_LE_AOA_CTE:
429+
return BT_DF_CTE_TYPE_AOA;
430+
case BT_HCI_LE_AOD_CTE_1US:
431+
return BT_DF_CTE_TYPE_AOD_1US;
432+
case BT_HCI_LE_AOD_CTE_2US:
433+
return BT_DF_CTE_TYPE_AOD_2US;
434+
case BT_HCI_LE_NO_CTE:
435+
return BT_DF_CTE_TYPE_NONE;
436+
default:
437+
return BT_DF_CTE_TYPE_NONE;
438+
}
439+
}
440+
425441
#if defined(CONFIG_BT_CONN_TX)
426442
static void hci_num_completed_packets(struct net_buf *buf)
427443
{

subsys/bluetooth/host/hci_core.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,14 @@ int bt_le_set_data_len(struct bt_conn *conn, uint16_t tx_octets, uint16_t tx_tim
434434
int bt_le_set_phy(struct bt_conn *conn, uint8_t all_phys,
435435
uint8_t pref_tx_phy, uint8_t pref_rx_phy, uint8_t phy_opts);
436436
uint8_t bt_get_phy(uint8_t hci_phy);
437-
437+
/**
438+
* @brief Convert CTE type value from HCI format to @ref bt_df_cte_type format.
439+
*
440+
* @param hci_cte_type CTE type in an HCI format.
441+
*
442+
* @return CTE type (@ref bt_df_cte_type).
443+
*/
444+
int bt_get_df_cte_type(uint8_t hci_cte_type);
438445
int bt_le_scan_update(bool fast_scan);
439446

440447
int bt_le_create_conn(const struct bt_conn *conn);

subsys/bluetooth/host/scan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ static void bt_hci_le_per_adv_report_common(struct net_buf *buf)
825825

826826
info.tx_power = evt->tx_power;
827827
info.rssi = evt->rssi;
828-
info.cte_type = BIT(evt->cte_type);
828+
info.cte_type = bt_get_df_cte_type(evt->cte_type);
829829
info.addr = &per_adv_sync->addr;
830830
info.sid = per_adv_sync->sid;
831831

0 commit comments

Comments
 (0)