Skip to content

Commit

Permalink
Bluetooth: Mesh: Prevent duplicate cdb appkeys
Browse files Browse the repository at this point in the history
Fixes issue where it is possible to store the same appkey
multiple times in CDB implementation.

Signed-off-by: Anders Storrø <[email protected]>
  • Loading branch information
Andrewpini committed Feb 29, 2024
1 parent ff8c892 commit 25c26ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
9 changes: 7 additions & 2 deletions include/zephyr/bluetooth/mesh/cdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,13 @@ void bt_mesh_cdb_node_foreach(bt_mesh_cdb_node_func_t func, void *user_data);
*
* Allocate a new subnet in the CDB.
*
* @note The provided net_idx must be unique, since multiple keys for the same index
* is not supported.
*
* @param net_idx NetIdx of the subnet.
*
* @return The new subnet or NULL if it cannot be allocated.
* @return The new subnet or NULL if it cannot be allocated due to
* lack of resources or the subnet has been already allocated.
*/
struct bt_mesh_cdb_subnet *bt_mesh_cdb_subnet_alloc(uint16_t net_idx);

Expand Down Expand Up @@ -328,7 +332,8 @@ int bt_mesh_cdb_subnet_key_export(const struct bt_mesh_cdb_subnet *sub, int key_
* @param net_idx NetIdx of NetKey that the application key is bound to.
* @param app_idx AppIdx of the application key.
*
* @return The new application key or NULL if it cannot be allocated.
* @return The new application key or NULL if it cannot be allocated due to
* lack of resources or the key has been already allocated.
*/
struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx,
uint16_t app_idx);
Expand Down
22 changes: 15 additions & 7 deletions subsys/bluetooth/mesh/cdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct bt_mesh_cdb bt_mesh_cdb = {
},
.app_keys = {
[0 ... (CONFIG_BT_MESH_CDB_APP_KEY_COUNT - 1)] = {
.app_idx = BT_MESH_KEY_UNUSED,
.net_idx = BT_MESH_KEY_UNUSED,
}
},
Expand Down Expand Up @@ -1024,26 +1025,32 @@ int bt_mesh_cdb_node_key_export(const struct bt_mesh_cdb_node *node, uint8_t out
return bt_mesh_key_export(out, &node->dev_key);
}

struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx,
uint16_t app_idx)
struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(uint16_t net_idx, uint16_t app_idx)
{
struct bt_mesh_cdb_app_key *key;
struct bt_mesh_cdb_app_key *vacant_key = NULL;
int i;

for (i = 0; i < ARRAY_SIZE(bt_mesh_cdb.app_keys); ++i) {
key = &bt_mesh_cdb.app_keys[i];

if (key->net_idx != BT_MESH_KEY_UNUSED) {
if (key->app_idx == app_idx) {
return NULL;
}

if (key->net_idx != BT_MESH_KEY_UNUSED || vacant_key) {
continue;
}

key->net_idx = net_idx;
key->app_idx = app_idx;
vacant_key = key;
}

return key;
if (vacant_key) {
vacant_key->net_idx = net_idx;
vacant_key->app_idx = app_idx;
}

return NULL;
return vacant_key;
}

void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store)
Expand All @@ -1055,6 +1062,7 @@ void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store)
}

key->net_idx = BT_MESH_KEY_UNUSED;
key->app_idx = BT_MESH_KEY_UNUSED;
bt_mesh_key_destroy(&key->keys[0].app_key);
bt_mesh_key_destroy(&key->keys[1].app_key);
memset(key->keys, 0, sizeof(key->keys));
Expand Down

0 comments on commit 25c26ff

Please sign in to comment.