Skip to content

Commit 021128f

Browse files
committed
Fix potential memory leak when copying into existing SHA contexts and zero-initialize temp GetHash contexts
1 parent a08efc9 commit 021128f

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

wolfcrypt/src/sha.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ int wc_ShaGetHash(wc_Sha* sha, byte* hash)
11371137
return BAD_FUNC_ARG;
11381138
}
11391139

1140-
WC_ALLOC_VAR_EX(tmpSha, wc_Sha, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
1140+
WC_CALLOC_VAR_EX(tmpSha, wc_Sha, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
11411141
return MEMORY_E);
11421142

11431143
ret = wc_ShaCopy(sha, tmpSha);

wolfcrypt/src/sha256.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,7 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
25462546
return BAD_FUNC_ARG;
25472547
}
25482548

2549-
WC_ALLOC_VAR_EX(tmpSha224, wc_Sha224, 1, NULL,
2549+
WC_CALLOC_VAR_EX(tmpSha224, wc_Sha224, 1, NULL,
25502550
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
25512551

25522552
ret = wc_Sha224Copy(sha224, tmpSha224);
@@ -2582,6 +2582,10 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
25822582
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
25832583
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
25842584

2585+
/* Free dst resources before copy to prevent memory leaks (e.g., msg
2586+
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
2587+
wc_Sha224Free(dst);
2588+
25852589
XMEMCPY(dst, src, sizeof(wc_Sha224));
25862590

25872591
#ifdef WOLFSSL_SMALL_STACK_CACHE
@@ -2691,7 +2695,7 @@ int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
26912695
return BAD_FUNC_ARG;
26922696
}
26932697

2694-
WC_ALLOC_VAR_EX(tmpSha256, wc_Sha256, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
2698+
WC_CALLOC_VAR_EX(tmpSha256, wc_Sha256, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
26952699
return MEMORY_E);
26962700

26972701
ret = wc_Sha256Copy(sha256, tmpSha256);
@@ -2728,6 +2732,10 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
27282732
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
27292733
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
27302734

2735+
/* Free dst resources before copy to prevent memory leaks (e.g., msg
2736+
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
2737+
wc_Sha256Free(dst);
2738+
27312739
XMEMCPY(dst, src, sizeof(wc_Sha256));
27322740

27332741
#ifdef WOLFSSL_MAXQ10XX_CRYPTO

wolfcrypt/src/sha512.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ static int Sha512_Family_GetHash(wc_Sha512* sha512, byte* hash,
22062206
return BAD_FUNC_ARG;
22072207
}
22082208

2209-
WC_ALLOC_VAR_EX(tmpSha512, wc_Sha512, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
2209+
WC_CALLOC_VAR_EX(tmpSha512, wc_Sha512, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
22102210
return MEMORY_E);
22112211

22122212
/* copy this sha512 into tmpSha */
@@ -2249,6 +2249,10 @@ int wc_Sha512Copy(wc_Sha512* src, wc_Sha512* dst)
22492249
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
22502250
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
22512251

2252+
/* Free dst resources before copy to prevent memory leaks (e.g., msg
2253+
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
2254+
wc_Sha512Free(dst);
2255+
22522256
XMEMCPY(dst, src, sizeof(wc_Sha512));
22532257
#ifdef WOLFSSL_SMALL_STACK_CACHE
22542258
/* This allocation combines the customary W buffer used by
@@ -2649,7 +2653,7 @@ int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash)
26492653
return BAD_FUNC_ARG;
26502654
}
26512655

2652-
WC_ALLOC_VAR_EX(tmpSha384, wc_Sha384, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
2656+
WC_CALLOC_VAR_EX(tmpSha384, wc_Sha384, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
26532657
return MEMORY_E);
26542658

26552659
/* copy this sha384 into tmpSha */
@@ -2687,6 +2691,10 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
26872691
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
26882692
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
26892693

2694+
/* Free dst resources before copy to prevent memory leaks (e.g., msg
2695+
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
2696+
wc_Sha384Free(dst);
2697+
26902698
XMEMCPY(dst, src, sizeof(wc_Sha384));
26912699

26922700
#ifdef WOLFSSL_SMALL_STACK_CACHE

0 commit comments

Comments
 (0)