Skip to content

Commit 3fed5f9

Browse files
committed
rsa: zero wc_MakeRsaKey stack temporaries for mem-zero check
wc_MakeRsaKey()'s non-small-stack path declares p/q/tmp1..3 as stack mp_ints and only mp_init's them after the argument and size checks. An early 'goto out' from those checks reaches the WOLFSSL_CHECK_MEM_ZERO cleanup, which calls mp_memzero_check() on the still-uninitialized structs; the garbage size field makes the check scan an arbitrary stack range and can false-abort on unrelated registered memory. Zero the temporaries up front (under WOLFSSL_CHECK_MEM_ZERO) so the early-out cleanup is safe.
1 parent cf5c126 commit 3fed5f9

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

wolfcrypt/src/rsa.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5402,6 +5402,23 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
54025402
#endif /* !WOLFSSL_CRYPTOCELL && !WOLFSSL_SE050 */
54035403
int err;
54045404

5405+
#if !defined(WOLFSSL_CRYPTOCELL) && \
5406+
(!defined(WOLFSSL_SE050) || defined(WOLFSSL_SE050_NO_RSA)) && \
5407+
!defined(WOLF_CRYPTO_CB_ONLY_RSA) && \
5408+
!defined(WOLFSSL_MICROCHIP_TA100) && \
5409+
!defined(WOLFSSL_SMALL_STACK) && defined(WOLFSSL_CHECK_MEM_ZERO)
5410+
/* Zero the stack temporaries so the mp_memzero_check() in the 'out'
5411+
* cleanup is safe even when an early argument/size check leaves via
5412+
* 'goto out' before these are mp_init'd - an uninitialized mp_int's size
5413+
* field would otherwise make the check scan an arbitrary stack range.
5414+
* Done here, after all declarations, to satisfy C89. */
5415+
XMEMSET(&p_buf, 0, sizeof(p_buf));
5416+
XMEMSET(&q_buf, 0, sizeof(q_buf));
5417+
XMEMSET(&tmp1_buf, 0, sizeof(tmp1_buf));
5418+
XMEMSET(&tmp2_buf, 0, sizeof(tmp2_buf));
5419+
XMEMSET(&tmp3_buf, 0, sizeof(tmp3_buf));
5420+
#endif
5421+
54055422
if (key == NULL || rng == NULL) {
54065423
err = BAD_FUNC_ARG;
54075424
goto out;

0 commit comments

Comments
 (0)