Skip to content

Commit f82fea4

Browse files
committed
fix(tests): correct the flash write length for NVS encrypted test
Write only till the embedded file size in the NVS partition. Earlier the length was kept as the whole partition size and it could result in accessing embedded rodata beyond the MMU mapped range.
1 parent 60ab963 commit f82fea4

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

components/nvs_flash/test_apps/main/test_nvs.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2016-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
66
#include <stdio.h>
77
#include <stdlib.h>
88
#include <string.h>
9+
#include <sys/param.h>
910

1011
#include <ctype.h>
1112
#include <errno.h>
@@ -587,6 +588,7 @@ TEST_CASE("test nvs apis for nvs partition generator utility with encryption ena
587588
extern const char nvs_key_start[] asm("_binary_encryption_keys_bin_start");
588589
extern const char nvs_key_end[] asm("_binary_encryption_keys_bin_end");
589590
extern const char nvs_data_sch0_start[] asm("_binary_partition_encrypted_bin_start");
591+
extern const char nvs_data_sch0_end[] asm("_binary_partition_encrypted_bin_end");
590592

591593
const esp_partition_t* key_part = esp_partition_find_first(
592594
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS, NULL);
@@ -600,15 +602,24 @@ TEST_CASE("test nvs apis for nvs partition generator utility with encryption ena
600602
ESP_ERROR_CHECK( esp_partition_write(key_part, i, nvs_key_start + i, SPI_FLASH_SEC_SIZE) );
601603
}
602604

603-
for (int i = 0; i < nvs_part->size; i+= SPI_FLASH_SEC_SIZE) {
605+
const int content_size = nvs_data_sch0_end - nvs_data_sch0_start - 1;
606+
TEST_ASSERT_TRUE((content_size % SPI_FLASH_SEC_SIZE) == 0);
607+
608+
const int size_to_write = MIN(content_size, nvs_part->size);
609+
for (int i = 0; i < size_to_write; i+= SPI_FLASH_SEC_SIZE) {
604610
ESP_ERROR_CHECK( esp_partition_write(nvs_part, i, nvs_data_sch0_start + i, SPI_FLASH_SEC_SIZE) );
605611
}
606612

607613
err = nvs_flash_read_security_cfg(key_part, &xts_cfg);
608614
#elif CONFIG_NVS_SEC_KEY_PROTECT_USING_HMAC
609615
extern const char nvs_data_sch1_start[] asm("_binary_partition_encrypted_hmac_bin_start");
616+
extern const char nvs_data_sch1_end[] asm("_binary_partition_encrypted_hmac_bin_end");
617+
618+
const int content_size = nvs_data_sch1_end - nvs_data_sch1_start - 1;
619+
TEST_ASSERT_TRUE((content_size % SPI_FLASH_SEC_SIZE) == 0);
610620

611-
for (int i = 0; i < nvs_part->size; i+= SPI_FLASH_SEC_SIZE) {
621+
const int size_to_write = MIN(content_size, nvs_part->size);
622+
for (int i = 0; i < size_to_write; i+= SPI_FLASH_SEC_SIZE) {
612623
ESP_ERROR_CHECK( esp_partition_write(nvs_part, i, nvs_data_sch1_start + i, SPI_FLASH_SEC_SIZE) );
613624
}
614625

0 commit comments

Comments
 (0)