Skip to content

Commit 69d6369

Browse files
committed
MemZero: Add more checks of buffers.
Added wc_MemZero_Add calls and wc_MemZero_Check calls wheter ForceZero is used. Fixed a couple of places that had the wrong size.
1 parent b3424c4 commit 69d6369

35 files changed

Lines changed: 1240 additions & 18 deletions

src/dtls.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,21 @@ static void FindPskSuiteFromExt(const WOLFSSL* ssl, TLSX* extensions,
571571
byte psk_key[MAX_PSK_KEY_LEN];
572572
word32 psk_keySz;
573573
byte foundSuite[SUITE_LEN];
574+
#ifdef WOLFSSL_CHECK_MEM_ZERO
575+
/* Register before the key is populated so any future path that
576+
* fails to clear it before scope exit is caught. Baseline the
577+
* buffer so it is defined at registration time. */
578+
XMEMSET(psk_key, 0, sizeof(psk_key));
579+
wc_MemZero_Add("FindPskSuiteFromExt psk_key", psk_key,
580+
sizeof(psk_key));
581+
#endif
574582
ret = FindPskSuite(ssl, current, psk_key, &psk_keySz,
575583
suites->suites + i, &found, foundSuite);
576584
/* Clear the key just in case */
577585
ForceZero(psk_key, sizeof(psk_key));
586+
#ifdef WOLFSSL_CHECK_MEM_ZERO
587+
wc_MemZero_Check(psk_key, sizeof(psk_key));
588+
#endif
578589
if (ret == 0 && found) {
579590
pskInfo->cipherSuite0 = foundSuite[0];
580591
pskInfo->cipherSuite = foundSuite[1];

src/pk.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4933,6 +4933,13 @@ static int _DH_compute_key(unsigned char* key, const WOLFSSL_BIGNUM* otherPub,
49334933
if (privSz <= 0) {
49344934
ret = WOLFSSL_FATAL_ERROR;
49354935
}
4936+
#if defined(WOLFSSL_CHECK_MEM_ZERO) && !defined(WOLFSSL_SMALL_STACK)
4937+
/* Register as soon as the stack array holds the private key so any
4938+
* future path that fails to zeroize it before exit is caught. */
4939+
else {
4940+
wc_MemZero_Add("_DH_compute_key priv", priv, (word32)privSz);
4941+
}
4942+
#endif
49364943
}
49374944
if (ret == 0) {
49384945
/* Get the public key into the array. */
@@ -4996,6 +5003,9 @@ static int _DH_compute_key(unsigned char* key, const WOLFSSL_BIGNUM* otherPub,
49965003
{
49975004
/* Zeroize sensitive data. */
49985005
ForceZero(priv, (word32)privSz);
5006+
#if defined(WOLFSSL_CHECK_MEM_ZERO) && !defined(WOLFSSL_SMALL_STACK)
5007+
wc_MemZero_Check(priv, sizeof(priv));
5008+
#endif
49995009
}
50005010
}
50015011
WC_FREE_VAR_EX(pub, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
@@ -7226,6 +7236,14 @@ int wolfSSL_PEM_do_header(EncryptedInfo* cipher, unsigned char* data, long* len,
72267236
if (passwordSz < 0) {
72277237
ret = 0;
72287238
}
7239+
#ifdef WOLFSSL_CHECK_MEM_ZERO
7240+
/* Register as soon as the stack buffer holds the secret so any future
7241+
* path that fails to zeroize it before exit is caught. */
7242+
else if (passwordSz > 0) {
7243+
wc_MemZero_Add("wolfSSL_PEM_do_header password", password,
7244+
(word32)passwordSz);
7245+
}
7246+
#endif
72297247
}
72307248

72317249
if (ret == 1) {
@@ -7239,6 +7257,9 @@ int wolfSSL_PEM_do_header(EncryptedInfo* cipher, unsigned char* data, long* len,
72397257
if (passwordSz > 0) {
72407258
/* Ensure password is erased from memory. */
72417259
ForceZero(password, (word32)passwordSz);
7260+
#ifdef WOLFSSL_CHECK_MEM_ZERO
7261+
wc_MemZero_Check(password, NAME_SZ);
7262+
#endif
72427263
}
72437264

72447265
return ret;
@@ -7476,6 +7497,14 @@ static int pem_write_mem_pkcs8privatekey(byte** pem, int* pemSz,
74767497
res = 0;
74777498
}
74787499
passwd = password;
7500+
#ifdef WOLFSSL_CHECK_MEM_ZERO
7501+
/* Register as soon as the stack buffer holds the secret so any
7502+
* future path that fails to zeroize it before exit is caught. */
7503+
if (passwdSz > 0) {
7504+
wc_MemZero_Add("pem_write_mem_pkcs8privatekey password",
7505+
password, (word32)passwdSz);
7506+
}
7507+
#endif
74797508
}
74807509

74817510
if (res == 1) {
@@ -7489,6 +7518,9 @@ static int pem_write_mem_pkcs8privatekey(byte** pem, int* pemSz,
74897518
/* Zeroize the password from memory. */
74907519
if ((password == passwd) && (passwdSz > 0)) {
74917520
ForceZero(password, (word32)passwdSz);
7521+
#ifdef WOLFSSL_CHECK_MEM_ZERO
7522+
wc_MemZero_Check(password, NAME_SZ);
7523+
#endif
74927524
}
74937525
}
74947526
else if ((res == 1) && (enc == NULL)) {

src/sniffer.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7594,6 +7594,22 @@ static int parseKeyLogFile(const char* fileName, char* error)
75947594
return WOLFSSL_SNIFFER_ERROR;
75957595
}
75967596

7597+
#ifdef WOLFSSL_CHECK_MEM_ZERO
7598+
/* Register the secret-bearing stack buffers as high as possible (right
7599+
* after the last early return that bypasses the ForceZeros, i.e. the
7600+
* fopen failure above) so any future path that fails to clear them before
7601+
* scope exit is caught. Every exit path below (the in-loop error return
7602+
* and the normal post-loop return) ForceZeros and Checks each of them, so
7603+
* exactly one Add is balanced by one Check. Baseline the not-yet-written
7604+
* buffers so they are defined at registration time (secretHex is already
7605+
* zero-initialized at declaration). */
7606+
XMEMSET(secret, 0, sizeof(secret));
7607+
XMEMSET(line, 0, sizeof(line));
7608+
wc_MemZero_Add("parseKeyLogFile secret", secret, sizeof(secret));
7609+
wc_MemZero_Add("parseKeyLogFile secretHex", secretHex, sizeof(secretHex));
7610+
wc_MemZero_Add("parseKeyLogFile line", line, sizeof(line));
7611+
#endif
7612+
75977613
while (fgets(line, (int)sizeof(line), file) != NULL) {
75987614
/* RFC 9850 Section 1: ignore empty lines and lines whose first
75997615
* character is the octothorpe ('#') comment marker. */
@@ -7658,6 +7674,11 @@ static int parseKeyLogFile(const char* fileName, char* error)
76587674
ForceZero(secret, SECRET_LENGTH);
76597675
ForceZero(secretHex, sizeof(secretHex));
76607676
ForceZero(line, sizeof(line));
7677+
#ifdef WOLFSSL_CHECK_MEM_ZERO
7678+
wc_MemZero_Check(secret, sizeof(secret));
7679+
wc_MemZero_Check(secretHex, sizeof(secretHex));
7680+
wc_MemZero_Check(line, sizeof(line));
7681+
#endif
76617682
return ret;
76627683
}
76637684
}
@@ -7666,6 +7687,11 @@ static int parseKeyLogFile(const char* fileName, char* error)
76667687
ForceZero(secret, SECRET_LENGTH);
76677688
ForceZero(secretHex, sizeof(secretHex));
76687689
ForceZero(line, sizeof(line));
7690+
#ifdef WOLFSSL_CHECK_MEM_ZERO
7691+
wc_MemZero_Check(secret, sizeof(secret));
7692+
wc_MemZero_Check(secretHex, sizeof(secretHex));
7693+
wc_MemZero_Check(line, sizeof(line));
7694+
#endif
76697695
return 0;
76707696
}
76717697

src/tls13.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,14 @@ int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen,
10541054
return BAD_FUNC_ARG;
10551055
}
10561056

1057+
#ifdef WOLFSSL_CHECK_MEM_ZERO
1058+
/* Poison and register firstExpand before it is written so that any path
1059+
* below (all of which funnel through cleanup) is covered. */
1060+
XMEMSET(firstExpand, 0xff, sizeof(firstExpand));
1061+
wc_MemZero_Add("Tls13_Exporter firstExpand", firstExpand,
1062+
sizeof(firstExpand));
1063+
#endif
1064+
10571065
/* Derive-Secret(Secret, label, "") */
10581066
ret = Tls13HKDFExpandLabel(ssl, firstExpand, hashLen,
10591067
ssl->arrays->exporterSecret, hashLen,
@@ -1076,6 +1084,9 @@ int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen,
10761084
* Hash(context_value); wipe both before the stack frame is reclaimed. */
10771085
ForceZero(firstExpand, sizeof(firstExpand));
10781086
ForceZero(hashOut, sizeof(hashOut));
1087+
#ifdef WOLFSSL_CHECK_MEM_ZERO
1088+
wc_MemZero_Check(firstExpand, sizeof(firstExpand));
1089+
#endif
10791090
return ret;
10801091
}
10811092
#endif
@@ -1533,6 +1544,11 @@ int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
15331544
WC_ALLOC_VAR_EX(key_dig, byte, MAX_PRF_DIG, ssl->heap,
15341545
DYNAMIC_TYPE_DIGEST, return MEMORY_E);
15351546

1547+
#ifdef WOLFSSL_CHECK_MEM_ZERO
1548+
XMEMSET(key_dig, 0xff, MAX_PRF_DIG);
1549+
wc_MemZero_Add("DeriveTls13Keys key_dig", key_dig, MAX_PRF_DIG);
1550+
#endif
1551+
15361552
if (side == ENCRYPT_AND_DECRYPT_SIDE) {
15371553
provision = PROVISION_CLIENT_SERVER;
15381554
}
@@ -1717,7 +1733,9 @@ int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
17171733
#endif /* WOLFSSL_DTLS13 */
17181734

17191735
end:
1720-
ForceZero(key_dig, (word32)i);
1736+
/* Zero the whole key_dig buffer (not just the i bytes derived) so no
1737+
* key-schedule material can linger in the unused tail. */
1738+
ForceZero(key_dig, MAX_PRF_DIG);
17211739
#ifdef WOLFSSL_SMALL_STACK
17221740
XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
17231741
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
@@ -6523,6 +6541,13 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
65236541

65246542
(void)suite;
65256543

6544+
#ifdef WOLFSSL_CHECK_MEM_ZERO
6545+
/* Poison and register binderKey up front; every exit below (including the
6546+
* error paths) funnels through the cleanup label which zeroes it. */
6547+
XMEMSET(binderKey, 0xff, sizeof(binderKey));
6548+
wc_MemZero_Add("DoPreSharedKeys binderKey", binderKey, sizeof(binderKey));
6549+
#endif
6550+
65266551
ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
65276552
if (ext == NULL) {
65286553
WOLFSSL_MSG("No pre shared extension keys found");
@@ -6739,6 +6764,9 @@ static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 inputSz,
67396764
cleanup:
67406765
ForceZero(binderKey, sizeof(binderKey));
67416766
ForceZero(binder, sizeof(binder));
6767+
#ifdef WOLFSSL_CHECK_MEM_ZERO
6768+
wc_MemZero_Check(binderKey, sizeof(binderKey));
6769+
#endif
67426770
WOLFSSL_LEAVE("DoPreSharedKeys", ret);
67436771

67446772
return ret;

0 commit comments

Comments
 (0)