Some TPMs stop responding at runtime, hours to months after they passed
probe. The first sign is usually "invalid TPM_STS.x 0xff", and from then
on every command fails only after the transport timeout. A TPM that is
already dead at boot fails to probe and never registers, which this
patch does not change.

The callers keep sending more commands: the hwrng thread every 10
seconds forever, and userspace through /dev/tpmrm0. Each attempt holds
tpm_mutex for the whole timeout and logs an error:

  tpm tpm0: invalid TPM_STS.x 0xff, dumping stack for forensics
  tpm tpm0: tpm_try_transmit: send(): error -62
  tpm tpm0: tpm_try_transmit: send(): error -62
  ...

The core already stops using a TPM it cannot trust: tpm_try_get_ops()
fails with -EIO once TPM_CHIP_FLAG_DISABLE is set, which
tpm2_load_null() does when it cannot restore the null key. Set the same
flag when two commands in a row time out.

The first timeout sets TPM_CHIP_FLAG_MISSED_RESPONSE, and a command that
gets an answer, whatever its TPM return code, clears it.

With interrupts, the wait helpers in tpm_tis_core, st33zp24 and
xen-tpmfront return -ETIME as soon as a signal is pending, so an -ETIME
with a signal pending is not counted. Otherwise any user could disable
the TPM by interrupting two reads of a PCR file in sysfs.

Some TPM 1.2 chips do not get ready while their self test runs, and
tpm1_do_selftest() keeps polling through those timeouts, see
commit 4643826a3da1 ("TPM: Work around buggy TPMs that block during
continue self test"). It now clears the flag after each of them.

A disabled TPM stays disabled until the driver is rebound or the machine
reboots.

Suggested-by: Jarkko Sakkinen <[email protected]>
Signed-off-by: Breno Leitao <[email protected]>
---
 drivers/char/tpm/tpm-interface.c | 15 +++++++++++++++
 drivers/char/tpm/tpm1-cmd.c      |  1 +
 include/linux/tpm.h              |  1 +
 3 files changed, 17 insertions(+)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index e0385b16616b46..cb794cfc5f63e8 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -26,6 +26,7 @@
 #include <linux/spinlock.h>
 #include <linux/suspend.h>
 #include <linux/freezer.h>
+#include <linux/sched/signal.h>
 #include <linux/tpm_eventlog.h>
 
 #include "tpm.h"
@@ -268,6 +269,20 @@ ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, 
size_t bufsiz)
                delay_msec *= 2;
                memcpy(buf, save, save_size);
        }
+
+       if (ret >= 0)
+               chip->flags &= ~TPM_CHIP_FLAG_MISSED_RESPONSE;
+
+       /* Some drivers also return -ETIME when a signal interrupts the wait. */
+       if (ret == -ETIME && !signal_pending(current)) {
+               if (!(chip->flags & TPM_CHIP_FLAG_MISSED_RESPONSE)) {
+                       chip->flags |= TPM_CHIP_FLAG_MISSED_RESPONSE;
+               } else if (!(chip->flags & TPM_CHIP_FLAG_DISABLE)) {
+                       dev_err(&chip->dev,
+                               "two commands in a row timed out, disabling the 
TPM\n");
+                       chip->flags |= TPM_CHIP_FLAG_DISABLE;
+               }
+       }
        return ret;
 }
 
diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
index 7a8606e8c7117b..e4e1f0799da196 100644
--- a/drivers/char/tpm/tpm1-cmd.c
+++ b/drivers/char/tpm/tpm1-cmd.c
@@ -647,6 +647,7 @@ int tpm1_do_selftest(struct tpm_chip *chip)
                 * until the self test duration expires.
                 */
                if (rc == -ETIME) {
+                       chip->flags &= ~TPM_CHIP_FLAG_MISSED_RESPONSE;
                        dev_info(&chip->dev, HW_ERR "TPM command timed out 
during continue self test");
                        tpm_msleep(delay_msec);
                        continue;
diff --git a/include/linux/tpm.h b/include/linux/tpm.h
index 0db277af45c393..0f97abc82846ae 100644
--- a/include/linux/tpm.h
+++ b/include/linux/tpm.h
@@ -197,6 +197,7 @@ enum tpm_chip_flags {
        TPM_CHIP_FLAG_HWRNG_DISABLED            = BIT(9),
        TPM_CHIP_FLAG_DISABLE                   = BIT(10),
        TPM_CHIP_FLAG_SYNC                      = BIT(11),
+       TPM_CHIP_FLAG_MISSED_RESPONSE           = BIT(12),
 };
 
 #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)

-- 
2.53.1-Meta


Reply via email to