From 923b35b6f91b1bc54511be8b9de89690d976787d Mon Sep 17 00:00:00 2001 From: Qiucheng Wang Date: Tue, 1 Sep 2020 21:03:01 +0000 Subject: [PATCH] Clean up memory allocation and free with oe functions Signed-off-by: Qiucheng Wang --- enclave/asym_keys.c | 16 ++++++------- enclave/crypto/cert.c | 8 +++---- enclave/sgx/collateralinfo.c | 45 ++++++++++++++++++------------------ 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/enclave/asym_keys.c b/enclave/asym_keys.c index 9bca3f95d3..56db7ba14e 100644 --- a/enclave/asym_keys.c +++ b/enclave/asym_keys.c @@ -199,7 +199,7 @@ static oe_result_t _export_keypair( OE_RAISE(result); /* Call again with the allocated memory. */ - key = (uint8_t*)malloc(key_size); + key = (uint8_t*)oe_malloc(key_size); if (key == NULL) OE_RAISE(OE_OUT_OF_MEMORY); @@ -220,7 +220,7 @@ static oe_result_t _export_keypair( if (key != NULL) { oe_secure_zero_fill(key, key_size); - free(key); + oe_free(key); } return result; @@ -327,13 +327,13 @@ static oe_result_t _load_asymmetric_key_by_policy( if (key_buffer_local != NULL) { oe_secure_zero_fill(key_buffer_local, key_buffer_size_local); - free(key_buffer_local); + oe_free(key_buffer_local); } if (key_info_local != NULL) { oe_secure_zero_fill(key_info_local, key_info_size_local); - free(key_info_local); + oe_free(key_info_local); } if (key != NULL) @@ -386,13 +386,13 @@ static oe_result_t _load_asymmetric_key( if (key_buffer_local != NULL) { oe_secure_zero_fill(key_buffer_local, key_buffer_size_local); - free(key_buffer_local); + oe_free(key_buffer_local); } if (key != NULL) { oe_secure_zero_fill(key, key_size); - free(key); + oe_free(key); } return result; @@ -470,13 +470,13 @@ void oe_free_key( if (key_buffer) { oe_secure_zero_fill(key_buffer, key_buffer_size); - free(key_buffer); + oe_free(key_buffer); } if (key_info) { oe_secure_zero_fill(key_info, key_info_size); - free(key_info); + oe_free(key_info); } } diff --git a/enclave/crypto/cert.c b/enclave/crypto/cert.c index 5cbbe3243d..99863dd9a8 100644 --- a/enclave/crypto/cert.c +++ b/enclave/crypto/cert.c @@ -848,7 +848,7 @@ oe_result_t oe_cert_verify( OE_RAISE_MSG( OE_INVALID_PARAMETER, "Invalid crls parameter", NULL); - if (!(p = malloc(sizeof(mbedtls_x509_crl)))) + if (!(p = oe_malloc(sizeof(mbedtls_x509_crl)))) OE_RAISE(OE_OUT_OF_MEMORY); OE_CHECK(oe_memcpy_s( @@ -908,7 +908,7 @@ oe_result_t oe_cert_verify( for (mbedtls_x509_crl* p = crl_list; p;) { mbedtls_x509_crl* next = p->next; - free(p); + oe_free(p); p = next; } } @@ -1089,7 +1089,7 @@ oe_result_t oe_gen_custom_x509_cert( mbedtls_x509write_crt_set_subject_key(&x509cert, &subject_key); mbedtls_x509write_crt_set_issuer_key(&x509cert, &issuer_key); - if ((buff = malloc(cert_buf_size)) == NULL) + if ((buff = oe_malloc(cert_buf_size)) == NULL) OE_RAISE(OE_OUT_OF_MEMORY); /* Get the drbg object */ @@ -1194,7 +1194,7 @@ oe_result_t oe_gen_custom_x509_cert( // mbedtls_ctr_drbg_free(&ctr_drbg); mbedtls_pk_free(&issuer_key); mbedtls_pk_free(&subject_key); - free(buff); + oe_free(buff); if (ret) result = OE_CRYPTO_ERROR; diff --git a/enclave/sgx/collateralinfo.c b/enclave/sgx/collateralinfo.c index d8a1a50414..066195040d 100644 --- a/enclave/sgx/collateralinfo.c +++ b/enclave/sgx/collateralinfo.c @@ -2,6 +2,7 @@ // Licensed under the MIT License. #include +#include #include #include #include @@ -223,7 +224,7 @@ oe_result_t oe_get_sgx_quote_verification_collateral( /* tcb_info */ if (in.tcb_info_size < out.tcb_info_size) { - if (!(in.tcb_info = realloc(in.tcb_info, out.tcb_info_size))) + if (!(in.tcb_info = oe_realloc(in.tcb_info, out.tcb_info_size))) { OE_RAISE(OE_OUT_OF_MEMORY); } @@ -234,7 +235,7 @@ oe_result_t oe_get_sgx_quote_verification_collateral( /* tcb_issuer_chain */ if (in.tcb_info_issuer_chain_size < out.tcb_info_issuer_chain_size) { - if (!(in.tcb_info_issuer_chain = realloc( + if (!(in.tcb_info_issuer_chain = oe_realloc( in.tcb_info_issuer_chain, out.tcb_info_issuer_chain_size))) { @@ -247,7 +248,7 @@ oe_result_t oe_get_sgx_quote_verification_collateral( /* pck crl */ if (in.pck_crl_size < out.pck_crl_size) { - if (!(in.pck_crl = realloc(in.pck_crl, out.pck_crl_size))) + if (!(in.pck_crl = oe_realloc(in.pck_crl, out.pck_crl_size))) { OE_RAISE(OE_OUT_OF_MEMORY); } @@ -259,7 +260,7 @@ oe_result_t oe_get_sgx_quote_verification_collateral( if (in.root_ca_crl_size < out.root_ca_crl_size) { if (!(in.root_ca_crl = - realloc(in.root_ca_crl, out.root_ca_crl_size))) + oe_realloc(in.root_ca_crl, out.root_ca_crl_size))) { OE_RAISE(OE_OUT_OF_MEMORY); } @@ -270,7 +271,7 @@ oe_result_t oe_get_sgx_quote_verification_collateral( /* pck crl issuer chain */ if (in.pck_crl_issuer_chain_size < out.pck_crl_issuer_chain_size) { - if (!(in.pck_crl_issuer_chain = realloc( + if (!(in.pck_crl_issuer_chain = oe_realloc( in.pck_crl_issuer_chain, out.pck_crl_issuer_chain_size))) { OE_RAISE(OE_OUT_OF_MEMORY); @@ -283,7 +284,7 @@ oe_result_t oe_get_sgx_quote_verification_collateral( if (in.qe_identity_size < out.qe_identity_size) { if (!(in.qe_identity = - realloc(in.qe_identity, out.qe_identity_size))) + oe_realloc(in.qe_identity, out.qe_identity_size))) { OE_RAISE(OE_OUT_OF_MEMORY); } @@ -295,7 +296,7 @@ oe_result_t oe_get_sgx_quote_verification_collateral( if (in.qe_identity_issuer_chain_size < out.qe_identity_issuer_chain_size) { - if (!(in.qe_identity_issuer_chain = realloc( + if (!(in.qe_identity_issuer_chain = oe_realloc( in.qe_identity_issuer_chain, out.qe_identity_issuer_chain_size))) { @@ -337,7 +338,7 @@ void oe_prealloc_quote_verification_collateral_args( { /* Allocate estimated buffers for quote_verification_collateral_args */ - buf->tcb_info = (uint8_t*)calloc(1, default_sizes->tcb_info_size); + buf->tcb_info = (uint8_t*)oe_calloc(1, default_sizes->tcb_info_size); if (buf->tcb_info) { @@ -349,7 +350,7 @@ void oe_prealloc_quote_verification_collateral_args( } buf->tcb_info_issuer_chain = - (uint8_t*)calloc(1, default_sizes->tcb_info_issuer_chain_size); + (uint8_t*)oe_calloc(1, default_sizes->tcb_info_issuer_chain_size); if (buf->tcb_info_issuer_chain) { @@ -361,7 +362,7 @@ void oe_prealloc_quote_verification_collateral_args( goto done; } - buf->pck_crl = (uint8_t*)calloc(1, default_sizes->pck_crl_size); + buf->pck_crl = (uint8_t*)oe_calloc(1, default_sizes->pck_crl_size); if (buf->pck_crl) { @@ -372,7 +373,7 @@ void oe_prealloc_quote_verification_collateral_args( goto done; } - buf->root_ca_crl = (uint8_t*)calloc(1, default_sizes->root_ca_crl_size); + buf->root_ca_crl = (uint8_t*)oe_calloc(1, default_sizes->root_ca_crl_size); if (buf->root_ca_crl) { @@ -384,7 +385,7 @@ void oe_prealloc_quote_verification_collateral_args( } buf->pck_crl_issuer_chain = - (uint8_t*)calloc(1, default_sizes->pck_crl_issuer_chain_size); + (uint8_t*)oe_calloc(1, default_sizes->pck_crl_issuer_chain_size); if (buf->pck_crl_issuer_chain) { @@ -396,7 +397,7 @@ void oe_prealloc_quote_verification_collateral_args( goto done; } - buf->qe_identity = (uint8_t*)calloc(1, default_sizes->qe_identity_size); + buf->qe_identity = (uint8_t*)oe_calloc(1, default_sizes->qe_identity_size); if (buf->qe_identity) { @@ -408,7 +409,7 @@ void oe_prealloc_quote_verification_collateral_args( } buf->qe_identity_issuer_chain = - (uint8_t*)calloc(1, default_sizes->qe_identity_issuer_chain_size); + (uint8_t*)oe_calloc(1, default_sizes->qe_identity_issuer_chain_size); if (buf->qe_identity_issuer_chain) { @@ -460,13 +461,13 @@ void oe_free_sgx_quote_verification_collateral_args( { if (args) { - free(args->tcb_info); - free(args->tcb_info_issuer_chain); - free(args->pck_crl); - free(args->root_ca_crl); - free(args->pck_crl_issuer_chain); - free(args->qe_identity); - free(args->qe_identity_issuer_chain); - free(args->host_out_buffer); + oe_free(args->tcb_info); + oe_free(args->tcb_info_issuer_chain); + oe_free(args->pck_crl); + oe_free(args->root_ca_crl); + oe_free(args->pck_crl_issuer_chain); + oe_free(args->qe_identity); + oe_free(args->qe_identity_issuer_chain); + oe_free(args->host_out_buffer); } } \ No newline at end of file