public inbox for linux-security-module@vger.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Dinghao Liu <dinghao.liu@zju.edu.cn>
Cc: kjlu@umn.edu, Mimi Zohar <zohar@linux.ibm.com>,
	James Morris <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	Dmitry Kasatkin <dmitry.kasatkin@nokia.com>,
	linux-integrity@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] evm: Fix memleak in init_desc
Date: Sat, 9 Jan 2021 09:07:16 -0800	[thread overview]
Message-ID: <X/nixOkNqQdWUAv8@sol.localdomain> (raw)
In-Reply-To: <20210109113305.11035-1-dinghao.liu@zju.edu.cn>

On Sat, Jan 09, 2021 at 07:33:05PM +0800, Dinghao Liu wrote:
> When kmalloc() fails, tmp_tfm allocated by
> crypto_alloc_shash() has not been freed, which
> leads to memleak.
> 
> Fixes: d46eb3699502b ("evm: crypto hash replaced by shash")
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
> ---
>  security/integrity/evm/evm_crypto.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
> index 168c3b78ac47..39fb31a638ac 100644
> --- a/security/integrity/evm/evm_crypto.c
> +++ b/security/integrity/evm/evm_crypto.c
> @@ -73,7 +73,7 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
>  {
>  	long rc;
>  	const char *algo;
> -	struct crypto_shash **tfm, *tmp_tfm;
> +	struct crypto_shash **tfm, *tmp_tfm = NULL;
>  	struct shash_desc *desc;
>  
>  	if (type == EVM_XATTR_HMAC) {
> @@ -118,13 +118,18 @@ static struct shash_desc *init_desc(char type, uint8_t hash_algo)
>  alloc:
>  	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(*tfm),
>  			GFP_KERNEL);
> -	if (!desc)
> +	if (!desc) {
> +		if (tmp_tfm)
> +			crypto_free_shash(tmp_tfm);
>  		return ERR_PTR(-ENOMEM);
> +	}
>  
>  	desc->tfm = *tfm;
>  
>  	rc = crypto_shash_init(desc);
>  	if (rc) {
> +		if (tmp_tfm)
> +			crypto_free_shash(tmp_tfm);
>  		kfree(desc);
>  		return ERR_PTR(rc);
>  	}

There's no need to check for NULL before calling crypto_free_shash().

- Eric

  reply	other threads:[~2021-01-09 17:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-09 11:33 [PATCH] evm: Fix memleak in init_desc Dinghao Liu
2021-01-09 17:07 ` Eric Biggers [this message]
2021-01-10  5:27   ` dinghao.liu
2021-01-10  6:53     ` Eric Biggers
2021-01-10  7:01       ` dinghao.liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=X/nixOkNqQdWUAv8@sol.localdomain \
    --to=ebiggers@kernel.org \
    --cc=dinghao.liu@zju.edu.cn \
    --cc=dmitry.kasatkin@nokia.com \
    --cc=jmorris@namei.org \
    --cc=kjlu@umn.edu \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=serge@hallyn.com \
    --cc=zohar@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox