Skip to contentSkip to conversation

Unsorted tpm2-sessions improvements

PATCH Superseded Jarkko Sakkinen 2 participants 9 messages Nov 15

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

cover Unsorted tpm2-sessions improvements

Collected patches that still make sense from [1] and tweaked where needed.

[1] https://lore.kernel.org/linux-integrity/[email protected]/#t

Cc Alec Brown <[email protected]>
Cc "Daniel P. Smith" <[email protected]>
Cc David Howells <[email protected]>
Cc James Bottomley <[email protected]>
Cc James Morris <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Jonathan McDowell <[email protected]>
Cc Matthew Garrett <[email protected]>
Cc Mimi Zohar <[email protected]>
Cc Paul Moore <[email protected]>
Cc Peter Huewe <[email protected]>
Cc Ross Philipson <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>

Jarkko Sakkinen (4):
tpm2-sessions: Drop TPM2_NAME_SIZE
KEYS: trusted: Open code tpm2_buf_append_auth()
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
tpm2-sessions: Re-orchestrate tpm2_read_public() use

 drivers/char/tpm/tpm-sysfs.c              |   2 +-
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 121 +++++++---------------
 include/linux/tpm.h                       |  18 ++--
 security/keys/trusted-keys/trusted_tpm2.c |  83 +++++++--------
 5 files changed, 93 insertions(+), 140 deletions(-)

--
2.47.3

author maintainer
#1 lore raw

PATCH 1/4 tpm2-sessions: Drop TPM2_NAME_SIZE

TPM2_NAME_SIZE was left to include/linux/tpm.h when the base definitions were consolidated. Switch struct tpm2_auth to TPM2_MAX_NAME_SIZE and convert TPM2_NAME_SIZE users to TPM2_NULL_NAME_SIZE, removing the redundant TPM2_NAME_SIZE definition.

Fixes 88c5a62c3af7 ("tpm: Move TPM common base definitions to the command header")
---
 drivers/char/tpm/tpm-sysfs.c     | 2 +-
 drivers/char/tpm/tpm2-sessions.c | 2 +-
 include/linux/tpm.h              | 8 +-------
 3 files changed, 3 insertions(+), 9 deletions(-)
drivers/char/tpm/tpm-sysfs.c +1 -1
index d915307ae833..0f726cf65189 100644
@@ -318,7 +318,7 @@ static ssize_t null_name_show(struct device *dev, struct device_attribute *attr,
 			      char *buf)
 {
 	struct tpm_chip *chip = to_tpm_chip(dev);
-	int size = TPM2_NAME_SIZE;
+	int size = TPM2_NULL_NAME_SIZE;
 
 	bin2hex(buf, chip->null_key_name, size);
 	size *= 2;
drivers/char/tpm/tpm2-sessions.c +1 -1
index 9608e545602c..94c11135beb3 100644
@@ -137,7 +137,7 @@ struct tpm2_auth {
 	 * we must compute and remember
 	 */
 	u32 name_h[AUTH_MAX_NAMES];
-	u8 name[AUTH_MAX_NAMES][2 + SHA512_DIGEST_SIZE];
+	u8 name[AUTH_MAX_NAMES][TPM2_MAX_NAME_SIZE];
 };
 
 #ifdef CONFIG_TCG_TPM2_HMAC
include/linux/tpm.h +1 -7
index 0db277af45c3..84c8fe226235 100644
@@ -94,12 +94,6 @@ struct tpm_chip_seqops {
 /* Fixed define for the curve we use which is NIST_P256 */
 #define EC_PT_SZ	32
 
-/*
- * fixed define for the size of a name.  This is actually HASHALG size
- * plus 2, so 32 for SHA256
- */
-#define TPM2_NAME_SIZE	34
-
 /*
  * The maximum size for an object context
  */
@@ -166,7 +160,7 @@ struct tpm_chip {
 	/* saved context for NULL seed */
 	u8 null_key_context[TPM2_MAX_CONTEXT_SIZE];
 	 /* name of NULL seed */
-	u8 null_key_name[TPM2_NAME_SIZE];
+	u8 null_key_name[TPM2_NULL_NAME_SIZE];
 	u8 null_ec_key_x[EC_PT_SZ];
 	u8 null_ec_key_y[EC_PT_SZ];
 	struct tpm2_auth *auth;
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 2/4 KEYS: trusted: Open code tpm2_buf_append_auth()

In reply to Jarkko Sakkinen

tpm2_buf_append_auth() has a single call site and most of its parameters are redundant. Open code it to the call site so that less cross-referencing is required while browsing the source code.

---
 security/keys/trusted-keys/trusted_tpm2.c | 40 ++++-------------------
 1 file changed, 7 insertions(+), 33 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +7 -33
index 30188a0ec676..563997600636 100644
@@ -197,36 +197,6 @@ int tpm2_key_priv(void *context, size_t hdrlen,
 	return 0;
 }
 
-/**
- * tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
- *
- * @buf: an allocated tpm_buf instance
- * @session_handle: session handle
- * @nonce: the session nonce, may be NULL if not used
- * @nonce_len: the session nonce length, may be 0 if not used
- * @attributes: the session attributes
- * @hmac: the session HMAC or password, may be NULL if not used
- * @hmac_len: the session HMAC or password length, maybe 0 if not used
- */
-static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
-				 const u8 *nonce, u16 nonce_len,
-				 u8 attributes,
-				 const u8 *hmac, u16 hmac_len)
-{
-	tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
-	tpm_buf_append_u32(buf, session_handle);
-	tpm_buf_append_u16(buf, nonce_len);
-
-	if (nonce && nonce_len)
-		tpm_buf_append(buf, nonce, nonce_len);
-
-	tpm_buf_append_u8(buf, attributes);
-	tpm_buf_append_u16(buf, hmac_len);
-
-	if (hmac && hmac_len)
-		tpm_buf_append(buf, hmac, hmac_len);
-}
-
 /**
  * tpm2_seal_trusted() - seal the payload of a trusted key
  *
@@ -537,9 +507,13 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		 * could repeat our actions with the exfiltrated
 		 * password.
 		 */
-		tpm2_buf_append_auth(buf, private->policyhandle,
-				     NULL /* nonce */, 0, 0,
-				     private->blobauth, private->blobauth_len);
+		tpm_buf_append_u32(buf, 9 + private->blobauth_len);
+		tpm_buf_append_u32(buf, private->policyhandle);
+		tpm_buf_append_u16(buf, 0);
+		tpm_buf_append_u8(buf, 0);
+		tpm_buf_append_u16(buf, private->blobauth_len);
+		tpm_buf_append(buf, private->blobauth, private->blobauth_len);
+
 		if (tpm2_chip_auth(chip)) {
 			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
 						    NULL, 0);
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 3/4 KEYS: trusted: Remove dead branch from tpm2_unseal_cmd

In reply to Jarkko Sakkinen

TPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always does set up either password or HMAC session.

Remove the branch in tpm2_unseal_cmd() conditionally setting TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised at run-time, and thus does not cause regressions.

---
 security/keys/trusted-keys/trusted_tpm2.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +1 -9
index 563997600636..22d27e2512db 100644
@@ -467,10 +467,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   u32 blob_handle)
 {
 	struct trusted_key_tpm *private = options->private;
-	struct tpm_header *head;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	u16 data_len;
-	int offset;
 	u8 *data;
 	int rc;
 
@@ -514,15 +512,9 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 		tpm_buf_append_u16(buf, private->blobauth_len);
 		tpm_buf_append(buf, private->blobauth, private->blobauth_len);
 
-		if (tpm2_chip_auth(chip)) {
+		if (tpm2_chip_auth(chip))
 			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
 						    NULL, 0);
-		} else  {
-			offset = buf->handles * 4 + TPM_HEADER_SIZE;
-			head = (struct tpm_header *)buf->data;
-			if (tpm_buf_length(buf) == offset)
-				head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
-		}
 	}
 
 	rc = tpm_buf_fill_hmac_session(chip, buf);
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

PATCH 4/4 tpm2-sessions: Re-orchestrate tpm2_read_public() use

In reply to Jarkko Sakkinen

Move TPM handle name resolution out of tpm_buf_append_name(), so that callers resolve object names explicitly before constructing HMAC-protected commands:

1. Export tpm2_read_public(), so that trusted_tpm2 can resolve handle names, and store non-object handles in big-endian byte order matching the TPM 2.0 handle Name definition.
2. Call tpm2_read_public() explicitly in tpm2_seal_trusted(), tpm2_load_cmd(), and tpm2_unseal_cmd() before starting the auth session, and pass the pre-resolved name and size to tpm_buf_append_name().
3. Rework tpm_buf_append_name() and tpm_buf_fill_hmac_session() to use the caller-supplied name and size directly.

This reduces fallible exits from tpm2_buf_append_name().

---
 drivers/char/tpm/tpm2-cmd.c               |   9 +-
 drivers/char/tpm/tpm2-sessions.c          | 119 +++++++---------------
 include/linux/tpm.h                       |  10 +-
 security/keys/trusted-keys/trusted_tpm2.c |  33 +++++-
 4 files changed, 82 insertions(+), 89 deletions(-)
drivers/char/tpm/tpm2-cmd.c +5 -4
index 4b2bb2ddc321..9c7e8b9aff4d 100644
@@ -194,11 +194,12 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
 
 	if (!disable_pcr_integrity) {
-		rc = tpm_buf_append_name(chip, buf, pcr_idx, NULL);
-		if (rc) {
-			tpm2_end_auth_session(chip);
+		__be32 pcr_idx_be = cpu_to_be32(pcr_idx);
+
+		rc = tpm_buf_append_name(chip, buf, pcr_idx,
+					 (u8 *)&pcr_idx_be, sizeof(pcr_idx_be));
+		if (rc)
 			return rc;
-		}
 		tpm_buf_append_hmac_session(chip, buf, 0, NULL, 0);
 	} else {
 		tpm_buf_append_handle(buf, pcr_idx);
drivers/char/tpm/tpm2-sessions.c +38 -81

189 lines not on this page · raw message

include/linux/tpm.h +9 -1
index 84c8fe226235..a9fd53363dc4 100644
@@ -300,7 +300,7 @@ static inline struct tpm2_auth *tpm2_chip_auth(struct tpm_chip *chip)
 }
 
 int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
-			u32 handle, u8 *name);
+			u32 handle, u8 *name, u16 name_size);
 void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf,
 				 u8 attributes, u8 *passphrase,
 				 int passphraselen);
@@ -314,6 +314,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf);
 int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 				int rc);
 void tpm2_end_auth_session(struct tpm_chip *chip);
+int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name);
 #else
 #include <linux/unaligned.h>
 
@@ -337,6 +338,13 @@ static inline int tpm_buf_check_hmac_response(struct tpm_chip *chip,
 {
 	return rc;
 }
+
+static inline int tpm2_read_public(struct tpm_chip *chip, u32 handle,
+				   void *name)
+{
+	put_unaligned_be32(handle, name);
+	return sizeof(u32);
+}
 #endif	/* CONFIG_TCG_TPM2_HMAC */
 
 #endif
security/keys/trusted-keys/trusted_tpm2.c +30 -3
index 22d27e2512db..f7a0a312a652 100644
@@ -211,9 +211,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 		      struct trusted_key_options *options)
 {
 	struct trusted_key_tpm *private = options->private;
+	u8 parent_name[TPM2_MAX_NAME_SIZE];
 	off_t offset = TPM_HEADER_SIZE;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	struct tpm_buf *sized __free(kfree) = NULL;
+	u16 parent_name_size;
 	int blob_len = 0;
 	int hash;
 	u32 flags;
@@ -230,6 +232,12 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (rc)
 		return rc;
 
+	rc = tpm2_read_public(chip, private->keyhandle, parent_name);
+	if (rc < 0)
+		goto out_put;
+
+	parent_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		goto out_put;
@@ -253,7 +261,8 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 
 	tpm_buf_init_sized(sized, TPM_BUFSIZE);
 
-	rc = tpm_buf_append_name(chip, buf, private->keyhandle, NULL);
+	rc = tpm_buf_append_name(chip, buf, private->keyhandle, parent_name,
+				 parent_name_size);
 	if (rc)
 		goto out;
 
@@ -358,7 +367,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 {
 	u8 *blob_ref __free(kfree) = NULL;
 	struct trusted_key_tpm *private = options->private;
+	u8 parent_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 parent_name_size;
 	unsigned int private_len;
 	unsigned int public_len;
 	unsigned int blob_len;
@@ -409,6 +420,12 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (blob_len > payload->blob_len)
 		return -E2BIG;
 
+	rc = tpm2_read_public(chip, private->keyhandle, parent_name);
+	if (rc < 0)
+		return rc;
+
+	parent_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		return rc;
@@ -422,7 +439,8 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	tpm_buf_init(buf, TPM_BUFSIZE);
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
 
-	rc = tpm_buf_append_name(chip, buf, private->keyhandle, NULL);
+	rc = tpm_buf_append_name(chip, buf, private->keyhandle, parent_name,
+				 parent_name_size);
 	if (rc)
 		return rc;
 
@@ -467,11 +485,19 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 			   u32 blob_handle)
 {
 	struct trusted_key_tpm *private = options->private;
+	u8 blob_name[TPM2_MAX_NAME_SIZE];
 	struct tpm_buf *buf __free(kfree) = NULL;
+	u16 blob_name_size;
 	u16 data_len;
 	u8 *data;
 	int rc;
 
+	rc = tpm2_read_public(chip, blob_handle, blob_name);
+	if (rc < 0)
+		return rc;
+
+	blob_name_size = rc;
+
 	rc = tpm2_start_auth_session(chip);
 	if (rc)
 		return rc;
@@ -485,7 +511,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	tpm_buf_init(buf, TPM_BUFSIZE);
 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
 
-	rc = tpm_buf_append_name(chip, buf, blob_handle, NULL);
+	rc = tpm_buf_append_name(chip, buf, blob_handle, blob_name,
+				 blob_name_size);
 	if (rc)
 		return rc;
 
Signature · 1 line
2.47.3
#6 lore raw

Re: [PATCH 3/4] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd

In reply to Jarkko Sakkinen
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

TPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always does set up either password or HMAC session.

Remove the branch in tpm2_unseal_cmd() conditionally setting TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised at run-time, and thus does not cause regressions.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#2 lore raw

Re: [PATCH 1/4] tpm2-sessions: Drop TPM2_NAME_SIZE

In reply to Jarkko Sakkinen
Sashiko has reviewed this patch and found no issues.
Quoted 8 lines

TPM2_NAME_SIZE was left to include/linux/tpm.h when the base definitions were consolidated. Switch struct tpm2_auth to TPM2_MAX_NAME_SIZE and convert TPM2_NAME_SIZE users to TPM2_NULL_NAME_SIZE, removing the redundant TPM2_NAME_SIZE definition.

Fixes 88c5a62c3af7 ("tpm: Move TPM common base definitions to the command header")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH 2/4] KEYS: trusted: Open code tpm2_buf_append_auth()

In reply to Jarkko Sakkinen
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

tpm2_buf_append_auth() has a single call site and most of its parameters are redundant. Open code it to the call site so that less cross-referencing is required while browsing the source code.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

Re: [PATCH 4/4] tpm2-sessions: Re-orchestrate tpm2_read_public() use

In reply to Jarkko Sakkinen
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] tpm: unsequenced modification of offset…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] tpm: unsequenced modification of offset in tpm2_read_public()
[1]
--

commit ebd1e7509520c997eb3480a23e996d7c9abd6acd
Author: Jarkko Sakkinen <[email protected]>

tpm2-sessions: Re-orchestrate tpm2_read_public() use

This commit exports tpm2_read_public() and moves TPM handle name resolution out of tpm_buf_append_name() so that callers resolve handle names explicitly before starting an authorization session.

Quoted 4 lines
drivers/char/tpm/tpm2-sessions.c +0 -0
index 94c11135beb3f..36308b384a5e3 100644

[ ... ]

Quoted 29 lines
drivers/char/tpm/tpm2-sessions.c +12 -2
@@ -163,7 +163,17 @@ static int name_size(const u8 *name)
 	}
 }
 
-static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
+/**
+ * tpm2_read_public() - Resolve TPM name for a handle
+ * @chip:	TPM chip to use.
+ * @handle:	TPM handle.
+ * @name:	Buffer for returning the name blob. Must have a capacity of at
+ *		least %TPM2_MAX_NAME_SIZE bytes.
+ *
+ * Return: Size of the TPM handle name on success, or a negative errno when
+ * TPM2_ReadPublic fails.
+ */
+int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 {
 	u32 mso = tpm2_handle_mso(handle);
 	off_t offset = TPM_HEADER_SIZE;
@@ -172,7 +182,7 @@ static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 
 	if (mso != TPM2_MSO_PERSISTENT && mso != TPM2_MSO_VOLATILE &&
 	    mso != TPM2_MSO_NVRAM) {
-		memcpy(name, &handle, sizeof(u32));
+		put_unaligned_be32(handle, name);
 		return sizeof(u32);
 	}
 

drivers/char/tpm/tpm2-sessions.c static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name) · line 225

Quoted 4 lines
drivers/char/tpm/tpm2-sessions.c +1 -0
@@ -212,39 +222,34 @@ static int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 	memcpy(name, &buf->data[offset], rc);
 	return name_size_alg;
 }
+EXPORT_SYMBOL_GPL(tpm2_read_public);

[Severity: High]
This problem wasn't introduced by this patch, but does the modification of offset in tpm2_read_public() [1] have unsequenced side effects?

drivers/char/tpm/tpm2-sessions.c:tpm2_read_public() {
	...
	/* Skip TPMT_PUBLIC: */
	offset += tpm_buf_read_u16(buf, &offset);
	...
}

tpm_buf_read_u16() increments offset by 2 through &offset, while the += operator also reads and writes offset without a sequence point between evaluating the left operand and calling tpm_buf_read_u16(). If the compiler evaluates the left operand before the call, += adds the returned length to the old offset and discards the +2 increment, misaligning the subsequent TPM2B_NAME and TPMT_HA reads.

[1] https://sashiko.dev/#/bug/linux-332997a7-b4f3-4fb9-b9c8-65529918139d

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4