Skip to content

Commit

Permalink
Clean up memory allocation and free with oe functions
Browse files Browse the repository at this point in the history
Signed-off-by: Qiucheng Wang <[email protected]>
  • Loading branch information
qiucwang committed Sep 2, 2020
1 parent 750eeeb commit 923b35b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
16 changes: 8 additions & 8 deletions enclave/asym_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions enclave/crypto/cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;

Expand Down
45 changes: 23 additions & 22 deletions enclave/sgx/collateralinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

#include <openenclave/corelibc/stdio.h>
#include <openenclave/corelibc/stdlib.h>
#include <openenclave/enclave.h>
#include <openenclave/internal/calls.h>
#include <openenclave/internal/raise.h>
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)))
{
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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)))
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 923b35b

Please sign in to comment.