tpm: fix multiple race conditions in tpm_ppi.c
PATCH 1/1 tpm: fix multiple race conditions in tpm_ppi.c
* Traversal of the ACPI device tree was not done right. It should lookup PPI only under the ACPI device that it is associated. Otherwise, it could match to a wrong PPI interface if there are two TPM devices in the device tree.
* Removed global ACPI handle and version string from tpm_ppi.c as this is racy. Instead they should be associated with the chip.
* Moved code just a tiny bit towards two-phase allocation to implement fix for the PPI race conditions.
* Added missing copyright platter in tpm_ppi.c.
--- drivers/char/tpm/tpm-interface.c | 31 +++++---- drivers/char/tpm/tpm.h | 21 ++++-- drivers/char/tpm/tpm_ppi.c | 134 ++++++++++++++++++++++++--------------- drivers/char/tpm/tpm_tis.c | 25 ++++++-- 4 files changed, 136 insertions(+), 75 deletions(-)
drivers/char/tpm/tpm-interface.c +19 -12
@@ -900,7 +900,7 @@ void tpm_remove_hardware(struct device *dev)tpm_dev_del_device (chip);tpm_sysfs_del_device (chip); -tpm_remove_ppi (&dev->kobj); +tpm_remove_ppi (chip, &dev->kobj);tpm_bios_log_teardown (chip->bios_dir);/* write it this way to be explicit (chip->dev == dev) */
@@ -1077,17 +1077,9 @@ static void tpm_dev_release(struct device *dev)* upon errant exit from this function specific probe function should call * pci_disable_device */ -struct tpm_chip *tpm_register_hardware (struct device *dev, -const struct tpm_class_ops *ops) +struct tpm_chip *tpm_chip_register (struct tpm_chip *chip,struct device *dev, +const struct tpm_class_ops *ops) { -struct tpm_chip *chip; - -/* Driver specific per-device data */ - chip =kzalloc (sizeof (*chip),GFP_KERNEL ); - -if (chip ==NULL ) -return NULL ; -mutex_init (&chip->tpm_mutex);INIT_LIST_HEAD (&chip->list);
@@ -1115,7 +1107,7 @@ struct tpm_chip *tpm_register_hardware(struct device *dev,if (tpm_sysfs_add_device (chip)) goto del_misc; -if (tpm_add_ppi (&dev->kobj)) +if (tpm_add_ppi (chip, &dev->kobj)) goto del_sysfs; chip->bios_dir =tpm_bios_log_setup (chip->devname);
@@ -1137,6 +1129,21 @@ out_free:kfree (chip);return NULL ; } +EXPORT_SYMBOL_GPL (tpm_chip_register); + +struct tpm_chip *tpm_register_hardware (struct device *dev, +const struct tpm_class_ops *ops) +{ +struct tpm_chip *chip; + +/* Driver specific per-device data */ + chip =kzalloc (sizeof (*chip),GFP_KERNEL ); + +if (chip ==NULL ) +return NULL ; + +return tpm_chip_register (chip, dev, ops); +}EXPORT_SYMBOL_GPL (tpm_register_hardware);MODULE_AUTHOR ("Leendert van Doorn ([email protected])" );
drivers/char/tpm/tpm.h +16 -5
@@ -27,6 +27,7 @@#include <linux/platform_device.h> #include <linux/io.h> #include <linux/tpm.h> +#include <linux/acpi.h> enum tpm_const {TPM_MINOR =224 ,/* officially assigned */
@@ -94,6 +95,8 @@ struct tpm_vendor_specific {#define TPM_VID_WINBOND 0x1050#define TPM_VID_STM 0x104A +#define TPM_PPI_VERSION_LEN 3 +struct tpm_chip {struct device *dev;/* Device stuff */ const struct tpm_class_ops *ops;
@@ -109,6 +112,11 @@ struct tpm_chip {struct dentry **bios_dir; +#ifdef CONFIG_ACPI +acpi_handle acpi_dev_handle; +char ppi_version[TPM_PPI_VERSION_LEN +1 ]; +#endif /* CONFIG_ACPI */ +struct list_head list;void (*release) (struct device *); };
@@ -321,7 +329,10 @@ extern int tpm_get_timeouts(struct tpm_chip *);extern void tpm_gen_interrupt (struct tpm_chip *);extern int tpm_do_selftest (struct tpm_chip *);extern unsigned long tpm_calc_ordinal_duration (struct tpm_chip *,u32 ); -extern struct tpm_chip *tpm_register_hardware (struct device *, +extern struct tpm_chip *tpm_chip_register (struct tpm_chip *chip, +struct device *dev, +const struct tpm_class_ops *ops); +extern struct tpm_chip *tpm_register_hardware (struct device *,const struct tpm_class_ops *ops);extern void tpm_dev_vendor_release (struct tpm_chip *);extern void tpm_remove_hardware (struct device *);
@@ -338,15 +349,15 @@ void tpm_sysfs_del_device(struct tpm_chip *chip);int tpm_pcr_read_dev (struct tpm_chip *chip,int pcr_idx,u8 *res_buf);#ifdef CONFIG_ACPI -extern int tpm_add_ppi (struct kobject *); -extern void tpm_remove_ppi (struct kobject *); +extern int tpm_add_ppi (struct tpm_chip *chip,struct kobject *parent); +extern void tpm_remove_ppi (struct tpm_chip *chip,struct kobject *parent);#else -static inline int tpm_add_ppi (struct kobject *parent) +static inline int tpm_add_ppi (struct tpm_chip *chip,struct kobject *parent) {return 0 ; } -static inline void tpm_remove_ppi (struct kobject *parent) +static inline void tpm_remove_ppi (struct tpm_chip *chip,struct kobject *parent) { }#endif
drivers/char/tpm/tpm_ppi.c +82 -52
240 lines not on this page · raw message
drivers/char/tpm/tpm_tis.c +19 -6
@@ -528,15 +528,17 @@ static bool interrupts = true;module_param (interrupts, bool,0444 );MODULE_PARM_DESC (interrupts,"Enable interrupts" ); -static int tpm_tis_init (struct device *dev,resource_size_t start, -resource_size_t len,unsigned int irq) +static int tpm_tis_init (struct device *dev,struct tpm_chip *chip, +resource_size_t start,resource_size_t len, +unsigned int irq) {u32 vendor, intfcaps, intmask;int rc, i, irq_s, irq_e, probe; -struct tpm_chip *chip; -if (!(chip =tpm_register_hardware (dev, &tpm_tis))) +if (!tpm_chip_register (chip, dev, &tpm_tis)) { +kfree (chip);return -ENODEV ; + } chip->vendor.iobase =ioremap (start, len);if (!chip->vendor.iobase) {
@@ -777,6 +779,7 @@ static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);static int tpm_tis_pnp_init (struct pnp_dev *pnp_dev,const struct pnp_device_id *pnp_id) { +struct tpm_chip *chip;resource_size_t start, len;unsigned int irq =0 ;
@@ -791,7 +794,13 @@ static int tpm_tis_pnp_init(struct pnp_dev *pnp_dev,if (is_itpm (pnp_dev)) itpm = true; -return tpm_tis_init (&pnp_dev->dev, start, len, irq); + chip =kzalloc (sizeof (*chip),GFP_KERNEL ); +if (chip ==NULL ) +return NULL ; +if (pnp_acpi_device (pnp_dev)) + chip->acpi_dev_handle =pnp_acpi_device (pnp_dev)->handle; + +return tpm_tis_init (&pnp_dev->dev, chip, start, len, irq); }static struct pnp_device_id tpm_pnp_tbl[] = {
@@ -849,6 +858,7 @@ module_param(force, bool, 0444);MODULE_PARM_DESC (force,"Force device probe rather than using ACPI entry" );static int __init init_tis (void ) { +struct tpm_chip *chip;int rc;#ifdef CONFIG_PNP if (!force)
@@ -863,7 +873,10 @@ static int __init init_tis(void) rc =PTR_ERR (pdev); goto err_dev; } - rc =tpm_tis_init (&pdev->dev,TIS_MEM_BASE ,TIS_MEM_LEN ,0 ); + chip =kzalloc (sizeof (*chip),GFP_KERNEL ); +if (chip ==NULL ) + goto err_init; + rc =tpm_tis_init (&pdev->dev, chip,TIS_MEM_BASE ,TIS_MEM_LEN ,0 );if (rc) goto err_init;return 0 ;
Signature · 1 line
2.1.0
Quoted 9 lines · Jarkko Sakkinen
* Traversal of the ACPI device tree was not done right. It should lookup PPI only under the ACPI device that it is associated. Otherwise, it could match to a wrong PPI interface if there are two TPM devices in the device tree.
* Removed global ACPI handle and version string fromtpm_ppi.cas this is racy. Instead they should be associated with the chip.
* Moved code just a tiny bit towards two-phase allocation to implement fix for the PPI race conditions.
* Added missing copyright platter intpm_ppi.c.
The PPI parts of this look fine to me, and are a really nice cleanup, thanks!
Personally, I'd sequence this commit right after your 'tpm: two-phase chip management functions' commit because it makes it much saner (no half step toward the new functions). I assume this is a theoretical problem? Or do you have a two TPM system?
Jason
Quoted 11 lines · Jason Gunthorpe
Quoted 9 lines · Jarkko Sakkinen
* Traversal of the ACPI device tree was not done right. It should lookup PPI only under the ACPI device that it is associated. Otherwise, it could match to a wrong PPI interface if there are two TPM devices in the device tree.
* Removed global ACPI handle and version string fromtpm_ppi.cas this is racy. Instead they should be associated with the chip.
* Moved code just a tiny bit towards two-phase allocation to implement fix for the PPI race conditions.
* Added missing copyright platter intpm_ppi.c.
Quoted 7 lines
The PPI parts of this look fine to me, and are a really nice cleanup, thanks!
Personally, I'd sequence this commit right after your 'tpm: two-phase chip management functions' commit because it makes it much saner (no half step toward the new functions). I assume this is a theoretical problem? Or do you have a two TPM system?
This has realized in Intel NUCs where there is PTT and dTPM module. Even when PTT is selected there is still ACPI device for dTPM so three is a race condition and PPI is unusable. I think that it's not good that code is not robust enough to deal with this.
Even if you forget the race condition it feels waste to lookup a handle that is already known.
I'll send non-TPM2 patches in their own patch set because they don't require to deal with sysfs attributes.
Quoted 1 line
Jason
/Jarkko
Quoted 9 lines · Jarkko Sakkinen
Quoted 4 lines
Personally, I'd sequence this commit right after your 'tpm: two-phase chip management functions' commit because it makes it much saner (no half step toward the new functions). I assume this is a theoretical problem? Or do you have a two TPM system?
This has realized in Intel NUCs where there is PTT and dTPM module. Even when PTT is selected there is still ACPI device for dTPM so three is a race condition and PPI is unusable. I think that it's not good that code is not robust enough to deal with this.
Oh OK, you should probably explain in the commit log that this is a bug fix that impacts real hardware, that qualifies it for the -stable tree.
Assuming two-phase commit is nearly ready to go, I'd still sequence this fix after two-phase for mainline and then use this patch as-is for the 3.17 -stable backport of the mainline commit.
Quoted 2 lines
Even if you forget the race condition it feels waste to lookup a handle that is already known.
There is no doubt that this new arrangement is much better than what was there before!
Thanks,
Jason
Quoted 12 lines · Jason Gunthorpe
Quoted 9 lines · Jarkko Sakkinen
Quoted 4 lines
Personally, I'd sequence this commit right after your 'tpm: two-phase chip management functions' commit because it makes it much saner (no half step toward the new functions). I assume this is a theoretical problem? Or do you have a two TPM system?
This has realized in Intel NUCs where there is PTT and dTPM module. Even when PTT is selected there is still ACPI device for dTPM so three is a race condition and PPI is unusable. I think that it's not good that code is not robust enough to deal with this.
Quoted 7 lines
Oh OK, you should probably explain in the commit log that this is a bug fix that impacts real hardware, that qualifies it for the -stable tree.
Assuming two-phase commit is nearly ready to go, I'd still sequence this fix after two-phase for mainline and then use this patch as-is for the 3.17 -stable backport of the mainline commit.
OK, makes sense. I'll try to get this done tonight.
Quoted 8 lines
Quoted 2 lines
Even if you forget the race condition it feels waste to lookup a handle that is already known.
There is no doubt that this new arrangement is much better than what was there before!
Thanks,
Jason
/Jarkko
Quoted 17 lines · Jarkko Sakkinen
Quoted 16 lines · Jason Gunthorpe
Quoted 9 lines · Jarkko Sakkinen
Quoted 4 lines
Personally, I'd sequence this commit right after your 'tpm: two-phase chip management functions' commit because it makes it much saner (no half step toward the new functions). I assume this is a theoretical problem? Or do you have a two TPM system?
This has realized in Intel NUCs where there is PTT and dTPM module. Even when PTT is selected there is still ACPI device for dTPM so three is a race condition and PPI is unusable. I think that it's not good that code is not robust enough to deal with this.
Oh OK, you should probably explain in the commit log that this is a bug fix that impacts real hardware, that qualifies it for the -stable tree.
Quoted 5 lines
Quoted 3 lines
Assuming two-phase commit is nearly ready to go, I'd still sequence this fix after two-phase for mainline and then use this patch as-is for the 3.17 -stable backport of the mainline commit.
OK, makes sense. I'll try to get this done tonight.
I propose that the current fix would be actually taken into 3.18 as it is and bigger changes would be introduced for 3.19 as the merge window is closed. I do not think it would be wise at this point to make larger structural changes.
I could however update the commit message and copyright platter (should have 2012-2014, not just 2014). What do you think? Peter?
/Jarkko