Re: [PATCH] tpm_tis: verify locality released before returning from release_locality

From: Laurent Bigonville
Date: Fri May 11 2018 - 15:02:49 EST


Le 05/05/18 Ã 22:03, Jerry Snitselaar a ÃcritÂ:
On Sat May 05 18, Jerry Snitselaar wrote:
For certain tpm chips releasing locality can take long enough that a
subsequent call to request_locality will see the locality as being
active when the access register is read in check_locality. So check
that the locality has been released before returning from
release_locality.

Cc: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxxxxxxxx>
Cc: Peter Huewe <peterhuewe@xxxxxx>
Cc: Jason Gunthorpe <jgg@xxxxxxxx>
Reported-by: Laurent Bigonville <bigon@xxxxxxxxxx>
Signed-off-by: Jerry Snitselaar <jsnitsel@xxxxxxxxxx>
Tested-by: Laurent Bigonville <bigon@xxxxxxxxxx>
---
drivers/char/tpm/tpm_tis_core.c | 47 ++++++++++++++++++++++++++++++++++++++++-
1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c
index 5a1f47b43947..d547cd309dbd 100644
--- a/drivers/char/tpm/tpm_tis_core.c
+++ b/drivers/char/tpm/tpm_tis_core.c
@@ -143,13 +143,58 @@ static bool check_locality(struct tpm_chip *chip, int l)
ÂÂÂÂreturn false;
}

+static bool locality_inactive(struct tpm_chip *chip, int l)
+{
+ÂÂÂ struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
+ÂÂÂ int rc;
+ÂÂÂ u8 access;
+
+ÂÂÂ rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
+ÂÂÂ if (rc < 0)
+ÂÂÂÂÂÂÂ return false;
+
+ÂÂÂ if ((access & (TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY))
+ÂÂÂÂÂÂÂ == TPM_ACCESS_VALID)
+ÂÂÂÂÂÂÂ return true;
+
+ÂÂÂ return false;
+}
+
static int release_locality(struct tpm_chip *chip, int l)
{
ÂÂÂÂstruct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
+ÂÂÂ unsigned long stop, timeout;
+ÂÂÂ long rc;

ÂÂÂÂtpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);

-ÂÂÂ return 0;
+ÂÂÂ stop = jiffies + chip->timeout_a;
+
+ÂÂÂ if (chip->flags & TPM_CHIP_FLAG_IRQ) {
+again:
+ÂÂÂÂÂÂÂ timeout = stop - jiffies;
+ÂÂÂÂÂÂÂ if ((long)timeout <= 0)
+ÂÂÂÂÂÂÂÂÂÂÂ return -1;
+
+ÂÂÂÂÂÂÂ rc = wait_event_interruptible_timeout(priv->int_queue,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ (locality_inactive(chip, l)),
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ timeout);
+
+ÂÂÂÂÂÂÂ if (rc > 0)
+ÂÂÂÂÂÂÂÂÂÂÂ return 0;
+
+ÂÂÂÂÂÂÂ if (rc == -ERESTARTSYS && freezing(current)) {
+ÂÂÂÂÂÂÂÂÂÂÂ clear_thread_flag(TIF_SIGPENDING);
+ÂÂÂÂÂÂÂÂÂÂÂ goto again;
+ÂÂÂÂÂÂÂ }
+ÂÂÂ } else {
+ÂÂÂÂÂÂÂ do {
+ÂÂÂÂÂÂÂÂÂÂÂ if (locality_inactive(chip, l))
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return 0;
+ÂÂÂÂÂÂÂÂÂÂÂ tpm_msleep(TPM_TIMEOUT);
+ÂÂÂÂÂÂÂ } while (time_before(jiffies, stop));
+ÂÂÂ }
+ÂÂÂ return -1;
}

static int request_locality(struct tpm_chip *chip, int l)
--
2.15.0


Laurent,

Can you try this patch with your system since it is the one
that has exhibited the problem so far. I've tested on a
tpm2.0 and tpm1.2 system here.

Regards,
Jerry