Skip to content

Commit eda95c7

Browse files
committed
Merge branch 'security-fixes-2' into 'main'
components: fix null dereference, resource leaks, and input validation See merge request app-frameworks/esp-matter!1529
2 parents df47859 + db711bd commit eda95c7

8 files changed

Lines changed: 38 additions & 18 deletions

File tree

components/esp_matter/data_model/esp_matter_attribute_utils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,12 @@ bool val_compare(const esp_matter_attr_val_t *val1, const esp_matter_attr_val_t
672672
if (val1->val.a.s == null_len || val1->val.a.s == 0) {
673673
return true;
674674
}
675+
if (val1->val.a.b == nullptr && val2->val.a.b == nullptr) {
676+
return true;
677+
}
678+
if (val1->val.a.b == nullptr || val2->val.a.b == nullptr) {
679+
return false;
680+
}
675681
return memcmp(val1->val.a.b, val2->val.a.b, val1->val.a.s) == 0;
676682
}
677683
case ESP_MATTER_VAL_TYPE_UINT8:

components/esp_matter/data_model/esp_matter_data_model.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,7 @@ void set_user_callback(command_t *command, callback_t user_callback)
13261326
{
13271327
if (!command) {
13281328
ESP_LOGE(TAG, "Command cannot be NULL");
1329+
return;
13291330
}
13301331
_command_t *current_command = (_command_t *)command;
13311332
current_command->user_callback = user_callback;

components/esp_matter/data_model/private/singly_linked_list.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ template <typename T>
7878
void SinglyLinkedList<T>::remove(T **head, T *target)
7979
{
8080
T **p = head;
81-
while (*p != target) {
81+
while (*p && *p != target) {
8282
p = &(*p)->next;
8383
}
84-
*p = target->next;
85-
free(target);
84+
if (*p != nullptr) {
85+
*p = target->next;
86+
free(target);
87+
}
8688
}
8789

8890
template <typename T>

components/esp_matter_bridge/esp_matter_bridge.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ static esp_err_t store_device_persistent_info(device_persistent_info_t *persiste
5555
persistent_info, sizeof(device_persistent_info_t));
5656
if (err != ESP_OK) {
5757
ESP_LOGE(TAG, "Failed on nvs_set_blob when storing device_persistent_info");
58+
nvs_close(handle);
59+
return err;
5860
}
5961
err = nvs_commit(handle);
6062
if (err != ESP_OK) {
@@ -255,6 +257,10 @@ esp_err_t set_device_type(device_t *bridged_device, uint32_t device_type_id, voi
255257
ESP_LOGE(TAG, "bridged_device cannot be NULL");
256258
return ESP_ERR_INVALID_ARG;
257259
}
260+
if (!device_type_callback) {
261+
ESP_LOGE(TAG, "device_type_callback is NULL, call initialize() first");
262+
return ESP_ERR_INVALID_STATE;
263+
}
258264
err = device_type_callback(bridged_device->endpoint, device_type_id, priv_data);
259265
if (err != ESP_OK) {
260266
return err;
@@ -345,7 +351,9 @@ device_t *create_device(node_t *node, uint16_t parent_endpoint_id, uint32_t devi
345351
remove_device(dev);
346352
return NULL;
347353
}
348-
store_bridged_endpoint_ids();
354+
if (store_bridged_endpoint_ids() != ESP_OK) {
355+
ESP_LOGE(TAG, "Failed to persist bridged endpoint IDs");
356+
}
349357
return dev;
350358
}
351359

components/esp_matter_bridge/esp_matter_bridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <esp_matter_data_model.h>
2020

2121
#define MAX_BRIDGED_DEVICE_COUNT \
22-
CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT - 1 - CONFIG_ESP_MATTER_AGGREGATOR_ENDPOINT_COUNT
22+
(CONFIG_ESP_MATTER_MAX_DYNAMIC_ENDPOINT_COUNT - 1 - CONFIG_ESP_MATTER_AGGREGATOR_ENDPOINT_COUNT)
2323
// There is an endpoint reserved as root endpoint
2424

2525
namespace esp_matter_bridge {

components/esp_matter_console/esp_matter_console.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,21 @@ void engine::for_each_command(command_iterator_t *on_command, void *arg)
4848

4949
esp_err_t engine::exec_command(int argc, char *argv[])
5050
{
51-
esp_err_t err = ESP_ERR_INVALID_ARG;
5251
if (argc <= 0) {
53-
return err;
52+
return ESP_ERR_INVALID_ARG;
5453
}
54+
5555
// find the command from the command set
5656
for (unsigned i = 0; i < _command_set_count; ++i) {
5757
for (unsigned j = 0; j < _command_set_size[i]; ++j) {
5858
if (strcmp(argv[0], _command_set[i][j].name) == 0 && _command_set[i][j].handler) {
59-
err = _command_set[i][j].handler(argc - 1, &argv[1]);
60-
break;
59+
return _command_set[i][j].handler(argc - 1, &argv[1]);
6160
}
6261
}
6362
}
64-
return err;
63+
64+
return ESP_ERR_INVALID_ARG;
65+
6566
}
6667

6768
esp_err_t engine::register_commands(const command_t *command_set, unsigned count)

components/esp_matter_console/esp_matter_console_attribute.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ static esp_err_t console_set_handler(int argc, char **argv)
1717
{
1818
VerifyOrReturnError(argc >= 4, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "The arguments for this command is invalid"));
1919

20-
uint16_t endpoint_id = strtoul((const char *)&argv[0][2], NULL, 16);
21-
uint32_t cluster_id = strtoul((const char *)&argv[1][2], NULL, 16);
22-
uint32_t attribute_id = strtoul((const char *)&argv[2][2], NULL, 16);
20+
uint16_t endpoint_id = strtoul(argv[0], NULL, 0);
21+
uint32_t cluster_id = strtoul(argv[1], NULL, 0);
22+
uint32_t attribute_id = strtoul(argv[2], NULL, 0);
2323

2424
attribute_t *attr = attribute::get(endpoint_id, cluster_id, attribute_id);
2525
if (!attr) {
@@ -158,9 +158,9 @@ static esp_err_t console_set_handler(int argc, char **argv)
158158
static esp_err_t console_get_handler(int argc, char **argv)
159159
{
160160
VerifyOrReturnError(argc >= 3, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "The arguments for this command is invalid"));
161-
uint16_t endpoint_id = strtoul((const char *)&argv[0][2], NULL, 16);
162-
uint32_t cluster_id = strtoul((const char *)&argv[1][2], NULL, 16);
163-
uint32_t attribute_id = strtoul((const char *)&argv[2][2], NULL, 16);
161+
uint16_t endpoint_id = strtoul(argv[0], NULL, 0);
162+
uint32_t cluster_id = strtoul(argv[1], NULL, 0);
163+
uint32_t attribute_id = strtoul(argv[2], NULL, 0);
164164

165165
attribute_t *attr = attribute::get(endpoint_id, cluster_id, attribute_id);
166166
if (!attr) {

components/esp_matter_console/esp_matter_console_udc.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ static esp_err_t send_udc_request(int argc, char *argv[])
3232
{
3333
ESP_RETURN_ON_FALSE(argc == 2, ESP_ERR_INVALID_ARG, TAG, "Incorrect arguments");
3434
chip::Inet::IPAddress commissioner;
35-
chip::Inet::IPAddress::FromString(argv[0], commissioner);
35+
ESP_RETURN_ON_FALSE(chip::Inet::IPAddress::FromString(argv[0], commissioner),
36+
ESP_ERR_INVALID_ARG, TAG, "Invalid IP address");
3637
uint16_t port = (uint16_t)strtol(argv[1], nullptr, 10);
3738
chip::Protocols::UserDirectedCommissioning::IdentificationDeclaration id;
3839
chip::Server::GetInstance().SendUserDirectedCommissioningRequest(
@@ -44,7 +45,8 @@ static esp_err_t send_udc_cancel(int argc, char *argv[])
4445
{
4546
ESP_RETURN_ON_FALSE(argc == 2, ESP_ERR_INVALID_ARG, TAG, "Incorrect arguments");
4647
chip::Inet::IPAddress commissioner;
47-
chip::Inet::IPAddress::FromString(argv[0], commissioner);
48+
ESP_RETURN_ON_FALSE(chip::Inet::IPAddress::FromString(argv[0], commissioner),
49+
ESP_ERR_INVALID_ARG, TAG, "Invalid IP address");
4850
uint16_t port = (uint16_t)strtol(argv[1], nullptr, 10);
4951
chip::Protocols::UserDirectedCommissioning::IdentificationDeclaration id;
5052
id.SetCancelPasscode(true);

0 commit comments

Comments
 (0)