Skip to content

Commit 1f3bea4

Browse files
committed
Fix potential memory leak when copying into existing SHA contexts and zero-initialize temp GetHash contexts
1 parent 9102df3 commit 1f3bea4

3 files changed

Lines changed: 41 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: 20 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,15 @@ 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
2593+
25852594
XMEMCPY(dst, src, sizeof(wc_Sha224));
25862595

25872596
#ifdef WOLFSSL_SMALL_STACK_CACHE
@@ -2691,7 +2700,7 @@ int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
26912700
return BAD_FUNC_ARG;
26922701
}
26932702

2694-
WC_ALLOC_VAR_EX(tmpSha256, wc_Sha256, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
2703+
WC_CALLOC_VAR_EX(tmpSha256, wc_Sha256, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
26952704
return MEMORY_E);
26962705

26972706
ret = wc_Sha256Copy(sha256, tmpSha256);
@@ -2728,6 +2737,15 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
27282737
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
27292738
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
27302739

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
2748+
27312749
XMEMCPY(dst, src, sizeof(wc_Sha256));
27322750

27332751
#ifdef WOLFSSL_MAXQ10XX_CRYPTO

wolfcrypt/src/sha512.c

Lines changed: 20 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,15 @@ 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
2260+
22522261
XMEMCPY(dst, src, sizeof(wc_Sha512));
22532262
#ifdef WOLFSSL_SMALL_STACK_CACHE
22542263
/* This allocation combines the customary W buffer used by
@@ -2649,7 +2658,7 @@ int wc_Sha384GetHash(wc_Sha384* sha384, byte* hash)
26492658
return BAD_FUNC_ARG;
26502659
}
26512660

2652-
WC_ALLOC_VAR_EX(tmpSha384, wc_Sha384, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
2661+
WC_CALLOC_VAR_EX(tmpSha384, wc_Sha384, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER,
26532662
return MEMORY_E);
26542663

26552664
/* copy this sha384 into tmpSha */
@@ -2687,6 +2696,15 @@ int wc_Sha384Copy(wc_Sha384* src, wc_Sha384* dst)
26872696
ret = 0; /* Reset ret to 0 to avoid returning the callback error code */
26882697
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
26892698

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
2707+
26902708
XMEMCPY(dst, src, sizeof(wc_Sha384));
26912709

26922710
#ifdef WOLFSSL_SMALL_STACK_CACHE

0 commit comments

Comments
 (0)