Skip to contentSkip to conversation

tpm: Detach page allocation from tpm_buf

PATCH In review Jarkko Sakkinen 3 participants 16 messages Sep 25, 2019 – Oct 3, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: Detach page allocation from tpm_buf

As has been seen recently, binding the buffer allocation and tpm_buf together is sometimes far from optimal. The buffer might come from the caller namely when tpm_send() is used by another subsystem. In addition we can stability in call sites w/o rollback (e.g. power events)>

Take allocation out of the tpm_buf framework and make it purely a wrapper for the data buffer.

---
 drivers/char/tpm/tpm-sysfs.c      |  19 ++-
 drivers/char/tpm/tpm.h            |  40 ++---
 drivers/char/tpm/tpm1-cmd.c       | 114 +++++++++----
 drivers/char/tpm/tpm2-cmd.c       | 265 +++++++++++++++++++-----------
 drivers/char/tpm/tpm2-space.c     |  64 +++++---
 drivers/char/tpm/tpm_vtpm_proxy.c |  24 +--
 6 files changed, 333 insertions(+), 193 deletions(-)
drivers/char/tpm/tpm-sysfs.c +13 -6
index edfa89160010..eeb90c9225b9 100644
@@ -32,21 +32,26 @@ 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_readpubek_out *out;
-	int i;
-	char *str = buf;
 	struct tpm_chip *chip = to_tpm_chip(dev);
+	struct tpm_readpubek_out *out;
+	struct page *data_page;
+	struct tpm_buf tpm_buf;
 	char anti_replay[20];
+	char *str = buf;
+	int i;
 
 	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))
+	data_page = alloc_page(GFP_HIGHUSER);
+	if (!data_page)
 		goto out_ops;
 
+	tpm_buf_reset(&tpm_buf, kmap(data_page), PAGE_SIZE, 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,
@@ -83,7 +88,9 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
 	}
 
 out_buf:
-	tpm_buf_destroy(&tpm_buf);
+	kunmap(data_page);
+	__free_page(data_page);
+
 out_ops:
 	tpm_put_ops(chip);
 	return str - buf;
drivers/char/tpm/tpm.h +17 -23
index a7fea3e0ca86..45316e5d2d36 100644
@@ -284,36 +284,30 @@ enum tpm_buf_flags {
 };
 
 struct tpm_buf {
-	struct page *data_page;
-	unsigned int flags;
 	u8 *data;
+	unsigned int size;
+	unsigned int flags;
 };
 
-static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
+static inline void tpm_buf_reset(struct tpm_buf *buf, u8 *data,
+	unsigned int size, u16 tag, u32 ordinal)
 {
-	struct tpm_header *head = (struct tpm_header *)buf->data;
+	struct tpm_header *head = (struct tpm_header *)data;
 
-	head->tag = cpu_to_be16(tag);
-	head->length = cpu_to_be32(sizeof(*head));
-	head->ordinal = cpu_to_be32(ordinal);
-}
-
-static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
-{
-	buf->data_page = alloc_page(GFP_HIGHUSER);
-	if (!buf->data_page)
-		return -ENOMEM;
+	/* sanity check */
+	if (size < TPM_HEADER_SIZE) {
+		WARN(1, "tpm_buf: overflow\n");
+		buf->flags |= TPM_BUF_OVERFLOW;
+		return;
+	}
 
+	buf->data = data;
+	buf->size = size;
 	buf->flags = 0;
-	buf->data = kmap(buf->data_page);
-	tpm_buf_reset(buf, tag, ordinal);
-	return 0;
-}
 
-static inline void tpm_buf_destroy(struct tpm_buf *buf)
-{
-	kunmap(buf->data_page);
-	__free_page(buf->data_page);
+	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)
@@ -341,7 +335,7 @@ static inline void tpm_buf_append(struct tpm_buf *buf,
 	if (buf->flags & TPM_BUF_OVERFLOW)
 		return;
 
-	if ((len + new_len) > PAGE_SIZE) {
+	if ((len + new_len) > buf->size) {
 		WARN(1, "tpm_buf: overflow\n");
 		buf->flags |= TPM_BUF_OVERFLOW;
 		return;
drivers/char/tpm/tpm1-cmd.c +78 -36

238 lines not on this page · raw message

drivers/char/tpm/tpm2-cmd.c +173 -92

530 lines not on this page · raw message

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

122 lines not on this page · raw message

drivers/char/tpm/tpm_vtpm_proxy.c +14 -10
index 2f6e087ec496..e6e89be0e149 100644
@@ -394,19 +394,23 @@ 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 proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
+	const struct tpm_header *header;
+	struct page *data_page;
 	struct tpm_buf buf;
 	int rc;
-	const struct tpm_header *header;
-	struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);
+
+	data_page = alloc_page(GFP_HIGHUSER);
+	if (!data_page)
+		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, kmap(data_page), PAGE_SIZE,
+			      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_reset(&buf, kmap(data_page), PAGE_SIZE,
+			      TPM_TAG_RQU_COMMAND, TPM_ORD_SET_LOCALITY);
+
 	tpm_buf_append_u8(&buf, locality);
 
 	proxy_dev->state |= STATE_DRIVER_COMMAND;
@@ -426,8 +430,8 @@ static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality)
 		locality = -1;
 
 out:
-	tpm_buf_destroy(&buf);
-
+	kunmap(data_page);
+	__free_page(data_page);
 	return locality;
 }
 
Signature · 1 line
2.20.1
maintainer
#1 lore raw

On Wed, 2019-09-25 at 16:48 +0300, Jarkko Sakkinen wrote:
[...]

drivers/char/tpm/tpm1-cmd.c struct tpm1_get_random_out { · line 549

Quoted 5 lines
drivers/char/tpm/tpm1-cmd.c +5 -0
+	data_page = alloc_page(GFP_HIGHUSER);
+	if (!data_page)
+		return -ENOMEM;
+
+	data_ptr = kmap(data_page);

I don't think this is such a good idea. On 64 bit it's no different from GFP_KERNEL and on 32 bit where we do have highmem, kmap space is at a premium, so doing a highmem allocation + kmap is more wasteful of resources than simply doing GFP_KERNEL. In general, you should only do GFP_HIGHMEM if the page is going to be mostly used by userspace, which really isn't the case here.

James

#2 lore raw
Quoted 14 lines · James Bottomley

On Wed, 2019-09-25 at 16:48 +0300, Jarkko Sakkinen wrote:
[...]

drivers/char/tpm/tpm1-cmd.c struct tpm1_get_random_out { · line 549

Quoted 5 lines
drivers/char/tpm/tpm1-cmd.c +5 -0
+	data_page = alloc_page(GFP_HIGHUSER);
+	if (!data_page)
+		return -ENOMEM;
+
+	data_ptr = kmap(data_page);

I don't think this is such a good idea. On 64 bit it's no different from GFP_KERNEL and on 32 bit where we do have highmem, kmap space is at a premium, so doing a highmem allocation + kmap is more wasteful of resources than simply doing GFP_KERNEL. In general, you should only do GFP_HIGHMEM if the page is going to be mostly used by userspace, which really isn't the case here.

Changing that in this commit would be wrong even if you are right. After this commit has been applied it is somewhat easier to make best choices for allocation in each call site (probably most will end up using stack).

/Jarkko

maintainer
#3 lore raw
Quoted 16 lines · Jarkko Sakkinen
Quoted 14 lines · James Bottomley

On Wed, 2019-09-25 at 16:48 +0300, Jarkko Sakkinen wrote:
[...]

drivers/char/tpm/tpm1-cmd.c struct tpm1_get_random_out { · line 549

Quoted 5 lines
drivers/char/tpm/tpm1-cmd.c +5 -0
+	data_page = alloc_page(GFP_HIGHUSER);
+	if (!data_page)
+		return -ENOMEM;
+
+	data_ptr = kmap(data_page);

I don't think this is such a good idea. On 64 bit it's no different from GFP_KERNEL and on 32 bit where we do have highmem, kmap space is at a premium, so doing a highmem allocation + kmap is more wasteful of resources than simply doing GFP_KERNEL. In general, you should only do GFP_HIGHMEM if the page is going to be mostly used by userspace, which really isn't the case here.

Quoted 4 lines

Changing that in this commit would be wrong even if you are right. After this commit has been applied it is somewhat easier to make best choices for allocation in each call site (probably most will end up using stack).

Agreed, but it could be a separate patch, prior to this one.  Why duplicate the problem all over only to change it later?

Mimi

#4 lore raw
Quoted 17 lines · Mimi Zohar
Quoted 16 lines · Jarkko Sakkinen
Quoted 14 lines · James Bottomley

On Wed, 2019-09-25 at 16:48 +0300, Jarkko Sakkinen wrote:
[...]

drivers/char/tpm/tpm1-cmd.c struct tpm1_get_random_out { · line 549

Quoted 5 lines
drivers/char/tpm/tpm1-cmd.c +5 -0
+	data_page = alloc_page(GFP_HIGHUSER);
+	if (!data_page)
+		return -ENOMEM;
+
+	data_ptr = kmap(data_page);

I don't think this is such a good idea. On 64 bit it's no different from GFP_KERNEL and on 32 bit where we do have highmem, kmap space is at a premium, so doing a highmem allocation + kmap is more wasteful of resources than simply doing GFP_KERNEL. In general, you should only do GFP_HIGHMEM if the page is going to be mostly used by userspace, which really isn't the case here.

Quoted 7 lines
Quoted 4 lines

Changing that in this commit would be wrong even if you are right. After this commit has been applied it is somewhat easier to make best choices for allocation in each call site (probably most will end up using stack).

Agreed, but it could be a separate patch, prior to this one.  Why duplicate the problem all over only to change it later?

What problem exactly it is duplicating? The existing allocation scheme here works correctly.

/Jarkko

maintainer
#5 lore raw
Quoted 23 lines · Jarkko Sakkinen
Quoted 22 lines · Mimi Zohar
Quoted 20 lines · Jarkko Sakkinen
Quoted 14 lines · James Bottomley

On Wed, 2019-09-25 at 16:48 +0300, Jarkko Sakkinen wrote:
[...]

drivers/char/tpm/tpm1-cmd.c struct tpm1_get_random_out { · line 549

Quoted 5 lines
drivers/char/tpm/tpm1-cmd.c +5 -0
+	data_page = alloc_page(GFP_HIGHUSER);
+	if (!data_page)
+		return -ENOMEM;
+
+	data_ptr = kmap(data_page);

I don't think this is such a good idea. On 64 bit it's no different from GFP_KERNEL and on 32 bit where we do have highmem, kmap space is at a premium, so doing a highmem allocation + kmap is more wasteful of resources than simply doing GFP_KERNEL. In general, you should only do GFP_HIGHMEM if the page is going to be mostly used by userspace, which really isn't the case here.

Changing that in this commit would be wrong even if you are right. After this commit has been applied it is somewhat easier to make best choices for allocation in each call site (probably most will end up using stack).

Quoted 5 lines
Quoted 2 lines

Agreed, but it could be a separate patch, prior to this one.  Why duplicate the problem all over only to change it later?

What problem exactly it is duplicating? The existing allocation scheme here works correctly.

In the current code "alloc_page(GFP_HIGHUSER)" exists in a single function.  With this patch, "alloc_page(GFP_HIGHUSER)" is duplicated 24 times.  If it is incorrect and we shouldn't be using GFP_HIGHUSER, as James said, then why duplicate it 24 times?  Fix it as a separate patch first, that could be backported if needed, and then make the change.

Mimi

#6 lore raw
Quoted 30 lines · Mimi Zohar
Quoted 23 lines · Jarkko Sakkinen
Quoted 22 lines · Mimi Zohar
Quoted 20 lines · Jarkko Sakkinen
Quoted 14 lines · James Bottomley

On Wed, 2019-09-25 at 16:48 +0300, Jarkko Sakkinen wrote:
[...]

drivers/char/tpm/tpm1-cmd.c struct tpm1_get_random_out { · line 549

Quoted 5 lines
drivers/char/tpm/tpm1-cmd.c +5 -0
+	data_page = alloc_page(GFP_HIGHUSER);
+	if (!data_page)
+		return -ENOMEM;
+
+	data_ptr = kmap(data_page);

I don't think this is such a good idea. On 64 bit it's no different from GFP_KERNEL and on 32 bit where we do have highmem, kmap space is at a premium, so doing a highmem allocation + kmap is more wasteful of resources than simply doing GFP_KERNEL. In general, you should only do GFP_HIGHMEM if the page is going to be mostly used by userspace, which really isn't the case here.

Changing that in this commit would be wrong even if you are right. After this commit has been applied it is somewhat easier to make best choices for allocation in each call site (probably most will end up using stack).

Quoted 5 lines
Quoted 2 lines

Agreed, but it could be a separate patch, prior to this one.  Why duplicate the problem all over only to change it later?

What problem exactly it is duplicating? The existing allocation scheme here works correctly.

Quoted 6 lines

In the current code "alloc_page(GFP_HIGHUSER)" exists in a single function.  With this patch, "alloc_page(GFP_HIGHUSER)" is duplicated 24 times.  If it is incorrect and we shouldn't be using GFP_HIGHUSER, as James said, then why duplicate it 24 times?  Fix it as a separate patch first, that could be backported if needed, and then make the change.

Sorry I mixed this with Jerry's proposal :-) I can do that of course.

/Jarkko

#7 lore raw
In reply to Jarkko Sakkinen

drivers/char/tpm/tpm2-cmd.c struct tpm2_get_random_out { · line 336

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm2-cmd.c +2 -1
-		tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
+		tpm_buf_reset(&buf, data_ptr, PAGE_SIZE,
+			      TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_EXTEND);

Oops.

/Jarkko

#8 lore raw
Quoted 6 lines · Jarkko Sakkinen

drivers/char/tpm/tpm2-cmd.c struct tpm2_get_random_out { · line 336

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm2-cmd.c +2 -1
-		tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
+		tpm_buf_reset(&buf, data_ptr, PAGE_SIZE,
+			      TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_EXTEND);

Oops.

Maybe we could use random as the probe for TPM version since we anyway send a TPM command as a probe for TPM version:

1. Try TPM2 get random.
2. If fail, try TPM1 get random.
3. Output random number to klog.

Something like 8 bytes would be sufficient. This would make sure that no new change breaks tpm_get_random() and also this would give some feedback that TPM is at least somewhat working.

/Jarkko

maintainer
#9 lore raw
Quoted 11 lines · Jarkko Sakkinen
Quoted 6 lines · Jarkko Sakkinen

drivers/char/tpm/tpm2-cmd.c struct tpm2_get_random_out { · line 336

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm2-cmd.c +2 -1
-		tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
+		tpm_buf_reset(&buf, data_ptr, PAGE_SIZE,
+			      TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_EXTEND);

Oops.

Maybe we could use random as the probe for TPM version since we anyway send a TPM command as a probe for TPM version:

Quoted 7 lines

1. Try TPM2 get random.
2. If fail, try TPM1 get random.
3. Output random number to klog.

Something like 8 bytes would be sufficient. This would make sure that no new change breaks tpm_get_random() and also this would give some feedback that TPM is at least somewhat working.

That involves sending 2 TPM commands.  At what point does this occur? On registration?  Whenever getting a random number?  Is the result cached in chip->flags?

Will this delay the TPM initialization, causing IMA to go into "TPM bypass mode"?

Mimi

#10 lore raw
Quoted 16 lines · Mimi Zohar
Quoted 15 lines · Jarkko Sakkinen
Quoted 6 lines · Jarkko Sakkinen

drivers/char/tpm/tpm2-cmd.c struct tpm2_get_random_out { · line 336

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm2-cmd.c +2 -1
-		tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
+		tpm_buf_reset(&buf, data_ptr, PAGE_SIZE,
+			      TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_EXTEND);

Oops.

Maybe we could use random as the probe for TPM version since we anyway send a TPM command as a probe for TPM version:

1. Try TPM2 get random.
2. If fail, try TPM1 get random.
3. Output random number to klog.

Quoted 7 lines
Quoted 3 lines

Something like 8 bytes would be sufficient. This would make sure that no new change breaks tpm_get_random() and also this would give some feedback that TPM is at least somewhat working.

That involves sending 2 TPM commands.  At what point does this occur? On registration?  Whenever getting a random number?  Is the result cached in chip->flags?

On registeration. It is just printed to klog.

Quoted 2 lines

Will this delay the TPM initialization, causing IMA to go into "TPM bypass mode"?

Of course it will delay the init.

As I've stated before the real fix for the bypass issue would be to make TPM as part of the core but this has not received much appeal. I think I've sent patch for this once.

/Jarkko

#11 lore raw
Quoted 27 lines · Jarkko Sakkinen
Quoted 16 lines · Mimi Zohar
Quoted 15 lines · Jarkko Sakkinen
Quoted 6 lines · Jarkko Sakkinen

drivers/char/tpm/tpm2-cmd.c struct tpm2_get_random_out { · line 336

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm2-cmd.c +2 -1
-		tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
+		tpm_buf_reset(&buf, data_ptr, PAGE_SIZE,
+			      TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_EXTEND);

Oops.

Maybe we could use random as the probe for TPM version since we anyway send a TPM command as a probe for TPM version:

1. Try TPM2 get random.
2. If fail, try TPM1 get random.
3. Output random number to klog.

Quoted 7 lines
Quoted 3 lines

Something like 8 bytes would be sufficient. This would make sure that no new change breaks tpm_get_random() and also this would give some feedback that TPM is at least somewhat working.

That involves sending 2 TPM commands.  At what point does this occur? On registration?  Whenever getting a random number?  Is the result cached in chip->flags?

On registeration. It is just printed to klog.

Quoted 8 lines
Quoted 2 lines

Will this delay the TPM initialization, causing IMA to go into "TPM bypass mode"?

Of course it will delay the init.

As I've stated before the real fix for the bypass issue would be to make TPM as part of the core but this has not received much appeal. I think I've sent patch for this once.

It has been like that people reject a fix to a race condition and then I get complains on adding minor latency to the init because of the existing race. It is ridicilous, really.

/Jarkko

maintainer
#12 lore raw
Quoted 8 lines · Jarkko Sakkinen
Quoted 8 lines
Quoted 2 lines

Will this delay the TPM initialization, causing IMA to go into "TPM bypass mode"?

Of course it will delay the init.

As I've stated before the real fix for the bypass issue would be to make TPM as part of the core but this has not received much appeal. I think I've sent patch for this once.

IMA initialization is way later than the TPM.  IMA is on the late_initcall(), while the TPM is on the subsys_initcall().  I'm not sure moving the TPM to core would make a difference.  There must be a way of deferring IMA until after the TPM has been initialized.  Any suggestions would be much appreciated.

(The TPM on the Pi still has a dependency on clock.)

Quoted 3 lines

It has been like that people reject a fix to a race condition and then I get complains on adding minor latency to the init because of the existing race. It is ridicilous, really.

I agree, but adding any latency will cause a regression.

Mimi

#13 lore raw
Quoted 11 lines · Mimi Zohar
Quoted 8 lines · Jarkko Sakkinen
Quoted 8 lines
Quoted 2 lines

Will this delay the TPM initialization, causing IMA to go into "TPM bypass mode"?

Of course it will delay the init.

As I've stated before the real fix for the bypass issue would be to make TPM as part of the core but this has not received much appeal. I think I've sent patch for this once.

Quoted 7 lines

IMA initialization is way later than the TPM.  IMA is on the late_initcall(), while the TPM is on the subsys_initcall().  I'm not sure moving the TPM to core would make a difference.  There must be a way of deferring IMA until after the TPM has been initialized.  Any suggestions would be much appreciated.

(The TPM on the Pi still has a dependency on clock.)

Right. I seriously need to study IMA code in near future with time.

Quoted 5 lines
Quoted 3 lines

It has been like that people reject a fix to a race condition and then I get complains on adding minor latency to the init because of the existing race. It is ridicilous, really.

I agree, but adding any latency will cause a regression.

OK, I get the picture here now. I have to some day look at the IMA code and see if I could draft something that would improve the situation. Thanks for explaining all this!

/Jarkko

maintainer
#14 lore raw
In reply to Jarkko Sakkinen
Quoted 21 lines · Jarkko Sakkinen
Quoted 20 lines · Mimi Zohar
Quoted 18 lines · Jarkko Sakkinen
Quoted 6 lines · Jarkko Sakkinen

drivers/char/tpm/tpm2-cmd.c struct tpm2_get_random_out { · line 336

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm2-cmd.c +2 -1
-		tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
+		tpm_buf_reset(&buf, data_ptr, PAGE_SIZE,
+			      TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_EXTEND);

Oops.

Maybe we could use random as the probe for TPM version since we anyway send a TPM command as a probe for TPM version:

1. Try TPM2 get random.
2. If fail, try TPM1 get random.
3. Output random number to klog.

Something like 8 bytes would be sufficient. This would make sure that no new change breaks tpm_get_random() and also this would give some feedback that TPM is at least somewhat working.

Quoted 5 lines
Quoted 3 lines

That involves sending 2 TPM commands.  At what point does this occur? On registration?  Whenever getting a random number?  Is the result cached in chip->flags?

On registeration. It is just printed to klog.

What sets "TPM_CHIP_FLAG_TPM2" in chip->flags?  And when?

Quoted 5 lines
Quoted 3 lines

Will this delay the TPM initialization, causing IMA to go into "TPM bypass mode"?

Of course it will delay the init.

Delaying the init will most likely cause regressions on systems with TPM 1.2 systems.

Instead of sending the TPM 2.0 command and on failure sending the TPM 1.2 version of the command, could chip->flags be tested?  And if not chip->flags, then provide the TPM version as part of registration.

Quoted 4 lines

As I've stated before the real fix for the bypass issue would be to make TPM as part of the core but this has not received much appeal. I think I've sent patch for this once.

I must have missed this discussion.

Mimi

#15 lore raw
Quoted 34 lines · Mimi Zohar
Quoted 21 lines · Jarkko Sakkinen
Quoted 20 lines · Mimi Zohar
Quoted 18 lines · Jarkko Sakkinen
Quoted 6 lines · Jarkko Sakkinen

drivers/char/tpm/tpm2-cmd.c struct tpm2_get_random_out { · line 336

Quoted 3 lines · Jarkko Sakkinen
drivers/char/tpm/tpm2-cmd.c +2 -1
-		tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
+		tpm_buf_reset(&buf, data_ptr, PAGE_SIZE,
+			      TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_EXTEND);

Oops.

Maybe we could use random as the probe for TPM version since we anyway send a TPM command as a probe for TPM version:

1. Try TPM2 get random.
2. If fail, try TPM1 get random.
3. Output random number to klog.

Something like 8 bytes would be sufficient. This would make sure that no new change breaks tpm_get_random() and also this would give some feedback that TPM is at least somewhat working.

Quoted 5 lines
Quoted 3 lines

That involves sending 2 TPM commands.  At what point does this occur? On registration?  Whenever getting a random number?  Is the result cached in chip->flags?

On registeration. It is just printed to klog.

What sets "TPM_CHIP_FLAG_TPM2" in chip->flags?  And when?

Quoted 4 lines
Quoted 3 lines

Will this delay the TPM initialization, causing IMA to go into "TPM bypass mode"?

Quoted 8 lines
Quoted 1 line

Of course it will delay the init.

Delaying the init will most likely cause regressions on systems with TPM 1.2 systems.

Instead of sending the TPM 2.0 command and on failure sending the TPM 1.2 version of the command, could chip->flags be tested?  And if not chip->flags, then provide the TPM version as part of registration.

No rush pushing this forward. I got your point.

Quoted 5 lines
Quoted 3 lines

As I've stated before the real fix for the bypass issue would be to make TPM as part of the core but this has not received much appeal. I think I've sent patch for this once.

I must have missed this discussion.

Yeah, I think that'd be a great idea. We need a better control on TPM core as multiple subsystem's depend on it in API level. Something to reconsider in future.

/Jarkko