Skip to contentSkip to conversation

tpm: introduce struct tpm_buf

PATCH Superseded Jarkko Sakkinen 2 participants 7 messages Jun 3, 2015 – Jun 9, 2015 TPM device driver

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tpm: introduce struct tpm_buf

This patch introduces struct tpm_buf that provides a string buffer for constructing TPM commands. This allows to construct variable sized TPM commands. This feature is needed for TPM 2.0 commands in order to allow policy authentication and algorithmic agility.

The commands in the tpm2-cmd.c have been updated to use struct tpm_buf. Lots of awkward length calculations could be dropped because the buffer knows its length.

The code is is along the lines of the string buffer code in security/trusted/trusted.h.

Changes since v1:

- Fixed potential alignment issues in tpm_buf_tag().

---
 drivers/char/tpm/tpm.h      |  87 ++++++++++++
 drivers/char/tpm/tpm2-cmd.c | 321 ++++++++++++++++----------------------------
 2 files changed, 200 insertions(+), 208 deletions(-)
drivers/char/tpm/tpm.h +87 -0
index f8319a0..205e7c2 100644
@@ -382,6 +382,93 @@ struct tpm_cmd_t {
 	tpm_cmd_params	params;
 } __packed;
 
+/* A string buffer type for constructing TPM commands. This is based on the
+ * code in security/keys/trusted.h.
+ */
+
+#define TPM_BUF_SIZE 512
+
+struct tpm_buf {
+	u8 data[TPM_BUF_SIZE];
+} __attribute__((aligned(8)));
+
+static inline void tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
+{
+	struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
+
+	head->tag = cpu_to_be16(tag);
+	head->length = cpu_to_be32(sizeof(*head));
+	head->ordinal = cpu_to_be32(ordinal);
+}
+
+static inline u32 tpm_buf_length(struct tpm_buf *buf)
+{
+	struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
+
+	return be32_to_cpu(head->length);
+}
+
+static inline u16 tpm_buf_tag(struct tpm_buf *buf)
+{
+	struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
+
+	return be16_to_cpu(head->tag);
+}
+
+static inline u8 *tpm_buf_out(struct tpm_buf *buf)
+{
+	return &buf->data[TPM_HEADER_SIZE];
+}
+
+static inline void tpm_buf_append(struct tpm_buf *buf,
+				  const unsigned char *data,
+				  unsigned int len)
+{
+	struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
+
+	BUG_ON((len + tpm_buf_length(buf)) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[tpm_buf_length(buf)], data, len);
+	head->length = cpu_to_be32(tpm_buf_length(buf) + len);
+}
+
+static inline void tpm_buf_store(struct tpm_buf *buf,
+				 unsigned int pos,
+				 const unsigned char *data,
+				 unsigned int len)
+{
+	BUG_ON((pos + len) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[pos], data, len);
+}
+
+static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
+{
+	tpm_buf_append(buf, &value, 1);
+}
+
+static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
+{
+	__be16 value2 = cpu_to_be16(value);
+
+	tpm_buf_append(buf, (u8 *) &value2, 2);
+}
+
+static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
+{
+	__be32 value2 = cpu_to_be32(value);
+
+	tpm_buf_append(buf, (u8 *) &value2, 4);
+}
+
+static inline void tpm_buf_store_u32(struct tpm_buf *buf, unsigned int pos,
+				     const u32 value)
+{
+	__be32 value2 = cpu_to_be32(value);
+
+	tpm_buf_store(buf, pos, (u8 *) &value2, 4);
+}
+
 extern struct class *tpm_class;
 extern dev_t tpm_devt;
 extern const struct file_operations tpm_fops;
drivers/char/tpm/tpm2-cmd.c +113 -208

486 lines not on this page · raw message

Signature · 1 line
2.1.4
maintainer
#1 lore raw

Hi,

Am Mittwoch, 3. Juni 2015, 18:39:14 schrieb Jarkko Sakkinen:

Quoted 29 lines

This patch introduces struct tpm_buf that provides a string buffer for constructing TPM commands. This allows to construct variable sized TPM commands. This feature is needed for TPM 2.0 commands in order to allow policy authentication and algorithmic agility.

The commands in the tpm2-cmd.c have been updated to use struct tpm_buf. Lots of awkward length calculations could be dropped because the buffer knows its length.

The code is is along the lines of the string buffer code in security/trusted/trusted.h.

Changes since v1:

- Fixed potential alignment issues in tpm_buf_tag().

drivers/char/tpm/tpm.h +11 -0
+static inline void tpm_buf_append(struct tpm_buf *buf,
+				  const unsigned char *data,
+				  unsigned int len)
+{
+	struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
+
+	BUG_ON((len + tpm_buf_length(buf)) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[tpm_buf_length(buf)], data, len);
+	head->length = cpu_to_be32(tpm_buf_length(buf) + len);
+}

drivers/char/tpm/tpm.h struct tpm_cmd_t { · line 443

Quoted 10 lines
drivers/char/tpm/tpm.h +10 -0
+
+static inline void tpm_buf_store(struct tpm_buf *buf,
+				 unsigned int pos,
+				 const unsigned char *data,
+				 unsigned int len)
+{
+	BUG_ON((pos + len) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[pos], data, len);
+}

Don't you have to update the ->length here?

Thanks,
Peter

#2 lore raw
Quoted 37 lines · Peter Hüwe

Hi,

Am Mittwoch, 3. Juni 2015, 18:39:14 schrieb Jarkko Sakkinen:

Quoted 33 lines

This patch introduces struct tpm_buf that provides a string buffer for constructing TPM commands. This allows to construct variable sized TPM commands. This feature is needed for TPM 2.0 commands in order to allow policy authentication and algorithmic agility.

The commands in the tpm2-cmd.c have been updated to use struct tpm_buf. Lots of awkward length calculations could be dropped because the buffer knows its length.

The code is is along the lines of the string buffer code in security/trusted/trusted.h.

Changes since v1:

- Fixed potential alignment issues in tpm_buf_tag().

drivers/char/tpm/tpm.h +15 -0
+static inline void tpm_buf_append(struct tpm_buf *buf,
+				  const unsigned char *data,
+				  unsigned int len)
+{
+	struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
+
+	BUG_ON((len + tpm_buf_length(buf)) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[tpm_buf_length(buf)], data, len);
+	head->length = cpu_to_be32(tpm_buf_length(buf) + len);
+}
+
+static inline void tpm_buf_store(struct tpm_buf *buf,
+				 unsigned int pos,
+				 const unsigned char *data,
Quoted 8 lines

drivers/char/tpm/tpm.h struct tpm_cmd_t { · line 443

Quoted 6 lines
drivers/char/tpm/tpm.h +6 -0
+				 unsigned int len)
+{
+	BUG_ON((pos + len) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[pos], data, len);
+}

Don't you have to update the ->length here?

No. Store is for placing value in position, not appending to the end.

Quoted 2 lines

Thanks,
Peter

/Jarkko

maintainer
#3 lore raw

Hi

Quoted 12 lines
Quoted 11 lines

drivers/char/tpm/tpm.h struct tpm_cmd_t { · line 443

Quoted 9 lines
drivers/char/tpm/tpm.h +9 -0
+static inline void tpm_buf_store(struct tpm_buf *buf,
+				 unsigned int pos,
+				 const unsigned char *data,
+				 unsigned int len)
+{
+	BUG_ON((pos + len) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[pos], data, len);
+}

Don't you have to update the ->length here?

Quoted 2 lines

No. Store is for placing value in position, not appending to the end.

Then either add a length check (whether ->length is big enough) and/or call the function "update"

Peter

#4 lore raw
Quoted 16 lines · Peter Huewe

Hi

Quoted 12 lines
Quoted 11 lines

drivers/char/tpm/tpm.h struct tpm_cmd_t { · line 443

Quoted 9 lines
drivers/char/tpm/tpm.h +9 -0
+static inline void tpm_buf_store(struct tpm_buf *buf,
+				 unsigned int pos,
+				 const unsigned char *data,
+				 unsigned int len)
+{
+	BUG_ON((pos + len) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[pos], data, len);
+}

Don't you have to update the ->length here?

Quoted 4 lines
Quoted 2 lines

No. Store is for placing value in position, not appending to the end.

Then either add a length check (whether ->length is big enough) and/or call the function "update"

There is a length check in the beginning (first line of the function body).

Quoted 1 line

Peter

/Jarkko

maintainer
#5 lore raw

Am 9. Juni 2015 13:39:13 MESZ, schrieb Jarkko Sakkinen <[email protected]>:

Quoted 18 lines · Peter Huewe
Quoted 17 lines

Hi

Quoted 13 lines
Quoted 11 lines

drivers/char/tpm/tpm.h struct tpm_cmd_t { · line 443

Quoted 9 lines
drivers/char/tpm/tpm.h +9 -0
+static inline void tpm_buf_store(struct tpm_buf *buf,
+				 unsigned int pos,
+				 const unsigned char *data,
+				 unsigned int len)
+{
+	BUG_ON((pos + len) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[pos], data, len);
+}

Don't you have to update the ->length here?

No. Store is for placing value in position, not appending to the

Quoted 9 lines

end.

Quoted 2 lines
Quoted 1 line

Then either add a length check (whether ->length is big enough)

and/or

Quoted 1 line

call the function "update"

There is a length check in the beginning (first line of the function body).

Nope.
The check in the first line checks whether the write is <= the max buffer size, but not <= head->length.

Since head->length is not updated (as per design) it is possible to write data without effect using this function. This is not what I expect from an API.

Example I create a buffer using tpm_buf_append with 12 bytes, so head->length == 12
Then I use tpm_buf_store at pos 10 and len 4 --> in the buffer are 14 bytes, but tpm_buf_length will only report 12 bytes.

Which is not what I would expect and your current check dies not prevent this.

Peter

Signature · 1 line
Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet.
#6 lore raw
Quoted 39 lines · Peter Huewe

Am 9. Juni 2015 13:39:13 MESZ, schrieb Jarkko Sakkinen <[email protected]>:

Quoted 18 lines · Peter Huewe
Quoted 17 lines

Hi

Quoted 13 lines
Quoted 11 lines

drivers/char/tpm/tpm.h struct tpm_cmd_t { · line 443

Quoted 9 lines
drivers/char/tpm/tpm.h +9 -0
+static inline void tpm_buf_store(struct tpm_buf *buf,
+				 unsigned int pos,
+				 const unsigned char *data,
+				 unsigned int len)
+{
+	BUG_ON((pos + len) > TPM_BUF_SIZE);
+
+	memcpy(&buf->data[pos], data, len);
+}

Don't you have to update the ->length here?

No. Store is for placing value in position, not appending to the

Quoted 9 lines

end.

Quoted 2 lines
Quoted 1 line

Then either add a length check (whether ->length is big enough)

and/or

Quoted 1 line

call the function "update"

There is a length check in the beginning (first line of the function body).

Nope.
The check in the first line checks whether the write is <= the max buffer size, but not <= head->length.

Since head->length is not updated (as per design) it is possible to write data without effect using this function. This is not what I expect from an API.

Quoted 5 lines

Example I create a buffer using tpm_buf_append with 12 bytes, so head->length == 12 Then I use tpm_buf_store at pos 10 and len 4 --> in the buffer are 14 bytes, but tpm_buf_length will only report 12 bytes.

Which is not what I would expect and your current check dies not prevent this.

Aah. Right. A valid point. I'll add this check to the invariant.

/Jarkko