Skip to content

Commit 1ff031a

Browse files
committed
host/ble_gap.c : Check allowed random address
Add a check to validate whether the random address being set is allowed as per spec
1 parent 14a0f78 commit 1ff031a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

nimble/host/include/host/ble_gap.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,18 @@ struct hci_conn_update;
295295

296296
/** @} */
297297

298+
/**
299+
* @defgroup Mask for checking random address validity
300+
* @{
301+
*/
302+
/** Static random address check mask. */
303+
#define BLE_STATIC_RAND_ADDR_MASK 0xC0
304+
305+
/** Non RPA check mask. */
306+
#define BLE_NON_RPA_MASK 0x3F
307+
308+
/** @} */
309+
298310
/** Connection security state */
299311
struct ble_gap_sec_state {
300312
/** If connection is encrypted */

nimble/host/src/ble_gap.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,6 +3430,7 @@ int
34303430
ble_gap_ext_adv_set_addr(uint8_t instance, const ble_addr_t *addr)
34313431
{
34323432
int rc;
3433+
ble_addr_t invalid_non_rpa_addr, invalid_static_rand_addr;
34333434

34343435
if (instance >= BLE_ADV_INSTANCES || addr->type != BLE_ADDR_RANDOM) {
34353436
return BLE_HS_EINVAL;
@@ -3439,6 +3440,35 @@ ble_gap_ext_adv_set_addr(uint8_t instance, const ble_addr_t *addr)
34393440
return BLE_HS_EDISABLED;
34403441
}
34413442

3443+
/*
3444+
A static address is a 48-bit randomly generated address and shall meet the following requirements:
3445+
The two most significant bits of the address shall be equal to 1
3446+
All bits of the random part of the address shall not be equal to 1
3447+
All bits of the random part of the address shall not be equal to 0
3448+
*/
3449+
3450+
memset(&invalid_non_rpa_addr.val, 0xff, BLE_DEV_ADDR_LEN);
3451+
memset(&invalid_static_rand_addr.val, 0x00, BLE_DEV_ADDR_LEN);
3452+
3453+
if ((addr->val[5] & BLE_STATIC_RAND_ADDR_MASK) == BLE_STATIC_RAND_ADDR_MASK) {
3454+
invalid_static_rand_addr.val[5] = invalid_static_rand_addr.val[5] | BLE_STATIC_RAND_ADDR_MASK;
3455+
3456+
if (memcmp(invalid_non_rpa_addr.val, addr->val, BLE_DEV_ADDR_LEN) == 0 ||
3457+
memcmp(invalid_static_rand_addr.val, addr->val, BLE_DEV_ADDR_LEN) == 0) {
3458+
return BLE_HS_EINVAL;
3459+
}
3460+
} else if ((addr->val[5] | BLE_NON_RPA_MASK) == BLE_NON_RPA_MASK) {
3461+
invalid_non_rpa_addr.val[5] = invalid_non_rpa_addr.val[5] & BLE_NON_RPA_MASK;
3462+
3463+
if (memcmp(invalid_non_rpa_addr.val, addr->val, BLE_DEV_ADDR_LEN) == 0 ||
3464+
memcmp(invalid_static_rand_addr.val, addr->val, BLE_DEV_ADDR_LEN) == 0) {
3465+
return BLE_HS_EINVAL;
3466+
}
3467+
} else {
3468+
BLE_HS_LOG(ERROR, "Invalid random address \n");
3469+
return BLE_HS_EINVAL;
3470+
}
3471+
34423472
ble_hs_lock();
34433473
rc = ble_gap_ext_adv_set_addr_no_lock(instance, addr->val);
34443474
ble_hs_unlock();

nimble/host/src/ble_hs_id_priv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int ble_hs_id_addr(uint8_t id_addr_type, const uint8_t **out_id_addr,
3232
int ble_hs_id_use_addr(uint8_t addr_type);
3333
void ble_hs_id_reset(void);
3434
void ble_hs_id_rnd_reset(void);
35+
bool ble_hs_id_is_rpa(const ble_addr_t *addr);
3536

3637
#ifdef __cplusplus
3738
}

0 commit comments

Comments
 (0)