2022-03-08 05:25:11

by GUO Zihua

[permalink] [raw]
Subject: [PATCH] tpm: Fix memory leak in tpmm_chip_alloc

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 <[email protected]>
---
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


2022-03-15 08:37:44

by GUO Zihua

[permalink] [raw]
Subject: Re: [PATCH] tpm: Fix memory leak in tpmm_chip_alloc



On 2022/3/15 0:37, Jason Gunthorpe wrote:
> 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 <[email protected]>
>> 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
>> +++ 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)
>
> This looks like the same issue as was adressed by the recent discussion..
>
> Jason
> .

Hi Jason,

Would you mind refer me to the discussion?

--
Best
GUO Zihua

2022-03-16 15:10:18

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH] tpm: Fix memory leak in tpmm_chip_alloc

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 <[email protected]>
> 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
> +++ 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)

This looks like the same issue as was adressed by the recent discussion..

Jason

2022-03-17 08:20:21

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH] tpm: Fix memory leak in tpmm_chip_alloc

On Tue, Mar 15, 2022 at 09:55:07AM +0800, Guozihua (Scott) wrote:
>
>
> On 2022/3/15 0:37, Jason Gunthorpe wrote:
> > 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 <[email protected]>
> > > 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
> > > +++ 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)
> >
> > This looks like the same issue as was adressed by the recent discussion..
> >
> > Jason
> > .
>
> Hi Jason,
>
> Would you mind refer me to the discussion?

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

And use tag tpmdd-next-v5.18-v2

If issues persists, let us know or send a fix.

BR, Jarkko

2022-03-17 08:24:24

by Jarkko Sakkinen

[permalink] [raw]
Subject: Re: [PATCH] tpm: Fix memory leak in tpmm_chip_alloc

On Mon, Mar 14, 2022 at 01:37:05PM -0300, Jason Gunthorpe wrote:
> 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 <[email protected]>
> > 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
> > +++ 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)
>
> This looks like the same issue as was adressed by the recent discussion..

Both fixes (Lino, jejb) are also part of my PR:

[*] https://lore.kernel.org/linux-integrity/[email protected]/

> Jason

BR, Jarkko