[PATCH][next] KEYS: trusted: Fix missing null return from kzalloc call

James Bottomley jejb at linux.ibm.com
Mon Apr 12 16:48:18 UTC 2021


On Mon, 2021-04-12 at 17:01 +0100, Colin King wrote:
> From: Colin Ian King <colin.king at 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 at 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 at 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




More information about the Linux-security-module-archive mailing list