From 3f0538eecd0f53fc4f9a09bad73418f0f058519a Mon Sep 17 00:00:00 2001 From: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com> Date: Sun, 1 Mar 2026 03:41:03 +0900 Subject: [PATCH 15/20] fix(tools): add missing null checks after malloc Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com> --- tools/tpm2_getekcertificate.c | 9 +++++++++ tools/tpm2_verifysignature.c | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/tools/tpm2_getekcertificate.c b/tools/tpm2_getekcertificate.c index b8fa5647..b824d59c 100644 --- a/tools/tpm2_getekcertificate.c +++ b/tools/tpm2_getekcertificate.c @@ -381,6 +381,11 @@ static bool retrieve_web_endorsement_certificate(char *b64h) { * rsa_cert_buffer for either RSA EK cert or ECC EK cert. */ ctx.rsa_cert_buffer = malloc(CURL_MAX_WRITE_SIZE); + if (!ctx.rsa_cert_buffer) { + LOG_ERR("oom"); + ret = false; + goto out_easy_cleanup; + } rc = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)ctx.rsa_cert_buffer); if (rc != CURLE_OK) { LOG_ERR("curl_easy_setopt for CURLOPT_WRITEDATA failed: %s", @@ -666,6 +671,10 @@ static tool_rc process_input(ESYS_CONTEXT *ectx) { if (ctx.ek_path) { ctx.out_public = malloc(sizeof(*ctx.out_public)); + if (!ctx.out_public) { + LOG_ERR("oom"); + return tool_rc_general_error; + } ctx.out_public->size = 0; bool res = files_load_public(ctx.ek_path, ctx.out_public); if (!res) { diff --git a/tools/tpm2_verifysignature.c b/tools/tpm2_verifysignature.c index e09dd48c..55e78263 100644 --- a/tools/tpm2_verifysignature.c +++ b/tools/tpm2_verifysignature.c @@ -193,6 +193,10 @@ static bool on_option(char key, char *value) { break; case 'd': { ctx.msg_hash = malloc(sizeof(TPM2B_DIGEST)); + if (!ctx.msg_hash) { + LOG_ERR("oom"); + return false; + } ctx.msg_hash->size = sizeof(ctx.msg_hash->buffer); if (!files_load_bytes_from_path(value, ctx.msg_hash->buffer, &ctx.msg_hash->size)) { -- 2.54.0