Skip to content

Commit 0f4fa21

Browse files
Josh Zimmermangregkh
authored andcommitted
tpm: Issue a TPM2_Shutdown for TPM2 devices.
commit d1bd4a792d3961a04e6154118816b00167aad91a upstream. If a TPM2 loses power without a TPM2_Shutdown command being issued (a "disorderly reboot"), it may lose some state that has yet to be persisted to NVRam, and will increment the DA counter. After the DA counter gets sufficiently large, the TPM will lock the user out. NOTE: This only changes behavior on TPM2 devices. Since TPM1 uses sysfs, and sysfs relies on implicit locking on chip->ops, it is not safe to allow this code to run in TPM1, or to add sysfs support to TPM2, until that locking is made explicit. Signed-off-by: Josh Zimmerman <joshz@google.com> Cc: stable@vger.kernel.org Fixes: 74d6b3c ("tpm: fix suspend/resume paths for TPM 2.0") Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: James Morris <james.l.morris@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5c9a297 commit 0f4fa21

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

drivers/char/tpm/tpm-chip.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,41 @@ static void tpm_dev_release(struct device *dev)
124124
kfree(chip);
125125
}
126126

127+
128+
/**
129+
* tpm_class_shutdown() - prepare the TPM device for loss of power.
130+
* @dev: device to which the chip is associated.
131+
*
132+
* Issues a TPM2_Shutdown command prior to loss of power, as required by the
133+
* TPM 2.0 spec.
134+
* Then, calls bus- and device- specific shutdown code.
135+
*
136+
* XXX: This codepath relies on the fact that sysfs is not enabled for
137+
* TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2
138+
* has sysfs support enabled before TPM sysfs's implicit locking is fixed.
139+
*/
140+
static int tpm_class_shutdown(struct device *dev)
141+
{
142+
struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
143+
144+
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
145+
down_write(&chip->ops_sem);
146+
tpm2_shutdown(chip, TPM2_SU_CLEAR);
147+
chip->ops = NULL;
148+
up_write(&chip->ops_sem);
149+
}
150+
/* Allow bus- and device-specific code to run. Note: since chip->ops
151+
* is NULL, more-specific shutdown code will not be able to issue TPM
152+
* commands.
153+
*/
154+
if (dev->bus && dev->bus->shutdown)
155+
dev->bus->shutdown(dev);
156+
else if (dev->driver && dev->driver->shutdown)
157+
dev->driver->shutdown(dev);
158+
return 0;
159+
}
160+
161+
127162
/**
128163
* tpmm_chip_alloc() - allocate a new struct tpm_chip instance
129164
* @dev: device to which the chip is associated
@@ -166,6 +201,7 @@ struct tpm_chip *tpmm_chip_alloc(struct device *dev,
166201
dev_set_drvdata(dev, chip);
167202

168203
chip->dev.class = tpm_class;
204+
chip->dev.class->shutdown = tpm_class_shutdown;
169205
chip->dev.release = tpm_dev_release;
170206
chip->dev.parent = dev;
171207
#ifdef CONFIG_ACPI

drivers/char/tpm/tpm-sysfs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ static const struct attribute_group tpm_dev_group = {
284284
int tpm_sysfs_add_device(struct tpm_chip *chip)
285285
{
286286
int err;
287+
288+
/* XXX: If you wish to remove this restriction, you must first update
289+
* tpm_sysfs to explicitly lock chip->ops.
290+
*/
291+
if (chip->flags & TPM_CHIP_FLAG_TPM2)
292+
return 0;
293+
287294
err = sysfs_create_group(&chip->dev.parent->kobj,
288295
&tpm_dev_group);
289296

0 commit comments

Comments
 (0)