Skip to content

Commit e0312fe

Browse files
committed
fix(mbedtls/aes): Fix external memory corruption caused due to unaligned length cache sync
Fixes the memory corruption issue that arises due to external memory cache sync of unaligned length bytes when L2 cache line size is greater than the L1 cache line size
1 parent 9240757 commit e0312fe

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

components/mbedtls/port/aes/dma/esp_aes_dma_core.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,15 @@ static inline void *aes_dma_calloc(size_t num, size_t size, uint32_t caps, size_
325325
return heap_caps_aligned_calloc(DMA_DESC_MEM_ALIGN_SIZE, num, size, caps | MALLOC_CAP_DMA | MALLOC_CAP_8BIT);
326326
}
327327

328-
static inline esp_err_t dma_desc_link(crypto_dma_desc_t *dmadesc, size_t crypto_dma_desc_num)
328+
static inline esp_err_t dma_desc_link(crypto_dma_desc_t *dmadesc, size_t crypto_dma_desc_num, size_t buffer_cache_line_size)
329329
{
330330
esp_err_t ret = ESP_OK;
331331
for (int i = 0; i < crypto_dma_desc_num; i++) {
332332
dmadesc[i].dw0.suc_eof = ((i == crypto_dma_desc_num - 1) ? 1 : 0);
333333
dmadesc[i].next = ((i == crypto_dma_desc_num - 1) ? NULL : &dmadesc[i+1]);
334334
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
335-
/* Write back both input buffers and output buffers to clear any cache dirty bit if set
336-
If we want to remove `ESP_CACHE_MSYNC_FLAG_UNALIGNED` aligned flag then we need to pass
337-
cache msync size = ALIGN_UP(dma_desc.size, cache_line_size), where cache_line_size is the
338-
the cache line size coressponding to the buffer that is being synced, instead of dma_desc.size
339-
Keeping the `ESP_CACHE_MSYNC_FLAG_UNALIGNED` flag just because it should not look like
340-
we are syncing extra bytes due to ALIGN_UP'ed size but just the number of bytes that
341-
are needed in the operation. */
342-
ret = esp_cache_msync(dmadesc[i].buffer, dmadesc[i].dw0.length, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
335+
/* Write back both input buffers and output buffers to clear any cache dirty bit if set */
336+
ret = esp_cache_msync(dmadesc[i].buffer, ALIGN_UP(dmadesc[i].dw0.length, buffer_cache_line_size), ESP_CACHE_MSYNC_FLAG_DIR_C2M);
343337
if (ret != ESP_OK) {
344338
return ret;
345339
}
@@ -471,7 +465,7 @@ static esp_err_t generate_descriptor_list(const uint8_t *buffer, const size_t le
471465
populated_dma_descs += 1;
472466
}
473467

474-
if (dma_desc_link(dma_descriptors, dma_descs_needed) != ESP_OK) {
468+
if (dma_desc_link(dma_descriptors, dma_descs_needed, cache_line_size) != ESP_OK) {
475469
ESP_LOGE(TAG, "DMA descriptors cache sync C2M failed");
476470
return ESP_FAIL;
477471
}

0 commit comments

Comments
 (0)