Skip to content

Commit 4713ad5

Browse files
committed
Add Free(dst) + XMEMSET before XMEMCPY in all wc_ hash Copy functions (MD5, SHA, SHA2, SHA3, SHAKE) and add copy cleanup tests to prevent resource leaks when copying into previously-used contexts.
1 parent 60573a3 commit 4713ad5

6 files changed

Lines changed: 308 additions & 6 deletions

File tree

wolfcrypt/src/md5.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,11 @@ int wc_Md5Copy(wc_Md5* src, wc_Md5* dst)
537537
if (src == NULL || dst == NULL)
538538
return BAD_FUNC_ARG;
539539

540+
/* Free dst resources before copy to prevent memory leaks (e.g.,
541+
* hardware contexts). XMEMCPY overwrites dst. */
542+
wc_Md5Free(dst);
543+
XMEMSET(dst, 0, sizeof(wc_Md5));
544+
540545
XMEMCPY(dst, src, sizeof(wc_Md5));
541546

542547
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)

wolfcrypt/src/sha.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
11751175
/* Free dst resources before copy to prevent memory leaks (e.g., msg
11761176
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
11771177
wc_ShaFree(dst);
1178+
XMEMSET(dst, 0, sizeof(wc_Sha));
11781179

11791180
XMEMCPY(dst, src, sizeof(wc_Sha));
11801181

wolfcrypt/src/sha256.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,7 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
25852585
/* Free dst resources before copy to prevent memory leaks (e.g., msg
25862586
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
25872587
wc_Sha224Free(dst);
2588+
XMEMSET(dst, 0, sizeof(wc_Sha224));
25882589

25892590
XMEMCPY(dst, src, sizeof(wc_Sha224));
25902591

@@ -2735,6 +2736,7 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
27352736
/* Free dst resources before copy to prevent memory leaks (e.g., msg
27362737
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
27372738
wc_Sha256Free(dst);
2739+
XMEMSET(dst, 0, sizeof(wc_Sha256));
27382740

27392741
XMEMCPY(dst, src, sizeof(wc_Sha256));
27402742

wolfcrypt/src/sha3.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,11 @@ static int wc_Sha3Copy(wc_Sha3* src, wc_Sha3* dst)
13061306
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
13071307
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
13081308

1309+
/* Free dst resources before copy to prevent memory leaks (e.g.,
1310+
* hardware contexts). XMEMCPY overwrites dst. */
1311+
wc_Sha3Free(dst);
1312+
XMEMSET(dst, 0, sizeof(wc_Sha3));
1313+
13091314
XMEMCPY(dst, src, sizeof(wc_Sha3));
13101315

13111316
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)

wolfcrypt/src/sha512.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,7 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
22522252
/* Free dst resources before copy to prevent memory leaks (e.g., msg
22532253
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
22542254
wc_Sha512Free(dst);
2255+
XMEMSET(dst, 0, sizeof(wc_Sha512));
22552256

22562257
XMEMCPY(dst, src, sizeof(wc_Sha512));
22572258
#ifdef WOLFSSL_SMALL_STACK_CACHE
@@ -2694,6 +2695,7 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
26942695
/* Free dst resources before copy to prevent memory leaks (e.g., msg
26952696
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
26962697
wc_Sha384Free(dst);
2698+
XMEMSET(dst, 0, sizeof(wc_Sha384));
26972699

26982700
XMEMCPY(dst, src, sizeof(wc_Sha384));
26992701

0 commit comments

Comments
 (0)