From: Colin King <hidden> Date: 2021-07-23 17:21:34
From: Colin Ian King <redacted>
There are several error return paths that don't kfree the allocated
blob, leading to memory leaks. Ensure blob is initialized to null as
some of the error return paths in function tpm2_key_decode do not
change blob. Add an error return path to kfree blob and use this on
the current leaky returns.
Addresses-Coverity: ("Resource leak")
Fixes: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs")
Signed-off-by: Colin Ian King <redacted>
---
security/keys/trusted-keys/trusted_tpm2.c | 30 ++++++++++++++++-------
1 file changed, 21 insertions(+), 9 deletions(-)
@@ -366,7 +366,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,unsignedintprivate_len;unsignedintpublic_len;unsignedintblob_len;-u8*blob,*pub;+u8*blob=NULL,*pub;intrc;u32attrs;
@@ -378,22 +378,30 @@ static int tpm2_load_cmd(struct tpm_chip *chip,}/* new format carries keyhandle but old format doesn't */-if(!options->keyhandle)-return-EINVAL;+if(!options->keyhandle){+rc=-EINVAL;+gotoerr;+}/* must be big enough for at least the two be16 size counts */-if(payload->blob_len<4)-return-EINVAL;+if(payload->blob_len<4){+rc=-EINVAL;+gotoerr;+}private_len=get_unaligned_be16(blob);/* must be big enough for following public_len */-if(private_len+2+2>(payload->blob_len))-return-E2BIG;+if(private_len+2+2>(payload->blob_len)){+rc=-E2BIG;+gotoerr;+}public_len=get_unaligned_be16(blob+2+private_len);-if(private_len+2+public_len+2>payload->blob_len)-return-E2BIG;+if(private_len+2+public_len+2>payload->blob_len){+rc=-E2BIG;+gotoerr;+}pub=blob+2+private_len+2;/* key attributes are always at offset 4 */
@@ -441,6 +449,10 @@ static int tpm2_load_cmd(struct tpm_chip *chip,rc=-EPERM;returnrc;++err:+kfree(blob);+returnrc;}/**
Hi Colin,
On Fri, 23 Jul 2021 at 22:51, Colin King [off-list ref] wrote:
From: Colin Ian King <redacted>
There are several error return paths that don't kfree the allocated
blob, leading to memory leaks. Ensure blob is initialized to null as
some of the error return paths in function tpm2_key_decode do not
change blob. Add an error return path to kfree blob and use this on
the current leaky returns.
It looks like there are still leaky return paths left such as
tpm_buf_init() failure etc. which needs to be fixed as well.
With that addressed, feel free to add:
Acked-by: Sumit Garg <redacted>
-Sumit
quoted hunk
Addresses-Coverity: ("Resource leak")
Fixes: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs")
Signed-off-by: Colin Ian King <redacted>
---
security/keys/trusted-keys/trusted_tpm2.c | 30 ++++++++++++++++-------
1 file changed, 21 insertions(+), 9 deletions(-)
@@ -366,7 +366,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,unsignedintprivate_len;unsignedintpublic_len;unsignedintblob_len;-u8*blob,*pub;+u8*blob=NULL,*pub;intrc;u32attrs;
@@ -378,22 +378,30 @@ static int tpm2_load_cmd(struct tpm_chip *chip,}/* new format carries keyhandle but old format doesn't */-if(!options->keyhandle)-return-EINVAL;+if(!options->keyhandle){+rc=-EINVAL;+gotoerr;+}/* must be big enough for at least the two be16 size counts */-if(payload->blob_len<4)-return-EINVAL;+if(payload->blob_len<4){+rc=-EINVAL;+gotoerr;+}private_len=get_unaligned_be16(blob);/* must be big enough for following public_len */-if(private_len+2+2>(payload->blob_len))-return-E2BIG;+if(private_len+2+2>(payload->blob_len)){+rc=-E2BIG;+gotoerr;+}public_len=get_unaligned_be16(blob+2+private_len);-if(private_len+2+public_len+2>payload->blob_len)-return-E2BIG;+if(private_len+2+public_len+2>payload->blob_len){+rc=-E2BIG;+gotoerr;+}pub=blob+2+private_len+2;/* key attributes are always at offset 4 */
@@ -441,6 +449,10 @@ static int tpm2_load_cmd(struct tpm_chip *chip,rc=-EPERM;returnrc;++err:+kfree(blob);+returnrc;}/**--
From: Jarkko Sakkinen <jarkko@kernel.org> Date: 2021-07-27 03:06:03
On Fri, Jul 23, 2021 at 06:21:21PM +0100, Colin King wrote:
From: Colin Ian King <redacted>
There are several error return paths that don't kfree the allocated
blob, leading to memory leaks. Ensure blob is initialized to null as
some of the error return paths in function tpm2_key_decode do not
change blob. Add an error return path to kfree blob and use this on
the current leaky returns.
Addresses-Coverity: ("Resource leak")
Fixes: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs")
Signed-off-by: Colin Ian King <redacted>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Probably makes sense (for me) to add also
Cc: stable@vger.kernel.org
?
/Jarkko