Skip to content

Commit

Permalink
[nrf fromtree] bootutil: Fixing memset not beeing called
Browse files Browse the repository at this point in the history
Memset could have been out optimized by compiler and also
not called in error path.

Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
(cherry picked from commit 256bc37)
(cherry picked from commit 29b544f)
  • Loading branch information
de-nordic committed May 26, 2023
1 parent 03e35ee commit 4921745
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions boot/bootutil/src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,21 @@ boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
}

#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)

#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
/* Replacement for memset(p, 0, sizeof(*p) that does not get
* optimized out.
*/
static void like_mbedtls_zeroize(void *p, size_t n)
{
volatile unsigned char *v = (unsigned char *)p;

for (size_t i = 0; i < n; i++) {
v[i] = 0;
}
}
#endif

/**
* Copies the contents of one flash region to another. You must erase the
* destination region prior to calling this function.
Expand Down Expand Up @@ -2363,17 +2378,22 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
FIH_PANIC;
}

fill_rsp(state, rsp);

fih_rc = FIH_SUCCESS;
out:
/*
* Since the boot_status struct stores plaintext encryption keys, reset
* them here to avoid the possibility of jumping into an image that could
* easily recover them.
*/
#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
like_mbedtls_zeroize(&bs, sizeof(bs));
#else
memset(&bs, 0, sizeof(struct boot_status));
#endif

fill_rsp(state, rsp);

fih_rc = FIH_SUCCESS;
out:
close_all_flash_areas(state);
FIH_RET(fih_rc);
}
Expand Down

0 comments on commit 4921745

Please sign in to comment.