]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KEYS: trusted: Fix a memory leak in tpm2_load_cmd
authorJarkko Sakkinen <jarkko@kernel.org>
Sat, 18 Oct 2025 10:30:36 +0000 (13:30 +0300)
committerJarkko Sakkinen <jarkko@kernel.org>
Sat, 29 Nov 2025 20:57:30 +0000 (22:57 +0200)
'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: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs")
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
security/keys/trusted-keys/trusted_tpm2.c

index edd7b9d7e4dce059c71cbf6433720218bc5c6dc8..91656e44b326c9d02c563bbe0b23c474491d01ba 100644 (file)
@@ -372,6 +372,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
                         struct trusted_key_options *options,
                         u32 *blob_handle)
 {
+       u8 *blob_ref __free(kfree) = NULL;
        struct tpm_buf buf;
        unsigned int private_len;
        unsigned int public_len;
@@ -385,6 +386,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
                /* old form */
                blob = payload->blob;
                payload->old_format = 1;
+       } else {
+               /* Bind for cleanup: */
+               blob_ref = blob;
        }
 
        /* new format carries keyhandle but old format doesn't */
@@ -449,8 +453,6 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
                        (__be32 *) &buf.data[TPM_HEADER_SIZE]);
 
 out:
-       if (blob != payload->blob)
-               kfree(blob);
        tpm_buf_destroy(&buf);
 
        if (rc > 0)