Skip to contentSkip to conversation

tpm: Repeal and replace tpm_buf_init*() and tpm_buf_destroy()

PATCH In review Jarkko Sakkinen 1 participant 2 messages Jul 1, 2025

Subsystems (4)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH v3 1/1 tpm: Repeal and replace tpm_buf_init*() and tpm_buf_destroy()

From: Jarkko Sakkinen <[email protected]>

Repeal and replace tpm_buf_init() and tpm_buf_init_sized() with tpm_buf_alloc(), which returns PAGE_SIZE of memory with the struct tpm_buf header in the beginning of the returned buffer. This leaves 4088 bytes of free space for data.

Given that kfree() is now the destructor for struct tpm_buf instances, tpm_buf_destroy() is obsolete, and can (and should) be wiped off.

The gist of this patch is that now a tpm_buf can be now declared using __free(kfree) declared in linux/slab.h:

	struct tpm_buf *buf __free(kfree) = NULL;

	/* ... */

	buf = tpm_buf_alloc();

This makes TPM driver and other subsystems using struct tpm_buf more memory-safe as there is no to worry about freeing up these temporary scratch buffers.

v3:
- Removed the cleanup class and moved on using __free(kfree) instead.
- Removed buf_size (James).
- I'm open for the idea of splitting still (Jason) but I'll hold at least this revision just to check that my core idea here is correct.
v2:
- Implement also memory allocation using the cleanup class.
- Rewrote the commit message.
- Implemented CLASS_TPM_BUF() helper macro.

---
 drivers/char/tpm/tpm-buf.c                |  52 ++---
 drivers/char/tpm/tpm-sysfs.c              |  20 +-
 drivers/char/tpm/tpm1-cmd.c               | 150 ++++++------
 drivers/char/tpm/tpm2-cmd.c               | 272 ++++++++++------------
 drivers/char/tpm/tpm2-sessions.c          | 122 +++++-----
 drivers/char/tpm/tpm2-space.c             |  44 ++--
 drivers/char/tpm/tpm_vtpm_proxy.c         |  30 ++-
 include/linux/tpm.h                       |  15 +-
 security/keys/trusted-keys/trusted_tpm1.c |  35 +--
 security/keys/trusted-keys/trusted_tpm2.c | 157 ++++++-------
 10 files changed, 414 insertions(+), 483 deletions(-)
drivers/char/tpm/tpm-buf.c +17 -35
index dc882fc9fa9e..e874bb57600c 100644
@@ -8,23 +8,28 @@
 #include <linux/tpm.h>
 
 /**
- * tpm_buf_init() - Allocate and initialize a TPM command
- * @buf:	A &tpm_buf
- * @tag:	TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS
- * @ordinal:	A command ordinal
+ * tpm_buf_alloc() - Allocate a TPM buffer
+ *
+ * Allocates `PAGE_SIZE` of memory and places &tpm_buf to the beginning of the
+ * allocated buffer. For that reason the total capacity can be at most 4088
+ * bytes. If a caller tries to surpass the maximum capacity, `TPM_BUF_OVERFLOW`
+ * will be set and any future operations will be ignored.
  *
- * Return: 0 or -ENOMEM
+ * Return:
+ * * &tpm_buf:	success
+ * * NULL:	out of memory
  */
-int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
+struct tpm_buf *tpm_buf_alloc(void)
 {
-	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
-	if (!buf->data)
-		return -ENOMEM;
+	struct tpm_buf *buf;
 
-	tpm_buf_reset(buf, tag, ordinal);
-	return 0;
+	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!buf)
+		return NULL;
+
+	return buf;
 }
-EXPORT_SYMBOL_GPL(tpm_buf_init);
+EXPORT_SYMBOL_GPL(tpm_buf_alloc);
 
 /**
  * tpm_buf_reset() - Initialize a TPM command
@@ -48,23 +53,6 @@ void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
 }
 EXPORT_SYMBOL_GPL(tpm_buf_reset);
 
-/**
- * tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer
- * @buf:	A @tpm_buf
- *
- * Return: 0 or -ENOMEM
- */
-int tpm_buf_init_sized(struct tpm_buf *buf)
-{
-	buf->data = (u8 *)__get_free_page(GFP_KERNEL);
-	if (!buf->data)
-		return -ENOMEM;
-
-	tpm_buf_reset_sized(buf);
-	return 0;
-}
-EXPORT_SYMBOL_GPL(tpm_buf_init_sized);
-
 /**
  * tpm_buf_reset_sized() - Initialize a sized buffer
  * @buf:	A &tpm_buf
@@ -78,12 +66,6 @@ void tpm_buf_reset_sized(struct tpm_buf *buf)
 }
 EXPORT_SYMBOL_GPL(tpm_buf_reset_sized);
 
-void tpm_buf_destroy(struct tpm_buf *buf)
-{
-	free_page((unsigned long)buf->data);
-}
-EXPORT_SYMBOL_GPL(tpm_buf_destroy);
-
 /**
  * tpm_buf_length() - Return the number of bytes consumed by the data
  * @buf:	A &tpm_buf
drivers/char/tpm/tpm-sysfs.c +10 -10
index 94231f052ea7..9aaebd8057d4 100644
@@ -32,28 +32,30 @@ struct tpm_readpubek_out {
 static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
 			  char *buf)
 {
-	struct tpm_buf tpm_buf;
+	struct tpm_buf *tpm_buf __free(kfree) = NULL;
 	struct tpm_readpubek_out *out;
 	int i;
 	char *str = buf;
 	struct tpm_chip *chip = to_tpm_chip(dev);
 	char anti_replay[20];
 
+	tpm_buf = tpm_buf_alloc();
+	if (!tpm_buf)
+		return -ENOMEM;
+
 	memset(&anti_replay, 0, sizeof(anti_replay));
 
 	if (tpm_try_get_ops(chip))
 		return 0;
 
-	if (tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK))
-		goto out_ops;
-
-	tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
+	tpm_buf_reset(tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK);
+	tpm_buf_append(tpm_buf, anti_replay, sizeof(anti_replay));
 
-	if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
+	if (tpm_transmit_cmd(chip, tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
 			     "attempting to read the PUBEK"))
-		goto out_buf;
+		goto out_ops;
 
-	out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
+	out = (struct tpm_readpubek_out *)&tpm_buf->data[10];
 	str +=
 	    sprintf(str,
 		    "Algorithm: %4ph\n"
@@ -71,8 +73,6 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
 	for (i = 0; i < 256; i += 16)
 		str += sprintf(str, "%16ph\n", &out->modulus[i]);
 
-out_buf:
-	tpm_buf_destroy(&tpm_buf);
 out_ops:
 	tpm_put_ops(chip);
 	return str - buf;
drivers/char/tpm/tpm1-cmd.c +72 -78

276 lines not on this page · raw message

drivers/char/tpm/tpm2-cmd.c +128 -144

521 lines not on this page · raw message

drivers/char/tpm/tpm2-sessions.c +59 -63

227 lines not on this page · raw message

drivers/char/tpm/tpm2-space.c +18 -26

106 lines not on this page · raw message

drivers/char/tpm/tpm_vtpm_proxy.c +13 -17
index 0818bb517805..6d76fe28826a 100644
@@ -395,40 +395,36 @@ static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip  *chip, u8 status)
 
 static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
 {
-	struct tpm_buf buf;
+	struct tpm_buf *buf __free(kfree) = NULL;
 	int rc;
 	const struct tpm_header *header;
 	struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
 
+	buf = tpm_buf_alloc();
+	if (!buf)
+		return -ENOMEM;
+
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
-		rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS,
-				  TPM2_CC_SET_LOCALITY);
+		tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_SET_LOCALITY);
 	else
-		rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND,
-				  TPM_ORD_SET_LOCALITY);
-	if (rc)
-		return rc;
-	tpm_buf_append_u8(&buf, locality);
+		tpm_buf_reset(buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SET_LOCALITY);
+
+	tpm_buf_append_u8(buf, locality);
 
 	proxy_dev->state |= STATE_DRIVER_COMMAND;
 
-	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to set locality");
+	rc = tpm_transmit_cmd(chip, buf, 0, "attempting to set locality");
 
 	proxy_dev->state &= ~STATE_DRIVER_COMMAND;
 
-	if (rc < 0) {
-		locality = rc;
-		goto out;
-	}
+	if (rc < 0)
+		return rc;
 
-	header = (const struct tpm_header *)buf.data;
+	header = (const struct tpm_header *)buf->data;
 	rc = be32_to_cpu(header->return_code);
 	if (rc)
 		locality = -1;
 
-out:
-	tpm_buf_destroy(&buf);
-
 	return locality;
 }
 
include/linux/tpm.h +7 -8
index 804fbbe3873d..463174182701 100644
@@ -375,13 +375,14 @@ enum tpm_buf_flags {
 };
 
 /*
- * A string buffer type for constructing TPM commands.
+ * A buffer for constructing and parsing TPM commands, responses and sized
+ * (TPM2B) buffers.
  */
 struct tpm_buf {
-	u32 flags;
-	u32 length;
-	u8 *data;
-	u8 handles;
+	u16 flags;
+	u16 length;
+	u16 handles;
+	u8 data[];
 };
 
 enum tpm2_object_attributes {
@@ -412,11 +413,9 @@ struct tpm2_hash {
 	unsigned int tpm_id;
 };
 
-int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal);
+struct tpm_buf *tpm_buf_alloc(void);
 void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal);
-int tpm_buf_init_sized(struct tpm_buf *buf);
 void tpm_buf_reset_sized(struct tpm_buf *buf);
-void tpm_buf_destroy(struct tpm_buf *buf);
 u32 tpm_buf_length(struct tpm_buf *buf);
 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);
security/keys/trusted-keys/trusted_tpm1.c +18 -17
index 89c9798d1800..106496c68625 100644
@@ -356,21 +356,24 @@ static int TSS_checkhmac2(unsigned char *buffer,
  */
 int trusted_tpm_send(unsigned char *cmd, size_t buflen)
 {
-	struct tpm_buf buf;
+	struct tpm_buf *buf __free(kfree) = NULL;
 	int rc;
 
 	if (!chip)
 		return -ENODEV;
 
+	buf = tpm_buf_alloc();
+	if (!buf)
+		return -ENOMEM;
+
 	rc = tpm_try_get_ops(chip);
 	if (rc)
 		return rc;
 
-	buf.flags = 0;
-	buf.length = buflen;
-	buf.data = cmd;
+	tpm_buf_append(buf, cmd, buflen);
+
 	dump_tpm_buf(cmd);
-	rc = tpm_transmit_cmd(chip, &buf, 4, "sending data");
+	rc = tpm_transmit_cmd(chip, buf, 4, "sending data");
 	dump_tpm_buf(cmd);
 
 	if (rc > 0)
@@ -672,23 +675,22 @@ static int tpm_unseal(struct tpm_buf *tb,
 static int key_seal(struct trusted_key_payload *p,
 		    struct trusted_key_options *o)
 {
-	struct tpm_buf tb;
+	struct tpm_buf *tb __free(kfree) = NULL;
 	int ret;
 
-	ret = tpm_buf_init(&tb, 0, 0);
-	if (ret)
-		return ret;
+	tb = tpm_buf_alloc();
+	if (!tb)
+		return -ENOMEM;
 
 	/* include migratable flag at end of sealed key */
 	p->key[p->key_len] = p->migratable;
 
-	ret = tpm_seal(&tb, o->keytype, o->keyhandle, o->keyauth,
+	ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
 		       p->key, p->key_len + 1, p->blob, &p->blob_len,
 		       o->blobauth, o->pcrinfo, o->pcrinfo_len);
 	if (ret < 0)
 		pr_info("srkseal failed (%d)\n", ret);
 
-	tpm_buf_destroy(&tb);
 	return ret;
 }
 
@@ -698,14 +700,14 @@ static int key_seal(struct trusted_key_payload *p,
 static int key_unseal(struct trusted_key_payload *p,
 		      struct trusted_key_options *o)
 {
-	struct tpm_buf tb;
+	struct tpm_buf *tb __free(kfree) = NULL;
 	int ret;
 
-	ret = tpm_buf_init(&tb, 0, 0);
-	if (ret)
-		return ret;
+	tb = tpm_buf_alloc();
+	if (!tb)
+		return -ENOMEM;
 
-	ret = tpm_unseal(&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
+	ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
 			 o->blobauth, p->key, &p->key_len);
 	if (ret < 0)
 		pr_info("srkunseal failed (%d)\n", ret);
@@ -713,7 +715,6 @@ static int key_unseal(struct trusted_key_payload *p,
 		/* pull migratable flag out of sealed key */
 		p->migratable = p->key[--p->key_len];
 
-	tpm_buf_destroy(&tb);
 	return ret;
 }
 
security/keys/trusted-keys/trusted_tpm2.c +72 -85

288 lines not on this page · raw message

Signature · 1 line
2.39.5
author maintainer
#1 lore raw
Quoted 6 lines · Jarkko Sakkinen

From: Jarkko Sakkinen <[email protected]>

Repeal and replace tpm_buf_init() and tpm_buf_init_sized() with tpm_buf_alloc(), which returns PAGE_SIZE of memory with the struct tpm_buf header in the beginning of the returned buffer. This leaves 4088 bytes of free space for data.

A minor error: it is now 4090 bytes:

struct tpm_buf {
	u16 flags;
	u16 length;
	u16 handles;
	u8 data[];
};

I guess this should be changed into:

struct tpm_buf {
	u8 flags;
	u8 handles;
	u16 length;
	u8 data[];
}

It's not strictly necessary given that we are already "above the hardware spec" but it neither makes much sense to keep handles or flags 16-bit words with MSB always empty in both of the words.

BR, Jarkko