Skip to contentSkip to conversation

add sysfs exports for TPM 2 PCR registers

PATCH Superseded James Bottomley 3 participants 9 messages Jul 21, 2020 – Jul 22, 2020

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

James Bottomley

author maintainer
#0 lore raw

v2 cover add sysfs exports for TPM 2 PCR registers

v2 to fix the failure on TPM 1.2

At last year's plumbers conference it was agreed in principle to export TPM 2 PCRs via sysfs. We also agreed we should conform to sysfs rules of one value per file, which rules out the "pcrs" file format of TPM 1.2 which has every PCR value in the same file.

I added these files using device groups, so one group per bank hash of the TPM. Using an emulator which supports a variety of hashes, you can see the structure of the group files:

root@testdeb:~# ls -F /sys/class/tpm/tpm0/
dev      pcr-sha1/    pcr-sha384/  power/      tpm_version_major
device@  pcr-sha256/  pcr-sha512/  subsystem@  uevent

As a future enhancement, we could use the group is_visible function to remove files corresponding to PCRs which don't exist. The reason this isn't present is that so far I've never seen a TPM with a missing PCR.

James

---

James Bottomley (1):
tpm: add sysfs exports for all banks of PCR registers

 drivers/char/tpm/tpm-sysfs.c | 180 +++++++++++++++++++++++++++++++++++
 include/linux/tpm.h          |   8 +-
 2 files changed, 187 insertions(+), 1 deletion(-)
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH v2 1/1 tpm: add sysfs exports for all banks of PCR registers

use macro magic to create sysfs per hash groups with 24 PCR files in them one for each possible agile hash of the TPM. The files are plugged in to a read function which is TPM version agnostic, so this works also for TPM 1.2 although the hash is only sha1 in that case. For every hash the TPM supports, a group named pcr-<hash> is created and each of the PCR read files placed under it.

---

v2: fix TPM 1.2 legacy links failure

---
 drivers/char/tpm/tpm-sysfs.c | 180 +++++++++++++++++++++++++++++++++++
 include/linux/tpm.h          |   8 +-
 2 files changed, 187 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm-sysfs.c +180 -0
index d52bf4df0bca..6c4e984d6c8d 100644
@@ -348,11 +348,191 @@ static const struct attribute_group tpm2_dev_group = {
 	.attrs = tpm2_dev_attrs,
 };
 
+struct tpm_pcr_attr {
+	int alg_id;
+	int pcr;
+	struct device_attribute attr;
+};
+
+#define to_tpm_pcr_attr(a) container_of(a, struct tpm_pcr_attr, attr)
+
+static ssize_t pcr_value_show(struct device *dev,
+			      struct device_attribute *attr,
+			      char *buf)
+{
+	struct tpm_pcr_attr *ha = to_tpm_pcr_attr(attr);
+	struct tpm_chip *chip = to_tpm_chip(dev);
+	struct tpm_digest digest;
+	int i;
+	int digest_size = 0;
+	int rc;
+	char *str = buf;
+
+	for (i = 0; i < chip->nr_allocated_banks; i++)
+		if (ha->alg_id == chip->allocated_banks[i].alg_id)
+			digest_size = chip->allocated_banks[i].digest_size;
+	/* should never happen */
+	if (!digest_size)
+		return -EINVAL;
+
+	digest.alg_id = ha->alg_id;
+	rc = tpm_pcr_read(chip, ha->pcr, &digest);
+	if (rc)
+		return rc;
+	for (i = 0; i < digest_size; i++)
+		str += sprintf(str, "%02X", digest.digest[i]);
+	str += sprintf(str, "\n");
+
+	return str - buf;
+}
+
+/*
+ * The following set of defines represents all the magic to build
+ * the per hash attribute groups for displaying each bank of PCRs.
+ * The only slight problem with this approach is that every PCR is
+ * hard coded to be present, so you don't know if an PCR is missing
+ * until a cat of the file returns -EINVAL
+ *
+ * Also note you must ignore checkpatch warnings in this macro
+ * code. This is deep macro magic that checkpatch.pl doesn't
+ * understand.
+ */
+
+/* Note, this must match TPM2_PLATFORM_PCR which is fixed at 24. */
+#define _TPM_HELPER(_alg, _hash, F) \
+	F(_alg, _hash, 0)	    \
+	F(_alg, _hash, 1)	    \
+	F(_alg, _hash, 2)	    \
+	F(_alg, _hash, 3)	    \
+	F(_alg, _hash, 4)	    \
+	F(_alg, _hash, 5)	    \
+	F(_alg, _hash, 6)	    \
+	F(_alg, _hash, 7)	    \
+	F(_alg, _hash, 8)	    \
+	F(_alg, _hash, 9)	    \
+	F(_alg, _hash, 10)	    \
+	F(_alg, _hash, 11)	    \
+	F(_alg, _hash, 12)	    \
+	F(_alg, _hash, 13)	    \
+	F(_alg, _hash, 14)	    \
+	F(_alg, _hash, 15)	    \
+	F(_alg, _hash, 16)	    \
+	F(_alg, _hash, 17)	    \
+	F(_alg, _hash, 18)	    \
+	F(_alg, _hash, 19)	    \
+	F(_alg, _hash, 20)	    \
+	F(_alg, _hash, 21)	    \
+	F(_alg, _hash, 22)	    \
+	F(_alg, _hash, 23)
+
+/* ignore checkpatch warning about trailing ; in macro. */
+#define PCR_ATTR(_alg, _hash, _pcr)				   \
+	static struct tpm_pcr_attr dev_attr_pcr_##_hash##_##_pcr = {	\
+		.alg_id = _alg,					   \
+		.pcr = _pcr,					   \
+		.attr = {					   \
+			.attr = {				   \
+				.name = __stringify(_pcr),	   \
+				.mode = 0444			   \
+			},					   \
+			.show = pcr_value_show			   \
+		}						   \
+	};
+
+#define PCR_ATTRS(_alg, _hash)			\
+	_TPM_HELPER(_alg, _hash, PCR_ATTR)
+
+/* ignore checkpatch warning about trailing , in macro. */
+#define PCR_ATTR_VAL(_alg, _hash, _pcr)		\
+	&dev_attr_pcr_##_hash##_##_pcr.attr.attr,
+
+#define PCR_ATTR_GROUP_ARRAY(_alg, _hash)		       \
+	static struct attribute *pcr_group_attrs_##_hash[] = { \
+		_TPM_HELPER(_alg, _hash, PCR_ATTR_VAL)	       \
+		NULL					       \
+	}
+
+#define PCR_ATTR_GROUP(_alg, _hash)			    \
+	static struct attribute_group pcr_group_##_hash = { \
+		.name = "pcr-" __stringify(_hash),	    \
+		.attrs = pcr_group_attrs_##_hash	    \
+	}
+
+#define PCR_ATTR_BUILD(_alg, _hash)	   \
+	PCR_ATTRS(_alg, _hash)		   \
+	PCR_ATTR_GROUP_ARRAY(_alg, _hash); \
+	PCR_ATTR_GROUP(_alg, _hash)
+/*
+ * End of macro structure to build an attribute group containing 24
+ * PCR value files for each supported hash algorithm
+ */
+
+/*
+ * The next set of macros implements the cleverness for each hash to
+ * build a static attribute group called pcr_group_<hash> which can be
+ * added to chip->groups[].
+ *
+ * The first argument is the TPM algorithm id and the second is the
+ * hash used as both the suffix and the group name.  Note: the group
+ * name is a directory in the top level tpm class with the name
+ * pcr-<hash>, so it must not clash with any other names already
+ * in the sysfs directory.
+ */
+PCR_ATTR_BUILD(TPM_ALG_SHA1, sha1);
+PCR_ATTR_BUILD(TPM_ALG_SHA256, sha256);
+PCR_ATTR_BUILD(TPM_ALG_SHA384, sha384);
+PCR_ATTR_BUILD(TPM_ALG_SHA512, sha512);
+PCR_ATTR_BUILD(TPM_ALG_SM3_256, sm3);
+
+
 void tpm_sysfs_add_device(struct tpm_chip *chip)
 {
+	int i;
+
 	WARN_ON(chip->groups_cnt != 0);
+
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
 	else
 		chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
+
+	/* add one group for each bank hash */
+	for (i = 0; i < chip->nr_allocated_banks; i++) {
+		switch (chip->allocated_banks[i].alg_id) {
+		case TPM_ALG_SHA1:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sha1;
+			break;
+		case TPM_ALG_SHA256:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sha256;
+			break;
+		case TPM_ALG_SHA384:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sha384;
+			break;
+		case TPM_ALG_SHA512:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sha512;
+			break;
+		case TPM_ALG_SM3_256:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sm3;
+			break;
+		default:
+			/*
+			 * If this warning triggers, send a patch to
+			 * add both a PCR_ATTR_BUILD() macro above for
+			 * the missing algorithm as well as an
+			 * additional case in this switch statement.
+			 */
+			WARN(1, "TPM with unsupported bank algorthm 0x%04x",
+			     chip->allocated_banks[i].alg_id);
+			break;
+		}
+	}
+
+	/*
+	 * This will only trigger if someone has added an additional
+	 * hash to the tpm_algorithms enum without incrementing
+	 * TPM_MAX_HASHES.  This has to be a BUG_ON because under this
+	 * condition, the chip->groups array will overflow corrupting
+	 * the chips structure.
+	 */
+	BUG_ON(chip->groups_cnt > TPM_MAX_HASHES);
 }
include/linux/tpm.h +7 -1
index 5026a06977e1..f73dc4f9fbf4 100644
@@ -42,6 +42,12 @@ enum tpm_algorithms {
 	TPM_ALG_SM3_256		= 0x0012,
 };
 
+/*
+ * maximum number of hashing algorithms a TPM can have.  This is
+ * basically a count of every hash in tpm_algorithms above
+ */
+#define TPM_MAX_HASHES	5
+
 struct tpm_digest {
 	u16 alg_id;
 	u8 digest[TPM_MAX_DIGEST_SIZE];
@@ -145,7 +151,7 @@ struct tpm_chip {
 
 	struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
 
-	const struct attribute_group *groups[3];
+	const struct attribute_group *groups[3 + TPM_MAX_HASHES];
 	unsigned int groups_cnt;
 
 	u32 nr_allocated_banks;
Signature · 1 line
2.26.2
maintainer
#2 lore raw

Re: [PATCH v2 1/1] tpm: add sysfs exports for all banks of PCR registers

Quoted 7 lines · James Bottomley

use macro magic to create sysfs per hash groups with 24 PCR files in them one for each possible agile hash of the TPM. The files are plugged in to a read function which is TPM version agnostic, so this works also for TPM 1.2 although the hash is only sha1 in that case. For every hash the TPM supports, a group named pcr-<hash> is created and each of the PCR read files placed under it.

Quoted 5 lines

---

v2: fix TPM 1.2 legacy links failure

Thanks, this version works with TPM 1.2.

Mimi

#3 lore raw

Re: [PATCH v2 1/1] tpm: add sysfs exports for all banks of PCR registers

In reply to James Bottomley

James Bottomley @ 2020-07-21 08:56 MST:

Quoted 202 lines

use macro magic to create sysfs per hash groups with 24 PCR files in them one for each possible agile hash of the TPM. The files are plugged in to a read function which is TPM version agnostic, so this works also for TPM 1.2 although the hash is only sha1 in that case. For every hash the TPM supports, a group named pcr-<hash> is created and each of the PCR read files placed under it.

---

v2: fix TPM 1.2 legacy links failure

---
 drivers/char/tpm/tpm-sysfs.c | 180 +++++++++++++++++++++++++++++++++++
 include/linux/tpm.h          |   8 +-
 2 files changed, 187 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm-sysfs.c +170 -0
index d52bf4df0bca..6c4e984d6c8d 100644
@@ -348,11 +348,191 @@ static const struct attribute_group tpm2_dev_group = {
 	.attrs = tpm2_dev_attrs,
 };
 
+struct tpm_pcr_attr {
+	int alg_id;
+	int pcr;
+	struct device_attribute attr;
+};
+
+#define to_tpm_pcr_attr(a) container_of(a, struct tpm_pcr_attr, attr)
+
+static ssize_t pcr_value_show(struct device *dev,
+			      struct device_attribute *attr,
+			      char *buf)
+{
+	struct tpm_pcr_attr *ha = to_tpm_pcr_attr(attr);
+	struct tpm_chip *chip = to_tpm_chip(dev);
+	struct tpm_digest digest;
+	int i;
+	int digest_size = 0;
+	int rc;
+	char *str = buf;
+
+	for (i = 0; i < chip->nr_allocated_banks; i++)
+		if (ha->alg_id == chip->allocated_banks[i].alg_id)
+			digest_size = chip->allocated_banks[i].digest_size;
+	/* should never happen */
+	if (!digest_size)
+		return -EINVAL;
+
+	digest.alg_id = ha->alg_id;
+	rc = tpm_pcr_read(chip, ha->pcr, &digest);
+	if (rc)
+		return rc;
+	for (i = 0; i < digest_size; i++)
+		str += sprintf(str, "%02X", digest.digest[i]);
+	str += sprintf(str, "\n");
+
+	return str - buf;
+}
+
+/*
+ * The following set of defines represents all the magic to build
+ * the per hash attribute groups for displaying each bank of PCRs.
+ * The only slight problem with this approach is that every PCR is
+ * hard coded to be present, so you don't know if an PCR is missing
+ * until a cat of the file returns -EINVAL
+ *
+ * Also note you must ignore checkpatch warnings in this macro
+ * code. This is deep macro magic that checkpatch.pl doesn't
+ * understand.
+ */
+
+/* Note, this must match TPM2_PLATFORM_PCR which is fixed at 24. */
+#define _TPM_HELPER(_alg, _hash, F) \
+	F(_alg, _hash, 0)	    \
+	F(_alg, _hash, 1)	    \
+	F(_alg, _hash, 2)	    \
+	F(_alg, _hash, 3)	    \
+	F(_alg, _hash, 4)	    \
+	F(_alg, _hash, 5)	    \
+	F(_alg, _hash, 6)	    \
+	F(_alg, _hash, 7)	    \
+	F(_alg, _hash, 8)	    \
+	F(_alg, _hash, 9)	    \
+	F(_alg, _hash, 10)	    \
+	F(_alg, _hash, 11)	    \
+	F(_alg, _hash, 12)	    \
+	F(_alg, _hash, 13)	    \
+	F(_alg, _hash, 14)	    \
+	F(_alg, _hash, 15)	    \
+	F(_alg, _hash, 16)	    \
+	F(_alg, _hash, 17)	    \
+	F(_alg, _hash, 18)	    \
+	F(_alg, _hash, 19)	    \
+	F(_alg, _hash, 20)	    \
+	F(_alg, _hash, 21)	    \
+	F(_alg, _hash, 22)	    \
+	F(_alg, _hash, 23)
+
+/* ignore checkpatch warning about trailing ; in macro. */
+#define PCR_ATTR(_alg, _hash, _pcr)				   \
+	static struct tpm_pcr_attr dev_attr_pcr_##_hash##_##_pcr = {	\
+		.alg_id = _alg,					   \
+		.pcr = _pcr,					   \
+		.attr = {					   \
+			.attr = {				   \
+				.name = __stringify(_pcr),	   \
+				.mode = 0444			   \
+			},					   \
+			.show = pcr_value_show			   \
+		}						   \
+	};
+
+#define PCR_ATTRS(_alg, _hash)			\
+	_TPM_HELPER(_alg, _hash, PCR_ATTR)
+
+/* ignore checkpatch warning about trailing , in macro. */
+#define PCR_ATTR_VAL(_alg, _hash, _pcr)		\
+	&dev_attr_pcr_##_hash##_##_pcr.attr.attr,
+
+#define PCR_ATTR_GROUP_ARRAY(_alg, _hash)		       \
+	static struct attribute *pcr_group_attrs_##_hash[] = { \
+		_TPM_HELPER(_alg, _hash, PCR_ATTR_VAL)	       \
+		NULL					       \
+	}
+
+#define PCR_ATTR_GROUP(_alg, _hash)			    \
+	static struct attribute_group pcr_group_##_hash = { \
+		.name = "pcr-" __stringify(_hash),	    \
+		.attrs = pcr_group_attrs_##_hash	    \
+	}
+
+#define PCR_ATTR_BUILD(_alg, _hash)	   \
+	PCR_ATTRS(_alg, _hash)		   \
+	PCR_ATTR_GROUP_ARRAY(_alg, _hash); \
+	PCR_ATTR_GROUP(_alg, _hash)
+/*
+ * End of macro structure to build an attribute group containing 24
+ * PCR value files for each supported hash algorithm
+ */
+
+/*
+ * The next set of macros implements the cleverness for each hash to
+ * build a static attribute group called pcr_group_<hash> which can be
+ * added to chip->groups[].
+ *
+ * The first argument is the TPM algorithm id and the second is the
+ * hash used as both the suffix and the group name.  Note: the group
+ * name is a directory in the top level tpm class with the name
+ * pcr-<hash>, so it must not clash with any other names already
+ * in the sysfs directory.
+ */
+PCR_ATTR_BUILD(TPM_ALG_SHA1, sha1);
+PCR_ATTR_BUILD(TPM_ALG_SHA256, sha256);
+PCR_ATTR_BUILD(TPM_ALG_SHA384, sha384);
+PCR_ATTR_BUILD(TPM_ALG_SHA512, sha512);
+PCR_ATTR_BUILD(TPM_ALG_SM3_256, sm3);
+
+
 void tpm_sysfs_add_device(struct tpm_chip *chip)
 {
+	int i;
+
 	WARN_ON(chip->groups_cnt != 0);
+
 	if (chip->flags & TPM_CHIP_FLAG_TPM2)
 		chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
 	else
 		chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
+
+	/* add one group for each bank hash */
+	for (i = 0; i < chip->nr_allocated_banks; i++) {
+		switch (chip->allocated_banks[i].alg_id) {
+		case TPM_ALG_SHA1:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sha1;
+			break;
+		case TPM_ALG_SHA256:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sha256;
+			break;
+		case TPM_ALG_SHA384:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sha384;
+			break;
+		case TPM_ALG_SHA512:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sha512;
+			break;
+		case TPM_ALG_SM3_256:
+			chip->groups[chip->groups_cnt++] = &pcr_group_sm3;
+			break;
+		default:
+			/*
+			 * If this warning triggers, send a patch to
+			 * add both a PCR_ATTR_BUILD() macro above for
+			 * the missing algorithm as well as an
+			 * additional case in this switch statement.
+			 */
+			WARN(1, "TPM with unsupported bank algorthm 0x%04x",
+			     chip->allocated_banks[i].alg_id);
+			break;
+		}

drivers/char/tpm/tpm-sysfs.c static const struct attribute_group tpm2_dev_group = { · line 537

Quoted 10 lines
drivers/char/tpm/tpm-sysfs.c +10 -0
+	}
+
+	/*
+	 * This will only trigger if someone has added an additional
+	 * hash to the tpm_algorithms enum without incrementing
+	 * TPM_MAX_HASHES.  This has to be a BUG_ON because under this
+	 * condition, the chip->groups array will overflow corrupting
+	 * the chips structure.
+	 */
+	BUG_ON(chip->groups_cnt > TPM_MAX_HASHES);

Should this check be 3 + TPM_MAX_HASHES like below?

Quoted 27 lines
drivers/char/tpm/tpm-sysfs.c +0 -0
 }
include/linux/tpm.h +7 -1
index 5026a06977e1..f73dc4f9fbf4 100644
@@ -42,6 +42,12 @@ enum tpm_algorithms {
 	TPM_ALG_SM3_256		= 0x0012,
 };
 
+/*
+ * maximum number of hashing algorithms a TPM can have.  This is
+ * basically a count of every hash in tpm_algorithms above
+ */
+#define TPM_MAX_HASHES	5
+
 struct tpm_digest {
 	u16 alg_id;
 	u8 digest[TPM_MAX_DIGEST_SIZE];
@@ -145,7 +151,7 @@ struct tpm_chip {
 
 	struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
 
-	const struct attribute_group *groups[3];
+	const struct attribute_group *groups[3 + TPM_MAX_HASHES];
 	unsigned int groups_cnt;
 
 	u32 nr_allocated_banks;
author maintainer
#4 lore raw

Re: [PATCH v2 1/1] tpm: add sysfs exports for all banks of PCR registers

Quoted 1 line · Jerry Snitselaar

James Bottomley @ 2020-07-21 08:56 MST:

[...]

Quoted 5 lines
Quoted 5 lines
(quoted diff) +2 -0
+	/*
+	 * This will only trigger if someone has added an

additional

(quoted diff) +2 -0
+	 * hash to the tpm_algorithms enum without incrementing
+	 * TPM_MAX_HASHES.  This has to be a BUG_ON because under
Quoted 8 lines

drivers/char/tpm/tpm-sysfs.c static const struct attribute_group tpm2_dev_group = { · line 537

Quoted 6 lines

this

(quoted diff) +1 -0
+	 * condition, the chip->groups array will overflow

corrupting

drivers/char/tpm/tpm-sysfs.c +3 -0
+	 * the chips structure.
+	 */
+	BUG_ON(chip->groups_cnt > TPM_MAX_HASHES);

Should this check be 3 + TPM_MAX_HASHES like below?

No, because at this point only a single additional group has been addedin addition to the hashes groups. The first line of tpm_sysfs_add_device is

	WARN_ON(chip->groups_cnt != 0);

And then we add the unnamed group. This loop over the banks follows it, so chip->groups_cnt should be nr_banks_allocated by the end (it's the index, which is one fewer than the number of entries in chip-

Quoted 1 line

groups[]). We have a problem if nr_banks_allocated > TPM_MAX_HASHES

which is what the BUG_ON checks.

James

#5 lore raw

Re: [PATCH v2 1/1] tpm: add sysfs exports for all banks of PCR registers

James Bottomley @ 2020-07-21 16:37 MST:

Quoted 23 lines · Jerry Snitselaar
Quoted 1 line

James Bottomley @ 2020-07-21 08:56 MST:

[...]

Quoted 5 lines
Quoted 5 lines
(quoted diff) +2 -0
+	/*
+	 * This will only trigger if someone has added an

additional

(quoted diff) +2 -0
+	 * hash to the tpm_algorithms enum without incrementing
+	 * TPM_MAX_HASHES.  This has to be a BUG_ON because under
Quoted 8 lines

drivers/char/tpm/tpm-sysfs.c static const struct attribute_group tpm2_dev_group = { · line 537

Quoted 6 lines

this

(quoted diff) +1 -0
+	 * condition, the chip->groups array will overflow

corrupting

drivers/char/tpm/tpm-sysfs.c +3 -0
+	 * the chips structure.
+	 */
+	BUG_ON(chip->groups_cnt > TPM_MAX_HASHES);

Should this check be 3 + TPM_MAX_HASHES like below?

No, because at this point only a single additional group has been addedin addition to the hashes groups. The first line of tpm_sysfs_add_device is

	WARN_ON(chip->groups_cnt != 0);
Quoted 7 lines

And then we add the unnamed group. This loop over the banks follows it, so chip->groups_cnt should be nr_banks_allocated by the end (it's the index, which is one fewer than the number of entries in chip-

Quoted 1 line

groups[]). We have a problem if nr_banks_allocated > TPM_MAX_HASHES

which is what the BUG_ON checks.

James

If the chip supported all 5 listed cases wouldn't groups_cnt be 6 at this point?

author maintainer
#6 lore raw

Re: [PATCH v2 1/1] tpm: add sysfs exports for all banks of PCR registers

Quoted 34 lines · Jerry Snitselaar

James Bottomley @ 2020-07-21 16:37 MST:

Quoted 32 lines · Jerry Snitselaar
Quoted 1 line

James Bottomley @ 2020-07-21 08:56 MST:

[...]

Quoted 7 lines
Quoted 7 lines
(quoted diff) +2 -0
+	/*
+	 * This will only trigger if someone has added an

additional

(quoted diff) +1 -0
+	 * hash to the tpm_algorithms enum without

incrementing

(quoted diff) +1 -0
+	 * TPM_MAX_HASHES.  This has to be a BUG_ON because

under

Quoted 8 lines

drivers/char/tpm/tpm-sysfs.c static const struct attribute_group tpm2_dev_group = { · line 537

Quoted 6 lines

this

(quoted diff) +1 -0
+	 * condition, the chip->groups array will overflow

corrupting

drivers/char/tpm/tpm-sysfs.c +3 -0
+	 * the chips structure.
+	 */
+	BUG_ON(chip->groups_cnt > TPM_MAX_HASHES);

Should this check be 3 + TPM_MAX_HASHES like below?

No, because at this point only a single additional group has been addedin addition to the hashes groups. The first line of tpm_sysfs_add_device is

	WARN_ON(chip->groups_cnt != 0);

And then we add the unnamed group. This loop over the banks follows it, so chip->groups_cnt should be nr_banks_allocated by the end (it's the index, which is one fewer than the number of entries in chip->groups[]). We have a problem if nr_banks_allocated > TPM_MAX_HASHES

Quoted 6 lines
Quoted 3 lines

which is what the BUG_ON checks.

James

If the chip supported all 5 listed cases wouldn't groups_cnt be 6 at this point?

Actually, yes, I think it would be because it's pointing at the next free index not the current one. So it should be BUG_ON (chip-

Quoted 1 line

groups_cnt > TPM_MAX_HASHES + 1)

James

Reviewed-by
#7 lore raw

Re: [PATCH v2 1/1] tpm: add sysfs exports for all banks of PCR registers

James Bottomley @ 2020-07-21 17:39 MST:

Quoted 39 lines · Jerry Snitselaar
Quoted 38 lines

James Bottomley @ 2020-07-21 16:37 MST:

Quoted 35 lines · Jerry Snitselaar
Quoted 1 line

James Bottomley @ 2020-07-21 08:56 MST:

[...]

Quoted 7 lines
Quoted 7 lines
(quoted diff) +2 -0
+	/*
+	 * This will only trigger if someone has added an

additional

(quoted diff) +1 -0
+	 * hash to the tpm_algorithms enum without

incrementing

(quoted diff) +1 -0
+	 * TPM_MAX_HASHES.  This has to be a BUG_ON because

under

Quoted 8 lines

drivers/char/tpm/tpm-sysfs.c static const struct attribute_group tpm2_dev_group = { · line 537

Quoted 6 lines

this

(quoted diff) +1 -0
+	 * condition, the chip->groups array will overflow

corrupting

drivers/char/tpm/tpm-sysfs.c +3 -0
+	 * the chips structure.
+	 */
+	BUG_ON(chip->groups_cnt > TPM_MAX_HASHES);

Should this check be 3 + TPM_MAX_HASHES like below?

No, because at this point only a single additional group has been addedin addition to the hashes groups. The first line of tpm_sysfs_add_device is

	WARN_ON(chip->groups_cnt != 0);

And then we add the unnamed group. This loop over the banks follows it, so chip->groups_cnt should be nr_banks_allocated by the end (it's the index, which is one fewer than the number of entries in chip->groups[]). We have a problem if nr_banks_allocated > TPM_MAX_HASHES

which is what the BUG_ON checks.

James

Quoted 8 lines
Quoted 2 lines

If the chip supported all 5 listed cases wouldn't groups_cnt be 6 at this point?

Actually, yes, I think it would be because it's pointing at the next free index not the current one. So it should be BUG_ON (chip-

Quoted 1 line

groups_cnt > TPM_MAX_HASHES + 1)

James

One other thought, should a note be added above tpm_algorithms to note that when that is changed TPM_MAX_HASHES should be changed as well?

With the above change to the BUG_ON you can add to v3:

author maintainer
#8 lore raw

Re: [PATCH v2 1/1] tpm: add sysfs exports for all banks of PCR registers

Quoted 47 lines · Jerry Snitselaar

James Bottomley @ 2020-07-21 17:39 MST:

Quoted 45 lines · Jerry Snitselaar
Quoted 35 lines

James Bottomley @ 2020-07-21 16:37 MST:

Quoted 33 lines · Jerry Snitselaar
Quoted 1 line

James Bottomley @ 2020-07-21 08:56 MST:

[...]

Quoted 8 lines
Quoted 8 lines
(quoted diff) +2 -0
+	/*
+	 * This will only trigger if someone has added an

additional

(quoted diff) +1 -0
+	 * hash to the tpm_algorithms enum without

incrementing

(quoted diff) +1 -0
+	 * TPM_MAX_HASHES.  This has to be a BUG_ON

because
under

Quoted 8 lines

drivers/char/tpm/tpm-sysfs.c static const struct attribute_group tpm2_dev_group = { · line 537

Quoted 6 lines

this

(quoted diff) +1 -0
+	 * condition, the chip->groups array will overflow

corrupting

drivers/char/tpm/tpm-sysfs.c +3 -0
+	 * the chips structure.
+	 */
+	BUG_ON(chip->groups_cnt > TPM_MAX_HASHES);

Should this check be 3 + TPM_MAX_HASHES like below?

No, because at this point only a single additional group has been addedin addition to the hashes groups. The first line of tpm_sysfs_add_device is

	WARN_ON(chip->groups_cnt != 0);

And then we add the unnamed group. This loop over the banks follows it, so chip->groups_cnt should be nr_banks_allocated by the end (it's the index, which is one fewer than the number of entries in chip->groups[]). We have a problem if nr_banks_allocated > TPM_MAX_HASHES

Quoted 6 lines
Quoted 3 lines

which is what the BUG_ON checks.

James

If the chip supported all 5 listed cases wouldn't groups_cnt be 6 at this point?

Actually, yes, I think it would be because it's pointing at the next free index not the current one. So it should be BUG_ON (chip-

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

groups_cnt > TPM_MAX_HASHES + 1)

James

One other thought, should a note be added above tpm_algorithms to note that when that is changed TPM_MAX_HASHES should be changed as well?

I certainly can ... it's free.

Quoted 1 line

With the above change to the BUG_ON you can add to v3:

OK, I also changed the BUG_ON back to a WARN_ON to match the initial one (if that one ever tripped, we'd get an overflow in the chip-

Quoted 1 line

groups[] as well, so it seems reasonable to keep them matching).

James

Quoted 2 lines