Skip to content

Commit 1f35b76

Browse files
committed
fix memory leaks
- BIGNUM must be freed both on tpm2tss_tpm2data_write and tpm2tss_tpm2data_read: definitely lost: 24 bytes in 1 blocks indirectly lost: 8 bytes in 1 blocks - TSSPRIVKEY's parent, privkey and pubkey members don't need to be allocated explicitly, because TSSPRIVKEY_new already does: definitely lost: 72 bytes in 3 blocks indirectly lost: 0 bytes in 0 blocks - TPM2_DATA* returned from RSA_get_app_data can be returned directly genkey_rsa() because RSA_free() doesn't free this item: definitely lost: 2,248 bytes in 1 blocks indirectly lost: 0 bytes in 0 blocks - update .gitignore to cover more generated files Signed-off-by: Oguzhan Turk <stkyoht@hotmail.com>
1 parent 3d01024 commit 1f35b76

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Makefile
44
Makefile.in
55
aclocal.m4
6+
ax_*.m4
7+
pkg.m4
68
aminclude_static.am
79
autom4te.cache/
810
compile
@@ -16,6 +18,7 @@ install-sh
1618
libtool
1719
libtpm2.la
1820
ltmain.sh
21+
stamp-h1
1922
m4/libtool.m4
2023
m4/ltoptions.m4
2124
m4/ltsugar.m4
@@ -26,6 +29,7 @@ src/.dirstamp
2629
src/.libs/
2730
src/*.o
2831
src/*.lo
32+
src/config.h
2933
tpm2tss-genkey
3034
libtpm2tss.la
3135
*.gcno
@@ -44,3 +48,5 @@ test/error_tpm2-tss-engine-common
4448
test/*.o
4549
config.h.in
4650
VERSION
51+
build/
52+
.vscode/

src/tpm2-tss-engine-common.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ tpm2tss_tpm2data_write(const TPM2_DATA *tpm2Data, const char *filename)
163163
goto error;
164164
}
165165
tpk->type = OBJ_txt2obj(OID_loadableKey, 1);
166-
tpk->parent = ASN1_INTEGER_new();
167-
tpk->privkey = ASN1_OCTET_STRING_new();
168-
tpk->pubkey = ASN1_OCTET_STRING_new();
169-
if (!tpk->type || !tpk->privkey || !tpk->pubkey || !tpk->parent) {
166+
if (!tpk->type) {
170167
ERR(tpm2tss_tpm2data_write, ERR_R_MALLOC_FAILURE);
171168
goto error;
172169
}
@@ -182,6 +179,8 @@ tpm2tss_tpm2data_write(const TPM2_DATA *tpm2Data, const char *filename)
182179
BN_set_word(bn_parent, TPM2_RH_OWNER);
183180
}
184181
BN_to_ASN1_INTEGER(bn_parent, tpk->parent);
182+
BN_free(bn_parent);
183+
185184
ASN1_STRING_set(tpk->privkey, &privbuf[0], privbuf_len);
186185
ASN1_STRING_set(tpk->pubkey, &pubbuf[0], pubbuf_len);
187186

@@ -195,6 +194,8 @@ tpm2tss_tpm2data_write(const TPM2_DATA *tpm2Data, const char *filename)
195194
BIO_free(bio);
196195
if (tpk)
197196
TSSPRIVKEY_free(tpk);
197+
if (bn_parent)
198+
BN_free(bn_parent);
198199
return 0;
199200
}
200201

@@ -347,7 +348,7 @@ tpm2tss_tpm2data_read(const char *filename, TPM2_DATA **tpm2Datap)
347348
TSSPRIVKEY *tpk = NULL;
348349
TPM2_DATA *tpm2Data = NULL;
349350
char type_oid[64];
350-
BIGNUM *bn_parent;
351+
BIGNUM *bn_parent = NULL;
351352

352353
if ((bio = BIO_new_file(filename, "r")) == NULL) {
353354
ERR(tpm2tss_tpm2data_read, TPM2TSS_R_FILE_READ);
@@ -382,6 +383,8 @@ tpm2tss_tpm2data_read(const char *filename, TPM2_DATA **tpm2Datap)
382383
} else {
383384
tpm2Data->parent = BN_get_word(bn_parent);
384385
}
386+
BN_free(bn_parent);
387+
385388
if (tpm2Data->parent == 0)
386389
tpm2Data->parent = TPM2_RH_OWNER;
387390

@@ -415,6 +418,8 @@ tpm2tss_tpm2data_read(const char *filename, TPM2_DATA **tpm2Datap)
415418
BIO_free(bio);
416419
if (tpk)
417420
TSSPRIVKEY_free(tpk);
421+
if (bn_parent)
422+
BN_free(bn_parent);
418423

419424
return 0;
420425
}

src/tpm2tss-genkey.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,7 @@ genkey_rsa()
265265

266266
VERB("Key generated\n");
267267

268-
TPM2_DATA *tpm2Data = OPENSSL_malloc(sizeof(*tpm2Data));
269-
if (tpm2Data == NULL) {
270-
ERR("out of memory\n");
271-
BN_free(e);
272-
RSA_free(rsa);
273-
return NULL;
274-
}
275-
memcpy(tpm2Data, RSA_get_app_data(rsa), sizeof(*tpm2Data));
268+
TPM2_DATA *tpm2Data = RSA_get_app_data(rsa);
276269

277270
BN_free(e);
278271
RSA_free(rsa);

0 commit comments

Comments
 (0)