All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: keyrings@vger.kernel.org
Cc: "Jarkko Sakkinen" <jarkko@kernel.org>,
	dm-devel@lists.linux.dev, linux-crypto@vger.kernel.org,
	linux-doc@vger.kernel.org, linux-integrity@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	"Ahmad Fatoum" <a.fatoum@pengutronix.de>,
	"Alasdair Kergon" <agk@redhat.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Benjamin Marzinski" <bmarzins@redhat.com>,
	"David Gstir" <david@sigma-star.at>,
	"David Howells" <dhowells@redhat.com>,
	"David Laight" <david.laight.linux@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	"David Woodhouse" <dwmw2@infradead.org>,
	"Eric Biggers" <ebiggers@kernel.org>,
	"Herbert Xu" <herbert@gondor.apana.org.au>,
	"Ignat Korchagin" <ignat@linux.win>,
	"James Bottomley" <James.Bottomley@HansenPartnership.com>,
	"James Morris" <jmorris@namei.org>,
	"James Prestwood" <prestwoj@gmail.com>,
	"Jason Gunthorpe" <jgg@ziepe.ca>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Jonathan McDowell" <noodles@meta.com>,
	"Lukas Wunner" <lukas@wunner.de>,
	"Marek Behún" <kabel@kernel.org>,
	"Mike Snitzer" <snitzer@kernel.org>,
	"Mikulas Patocka" <mpatocka@redhat.com>,
	"Mimi Zohar" <zohar@linux.ibm.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"Peter Huewe" <peterhuewe@gmx.de>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Sumit Garg" <sumit.garg@kernel.org>
Subject: [PATCH v6 07/18] tpm-buf: Make tpm_buf_read*() fallible
Date: Sat, 28 Nov 2026 23:56:09 +0200	[thread overview]
Message-ID: <20261128215621.2253928-8-jarkko@kernel.org> (raw)
In-Reply-To: <20261128215621.2253928-1-jarkko@kernel.org>

Make tpm_buf_read_u8(), tpm_buf_read_u16() and tpm_buf_read_u32() flows
unambiguous, and fallible by writing the updated offset to @offset.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 drivers/char/tpm/tpm-buf.c                |  56 ++++---
 drivers/char/tpm/tpm2-sessions.c          | 189 ++++++++++++++++------
 include/linux/tpm_buf.h                   |   6 +-
 security/keys/trusted-keys/trusted_tpm2.c |  10 +-
 4 files changed, 180 insertions(+), 81 deletions(-)

diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c
index 233e81d3f149..a2d79b3a9385 100644
--- a/drivers/char/tpm/tpm-buf.c
+++ b/drivers/char/tpm/tpm-buf.c
@@ -204,39 +204,35 @@ void tpm_buf_append_handle(struct tpm_buf *buf, u32 handle)
  * @offset:	offset within the buffer
  * @count:	the number of bytes to read
  * @output:	the output buffer
+ *
+ * Return: true on success, and @offset is advanced by @count bytes, or false
+ * if the read would cross the end of the buffer.
  */
-static void tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void *output)
+static bool tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void *output)
 {
 	off_t next_offset;
 
-	if (buf->flags & TPM_BUF_INVALID)
-		return;
-
 	next_offset = *offset + count;
-	if (next_offset > buf->length) {
-		WARN(1, "tpm_buf: read out of boundary\n");
-		buf->flags |= TPM_BUF_INVALID;
-		return;
-	}
+	if (next_offset > buf->length)
+		return false;
 
 	memcpy(output, &buf->data[*offset], count);
 	*offset = next_offset;
+	return true;
 }
 
 /**
  * tpm_buf_read_u8() - Read 8-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 8-bit word
  *
- * Return: next 8-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset, u8 *value)
 {
-	u8 value = 0;
-
-	tpm_buf_read(buf, offset, sizeof(value), &value);
-
-	return value;
+	return tpm_buf_read(buf, offset, sizeof(*value), value);
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u8);
 
@@ -244,16 +240,20 @@ EXPORT_SYMBOL_GPL(tpm_buf_read_u8);
  * tpm_buf_read_u16() - Read 16-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 16-bit word
  *
- * Return: next 16-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset, u16 *value)
 {
-	u16 value = 0;
+	__be16 value_be;
 
-	tpm_buf_read(buf, offset, sizeof(value), &value);
+	if (!tpm_buf_read(buf, offset, sizeof(value_be), &value_be))
+		return false;
 
-	return be16_to_cpu(value);
+	*value = be16_to_cpu(value_be);
+	return true;
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u16);
 
@@ -261,16 +261,20 @@ EXPORT_SYMBOL_GPL(tpm_buf_read_u16);
  * tpm_buf_read_u32() - Read 32-bit word from a TPM buffer
  * @buf:	&tpm_buf instance
  * @offset:	offset within the buffer
+ * @value:	returns the 32-bit word
  *
- * Return: next 32-bit word
+ * Return: true on success, and @offset is advanced, or false if the read
+ * would cross the end of the buffer.
  */
-u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset)
+bool tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset, u32 *value)
 {
-	u32 value = 0;
+	__be32 value_be;
 
-	tpm_buf_read(buf, offset, sizeof(value), &value);
+	if (!tpm_buf_read(buf, offset, sizeof(value_be), &value_be))
+		return false;
 
-	return be32_to_cpu(value);
+	*value = be32_to_cpu(value_be);
+	return true;
 }
 EXPORT_SYMBOL_GPL(tpm_buf_read_u32);
 
diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c
index ced5119614bf..5ca1203df21a 100644
--- a/drivers/char/tpm/tpm2-sessions.c
+++ b/drivers/char/tpm/tpm2-sessions.c
@@ -179,6 +179,7 @@ int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 	off_t offset = TPM_HEADER_SIZE;
 	struct tpm_buf *buf __free(kfree) = NULL;
 	int rc, name_size_alg;
+	u16 size;
 
 	if (mso != TPM2_MSO_PERSISTENT && mso != TPM2_MSO_VOLATILE &&
 	    mso != TPM2_MSO_NVRAM) {
@@ -199,8 +200,10 @@ int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 		return tpm_ret_to_err(rc);
 
 	/* Skip TPMT_PUBLIC: */
-	rc = tpm_buf_read_u16(buf, &offset);
-	offset += rc;
+	if (!tpm_buf_read_u16(buf, &offset, &size))
+		return -EIO;
+
+	offset += size;
 
 	/*
 	 * Ensure space for the length field of TPM2B_NAME and hashAlg field of
@@ -209,18 +212,20 @@ int tpm2_read_public(struct tpm_chip *chip, u32 handle, void *name)
 	if (offset + 4 > tpm_buf_length(buf))
 		return -EIO;
 
-	rc = tpm_buf_read_u16(buf, &offset);
+	if (!tpm_buf_read_u16(buf, &offset, &size))
+		return -EIO;
+
 	name_size_alg = name_size(&buf->data[offset]);
 	if (name_size_alg < 0)
 		return name_size_alg;
 
-	if (rc != name_size_alg)
+	if (size != name_size_alg)
 		return -EIO;
 
-	if (offset + rc > tpm_buf_length(buf))
+	if (offset + size > tpm_buf_length(buf))
 		return -EIO;
 
-	memcpy(name, &buf->data[offset], rc);
+	memcpy(name, &buf->data[offset], size);
 	return name_size_alg;
 }
 EXPORT_SYMBOL_GPL(tpm2_read_public);
@@ -618,21 +623,41 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
 	handles = (attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0);
 	offset_s += handles * sizeof(u32);
 
-	val = tpm_buf_read_u32(buf, &offset_s);
+	if (!tpm_buf_read_u32(buf, &offset_s, &val)) {
+		ret = -EIO;
+		goto err;
+	}
+
 	/* point offset_p to the start of the parameters */
 	offset_p = offset_s + val;
 	for (i = 1; offset_s < offset_p; i++) {
-		u32 handle = tpm_buf_read_u32(buf, &offset_s);
+		u32 handle;
 		u16 len;
 		u8 a;
 
+		if (!tpm_buf_read_u32(buf, &offset_s, &handle)) {
+			ret = -EIO;
+			goto err;
+		}
+
 		/* nonce (already in auth) */
-		len = tpm_buf_read_u16(buf, &offset_s);
+		if (!tpm_buf_read_u16(buf, &offset_s, &len)) {
+			ret = -EIO;
+			goto err;
+		}
+
 		offset_s += len;
 
-		a = tpm_buf_read_u8(buf, &offset_s);
+		if (!tpm_buf_read_u8(buf, &offset_s, &a)) {
+			ret = -EIO;
+			goto err;
+		}
+
+		if (!tpm_buf_read_u16(buf, &offset_s, &len)) {
+			ret = -EIO;
+			goto err;
+		}
 
-		len = tpm_buf_read_u16(buf, &offset_s);
 		if (handle == auth->handle && auth->attrs == a) {
 			hmac = &buf->data[offset_s];
 			/*
@@ -665,7 +690,11 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)
 			  auth->tpm_nonce, AES_KEY_BYTES + AES_BLOCK_SIZE,
 			  auth->scratch);
 
-		len = tpm_buf_read_u16(buf, &offset_p);
+		if (!tpm_buf_read_u16(buf, &offset_p, &len)) {
+			ret = -EIO;
+			goto err;
+		}
+
 		aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
 		aescfb_encrypt(&auth->aes_key, &buf->data[offset_p],
 			       &buf->data[offset_p], len,
@@ -740,7 +769,10 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 	struct sha256_ctx sctx;
 	struct hmac_sha256_ctx hctx;
 	u16 tag = be16_to_cpu(head->tag);
-	int parm_len, len, i, handles;
+	u32 parm_len;
+	int i, handles;
+	u8 session_attrs;
+	u16 len;
 
 	if (!auth)
 		return rc;
@@ -770,26 +802,38 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 
 	/* point to area beyond handles */
 	offset_s = TPM_HEADER_SIZE + handles * 4;
-	parm_len = tpm_buf_read_u32(buf, &offset_s);
+	if (!tpm_buf_read_u32(buf, &offset_s, &parm_len))
+		goto out;
+
 	offset_p = offset_s;
 	offset_s += parm_len;
 	/* skip over any sessions before ours */
 	for (i = 0; i < auth->session - 1; i++) {
-		len = tpm_buf_read_u16(buf, &offset_s);
+		if (!tpm_buf_read_u16(buf, &offset_s, &len))
+			goto out;
+
 		offset_s += len + 1;
-		len = tpm_buf_read_u16(buf, &offset_s);
+		if (!tpm_buf_read_u16(buf, &offset_s, &len))
+			goto out;
+
 		offset_s += len;
 	}
 	/* TPM nonce */
-	len = tpm_buf_read_u16(buf, &offset_s);
+	if (!tpm_buf_read_u16(buf, &offset_s, &len))
+		goto out;
+
 	if (offset_s + len > tpm_buf_length(buf))
 		goto out;
 	if (len != SHA256_DIGEST_SIZE)
 		goto out;
 	memcpy(auth->tpm_nonce, &buf->data[offset_s], len);
 	offset_s += len;
-	attrs = tpm_buf_read_u8(buf, &offset_s);
-	len = tpm_buf_read_u16(buf, &offset_s);
+	if (!tpm_buf_read_u8(buf, &offset_s, &session_attrs))
+		goto out;
+
+	if (!tpm_buf_read_u16(buf, &offset_s, &len))
+		goto out;
+
 	if (offset_s + len != tpm_buf_length(buf))
 		goto out;
 	if (len != SHA256_DIGEST_SIZE)
@@ -831,7 +875,10 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 			  auth->our_nonce, AES_KEY_BYTES + AES_BLOCK_SIZE,
 			  auth->scratch);
 
-		len = tpm_buf_read_u16(buf, &offset_p);
+		if (!tpm_buf_read_u16(buf, &offset_p, &len)) {
+			rc = -EINVAL;
+			goto out;
+		}
 		aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
 		aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
 			       &buf->data[offset_p], len,
@@ -884,7 +931,7 @@ static int tpm2_parse_start_auth_session(struct tpm2_auth *auth,
 	struct tpm_header *head = (struct tpm_header *)buf->data;
 	u32 tot_len = be32_to_cpu(head->length);
 	off_t offset = TPM_HEADER_SIZE;
-	u32 val;
+	u16 val;
 
 	/* we're starting after the header so adjust the length */
 	tot_len -= TPM_HEADER_SIZE;
@@ -893,8 +940,12 @@ static int tpm2_parse_start_auth_session(struct tpm2_auth *auth,
 	if (tot_len != 4 + 2 + sizeof(auth->tpm_nonce))
 		return -EINVAL;
 
-	auth->handle = tpm_buf_read_u32(buf, &offset);
-	val = tpm_buf_read_u16(buf, &offset);
+	if (!tpm_buf_read_u32(buf, &offset, &auth->handle))
+		return -EIO;
+
+	if (!tpm_buf_read_u16(buf, &offset, &val))
+		return -EIO;
+
 	if (val != sizeof(auth->tpm_nonce))
 		return -EINVAL;
 	memcpy(auth->tpm_nonce, &buf->data[offset], sizeof(auth->tpm_nonce));
@@ -1066,15 +1117,20 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	off_t offset_r = TPM_HEADER_SIZE, offset_t;
 	u16 len = TPM_HEADER_SIZE;
 	u32 total_len = be32_to_cpu(head->length);
-	u32 val, param_len, keyhandle;
+	u32 val32, param_len, keyhandle;
+	u16 val;
+
+	if (!tpm_buf_read_u32(buf, &offset_r, &keyhandle))
+		return -EIO;
 
-	keyhandle = tpm_buf_read_u32(buf, &offset_r);
 	if (handle)
 		*handle = keyhandle;
 	else
 		tpm2_flush_context(chip, keyhandle);
 
-	param_len = tpm_buf_read_u32(buf, &offset_r);
+	if (!tpm_buf_read_u32(buf, &offset_r, &param_len))
+		return -EIO;
+
 	/*
 	 * param_len doesn't include the header, but all the other
 	 * lengths and offsets do, so add it to parm len to make
@@ -1084,7 +1140,10 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 
 	if (param_len + 8 > total_len)
 		return -EINVAL;
-	len = tpm_buf_read_u16(buf, &offset_r);
+
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	offset_t = offset_r;
 	if (name) {
 		/*
@@ -1096,64 +1155,88 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	}
 
 	/* validate the public key */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
 
 	/* key type (must be what we asked for) */
 	if (val != TPM_ALG_ECC)
 		return -EINVAL;
-	val = tpm_buf_read_u16(buf, &offset_t);
+
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
 
 	/* name algorithm */
 	if (val != TPM_ALG_SHA256)
 		return -EINVAL;
-	val = tpm_buf_read_u32(buf, &offset_t);
+
+	if (!tpm_buf_read_u32(buf, &offset_t, &val32))
+		return -EIO;
 
 	/* object properties */
-	if (val != TPM2_OA_NULL_KEY)
+	if (val32 != TPM2_OA_NULL_KEY)
 		return -EINVAL;
 
 	/* auth policy (empty) */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != 0)
 		return -EINVAL;
 
 	/* symmetric key parameters */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM_ALG_AES)
 		return -EINVAL;
 
 	/* symmetric key length */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != AES_KEY_BITS)
 		return -EINVAL;
 
 	/* symmetric encryption scheme */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM_ALG_CFB)
 		return -EINVAL;
 
 	/* signing scheme */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM_ALG_NULL)
 		return -EINVAL;
 
 	/* ECC Curve */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM2_ECC_NIST_P256)
 		return -EINVAL;
 
 	/* KDF Scheme */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != TPM_ALG_NULL)
 		return -EINVAL;
 
 	/* extract public key (x and y points) */
-	val = tpm_buf_read_u16(buf, &offset_t);
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != EC_PT_SZ)
 		return -EINVAL;
 	memcpy(chip->null_ec_key_x, &buf->data[offset_t], val);
 	offset_t += val;
-	val = tpm_buf_read_u16(buf, &offset_t);
+
+	if (!tpm_buf_read_u16(buf, &offset_t, &val))
+		return -EIO;
+
 	if (val != EC_PT_SZ)
 		return -EINVAL;
 	memcpy(chip->null_ec_key_y, &buf->data[offset_t], val);
@@ -1169,30 +1252,40 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 		return -EINVAL;
 
 	/* creation data (skip) */
-	len = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	offset_r += len;
 	if (offset_r > param_len)
 		return -EINVAL;
 
 	/* creation digest (must be sha256) */
-	len = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	offset_r += len;
 	if (len != SHA256_DIGEST_SIZE || offset_r > param_len)
 		return -EINVAL;
 
 	/* TPMT_TK_CREATION follows */
 	/* tag, must be TPM_ST_CREATION (0x8021) */
-	val = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &val))
+		return -EIO;
+
 	if (val != TPM2_ST_CREATION || offset_r > param_len)
 		return -EINVAL;
 
 	/* hierarchy */
-	val = tpm_buf_read_u32(buf, &offset_r);
-	if (val != hierarchy || offset_r > param_len)
+	if (!tpm_buf_read_u32(buf, &offset_r, &val32))
+		return -EIO;
+
+	if (val32 != hierarchy || offset_r > param_len)
 		return -EINVAL;
 
 	/* the ticket digest HMAC (might not be sha256) */
-	len = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	offset_r += len;
 	if (offset_r > param_len)
 		return -EINVAL;
@@ -1201,7 +1294,9 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 	 * finally we have the name, which is a sha256 digest plus a 2
 	 * byte algorithm type
 	 */
-	len = tpm_buf_read_u16(buf, &offset_r);
+	if (!tpm_buf_read_u16(buf, &offset_r, &len))
+		return -EIO;
+
 	if (offset_r + len != param_len + 8)
 		return -EINVAL;
 	if (len != SHA256_DIGEST_SIZE + 2)
diff --git a/include/linux/tpm_buf.h b/include/linux/tpm_buf.h
index f8c105d8b8bf..d0f4c1c3ee2d 100644
--- a/include/linux/tpm_buf.h
+++ b/include/linux/tpm_buf.h
@@ -52,9 +52,9 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length);
 void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value);
 void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value);
 void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value);
-u8 tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset);
-u16 tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset);
-u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset);
+bool tpm_buf_read_u8(struct tpm_buf *buf, off_t *offset, u8 *value);
+bool tpm_buf_read_u16(struct tpm_buf *buf, off_t *offset, u16 *value);
+bool tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset, u32 *value);
 void tpm_buf_append_handle(struct tpm_buf *buf, u32 handle);
 
 #endif
diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c
index 575b488a4f78..e20e54203b83 100644
--- a/security/keys/trusted-keys/trusted_tpm2.c
+++ b/security/keys/trusted-keys/trusted_tpm2.c
@@ -217,6 +217,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	struct tpm_buf *sized __free(kfree) = NULL;
 	u16 parent_name_size;
 	int blob_len = 0;
+	u32 size;
 	int hash;
 	u32 flags;
 	int rc;
@@ -323,18 +324,17 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
 	if (rc)
 		goto out;
 
-	blob_len = tpm_buf_read_u32(buf, &offset);
-	if (buf->flags & TPM_BUF_INVALID) {
-		rc = -E2BIG;
+	if (!tpm_buf_read_u32(buf, &offset, &size)) {
+		rc = -EIO;
 		goto out;
 	}
 
-	if (buf->length - offset < blob_len) {
+	if (buf->length - offset < size) {
 		rc = -EFAULT;
 		goto out;
 	}
 
-	blob_len = tpm2_key_encode(payload, options, &buf->data[offset], blob_len);
+	blob_len = tpm2_key_encode(payload, options, &buf->data[offset], size);
 	if (blob_len < 0)
 		rc = blob_len;
 
-- 
2.47.3


  parent reply	other threads:[~2026-11-28 21:57 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 21:56 [PATCH v6 00/18] KEYS: trusted: asymmetric keys Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 01/18] KEYS: trusted: Validate public_len before reading key attributes Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 02/18] KEYS: Factor out KEYCTL_PKEY Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 03/18] tpm2-sessions: Drop TPM2_NAME_SIZE Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 04/18] KEYS: trusted: Open code tpm2_buf_append_auth() Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 05/18] KEYS: trusted: Remove dead branch from tpm2_unseal_cmd Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 06/18] tpm2-sessions: Re-orchestrate tpm2_read_public() use Jarkko Sakkinen
2026-11-28 21:56 ` Jarkko Sakkinen [this message]
2026-11-28 21:56 ` [PATCH v6 08/18] KEYS: trusted: Allow ASN.1 TPM2 sealed data shorter than MIN_KEY_SIZE Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 09/18] lib/asn1_encoder: Add asn1_encode_integer_bytes() Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 10/18] tpm: Export tpm2_create_primary() Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 11/18] tpm: Add persistent hierarchies to tpm2_create_primary() Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 12/18] KEYS: trusted: Extend TPMKey support Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 13/18] KEYS: trusted: Reject zero-length keys in symmetric consumers Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 14/18] KEYS: trusted: Add private backend data to trusted_key_payload Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 15/18] KEYS: trusted: Factor out TPM2 blob loading Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 16/18] KEYS: trusted: Add asymmetric key operations Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 17/18] KEYS: trusted: Add asymmetric key support for TPM2 Jarkko Sakkinen
2026-11-28 21:56 ` [PATCH v6 18/18] docs: trusted-encrypted: Document asymmetric trusted keys Jarkko Sakkinen
2026-11-28 22:07 ` [PATCH v6 00/18] KEYS: trusted: asymmetric keys Jarkko Sakkinen
2026-11-28 22:22   ` Jarkko Sakkinen
2026-11-28 22:27     ` Jarkko Sakkinen
2026-11-28 22:22 ` sashiko-bot
2026-11-29 12:23 ` Jarkko Sakkinen
2026-11-29 12:37   ` Jarkko Sakkinen
2026-11-29 12:40     ` Jarkko Sakkinen
2026-11-29 23:23 ` Eric Biggers
2026-11-30  1:03   ` Jarkko Sakkinen
2026-11-30  1:14     ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128215621.2253928-8-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=a.fatoum@pengutronix.de \
    --cc=agk@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=bmarzins@redhat.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=david.laight.linux@gmail.com \
    --cc=david@sigma-star.at \
    --cc=dhowells@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=dwmw2@infradead.org \
    --cc=ebiggers@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=ignat@linux.win \
    --cc=jgg@ziepe.ca \
    --cc=jmorris@namei.org \
    --cc=kabel@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=mpatocka@redhat.com \
    --cc=noodles@meta.com \
    --cc=paul@paul-moore.com \
    --cc=peterhuewe@gmx.de \
    --cc=prestwoj@gmail.com \
    --cc=rdunlap@infradead.org \
    --cc=serge@hallyn.com \
    --cc=skhan@linuxfoundation.org \
    --cc=snitzer@kernel.org \
    --cc=sumit.garg@kernel.org \
    --cc=zohar@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.