Skip to content

Commit 1c6405e

Browse files
committed
Merge branch 'fix/esp_tee_coverity_bugs' into 'master'
fix(security): Fixed coverity warnings related to the `esp_tee` component Closes IDF-12803, IDF-12804, and IDF-12826 See merge request espressif/esp-idf!38360
2 parents 1522efa + 13aff0b commit 1c6405e

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

components/esp_tee/subproject/components/tee_sec_storage/tee_sec_storage.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,15 @@ esp_err_t esp_tee_sec_storage_get_signature(uint16_t slot_id, esp_tee_sec_storag
511511
mbedtls_ecp_keypair_init(&priv_key);
512512
mbedtls_ecdsa_init(&sign_ctx);
513513

514+
size_t key_len = 0;
514515
int ret = -1;
515516
if (key_type == ESP_SEC_STG_KEY_ECDSA_SECP256R1) {
516517
ret = mbedtls_ecp_read_key(MBEDTLS_ECP_DP_SECP256R1, &priv_key, keyctx.ecdsa_secp256r1.priv_key, sizeof(keyctx.ecdsa_secp256r1.priv_key));
518+
key_len = ECDSA_SECP256R1_KEY_LEN;
517519
#if CONFIG_SECURE_TEE_SEC_STG_SUPPORT_SECP192R1_SIGN
518520
} else if (key_type == ESP_SEC_STG_KEY_ECDSA_SECP192R1) {
519521
ret = mbedtls_ecp_read_key(MBEDTLS_ECP_DP_SECP192R1, &priv_key, keyctx.ecdsa_secp192r1.priv_key, sizeof(keyctx.ecdsa_secp192r1.priv_key));
522+
key_len = ECDSA_SECP192R1_KEY_LEN;
520523
#endif
521524
} else {
522525
ESP_LOGE(TAG, "Unsupported key type for signature generation");
@@ -547,12 +550,6 @@ esp_err_t esp_tee_sec_storage_get_signature(uint16_t slot_id, esp_tee_sec_storag
547550

548551
memset(out_sign, 0x00, sizeof(esp_tee_sec_storage_sign_t));
549552

550-
size_t key_len = (key_type == ESP_SEC_STG_KEY_ECDSA_SECP256R1) ? ECDSA_SECP256R1_KEY_LEN :
551-
#if CONFIG_SECURE_TEE_SEC_STG_SUPPORT_SECP192R1_SIGN
552-
ECDSA_SECP192R1_KEY_LEN;
553-
#else
554-
0;
555-
#endif
556553
ret = mbedtls_mpi_write_binary(&r, out_sign->sign_r, key_len);
557554
if (ret != 0) {
558555
err = ESP_FAIL;

components/esp_tee/subproject/main/core/esp_tee_init.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -24,15 +24,15 @@
2424
/* TEE symbols */
2525
extern uint32_t _tee_stack;
2626
extern uint32_t _tee_intr_stack_bottom;
27-
extern uint32_t _tee_heap_start;
28-
extern uint32_t _tee_heap_end;
2927
extern uint32_t _tee_bss_start;
3028
extern uint32_t _tee_bss_end;
3129

3230
extern uint32_t _sec_world_entry;
3331
extern uint32_t _tee_s_intr_handler;
3432

35-
#define TEE_HEAP_SIZE (((uint32_t)&_tee_heap_end - (uint32_t)&_tee_heap_start))
33+
extern uint8_t _tee_heap_start[];
34+
extern uint8_t _tee_heap_end[];
35+
#define TEE_HEAP_SIZE ((size_t)(_tee_heap_end - _tee_heap_start))
3636

3737
static const char *TAG = "esp_tee_init";
3838

@@ -131,8 +131,12 @@ void __attribute__((noreturn)) esp_tee_init(uint32_t ree_entry_addr, uint32_t re
131131
/* TEE compatibility check and App config data initialization. */
132132
tee_init_app_config();
133133

134-
/* TEE Secure World heap initialization. */
135-
assert(esp_tee_heap_init(((void *)&_tee_heap_start), TEE_HEAP_SIZE) == ESP_OK);
134+
/* TEE heap initialization. */
135+
esp_err_t err = esp_tee_heap_init((void *)_tee_heap_start, TEE_HEAP_SIZE);
136+
if (err != ESP_OK) {
137+
ESP_LOGE(TAG, "Failed to setup the TEE heap!");
138+
abort();
139+
}
136140

137141
/* SoC specific secure initialization. */
138142
esp_tee_soc_secure_sys_init();
@@ -148,15 +152,19 @@ void __attribute__((noreturn)) esp_tee_init(uint32_t ree_entry_addr, uint32_t re
148152
((void *)&_tee_heap_start), TEE_HEAP_SIZE, TEE_HEAP_SIZE / 1024, "RAM");
149153

150154
/* Setting up the permissible flash operation address range */
151-
esp_err_t err = esp_tee_flash_setup_prot_ctx(tee_boot_part);
155+
err = esp_tee_flash_setup_prot_ctx(tee_boot_part);
152156
if (err != ESP_OK) {
153157
ESP_LOGE(TAG, "Failed to setup the TEE flash memory protection!");
154158
abort();
155159
}
156160
ESP_FAULT_ASSERT(err == ESP_OK);
157161

158162
/* Setting up the running non-secure app partition as per the address provided by the bootloader */
159-
assert(esp_tee_flash_set_running_ree_partition(ree_drom_addr) == ESP_OK);
163+
err = esp_tee_flash_set_running_ree_partition(ree_drom_addr);
164+
if (err != ESP_OK) {
165+
ESP_LOGE(TAG, "Failed to setup the active REE partition!");
166+
abort();
167+
}
160168

161169
tee_print_app_info();
162170

components/mbedtls/esp_tee/esp_tee_mbedtls_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
#define MBEDTLS_ASN1_PARSE_C
4343
#define MBEDTLS_BIGNUM_C
4444
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
45+
#if CONFIG_SECURE_TEE_SEC_STG_SUPPORT_SECP192R1_SIGN
4546
#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
47+
#endif
4648
#define MBEDTLS_ECP_C
4749
#define MBEDTLS_ECDSA_C
4850

components/mbedtls/port/ecdsa/ecdsa_alt.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ int esp_ecdsa_privkey_load_pk_context(mbedtls_pk_context *key_ctx, int efuse_blk
258258

259259
mbedtls_pk_init(key_ctx);
260260
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECDSA);
261-
mbedtls_pk_setup(key_ctx, pk_info);
261+
if (mbedtls_pk_setup(key_ctx, pk_info) != 0) {
262+
return -1;
263+
}
262264
keypair = mbedtls_pk_ec(*key_ctx);
263265

264266
return esp_ecdsa_privkey_load_mpi(&(keypair->MBEDTLS_PRIVATE(d)), efuse_blk);
@@ -466,7 +468,11 @@ int esp_ecdsa_tee_set_pk_context(mbedtls_pk_context *key_ctx, esp_ecdsa_pk_conf_
466468

467469
mbedtls_pk_init(key_ctx);
468470
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECDSA);
469-
mbedtls_pk_setup(key_ctx, pk_info);
471+
ret = mbedtls_pk_setup(key_ctx, pk_info);
472+
if (ret != 0) {
473+
ESP_LOGE(TAG, "Failed to setup pk context, mbedtls_pk_setup() returned %d", ret);
474+
return ret;
475+
}
470476
keypair = mbedtls_pk_ec(*key_ctx);
471477

472478
mbedtls_mpi_init(&(keypair->MBEDTLS_PRIVATE(d)));

0 commit comments

Comments
 (0)