[PATCH 1/8] tpm: block messages while suspended

From: Stephen Boyd
Date: Thu Jun 13 2019 - 14:14:18 EST


From: Andrey Pronin <apronin@xxxxxxxxxxxx>

Other drivers or userspace may initiate sending a message to the tpm
while the device itself and the controller of the bus it is on are
suspended. That may break the bus driver logic.
Block sending messages while the device is suspended.

Signed-off-by: Andrey Pronin <apronin@xxxxxxxxxxxx>
Signed-off-by: Stephen Boyd <swboyd@xxxxxxxxxxxx>
---

I don't think this was ever posted before.

drivers/char/tpm/tpm-interface.c | 16 ++++++++++++++--
include/linux/tpm.h | 2 ++
2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index ae1030c9b086..7232527652e8 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -86,6 +86,11 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
return -E2BIG;
}

+ if (test_bit(0, &chip->is_suspended)) {
+ dev_warn(&chip->dev, "blocking transmit while suspended\n");
+ return -EAGAIN;
+ }
+
rc = chip->ops->send(chip, buf, count);
if (rc < 0) {
if (rc != -EPIPE)
@@ -403,14 +408,19 @@ int tpm_pm_suspend(struct device *dev)
return 0;

if (!tpm_chip_start(chip)) {
- if (chip->flags & TPM_CHIP_FLAG_TPM2)
+ if (chip->flags & TPM_CHIP_FLAG_TPM2) {
tpm2_shutdown(chip, TPM2_SU_STATE);
- else
+ set_bit(0, &chip->is_suspended);
+ } else {
rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
+ }

tpm_chip_stop(chip);
}

+ if (!rc)
+ set_bit(0, &chip->is_suspended);
+
return rc;
}
EXPORT_SYMBOL_GPL(tpm_pm_suspend);
@@ -426,6 +436,8 @@ int tpm_pm_resume(struct device *dev)
if (chip == NULL)
return -ENODEV;

+ clear_bit(0, &chip->is_suspended);
+
return 0;
}
EXPORT_SYMBOL_GPL(tpm_pm_resume);
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 1b5436b213a2..48df005228d0 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -132,6 +132,8 @@ struct tpm_chip {
int dev_num; /* /dev/tpm# */
unsigned long is_open; /* only one allowed */

+ unsigned long is_suspended;
+
char hwrng_name[64];
struct hwrng hwrng;

--
Sent by a computer through tubes