Skip to contentSkip to conversation

v4.16 tpmdd backports

PATCH In review Jarkko Sakkinen 3 participants 8 messages Apr 25, 2018 – May 1, 2018

Subsystems (3)
lore
Share & tools
lore
#3 lore raw

PATCH 3/3 tpm: add retry logic

In reply to Jarkko Sakkinen

From: James Bottomley <[email protected]>

commit e2fb992d82c626c43ed0566e07c410e56a087af3 upstream

TPM2 can return TPM2_RC_RETRY to any command and when it does we get unexpected failures inside the kernel that surprise users (this is mostly observed in the trusted key handling code). The UEFI 2.6 spec has advice on how to handle this:

The firmware SHALL not return TPM2_RC_RETRY prior to the completion of the call to ExitBootServices().

Implementer’s Note: the implementation of this function should check the return value in the TPM response and, if it is TPM2_RC_RETRY, resend the command. The implementation may abort if a sufficient number of retries has been done.

So we follow that advice in our tpm_transmit() code using TPM2_DURATION_SHORT as the initial wait duration and TPM2_DURATION_LONG as the maximum wait time. This should fix all the in-kernel use cases and also means that user space TSS implementations don't have to have their own retry handling.

---
 drivers/char/tpm/tpm-interface.c | 75 +++++++++++++++++++++++++-------
 drivers/char/tpm/tpm.h           |  1 +
 2 files changed, 61 insertions(+), 15 deletions(-)
drivers/char/tpm/tpm-interface.c +60 -15
index f71352cf7552..255db6fe15c8 100644
@@ -399,21 +399,10 @@ static void tpm_relinquish_locality(struct tpm_chip *chip)
 	chip->locality = -1;
 }
 
-/**
- * tpm_transmit - Internal kernel interface to transmit TPM commands.
- *
- * @chip: TPM chip to use
- * @space: tpm space
- * @buf: TPM command buffer
- * @bufsiz: length of the TPM command buffer
- * @flags: tpm transmit flags - bitmap
- *
- * Return:
- *     0 when the operation is successful.
- *     A negative number for system errors (errno).
- */
-ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
-		     u8 *buf, size_t bufsiz, unsigned int flags)
+static ssize_t tpm_try_transmit(struct tpm_chip *chip,
+				struct tpm_space *space,
+				u8 *buf, size_t bufsiz,
+				unsigned int flags)
 {
 	struct tpm_output_header *header = (void *)buf;
 	int rc;
@@ -544,6 +533,62 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	return rc ? rc : len;
 }
 
+/**
+ * tpm_transmit - Internal kernel interface to transmit TPM commands.
+ *
+ * @chip: TPM chip to use
+ * @space: tpm space
+ * @buf: TPM command buffer
+ * @bufsiz: length of the TPM command buffer
+ * @flags: tpm transmit flags - bitmap
+ *
+ * A wrapper around tpm_try_transmit that handles TPM2_RC_RETRY
+ * returns from the TPM and retransmits the command after a delay up
+ * to a maximum wait of TPM2_DURATION_LONG.
+ *
+ * Note: TPM1 never returns TPM2_RC_RETRY so the retry logic is TPM2
+ * only
+ *
+ * Return:
+ *     the length of the return when the operation is successful.
+ *     A negative number for system errors (errno).
+ */
+ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
+		     u8 *buf, size_t bufsiz, unsigned int flags)
+{
+	struct tpm_output_header *header = (struct tpm_output_header *)buf;
+	/* space for header and handles */
+	u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
+	unsigned int delay_msec = TPM2_DURATION_SHORT;
+	u32 rc = 0;
+	ssize_t ret;
+	const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
+				     bufsiz);
+
+	/*
+	 * Subtlety here: if we have a space, the handles will be
+	 * transformed, so when we restore the header we also have to
+	 * restore the handles.
+	 */
+	memcpy(save, buf, save_size);
+
+	for (;;) {
+		ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
+		if (ret < 0)
+			break;
+		rc = be32_to_cpu(header->return_code);
+		if (rc != TPM2_RC_RETRY)
+			break;
+		delay_msec *= 2;
+		if (delay_msec > TPM2_DURATION_LONG) {
+			dev_err(&chip->dev, "TPM is in retry loop\n");
+			break;
+		}
+		tpm_msleep(delay_msec);
+		memcpy(buf, save, save_size);
+	}
+	return ret;
+}
 /**
  * tpm_transmit_cmd - send a tpm command to the device
  *    The function extracts tpm out header return code
drivers/char/tpm/tpm.h +1 -0
index f895fba4e20d..d73f3fb81b42 100644
@@ -108,6 +108,7 @@ enum tpm2_return_codes {
 	TPM2_RC_COMMAND_CODE    = 0x0143,
 	TPM2_RC_TESTING		= 0x090A, /* RC_WARN */
 	TPM2_RC_REFERENCE_H0	= 0x0910,
+	TPM2_RC_RETRY		= 0x0922,
 };
 
 enum tpm2_algorithms {
Signature · 1 line
2.17.0
#0 lore raw

cover v4.16 tpmdd backports

"tpm: add retry logic" caused merge conflicts so I picked couple of other fixes in order to get it apply cleanly.

James Bottomley (1):
tpm: add retry logic

Tomas Winkler (1):
tpm: cmd_ready command can be issued only after granting locality

Winkler, Tomas (1):
tpm: tpm-interface: fix tpm_transmit/_cmd kdoc

 drivers/char/tpm/tpm-interface.c | 131 ++++++++++++++++++++++++-------
 drivers/char/tpm/tpm.h           |   1 +
 drivers/char/tpm/tpm_crb.c       | 108 +++++++++++++++++--------
 drivers/char/tpm/tpm_tis_core.c  |   4 +-
 include/linux/tpm.h              |   2 +-
 5 files changed, 183 insertions(+), 63 deletions(-)
Signature · 1 line
2.17.0
#1 lore raw

PATCH 1/3 tpm: cmd_ready command can be issued only after granting locality

From: Tomas Winkler <[email protected]>

commit 888d867df4417deffc33927e6fc2c6925736fe92 upstream

The correct sequence is to first request locality and only after that perform cmd_ready handshake, otherwise the hardware will drop the subsequent message as from the device point of view the cmd_ready handshake wasn't performed. Symmetrically locality has to be relinquished only after going idle handshake has completed, this requires that go_idle has to poll for the completion and as well locality relinquish has to poll for completion so it is not overridden in back to back commands flow.

Two wrapper functions are added (request_locality relinquish_locality) to simplify the error handling.

The issue is only visible on devices that support multiple localities.

Fixes 877c57d0d0ca ("tpm_crb: request and relinquish locality 0")
---
 drivers/char/tpm/tpm-interface.c |  54 ++++++++++++----
 drivers/char/tpm/tpm_crb.c       | 108 +++++++++++++++++++++----------
 drivers/char/tpm/tpm_tis_core.c  |   4 +-
 include/linux/tpm.h              |   2 +-
 4 files changed, 120 insertions(+), 48 deletions(-)
drivers/char/tpm/tpm-interface.c +41 -13
index 248c04090dea..0091eccdaf8d 100644
@@ -369,6 +369,36 @@ static int tpm_validate_command(struct tpm_chip *chip,
 	return -EINVAL;
 }
 
+static int tpm_request_locality(struct tpm_chip *chip)
+{
+	int rc;
+
+	if (!chip->ops->request_locality)
+		return 0;
+
+	rc = chip->ops->request_locality(chip, 0);
+	if (rc < 0)
+		return rc;
+
+	chip->locality = rc;
+
+	return 0;
+}
+
+static void tpm_relinquish_locality(struct tpm_chip *chip)
+{
+	int rc;
+
+	if (!chip->ops->relinquish_locality)
+		return;
+
+	rc = chip->ops->relinquish_locality(chip, chip->locality);
+	if (rc)
+		dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
+
+	chip->locality = -1;
+}
+
 /**
  * tmp_transmit - Internal kernel interface to transmit TPM commands.
  *
@@ -422,8 +452,6 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	if (!(flags & TPM_TRANSMIT_UNLOCKED))
 		mutex_lock(&chip->tpm_mutex);
 
-	if (chip->dev.parent)
-		pm_runtime_get_sync(chip->dev.parent);
 
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, true);
@@ -431,14 +459,15 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	/* Store the decision as chip->locality will be changed. */
 	need_locality = chip->locality == -1;
 
-	if (!(flags & TPM_TRANSMIT_RAW) &&
-	    need_locality && chip->ops->request_locality)  {
-		rc = chip->ops->request_locality(chip, 0);
+	if (!(flags & TPM_TRANSMIT_RAW) && need_locality) {
+		rc = tpm_request_locality(chip);
 		if (rc < 0)
 			goto out_no_locality;
-		chip->locality = rc;
 	}
 
+	if (chip->dev.parent)
+		pm_runtime_get_sync(chip->dev.parent);
+
 	rc = tpm2_prepare_space(chip, space, ordinal, buf);
 	if (rc)
 		goto out;
@@ -499,17 +528,16 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 	rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
 
 out:
-	if (need_locality && chip->ops->relinquish_locality) {
-		chip->ops->relinquish_locality(chip, chip->locality);
-		chip->locality = -1;
-	}
+	if (chip->dev.parent)
+		pm_runtime_put_sync(chip->dev.parent);
+
+	if (need_locality)
+		tpm_relinquish_locality(chip);
+
 out_no_locality:
 	if (chip->ops->clk_enable != NULL)
 		chip->ops->clk_enable(chip, false);
 
-	if (chip->dev.parent)
-		pm_runtime_put_sync(chip->dev.parent);
-
 	if (!(flags & TPM_TRANSMIT_UNLOCKED))
 		mutex_unlock(&chip->tpm_mutex);
 	return rc ? rc : len;
drivers/char/tpm/tpm_crb.c +75 -33

192 lines not on this page · raw message

drivers/char/tpm/tpm_tis_core.c +3 -1
index da074e3db19b..5a1f47b43947 100644
@@ -143,11 +143,13 @@ static bool check_locality(struct tpm_chip *chip, int l)
 	return false;
 }
 
-static void release_locality(struct tpm_chip *chip, int l)
+static int release_locality(struct tpm_chip *chip, int l)
 {
 	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
 
 	tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
+
+	return 0;
 }
 
 static int request_locality(struct tpm_chip *chip, int l)
include/linux/tpm.h +1 -1
index bcdd3790e94d..06639fb6ab85 100644
@@ -44,7 +44,7 @@ struct tpm_class_ops {
 	bool (*update_timeouts)(struct tpm_chip *chip,
 				unsigned long *timeout_cap);
 	int (*request_locality)(struct tpm_chip *chip, int loc);
-	void (*relinquish_locality)(struct tpm_chip *chip, int loc);
+	int (*relinquish_locality)(struct tpm_chip *chip, int loc);
 	void (*clk_enable)(struct tpm_chip *chip, bool value);
 };
 
Signature · 1 line
2.17.0
#2 lore raw

PATCH 2/3 tpm: tpm-interface: fix tpm_transmit/_cmd kdoc

In reply to Jarkko Sakkinen

From: "Winkler, Tomas" <[email protected]>

commit 65520d46a4adbf7f23bbb6d9b1773513f7bc7821 upstream

Fix tmp_ -> tpm_ typo and add reference to 'space' parameter in kdoc for tpm_transmit and tpm_transmit_cmd functions.

---
 drivers/char/tpm/tpm-interface.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm-interface.c +4 -2
index 0091eccdaf8d..f71352cf7552 100644
@@ -400,9 +400,10 @@ static void tpm_relinquish_locality(struct tpm_chip *chip)
 }
 
 /**
- * tmp_transmit - Internal kernel interface to transmit TPM commands.
+ * tpm_transmit - Internal kernel interface to transmit TPM commands.
  *
  * @chip: TPM chip to use
+ * @space: tpm space
  * @buf: TPM command buffer
  * @bufsiz: length of the TPM command buffer
  * @flags: tpm transmit flags - bitmap
@@ -544,10 +545,11 @@ ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
 }
 
 /**
- * tmp_transmit_cmd - send a tpm command to the device
+ * tpm_transmit_cmd - send a tpm command to the device
  *    The function extracts tpm out header return code
  *
  * @chip: TPM chip to use
+ * @space: tpm space
  * @buf: TPM command buffer
  * @bufsiz: length of the buffer
  * @min_rsp_body_length: minimum expected length of response body
Signature · 1 line
2.17.0
maintainer
#4 lore raw
In reply to Jarkko Sakkinen
Quoted 2 lines · Jarkko Sakkinen

"tpm: add retry logic" caused merge conflicts so I picked couple of other fixes in order to get it apply cleanly.

Are these only needed in 4.16.y? Nothing earlier?

thanks for the backports, I'll queue them up after this next 4.16.y kernel is released at the end of this week.

greg k-h

maintainer
#5 lore raw
Quoted 5 lines · Greg KH
Quoted 2 lines · Jarkko Sakkinen

"tpm: add retry logic" caused merge conflicts so I picked couple of other fixes in order to get it apply cleanly.

Are these only needed in 4.16.y? Nothing earlier?

The retry one (tpm: add retry logic) could go back as far as you can, but the bug it causes is rarely seen: mostly it's a failure of the kernel trusted keys due to a tpm retry being interpreted as a fatal error. The number of users we have for kernel trusted keys seems to be pretty small ...

I'd say if the backport works as is, go for it, but if we get a patch apply failure, it's probably not worth trying to work out how to modify the patch again until someone actually complains about the problem.

James

maintainer
#6 lore raw
Quoted 13 lines · James Bottomley
Quoted 5 lines · Greg KH
Quoted 2 lines · Jarkko Sakkinen

"tpm: add retry logic" caused merge conflicts so I picked couple of other fixes in order to get it apply cleanly.

Are these only needed in 4.16.y?  Nothing earlier?

The retry one (tpm: add retry logic) could go back as far as you can, but the bug it causes is rarely seen: mostly it's a failure of the kernel trusted keys due to a tpm retry being interpreted as a fatal error. The number of users we have for kernel trusted keys seems to be pretty small ...

Quoted 3 lines

I'd say if the backport works as is, go for it, but if we get a patch apply failure, it's probably not worth trying to work out how to modify the patch again until someone actually complains about the problem.

They seem to all work for 4.14.y and 4.16.y, so now queued up, thanks.

greg k-h

#7 lore raw
Quoted 14 lines · Greg KH
Quoted 13 lines · James Bottomley
Quoted 5 lines · Greg KH
Quoted 2 lines · Jarkko Sakkinen

"tpm: add retry logic" caused merge conflicts so I picked couple of other fixes in order to get it apply cleanly.

Are these only needed in 4.16.y? Nothing earlier?

The retry one (tpm: add retry logic) could go back as far as you can, but the bug it causes is rarely seen: mostly it's a failure of the kernel trusted keys due to a tpm retry being interpreted as a fatal error. The number of users we have for kernel trusted keys seems to be pretty small ...

Quoted 7 lines
Quoted 3 lines

I'd say if the backport works as is, go for it, but if we get a patch apply failure, it's probably not worth trying to work out how to modify the patch again until someone actually complains about the problem.

They seem to all work for 4.14.y and 4.16.y, so now queued up, thanks.

greg k-h

+1 what James said. Thank you.

/Jarkko