Skip to contentSkip to conversation

KEYS: trusted: asymmetric keys

PATCH Superseded Jarkko Sakkinen 2 participants 16 messages Nov 24

Subsystems (10)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

cover KEYS: trusted: asymmetric keys

OVERVIEW
========

Extend TPM 2.0 trusted keys to load ASN.1 TPMKey [1] asymmetric keys (OID_TPMLoadableKey, 2.23.133.10.1.3) and expose keyctl pkey-operations directly on key_type_trusted. In addition there are some TPMKey interoperability fixes.

I.e. this my old series with a twist: instead of introducing a separate subtype for asymmetric keys, factor out pkey-operations into a separate CONFIG_KEYCTL_PKEY-feature, and wire asymmetric operations to key_type_trusted.

This work was originally derived from [3] and is direct offspring of [2].

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
[2] https://lore.kernel.org/linux-integrity/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

Cc Alasdair Kergon <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Benjamin Marzinski <[email protected]>
Cc David Howells <[email protected]>
Cc "David S. Miller" <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Ignat Korchagin <[email protected]>
Cc James Bottomley <[email protected]>
Cc James Morris <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Mikulas Patocka <[email protected]>
Cc Mimi Zohar <[email protected]>
Cc Paul Moore <[email protected]>
Cc Peter Huewe <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Shuah Khan <[email protected]>

Jarkko Sakkinen (9):
KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE lib/asn1_encoder: Add asn1_encode_integer_bytes()
tpm: Export tpm2_create_primary()
tpm: Add persistent hierarchies to tpm2_create_primary()
KEYS: trusted: Extend TPMKey support
KEYS: Factor out KEYCTL_PKEY
KEYS: trusted: Reject zero-length keys in symmetric consumers
KEYS: trusted: Factor out TPM2 blob loading
KEYS: trusted: Add asymmetric key support for TPM2

 .../security/keys/trusted-encrypted.rst       |   25 +-
 crypto/af_alg.c                               |    2 +
 crypto/asymmetric_keys/Kconfig                |    1 +
 drivers/char/tpm/Kconfig                      |    2 +-
 drivers/char/tpm/tpm2-sessions.c              |  203 +--
 drivers/md/dm-crypt.c                         |    2 +-
 include/keys/trusted-type.h                   |    5 +-
 include/keys/trusted_tpm.h                    |   16 +-
 include/linux/asn1_encoder.h                  |    3 +
 include/linux/tpm.h                           |    2 +
 include/linux/tpm_command.h                   |   13 +
 lib/asn1_encoder.c                            |   62 +
 security/keys/Kconfig                         |    3 +
 security/keys/Makefile                        |    2 +-
 .../keys/encrypted-keys/masterkey_trusted.c   |    5 +
 security/keys/internal.h                      |    2 +-
 security/keys/keyctl.c                        |    2 +-
 security/keys/trusted-keys/Kconfig            |   10 +
 security/keys/trusted-keys/tpm2key.asn1       |   19 +
 security/keys/trusted-keys/trusted_core.c     |   20 +-
 security/keys/trusted-keys/trusted_tpm1.c     |   26 +
 security/keys/trusted-keys/trusted_tpm2.c     | 1332 ++++++++++++++++-
 22 files changed, 1558 insertions(+), 199 deletions(-)

--
2.47.3

author maintainer
#1 lore raw

PATCH 1/9 KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE

tpm2_unseal_cmd() rejects unsealed payloads shorter than MIN_KEY_SIZE (32 bytes) with -EFAULT even when loading an ASN.1 TPMKey (OID_TPMSealedData, 2.23.133.10.1.5) blob.

While kernel-generated trusted keys ("new <keylen>") and legacy old_format blobs are at least 32 bytes long, external TPM2 tools such as sealing any non-empty payload up to 128 bytes

Keep MIN_KEY_SIZE for legacy old_format blobs and accept 1..MAX_KEY_SIZE bytes for ASN.1 TPMKey sealed blobs.

---
 security/keys/trusted-keys/trusted_tpm2.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
security/keys/trusted-keys/trusted_tpm2.c +3 -1
index 30188a0ec676..d7336f25b514 100644
@@ -559,9 +559,11 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
 	rc = tpm_buf_check_hmac_response(chip, buf, rc);
 
 	if (!rc) {
+		u16 min_len = payload->old_format ? MIN_KEY_SIZE : 1;
+
 		data_len = be16_to_cpup(
 			(__be16 *)&buf->data[TPM_HEADER_SIZE + 4]);
-		if (data_len < MIN_KEY_SIZE ||  data_len > MAX_KEY_SIZE)
+		if (data_len < min_len || data_len > MAX_KEY_SIZE)
 			return -EFAULT;
 
 		if (tpm_buf_length(buf) < TPM_HEADER_SIZE + 6 + data_len)
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/9 lib/asn1_encoder: Add asn1_encode_integer_bytes()

In reply to Jarkko Sakkinen

Add a helper encoding a positive integer from a byte array in big-endian format.

---
 include/linux/asn1_encoder.h |  3 ++
 lib/asn1_encoder.c           | 62 ++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
include/linux/asn1_encoder.h +3 -0
index d17484dffb74..e206bd425854 100644
@@ -12,6 +12,9 @@ unsigned char *
 asn1_encode_integer(unsigned char *data, const unsigned char *end_data,
 		    s64 integer);
 unsigned char *
+asn1_encode_integer_bytes(unsigned char *data, const unsigned char *end_data,
+			  const unsigned char *integer, u32 integer_len);
+unsigned char *
 asn1_encode_oid(unsigned char *data, const unsigned char *end_data,
 		u32 oid[], int oid_len);
 unsigned char *
lib/asn1_encoder.c +62 -0

74 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH 3/9 tpm: Export tpm2_create_primary()

In reply to Jarkko Sakkinen

Move tpm2_create_primary() outside CONFIG_TCG_TPM2_HMAC and export it for callers such as trusted keys.

---
 drivers/char/tpm/Kconfig         |   2 +-
 drivers/char/tpm/tpm2-sessions.c | 103 ++++++++++++++++---------------
 include/linux/tpm.h              |   2 +
 3 files changed, 56 insertions(+), 51 deletions(-)
drivers/char/tpm/Kconfig +1 -1
index 35aff3003a32..7481f2c8c768 100644
@@ -9,6 +9,7 @@ menuconfig TCG_TPM
 	imply SECURITYFS
 	select CRYPTO
 	select CRYPTO_HASH_INFO
+	select CRYPTO_LIB_SHA256
 	help
 	  If you have a TPM security chip in your system, which
 	  implements the Trusted Computing Group's specification,
@@ -32,7 +33,6 @@ config TCG_TPM2_HMAC
 	default n
 	select CRYPTO_ECDH
 	select CRYPTO_LIB_AESCFB
-	select CRYPTO_LIB_SHA256
 	select CRYPTO_LIB_UTILS
 	help
 	  Setting this causes us to deploy a scheme which uses request
drivers/char/tpm/tpm2-sessions.c +53 -50

147 lines not on this page · raw message

include/linux/tpm.h +2 -0
index 0f97abc82846..381af99dad31 100644
@@ -255,6 +255,8 @@ extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
 extern struct tpm_chip *tpm_default_chip(void);
 void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
 int tpm2_find_hash_alg(unsigned int crypto_id);
+int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy, u32 *handle,
+			u8 *name);
 
 static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
 {
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 4/9 tpm: Add persistent hierarchies to tpm2_create_primary()

In reply to Jarkko Sakkinen

TPMKey files can specify a 0x40 MSO permanent handle as parent, requiring TPM2_CreatePrimary with the TCG H-2 NIST P-256 template [1].

Populate chip->null_ec_key_{x,y} in tpm2_parse_create_primary() only for TPM2_RH_NULL.

Verify the returned key name against the locally computed TPMT_PUBLIC digest, and flush the transient handle on validation failure.

[1] https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html#section-3.1.8

---
 drivers/char/tpm/tpm2-sessions.c | 104 +++++++++++++++++--------------
 1 file changed, 56 insertions(+), 48 deletions(-)
drivers/char/tpm/tpm2-sessions.c +56 -48
index b3b4f7a89d9d..98f668874c3e 100644
@@ -1118,10 +1118,11 @@ int tpm2_sessions_init(struct tpm_chip *chip)
 	return rc;
 }
 #endif /* CONFIG_TCG_TPM2_HMAC */
+
 /*
- * A mask containing the object attributes for the kernel held null primary key
- * used in HMAC encryption. For more information on specific attributes look up
- * to "8.3 TPMA_OBJECT (Object Attributes)".
+ * A mask containing the object attributes for the TCG mandated H-2 ECC P-256
+ * primary storage key template. For more information on specific attributes
+ * look up "8.3 TPMA_OBJECT (Object Attributes)".
  */
 #define TPM2_OA_NULL_KEY ( \
 	TPM2_OA_NO_DA | \
@@ -1151,15 +1152,14 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 {
 	struct tpm_header *head = (struct tpm_header *)buf->data;
 	off_t offset_r = TPM_HEADER_SIZE, offset_t;
-	u16 len = TPM_HEADER_SIZE;
+	u8 resp_name[SHA256_DIGEST_SIZE + 2];
 	u32 total_len = be32_to_cpu(head->length);
 	u32 val, param_len, keyhandle;
+	u16 len = TPM_HEADER_SIZE;
 
 	keyhandle = tpm_buf_read_u32(buf, &offset_r);
-	if (handle)
-		*handle = keyhandle;
-	else
-		tpm2_flush_context(chip, keyhandle);
+	if (buf->flags & TPM_BUF_INVALID)
+		return -EINVAL;
 
 	param_len = tpm_buf_read_u32(buf, &offset_r);
 	/*
@@ -1170,85 +1170,87 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	param_len += TPM_HEADER_SIZE;
 
 	if (param_len + 8 > total_len)
-		return -EINVAL;
+		goto err;
 	len = tpm_buf_read_u16(buf, &offset_r);
 	offset_t = offset_r;
 	if (offset_r + len > tpm_buf_length(buf))
-		return -EINVAL;
-	if (name) {
-		/*
-		 * now we have the public area, compute the name of
-		 * the object
-		 */
-		put_unaligned_be16(TPM_ALG_SHA256, name);
-		sha256(&buf->data[offset_r], len, name + 2);
-	}
+		goto err;
+
+	/*
+	 * Now we have the public area, compute the name of the object.
+	 */
+	put_unaligned_be16(TPM_ALG_SHA256, resp_name);
+	sha256(&buf->data[offset_r], len, resp_name + 2);
+	if (name)
+		memcpy(name, resp_name, sizeof(resp_name));
 
 	/* validate the public key */
 	val = tpm_buf_read_u16(buf, &offset_t);
 
 	/* key type (must be what we asked for) */
 	if (val != TPM_ALG_ECC)
-		return -EINVAL;
+		goto err;
 	val = tpm_buf_read_u16(buf, &offset_t);
 
 	/* name algorithm */
 	if (val != TPM_ALG_SHA256)
-		return -EINVAL;
+		goto err;
 	val = tpm_buf_read_u32(buf, &offset_t);
 
 	/* object properties */
 	if (val != TPM2_OA_NULL_KEY)
-		return -EINVAL;
+		goto err;
 
 	/* auth policy (empty) */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != 0)
-		return -EINVAL;
+		goto err;
 
 	/* symmetric key parameters */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != TPM_ALG_AES)
-		return -EINVAL;
+		goto err;
 
 	/* symmetric key length */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != AES_KEY_BITS)
-		return -EINVAL;
+		goto err;
 
 	/* symmetric encryption scheme */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != TPM_ALG_CFB)
-		return -EINVAL;
+		goto err;
 
 	/* signing scheme */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != TPM_ALG_NULL)
-		return -EINVAL;
+		goto err;
 
 	/* ECC Curve */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != TPM2_ECC_NIST_P256)
-		return -EINVAL;
+		goto err;
 
 	/* KDF Scheme */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != TPM_ALG_NULL)
-		return -EINVAL;
+		goto err;
 
 	/* extract public key (x and y points) */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != EC_PT_SZ)
-		return -EINVAL;
+		goto err;
 #ifdef CONFIG_TCG_TPM2_HMAC
-	memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
+	if (hierarchy == TPM2_RH_NULL)
+		memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
 #endif
 	offset_t += val;
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != EC_PT_SZ)
-		return -EINVAL;
+		goto err;
 #ifdef CONFIG_TCG_TPM2_HMAC
-	memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
+	if (hierarchy == TPM2_RH_NULL)
+		memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
 #endif
 	offset_t += val;
 
@@ -1257,38 +1259,38 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 
 	/* should have exactly consumed the TPM2B public structure */
 	if (offset_t != offset_r)
-		return -EINVAL;
+		goto err;
 	if (offset_r > param_len)
-		return -EINVAL;
+		goto err;
 
 	/* creation data (skip) */
 	len = tpm_buf_read_u16(buf, &offset_r);
 	offset_r += len;
 	if (offset_r > param_len)
-		return -EINVAL;
+		goto err;
 
 	/* creation digest (must be sha256) */
 	len = tpm_buf_read_u16(buf, &offset_r);
 	offset_r += len;
 	if (len != SHA256_DIGEST_SIZE || offset_r > param_len)
-		return -EINVAL;
+		goto err;
 
 	/* TPMT_TK_CREATION follows */
 	/* tag, must be TPM_ST_CREATION (0x8021) */
 	val = tpm_buf_read_u16(buf, &offset_r);
 	if (val != TPM2_ST_CREATION || offset_r > param_len)
-		return -EINVAL;
+		goto err;
 
 	/* hierarchy */
 	val = tpm_buf_read_u32(buf, &offset_r);
 	if (val != hierarchy || offset_r > param_len)
-		return -EINVAL;
+		goto err;
 
 	/* the ticket digest HMAC (might not be sha256) */
 	len = tpm_buf_read_u16(buf, &offset_r);
 	offset_r += len;
 	if (offset_r > param_len)
-		return -EINVAL;
+		goto err;
 
 	/*
 	 * finally we have the name, which is a sha256 digest plus a 2
@@ -1296,19 +1298,25 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	 */
 	len = tpm_buf_read_u16(buf, &offset_r);
 	if (offset_r + len != param_len + 8)
-		return -EINVAL;
+		goto err;
 	if (len != SHA256_DIGEST_SIZE + 2)
-		return -EINVAL;
+		goto err;
 
-#ifdef CONFIG_TCG_TPM2_HMAC
-	if (memcmp(chip->null_key_name, &buf->data[offset_r],
-		   SHA256_DIGEST_SIZE + 2) != 0) {
-		dev_err(&chip->dev, "NULL Seed name comparison failed\n");
-		return -EINVAL;
+	if (memcmp(resp_name, &buf->data[offset_r], sizeof(resp_name)) != 0) {
+		dev_err(&chip->dev, "primary key name comparison failed\n");
+		goto err;
 	}
-#endif
+
+	if (handle)
+		*handle = keyhandle;
+	else
+		tpm2_flush_context(chip, keyhandle);
 
 	return 0;
+
+err:
+	tpm2_flush_context(chip, keyhandle);
+	return -EINVAL;
 }
 
 /**
@@ -1420,7 +1428,7 @@ int tpm2_create_primary(struct tpm_chip *chip, u32 hierarchy, u32 *handle,
 	tpm_buf_append_u32(buf, 0);
 
 	rc = tpm_transmit_cmd(chip, buf, 0,
-			      "attempting to create NULL primary");
+			      "attempting to create primary key");
 
 	if (rc == TPM2_RC_SUCCESS)
 		rc = tpm2_parse_create_primary(chip, buf, handle, hierarchy,
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

PATCH 5/9 KEYS: trusted: Extend TPMKey support

In reply to Jarkko Sakkinen

Extend tpm2key.asn1 with the optional [1] policy, [2] secret, [3] authPolicy, [4] description, and [5] rsaParent fields.

Create a temporary TCG H-2 NIST P-256 primary key in tpm2_seal_trusted() and tpm2_load_cmd() when the parent handle is TPM2_RH_OWNER, TPM2_RH_NULL, or TPM2_RH_PLATFORM.

---
 .../security/keys/trusted-encrypted.rst       |  18 ++-
 include/keys/trusted_tpm.h                    |   1 -
 include/linux/tpm_command.h                   |   2 +
 security/keys/trusted-keys/tpm2key.asn1       |  19 +++
 security/keys/trusted-keys/trusted_tpm2.c     | 142 ++++++++++++++++--
 5 files changed, 161 insertions(+), 21 deletions(-)
Documentation/security/keys/trusted-encrypted.rst +12 -6
index ddff7c7c2582..c6a507ef228a 100644
@@ -561,6 +561,11 @@ policy::
     TPMKey ::= SEQUENCE {
         type		OBJECT IDENTIFIER
         emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL
+        policy		[1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL
+        secret		[2] EXPLICIT OCTET STRING OPTIONAL
+        authPolicy	[3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL
+        description	[4] EXPLICIT UTF8String OPTIONAL
+        rsaParent	[5] EXPLICIT BOOLEAN OPTIONAL
         parent		INTEGER
         pubkey		OCTET STRING
         privkey		OCTET STRING
@@ -592,12 +597,13 @@ phrase.  This is used by most user space consumers to decide whether
 to prompt for a password.
 
 parent represents the parent key handle, either in the 0x81 MSO space,
-like 0x81000001 for the RSA primary storage key.  Userspace programmes
-also support specifying the primary handle in the 0x40 MSO space.  If
-this happens the Elliptic Curve variant of the primary key using the
-TCG defined template will be generated on the fly into a volatile
-object and used as the parent.  The current kernel code only supports
-the 0x81 MSO form.
+like 0x81000001 for a persistent primary storage key, or a hierarchy
+handle in the 0x40 MSO permanent handle space (TPM_RH_OWNER 0x40000001,
+TPM_RH_NULL 0x40000007, or TPM_RH_PLATFORM 0x4000000c).  When a 0x40
+MSO hierarchy handle is used without rsaParent, the Elliptic Curve
+variant of the primary key using the TCG defined H-2 NIST P-256 template
+is generated on the fly into a volatile object, used as the parent, and
+flushed afterwards.
 
 pubkey is the binary representation of TPM2B_PRIVATE excluding the
 initial TPM2B header, which can be reconstructed from the ASN.1 octet
include/keys/trusted_tpm.h +0 -1
index 9a83269821e5..b8b1f5c8fbc3 100644
@@ -26,5 +26,4 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 int tpm2_unseal_trusted(struct tpm_chip *chip,
 			struct trusted_key_payload *payload,
 			struct trusted_key_options *options);
-
 #endif
include/linux/tpm_command.h +2 -0
index 9ea1ee3cbef1..6576a7df6d8c 100644
@@ -259,8 +259,10 @@ enum tpm2_cc_attrs {
 };
 
 enum tpm2_permanent_handles {
+	TPM2_RH_OWNER		= 0x40000001,
 	TPM2_RH_NULL		= 0x40000007,
 	TPM2_RS_PW		= 0x40000009,
+	TPM2_RH_PLATFORM	= 0x4000000C,
 };
 
 /* Most Significant Octet for key types  */
security/keys/trusted-keys/tpm2key.asn1 +19 -0
index f57f869ad600..d15dfcf593a5 100644
@@ -5,7 +5,26 @@
 TPMKey ::= SEQUENCE {
 	type		OBJECT IDENTIFIER ({tpm2_key_type}),
 	emptyAuth	[0] EXPLICIT BOOLEAN OPTIONAL,
+	policy		[1] EXPLICIT TPMPolicySequence OPTIONAL,
+	secret		[2] EXPLICIT OCTET STRING OPTIONAL ({tpm2_key_secret}),
+	authPolicy	[3] EXPLICIT TPMAuthPolicySequence OPTIONAL,
+	description	[4] EXPLICIT UTF8String OPTIONAL,
+	rsaParent	[5] EXPLICIT BOOLEAN OPTIONAL ({tpm2_key_rsa_parent}),
 	parent		INTEGER ({tpm2_key_parent}),
 	pubkey		OCTET STRING ({tpm2_key_pub}),
 	privkey		OCTET STRING ({tpm2_key_priv})
 	}
+
+TPMPolicySequence ::= SEQUENCE OF TPMPolicy ({tpm2_key_policy})
+
+TPMPolicy ::= SEQUENCE {
+	commandCode	[0] EXPLICIT INTEGER,
+	commandPolicy	[1] EXPLICIT OCTET STRING
+	}
+
+TPMAuthPolicySequence ::= SEQUENCE OF TPMAuthPolicy ({tpm2_key_auth_policy})
+
+TPMAuthPolicy ::= SEQUENCE {
+	name		[0] EXPLICIT UTF8String OPTIONAL,
+	policy		[1] EXPLICIT TPMPolicySequence
+	}
security/keys/trusted-keys/trusted_tpm2.c +128 -14

273 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#8 lore raw

PATCH 6/9 KEYS: Factor out KEYCTL_PKEY

In reply to Jarkko Sakkinen

Factor out CONFIG_KEYCTL_PKEY from CONFIG_ASYMMETRIC_KEY_TYPE so key types other than asymmetric can select keyctl public-key operations without pulling in crypto/asymmetric_keys/.

---
 crypto/asymmetric_keys/Kconfig | 1 +
 security/keys/Kconfig          | 3 +++
 security/keys/Makefile         | 2 +-
 security/keys/internal.h       | 2 +-
 security/keys/keyctl.c         | 2 +-
 5 files changed, 7 insertions(+), 3 deletions(-)
crypto/asymmetric_keys/Kconfig +1 -0
index c73979b2e98e..1c78a9a663c5 100644
@@ -2,6 +2,7 @@
 menuconfig ASYMMETRIC_KEY_TYPE
 	bool "Asymmetric (public-key cryptographic) key type"
 	depends on KEYS
+	select KEYCTL_PKEY
 	help
 	  This option provides support for a key type that holds the data for
 	  the asymmetric keys used for public key cryptographic operations such
security/keys/Kconfig +3 -0
index 3f64d60d0f8a..c0cd894d306c 100644
@@ -22,6 +22,9 @@ menuconfig KEYS
 
 if KEYS
 
+config KEYCTL_PKEY
+	bool
+
 config KEYS_REQUEST_CACHE
 	bool "Enable temporary caching of the last request_key() result"
 	help
security/keys/Makefile +1 -1
index 5f40807f05b3..aafd28f7ee2f 100644
@@ -22,7 +22,7 @@ obj-$(CONFIG_PROC_FS) += proc.o
 obj-$(CONFIG_SYSCTL) += sysctl.o
 obj-$(CONFIG_PERSISTENT_KEYRINGS) += persistent.o
 obj-$(CONFIG_KEY_DH_OPERATIONS) += dh.o
-obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += keyctl_pkey.o
+obj-$(CONFIG_KEYCTL_PKEY) += keyctl_pkey.o
 
 #
 # Key types
security/keys/internal.h +1 -1
index c452d71cce03..28c6cd942c59 100644
@@ -314,7 +314,7 @@ static inline long compat_keyctl_dh_compute(
 #endif
 #endif
 
-#ifdef CONFIG_ASYMMETRIC_KEY_TYPE
+#ifdef CONFIG_KEYCTL_PKEY
 extern long keyctl_pkey_query(key_serial_t,
 			      const char __user *,
 			      struct keyctl_pkey_query __user *);
security/keys/keyctl.c +1 -1
index d6b10fd67627..417bba539bb1 100644
@@ -28,7 +28,7 @@ static const unsigned char keyrings_capabilities[2] = {
 	[0] = (KEYCTL_CAPS0_CAPABILITIES |
 	       (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS)	? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
 	       (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS)	? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
-	       (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE)	? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
+	       (IS_ENABLED(CONFIG_KEYCTL_PKEY)		? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
 	       (IS_ENABLED(CONFIG_BIG_KEYS)		? KEYCTL_CAPS0_BIG_KEY : 0) |
 	       KEYCTL_CAPS0_INVALIDATE |
 	       KEYCTL_CAPS0_RESTRICT_KEYRING |
Signature · 1 line
2.47.3
author maintainer
#10 lore raw

PATCH 7/9 KEYS: trusted: Reject zero-length keys in symmetric consumers

In reply to Jarkko Sakkinen

Asymmetric trusted keys will not carry a raw symmetric key payload (key_len == 0). Reject zero-length trusted keys in encrypted-keys, af_alg, dm-crypt, and trusted_update() resealing.

---
 crypto/af_alg.c                                  | 2 ++
 drivers/md/dm-crypt.c                            | 2 +-
 security/keys/encrypted-keys/masterkey_trusted.c | 5 +++++
 security/keys/trusted-keys/trusted_core.c        | 2 +-
 4 files changed, 9 insertions(+), 2 deletions(-)
crypto/af_alg.c +2 -0
index ab84c4488a15..133cb88e3c12 100644
@@ -323,6 +323,8 @@ static const u8 *key_data_ptr_trusted(const struct key *key,
 	if (IS_ERR_OR_NULL(tkp))
 		return ERR_PTR(-EKEYREVOKED);
 
+	if (!tkp->key_len)
+		return ERR_PTR(-EINVAL);
 	*datalen = tkp->key_len;
 
 	return tkp->key;
drivers/md/dm-crypt.c +1 -1
index 02038a9cd21d..1e034c747d39 100644
@@ -2492,7 +2492,7 @@ static int set_key_trusted(struct crypt_config *cc, struct key *key)
 	if (!tkp)
 		return -EKEYREVOKED;
 
-	if (cc->key_size != tkp->key_len)
+	if (!tkp->key_len || cc->key_size != tkp->key_len)
 		return -EINVAL;
 
 	memcpy(cc->key, tkp->key, cc->key_size);
security/keys/encrypted-keys/masterkey_trusted.c +5 -0
index e6d22ce77e98..8c6e6bdd8ef7 100644
@@ -36,6 +36,11 @@ struct key *request_trusted_key(const char *trusted_desc,
 
 	down_read(&tkey->sem);
 	tpayload = tkey->payload.data[0];
+	if (!tpayload->key_len) {
+		up_read(&tkey->sem);
+		key_put(tkey);
+		return ERR_PTR(-EINVAL);
+	}
 	*master_key = tpayload->key;
 	*master_keylen = tpayload->key_len;
 error:
security/keys/trusted-keys/trusted_core.c +1 -1
index 11d7d296d063..3721980ed83a 100644
@@ -249,7 +249,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	if (key_is_negative(key))
 		return -ENOKEY;
 	p = key->payload.data[0];
-	if (!p->migratable)
+	if (!p->migratable || !p->key_len)
 		return -EPERM;
 	if (datalen == 0 || datalen > 32767 || !prep->data)
 		return -EINVAL;
Signature · 1 line
2.47.3
author maintainer
#11 lore raw

PATCH 8/9 KEYS: trusted: Factor out TPM2 blob loading

In reply to Jarkko Sakkinen

Factor out TPM2 blob loading:

1. tpm2_key_decode() from trusted_key_options.
2. tpm2_load_blob() and tpm2_buf_append_key_auth() from tpm2_load_cmd() and tpm2_unseal_cmd().

---
 include/keys/trusted-type.h               |   1 +
 security/keys/trusted-keys/trusted_core.c |  18 +-
 security/keys/trusted-keys/trusted_tpm2.c | 210 ++++++++++++----------
 3 files changed, 131 insertions(+), 98 deletions(-)
include/keys/trusted-type.h +1 -0
index 3db61b57cf73..790ffabb0d18 100644
@@ -33,6 +33,7 @@ struct trusted_key_payload {
 	unsigned int blob_len;
 	unsigned char migratable;
 	unsigned char old_format;
+	void *private;
 	unsigned char key[MAX_KEY_SIZE + 1];
 	unsigned char blob[MAX_BLOB_SIZE];
 };
security/keys/trusted-keys/trusted_core.c +13 -5
index 3721980ed83a..b8f980da606d 100644
@@ -149,6 +149,14 @@ static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
 	return p;
 }
 
+static void trusted_payload_free(struct trusted_key_payload *p)
+{
+	if (!p)
+		return;
+	kfree_sensitive(p->private);
+	kfree_sensitive(p);
+}
+
 /*
  * trusted_instantiate - create a new trusted key
  *
@@ -223,7 +231,7 @@ static int trusted_instantiate(struct key *key,
 	if (!ret)
 		rcu_assign_keypointer(key, payload);
 	else
-		kfree_sensitive(payload);
+		trusted_payload_free(payload);
 	return ret;
 }
 
@@ -232,7 +240,7 @@ static void trusted_rcu_free(struct rcu_head *rcu)
 	struct trusted_key_payload *p;
 
 	p = container_of(rcu, struct trusted_key_payload, rcu);
-	kfree_sensitive(p);
+	trusted_payload_free(p);
 }
 
 /*
@@ -269,7 +277,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	ret = datablob_parse(&datablob, new_p);
 	if (ret != Opt_update) {
 		ret = -EINVAL;
-		kfree_sensitive(new_p);
+		trusted_payload_free(new_p);
 		goto out;
 	}
 
@@ -283,7 +291,7 @@ static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
 	ret = static_call(trusted_key_seal)(new_p, datablob);
 	if (ret < 0) {
 		pr_info("key_seal failed (%d)\n", ret);
-		kfree_sensitive(new_p);
+		trusted_payload_free(new_p);
 		goto out;
 	}
 
@@ -322,7 +330,7 @@ static long trusted_read(const struct key *key, char *buffer,
  */
 static void trusted_destroy(struct key *key)
 {
-	kfree_sensitive(key->payload.data[0]);
+	trusted_payload_free(key->payload.data[0]);
 }
 
 struct key_type key_type_trusted = {
security/keys/trusted-keys/trusted_tpm2.c +117 -93

300 lines not on this page · raw message

Signature · 1 line
2.47.3
author maintainer
#12 lore raw

PATCH 9/9 KEYS: trusted: Add asymmetric key support for TPM2

In reply to Jarkko Sakkinen

Extend the TPM 2.0 trusted key backend to load OID_TPMLoadableKey (2.23.133.10.1.3) RSA (2048/3072/4096) and ECC (NIST P-256/P-384/P-521) keys and expose query, sign, verify, and RSA encrypt/decrypt operations through key_type_trusted.

The feature is enabled with opt-in CONFIG_TRUSTED_KEYS_ASYMMETRIC.

---
 .../security/keys/trusted-encrypted.rst       |    7 +-
 include/keys/trusted-type.h                   |    4 +-
 include/keys/trusted_tpm.h                    |   15 +
 include/linux/tpm_command.h                   |   11 +
 security/keys/trusted-keys/Kconfig            |   10 +
 security/keys/trusted-keys/trusted_tpm1.c     |   26 +
 security/keys/trusted-keys/trusted_tpm2.c     | 1032 ++++++++++++++++-
 7 files changed, 1100 insertions(+), 5 deletions(-)
Documentation/security/keys/trusted-encrypted.rst +6 -1
index c6a507ef228a..30a8c6d1c0c8 100644
@@ -589,7 +589,12 @@ available are::
                     represents a symmetric key and must be unsealed before
                     use.
 
-The trusted key code only uses the TPM Sealed Data OID.
+The trusted key code uses TPM Sealed Data for symmetric keys.
+
+When ``CONFIG_TRUSTED_KEYS_ASYMMETRIC`` is enabled, TPM 2.0 backend accepts TPM
+Loadable keys through the ``trusted`` key type. Use the keyctl pkey-operations
+for these keys. For RSA OAEP encryption and decryption, specify ``enc=oaep`` and
+a ``hash=`` matching the key's scheme, if fixed.
 
 emptyAuth is true if the key has well known authorization "".  If it
 is false or not present, the key requires an explicit authorization
include/keys/trusted-type.h +3 -1
index 790ffabb0d18..eebc2c33bce7 100644
@@ -19,7 +19,9 @@
 
 #define MIN_KEY_SIZE			32
 #define MAX_KEY_SIZE			128
-#if IS_ENABLED(CONFIG_TRUSTED_KEYS_PKWM)
+#if IS_ENABLED(CONFIG_TRUSTED_KEYS_TPM)
+#define MAX_BLOB_SIZE			2048
+#elif IS_ENABLED(CONFIG_TRUSTED_KEYS_PKWM)
 #define MAX_BLOB_SIZE			1152
 #else
 #define MAX_BLOB_SIZE                   512
include/keys/trusted_tpm.h +15 -0
index b8b1f5c8fbc3..ea968ecc0c3f 100644
@@ -4,6 +4,8 @@
 
 #include <keys/trusted-type.h>
 
+struct kernel_pkey_params;
+struct kernel_pkey_query;
 extern struct trusted_key_ops trusted_key_tpm_ops;
 
 struct trusted_key_tpm {
@@ -18,6 +20,12 @@ struct trusted_key_tpm {
 	u32 policydigest_len;
 	unsigned char policydigest[MAX_DIGEST_SIZE];
 	u32 policyhandle;
+	u32 attrs;
+	u16 alg_type;
+	u16 scheme;
+	u16 scheme_hash;
+	u16 key_bits;
+	u16 coord_size;
 };
 
 int tpm2_seal_trusted(struct tpm_chip *chip,
@@ -26,4 +34,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 int tpm2_unseal_trusted(struct tpm_chip *chip,
 			struct trusted_key_payload *payload,
 			struct trusted_key_options *options);
+int tpm2_asym_query(const struct kernel_pkey_params *params,
+		    struct kernel_pkey_query *info);
+int tpm2_asym_eds_op(struct tpm_chip *chip, struct kernel_pkey_params *params,
+		     const void *in, void *out);
+int tpm2_asym_verify_signature(struct tpm_chip *chip,
+			       struct kernel_pkey_params *params,
+			       const void *in, const void *in2);
 #endif
include/linux/tpm_command.h +11 -0
index 6576a7df6d8c..498b82cf3142 100644
@@ -184,6 +184,7 @@ enum tpm2_structures {
 	TPM2_ST_NO_SESSIONS	= 0x8001,
 	TPM2_ST_SESSIONS	= 0x8002,
 	TPM2_ST_CREATION	= 0x8021,
+	TPM2_ST_HASHCHECK	= 0x8024,
 };
 
 /* Indicates from what layer of the software stack the error comes from */
@@ -218,12 +219,15 @@ enum tpm2_command_codes {
 	TPM2_CC_NV_READ			= 0x014E,
 	TPM2_CC_CREATE			= 0x0153,
 	TPM2_CC_LOAD			= 0x0157,
+	TPM2_CC_RSA_DECRYPT		= 0x0159,
 	TPM2_CC_SEQUENCE_UPDATE		= 0x015C,
+	TPM2_CC_SIGN			= 0x015D,
 	TPM2_CC_UNSEAL			= 0x015E,
 	TPM2_CC_CONTEXT_LOAD		= 0x0161,
 	TPM2_CC_CONTEXT_SAVE		= 0x0162,
 	TPM2_CC_FLUSH_CONTEXT		= 0x0165,
 	TPM2_CC_READ_PUBLIC		= 0x0173,
+	TPM2_CC_RSA_ENCRYPT		= 0x0174,
 	TPM2_CC_START_AUTH_SESS		= 0x0176,
 	TPM2_CC_VERIFY_SIGNATURE	= 0x0177,
 	TPM2_CC_GET_CAPABILITY		= 0x017A,
@@ -278,6 +282,8 @@ enum tpm2_mso_type {
 enum tpm2_curves {
 	TPM2_ECC_NONE		= 0x0000,
 	TPM2_ECC_NIST_P256	= 0x0003,
+	TPM2_ECC_NIST_P384	= 0x0004,
+	TPM2_ECC_NIST_P521	= 0x0005,
 };
 
 enum tpm2_object_attributes {
@@ -457,6 +463,7 @@ struct tpm2_context {
 /* If you add a new hash to this, increment TPM_MAX_HASHES below */
 enum tpm_algorithms {
 	TPM_ALG_ERROR		= 0x0000,
+	TPM_ALG_RSA		= 0x0001,
 	TPM_ALG_SHA1		= 0x0004,
 	TPM_ALG_AES		= 0x0006,
 	TPM_ALG_KEYEDHASH	= 0x0008,
@@ -465,6 +472,10 @@ enum tpm_algorithms {
 	TPM_ALG_SHA512		= 0x000D,
 	TPM_ALG_NULL		= 0x0010,
 	TPM_ALG_SM3_256		= 0x0012,
+	TPM_ALG_RSASSA		= 0x0014,
+	TPM_ALG_RSAES		= 0x0015,
+	TPM_ALG_OAEP		= 0x0017,
+	TPM_ALG_ECDSA		= 0x0018,
 	TPM_ALG_ECC		= 0x0023,
 	TPM_ALG_CFB		= 0x0043,
 };
security/keys/trusted-keys/Kconfig +10 -0
index e5a4a53aeab2..48fea7981ab4 100644
@@ -38,6 +38,16 @@ config TRUSTED_KEYS_TPM
 	  The TPM only unseals the keys, if the boot PCRs and other
 	  criteria match.
 
+config TRUSTED_KEYS_ASYMMETRIC
+	bool "Asymmetric TPM 2.0 trusted keys"
+	depends on TRUSTED_KEYS_TPM
+	select KEYCTL_PKEY
+	help
+	  Allow the trusted key type to load TPM 2.0 RSA and ECC keys and
+	  use them through the keyctl public-key operations.
+	  This adds signing, signature verification, and RSA encryption and
+	  decryption without changing support for sealed symmetric keys.
+
 config TRUSTED_KEYS_TEE
 	bool "TEE-based trusted keys"
 	depends on TEE >= TRUSTED_KEYS
security/keys/trusted-keys/trusted_tpm1.c +26 -0
index 3b7af11d30c4..4e458473b5dc 100644
@@ -943,6 +943,9 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
 		}
 	}
 
+	if (!p->key_len)
+		p->private = no_free_ptr(private);
+
 	return ret;
 }
 
@@ -951,6 +954,20 @@ static int trusted_tpm_get_random(unsigned char *key, size_t key_len)
 	return tpm_get_random(chip, key, key_len);
 }
 
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+static int trusted_tpm_asym_eds_op(struct kernel_pkey_params *params,
+				   const void *in, void *out)
+{
+	return tpm2_asym_eds_op(chip, params, in, out);
+}
+
+static int trusted_tpm_asym_verify_signature(struct kernel_pkey_params *params,
+					     const void *in, const void *in2)
+{
+	return tpm2_asym_verify_signature(chip, params, in, in2);
+}
+#endif
+
 static int __init init_digests(void)
 {
 	int i;
@@ -976,6 +993,15 @@ static int __init trusted_tpm_init(void)
 	ret = init_digests();
 	if (ret < 0)
 		goto err_put;
+
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+	if (tpm_is_tpm2(chip) > 0) {
+		key_type_trusted.asym_query = tpm2_asym_query;
+		key_type_trusted.asym_eds_op = trusted_tpm_asym_eds_op;
+		key_type_trusted.asym_verify_signature =
+			trusted_tpm_asym_verify_signature;
+	}
+#endif
 	ret = register_key_type(&key_type_trusted);
 	if (ret < 0)
 		goto err_free;
security/keys/trusted-keys/trusted_tpm2.c +1029 -3

1,088 lines not on this page · raw message

Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH 2/9] lib/asn1_encoder: Add asn1_encode_integer_bytes()

In reply to Jarkko Sakkinen
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] lib/asn1_encoder: unreachable !bytes_len branch in asn1_encode_integer_bytes() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] lib/asn1_encoder: unreachable !bytes_len branch in asn1_encode_integer_bytes()
--

commit 881f7930374a268f10571bcbb28c0f38ce090851
Author: Jarkko Sakkinen <[email protected]>

lib/asn1_encoder: Add asn1_encode_integer_bytes()

This commit adds asn1_encode_integer_bytes() to encode a positive integer from a big-endian byte array into ASN.1 format while stripping leading zeros.

Quoted 4 lines
lib/asn1_encoder.c +0 -0
index 92f35aae13b13..22e0acd6fe08d 100644

[ ... ]

Quoted 31 lines
lib/asn1_encoder.c +27 -0
@@ -85,6 +87,66 @@ asn1_encode_integer(unsigned char *data, const unsigned char *end_data,
 }
 EXPORT_SYMBOL_GPL(asn1_encode_integer);
 
+/**
+ * asn1_encode_integer_bytes() - encode positive integer bytes to ASN.1
+ * @data:		pointer to the pointer to the data
+ * @end_data:		end of data pointer, points one beyond last usable byte in @data
+ * @bytes:		integer bytes
+ * @bytes_len:		amount of bytes
+ *
+ * Encode a positive integer from a byte array in big-endian format. Strip
+ * leading zeros.
+ */
+unsigned char *
+asn1_encode_integer_bytes(unsigned char *data, const unsigned char *end_data,
+			  const unsigned char *bytes, u32 bytes_len)
+{
+	static const unsigned char zero;
+	int data_len = end_data - data;
+	bool add_pad = false;
+	int ret;
+
+	if (IS_ERR(data))
+		return data;
+
+	if (!bytes || !bytes_len)
+		return ERR_PTR(-EINVAL);
+
+	/* Strip leading zeros: */
+	while (bytes_len > 1 && bytes[0] == 0) {

lib/asn1_encoder.c asn1_encode_integer(unsigned char *data, const unsigned char *end_data, · line 126

Quoted 10 lines
lib/asn1_encoder.c +10 -0
+		bytes++;
+		bytes_len--;
+	}
+
+	if (!bytes_len) {
+		bytes = &zero;
+		bytes_len = 1;
+	} else {
+		add_pad = bytes[0] & 0x80;
+	}

[Severity: Low]
Is the !bytes_len branch and static zero variable reachable here?

The function returns ERR_PTR(-EINVAL) on entry when bytes_len is 0, and the while loop stops decrementing once bytes_len reaches 1, so bytes_len is always at least 1 after the loop.

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

Re: [PATCH 4/9] tpm: Add persistent hierarchies to tpm2_create_primary()

In reply to Jarkko Sakkinen
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] tpm: OOB read and premature state mutation in tpm2_parse_create_primary() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] tpm: OOB read and premature state mutation in tpm2_parse_create_primary()
--

commit 92830ac1679c3da842e2672d38ef677266816bbc
Author: Jarkko Sakkinen <[email protected]>

tpm: Add persistent hierarchies to tpm2_create_primary()

This commit updates tpm2_parse_create_primary() to only populate chip->null_ec_key_x and chip->null_ec_key_y when the hierarchy is TPM2_RH_NULL, verifies the returned key name against the locally computed TPMT_PUBLIC digest, and flushes the transient handle on validation failure.

Quoted 4 lines
drivers/char/tpm/tpm2-sessions.c +0 -0
index b3b4f7a89d9d3..98f668874c3e2 100644

[ ... ]

Quoted 27 lines
drivers/char/tpm/tpm2-sessions.c +10 -10
@@ -1170,85 +1170,87 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	param_len += TPM_HEADER_SIZE;
 
 	if (param_len + 8 > total_len)
-		return -EINVAL;
+		goto err;
 	len = tpm_buf_read_u16(buf, &offset_r);
 	offset_t = offset_r;
 	if (offset_r + len > tpm_buf_length(buf))
-		return -EINVAL;
-	if (name) {
-		/*
-		 * now we have the public area, compute the name of
-		 * the object
-		 */
-		put_unaligned_be16(TPM_ALG_SHA256, name);
-		sha256(&buf->data[offset_r], len, name + 2);
-	}
+		goto err;
+
+	/*
+	 * Now we have the public area, compute the name of the object.
+	 */
+	put_unaligned_be16(TPM_ALG_SHA256, resp_name);
+	sha256(&buf->data[offset_r], len, resp_name + 2);
+	if (name)
+		memcpy(name, resp_name, sizeof(resp_name));

[ ... ]

Quoted 23 lines
drivers/char/tpm/tpm2-sessions.c +6 -4
 	/* extract public key (x and y points) */
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != EC_PT_SZ)
-		return -EINVAL;
+		goto err;
 #ifdef CONFIG_TCG_TPM2_HMAC
-	memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
+	if (hierarchy == TPM2_RH_NULL)
+		memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
 #endif
 	offset_t += val;
 	val = tpm_buf_read_u16(buf, &offset_t);
 	if (val != EC_PT_SZ)
-		return -EINVAL;
+		goto err;
 #ifdef CONFIG_TCG_TPM2_HMAC
-	memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
+	if (hierarchy == TPM2_RH_NULL)
+		memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
 #endif
 	offset_t += val;

drivers/char/tpm/tpm2-sessions.c static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf, · line 1262

Quoted 5 lines
drivers/char/tpm/tpm2-sessions.c +1 -1
@@ -1257,38 +1259,38 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 
 	/* should have exactly consumed the TPM2B public structure */
 	if (offset_t != offset_r)
-		return -EINVAL;
+		goto err;

[Severity: Medium]
Can a truncated TPM2B_PUBLIC response cause the memcpy() calls into chip->null_ec_key_x and chip->null_ec_key_y to read past buf->length and overwrite chip->null_ec_key_{x,y} before validation completes?

At the start of tpm2_parse_create_primary(), only offset_r + len <= tpm_buf_length(buf) is checked, without a minimum bound on len, and the exact consumption check (offset_t != offset_r) runs only after both 32-byte copies have already executed.

In addition, unlike *handle = keyhandle (which was moved to the end of the function), the caller's name buffer and chip->null_ec_key_{x,y} are still updated before the public area, creation ticket, and returned object name are validated. Because this patch also removes the comparison against chip->null_key_name, a post-boot TPM2_RH_NULL call with a mismatched response overwrites the EC coordinates used by tpm2_start_auth_session() to encrypt session salts.

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

Re: [PATCH 6/9] KEYS: Factor out KEYCTL_PKEY

In reply to Jarkko Sakkinen
Quoted 5 lines · Jarkko Sakkinen

Factor out CONFIG_KEYCTL_PKEY from CONFIG_ASYMMETRIC_KEY_TYPE so key types other than asymmetric can select keyctl public-key operations without pulling in crypto/asymmetric_keys/.

This is really the only "innovation" in the new series. Everything else follows from this change.

E.g., the very last patch is relocation for the most part, when compared to the previous series.

Br, Jarkko

#13 lore raw

Re: [PATCH 9/9] KEYS: trusted: Add asymmetric key support for TPM2

In reply to Jarkko Sakkinen
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] KEYS: trusted: 32-bit param_size overflow in tpm2_asym_rsa_decrypt() - [Low] KEYS: trusted…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KEYS: trusted: 32-bit param_size overflow in tpm2_asym_rsa_decrypt()
- [Low] KEYS: trusted: blob_len mismatch in tpm2_load_cmd() and tpm2_asym_load()
--

commit 9a2d1dec7eb8e2fb4164f93f474ab53d2345c7be
Author: Jarkko Sakkinen <[email protected]>

KEYS: trusted: Add asymmetric key support for TPM2

This commit extends the TPM 2.0 trusted key backend to load OID_TPMLoadableKey RSA and ECC keys and expose query, sign, verify, and RSA encrypt/decrypt operations through key_type_trusted.

Quoted 4 lines
security/keys/trusted-keys/trusted_tpm2.c +0 -0
index e3cd02e19857e..e4731724908a7 100644

[ ... ]

Quoted 20 lines
security/keys/trusted-keys/trusted_tpm2.c +9 -1
@@ -750,10 +987,799 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
 	if (rc)
 		goto out;
 
-	rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
+	if (!private->alg_type)
+		rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
 	tpm2_flush_context(chip, blob_handle);
 
 out:
 	tpm_put_ops(chip);
 	return tpm_ret_to_err(rc);
 }
+
+#ifdef CONFIG_TRUSTED_KEYS_ASYMMETRIC
+static int tpm2_asym_load(struct tpm_chip *chip,
+			  const struct trusted_key_payload *payload,
+			  struct trusted_key_tpm *private,
+			  u32 *key_handle)
+{

security/keys/trusted-keys/trusted_tpm2.c int tpm2_unseal_trusted(struct tpm_chip *chip, · line 1014

Quoted 10 lines
security/keys/trusted-keys/trusted_tpm2.c +10 -0
+	u8 *blob __free(kfree) = NULL;
+	unsigned int blob_len;
+	int rc;
+
+	rc = tpm2_key_decode(payload, private, NULL, &blob, &blob_len, NULL);
+	if (rc)
+		return rc;
+
+	return tpm2_load_blob(chip, private, blob, blob_len, key_handle);
+}

[Severity: Low]
Can tpm2_asym_load() pass a different blob_len to tpm2_load_blob() than tpm2_load_cmd() did when the key was instantiated?

In tpm2_load_cmd(), blob_len is clamped to private_len + public_len + 4, which strips any trailing bytes in the ASN.1 OCTET STRINGs before loading. tpm2_asym_load() passes the untruncated ctx.priv_len + ctx.pub_len from tpm2_key_decode() directly to tpm2_load_blob(), so a key with trailing bytes will load during add_key() but fail on subsequent pkey operations.

[ ... ]

Quoted 6 lines
security/keys/trusted-keys/trusted_tpm2.c +6 -0
+static int tpm2_asym_rsa_decrypt(struct tpm_chip *chip,
+				 const struct trusted_key_payload *payload,
+				 struct trusted_key_tpm *private,
+				 struct kernel_pkey_params *params,
+				 const void *in, void *out)
+{

[ ... ]

Quoted 5 lines
security/keys/trusted-keys/trusted_tpm2.c +5 -0
+	offset = TPM_HEADER_SIZE;
+	param_size = tpm_buf_read_u32(buf, &offset);
+	decrypted_len = tpm_buf_read_u16(buf, &offset);
+	if ((buf->flags & TPM_BUF_INVALID) ||
+	    param_size < 2 + decrypted_len ||

security/keys/trusted-keys/trusted_tpm2.c int tpm2_unseal_trusted(struct tpm_chip *chip, · line 1271

Quoted 10 lines
security/keys/trusted-keys/trusted_tpm2.c +10 -0
+	    offset - 2 + param_size > buf->length) {
+		rc = -EIO;
+		goto out_key;
+	}
+	if (params->out_len < decrypted_len) {
+		rc = -EMSGSIZE;
+		goto out_key;
+	}
+
+	memcpy(out, &buf->data[offset], decrypted_len);

[Severity: Medium]
Can offset - 2 + param_size wrap around on 32-bit architectures?

When off_t is 32-bit, a malformed unauthenticated TPM response with param_size >= 0xfffffff2 wraps offset - 2 + param_size past buf->length while satisfying param_size >= 2 + decrypted_len, allowing memcpy() to read up to decrypted_len bytes past buf->length from buf->data. The same 32-bit overflow can occur in tpm2_asym_parse_signature() at offset + param_size > buf->length. Checking
param_size > buf->length - (offset - 2) and
param_size > buf->length - offset avoids the wraparound.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#15 lore raw
In reply to Jarkko Sakkinen
Sashiko has finished reviewing this 9-patch series:

Sashiko has finished reviewing this 9-patch series:

- Patches 1, 3, and 5-8: no issues found.
- Patch 2 ("lib/asn1_encoder: Add asn1_encode_integer_bytes()"): 1 potential issue (1 Low)
- Patch 4 ("tpm: Add persistent hierarchies to tpm2_create_prima..."): 1 potential issue (1 Medium)
- Patch 9 ("KEYS: trusted: Add asymmetric key support for TPM2"): 2 potential issues (1 Medium, 1 Low)

Detailed reviews have been sent as separate replies to patches 2, 4, and 9.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
author maintainer
#14 lore raw

Re: [PATCH 9/9] KEYS: trusted: Add asymmetric key support for TPM2

In reply to [email protected]
Quoted 4 lines · On Tue, Nov 24, 2026 at 12:25:44PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KEYS: trusted: 32-bit param_size overflow in tpm2_asym_rsa_decrypt()

I've now learned that sometimes people expect you to fix these before they will review, or they get upset if you fix them and iterate the series. Someone will be rip their trousers whatever you do.

Or then, some gets pissed when some random bot gives feedback pm the next day.

Since this is the worst of the worst I'll mark it down and let's consider it addressed.

I like Sashiko but don't know how to dance the dance.

Br, Jarkko