tpmdd-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
* [PATCH] tpm: Use the right clean up after cdev_add completes
@ 2017-02-23 21:19 Jason Gunthorpe
  2017-02-24 16:09 ` Jarkko Sakkinen
       [not found] ` <20170223211914.GA12752-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
  0 siblings, 2 replies; 6+ messages in thread
From: Jason Gunthorpe @ 2017-02-23 21:19 UTC (permalink / raw)
  To: Jarkko Sakkinen
  Cc: linux-security-module-u79uwXL29TY76Z2rM5mHXA,
	tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

Once cdev_add is done the device node is visible to user space and
could have been opened. Thus we have to go through the locking
process in tpm_del_char_device if device_add fails.

Fixes: 2c91ce8523a ("tpm: fix the rollback in tpm_chip_register()")
Signed-off-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
---
 drivers/char/tpm/tpm-chip.c | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)

static function moved to avoid a prototype

This was noticed while reviewing the cdev patchset from Logan

diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index a77262d31911a1..c82acf0a6e7353 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -226,6 +226,26 @@ struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
 }
 EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
 
+static void tpm_del_char_device(struct tpm_chip *chip, bool with_device)
+{
+	cdev_del(&chip->cdev);
+	if (with_device) {
+		device_del(&chip->dev);
+
+		/* Make the chip unavailable. */
+		mutex_lock(&idr_lock);
+		idr_replace(&dev_nums_idr, NULL, chip->dev_num);
+		mutex_unlock(&idr_lock);
+	}
+
+	/* Make the driver uncallable. */
+	down_write(&chip->ops_sem);
+	if (chip->flags & TPM_CHIP_FLAG_TPM2)
+		tpm2_shutdown(chip, TPM2_SU_CLEAR);
+	chip->ops = NULL;
+	up_write(&chip->ops_sem);
+}
+
 static int tpm_add_char_device(struct tpm_chip *chip)
 {
 	int rc;
@@ -246,8 +266,7 @@ static int tpm_add_char_device(struct tpm_chip *chip)
 			"unable to device_register() %s, major %d, minor %d, err=%d\n",
 			dev_name(&chip->dev), MAJOR(chip->dev.devt),
 			MINOR(chip->dev.devt), rc);
-
-		cdev_del(&chip->cdev);
+		tpm_del_char_device(chip, false);
 		return rc;
 	}
 
@@ -259,24 +278,6 @@ static int tpm_add_char_device(struct tpm_chip *chip)
 	return rc;
 }
 
-static void tpm_del_char_device(struct tpm_chip *chip)
-{
-	cdev_del(&chip->cdev);
-	device_del(&chip->dev);
-
-	/* Make the chip unavailable. */
-	mutex_lock(&idr_lock);
-	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
-	mutex_unlock(&idr_lock);
-
-	/* Make the driver uncallable. */
-	down_write(&chip->ops_sem);
-	if (chip->flags & TPM_CHIP_FLAG_TPM2)
-		tpm2_shutdown(chip, TPM2_SU_CLEAR);
-	chip->ops = NULL;
-	up_write(&chip->ops_sem);
-}
-
 static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
 {
 	struct attribute **i;
@@ -384,6 +385,6 @@ void tpm_chip_unregister(struct tpm_chip *chip)
 {
 	tpm_del_legacy_sysfs(chip);
 	tpm_bios_log_teardown(chip);
-	tpm_del_char_device(chip);
+	tpm_del_char_device(chip, true);
 }
 EXPORT_SYMBOL_GPL(tpm_chip_unregister);
-- 
2.7.4

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

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

* Re: [PATCH] tpm: Use the right clean up after cdev_add completes
  2017-02-23 21:19 [PATCH] tpm: Use the right clean up after cdev_add completes Jason Gunthorpe
@ 2017-02-24 16:09 ` Jarkko Sakkinen
       [not found] ` <20170223211914.GA12752-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
  1 sibling, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2017-02-24 16:09 UTC (permalink / raw)
  To: Jason Gunthorpe; +Cc: tpmdd-devel, linux-security-module

On Thu, Feb 23, 2017 at 02:19:14PM -0700, Jason Gunthorpe wrote:
> Once cdev_add is done the device node is visible to user space and
> could have been opened. Thus we have to go through the locking
> process in tpm_del_char_device if device_add fails.
> 
> Fixes: 2c91ce8523a ("tpm: fix the rollback in tpm_chip_register()")
> Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>

Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

/Jarkko

> ---
>  drivers/char/tpm/tpm-chip.c | 43 ++++++++++++++++++++++---------------------
>  1 file changed, 22 insertions(+), 21 deletions(-)
> 
> static function moved to avoid a prototype
> 
> This was noticed while reviewing the cdev patchset from Logan
> 
> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
> index a77262d31911a1..c82acf0a6e7353 100644
> --- a/drivers/char/tpm/tpm-chip.c
> +++ b/drivers/char/tpm/tpm-chip.c
> @@ -226,6 +226,26 @@ struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
>  }
>  EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
>  
> +static void tpm_del_char_device(struct tpm_chip *chip, bool with_device)
> +{
> +	cdev_del(&chip->cdev);
> +	if (with_device) {
> +		device_del(&chip->dev);
> +
> +		/* Make the chip unavailable. */
> +		mutex_lock(&idr_lock);
> +		idr_replace(&dev_nums_idr, NULL, chip->dev_num);
> +		mutex_unlock(&idr_lock);
> +	}
> +
> +	/* Make the driver uncallable. */
> +	down_write(&chip->ops_sem);
> +	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> +		tpm2_shutdown(chip, TPM2_SU_CLEAR);
> +	chip->ops = NULL;
> +	up_write(&chip->ops_sem);
> +}
> +
>  static int tpm_add_char_device(struct tpm_chip *chip)
>  {
>  	int rc;
> @@ -246,8 +266,7 @@ static int tpm_add_char_device(struct tpm_chip *chip)
>  			"unable to device_register() %s, major %d, minor %d, err=%d\n",
>  			dev_name(&chip->dev), MAJOR(chip->dev.devt),
>  			MINOR(chip->dev.devt), rc);
> -
> -		cdev_del(&chip->cdev);
> +		tpm_del_char_device(chip, false);
>  		return rc;
>  	}
>  
> @@ -259,24 +278,6 @@ static int tpm_add_char_device(struct tpm_chip *chip)
>  	return rc;
>  }
>  
> -static void tpm_del_char_device(struct tpm_chip *chip)
> -{
> -	cdev_del(&chip->cdev);
> -	device_del(&chip->dev);
> -
> -	/* Make the chip unavailable. */
> -	mutex_lock(&idr_lock);
> -	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
> -	mutex_unlock(&idr_lock);
> -
> -	/* Make the driver uncallable. */
> -	down_write(&chip->ops_sem);
> -	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> -		tpm2_shutdown(chip, TPM2_SU_CLEAR);
> -	chip->ops = NULL;
> -	up_write(&chip->ops_sem);
> -}
> -
>  static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
>  {
>  	struct attribute **i;
> @@ -384,6 +385,6 @@ void tpm_chip_unregister(struct tpm_chip *chip)
>  {
>  	tpm_del_legacy_sysfs(chip);
>  	tpm_bios_log_teardown(chip);
> -	tpm_del_char_device(chip);
> +	tpm_del_char_device(chip, true);
>  }
>  EXPORT_SYMBOL_GPL(tpm_chip_unregister);
> -- 
> 2.7.4

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

* Re: [PATCH] tpm: Use the right clean up after cdev_add completes
       [not found] ` <20170223211914.GA12752-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
@ 2017-02-24 17:43   ` Jarkko Sakkinen
       [not found]     ` <20170224174354.w5e2v3nxntod72je-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Jarkko Sakkinen @ 2017-02-24 17:43 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: linux-security-module-u79uwXL29TY76Z2rM5mHXA,
	tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

On Thu, Feb 23, 2017 at 02:19:14PM -0700, Jason Gunthorpe wrote:
> Once cdev_add is done the device node is visible to user space and
> could have been opened. Thus we have to go through the locking
> process in tpm_del_char_device if device_add fails.
> 
> Fixes: 2c91ce8523a ("tpm: fix the rollback in tpm_chip_register()")
> Signed-off-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>

Pushed.

/Jarkko

> ---
>  drivers/char/tpm/tpm-chip.c | 43 ++++++++++++++++++++++---------------------
>  1 file changed, 22 insertions(+), 21 deletions(-)
> 
> static function moved to avoid a prototype
> 
> This was noticed while reviewing the cdev patchset from Logan
> 
> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
> index a77262d31911a1..c82acf0a6e7353 100644
> --- a/drivers/char/tpm/tpm-chip.c
> +++ b/drivers/char/tpm/tpm-chip.c
> @@ -226,6 +226,26 @@ struct tpm_chip *tpmm_chip_alloc(struct device *pdev,
>  }
>  EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
>  
> +static void tpm_del_char_device(struct tpm_chip *chip, bool with_device)
> +{
> +	cdev_del(&chip->cdev);
> +	if (with_device) {
> +		device_del(&chip->dev);
> +
> +		/* Make the chip unavailable. */
> +		mutex_lock(&idr_lock);
> +		idr_replace(&dev_nums_idr, NULL, chip->dev_num);
> +		mutex_unlock(&idr_lock);
> +	}
> +
> +	/* Make the driver uncallable. */
> +	down_write(&chip->ops_sem);
> +	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> +		tpm2_shutdown(chip, TPM2_SU_CLEAR);
> +	chip->ops = NULL;
> +	up_write(&chip->ops_sem);
> +}
> +
>  static int tpm_add_char_device(struct tpm_chip *chip)
>  {
>  	int rc;
> @@ -246,8 +266,7 @@ static int tpm_add_char_device(struct tpm_chip *chip)
>  			"unable to device_register() %s, major %d, minor %d, err=%d\n",
>  			dev_name(&chip->dev), MAJOR(chip->dev.devt),
>  			MINOR(chip->dev.devt), rc);
> -
> -		cdev_del(&chip->cdev);
> +		tpm_del_char_device(chip, false);
>  		return rc;
>  	}
>  
> @@ -259,24 +278,6 @@ static int tpm_add_char_device(struct tpm_chip *chip)
>  	return rc;
>  }
>  
> -static void tpm_del_char_device(struct tpm_chip *chip)
> -{
> -	cdev_del(&chip->cdev);
> -	device_del(&chip->dev);
> -
> -	/* Make the chip unavailable. */
> -	mutex_lock(&idr_lock);
> -	idr_replace(&dev_nums_idr, NULL, chip->dev_num);
> -	mutex_unlock(&idr_lock);
> -
> -	/* Make the driver uncallable. */
> -	down_write(&chip->ops_sem);
> -	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> -		tpm2_shutdown(chip, TPM2_SU_CLEAR);
> -	chip->ops = NULL;
> -	up_write(&chip->ops_sem);
> -}
> -
>  static void tpm_del_legacy_sysfs(struct tpm_chip *chip)
>  {
>  	struct attribute **i;
> @@ -384,6 +385,6 @@ void tpm_chip_unregister(struct tpm_chip *chip)
>  {
>  	tpm_del_legacy_sysfs(chip);
>  	tpm_bios_log_teardown(chip);
> -	tpm_del_char_device(chip);
> +	tpm_del_char_device(chip, true);
>  }
>  EXPORT_SYMBOL_GPL(tpm_chip_unregister);
> -- 
> 2.7.4

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

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

* Re: [PATCH] tpm: Use the right clean up after cdev_add completes
       [not found]     ` <20170224174354.w5e2v3nxntod72je-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
@ 2017-02-24 20:59       ` Jarkko Sakkinen
       [not found]         ` <20170224205949.u63xb5qhqzd7lqtp-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
  2017-02-24 21:16         ` Jason Gunthorpe
  0 siblings, 2 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2017-02-24 20:59 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: linux-security-module-u79uwXL29TY76Z2rM5mHXA,
	tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

On Fri, Feb 24, 2017 at 07:43:54PM +0200, Jarkko Sakkinen wrote:
> On Thu, Feb 23, 2017 at 02:19:14PM -0700, Jason Gunthorpe wrote:
> > Once cdev_add is done the device node is visible to user space and
> > could have been opened. Thus we have to go through the locking
> > process in tpm_del_char_device if device_add fails.
> > 
> > Fixes: 2c91ce8523a ("tpm: fix the rollback in tpm_chip_register()")
> > Signed-off-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
> 
> Pushed.

It would make easier to merge this with resource manager commits if
there was instead

void tpm_chip_shutdown(struct tpm_chip *chip)
{
	/* Make the driver uncallable. */
	down_write(&chip->ops_sem);
	if (chip->flags & TPM_CHIP_FLAG_TPM2)
		tpm2_shutdown(chip, TPM2_SU_CLEAR);
	chip->ops = NULL;
	up_write(&chip->ops_sem);
}

And you would call this instead of wiring into tpm_del_char_device.

I can update the commit.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

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

* Re: [PATCH] tpm: Use the right clean up after cdev_add completes
       [not found]         ` <20170224205949.u63xb5qhqzd7lqtp-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
@ 2017-02-24 21:11           ` Jarkko Sakkinen
  0 siblings, 0 replies; 6+ messages in thread
From: Jarkko Sakkinen @ 2017-02-24 21:11 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: linux-security-module-u79uwXL29TY76Z2rM5mHXA,
	tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f

On Fri, Feb 24, 2017 at 10:59:49PM +0200, Jarkko Sakkinen wrote:
> On Fri, Feb 24, 2017 at 07:43:54PM +0200, Jarkko Sakkinen wrote:
> > On Thu, Feb 23, 2017 at 02:19:14PM -0700, Jason Gunthorpe wrote:
> > > Once cdev_add is done the device node is visible to user space and
> > > could have been opened. Thus we have to go through the locking
> > > process in tpm_del_char_device if device_add fails.
> > > 
> > > Fixes: 2c91ce8523a ("tpm: fix the rollback in tpm_chip_register()")
> > > Signed-off-by: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
> > 
> > Pushed.
> 
> It would make easier to merge this with resource manager commits if
> there was instead
> 
> void tpm_chip_shutdown(struct tpm_chip *chip)
> {
> 	/* Make the driver uncallable. */
> 	down_write(&chip->ops_sem);
> 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> 		tpm2_shutdown(chip, TPM2_SU_CLEAR);
> 	chip->ops = NULL;
> 	up_write(&chip->ops_sem);
> }
> 
> And you would call this instead of wiring into tpm_del_char_device.
> 
> I can update the commit.
> 
> /Jarkko

Actually lets keep as it is. I'll extend tpm_del_char_device a bit
instead in the RM series.

/Jarkko

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

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

* Re: [PATCH] tpm: Use the right clean up after cdev_add completes
  2017-02-24 20:59       ` Jarkko Sakkinen
       [not found]         ` <20170224205949.u63xb5qhqzd7lqtp-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
@ 2017-02-24 21:16         ` Jason Gunthorpe
  1 sibling, 0 replies; 6+ messages in thread
From: Jason Gunthorpe @ 2017-02-24 21:16 UTC (permalink / raw)
  To: Jarkko Sakkinen; +Cc: tpmdd-devel, linux-security-module

On Fri, Feb 24, 2017 at 10:59:50PM +0200, Jarkko Sakkinen wrote:
> On Fri, Feb 24, 2017 at 07:43:54PM +0200, Jarkko Sakkinen wrote:
> > On Thu, Feb 23, 2017 at 02:19:14PM -0700, Jason Gunthorpe wrote:
> > > Once cdev_add is done the device node is visible to user space and
> > > could have been opened. Thus we have to go through the locking
> > > process in tpm_del_char_device if device_add fails.
> > > 
> > > Fixes: 2c91ce8523a ("tpm: fix the rollback in tpm_chip_register()")
> > > Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
> > 
> > Pushed.
> 
> It would make easier to merge this with resource manager commits if
> there was instead
> 
> void tpm_chip_shutdown(struct tpm_chip *chip)
> {
> 	/* Make the driver uncallable. */
> 	down_write(&chip->ops_sem);
> 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
> 		tpm2_shutdown(chip, TPM2_SU_CLEAR);
> 	chip->ops = NULL;
> 	up_write(&chip->ops_sem);
> }
> 
> And you would call this instead of wiring into tpm_del_char_device.

Hum.. I think this direction in the RM patch would be more
maintainable.. Otherwise tpm_add_char_device has very complicated
error handling.

diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index c82acf0a6e7353..444cf2495ef0e0 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -353,18 +353,26 @@ int tpm_chip_register(struct tpm_chip *chip)
 	tpm_add_ppi(chip);
 
 	rc = tpm_add_char_device(chip);
-	if (rc) {
-		tpm_bios_log_teardown(chip);
-		return rc;
-	}
+	if (rc)
+		goto err_add_char;
+
+	rc = tpm_add_rm_char_device(chip);
+	if (rc)
+		goto err_add_rm_char;
 
 	rc = tpm_add_legacy_sysfs(chip);
-	if (rc) {
-		tpm_chip_unregister(chip);
-		return rc;
-	}
+	if (rc)
+		goto err_add_legacy_sysfs;
 
 	return 0;
+
+err_add_legacy_sysfs:
+	tpm_del_rm_char_device(chip);
+err_add_rm_char:
+	tpm_del_char_device(chip, true);
+err_add_char:
+	tpm_bios_log_teardown(chip);
+	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm_chip_register);
 
@@ -384,6 +392,7 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);
 void tpm_chip_unregister(struct tpm_chip *chip)
 {
 	tpm_del_legacy_sysfs(chip);
+	tpm_del_rm_char_device(chip);
 	tpm_bios_log_teardown(chip);
 	tpm_del_char_device(chip, true);
 }

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

end of thread, other threads:[~2017-02-24 21:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-23 21:19 [PATCH] tpm: Use the right clean up after cdev_add completes Jason Gunthorpe
2017-02-24 16:09 ` Jarkko Sakkinen
     [not found] ` <20170223211914.GA12752-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-02-24 17:43   ` Jarkko Sakkinen
     [not found]     ` <20170224174354.w5e2v3nxntod72je-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2017-02-24 20:59       ` Jarkko Sakkinen
     [not found]         ` <20170224205949.u63xb5qhqzd7lqtp-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2017-02-24 21:11           ` Jarkko Sakkinen
2017-02-24 21:16         ` Jason Gunthorpe

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).