Skip to content

Commit 62cd5d4

Browse files
committed
KEYS: trusted: Fix a memory leak in tpm2_load_cmd
'tpm2_load_cmd' allocates a tempoary blob indirectly via 'tpm2_key_decode' but it is not freed in the failure paths. Address this by wrapping the blob into with a cleanup helper. Cc: stable@vger.kernel.org # v5.13+ Fixes: f221974 ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs") Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
1 parent 127fa2a commit 62cd5d4

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

security/keys/trusted-keys/trusted_tpm2.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
372372
struct trusted_key_options *options,
373373
u32 *blob_handle)
374374
{
375+
u8 *blob_ref __free(kfree) = NULL;
375376
struct tpm_buf buf;
376377
unsigned int private_len;
377378
unsigned int public_len;
@@ -385,6 +386,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
385386
/* old form */
386387
blob = payload->blob;
387388
payload->old_format = 1;
389+
} else {
390+
/* Bind for cleanup: */
391+
blob_ref = blob;
388392
}
389393

390394
/* new format carries keyhandle but old format doesn't */
@@ -449,8 +453,6 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
449453
(__be32 *) &buf.data[TPM_HEADER_SIZE]);
450454

451455
out:
452-
if (blob != payload->blob)
453-
kfree(blob);
454456
tpm_buf_destroy(&buf);
455457

456458
if (rc > 0)

0 commit comments

Comments
 (0)