Skip to contentSkip to conversation

Migrate all TPM 2.0 commands to use struct tpm_buf

PATCH Superseded Jarkko Sakkinen 5 participants 17 messages Mar 5, 2018 – Mar 19, 2018

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Migrate all TPM 2.0 commands to use struct tpm_buf

In order to make struct tpm_buf the first class object for constructing TPM commands, this patch set migrates all TPM 2.0 commands to use it. The next step after this is to migrate TPM 1.2 commands in a subsequent patch set. Finally, tpm_transmit_cmd() can take simply struct tpm_buf as its argument and this interface can be exported to be used by the kernel keyring.

The ultimate goal of this work is to make constructing TPM commands inside the kernel simple and robust.

v3:
* Fixed error handling in tpm2_get_random()

v2:
* Fixed author information in the commit that fixes the self-test issue, removed '\n' from the log message and added the missing tested-by. (James: sincere apologies about this)
* Removed the redundant "out of memory" log message from tpm2_shutdown().
* tpm_buf_destroy() was called before using the response data in tpm2_probe().
* Added missing tpm_buf_destroy() to tpm2_get_random().

James Bottomley (1):
tpm: fix intermittent failure with self tests

Jarkko Sakkinen (4):
tpm: migrate tpm2_shutdown() to use struct tpm_buf
tpm: migrate tpm2_probe() to use struct tpm_buf
tpm: migrate tpm2_get_tpm_pt() to use struct tpm_buf
tpm: migrate tpm2_get_random() to use struct tpm_buf

 drivers/char/tpm/tpm-interface.c |  20 ++-
 drivers/char/tpm/tpm.h           |  20 +--
 drivers/char/tpm/tpm2-cmd.c      | 286 ++++++++++++++-------------------------
 3 files changed, 132 insertions(+), 194 deletions(-)
Signature · 1 line
2.15.1
#1 lore raw

PATCH v3 1/5 tpm: fix intermittent failure with self tests

From: James Bottomley <[email protected]>

My Nuvoton 6xx in a Dell XPS-13 has been intermittently failing to work (necessitating a reboot). The problem seems to be that the TPM gets into a state where the partial self-test doesn't return TPM_RC_SUCCESS (meaning all tests have run to completion), but instead returns TPM_RC_TESTING (meaning some tests are still running in the background). There are various theories that resending the self-test command actually causes the tests to restart and thus triggers more TPM_RC_TESTING returns until the timeout is exceeded.

There are several issues here: firstly being we shouldn't slow down the boot sequence waiting for the self test to complete once the TPM backgrounds them. It will actually make available all functions that have passed and if it gets a failure return TPM_RC_FAILURE to every subsequent command. So the fix is to kick off self tests once and if they return TPM_RC_TESTING log that as a backgrounded self test and continue on. In order to prevent other tpm users from seeing any TPM_RC_TESTING returns (which it might if they send a command that needs a TPM subsystem which is still under test), we loop in tpm_transmit_cmd until either a timeout or we don't get a TPM_RC_TESTING return.

Finally, there have been observations of strange returns from a partial test. One Nuvoton is occasionally returning TPM_RC_COMMAND_CODE, so treat any unexpected return from a partial self test as an indication we need to run a full self test.

[[email protected]: cleaned up James' original commit and
added a proper Fixes line]

Fixes 2482b1bba5122 ("tpm: Trigger only missing TPM 2.0 self tests")
---
 drivers/char/tpm/tpm-interface.c | 20 ++++++++++++---
 drivers/char/tpm/tpm.h           |  1 +
 drivers/char/tpm/tpm2-cmd.c      | 54 ++++++++++++----------------------------
 3 files changed, 33 insertions(+), 42 deletions(-)
drivers/char/tpm/tpm-interface.c +16 -4
index 9e80a953d693..1adb976a2e37 100644
@@ -537,14 +537,26 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
 			 const char *desc)
 {
 	const struct tpm_output_header *header = buf;
+	unsigned int delay_msec = TPM2_DURATION_SHORT;
 	int err;
 	ssize_t len;
 
-	len = tpm_transmit(chip, space, (u8 *)buf, bufsiz, flags);
-	if (len <  0)
-		return len;
+	for (;;) {
+		len = tpm_transmit(chip, space, (u8 *)buf, bufsiz, flags);
+		if (len <  0)
+			return len;
+		err = be32_to_cpu(header->return_code);
+		if (err != TPM2_RC_TESTING)
+			break;
+
+		delay_msec *= 2;
+		if (delay_msec > TPM2_DURATION_LONG) {
+			dev_err(&chip->dev, "the self test is still running\n");
+			break;
+		}
+		tpm_msleep(delay_msec);
+	}
 
-	err = be32_to_cpu(header->return_code);
 	if (err != 0 && desc)
 		dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
 			desc);
drivers/char/tpm/tpm.h +1 -0
index f895fba4e20d..cccd5994a0e1 100644
@@ -104,6 +104,7 @@ enum tpm2_return_codes {
 	TPM2_RC_HASH		= 0x0083, /* RC_FMT1 */
 	TPM2_RC_HANDLE		= 0x008B,
 	TPM2_RC_INITIALIZE	= 0x0100, /* RC_VER1 */
+	TPM2_RC_FAILURE		= 0x0101,
 	TPM2_RC_DISABLED	= 0x0120,
 	TPM2_RC_COMMAND_CODE    = 0x0143,
 	TPM2_RC_TESTING		= 0x090A, /* RC_WARN */
drivers/char/tpm/tpm2-cmd.c +16 -38

96 lines not on this page · raw message

Signature · 1 line
2.15.1
#5 lore raw

PATCH v3 2/5 tpm: migrate tpm2_shutdown() to use struct tpm_buf

In reply to Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_shutdown() to use it. In addition, removed the klog entry when tpm_transmit_cmd() fails because tpm_tansmit_cmd() already prints an error message.

---
 drivers/char/tpm/tpm2-cmd.c | 44 ++++++++++++--------------------------------
 1 file changed, 12 insertions(+), 32 deletions(-)
drivers/char/tpm/tpm2-cmd.c +12 -32
index 89a5397b18d2..abe6ef4a7a0b 100644
@@ -27,10 +27,6 @@ enum tpm2_session_attributes {
 	TPM2_SA_CONTINUE_SESSION	= BIT(0),
 };
 
-struct tpm2_startup_in {
-	__be16	startup_type;
-} __packed;
-
 struct tpm2_get_tpm_pt_in {
 	__be32	cap_id;
 	__be32	property_id;
@@ -55,7 +51,6 @@ struct tpm2_get_random_out {
 } __packed;
 
 union tpm2_cmd_params {
-	struct	tpm2_startup_in		startup_in;
 	struct	tpm2_get_tpm_pt_in	get_tpm_pt_in;
 	struct	tpm2_get_tpm_pt_out	get_tpm_pt_out;
 	struct	tpm2_get_random_in	getrandom_in;
@@ -410,11 +405,8 @@ void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
 	int rc;
 
 	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
-	if (rc) {
-		dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
-			 handle);
+	if (rc)
 		return;
-	}
 
 	tpm_buf_append_u32(&buf, handle);
 
@@ -760,40 +752,28 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,  u32 *value,
 }
 EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
 
-#define TPM2_SHUTDOWN_IN_SIZE \
-	(sizeof(struct tpm_input_header) + \
-	 sizeof(struct tpm2_startup_in))
-
-static const struct tpm_input_header tpm2_shutdown_header = {
-	.tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
-	.length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
-	.ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
-};
-
 /**
  * tpm2_shutdown() - send shutdown command to the TPM chip
  *
+ * In places where shutdown command is sent there's no much we can do except
+ * print the error code on a system failure.
+ *
  * @chip:		TPM chip to use.
  * @shutdown_type:	shutdown type. The value is either
  *			TPM_SU_CLEAR or TPM_SU_STATE.
  */
 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
 {
-	struct tpm2_cmd cmd;
+	struct tpm_buf buf;
 	int rc;
 
-	cmd.header.in = tpm2_shutdown_header;
-	cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
-
-	rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
-			      "stopping the TPM");
-
-	/* In places where shutdown command is sent there's no much we can do
-	 * except print the error code on a system failure.
-	 */
-	if (rc < 0 && rc != -EPIPE)
-		dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
-			 rc);
+	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
+	if (rc)
+		return;
+	tpm_buf_append_u16(&buf, shutdown_type);
+	tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
+			 "stopping the TPM");
+	tpm_buf_destroy(&buf);
 }
 
 /*
Signature · 1 line
2.15.1
#6 lore raw

PATCH v3 3/5 tpm: migrate tpm2_probe() to use struct tpm_buf

In reply to Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_probe() to use it.

---
 drivers/char/tpm/tpm2-cmd.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
drivers/char/tpm/tpm2-cmd.c +15 -12
index abe6ef4a7a0b..890d83c5c78b 100644
@@ -851,22 +851,25 @@ static int tpm2_do_selftest(struct tpm_chip *chip)
  */
 int tpm2_probe(struct tpm_chip *chip)
 {
-	struct tpm2_cmd cmd;
+	struct tpm_output_header *out;
+	struct tpm_buf buf;
 	int rc;
 
-	cmd.header.in = tpm2_get_tpm_pt_header;
-	cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
-	cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
-	cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
-
-	rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
-	if (rc <  0)
+	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
+	if (rc)
 		return rc;
-
-	if (be16_to_cpu(cmd.header.out.tag) = TPM2_ST_NO_SESSIONS)
+	tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
+	tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
+	tpm_buf_append_u32(&buf, 1);
+	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
+	if (rc <  0)
+		goto out;
+	out = (struct tpm_output_header *)buf.data;
+	if (be16_to_cpu(out->tag) = TPM2_ST_NO_SESSIONS)
 		chip->flags |= TPM_CHIP_FLAG_TPM2;
-
-	return 0;
+out:
+	tpm_buf_destroy(&buf);
+	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm2_probe);
 
Signature · 1 line
2.15.1
#15 lore raw

PATCH v3 4/5 tpm: migrate tpm2_get_tpm_pt() to use struct tpm_buf

In reply to Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_get_tpm_pt() to use it.

---
 drivers/char/tpm/tpm2-cmd.c | 63 +++++++++++++++++----------------------------
 1 file changed, 23 insertions(+), 40 deletions(-)
drivers/char/tpm/tpm2-cmd.c +23 -40
index 890d83c5c78b..e02f7d46e9ac 100644
@@ -27,20 +27,6 @@ enum tpm2_session_attributes {
 	TPM2_SA_CONTINUE_SESSION	= BIT(0),
 };
 
-struct tpm2_get_tpm_pt_in {
-	__be32	cap_id;
-	__be32	property_id;
-	__be32	property_cnt;
-} __packed;
-
-struct tpm2_get_tpm_pt_out {
-	u8	more_data;
-	__be32	subcap_id;
-	__be32	property_cnt;
-	__be32	property_id;
-	__be32	value;
-} __packed;
-
 struct tpm2_get_random_in {
 	__be16	size;
 } __packed;
@@ -51,8 +37,6 @@ struct tpm2_get_random_out {
 } __packed;
 
 union tpm2_cmd_params {
-	struct	tpm2_get_tpm_pt_in	get_tpm_pt_in;
-	struct	tpm2_get_tpm_pt_out	get_tpm_pt_out;
 	struct	tpm2_get_random_in	getrandom_in;
 	struct	tpm2_get_random_out	getrandom_out;
 };
@@ -377,19 +361,6 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
 	return total ? total : -EIO;
 }
 
-#define TPM2_GET_TPM_PT_IN_SIZE \
-	(sizeof(struct tpm_input_header) + \
-	 sizeof(struct tpm2_get_tpm_pt_in))
-
-#define TPM2_GET_TPM_PT_OUT_BODY_SIZE \
-	 sizeof(struct tpm2_get_tpm_pt_out)
-
-static const struct tpm_input_header tpm2_get_tpm_pt_header = {
-	.tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
-	.length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
-	.ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
-};
-
 /**
  * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
  * @chip: TPM chip to use
@@ -723,6 +694,14 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
 	return rc;
 }
 
+struct tpm2_get_cap_out {
+	u8 more_data;
+	__be32 subcap_id;
+	__be32 property_cnt;
+	__be32 property_id;
+	__be32 value;
+} __packed;
+
 /**
  * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  * @chip:		TPM chip to use.
@@ -735,19 +714,23 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,  u32 *value,
 			const char *desc)
 {
-	struct tpm2_cmd cmd;
+	struct tpm2_get_cap_out *out;
+	struct tpm_buf buf;
 	int rc;
 
-	cmd.header.in = tpm2_get_tpm_pt_header;
-	cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
-	cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
-	cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
-
-	rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
-			      TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
-	if (!rc)
-		*value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
-
+	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
+	if (rc)
+		return rc;
+	tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
+	tpm_buf_append_u32(&buf, property_id);
+	tpm_buf_append_u32(&buf, 1);
+	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
+	if (!rc) {
+		out = (struct tpm2_get_cap_out *)
+			&buf.data[TPM_HEADER_SIZE];
+		*value = be32_to_cpu(out->value);
+	}
+	tpm_buf_destroy(&buf);
 	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
Signature · 1 line
2.15.1
#16 lore raw

PATCH v3 5/5 tpm: migrate tpm2_get_random() to use struct tpm_buf

In reply to Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_get_random() to use it. In addition, removed remaining references to struct tpm2_cmd. All of them use it to acquire the length of the response, which can be achieved by using tpm_buf_length().

---
 drivers/char/tpm/tpm.h      | 19 ++++-----
 drivers/char/tpm/tpm2-cmd.c | 98 ++++++++++++++++++---------------------------
 2 files changed, 49 insertions(+), 68 deletions(-)
drivers/char/tpm/tpm.h +10 -9
index cccd5994a0e1..29c0717437bc 100644
@@ -412,23 +412,24 @@ struct tpm_buf {
 	u8 *data;
 };
 
-static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
+static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
 {
 	struct tpm_input_header *head;
+	head = (struct tpm_input_header *)buf->data;
+	head->tag = cpu_to_be16(tag);
+	head->length = cpu_to_be32(sizeof(*head));
+	head->ordinal = cpu_to_be32(ordinal);
+}
 
+static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
+{
 	buf->data_page = alloc_page(GFP_HIGHUSER);
 	if (!buf->data_page)
 		return -ENOMEM;
 
 	buf->flags = 0;
 	buf->data = kmap(buf->data_page);
-
-	head = (struct tpm_input_header *) buf->data;
-
-	head->tag = cpu_to_be16(tag);
-	head->length = cpu_to_be32(sizeof(*head));
-	head->ordinal = cpu_to_be32(ordinal);
-
+	tpm_buf_reset(buf, tag, ordinal);
 	return 0;
 }
 
@@ -557,7 +558,7 @@ static inline u32 tpm2_rc_value(u32 rc)
 int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
 int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
 		    struct tpm2_digest *digests);
-int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max);
+int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);
 void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
 			    unsigned int flags);
 int tpm2_seal_trusted(struct tpm_chip *chip,
drivers/char/tpm/tpm2-cmd.c +39 -59

162 lines not on this page · raw message

Signature · 1 line
2.15.1
#7 lore raw

Re: [PATCH v3 3/5] tpm: migrate tpm2_probe() to use struct tpm_buf

In reply to Jarkko Sakkinen

Hi Jarrko,

Quoted 4 lines · Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_probe() to use it.

With this patch, the Pi doesn't find the TPM.  I'm seeing the following line in dmesg.

[    1.087414] tpm_tis_spi: probe of spi0.0 failed with error 256

Mimi

Quoted 46 lines
---
 drivers/char/tpm/tpm2-cmd.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
drivers/char/tpm/tpm2-cmd.c +15 -12
index abe6ef4a7a0b..890d83c5c78b 100644
@@ -851,22 +851,25 @@ static int tpm2_do_selftest(struct tpm_chip *chip)
  */
 int tpm2_probe(struct tpm_chip *chip)
 {
-	struct tpm2_cmd cmd;
+	struct tpm_output_header *out;
+	struct tpm_buf buf;
 	int rc;
 
-	cmd.header.in = tpm2_get_tpm_pt_header;
-	cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
-	cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
-	cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
-
-	rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
-	if (rc <  0)
+	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
+	if (rc)
 		return rc;
-
-	if (be16_to_cpu(cmd.header.out.tag) = TPM2_ST_NO_SESSIONS)
+	tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
+	tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
+	tpm_buf_append_u32(&buf, 1);
+	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
+	if (rc <  0)
+		goto out;
+	out = (struct tpm_output_header *)buf.data;
+	if (be16_to_cpu(out->tag) = TPM2_ST_NO_SESSIONS)
 		chip->flags |= TPM_CHIP_FLAG_TPM2;
-
-	return 0;
+out:
+	tpm_buf_destroy(&buf);
+	return rc;
 }
 EXPORT_SYMBOL_GPL(tpm2_probe);
 
#12 lore raw

Re: [PATCH v3 3/5] tpm: migrate tpm2_probe() to use struct tpm_buf

Quoted 7 lines · Jarkko Sakkinen
Quoted 4 lines · Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_probe() to use it.

Jason, is this now OK? I removed the error klog.

Sure the log message is gone - but I haven't been auditing your tpm_buf work..

Jason

Acked-by on v3 3/5
#13 lore raw

Re: [PATCH v3 3/5] tpm: migrate tpm2_probe() to use struct tpm_buf

In reply to Jarkko Sakkinen
Quoted 37 lines · Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_probe() to use it.

---
  drivers/char/tpm/tpm2-cmd.c | 27 +++++++++++++++------------
  1 file changed, 15 insertions(+), 12 deletions(-)
drivers/char/tpm/tpm2-cmd.c +8 -10
index abe6ef4a7a0b..890d83c5c78b 100644
@@ -851,22 +851,25 @@ static int tpm2_do_selftest(struct tpm_chip *chip)
   */
  int tpm2_probe(struct tpm_chip *chip)
  {
-	struct tpm2_cmd cmd;
+	struct tpm_output_header *out;
+	struct tpm_buf buf;
  	int rc;
 
-	cmd.header.in = tpm2_get_tpm_pt_header;
-	cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
-	cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
-	cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
-
-	rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
-	if (rc <  0)
+	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
+	if (rc)
  		return rc;
-
-	if (be16_to_cpu(cmd.header.out.tag) = TPM2_ST_NO_SESSIONS)
+	tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
+	tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
+	tpm_buf_append_u32(&buf, 1);
+	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);

drivers/char/tpm/tpm2-cmd.c static int tpm2_do_selftest(struct tpm_chip *chip) · line 872

Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +7 -2
+	if (rc <  0)
+		goto out;
+	out = (struct tpm_output_header *)buf.data;
+	if (be16_to_cpu(out->tag) = TPM2_ST_NO_SESSIONS)
  		chip->flags |= TPM_CHIP_FLAG_TPM2;
-
-	return 0;
+out:
+	tpm_buf_destroy(&buf);
+	return rc;

Looks better :-).

Quoted 3 lines
drivers/char/tpm/tpm2-cmd.c +0 -0
  }
  EXPORT_SYMBOL_GPL(tpm2_probe);
 
#2 lore raw

Re: [PATCH v3 1/5] tpm: fix intermittent failure with self tests

In reply to Jarkko Sakkinen
Quoted 27 lines · Jarkko Sakkinen

From: James Bottomley <[email protected]>

My Nuvoton 6xx in a Dell XPS-13 has been intermittently failing to work (necessitating a reboot). The problem seems to be that the TPM gets into a state where the partial self-test doesn't return TPM_RC_SUCCESS (meaning all tests have run to completion), but instead returns TPM_RC_TESTING (meaning some tests are still running in the background). There are various theories that resending the self-test command actually causes the tests to restart and thus triggers more TPM_RC_TESTING returns until the timeout is exceeded.

There are several issues here: firstly being we shouldn't slow down the boot sequence waiting for the self test to complete once the TPM backgrounds them. It will actually make available all functions that have passed and if it gets a failure return TPM_RC_FAILURE to every subsequent command. So the fix is to kick off self tests once and if they return TPM_RC_TESTING log that as a backgrounded self test and continue on. In order to prevent other tpm users from seeing any TPM_RC_TESTING returns (which it might if they send a command that needs a TPM subsystem which is still under test), we loop in tpm_transmit_cmd until either a timeout or we don't get a TPM_RC_TESTING return.

Finally, there have been observations of strange returns from a partial test. One Nuvoton is occasionally returning TPM_RC_COMMAND_CODE, so treat any unexpected return from a partial self test as an indication we need to run a full self test.

Quoted 8 lines

[[email protected]: cleaned up James' original commit and
added a proper Fixes line]

Fixes 2482b1bba5122 ("tpm: Trigger only missing TPM 2.0 self tests")

Already applied to my master (bleeding edge) branch in order to facilitate testing/review:

git://git.infradead.org/users/jjs/linux-tpmdd.git

I also rewrote the description of the updatesthat I did to this commit to be more precise (see the commit msg in my GIT tree). If it turns out that some klog would make a huge difference I'm willing to consider that later but lets go with this for now.

I'll also send an updated tpm_buf patch set with this one dropped soon.

/Jarkko

#8 lore raw

Re: [PATCH v3 3/5] tpm: migrate tpm2_probe() to use struct tpm_buf

In reply to Mimi Zohar
Quoted 6 lines · Mimi Zohar

Hi Jarrko,

Quoted 3 lines · Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_probe() to use it.

Quoted 6 lines
Quoted 1 line

With this patch, the Pi doesn't find the TPM.  I'm seeing the following line in dmesg.

[    1.087414] tpm_tis_spi: probe of spi0.0 failed with error 256

Thank you for reporting this Mimi. Does it have TPM1/TPM2?

/Jarkko

#9 lore raw

Re: [PATCH v3 3/5] tpm: migrate tpm2_probe() to use struct tpm_buf

Quoted 7 lines · Jarkko Sakkinen
Quoted 6 lines · Mimi Zohar

Hi Jarrko,

Quoted 3 lines · Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_probe() to use it.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

With this patch, the Pi doesn't find the TPM.  I'm seeing the following line in dmesg.

[    1.087414] tpm_tis_spi: probe of spi0.0 failed with error 256

Thank you for reporting this Mimi. Does it have TPM1/TPM2?

The pi has a TPM 2.0 attached to the GPIO.  James pointed out the change in return codes.

+ if (be16_to_cpu(out->tag) = TPM2_ST_NO_SESSIONS)

                chip->flags |= TPM_CHIP_FLAG_TPM2;

-
- return 0;
+out:
+ tpm_buf_destroy(&buf);
+ return rc;
}
EXPORT_SYMBOL_GPL(tpm2_probe);

Mimi

maintainer
#3 lore raw

Re: [PATCH v3 1/5] tpm: fix intermittent failure with self tests

In reply to Jarkko Sakkinen
Quoted 28 lines · Jarkko Sakkinen

index 9e80a953d693..1adb976a2e37 100644

drivers/char/tpm/tpm-interface.c +1 -0
@@ -537,14 +537,26 @@ ssize_t tpm_transmit_cmd(struct tpm_chip *chip,
 struct tpm_space *space,
  			 const char *desc)
  {
  	const struct tpm_output_header *header = buf;
+	unsigned int delay_msec = TPM2_DURATION_SHORT;
  	int err;
  	ssize_t len;

drivers/char/tpm/tpm-interface.c +2 -3
-	len = tpm_transmit(chip, space, (u8 *)buf, bufsiz, flags);
-	if (len <  0)
-		return len;
+	for (;;) {
+		len = tpm_transmit(chip, space, (u8 *)buf, bufsiz,

flags);

drivers/char/tpm/tpm-interface.c +9 -0
+		if (len <  0)
+			return len;
+		err = be32_to_cpu(header->return_code);
+		if (err != TPM2_RC_TESTING)
+			break;
+
+		delay_msec *= 2;
+		if (delay_msec > TPM2_DURATION_LONG) {
+			dev_err(&chip->dev, "the self test is still

running\n");

drivers/char/tpm/tpm-interface.c ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space, · line 558

Quoted 4 lines
drivers/char/tpm/tpm-interface.c +4 -0
+			break;
+		}
+		tpm_msleep(delay_msec);
+	}

It turns out this bit is wrong ... I just discovered it testing the RC_RETRY code.  You can't feed the buf back to tpm_transmit because the header has already been changed to give you back the return code.  To make this work, you have to save the header and handle area and restore it before the command is resent.

I think the best solution for this hunk of code is to merge it with the retry code.

James

#10 lore raw

Re: [PATCH v3 3/5] tpm: migrate tpm2_probe() to use struct tpm_buf

In reply to Mimi Zohar
Quoted 31 lines · Mimi Zohar
Quoted 7 lines · Jarkko Sakkinen
Quoted 6 lines · Mimi Zohar

Hi Jarrko,

Quoted 3 lines · Jarkko Sakkinen

In order to make struct tpm_buf the first class object for constructing TPM commands, migrate tpm2_probe() to use it.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

With this patch, the Pi doesn't find the TPM.  I'm seeing the following line in dmesg.

[    1.087414] tpm_tis_spi: probe of spi0.0 failed with error 256

Thank you for reporting this Mimi. Does it have TPM1/TPM2?

The pi has a TPM 2.0 attached to the GPIO.  James pointed out the change in return codes.

drivers/char/tpm/tpm2-cmd.c +4 -2
+       if (be16_to_cpu(out->tag) = TPM2_ST_NO_SESSIONS)
                chip->flags |= TPM_CHIP_FLAG_TPM2;
-
-       return 0;
+out:
+       tpm_buf_destroy(&buf);
+       return rc;
 }
 EXPORT_SYMBOL_GPL(tpm2_probe);
Quoted 1 line

Mimi

Oops, thank you James and Mimi for pointing this out. I'll refine the patch set.

/Jarkko

#4 lore raw

Re: [PATCH v3 1/5] tpm: fix intermittent failure with self tests

In reply to James Bottomley
Quoted 8 lines · James Bottomley

It turns out this bit is wrong ... I just discovered it testing the RC_RETRY code.  You can't feed the buf back to tpm_transmit because the header has already been changed to give you back the return code.  To make this work, you have to save the header and handle area and restore it before the command is resent.

I think the best solution for this hunk of code is to merge it with the retry code.

Yeah, right, so it is embarrasingly so :-) I wonder how I missed ths during the review. Thanks for spotting this one out. Are you saying that I should merge the patches into a single one? Perhaps you could send me the merged patch if so?

If you agree, please send only that patch. I'll send v4 of my patch set after 4.17 pull request.

/Jarkko