Skip to content

Commit 034bb41

Browse files
fix(cipher): resolve allocator inconsistency for IV buffers
In tpm2-provider-cipher, cctx->ivector is allocated using OPENSSL_zalloc and deallocated using OPENSSL_clear_free(). During tpm2_cipher_process_buffer/update_stream(), the IV buffer cctx->ivector will be replaced with the IV buffer ivector allocated in encrypt_decrypt(); this may happen (de)allocator inconsistency. tpm2-tss uses the standard malloc/free(), while OpenSSL uses the OPENSSL_malloc/free(); the latter may use custom (de)allocator set via CRYPTO_set_mem_functions(). This commit resolves this potential malloc/free inconsistency. Co-authored-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com> Co-authored-by: Petr Gotthard <petr.gotthard@centrum.cz>
1 parent 7ec301c commit 034bb41

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/tpm2-provider-cipher.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <tss2/tss2_mu.h>
1111

1212
#include "tpm2-provider-pkey.h"
13+
#include "tpm2-provider-types.h"
1314

1415
typedef struct tpm2_cipher_ctx_st TPM2_CIPHER_CTX;
1516

@@ -54,7 +55,7 @@ tpm2_cipher_all_newctx(void *provctx,
5455
cctx->algorithm = algdef;
5556
cctx->block_size = block_bits/8;
5657
cctx->padding = 1;
57-
cctx->ivector = OPENSSL_zalloc(sizeof(TPM2B_IV));
58+
cctx->ivector = calloc(1, sizeof(TPM2B_IV));
5859
if (cctx->ivector == NULL) {
5960
OPENSSL_clear_free(cctx, sizeof(TPM2_CIPHER_CTX));
6061
return NULL;
@@ -90,7 +91,7 @@ tpm2_cipher_freectx(void *ctx)
9091
return;
9192

9293
tpm2_esys_flush_context(cctx->esys_lock, cctx->esys_ctx, cctx->object);
93-
OPENSSL_clear_free(cctx->ivector, sizeof(TPM2B_IV));
94+
cleanse_free(cctx->ivector, sizeof(TPM2B_IV));
9495

9596
OPENSSL_clear_free(cctx, sizeof(TPM2_CIPHER_CTX));
9697
}
@@ -259,7 +260,7 @@ tpm2_cipher_process_buffer(TPM2_CIPHER_CTX *cctx, int padded,
259260
r = encrypt_decrypt(cctx, &outbuff, &ivector);
260261
TPM2_CHECK_RC(cctx->core, r, TPM2_ERR_CANNOT_ENCRYPT, return 0);
261262

262-
OPENSSL_clear_free(cctx->ivector, sizeof(TPM2B_IV));
263+
cleanse_free(cctx->ivector, sizeof(TPM2B_IV));
263264
cctx->ivector = ivector;
264265

265266
cctx->buffer.size = 0;
@@ -388,7 +389,7 @@ tpm2_cipher_update_stream(void *ctx,
388389
r = encrypt_decrypt(cctx, &outbuff, &ivector);
389390
TPM2_CHECK_RC(cctx->core, r, TPM2_ERR_CANNOT_ENCRYPT, return 0);
390391

391-
OPENSSL_clear_free(cctx->ivector, sizeof(TPM2B_IV));
392+
cleanse_free(cctx->ivector, sizeof(TPM2B_IV));
392393
cctx->ivector = ivector;
393394

394395
if (outbuff->size < consume

0 commit comments

Comments
 (0)