Skip to content

Commit d04dd27

Browse files
sigvartmhbjarki-andreasen
authored andcommitted
[nrf noup] zephyr: Clean up non-secure RAM if enabled
To ensure that MCUBoot does not leak keys or other material through memory to non-secure side we clear the memory before jumping to the next image. Signed-off-by: Sigvart Hovland <[email protected]> Signed-off-by: Dominik Ermel <[email protected]> Signed-off-by: Ole Sæther <[email protected]> (cherry picked from commit 047d463)
1 parent 50c5cdb commit d04dd27

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ if(SYSBUILD)
575575
set(mcuboot_image_upgrade_footer_size ${required_upgrade_size} CACHE INTERNAL "Estimated MCUboot update image trailer size" FORCE)
576576
endif()
577577

578-
if(CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL)
578+
if(CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL OR CONFIG_MCUBOOT_CLEANUP_NONSECURE_RAM)
579579
zephyr_library_sources(
580580
${BOOT_DIR}/zephyr/nrf_cleanup.c
581581
)

boot/zephyr/include/nrf_cleanup.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
*/
1717
void nrf_cleanup_peripheral(void);
1818

19+
/**
20+
* Perform cleanup of non-secure RAM that may have been used by MCUBoot.
21+
*/
22+
void nrf_cleanup_ns_ram(void);
23+
1924
#endif

boot/zephyr/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ K_SEM_DEFINE(boot_log_sem, 1, 1);
142142
#include <pm_config.h>
143143
#endif
144144

145-
#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL
145+
#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL || CONFIG_MCUBOOT_NRF_CLEANUP_NONSECURE_RAM
146146
#include <nrf_cleanup.h>
147147
#endif
148148

@@ -219,6 +219,9 @@ static void do_boot(struct boot_rsp *rsp)
219219
#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL
220220
nrf_cleanup_peripheral();
221221
#endif
222+
#if CONFIG_MCUBOOT_NRF_CLEANUP_NONSECURE_RAM && defined(PM_SRAM_NONSECURE_NAME)
223+
nrf_cleanup_ns_ram();
224+
#endif
222225
#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
223226
cleanup_arm_nvic(); /* cleanup NVIC registers */
224227

boot/zephyr/nrf_cleanup.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
#include <string.h>
2222

23+
#if USE_PARTITION_MANAGER
24+
#include <pm_config.h>
25+
#endif
26+
2327
#define NRF_UARTE_SUBSCRIBE_CONF_OFFS offsetof(NRF_UARTE_Type, SUBSCRIBE_STARTRX)
2428
#define NRF_UARTE_SUBSCRIBE_CONF_SIZE (offsetof(NRF_UARTE_Type, EVENTS_CTS) -\
2529
NRF_UARTE_SUBSCRIBE_CONF_OFFS)
@@ -81,3 +85,12 @@ void nrf_cleanup_peripheral(void)
8185
#endif
8286
nrf_cleanup_clock();
8387
}
88+
89+
#if USE_PARTITION_MANAGER \
90+
&& defined(CONFIG_ARM_TRUSTZONE_M) \
91+
&& defined(PM_SRAM_NONSECURE_NAME)
92+
void nrf_cleanup_ns_ram(void)
93+
{
94+
memset((void *) PM_SRAM_NONSECURE_ADDRESS, 0, PM_SRAM_NONSECURE_SIZE);
95+
}
96+
#endif

0 commit comments

Comments
 (0)