From c1c4990f75a2011f2fb915b92710f2ee73db3cc0 Mon Sep 17 00:00:00 2001 From: lly Date: Sat, 4 Jul 2020 20:55:48 +0800 Subject: [PATCH] ble_mesh: stack: Add more checks about input prov bearers --- .../api/core/esp_ble_mesh_provisioning_api.c | 30 ++++++++++++ components/bt/esp_ble_mesh/mesh_core/main.c | 49 ++++++++++++++----- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c index 9fbbd6b591..e0242de729 100644 --- a/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c +++ b/components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c @@ -35,11 +35,29 @@ bool esp_ble_mesh_node_is_provisioned(void) return bt_mesh_is_provisioned(); } +static bool prov_bearers_valid(esp_ble_mesh_prov_bearer_t bearers) +{ + if ((!(bearers & (ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT))) || + (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + !(bearers & ESP_BLE_MESH_PROV_ADV)) || + (!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + !(bearers & ESP_BLE_MESH_PROV_GATT))) { + return false; + } + return true; +} + esp_err_t esp_ble_mesh_node_prov_enable(esp_ble_mesh_prov_bearer_t bearers) { btc_ble_mesh_prov_args_t arg = {0}; btc_msg_t msg = {0}; + if (prov_bearers_valid(bearers) == false) { + return ESP_ERR_INVALID_ARG; + } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; @@ -56,6 +74,10 @@ esp_err_t esp_ble_mesh_node_prov_disable(esp_ble_mesh_prov_bearer_t bearers) btc_ble_mesh_prov_args_t arg = {0}; btc_msg_t msg = {0}; + if (prov_bearers_valid(bearers) == false) { + return ESP_ERR_INVALID_ARG; + } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; @@ -232,6 +254,10 @@ esp_err_t esp_ble_mesh_provisioner_prov_enable(esp_ble_mesh_prov_bearer_t bearer btc_ble_mesh_prov_args_t arg = {0}; btc_msg_t msg = {0}; + if (prov_bearers_valid(bearers) == false) { + return ESP_ERR_INVALID_ARG; + } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; @@ -249,6 +275,10 @@ esp_err_t esp_ble_mesh_provisioner_prov_disable(esp_ble_mesh_prov_bearer_t beare btc_ble_mesh_prov_args_t arg = {0}; btc_msg_t msg = {0}; + if (prov_bearers_valid(bearers) == false) { + return ESP_ERR_INVALID_ARG; + } + ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED); msg.sig = BTC_SIG_API_CALL; diff --git a/components/bt/esp_ble_mesh/mesh_core/main.c b/components/bt/esp_ble_mesh/mesh_core/main.c index 32a9c09391..bc583e75ee 100644 --- a/components/bt/esp_ble_mesh/mesh_core/main.c +++ b/components/bt/esp_ble_mesh/mesh_core/main.c @@ -171,12 +171,32 @@ bool bt_mesh_is_provisioner_en(void) } } +static bool prov_bearers_valid(bt_mesh_prov_bearer_t bearers) +{ + if ((!(bearers & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) || + (IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + !IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + !(bearers & BLE_MESH_PROV_ADV)) || + (!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && + IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && + !(bearers & BLE_MESH_PROV_GATT))) { + BT_ERR("Invalid bearers 0x%02x", bearers); + return false; + } + return true; +} + int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers) { if (bt_mesh_is_provisioned()) { + BT_WARN("%s, Already", __func__); return -EALREADY; } + if (prov_bearers_valid(bearers) == false) { + return -EINVAL; + } + bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_NODE); if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { @@ -206,6 +226,10 @@ int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers) return -EALREADY; } + if (prov_bearers_valid(bearers) == false) { + return -EINVAL; + } + bt_mesh_atomic_clear_bit(bt_mesh.flags, BLE_MESH_NODE); if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) { @@ -509,13 +533,6 @@ int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers) } #endif - if ((IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) && - (bearers & BLE_MESH_PROV_ADV)) || - (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && - (bearers & BLE_MESH_PROV_GATT))) { - bt_mesh_scan_enable(); - } - if (IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) && (bearers & BLE_MESH_PROV_GATT)) { bt_mesh_provisioner_pb_gatt_enable(); @@ -523,13 +540,15 @@ int bt_mesh_provisioner_net_start(bt_mesh_prov_bearer_t bearers) bt_mesh_atomic_set_bit(bt_mesh.flags, BLE_MESH_VALID_PROV); + if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { + bt_mesh_friend_init(); + } + if (bt_mesh_beacon_get() == BLE_MESH_BEACON_ENABLED) { bt_mesh_beacon_enable(); } - if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) { - bt_mesh_friend_init(); - } + bt_mesh_scan_enable(); return 0; } @@ -543,6 +562,10 @@ int bt_mesh_provisioner_enable(bt_mesh_prov_bearer_t bearers) return -EALREADY; } + if (prov_bearers_valid(bearers) == false) { + return -EINVAL; + } + err = bt_mesh_provisioner_set_prov_info(); if (err) { BT_ERR("%s, Failed to set provisioning info", __func__); @@ -573,9 +596,13 @@ int bt_mesh_provisioner_disable(bt_mesh_prov_bearer_t bearers) return -EALREADY; } + if (prov_bearers_valid(bearers) == false) { + return -EINVAL; + } + enable = bt_mesh_provisioner_get_prov_bearer(); if (!(enable & bearers)) { - BT_ERR("%s, Bearers mismatch", __func__); + BT_ERR("Mismatch bearers 0x%02x", bearers); return -EINVAL; }