From 707a9046cd6d572fdcdcb000b13ffbcb81cdfb27 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Fri, 15 Jun 2018 17:41:33 -0400 Subject: [PATCH] Use malloc/free rather than TPM_Malloc/TPM_Free in library code Signed-off-by: Stefan Berger --- src/tpm_library.c | 22 +++++++++++++++------- src/tpm_library_intern.h | 2 ++ src/tpm_tpm12_interface.c | 13 +++++++++++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/tpm_library.c b/src/tpm_library.c index 30362841..57c979e4 100644 --- a/src/tpm_library.c +++ b/src/tpm_library.c @@ -240,7 +240,6 @@ static unsigned char *TPMLIB_OpenSSL_Base64Decode(char *input, BIO *b64, *bmem; unsigned char *res = NULL; int n; - TPM_RESULT rc; b64 = BIO_new(BIO_f_base64()); if (!b64) { @@ -255,14 +254,15 @@ static unsigned char *TPMLIB_OpenSSL_Base64Decode(char *input, bmem = BIO_push(b64, bmem); BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL); - rc = TPM_Malloc(&res, outputlen); - if (rc != TPM_SUCCESS) { + res = malloc(outputlen); + if (!res) { + TPMLIB_LogError("Could not allocate %u bytes.\n", outputlen); goto cleanup; } n = BIO_read(bmem, res, outputlen); if (n <= 0) { - TPM_Free(res); + free(res); res = NULL; goto cleanup; } @@ -296,8 +296,12 @@ static unsigned char *TPMLIB_Base64Decode(const char *start, const char *end, end++; - if (TPM_Malloc((unsigned char **)&input, end - start + 1) != TPM_SUCCESS) + input = malloc(end - start + 1); + if (!input) { + TPMLIB_LogError("Could not allocate %u bytes.\n", + (unsigned int)(end - start + 1)); return NULL; + } /* copy from source string skipping '\n' and '\r' and using '=' to calculate the exact length */ @@ -537,9 +541,13 @@ TPM_RESULT CopyCachedState(enum TPMLIB_StateType st, *is_empty_buffer = (*buflen == BUFLEN_EMPTY_BUFFER); if (cached_blobs[st].buffer) { - ret = TPM_Malloc(buffer, *buflen); - if (ret == TPM_SUCCESS) + *buffer = malloc(*buflen); + if (!*buffer) { + TPMLIB_LogError("Could not allocate %u bytes.\n", *buflen); + ret = TPM_SIZE; + } else { memcpy(*buffer, cached_blobs[st].buffer, *buflen); + } } else { *buffer = NULL; } diff --git a/src/tpm_library_intern.h b/src/tpm_library_intern.h index af4dc446..171036c2 100644 --- a/src/tpm_library_intern.h +++ b/src/tpm_library_intern.h @@ -90,6 +90,8 @@ uint32_t TPM12_GetBufferSize(void); int TPMLIB_LogPrintf(const char *format, ...); void TPMLIB_LogPrintfA(unsigned int indent, const char *format, ...); +#define TPMLIB_LogError(format, ...) \ + TPMLIB_LogPrintfA(~0, "libtpms: "format, __VA_ARGS__) #define TPMLIB_LogTPM12Error(format, ...) \ TPMLIB_LogPrintfA(~0, "libtpms/tpm12: "format, __VA_ARGS__) diff --git a/src/tpm_tpm12_interface.c b/src/tpm_tpm12_interface.c index 59b32957..dbd1ed51 100644 --- a/src/tpm_tpm12_interface.c +++ b/src/tpm_tpm12_interface.c @@ -445,14 +445,23 @@ TPM_RESULT TPM12_SetState(enum TPMLIB_StateType st, return TPM_INVALID_POSTINIT; if (ret == TPM_SUCCESS) { - ret = TPM_Malloc((unsigned char **)&stream, buflen); + stream = malloc(buflen); + if (!stream) { + TPMLIB_LogError("Could not allocate %u bytes.\n", buflen); + ret = TPM_SIZE; + } } if (ret == TPM_SUCCESS) { orig_stream = stream; memcpy(stream, buffer, buflen); - ret = TPM_Malloc((unsigned char **)&tpm_state, sizeof(tpm_state_t)); + tpm_state = malloc(sizeof(tpm_state_t)); + if (!tpm_state) { + TPMLIB_LogError("Could not allocated %zu bytes.\n", + sizeof(tpm_state_t)); + ret = TPM_SIZE; + } } if (ret == TPM_SUCCESS) {