Commit 256bc37a authored by Dominik Ermel's avatar Dominik Ermel Committed by Dominik Ermel
Browse files

bootutil: Fixing memset not beeing called



Memset could have been out optimized by compiler and also
not called in error path.

Signed-off-by: default avatarDominik Ermel <dominik.ermel@nordicsemi.no>
parent eb7658e8
Loading
Loading
Loading
Loading
+24 −4
Original line number Diff line number Diff line
@@ -935,6 +935,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.
@@ -2234,17 +2249,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);
}