Skip to content

Commit 4921745

Browse files
committed
[nrf fromtree] bootutil: Fixing memset not beeing called
Memset could have been out optimized by compiler and also not called in error path. Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit 256bc37) (cherry picked from commit 29b544f)
1 parent 03e35ee commit 4921745

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

boot/bootutil/src/loader.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,21 @@ boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
10461046
}
10471047

10481048
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
1049+
1050+
#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
1051+
/* Replacement for memset(p, 0, sizeof(*p) that does not get
1052+
* optimized out.
1053+
*/
1054+
static void like_mbedtls_zeroize(void *p, size_t n)
1055+
{
1056+
volatile unsigned char *v = (unsigned char *)p;
1057+
1058+
for (size_t i = 0; i < n; i++) {
1059+
v[i] = 0;
1060+
}
1061+
}
1062+
#endif
1063+
10491064
/**
10501065
* Copies the contents of one flash region to another. You must erase the
10511066
* destination region prior to calling this function.
@@ -2363,17 +2378,22 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
23632378
if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
23642379
FIH_PANIC;
23652380
}
2381+
2382+
fill_rsp(state, rsp);
2383+
2384+
fih_rc = FIH_SUCCESS;
2385+
out:
23662386
/*
23672387
* Since the boot_status struct stores plaintext encryption keys, reset
23682388
* them here to avoid the possibility of jumping into an image that could
23692389
* easily recover them.
23702390
*/
2391+
#if defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_SWAP_SAVE_ENCTLV)
2392+
like_mbedtls_zeroize(&bs, sizeof(bs));
2393+
#else
23712394
memset(&bs, 0, sizeof(struct boot_status));
2395+
#endif
23722396

2373-
fill_rsp(state, rsp);
2374-
2375-
fih_rc = FIH_SUCCESS;
2376-
out:
23772397
close_all_flash_areas(state);
23782398
FIH_RET(fih_rc);
23792399
}

0 commit comments

Comments
 (0)