linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: GUO Zihua <guozihua@huawei.com>
Cc: linux-integrity@vger.kernel.org, wangweiyang2@huawei.com,
	xiujianfeng@huawei.com, weiyongjun1@huawei.com,
	peterhuewe@gmx.de, jgg@ziepe.ca, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tpm: Fix memory leak in tpmm_chip_alloc
Date: Mon, 7 Mar 2022 15:45:38 +0200	[thread overview]
Message-ID: <YiYMgnpnf3Y6l8IY@iki.fi> (raw)
In-Reply-To: <20220307104827.40843-1-guozihua@huawei.com>

On Mon, Mar 07, 2022 at 06:48:27PM +0800, GUO Zihua wrote:
> Fix a memory leak in tpmm_chip_alloc. devm_add_action_or_reset would
> call put_device on error, while tpm->devs is left untouched. Call
> put_device on tpm->devs as well if devm_add_action_or_reset returns an
> error.
> 
> Fixes: fdc915f7f719 ("tpm: expose spaces via a device link /dev/tpmrm")
> Signed-off-by: GUO Zihua <guozihua@huawei.com>
> ---
>  drivers/char/tpm/tpm-chip.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
> index b009e7479b70..0a92334e8c40 100644
> --- a/drivers/char/tpm/tpm-chip.c
> +++ b/drivers/char/tpm/tpm-chip.c
> @@ -308,6 +308,12 @@ static int tpm_class_shutdown(struct device *dev)
>  	return 0;
>  }
>  
> +static void tpm_chip_free(struct tpm_chip *chip)
> +{
> +	put_device(&chip->devs);
> +	put_device(&chip->dev);
> +}
> +
>  /**
>   * tpm_chip_alloc() - allocate a new struct tpm_chip instance
>   * @pdev: device to which the chip is associated
> @@ -396,8 +402,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev,
>  	return chip;
>  
>  out:
> -	put_device(&chip->devs);
> -	put_device(&chip->dev);
> +	tpm_chip_free(chip);
>  	return ERR_PTR(rc);
>  }
>  EXPORT_SYMBOL_GPL(tpm_chip_alloc);
> @@ -420,8 +425,8 @@ struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
>  		return chip;
>  
>  	rc = devm_add_action_or_reset(pdev,
> -				      (void (*)(void *)) put_device,
> -				      &chip->dev);
> +				      (void (*)(void *)) tpm_chip_free,
> +				      chip);
>  	if (rc)
>  		return ERR_PTR(rc);
>  
> -- 
> 2.17.1
> 

Please test against the latest in

git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd.git

and share your results.

BR, Jarkko

  reply	other threads:[~2022-03-07 13:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-07 10:48 [PATCH] tpm: Fix memory leak in tpmm_chip_alloc GUO Zihua
2022-03-07 13:45 ` Jarkko Sakkinen [this message]
2022-03-10  3:33   ` Guozihua (Scott)
2022-03-11 16:34     ` Jarkko Sakkinen
2022-03-14  7:10       ` Guozihua (Scott)
2022-03-15  3:11       ` Guozihua (Scott)
2022-03-14 16:37 ` Jason Gunthorpe
2022-03-15  1:55   ` Guozihua (Scott)
2022-03-17  7:33     ` Jarkko Sakkinen
2022-03-17  7:31   ` Jarkko Sakkinen

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=YiYMgnpnf3Y6l8IY@iki.fi \
    --to=jarkko@kernel.org \
    --cc=guozihua@huawei.com \
    --cc=jgg@ziepe.ca \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterhuewe@gmx.de \
    --cc=wangweiyang2@huawei.com \
    --cc=weiyongjun1@huawei.com \
    --cc=xiujianfeng@huawei.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;
as well as URLs for NNTP newsgroup(s).