Skip to contentSkip to conversation

tpm: introduce struct tpm_buf

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

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 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..f1a1273 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];
+};
+
+static inline void tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
+{
+	struct tpm_input_header *head;
+
+	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)
+{
+	return be16_to_cpu(*(__be16 *) &buf->data[0]);
+}
+
+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,

Quoted 17 lines

Betreff: [PATCH] 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.

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

Quoted 8 lines
drivers/char/tpm/tpm.h +5 -0
@@ -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

Where does 512 come from? What about longer commands? Isn't TPM_BUF_SIZE defined elsewhere as 4096?

Quoted 17 lines
drivers/char/tpm/tpm.h +15 -0
+
+struct tpm_buf {
+ u8 data[TPM_BUF_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);

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

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);

Isn't the updating of the length missing?

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

Quoted 1 line
drivers/char/tpm/tpm.h +1 -0
+}

Thanks,
Peter

#2 lore raw
Quoted 21 lines · Peter Huewe

Hi,

Quoted 20 lines

Betreff: [PATCH] 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.

drivers/char/tpm/tpm.h +0 -0
@@ -382,6 +382,93 @@ struct tpm_cmd_t {
 tpm_cmd_params params;
 } __packed;
 
Quoted 6 lines

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

Quoted 5 lines
drivers/char/tpm/tpm.h +5 -0
+/* A string buffer type for constructing TPM commands. This is based on the
+ * code in security/keys/trusted.h.
+ */
+
+#define TPM_BUF_SIZE 512

Where does 512 come from? What about longer commands? Isn't TPM_BUF_SIZE defined elsewhere as 4096?

No matter which algorithm we use for trusted keys, 512 bytes is enough for command and response and trusted keys is the most demanding use case for TPM at the moment.

I chose this buffer size because I can still get away without using heap, which is nice for some use cases that should be resistant to failure (suspend/resume mainly).

This was the buffer size also used in security/keys/trusted.c that contains TPM 1.x based trusted keys (that I hope will migrate to TPM driver soon).

However, if there is rationale to use a larger buffer size and heap, I'm welcome for counter opinions.

Quoted 28 lines
Quoted 17 lines
drivers/char/tpm/tpm.h +15 -0
+
+struct tpm_buf {
+ u8 data[TPM_BUF_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);

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

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);

Isn't the updating of the length missing?

Quoted 4 lines

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

Quoted 1 line
drivers/char/tpm/tpm.h +1 -0
+}

Thanks,
Peter

/Jarkko

#3 lore raw
In reply to Jarkko Sakkinen

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

Quoted 8 lines · Jarkko Sakkinen
drivers/char/tpm/tpm.h +8 -0
+/* 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];

This should be u32 or u64 to guarentee correct alignment for the casting.

Quoted 13 lines
drivers/char/tpm/tpm.h +13 -0
+};
+
+static inline void tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
+{
+	struct tpm_input_header *head;
+
+	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);
+}
+

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

Quoted 10 lines
drivers/char/tpm/tpm.h +10 -0
+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)
+{
+	return be16_to_cpu(*(__be16 *) &buf->data[0]);

be16_to_cpup ?

Any thought on someday using this for tpm1 as well?

Jason

#4 lore raw
Quoted 11 lines · Jason Gunthorpe

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

Quoted 8 lines · Jarkko Sakkinen
drivers/char/tpm/tpm.h +8 -0
+/* 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];

This should be u32 or u64 to guarentee correct alignment for the casting.

Good catch. The functions where this might cause trouble are *_length() and *_tag().

In other places misalignment should not cause any regressions since data is not directly assigned to the buffer with pointer casting.

I would prefer to fix by changing *_length() and *_tag() to copy the value to a local variable and return that. It's a fail safe way and here the performance is not an issue.

Quoted 17 lines
Quoted 17 lines
drivers/char/tpm/tpm.h +17 -0
+};
+
+static inline void tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
+{
+	struct tpm_input_header *head;
+
+	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;
+
Quoted 8 lines

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

Quoted 6 lines
drivers/char/tpm/tpm.h +6 -0
+	return be32_to_cpu(head->length);
+}
+
+static inline u16 tpm_buf_tag(struct tpm_buf *buf)
+{
+	return be16_to_cpu(*(__be16 *) &buf->data[0]);

be16_to_cpup ?

Thanks, I'll change this.

Quoted 1 line

Any thought on someday using this for tpm1 as well?

Yes, I think this could form the baseline so that the sealing code that currently resides is security/keys/trusted.c could be eventually moved to drivers/char/tpm.

I will be contributing API to include/linux/tpm.h that can be then called by trusted keys. I will also contribute the implementation for TPM 2.0 sealing but it is up to those who implemented TPM 1.x sealing code to do the migration. I put all the enablers in place so that doing that transition should be relatively painless.

I have PoC for TPM 2.0 trusted keys written in Python available here:

https://github.com/jsakkine/tpm2-scripts/blob/master/tpm2.py

(In kernel version the default hash algorithm should be probably SHA-256 as SHA-1 is not considered secure by NIST anymore)

Doing also all the TPM 1.x migration on top of this would make oversubscribed.

Quoted 1 line

Jason

/Jarkko

#5 lore raw
Quoted 16 lines · Jarkko Sakkinen
Quoted 11 lines · Jason Gunthorpe

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

Quoted 8 lines · Jarkko Sakkinen
drivers/char/tpm/tpm.h +8 -0
+/* 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];

This should be u32 or u64 to guarentee correct alignment for the casting.

Good catch. The functions where this might cause trouble are *_length() and *_tag().

Quoted 6 lines

In other places misalignment should not cause any regressions since data is not directly assigned to the buffer with pointer casting.

I would prefer to fix by changing *_length() and *_tag() to copy the value to a local variable and return that. It's a fail safe way and here the performance is not an issue.

I would change the type, that is very simple and will improve performance of the memcpy as well.

It actually isn't a problem for the casts to tpm_input_header - any structure marked __packed will cause the compiler to assume that the entire structure is unaligned and code gen accordingly, so the cast will always work, but on x86 it will be more efficient if the array is aligned.

Ideally we could change to something like:
__attribute__((packed,aligned(4)))

Instead of __packed which will help the compiler minimize unaligned load instructions..

Jason

#6 lore raw
Quoted 33 lines · Jason Gunthorpe
Quoted 16 lines · Jarkko Sakkinen
Quoted 11 lines · Jason Gunthorpe

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

Quoted 8 lines · Jarkko Sakkinen
drivers/char/tpm/tpm.h +8 -0
+/* 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];

This should be u32 or u64 to guarentee correct alignment for the casting.

Good catch. The functions where this might cause trouble are *_length() and *_tag().

Quoted 6 lines

In other places misalignment should not cause any regressions since data is not directly assigned to the buffer with pointer casting.

I would prefer to fix by changing *_length() and *_tag() to copy the value to a local variable and return that. It's a fail safe way and here the performance is not an issue.

I would change the type, that is very simple and will improve performance of the memcpy as well.

It actually isn't a problem for the casts to tpm_input_header - any structure marked __packed will cause the compiler to assume that the entire structure is unaligned and code gen accordingly, so the cast will always work, but on x86 it will be more efficient if the array is aligned.

Quoted 5 lines

Ideally we could change to something like:
__attribute__((packed,aligned(4)))

Instead of __packed which will help the compiler minimize unaligned load instructions..

I realized basically the same what you said here when I refined the patch (read this email after sending v2) :) I decided that align to 8 bytes.

I don't think the packed attribute is needed here because now I cast the beginning to tpm_input_header, which is a packed struct.

Quoted 1 line

Jason

/Jarkko

#7 lore raw
Quoted 3 lines · Jarkko Sakkinen

I realized basically the same what you said here when I refined the patch (read this email after sending v2) :) I decided that align to 8 bytes.

Don't use the __attribute__((align)), use just a u64, the effect is identical, and the latter doesn't rely on compiler specific features.

Quoted 2 lines

I don't think the packed attribute is needed here because now I cast the beginning to tpm_input_header, which is a packed struct.

I ment the attributs could be changed on the tpm_input_header, the buf should just be a u64 array..

Jason

#8 lore raw
Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines · Jarkko Sakkinen

I realized basically the same what you said here when I refined the patch (read this email after sending v2) :) I decided that align to 8 bytes.

Don't use the __attribute__((align)), use just a u64, the effect is identical, and the latter doesn't rely on compiler specific features.

Byte array is otherwise more convenient and this feature is widely used:

$ git grep "__attribute__((aligned" | wc -l
222

I don't see your point here.

Quoted 7 lines
Quoted 2 lines

I don't think the packed attribute is needed here because now I cast the beginning to tpm_input_header, which is a packed struct.

I ment the attributs could be changed on the tpm_input_header, the buf should just be a u64 array..

Jason

/Jarkko