Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KEYS: trusted: Fix memory leak on object td

Two error return paths are neglecting to free allocated object td,
causing a memory leak. Fix this by returning via the error return
path that securely kfree's td.

Fixes clang scan-build warning:
security/keys/trusted-keys/trusted_tpm1.c:496:10: warning: Potential
memory leak [unix.Malloc]

Cc: stable@vger.kernel.org
Fixes: 5df16caada3f ("KEYS: trusted: Fix incorrect handling of tpm_get_random()")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>

authored by

Colin Ian King and committed by
Jarkko Sakkinen
83a775d5 dbb5afad

+5 -3
+5 -3
security/keys/trusted-keys/trusted_tpm1.c
··· 493 493 494 494 ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE); 495 495 if (ret < 0) 496 - return ret; 496 + goto out; 497 497 498 - if (ret != TPM_NONCE_SIZE) 499 - return -EIO; 498 + if (ret != TPM_NONCE_SIZE) { 499 + ret = -EIO; 500 + goto out; 501 + } 500 502 501 503 ordinal = htonl(TPM_ORD_SEAL); 502 504 datsize = htonl(datalen);