diff mbox series

[v2] KEYS: trusted: Fix memory leak in tpm2_key_encode()

Message ID 20220608131732.550234-1-niejianglei2021@163.com (mailing list archive)
State Handled Elsewhere
Headers show
Series [v2] KEYS: trusted: Fix memory leak in tpm2_key_encode() | expand

Commit Message

Jianglei Nie June 8, 2022, 1:17 p.m. UTC
tpm2_key_encode() allocates a memory chunk from scratch with kmalloc(),
but it is never freed, which leads to a memory leak. Free the memory
chunk with kfree() in the return path.

Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
---
 security/keys/trusted-keys/trusted_tpm2.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

Comments

Jonathan McDowell June 8, 2022, 1:38 p.m. UTC | #1
On Wed, Jun 08, 2022 at 09:17:32PM +0800, Jianglei Nie wrote:
> tpm2_key_encode() allocates a memory chunk from scratch with kmalloc(),
> but it is never freed, which leads to a memory leak. Free the memory
> chunk with kfree() in the return path.

This change only does the kfree in the success path; "out" just returns
the error without freeing the memory.

> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
> ---
>  security/keys/trusted-keys/trusted_tpm2.c | 23 +++++++++++++++++------
>  1 file changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
> index 0165da386289..8b7ab22950d1 100644
> --- a/security/keys/trusted-keys/trusted_tpm2.c
> +++ b/security/keys/trusted-keys/trusted_tpm2.c
> @@ -32,6 +32,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
>  			   struct trusted_key_options *options,
>  			   u8 *src, u32 len)
>  {
> +	int err;
>  	const int SCRATCH_SIZE = PAGE_SIZE;
>  	u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
>  	u8 *work = scratch, *work1;
> @@ -57,8 +58,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
>  		unsigned char bool[3], *w = bool;
>  		/* tag 0 is emptyAuth */
>  		w = asn1_encode_boolean(w, w + sizeof(bool), true);
> -		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
> -			return PTR_ERR(w);
> +		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) {
> +			err = PTR_ERR(w);
> +			goto out;
> +		}
>  		work = asn1_encode_tag(work, end_work, 0, bool, w - bool);
>  	}
>  
> @@ -69,8 +72,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
>  	 * trigger, so if it does there's something nefarious going on
>  	 */
>  	if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
> -		 "BUG: scratch buffer is too small"))
> -		return -EINVAL;
> +		 "BUG: scratch buffer is too small")) {
> +		err = -EINVAL;
> +		goto out;
> +	}
>  
>  	work = asn1_encode_integer(work, end_work, options->keyhandle);
>  	work = asn1_encode_octet_string(work, end_work, pub, pub_len);
> @@ -79,10 +84,16 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
>  	work1 = payload->blob;
>  	work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
>  				     scratch, work - scratch);
> -	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
> -		return PTR_ERR(work1);
> +	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) {
> +		err = -EINVAL;
> +		goto out;
> +	}
> +	kfree(scratch);
>  
>  	return work1 - payload->blob;
> +
> +out:
> +	return err;
>  }
>  
>  struct tpm2_key_context {
> -- 
> 2.25.1
Jarkko Sakkinen June 9, 2022, 5:21 a.m. UTC | #2
On Wed, Jun 08, 2022 at 09:17:32PM +0800, Jianglei Nie wrote:
> tpm2_key_encode() allocates a memory chunk from scratch with kmalloc(),
> but it is never freed, which leads to a memory leak. Free the memory
> chunk with kfree() in the return path.
> 
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
> ---

You should write down the changelog ere. No idea what changed
from the previous version.

>  security/keys/trusted-keys/trusted_tpm2.c | 23 +++++++++++++++++------
>  1 file changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
> index 0165da386289..8b7ab22950d1 100644
> --- a/security/keys/trusted-keys/trusted_tpm2.c
> +++ b/security/keys/trusted-keys/trusted_tpm2.c
> @@ -32,6 +32,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
>  			   struct trusted_key_options *options,
>  			   u8 *src, u32 len)
>  {
> +	int err;

Declare as the last local variable (reverse christmas tree order).

Also, I'd use "int ret" since in other functions that is used.

>  	const int SCRATCH_SIZE = PAGE_SIZE;
>  	u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
>  	u8 *work = scratch, *work1;
> @@ -57,8 +58,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
>  		unsigned char bool[3], *w = bool;
>  		/* tag 0 is emptyAuth */
>  		w = asn1_encode_boolean(w, w + sizeof(bool), true);
> -		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
> -			return PTR_ERR(w);
> +		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) {
> +			err = PTR_ERR(w);
> +			goto out;
> +		}
>  		work = asn1_encode_tag(work, end_work, 0, bool, w - bool);
>  	}
>  
> @@ -69,8 +72,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
>  	 * trigger, so if it does there's something nefarious going on
>  	 */
>  	if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
> -		 "BUG: scratch buffer is too small"))
> -		return -EINVAL;
> +		 "BUG: scratch buffer is too small")) {
> +		err = -EINVAL;
> +		goto out;
> +	}
>  
>  	work = asn1_encode_integer(work, end_work, options->keyhandle);
>  	work = asn1_encode_octet_string(work, end_work, pub, pub_len);
> @@ -79,10 +84,16 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
>  	work1 = payload->blob;
>  	work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
>  				     scratch, work - scratch);
> -	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
> -		return PTR_ERR(work1);
> +	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) {
> +		err = -EINVAL;
> +		goto out;

Why you are changing the return value from PTR_ERR(work1
to -EINVAL?

> +	}
> +	kfree(scratch);
>  
>  	return work1 - payload->blob;
> +
> +out:

Nit:

err:

It's only used for the error path.

> +	return err;
>  }
>  
>  struct tpm2_key_context {
> -- 
> 2.25.1
> 

BR, Jarkko
Jarkko Sakkinen June 9, 2022, 5:22 a.m. UTC | #3
On Thu, Jun 09, 2022 at 08:21:20AM +0300, Jarkko Sakkinen wrote:
> On Wed, Jun 08, 2022 at 09:17:32PM +0800, Jianglei Nie wrote:
> > tpm2_key_encode() allocates a memory chunk from scratch with kmalloc(),
> > but it is never freed, which leads to a memory leak. Free the memory
> > chunk with kfree() in the return path.
> > 
> > Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
> > ---
> 
> You should write down the changelog ere. No idea what changed
> from the previous version.
> 
> >  security/keys/trusted-keys/trusted_tpm2.c | 23 +++++++++++++++++------
> >  1 file changed, 17 insertions(+), 6 deletions(-)
> > 
> > diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
> > index 0165da386289..8b7ab22950d1 100644
> > --- a/security/keys/trusted-keys/trusted_tpm2.c
> > +++ b/security/keys/trusted-keys/trusted_tpm2.c
> > @@ -32,6 +32,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
> >  			   struct trusted_key_options *options,
> >  			   u8 *src, u32 len)
> >  {
> > +	int err;
> 
> Declare as the last local variable (reverse christmas tree order).
> 
> Also, I'd use "int ret" since in other functions that is used.
> 
> >  	const int SCRATCH_SIZE = PAGE_SIZE;
> >  	u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
> >  	u8 *work = scratch, *work1;
> > @@ -57,8 +58,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
> >  		unsigned char bool[3], *w = bool;
> >  		/* tag 0 is emptyAuth */
> >  		w = asn1_encode_boolean(w, w + sizeof(bool), true);
> > -		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
> > -			return PTR_ERR(w);
> > +		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) {
> > +			err = PTR_ERR(w);
> > +			goto out;
> > +		}
> >  		work = asn1_encode_tag(work, end_work, 0, bool, w - bool);
> >  	}
> >  
> > @@ -69,8 +72,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
> >  	 * trigger, so if it does there's something nefarious going on
> >  	 */
> >  	if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
> > -		 "BUG: scratch buffer is too small"))
> > -		return -EINVAL;
> > +		 "BUG: scratch buffer is too small")) {
> > +		err = -EINVAL;
> > +		goto out;
> > +	}
> >  
> >  	work = asn1_encode_integer(work, end_work, options->keyhandle);
> >  	work = asn1_encode_octet_string(work, end_work, pub, pub_len);
> > @@ -79,10 +84,16 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
> >  	work1 = payload->blob;
> >  	work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
> >  				     scratch, work - scratch);
> > -	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
> > -		return PTR_ERR(work1);
> > +	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) {
> > +		err = -EINVAL;
> > +		goto out;
> 
> Why you are changing the return value from PTR_ERR(work1
> to -EINVAL?
> 
> > +	}
> > +	kfree(scratch);
> >  
> >  	return work1 - payload->blob;
> > +
> > +out:
> 
> Nit:
> 
> err:
> 
> It's only used for the error path.
> 
> > +	return err;
> >  }
> >  
> >  struct tpm2_key_context {
> > -- 
> > 2.25.1
> > 
> 
> BR, Jarkko

And the patch is also missing fixes tag:

Fixes: f2219745250f ("security: keys: trusted: use ASN.1 TPM2 key format for the blobs")

BR, Jarkko
Jarkko Sakkinen June 9, 2022, 5:28 a.m. UTC | #4
On Wed, Jun 08, 2022 at 01:38:35PM +0000, Jonathan McDowell wrote:
> On Wed, Jun 08, 2022 at 09:17:32PM +0800, Jianglei Nie wrote:
> > tpm2_key_encode() allocates a memory chunk from scratch with kmalloc(),
> > but it is never freed, which leads to a memory leak. Free the memory
> > chunk with kfree() in the return path.
> 
> This change only does the kfree in the success path; "out" just returns
> the error without freeing the memory.

A valid point.

> > Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
> > ---
> >  security/keys/trusted-keys/trusted_tpm2.c | 23 +++++++++++++++++------
> >  1 file changed, 17 insertions(+), 6 deletions(-)
> > 
> > diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
> > index 0165da386289..8b7ab22950d1 100644
> > --- a/security/keys/trusted-keys/trusted_tpm2.c
> > +++ b/security/keys/trusted-keys/trusted_tpm2.c
> > @@ -32,6 +32,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
> >  			   struct trusted_key_options *options,
> >  			   u8 *src, u32 len)
> >  {
> > +	int err;
> >  	const int SCRATCH_SIZE = PAGE_SIZE;
> >  	u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);

Also, the fix is half-way there, it does not have OOM
check for scratch.

I.e. I'd change the declaration as:

        u8 *scratch;

And later on after declarations:

        scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
        if (!scratch)
                return -ENOMEM; 

> >  	u8 *work = scratch, *work1;
> > @@ -57,8 +58,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
> >  		unsigned char bool[3], *w = bool;
> >  		/* tag 0 is emptyAuth */
> >  		w = asn1_encode_boolean(w, w + sizeof(bool), true);
> > -		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
> > -			return PTR_ERR(w);
> > +		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) {
> > +			err = PTR_ERR(w);
> > +			goto out;
> > +		}
> >  		work = asn1_encode_tag(work, end_work, 0, bool, w - bool);
> >  	}
> >  
> > @@ -69,8 +72,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
> >  	 * trigger, so if it does there's something nefarious going on
> >  	 */
> >  	if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
> > -		 "BUG: scratch buffer is too small"))
> > -		return -EINVAL;
> > +		 "BUG: scratch buffer is too small")) {
> > +		err = -EINVAL;
> > +		goto out;
> > +	}
> >  
> >  	work = asn1_encode_integer(work, end_work, options->keyhandle);
> >  	work = asn1_encode_octet_string(work, end_work, pub, pub_len);
> > @@ -79,10 +84,16 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,
> >  	work1 = payload->blob;
> >  	work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
> >  				     scratch, work - scratch);
> > -	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
> > -		return PTR_ERR(work1);
> > +	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) {
> > +		err = -EINVAL;
> > +		goto out;
> > +	}
> > +	kfree(scratch);
> >  
> >  	return work1 - payload->blob;
> > +
> > +out:
> > +	return err;

I.e.

        kfree(scratch);
        return work1 - payload->blob;

err:
        kfree(scratch);
        return ret;


> >  }
> >  
> >  struct tpm2_key_context {
> > -- 
> > 2.25.1

BR, Jarkko
diff mbox series

Patch

diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 0165da386289..8b7ab22950d1 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -32,6 +32,7 @@  static int tpm2_key_encode(struct trusted_key_payload *payload,
 			   struct trusted_key_options *options,
 			   u8 *src, u32 len)
 {
+	int err;
 	const int SCRATCH_SIZE = PAGE_SIZE;
 	u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
 	u8 *work = scratch, *work1;
@@ -57,8 +58,10 @@  static int tpm2_key_encode(struct trusted_key_payload *payload,
 		unsigned char bool[3], *w = bool;
 		/* tag 0 is emptyAuth */
 		w = asn1_encode_boolean(w, w + sizeof(bool), true);
-		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
-			return PTR_ERR(w);
+		if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) {
+			err = PTR_ERR(w);
+			goto out;
+		}
 		work = asn1_encode_tag(work, end_work, 0, bool, w - bool);
 	}
 
@@ -69,8 +72,10 @@  static int tpm2_key_encode(struct trusted_key_payload *payload,
 	 * trigger, so if it does there's something nefarious going on
 	 */
 	if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
-		 "BUG: scratch buffer is too small"))
-		return -EINVAL;
+		 "BUG: scratch buffer is too small")) {
+		err = -EINVAL;
+		goto out;
+	}
 
 	work = asn1_encode_integer(work, end_work, options->keyhandle);
 	work = asn1_encode_octet_string(work, end_work, pub, pub_len);
@@ -79,10 +84,16 @@  static int tpm2_key_encode(struct trusted_key_payload *payload,
 	work1 = payload->blob;
 	work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
 				     scratch, work - scratch);
-	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
-		return PTR_ERR(work1);
+	if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed")) {
+		err = -EINVAL;
+		goto out;
+	}
+	kfree(scratch);
 
 	return work1 - payload->blob;
+
+out:
+	return err;
 }
 
 struct tpm2_key_context {