Linux-Security-Module Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call
@ 2021-04-12 16:01 Colin King
  2021-04-12 16:48 ` James Bottomley
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Colin King @ 2021-04-12 16:01 UTC (permalink / raw)
  To: James Bottomley, Jarkko Sakkinen, Mimi Zohar, David Howells,
	James Morris, Serge E . Hallyn, Sumit Garg, linux-integrity,
	keyrings, linux-security-module
  Cc: kernel-janitors, linux-kernel

From: Colin Ian King <colin.king@canonical.com>

The kzalloc call can return null with the GFP_KERNEL flag so
add a null check and exit via a new error exit label. Use the
same exit error label for another error path too.

Addresses-Coverity: ("Dereference null return value")
Fixes: 830027e2cb55 ("KEYS: trusted: Add generic trusted keys framework")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 security/keys/trusted-keys/trusted_core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
index ec3a066a4b42..90774793f0b1 100644
--- a/security/keys/trusted-keys/trusted_core.c
+++ b/security/keys/trusted-keys/trusted_core.c
@@ -116,11 +116,13 @@ static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
 
 	ret = key_payload_reserve(key, sizeof(*p));
 	if (ret < 0)
-		return p;
+		goto err;
 	p = kzalloc(sizeof(*p), GFP_KERNEL);
+	if (!p)
+		goto err;
 
 	p->migratable = migratable;
-
+err:
 	return p;
 }
 
-- 
2.30.2


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call
  2021-04-12 16:01 [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call Colin King
@ 2021-04-12 16:48 ` James Bottomley
  2021-04-12 17:04   ` Colin Ian King
  2021-04-13  5:28 ` Sumit Garg
  2021-04-14 11:07 ` Jarkko Sakkinen
  2 siblings, 1 reply; 6+ messages in thread
From: James Bottomley @ 2021-04-12 16:48 UTC (permalink / raw)
  To: Colin King, Jarkko Sakkinen, Mimi Zohar, David Howells,
	James Morris, Serge E . Hallyn, Sumit Garg, linux-integrity,
	keyrings, linux-security-module
  Cc: kernel-janitors, linux-kernel

On Mon, 2021-04-12 at 17:01 +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The kzalloc call can return null with the GFP_KERNEL flag so
> add a null check and exit via a new error exit label. Use the
> same exit error label for another error path too.
> 
> Addresses-Coverity: ("Dereference null return value")
> Fixes: 830027e2cb55 ("KEYS: trusted: Add generic trusted keys
> framework")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  security/keys/trusted-keys/trusted_core.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/security/keys/trusted-keys/trusted_core.c
> b/security/keys/trusted-keys/trusted_core.c
> index ec3a066a4b42..90774793f0b1 100644
> --- a/security/keys/trusted-keys/trusted_core.c
> +++ b/security/keys/trusted-keys/trusted_core.c
> @@ -116,11 +116,13 @@ static struct trusted_key_payload
> *trusted_payload_alloc(struct key *key)
>  
>  	ret = key_payload_reserve(key, sizeof(*p));
>  	if (ret < 0)
> -		return p;
> +		goto err;
>  	p = kzalloc(sizeof(*p), GFP_KERNEL);
> +	if (!p)
> +		goto err;
>  
>  	p->migratable = migratable;
> -
> +err:
>  	return p;

This is clearly a code migration bug in 

commit 251c85bd106099e6f388a89e88e12d14de2c9cda
Author: Sumit Garg <sumit.garg@linaro.org>
Date:   Mon Mar 1 18:41:24 2021 +0530

    KEYS: trusted: Add generic trusted keys framework

Which has for addition to trusted_core.c:

+static struct trusted_key_payload *trusted_payload_alloc(struct key
*key)
+{
+       struct trusted_key_payload *p = NULL;
+       int ret;
+
+       ret = key_payload_reserve(key, sizeof(*p));
+       if (ret < 0)
+               return p;
+       p = kzalloc(sizeof(*p), GFP_KERNEL);
+
+       p->migratable = migratable;
+
+       return p;
+}

And for trusted_tpm1.c:

-static struct trusted_key_payload *trusted_payload_alloc(struct key
*key)
-{
-       struct trusted_key_payload *p = NULL;
-       int ret;
-
-       ret = key_payload_reserve(key, sizeof *p);
-       if (ret < 0)
-               return p;
-       p = kzalloc(sizeof *p, GFP_KERNEL);
-       if (p)
-               p->migratable = 1; /* migratable by default */
-       return p;
-}

The trusted_tpm1.c code was correct and we got this bug introduced by
what should have been a simple cut and paste ... how did that happen? 
And therefore, how safe is the rest of the extraction into
trusted_core.c?

James



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call
  2021-04-12 16:48 ` James Bottomley
@ 2021-04-12 17:04   ` Colin Ian King
  2021-04-13  5:42     ` Sumit Garg
  0 siblings, 1 reply; 6+ messages in thread
From: Colin Ian King @ 2021-04-12 17:04 UTC (permalink / raw)
  To: jejb, Jarkko Sakkinen, Mimi Zohar, David Howells, James Morris,
	Serge E . Hallyn, Sumit Garg, linux-integrity, keyrings,
	linux-security-module
  Cc: kernel-janitors, linux-kernel

On 12/04/2021 17:48, James Bottomley wrote:
> On Mon, 2021-04-12 at 17:01 +0100, Colin King wrote:
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> The kzalloc call can return null with the GFP_KERNEL flag so
>> add a null check and exit via a new error exit label. Use the
>> same exit error label for another error path too.
>>
>> Addresses-Coverity: ("Dereference null return value")
>> Fixes: 830027e2cb55 ("KEYS: trusted: Add generic trusted keys
>> framework")
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>> ---
>>  security/keys/trusted-keys/trusted_core.c | 6 ++++--
>>  1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/security/keys/trusted-keys/trusted_core.c
>> b/security/keys/trusted-keys/trusted_core.c
>> index ec3a066a4b42..90774793f0b1 100644
>> --- a/security/keys/trusted-keys/trusted_core.c
>> +++ b/security/keys/trusted-keys/trusted_core.c
>> @@ -116,11 +116,13 @@ static struct trusted_key_payload
>> *trusted_payload_alloc(struct key *key)
>>  
>>  	ret = key_payload_reserve(key, sizeof(*p));
>>  	if (ret < 0)
>> -		return p;
>> +		goto err;
>>  	p = kzalloc(sizeof(*p), GFP_KERNEL);
>> +	if (!p)
>> +		goto err;
>>  
>>  	p->migratable = migratable;
>> -
>> +err:
>>  	return p;
> 
> This is clearly a code migration bug in 
> 
> commit 251c85bd106099e6f388a89e88e12d14de2c9cda
> Author: Sumit Garg <sumit.garg@linaro.org>
> Date:   Mon Mar 1 18:41:24 2021 +0530
> 
>     KEYS: trusted: Add generic trusted keys framework
> 
> Which has for addition to trusted_core.c:
> 
> +static struct trusted_key_payload *trusted_payload_alloc(struct key
> *key)
> +{
> +       struct trusted_key_payload *p = NULL;
> +       int ret;
> +
> +       ret = key_payload_reserve(key, sizeof(*p));
> +       if (ret < 0)
> +               return p;
> +       p = kzalloc(sizeof(*p), GFP_KERNEL);
> +
> +       p->migratable = migratable;
> +
> +       return p;
> +}
> 
> And for trusted_tpm1.c:
> 
> -static struct trusted_key_payload *trusted_payload_alloc(struct key
> *key)
> -{
> -       struct trusted_key_payload *p = NULL;
> -       int ret;
> -
> -       ret = key_payload_reserve(key, sizeof *p);
> -       if (ret < 0)
> -               return p;
> -       p = kzalloc(sizeof *p, GFP_KERNEL);
> -       if (p)
> -               p->migratable = 1; /* migratable by default */
> -       return p;
> -}
> 
> The trusted_tpm1.c code was correct and we got this bug introduced by
> what should have been a simple cut and paste ... how did that happen? 
> And therefore, how safe is the rest of the extraction into
> trusted_core.c?
> 

fortunately it gets caught by static analysis, but it does make me also
concerned about what else has changed and how this gets through review.

> James
> 
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call
  2021-04-12 16:01 [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call Colin King
  2021-04-12 16:48 ` James Bottomley
@ 2021-04-13  5:28 ` Sumit Garg
  2021-04-14 11:07 ` Jarkko Sakkinen
  2 siblings, 0 replies; 6+ messages in thread
From: Sumit Garg @ 2021-04-13  5:28 UTC (permalink / raw)
  To: Colin King
  Cc: James Bottomley, Jarkko Sakkinen, Mimi Zohar, David Howells,
	James Morris, Serge E . Hallyn, linux-integrity,
	open list:ASYMMETRIC KEYS, open list:SECURITY SUBSYSTEM,
	kernel-janitors, Linux Kernel Mailing List

On Mon, 12 Apr 2021 at 21:31, Colin King <colin.king@canonical.com> wrote:
>
> From: Colin Ian King <colin.king@canonical.com>
>
> The kzalloc call can return null with the GFP_KERNEL flag so
> add a null check and exit via a new error exit label. Use the
> same exit error label for another error path too.
>
> Addresses-Coverity: ("Dereference null return value")
> Fixes: 830027e2cb55 ("KEYS: trusted: Add generic trusted keys framework")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  security/keys/trusted-keys/trusted_core.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>

Ah, it's my bad. Thanks for fixing this issue.

Reviewed-by: Sumit Garg <sumit.garg@linaro.org>

-Sumit

> diff --git a/security/keys/trusted-keys/trusted_core.c b/security/keys/trusted-keys/trusted_core.c
> index ec3a066a4b42..90774793f0b1 100644
> --- a/security/keys/trusted-keys/trusted_core.c
> +++ b/security/keys/trusted-keys/trusted_core.c
> @@ -116,11 +116,13 @@ static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
>
>         ret = key_payload_reserve(key, sizeof(*p));
>         if (ret < 0)
> -               return p;
> +               goto err;
>         p = kzalloc(sizeof(*p), GFP_KERNEL);
> +       if (!p)
> +               goto err;
>
>         p->migratable = migratable;
> -
> +err:
>         return p;
>  }
>
> --
> 2.30.2
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call
  2021-04-12 17:04   ` Colin Ian King
@ 2021-04-13  5:42     ` Sumit Garg
  0 siblings, 0 replies; 6+ messages in thread
From: Sumit Garg @ 2021-04-13  5:42 UTC (permalink / raw)
  To: Colin Ian King
  Cc: James Bottomley, Jarkko Sakkinen, Mimi Zohar, David Howells,
	James Morris, Serge E . Hallyn, linux-integrity,
	open list:ASYMMETRIC KEYS, open list:SECURITY SUBSYSTEM,
	kernel-janitors, Linux Kernel Mailing List

On Mon, 12 Apr 2021 at 22:34, Colin Ian King <colin.king@canonical.com> wrote:
>
> On 12/04/2021 17:48, James Bottomley wrote:
> > On Mon, 2021-04-12 at 17:01 +0100, Colin King wrote:
> >> From: Colin Ian King <colin.king@canonical.com>
> >>
> >> The kzalloc call can return null with the GFP_KERNEL flag so
> >> add a null check and exit via a new error exit label. Use the
> >> same exit error label for another error path too.
> >>
> >> Addresses-Coverity: ("Dereference null return value")
> >> Fixes: 830027e2cb55 ("KEYS: trusted: Add generic trusted keys
> >> framework")
> >> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> >> ---
> >>  security/keys/trusted-keys/trusted_core.c | 6 ++++--
> >>  1 file changed, 4 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/security/keys/trusted-keys/trusted_core.c
> >> b/security/keys/trusted-keys/trusted_core.c
> >> index ec3a066a4b42..90774793f0b1 100644
> >> --- a/security/keys/trusted-keys/trusted_core.c
> >> +++ b/security/keys/trusted-keys/trusted_core.c
> >> @@ -116,11 +116,13 @@ static struct trusted_key_payload
> >> *trusted_payload_alloc(struct key *key)
> >>
> >>      ret = key_payload_reserve(key, sizeof(*p));
> >>      if (ret < 0)
> >> -            return p;
> >> +            goto err;
> >>      p = kzalloc(sizeof(*p), GFP_KERNEL);
> >> +    if (!p)
> >> +            goto err;
> >>
> >>      p->migratable = migratable;
> >> -
> >> +err:
> >>      return p;
> >
> > This is clearly a code migration bug in
> >
> > commit 251c85bd106099e6f388a89e88e12d14de2c9cda
> > Author: Sumit Garg <sumit.garg@linaro.org>
> > Date:   Mon Mar 1 18:41:24 2021 +0530
> >
> >     KEYS: trusted: Add generic trusted keys framework
> >
> > Which has for addition to trusted_core.c:
> >
> > +static struct trusted_key_payload *trusted_payload_alloc(struct key
> > *key)
> > +{
> > +       struct trusted_key_payload *p = NULL;
> > +       int ret;
> > +
> > +       ret = key_payload_reserve(key, sizeof(*p));
> > +       if (ret < 0)
> > +               return p;
> > +       p = kzalloc(sizeof(*p), GFP_KERNEL);
> > +
> > +       p->migratable = migratable;
> > +
> > +       return p;
> > +}
> >
> > And for trusted_tpm1.c:
> >
> > -static struct trusted_key_payload *trusted_payload_alloc(struct key
> > *key)
> > -{
> > -       struct trusted_key_payload *p = NULL;
> > -       int ret;
> > -
> > -       ret = key_payload_reserve(key, sizeof *p);
> > -       if (ret < 0)
> > -               return p;
> > -       p = kzalloc(sizeof *p, GFP_KERNEL);
> > -       if (p)
> > -               p->migratable = 1; /* migratable by default */
> > -       return p;
> > -}
> >
> > The trusted_tpm1.c code was correct and we got this bug introduced by
> > what should have been a simple cut and paste ... how did that happen?

It was a little more than just cut and paste where I did generalized
"migratable" flag to be provided by the corresponding trust source's
ops struct.

> > And therefore, how safe is the rest of the extraction into
> > trusted_core.c?
> >
>
> fortunately it gets caught by static analysis, but it does make me also
> concerned about what else has changed and how this gets through review.
>

I agree that extraction into trusted_core.c was a complex change but
this patch has been up for review for almost 2 years [1]. And
extensive testing can't catch this sort of bug as allocation wouldn't
normally fail.

[1] https://lwn.net/Articles/795416/

-Sumit

> > James
> >
> >
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call
  2021-04-12 16:01 [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call Colin King
  2021-04-12 16:48 ` James Bottomley
  2021-04-13  5:28 ` Sumit Garg
@ 2021-04-14 11:07 ` Jarkko Sakkinen
  2 siblings, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2021-04-14 11:07 UTC (permalink / raw)
  To: Colin King
  Cc: James Bottomley, Mimi Zohar, David Howells, James Morris,
	Serge E . Hallyn, Sumit Garg, linux-integrity, keyrings,
	linux-security-module, kernel-janitors, linux-kernel

On Mon, Apr 12, 2021 at 05:01:01PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The kzalloc call can return null with the GFP_KERNEL flag so
> add a null check and exit via a new error exit label. Use the
> same exit error label for another error path too.
> 
> Addresses-Coverity: ("Dereference null return value")
> Fixes: 830027e2cb55 ("KEYS: trusted: Add generic trusted keys framework")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
 
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

/Jarkko

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-12 16:01 [PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call Colin King
2021-04-12 16:48 ` James Bottomley
2021-04-12 17:04   ` Colin Ian King
2021-04-13  5:42     ` Sumit Garg
2021-04-13  5:28 ` Sumit Garg
2021-04-14 11:07 ` Jarkko Sakkinen

Linux-Security-Module Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-security-module/0 linux-security-module/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-security-module linux-security-module/ https://lore.kernel.org/linux-security-module \
		linux-security-module@vger.kernel.org
	public-inbox-index linux-security-module

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-security-module


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git