Skip to content

Commit 4c9b980

Browse files
committed
Fix potential memory leak in SHA Copy and zero-initialize temp GetHash contexts; zero HMAC dst hash before copy to prevent shared pointers
1 parent 1f3bea4 commit 4c9b980

4 files changed

Lines changed: 21 additions & 32 deletions

File tree

wolfcrypt/src/hmac.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ int wc_HmacCopy(Hmac* src, Hmac* dst) {
331331

332332
XMEMCPY(dst, src, sizeof(*dst));
333333

334+
/* Zero hash context after shallow copy to prevent shared sub-pointers
335+
* (e.g., msg, W buffers) with src. The hash Copy function will perform
336+
* the proper deep copy. */
337+
XMEMSET(&dst->hash, 0, sizeof(wc_HmacHash));
338+
334339
ret = HmacKeyCopyHash(src->macType, &src->hash, &dst->hash);
335340

336341
if (ret != 0)

wolfcrypt/src/sha.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,10 @@ int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
11721172
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
11731173
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
11741174

1175+
/* Free dst resources before copy to prevent memory leaks (e.g., msg
1176+
* buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
1177+
wc_ShaFree(dst);
1178+
11751179
XMEMCPY(dst, src, sizeof(wc_Sha));
11761180

11771181
#if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3)

wolfcrypt/src/sha256.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,14 +2582,9 @@ 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's msg buffer before copy to prevent potential memory leak
2586-
* when XMEMCPY overwrites dst with src's pointers. */
2587-
#if defined(WOLFSSL_HASH_KEEP)
2588-
if (dst->msg != NULL) {
2589-
XFREE(dst->msg, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
2590-
dst->msg = NULL;
2591-
}
2592-
#endif
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);
25932588

25942589
XMEMCPY(dst, src, sizeof(wc_Sha224));
25952590

@@ -2737,14 +2732,9 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
27372732
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
27382733
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
27392734

2740-
/* Free dst's msg buffer before copy to prevent potential memory leak
2741-
* when XMEMCPY overwrites dst with src's pointers. */
2742-
#if defined(WOLFSSL_HASH_KEEP)
2743-
if (dst->msg != NULL) {
2744-
XFREE(dst->msg, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
2745-
dst->msg = NULL;
2746-
}
2747-
#endif
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);
27482738

27492739
XMEMCPY(dst, src, sizeof(wc_Sha256));
27502740

wolfcrypt/src/sha512.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,14 +2249,9 @@ 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's msg buffer before copy to prevent potential memory leak
2253-
* when XMEMCPY overwrites dst with src's pointers. */
2254-
#if defined(WOLFSSL_HASH_KEEP)
2255-
if (dst->msg != NULL) {
2256-
XFREE(dst->msg, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
2257-
dst->msg = NULL;
2258-
}
2259-
#endif
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);
22602255

22612256
XMEMCPY(dst, src, sizeof(wc_Sha512));
22622257
#ifdef WOLFSSL_SMALL_STACK_CACHE
@@ -2696,14 +2691,9 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
26962691
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
26972692
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
26982693

2699-
/* Free dst's msg buffer before copy to prevent potential memory leak
2700-
* when XMEMCPY overwrites dst with src's pointers. */
2701-
#if defined(WOLFSSL_HASH_KEEP)
2702-
if (dst->msg != NULL) {
2703-
XFREE(dst->msg, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
2704-
dst->msg = NULL;
2705-
}
2706-
#endif
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);
27072697

27082698
XMEMCPY(dst, src, sizeof(wc_Sha384));
27092699

0 commit comments

Comments
 (0)