Skip to content

Commit 4948b08

Browse files
authored
Merge pull request #214 from ColtonWilley/cleanup_memleaks_ossl_102
Clean up memory leaks in wolfengine using openssl 1.0.2
2 parents 70670a5 + 139ee01 commit 4948b08

13 files changed

Lines changed: 118 additions & 3 deletions

src/we_aes_ctr.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,18 @@ static int we_aes_ctr_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
195195

196196
/* Get the AES-CTR data to work with. */
197197
aes = (we_AesCtr *)EVP_CIPHER_CTX_get_cipher_data(ctx);
198-
if (aes != NULL) {
198+
if (aes == NULL) {
199199
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_CIPHER,
200200
"EVP_CIPHER_CTX_get_cipher_data", aes);
201201
ret = 0;
202202
}
203203
if (ret == 1) {
204204
switch (type) {
205+
case EVP_CTRL_INIT:
206+
{
207+
XMEMSET(aes, 0, sizeof(we_AesCtr));
208+
break;
209+
}
205210
default:
206211
XSNPRINTF(errBuff, sizeof(errBuff), "Unsupported ctrl type %d",
207212
type);
@@ -219,6 +224,7 @@ static int we_aes_ctr_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
219224
/** Flags for AES-CTR method. */
220225
#define AES_CTR_FLAGS \
221226
(EVP_CIPH_ALWAYS_CALL_INIT | \
227+
EVP_CIPH_CTRL_INIT | \
222228
EVP_CIPH_FLAG_CUSTOM_CIPHER | \
223229
EVP_CIPH_FLAG_DEFAULT_ASN1 | \
224230
EVP_CIPH_CTR_MODE)

src/we_internal.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,12 +1011,22 @@ static int wolfengine_destroy(ENGINE *e)
10111011
we_rsa_method = NULL;
10121012
#endif /* WE_HAVE_RSA */
10131013
#ifdef WE_HAVE_ECC
1014-
/* we_ec_method is freed by OpenSSL_cleanup(). */
1014+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
1015+
ECDSA_METHOD_free(we_ecdsa_method);
1016+
we_ecdsa_method = NULL;
1017+
#endif
1018+
/* For 1.1.1 and above we_ec_method is freed by OpenSSL_cleanup(). */
10151019
#ifdef WE_HAVE_EC_KEY
10161020
EC_KEY_METHOD_free(we_ec_key_method);
10171021
we_ec_key_method = NULL;
10181022
#endif
10191023
#endif
1024+
#if defined(WE_HAVE_ECDH)
1025+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
1026+
OPENSSL_free(we_ecdh_method);
1027+
we_ecdh_method = NULL;
1028+
#endif
1029+
#endif
10201030
#ifdef WE_HAVE_DES3CBC
10211031
EVP_CIPHER_meth_free(we_des3_cbc_ciph);
10221032
we_des3_cbc_ciph = NULL;

src/we_mac.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ int we_init_hmac_pkey_asn1_meth(void)
14491449
#endif
14501450
/* Add our created asn1 method to the internal list of available
14511451
* methods. */
1452+
/* Known, still-reachable memory leak from openssl internals on 1.0.2 */
14521453
EVP_PKEY_asn1_add0(we_hmac_pkey_asn1_method);
14531454
}
14541455
/* No failure after allocation - no need to free on error. */
@@ -1966,6 +1967,7 @@ int we_init_cmac_pkey_asn1_meth(void)
19661967
we_cmac_pkey_asn1_size, 0);
19671968
/* Add our created asn1 method to the internal list of available
19681969
* methods. */
1970+
/* Known, still-reachable memory leak from openssl internals on 1.0.2 */
19691971
EVP_PKEY_asn1_add0(we_cmac_pkey_asn1_method);
19701972
EVP_PKEY_asn1_add_alias(EVP_PKEY_CMAC, NID_wolfengine_cmac);
19711973

src/we_wolfengine.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static ENGINE *engine_wolfengine(void)
4545

4646
WOLFENGINE_ENTER(WE_LOG_ENGINE, "engine_wolfengine");
4747

48+
/* Known, still-reachable memory leak from openssl internals on 1.0.2 */
4849
ret = ENGINE_new();
4950
if (ret == NULL) {
5051
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_ENGINE, "ENGINE_new", ret);
@@ -75,6 +76,7 @@ void ENGINE_load_wolfengine(void)
7576
return;
7677
ENGINE_add(toadd);
7778
ENGINE_free(toadd);
79+
/* Known, still-reachable memory leak from openssl internals on 1.0.2 */
7880
ERR_clear_error();
7981

8082
WOLFENGINE_LEAVE(WE_LOG_ENGINE, "ENGINE_load_wolfengine", 1);

test/test_aestag.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ static int test_aes_tag_fixed(ENGINE *e, void *data, const EVP_CIPHER *cipher,
349349

350350
(void)data;
351351

352+
XMEMSET(key, 0, sizeof(key));
353+
XMEMSET(iv, 0, sizeof(iv));
354+
352355
if (RAND_bytes(key, keyLen) == 0) {
353356
err = 1;
354357
}
@@ -509,6 +512,10 @@ static int test_aes_tag_tls(ENGINE *e, void *data, const EVP_CIPHER *cipher,
509512

510513
(void)data;
511514

515+
XMEMSET(key, 0, sizeof(key));
516+
XMEMSET(iv, 0, sizeof(iv));
517+
XMEMSET(msg, 0, sizeof(msg));
518+
512519
aad[8] = 23; /* Content type */
513520
aad[9] = 3; /* Protocol major version */
514521
aad[10] = 2; /* Protocol minor version */

test/test_cipher.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ static int test_cipher_enc_dec(ENGINE *e, void *data, const EVP_CIPHER *cipher,
105105

106106
(void)data;
107107

108+
/* Must memset to make valgrind happy */
109+
XMEMSET(key, 0, sizeof(key));
110+
XMEMSET(iv, 0, sizeof(iv));
111+
108112
if (RAND_bytes(key, keyLen) != 1) {
109113
err = 1;
110114
}
@@ -218,7 +222,7 @@ static int test_stream_dec(ENGINE *e, const EVP_CIPHER *cipher,
218222
int encLen, unsigned char *dec)
219223
{
220224
int err;
221-
EVP_CIPHER_CTX *ctx;
225+
EVP_CIPHER_CTX *ctx = NULL;
222226
int dLen;
223227
int decLen;
224228
int i;
@@ -280,6 +284,12 @@ static int test_stream_enc_dec(ENGINE *e, void *data, const EVP_CIPHER *cipher,
280284

281285
(void)data;
282286

287+
/* Must memset to make valgrind happy */
288+
XMEMSET(key, 0, sizeof(key));
289+
XMEMSET(iv, 0, sizeof(iv));
290+
XMEMSET(enc, 0, sizeof(enc));
291+
XMEMSET(encExp, 0, sizeof(encExp));
292+
283293
if (RAND_bytes(key, keyLen) != 1) {
284294
printf("generate key failed\n");
285295
err = 1;
@@ -674,6 +684,9 @@ int test_aes_ctr_iv_init_regression(ENGINE *e, void *data)
674684

675685
(void)data;
676686

687+
XMEMSET(iv, 0, sizeof(iv));
688+
XMEMSET(key, 0, sizeof(key));
689+
677690
/* Generate a random IV and key. */
678691
err = RAND_bytes(iv, AES_BLOCK_SIZE) != 1;
679692
if (err == 0) {

test/test_digest.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ static int test_create_digest(const EVP_MD *md, ENGINE *e, void *data)
7979

8080
(void)data;
8181

82+
/* Must memset longMsg to make valgrind happy */
83+
XMEMSET(digest, 0, sizeof(digest));
84+
XMEMSET(longMsg, 0, sizeof(longMsg));
85+
8286
RAND_bytes(longMsg, sizeof(longMsg));
8387

8488
dLen = 0;

test/test_ecc.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ int test_ecdsa_p192_pkey(ENGINE *e, void *data)
879879

880880
(void)data;
881881

882+
XMEMSET(buf, 0, sizeof(buf));
883+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
884+
882885
err = RAND_bytes(buf, sizeof(buf)) == 0;
883886
if (err == 0) {
884887
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_192));
@@ -946,6 +949,9 @@ int test_ecdsa_p224_pkey(ENGINE *e, void *data)
946949

947950
(void)data;
948951

952+
XMEMSET(buf, 0, sizeof(buf));
953+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
954+
949955
err = RAND_bytes(buf, sizeof(buf)) == 0;
950956
if (err == 0) {
951957
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_224));
@@ -1001,6 +1007,9 @@ int test_ecdsa_p256_pkey(ENGINE *e, void *data)
10011007

10021008
(void)data;
10031009

1010+
XMEMSET(buf, 0, sizeof(buf));
1011+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
1012+
10041013
err = RAND_bytes(buf, sizeof(buf)) == 0;
10051014
if (err == 0) {
10061015
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_256));
@@ -1056,6 +1065,9 @@ int test_ecdsa_p384_pkey(ENGINE *e, void *data)
10561065

10571066
(void)data;
10581067

1068+
XMEMSET(buf, 0, sizeof(buf));
1069+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
1070+
10591071
err = RAND_bytes(buf, sizeof(buf)) == 0;
10601072
if (err == 0) {
10611073
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_384));
@@ -1111,6 +1123,8 @@ int test_ecdsa_p521_pkey(ENGINE *e, void *data)
11111123

11121124
(void)data;
11131125

1126+
XMEMSET(buf, 0, sizeof(buf));
1127+
11141128
err = RAND_bytes(buf, sizeof(buf)) == 0;
11151129
if (err == 0) {
11161130
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_521));
@@ -1166,6 +1180,9 @@ int test_ecdsa_p192(ENGINE *e, void *data)
11661180

11671181
(void)data;
11681182

1183+
XMEMSET(buf, 0, sizeof(buf));
1184+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
1185+
11691186
err = RAND_bytes(buf, sizeof(buf)) == 0;
11701187
if (err == 0) {
11711188
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_192));
@@ -1234,6 +1251,9 @@ int test_ecdsa_p224(ENGINE *e, void *data)
12341251

12351252
(void)data;
12361253

1254+
XMEMSET(buf, 0, sizeof(buf));
1255+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
1256+
12371257
err = RAND_bytes(buf, sizeof(buf)) == 0;
12381258
if (err == 0) {
12391259
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_224));
@@ -1289,6 +1309,9 @@ int test_ecdsa_p256(ENGINE *e, void *data)
12891309

12901310
(void)data;
12911311

1312+
XMEMSET(buf, 0, sizeof(buf));
1313+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
1314+
12921315
err = RAND_bytes(buf, sizeof(buf)) == 0;
12931316
if (err == 0) {
12941317
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_256));
@@ -1344,6 +1367,9 @@ int test_ecdsa_p384(ENGINE *e, void *data)
13441367

13451368
(void)data;
13461369

1370+
XMEMSET(buf, 0, sizeof(buf));
1371+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
1372+
13471373
err = RAND_bytes(buf, sizeof(buf)) == 0;
13481374
if (err == 0) {
13491375
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_384));
@@ -1399,6 +1425,9 @@ int test_ecdsa_p521(ENGINE *e, void *data)
13991425

14001426
(void)data;
14011427

1428+
XMEMSET(buf, 0, sizeof(buf));
1429+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
1430+
14021431
err = RAND_bytes(buf, sizeof(buf)) == 0;
14031432
if (err == 0) {
14041433
pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, sizeof(ecc_key_der_521));
@@ -1546,6 +1575,9 @@ int test_ec_key_ecdh_keygen(ENGINE *e, int nid, int len)
15461575
unsigned char secretA[66];
15471576
unsigned char secretB[66];
15481577

1578+
XMEMSET(secretA, 0, sizeof(secretA));
1579+
XMEMSET(secretB, 0, sizeof(secretB));
1580+
15491581
err = (group = EC_GROUP_new_by_curve_name(nid)) == NULL;
15501582
if (err == 0) {
15511583
err = (keyA = EC_KEY_new_method(e)) == NULL;
@@ -1667,6 +1699,9 @@ int test_ec_key_ecdh(ENGINE *e, const unsigned char *privKey, size_t len,
16671699
unsigned char secretB[66];
16681700
const unsigned char *p;
16691701

1702+
XMEMSET(secretA, 0, sizeof(secretA));
1703+
XMEMSET(secretB, 0, sizeof(secretB));
1704+
16701705
err = (keyA = EC_KEY_new_method(e)) == NULL;
16711706
if (err == 0) {
16721707
p = privKey;
@@ -1847,6 +1882,9 @@ int test_ec_key_ecdsa(ENGINE *e, const unsigned char *privKey,
18471882
unsigned char buf[20];
18481883
const unsigned char *p;
18491884

1885+
XMEMSET(buf, 0, sizeof(buf));
1886+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
1887+
18501888
err = RAND_bytes(buf, sizeof(buf)) == 0;
18511889
if (err == 0) {
18521890
err = (key = EC_KEY_new_method(e)) == NULL;
@@ -2032,6 +2070,8 @@ static int test_ecdh_direct(ENGINE* e, const unsigned char* keyDer,
20322070
err = 1;
20332071
}
20342072

2073+
XMEMSET(secret, 0, sizeof(secret));
2074+
20352075
p = keyDer;
20362076
peerPrivKey = keyPeerDer;
20372077

@@ -2246,6 +2286,9 @@ static int test_ecdsa_key(ENGINE *e, const unsigned char *privKey,
22462286

22472287
PRINT_MSG("ENTER: test_ecdsa");
22482288

2289+
XMEMSET(buf, 0, sizeof(buf));
2290+
XMEMSET(ecdsaSig, 0, sizeof(ecdsaSig));
2291+
22492292
err = RAND_bytes(buf, sizeof(buf)) == 0;
22502293
if (err == 0) {
22512294
p = privKey;

test/test_hkdf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ static int test_hkdf_calc(ENGINE *e, unsigned char *key, int keyLen,
3535

3636
(void)mode;
3737

38+
XMEMSET(inKey, 0, sizeof(inKey));
39+
XMEMSET(salt, 0, sizeof(salt));
40+
XMEMSET(info, 0, sizeof(info));
41+
3842
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, e);
3943
if (ctx == NULL) {
4044
err = 1;
@@ -277,6 +281,9 @@ static int test_hkdf_str_md(ENGINE *e, const char *md, const char *mode)
277281
unsigned char oKey[128];
278282
unsigned char wKey[128];
279283

284+
XMEMSET(oKey, 0, sizeof(oKey));
285+
XMEMSET(wKey, 0, sizeof(wKey));
286+
280287
PRINT_MSG("Calc with strings OpenSSL");
281288
err = test_hkdf_str_calc(NULL, oKey, sizeof(oKey), md, mode);
282289
if (err == 1) {

test/test_hmac.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ int test_hmac_create(ENGINE *e, void *data)
119119
unsigned char pswd[] = "My empire of dirt";
120120
unsigned char bigPswd[100];
121121

122+
XMEMSET(bigPswd, 0, sizeof(bigPswd));
123+
122124
PRINT_MSG("Testing with SHA1");
123125
ret = test_hmac_create_helper(e, data, EVP_sha1(), pswd, sizeof(pswd));
124126
if (ret == 0) {

0 commit comments

Comments
 (0)