From 11f16a0ee2c1cbfb0db96a5e2e7c5a0b9cb331a0 Mon Sep 17 00:00:00 2001 From: Prasad Alatkar Date: Mon, 2 Mar 2020 16:29:45 +0530 Subject: [PATCH] nimble/host: Fix check for valid static random address --- nimble/host/src/ble_hs_id.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/nimble/host/src/ble_hs_id.c b/nimble/host/src/ble_hs_id.c index 176ad2093..e8b6c8b63 100644 --- a/nimble/host/src/ble_hs_id.c +++ b/nimble/host/src/ble_hs_id.c @@ -58,14 +58,24 @@ ble_hs_id_set_rnd(const uint8_t *rnd_addr) { uint8_t addr_type_byte; int rc; - uint8_t all_zeros[BLE_DEV_ADDR_LEN] = {0}, all_ones[BLE_DEV_ADDR_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + int ones; ble_hs_lock(); - /* Make sure all bits are neither one nor zero */ + /* Make sure random part of rnd_addr is not all ones or zeros. Reference: + * Core v5.0, Vol 6, Part B, section 1.3.2.1 */ addr_type_byte = rnd_addr[5] & 0xc0; + + /* count bits set to 1 in random part of address */ + ones = __builtin_popcount(rnd_addr[0]); + ones += __builtin_popcount(rnd_addr[1]); + ones += __builtin_popcount(rnd_addr[2]); + ones += __builtin_popcount(rnd_addr[3]); + ones += __builtin_popcount(rnd_addr[4]); + ones += __builtin_popcount(rnd_addr[5] & 0x3f); + if ((addr_type_byte != 0x00 && addr_type_byte != 0xc0) || - !memcmp(rnd_addr, all_zeros, BLE_DEV_ADDR_LEN) || !memcmp(rnd_addr, all_ones, BLE_DEV_ADDR_LEN)) { + (ones == 0 || ones == 46)) { rc = BLE_HS_EINVAL; goto done; } @@ -77,8 +87,6 @@ ble_hs_id_set_rnd(const uint8_t *rnd_addr) memcpy(ble_hs_id_rnd, rnd_addr, 6); - rc = 0; - done: ble_hs_unlock(); return rc;