Skip to content

Commit

Permalink
[nrf noup] zephyr: Clean up non-secure RAM if enabled
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
sigvartmh authored and tmon-nordic committed Feb 17, 2023
1 parent 6242c86 commit d97046e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion boot/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ zephyr_library_sources(
)
endif()

if(CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL)
if(CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL OR CONFIG_MCUBOOT_CLEANUP_NONSECURE_RAM)
zephyr_library_sources(
${BOOT_DIR}/zephyr/nrf_cleanup.c
)
Expand Down
5 changes: 5 additions & 0 deletions boot/zephyr/include/nrf_cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@
*/
void nrf_cleanup_peripheral(void);

/**
* Perform cleanup of non-secure RAM that may have been used by MCUBoot.
*/
void nrf_cleanup_ns_ram(void);

#endif
5 changes: 4 additions & 1 deletion boot/zephyr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ K_SEM_DEFINE(boot_log_sem, 1, 1);
#include <pm_config.h>
#endif

#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL
#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL || CONFIG_MCUBOOT_NRF_CLEANUP_NONSECURE_RAM
#include <nrf_cleanup.h>
#endif

Expand Down Expand Up @@ -230,6 +230,9 @@ static void do_boot(struct boot_rsp *rsp)
#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL
nrf_cleanup_peripheral();
#endif
#if CONFIG_MCUBOOT_NRF_CLEANUP_NONSECURE_RAM && defined(PM_SRAM_NONSECURE_NAME)
nrf_cleanup_ns_ram();
#endif
#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
cleanup_arm_nvic(); /* cleanup NVIC registers */

Expand Down
13 changes: 13 additions & 0 deletions boot/zephyr/nrf_cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

#include <string.h>

#if defined(USE_PARTITION_MANAGER)
#include <pm_config.h>
#endif

#define NRF_UARTE_SUBSCRIBE_CONF_OFFS offsetof(NRF_UARTE_Type, SUBSCRIBE_STARTRX)
#define NRF_UARTE_SUBSCRIBE_CONF_SIZE (offsetof(NRF_UARTE_Type, EVENTS_CTS) -\
NRF_UARTE_SUBSCRIBE_CONF_OFFS)
Expand Down Expand Up @@ -81,3 +85,12 @@ void nrf_cleanup_peripheral(void)
#endif
nrf_cleanup_clock();
}

#if defined(USE_PARTITION_MANAGER) \
&& defined(CONFIG_ARM_TRUSTZONE_M) \
&& defined(PM_SRAM_NONSECURE_NAME)
void nrf_cleanup_ns_ram(void)
{
memset((void *) PM_SRAM_NONSECURE_ADDRESS, 0, PM_SRAM_NONSECURE_SIZE);
}
#endif

0 comments on commit d97046e

Please sign in to comment.