mirror of
https://github.com/stefanberger/swtpm.git
synced 2026-02-03 19:58:33 +00:00
swtpm: get rid of TPM_Malloc/TPM_Free/TPM_Realloc
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
This commit is contained in:
parent
27bf9db67e
commit
d2cd1d6e4e
@ -191,7 +191,7 @@ static int ctrlchannel_return_state(ptm_getstate *pgs, int fd)
|
||||
fd = -1;
|
||||
}
|
||||
|
||||
TPM_Free(blob);
|
||||
free(blob);
|
||||
|
||||
return fd;
|
||||
}
|
||||
@ -207,9 +207,13 @@ static int ctrlchannel_receive_state(ptm_setstate *pss, ssize_t n, int fd)
|
||||
uint32_t flags = be32toh(pss->u.req.state_flags);
|
||||
TPM_BOOL is_encrypted = (flags & PTM_STATE_FLAG_ENCRYPTED) != 0;
|
||||
|
||||
res = TPM_Malloc(&blob, blob_length);
|
||||
if (res)
|
||||
blob = malloc(blob_length);
|
||||
if (!blob) {
|
||||
logprintf(STDERR_FILENO,
|
||||
"Could not allocated %u bytes.\n", blob_length);
|
||||
res = TPM_FAIL;
|
||||
goto err_send_resp;
|
||||
}
|
||||
|
||||
n -= offsetof(ptm_setstate, u.req.data);
|
||||
/* n holds the number of available data bytes */
|
||||
@ -253,7 +257,7 @@ err_send_resp:
|
||||
|
||||
err_fd_broken:
|
||||
|
||||
TPM_Free(blob);
|
||||
free(blob);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
@ -260,7 +260,7 @@ static bool cached_stateblob_is_loaded(uint32_t blobtype,
|
||||
*/
|
||||
static void cached_stateblob_free(void)
|
||||
{
|
||||
TPM_Free(cached_stateblob.data);
|
||||
free(cached_stateblob.data);
|
||||
cached_stateblob.data = NULL;
|
||||
cached_stateblob.data_length = 0;
|
||||
}
|
||||
@ -604,10 +604,11 @@ ptm_set_stateblob_append(uint32_t blobtype,
|
||||
{
|
||||
TPM_RESULT res = 0;
|
||||
static struct stateblob stateblob;
|
||||
unsigned char *tmp;
|
||||
|
||||
if (stateblob.type != blobtype) {
|
||||
/* new blob; clear old data */
|
||||
TPM_Free(stateblob.data);
|
||||
free(stateblob.data);
|
||||
stateblob.data = NULL;
|
||||
stateblob.length = 0;
|
||||
stateblob.type = blobtype;
|
||||
@ -622,16 +623,19 @@ ptm_set_stateblob_append(uint32_t blobtype,
|
||||
}
|
||||
|
||||
/* append */
|
||||
res = TPM_Realloc(&stateblob.data, stateblob.length + length);
|
||||
if (res != 0) {
|
||||
tmp = realloc(stateblob.data, stateblob.length + length);
|
||||
if (!tmp) {
|
||||
logprintf(STDERR_FILENO,
|
||||
"Could not allocate %u bytes.\n", stateblob.length + length);
|
||||
/* error */
|
||||
TPM_Free(stateblob.data);
|
||||
free(stateblob.data);
|
||||
stateblob.data = NULL;
|
||||
stateblob.length = 0;
|
||||
stateblob.type = 0;
|
||||
|
||||
return res;
|
||||
}
|
||||
return TPM_FAIL;
|
||||
} else
|
||||
stateblob.data = tmp;
|
||||
|
||||
memcpy(&stateblob.data[stateblob.length], data, length);
|
||||
stateblob.length += length;
|
||||
@ -647,7 +651,7 @@ ptm_set_stateblob_append(uint32_t blobtype,
|
||||
0 /* tpm_number */,
|
||||
blobtype);
|
||||
|
||||
TPM_Free(stateblob.data);
|
||||
free(stateblob.data);
|
||||
stateblob.data = NULL;
|
||||
stateblob.length = 0;
|
||||
stateblob.type = 0;
|
||||
@ -1001,7 +1005,7 @@ static void ptm_ioctl(fuse_req_t req, int cmd, void *arg,
|
||||
|
||||
tpm_running = false;
|
||||
|
||||
TPM_Free(ptm_response);
|
||||
free(ptm_response);
|
||||
ptm_response = NULL;
|
||||
|
||||
fuse_reply_ioctl(req, 0, &res, sizeof(res));
|
||||
@ -1014,7 +1018,7 @@ static void ptm_ioctl(fuse_req_t req, int cmd, void *arg,
|
||||
res = TPM_SUCCESS;
|
||||
TPMLIB_Terminate();
|
||||
|
||||
TPM_Free(ptm_response);
|
||||
free(ptm_response);
|
||||
ptm_response = NULL;
|
||||
|
||||
fuse_reply_ioctl(req, 0, &res, sizeof(res));
|
||||
|
||||
@ -44,6 +44,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <poll.h>
|
||||
#include <sys/stat.h>
|
||||
@ -108,11 +109,11 @@ int mainLoop(struct mainLoopParams *mlp,
|
||||
|
||||
max_command_length = tpmlib_get_tpm_property(TPMPROP_TPM_BUFFER_MAX);
|
||||
|
||||
rc = TPM_Malloc(&command, max_command_length);
|
||||
if (rc != TPM_SUCCESS) {
|
||||
command = malloc(max_command_length);
|
||||
if (!command) {
|
||||
logprintf(STDERR_FILENO, "Could not allocate %u bytes for buffer.\n",
|
||||
max_command_length);
|
||||
return rc;
|
||||
return TPM_FAIL;
|
||||
}
|
||||
|
||||
connection_fd.fd = -1;
|
||||
@ -261,8 +262,8 @@ skip_process:
|
||||
break;
|
||||
}
|
||||
|
||||
TPM_Free(rbuffer);
|
||||
TPM_Free(command);
|
||||
free(rbuffer);
|
||||
free(command);
|
||||
|
||||
if (ctrlclntfd >= 0)
|
||||
close(ctrlclntfd);
|
||||
|
||||
@ -50,6 +50,7 @@
|
||||
#include <libtpms/tpm_memory.h>
|
||||
|
||||
#include "swtpm_aes.h"
|
||||
#include "logging.h"
|
||||
|
||||
#define printf(X ...)
|
||||
|
||||
@ -85,11 +86,21 @@ TPM_RESULT TPM_SymmetricKeyData_Encrypt(unsigned char **encrypt_data, /* outpu
|
||||
printf(" TPM_SymmetricKeyData_Encrypt: Padded length %u pad length %u\n",
|
||||
*encrypt_length, pad_length);
|
||||
/* allocate memory for the encrypted response */
|
||||
rc = TPM_Malloc(encrypt_data, *encrypt_length);
|
||||
*encrypt_data = malloc(*encrypt_length);
|
||||
if (!*encrypt_data) {
|
||||
logprintf(STDERR_FILENO,
|
||||
"Could not allocated %u bytes.\n", *encrypt_length);
|
||||
rc = TPM_SIZE;
|
||||
}
|
||||
}
|
||||
/* allocate memory for the padded decrypted data */
|
||||
if (rc == 0) {
|
||||
rc = TPM_Malloc(&decrypt_data_pad, *encrypt_length);
|
||||
decrypt_data_pad = malloc(*encrypt_length);
|
||||
if (!decrypt_data_pad) {
|
||||
logprintf(STDERR_FILENO,
|
||||
"Could not allocated %u bytes.\n", *encrypt_length);
|
||||
rc = TPM_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
if (rc == 0) {
|
||||
@ -156,7 +167,12 @@ TPM_RESULT TPM_SymmetricKeyData_Decrypt(unsigned char **decrypt_data, /* outpu
|
||||
}
|
||||
/* allocate memory for the padded decrypted data */
|
||||
if (rc == 0) {
|
||||
rc = TPM_Malloc(decrypt_data, encrypt_length);
|
||||
*decrypt_data = malloc(encrypt_length);
|
||||
if (!*decrypt_data) {
|
||||
logprintf(STDERR_FILENO,
|
||||
"Could not allocated %u bytes.\n", encrypt_length);
|
||||
rc = TPM_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
if (rc == 0) {
|
||||
|
||||
@ -300,8 +300,8 @@ SWTPM_NVRAM_LoadData(unsigned char **data, /* freed by caller */
|
||||
/* allocate a buffer for the actual data */
|
||||
if ((rc == 0) && *length != 0) {
|
||||
TPM_DEBUG(" SWTPM_NVRAM_LoadData: Reading %u bytes of data\n", *length);
|
||||
rc = TPM_Malloc(data, *length);
|
||||
if (rc != 0) {
|
||||
*data = malloc(*length);
|
||||
if (!*data) {
|
||||
logprintf(STDERR_FILENO,
|
||||
"SWTPM_NVRAM_LoadData: Error (fatal) allocating %u "
|
||||
"bytes\n", *length);
|
||||
@ -360,7 +360,7 @@ SWTPM_NVRAM_LoadData(unsigned char **data, /* freed by caller */
|
||||
TPM_DEBUG(" SWTPM_NVRAM_LoadData: Decrypted %u bytes of "
|
||||
"data to %u bytes.\n",
|
||||
*length, decrypt_length);
|
||||
TPM_Free(*data);
|
||||
free(*data);
|
||||
*data = decrypt_data;
|
||||
*length = decrypt_length;
|
||||
}
|
||||
@ -481,7 +481,7 @@ SWTPM_NVRAM_StoreData_Intern(const unsigned char *data,
|
||||
}
|
||||
|
||||
tlv_data_free(td, td_len);
|
||||
TPM_Free(filedata);
|
||||
free(filedata);
|
||||
|
||||
TPM_DEBUG(" SWTPM_NVRAM_StoreData: rc=%d\n", rc);
|
||||
|
||||
@ -578,7 +578,7 @@ TPM_RESULT SWTPM_NVRAM_Store_Volatile(void)
|
||||
rc = SWTPM_NVRAM_StoreData(buffer, buflen, tpm_number, name);
|
||||
}
|
||||
|
||||
TPM_Free(buffer);
|
||||
free(buffer);
|
||||
|
||||
return rc;
|
||||
}
|
||||
@ -673,11 +673,15 @@ SWTPM_CalcHMAC(const unsigned char *in, uint32_t in_length,
|
||||
return TPM_FAIL;
|
||||
}
|
||||
|
||||
rc = TPM_Malloc(&buffer, md_len);
|
||||
buffer = malloc(md_len);
|
||||
|
||||
if (rc == TPM_SUCCESS) {
|
||||
if (buffer) {
|
||||
*td = TLV_DATA(TAG_HMAC, md_len, buffer);
|
||||
memcpy(buffer, md, md_len);
|
||||
} else {
|
||||
logprintf(STDOUT_FILENO,
|
||||
"Could not allocate %u bytes.\n", md_len);
|
||||
rc = TPM_FAIL;
|
||||
}
|
||||
|
||||
return rc;
|
||||
@ -756,11 +760,15 @@ SWTPM_CheckHash(const unsigned char *in, uint32_t in_length,
|
||||
}
|
||||
|
||||
if (rc == 0) {
|
||||
rc = TPM_Malloc(&dest, data_length);
|
||||
if (rc == 0) {
|
||||
dest = malloc(data_length);
|
||||
if (dest) {
|
||||
*out = dest;
|
||||
*out_length = data_length;
|
||||
memcpy(dest, data, data_length);
|
||||
} else {
|
||||
logprintf(STDOUT_FILENO,
|
||||
"Could not allocated %u bytes.\n", data_length);
|
||||
rc = TPM_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -861,7 +869,7 @@ SWTPM_NVRAM_DecryptData(const encryptionkey *key,
|
||||
default:
|
||||
rc = TPM_FAIL;
|
||||
}
|
||||
TPM_Free(tmp_data);
|
||||
free(tmp_data);
|
||||
}
|
||||
}
|
||||
|
||||
@ -879,10 +887,14 @@ SWTPM_NVRAM_GetPlainData(unsigned char **plain, uint32_t *plain_length,
|
||||
|
||||
switch (hdrversion) {
|
||||
case 1:
|
||||
rc = TPM_Malloc(plain, length);
|
||||
if (rc == 0) {
|
||||
*plain = malloc(length);
|
||||
if (*plain) {
|
||||
memcpy(*plain, data, length);
|
||||
*plain_length = length;
|
||||
} else {
|
||||
logprintf(STDERR_FILENO,
|
||||
"Could not allocate %u bytes.\n", length);
|
||||
rc = TPM_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
@ -893,10 +905,14 @@ SWTPM_NVRAM_GetPlainData(unsigned char **plain, uint32_t *plain_length,
|
||||
rc = TPM_FAIL;
|
||||
break;
|
||||
}
|
||||
rc = TPM_Malloc(plain, td->tlv.length);
|
||||
if (rc == 0) {
|
||||
*plain = malloc(td->tlv.length);
|
||||
if (*plain) {
|
||||
memcpy(*plain, td->u.const_ptr, td->tlv.length);
|
||||
*plain_length = td->tlv.length;
|
||||
} else {
|
||||
logprintf(STDERR_FILENO,
|
||||
"Could not allocate %u bytes.\n", td->tlv.length);
|
||||
rc = TPM_FAIL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -959,22 +975,26 @@ SWTPM_NVRAM_PrependHeader(unsigned char **data, uint32_t *length,
|
||||
};
|
||||
TPM_RESULT res;
|
||||
|
||||
res = TPM_Malloc(&out, out_len);
|
||||
if (res != TPM_SUCCESS)
|
||||
out = malloc(out_len);
|
||||
if (!out) {
|
||||
logprintf(STDERR_FILENO,
|
||||
"Could not allocate %u bytes.\n", out_len);
|
||||
res = TPM_FAIL;
|
||||
goto error;
|
||||
}
|
||||
|
||||
memcpy(out, &bh, sizeof(bh));
|
||||
memcpy(&out[sizeof(bh)], *data, *length);
|
||||
|
||||
TPM_Free(*data);
|
||||
free(*data);
|
||||
|
||||
*data = out;
|
||||
*length = out_len;
|
||||
|
||||
return res;
|
||||
return TPM_SUCCESS;
|
||||
|
||||
error:
|
||||
TPM_Free(*data);
|
||||
free(*data);
|
||||
*data = NULL;
|
||||
*length = 0;
|
||||
|
||||
@ -1102,8 +1122,8 @@ TPM_RESULT SWTPM_NVRAM_GetStateBlob(unsigned char **data,
|
||||
|
||||
err_exit:
|
||||
tlv_data_free(td, td_len);
|
||||
TPM_Free(buffer);
|
||||
TPM_Free(plain);
|
||||
free(buffer);
|
||||
free(plain);
|
||||
|
||||
return res;
|
||||
}
|
||||
@ -1215,10 +1235,10 @@ TPM_RESULT SWTPM_NVRAM_SetStateBlob(unsigned char *data,
|
||||
/* SetState will make a copy of the buffer */
|
||||
res = TPMLIB_SetState(st, plain, plain_len);
|
||||
|
||||
TPM_Free(plain);
|
||||
free(plain);
|
||||
|
||||
cleanup:
|
||||
TPM_Free(mig_decrypt);
|
||||
free(mig_decrypt);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -38,11 +38,13 @@
|
||||
#include "config.h"
|
||||
|
||||
#include "tlv.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <libtpms/tpm_library.h>
|
||||
#include <libtpms/tpm_memory.h>
|
||||
#include <libtpms/tpm_error.h>
|
||||
|
||||
void
|
||||
tlv_data_free(tlv_data *td, size_t td_len)
|
||||
@ -51,7 +53,7 @@ tlv_data_free(tlv_data *td, size_t td_len)
|
||||
|
||||
for (i = 0; i < td_len; i++) {
|
||||
if (!td[i].is_const_ptr)
|
||||
TPM_Free(td[i].u.ptr);
|
||||
free(td[i].u.ptr);
|
||||
memset(&td[i], 0, sizeof(*td));
|
||||
}
|
||||
}
|
||||
@ -67,12 +69,12 @@ TPM_RESULT
|
||||
tlv_data_append(unsigned char **buffer, uint32_t *buffer_len,
|
||||
tlv_data *td, size_t td_len)
|
||||
{
|
||||
TPM_RESULT res;
|
||||
size_t i;
|
||||
tlv_header tlv;
|
||||
uint32_t totlen;
|
||||
size_t addlen = 0;
|
||||
unsigned char *ptr;
|
||||
unsigned char *tmp;
|
||||
|
||||
for (i = 0; i < td_len; i++)
|
||||
addlen += sizeof(tlv) + td[i].tlv.length;
|
||||
@ -82,9 +84,12 @@ tlv_data_append(unsigned char **buffer, uint32_t *buffer_len,
|
||||
else
|
||||
totlen = addlen;
|
||||
|
||||
res = TPM_Realloc(buffer, totlen);
|
||||
if (res)
|
||||
return res;
|
||||
tmp = realloc(*buffer, totlen);
|
||||
if (!tmp) {
|
||||
logprintf(STDERR_FILENO, "Could not allocate %u bytes.\n", totlen);
|
||||
return TPM_FAIL;
|
||||
}
|
||||
*buffer = tmp;
|
||||
|
||||
ptr = *buffer + *buffer_len;
|
||||
*buffer_len = totlen;
|
||||
|
||||
@ -40,6 +40,7 @@
|
||||
#include <assert.h>
|
||||
#include <endian.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <libtpms/tpm_library.h>
|
||||
#include <libtpms/tpm_error.h>
|
||||
@ -170,7 +171,7 @@ TPM_RESULT tpmlib_TpmEstablished_Reset(TPM_MODIFIER_INDICATOR *g_locality,
|
||||
res = be32toh(tpmrh->errcode);
|
||||
}
|
||||
*g_locality = orig_locality;
|
||||
TPM_Free(rbuffer);
|
||||
free(rbuffer);
|
||||
|
||||
return res;
|
||||
}
|
||||
@ -188,7 +189,9 @@ static void tpmlib_write_error_response(unsigned char **rbuffer,
|
||||
|
||||
if (*rbuffer == NULL ||
|
||||
*rTotal < sizeof(errresp)) {
|
||||
TPM_Realloc(rbuffer, sizeof(errresp));
|
||||
free(*rbuffer);
|
||||
|
||||
*rbuffer = malloc(sizeof(errresp));
|
||||
if (*rbuffer)
|
||||
*rTotal = sizeof(errresp);
|
||||
else
|
||||
|
||||
Loading…
Reference in New Issue
Block a user