Skip to content

Commit fab9b94

Browse files
author
lly
committed
ble_mesh: Add length check for some mesh operations
1 parent c77b739 commit fab9b94

File tree

8 files changed

+48
-12
lines changed

8 files changed

+48
-12
lines changed

components/bt/esp_ble_mesh/api/core/esp_ble_mesh_local_data_operation_api.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ uint16_t *esp_ble_mesh_is_model_subscribed_to_group(esp_ble_mesh_model_t *model,
4444

4545
esp_ble_mesh_elem_t *esp_ble_mesh_find_element(uint16_t element_addr)
4646
{
47+
if (!ESP_BLE_MESH_ADDR_IS_UNICAST(element_addr)) {
48+
return NULL;
49+
}
4750
return btc_ble_mesh_elem_find(element_addr);
4851
}
4952

components/bt/esp_ble_mesh/api/core/esp_ble_mesh_provisioning_api.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ esp_err_t esp_ble_mesh_node_input_string(const char *string)
118118
btc_ble_mesh_prov_args_t arg = {0};
119119
btc_msg_t msg = {0};
120120

121-
if (!string) {
121+
if (!string || strlen(string) > ESP_BLE_MESH_PROV_INPUT_OOB_MAX_LEN) {
122122
return ESP_ERR_INVALID_ARG;
123123
}
124124

@@ -187,7 +187,8 @@ esp_err_t esp_ble_mesh_provisioner_input_string(const char *string, uint8_t link
187187
btc_ble_mesh_prov_args_t arg = {0};
188188
btc_msg_t msg = {0};
189189

190-
if (!string || link_idx >= MAX_PROV_LINK_IDX) {
190+
if (!string || strlen(string) > ESP_BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN ||
191+
link_idx >= MAX_PROV_LINK_IDX) {
191192
return ESP_ERR_INVALID_ARG;
192193
}
193194

@@ -353,6 +354,10 @@ esp_err_t esp_ble_mesh_provisioner_set_dev_uuid_match(const uint8_t *match_val,
353354
btc_ble_mesh_prov_args_t arg = {0};
354355
btc_msg_t msg = {0};
355356

357+
if (match_len + offset > ESP_BLE_MESH_OCTET16_LEN) {
358+
return ESP_ERR_INVALID_ARG;
359+
}
360+
356361
ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
357362

358363
msg.sig = BTC_SIG_API_CALL;
@@ -449,7 +454,8 @@ esp_err_t esp_ble_mesh_set_fast_prov_info(esp_ble_mesh_fast_prov_info_t *fast_pr
449454
btc_ble_mesh_prov_args_t arg = {0};
450455
btc_msg_t msg = {0};
451456

452-
if (fast_prov_info == NULL) {
457+
if (fast_prov_info == NULL || (fast_prov_info->offset +
458+
fast_prov_info->match_len > ESP_BLE_MESH_OCTET16_LEN)) {
453459
return ESP_ERR_INVALID_ARG;
454460
}
455461

components/bt/esp_ble_mesh/api/esp_ble_mesh_defs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ typedef enum {
242242
ESP_BLE_MESH_PROV_OOB_ON_DEV = BIT(15),
243243
} esp_ble_mesh_prov_oob_info_t;
244244

245+
/*!< Maximum length of value used by Static OOB authentication */
246+
#define ESP_BLE_MESH_PROV_STATIC_OOB_MAX_LEN 16
247+
248+
/*!< Maximum length of string used by Output OOB authentication */
249+
#define ESP_BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN 8
250+
251+
/*!< Maximum length of string used by Output OOB authentication */
252+
#define ESP_BLE_MESH_PROV_INPUT_OOB_MAX_LEN 8
253+
245254
/*!< Macros used to define message opcode */
246255
#define ESP_BLE_MESH_MODEL_OP_1(b0) (b0)
247256
#define ESP_BLE_MESH_MODEL_OP_2(b0, b1) (((b0) << 8) | (b1))

components/bt/esp_ble_mesh/mesh_core/include/mesh_main.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ typedef enum {
6262
BLE_MESH_PROV_OOB_ON_DEV = BIT(15),
6363
} bt_mesh_prov_oob_info_t;
6464

65+
#define BLE_MESH_PROV_STATIC_OOB_MAX_LEN 16
66+
#define BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN 8
67+
#define BLE_MESH_PROV_INPUT_OOB_MAX_LEN 8
68+
6569
/** Provisioning properties & capabilities. */
6670
struct bt_mesh_prov {
6771
#if CONFIG_BLE_MESH_NODE

components/bt/esp_ble_mesh/mesh_core/prov.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,13 @@ int bt_mesh_prov_init(const struct bt_mesh_prov *prov_info)
17431743
return -EINVAL;
17441744
}
17451745

1746+
if (prov_info->static_val_len > BLE_MESH_PROV_STATIC_OOB_MAX_LEN ||
1747+
prov_info->output_size > BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN ||
1748+
prov_info->input_size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) {
1749+
BT_ERR("%s, Invalid auth oob length", __func__);
1750+
return -EINVAL;
1751+
}
1752+
17461753
/* Changed by Espressif. Use micro-ecc to generate public key now. */
17471754
key = bt_mesh_pub_key_get();
17481755
if (!key) {

components/bt/esp_ble_mesh/mesh_core/provisioner_prov.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,11 @@ int bt_mesh_provisioner_set_oob_output_data(const u8_t idx, const u8_t *num, u8_
21782178
* Parameter num_flag is used to indicate whether the value
21792179
* output by provisioner is number or string.
21802180
*/
2181+
if (num == NULL || size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) {
2182+
BT_ERR("%s, Invalid parameter", __func__);
2183+
return -EINVAL;
2184+
}
2185+
21812186
if (!link[idx].auth) {
21822187
BT_ERR("%s, link auth is NULL", __func__);
21832188
return -EINVAL;

components/bt/esp_ble_mesh/mesh_core/proxy_client.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,11 @@ void bt_mesh_proxy_client_adv_ind_recv(struct net_buf_simple *buf, const bt_mesh
660660

661661
switch (type) {
662662
case BLE_MESH_PROXY_ADV_NET_ID: {
663+
if (buf->len != sizeof(ctx.net_id.net_id)) {
664+
BT_WARN("Malformed Network ID");
665+
return;
666+
}
667+
663668
struct bt_mesh_subnet *sub = NULL;
664669
sub = bt_mesh_is_net_id_exist(buf->data);
665670
if (!sub) {

examples/bluetooth/esp_ble_mesh/common_vendor_models/fast_prov_vendor_model/components/esp_fast_prov_server_model.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ esp_err_t example_fast_prov_server_recv_msg(esp_ble_mesh_model_t *model,
139139
* status_bit_mask (2) + status_ctx_flag (1) + status_unicast (1) + status_net_idx (1) +
140140
* status_group (1) + status_pri_prov (1) + status_match (1) + status_action (1).
141141
*/
142-
uint8_t match_len = 0, match_val[16];
142+
uint8_t match_len = 0, match_val[16] = {0};
143143
uint8_t status_unicast = 0;
144144
uint8_t flags = 0;
145145

@@ -186,6 +186,11 @@ esp_err_t example_fast_prov_server_recv_msg(esp_ble_mesh_model_t *model,
186186
uint16_t pri_prov_addr = (ctx_flags & BIT(7)) ? net_buf_simple_pull_le16(buf) : ESP_BLE_MESH_ADDR_UNASSIGNED;
187187
if (ctx_flags & BIT(8)) {
188188
match_len = buf->len - ((ctx_flags & BIT(9)) ? 1 : 0);
189+
if (match_len > ESP_BLE_MESH_OCTET16_LEN) {
190+
net_buf_simple_add_le16(msg, BIT(5));
191+
net_buf_simple_add_u8(msg, 0x01); /* too large match value length */
192+
break;
193+
}
189194
memcpy(match_val, buf->data, match_len);
190195
net_buf_simple_pull(buf, match_len);
191196
}
@@ -249,14 +254,6 @@ esp_err_t example_fast_prov_server_recv_msg(esp_ble_mesh_model_t *model,
249254
}
250255
}
251256

252-
if (ctx_flags & BIT(8)) {
253-
if (match_len > 16) {
254-
net_buf_simple_add_le16(msg, BIT(5));
255-
net_buf_simple_add_u8(msg, 0x01); /* too large match value length */
256-
break;
257-
}
258-
}
259-
260257
if (ctx_flags & BIT(9)) {
261258
if ((action & BIT_MASK(2)) != FAST_PROV_ACT_ENTER) {
262259
net_buf_simple_add_le16(msg, BIT(6));

0 commit comments

Comments
 (0)