Skip to content

Commit ccf6d67

Browse files
anhmoltlemrey
authored andcommitted
bluetooth: services: nus: fix unsafe use of conn_handle as array idx
It is not safe to assume that connection handles will start at zero and always stay below the number of max connections. Therefore, connection handle cannot, and should not, be used to index into arrays or lists. Fix this by using the nrf_sdh_ble_idx_get function to return an index that has been assigned to the connection handle by the softdevice handler. Signed-off-by: Andreas Moltumyr <[email protected]>
1 parent 8e19b04 commit ccf6d67

File tree

2 files changed

+3
-10
lines changed

2 files changed

+3
-10
lines changed

subsys/bluetooth/services/Kconfig.nus

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ menuconfig BLE_NUS
99

1010
if BLE_NUS
1111

12-
config BLE_NUS_MAX_CLIENTS
13-
int "Max NUS clients"
14-
range 0 NRF_SDH_BLE_PERIPHERAL_LINK_COUNT
15-
default 1
16-
1712
module=BLE_NUS
1813
module-dep=LOG
1914
module-str=BLE Nordic UART Service

subsys/bluetooth/services/nus.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ LOG_MODULE_REGISTER(ble_nus, CONFIG_BLE_NUS_LOG_LEVEL);
2323
/** The UUID of the RX Characteristic. */
2424
#define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0002
2525

26-
static struct ble_nus_client_context contexts[CONFIG_BLE_NUS_MAX_CLIENTS];
26+
static struct ble_nus_client_context contexts[CONFIG_NRF_SDH_BLE_TOTAL_LINK_COUNT];
2727

2828
static struct ble_nus_client_context *ble_nus_client_context_get(uint16_t conn_handle)
2929
{
30-
if (conn_handle == BLE_CONN_HANDLE_INVALID || conn_handle >= CONFIG_BLE_NUS_MAX_CLIENTS) {
31-
return NULL;
32-
}
30+
const int idx = nrf_sdh_ble_idx_get(conn_handle);
3331

34-
return &contexts[conn_handle];
32+
return ((idx >= 0) ? &contexts[idx] : NULL);
3533
}
3634

3735
static uint32_t nus_rx_char_add(struct ble_nus *nus, struct ble_nus_config const *cfg)

0 commit comments

Comments
 (0)