Skip to contentSkip to conversation

enhance TPM 2.0 extend function to support multiple PCR banks

PATCH New Nayna Jain 1 participant 3 messages Jan 30, 2017 TPM device driver

lore
Share & tools
lore

Nayna Jain

author
#0 lore raw

v7 cover enhance TPM 2.0 extend function to support multiple PCR banks

IMA extends its hash measurements in the TPM PCRs, based on policy. The existing in-kernel TPM extend function extends only the SHA1 PCR bank. TPM 2.0 defines multiple PCR banks, to support different hash algorithms. The TCG TPM 2.0 Specification[1] recommends extending all active PCR banks to prevent malicious users from setting unused PCR banks with fake measurements and quoting them. This patch set adds support for extending all active PCR banks, as recommended.

The first patch implements the TPM 2.0 capability to retrieve the list of active PCR banks.

The second patch modifies the tpm_pcr_extend() and tpm2_pcr_extend() interface to support extending multiple PCR banks. The existing tpm_pcr_extend() interface expects only a SHA1 digest. Hence, to extend all active PCR banks with differing digest sizes for TPM 2.0, the SHA1 digest is padded with 0's as needed.

[1] TPM 2.0 Specification referred here is "TCG PC Client Specific Platform Firmware Profile for TPM 2.0"

Changelog v7:
- Patch "tpm: implement TPM 2.0 capability to get active PCR banks"
- Fixed missing TPM error handling in tpm2_get_pcr_allocation(). Thanks Jarkko for noticing it.
- Included Stefan's suggestion on adding buffer access check.
- Patch "tpm: enchance TPM 2.0 PCR extend to support multiple banks"
- Moved tpm2_digest and include for hash_info.h in tpm.h. tpm_eventlog.h has no modifications now.

Changelog v6:
- Patch "tpm: implement TPM 2.0 capability to get active PCR banks"
- Fixed the regression - missing tpm_buf_destroy() in in tpm2_get_pcr_allocation(). Thanks Jarkko for noticing.
- Added TPM2_ALG_ERROR = 0x0000 to represent invalid algorithm.

Changelog v5:
- Patch "tpm: implement TPM 2.0 capability to get active PCR banks"
- Included Jarkko's feedbacks
- Moved variable declaration to start of function in tpm_pcr_extend()

Changelog v4:
- Updated cover letter as per Mimi's feedback.
- Rebased to Jarkko's latest master branch (4064b6b tpm_tis: use default timeout value if chip reports it as zero)
- Patch "tpm: implement TPM 2.0 capability to get active PCR banks"
- Included Jarkko's feedbacks
- Moved call to tpm2_get_pcr_allocation to Patch 2
- Renamed struct tpm2_tpms_pcr_selection to struct tpm2_pcr_selection and moved the struct to before tpm2_get_pcr_allocation()
- Fixed code formatting
- Patch "tpm: enchance TPM 2.0 PCR extend to support multiple banks"
- Included Jarkkos' feedbacks
- Updated commit msg to mention dependency on CRYPTO_HASH_INFO
- Renamed struct tpmt_hash to struct tpm2_digest
- Removed struct tpml_digest_values, tpm2_pcr_extend() now accepts count and digests list as two separate arguments. Added check for count of hashes passed.
- Cleaned up struct tpm2_pcr_extend_in as not required anymore with use of tpm_buf
- Moved struct tpm2_null_auth_area just before tpm2_pcr_extend() as it is the only function using it for now.
- Fixed code formatting

Changelog v3:
- Rebased to the Jarkko's latest master branch (8e25809 tpm:
Do not print an error message when doing TPM auto startup)
- Patch "tpm: implement TPM 2.0 capability to get active PCR banks"
- Included Jarkko's feedbacks
- Removed getcap_in, getcap_out and used tpm_buf for getting capability.
- Used ARRAY_SIZE in place of TPM_MAX_PCR_BANKS and included other feedbacks.
- Patch "tpm: enhance TPM 2.0 PCR extend to support multiple banks"
- Fixed kbuild errors
- Fixed buf.data uninitialized warning.
- Added TCG_TPM dependency on CONFIG_CRYPTO_HASH_INFO in Kconfig.

Changelog v2:

- Patch "tpm: implement TPM 2.0 capability to get active PCR banks"
- defined structs definition in tpm2-cmd.c.
- no_of_active_banks field is removed. Instead, constant TPM2_MAX_PCR_BANKS is defined.
- renamed tpm2_get_active_pcr_banks() to tpm2_get_pcr_allocation()
- removed generic function tpm2_get_capability().

- Patch "tpm: enchance TPM 2.0 PCR extend to support multiple banks"
- Removed tpm2.h, and defined structs common for extend and event log in tpm_eventlog.h
- uses tpm_buf in tpm2_pcr_extend().

Nayna Jain (2):
tpm: implement TPM 2.0 capability to get active PCR banks
tpm: enhance TPM 2.0 PCR extend to support multiple banks

 drivers/char/tpm/Kconfig         |   1 +
 drivers/char/tpm/tpm-interface.c |  15 +++-
 drivers/char/tpm/tpm.h           |  14 +++-
 drivers/char/tpm/tpm2-cmd.c      | 168 +++++++++++++++++++++++++++++----------
 4 files changed, 154 insertions(+), 44 deletions(-)
Signature · 6 lines
2.5.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
author
#1 lore raw

PATCH v7 1/2 tpm: implement TPM 2.0 capability to get active PCR banks

This patch implements the TPM 2.0 capability TPM_CAP_PCRS to retrieve the active PCR banks from the TPM. This is needed to enable extending all active banks as recommended by TPM 2.0 TCG Specification.

---
 drivers/char/tpm/tpm.h      |  5 +++
 drivers/char/tpm/tpm2-cmd.c | 77 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+)
drivers/char/tpm/tpm.h +5 -0
index dbe0c5a..db0398a 100644
@@ -97,6 +97,7 @@ enum tpm2_return_codes {
 };
 
 enum tpm2_algorithms {
+	TPM2_ALG_ERROR		= 0x0000,
 	TPM2_ALG_SHA1		= 0x0004,
 	TPM2_ALG_KEYEDHASH	= 0x0008,
 	TPM2_ALG_SHA256		= 0x000B,
@@ -127,6 +128,7 @@ enum tpm2_permanent_handles {
 };
 
 enum tpm2_capabilities {
+	TPM2_CAP_PCRS		= 5,
 	TPM2_CAP_TPM_PROPERTIES = 6,
 };
 
@@ -187,6 +189,8 @@ struct tpm_chip {
 
 	const struct attribute_group *groups[3];
 	unsigned int groups_cnt;
+
+	u16 active_banks[7];
 #ifdef CONFIG_ACPI
 	acpi_handle acpi_dev_handle;
 	char ppi_version[TPM_PPI_VERSION_LEN + 1];
@@ -540,4 +544,5 @@ int tpm2_auto_startup(struct tpm_chip *chip);
 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
 unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal);
 int tpm2_probe(struct tpm_chip *chip);
+ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip);
 #endif
drivers/char/tpm/tpm2-cmd.c +77 -0

80 lines not on this page · raw message

Signature · 6 lines
2.5.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
author
#2 lore raw

PATCH v7 2/2 tpm: enhance TPM 2.0 PCR extend to support multiple banks

In reply to Nayna Jain

The current TPM 2.0 device driver extends only the SHA1 PCR bank but the TCG Specification[1] recommends extending all active PCR banks, to prevent malicious users from setting unused PCR banks with fake measurements and quoting them.

The existing in-kernel interface(tpm_pcr_extend()) expects only a SHA1 digest. To extend all active PCR banks with differing digest sizes, the SHA1 digest is padded with trailing 0's as needed.

This patch reuses the defined digest sizes from the crypto subsystem, adding a dependency on CRYPTO_HASH_INFO module.

[1] TPM 2.0 Specification referred here is "TCG PC Client Specific Platform Firmware Profile for TPM 2.0"

---
 drivers/char/tpm/Kconfig         |  1 +
 drivers/char/tpm/tpm-interface.c | 15 ++++++-
 drivers/char/tpm/tpm.h           |  9 +++-
 drivers/char/tpm/tpm2-cmd.c      | 91 +++++++++++++++++++++-------------------
 4 files changed, 72 insertions(+), 44 deletions(-)
drivers/char/tpm/Kconfig +1 -0
index 277186d..af985cc 100644
@@ -6,6 +6,7 @@ menuconfig TCG_TPM
 	tristate "TPM Hardware Support"
 	depends on HAS_IOMEM
 	select SECURITYFS
+	select CRYPTO_HASH_INFO
 	---help---
 	  If you have a TPM security chip in your system, which
 	  implements the Trusted Computing Group's specification,
drivers/char/tpm/tpm-interface.c +14 -1
index 2ea16ab..7fa05a9 100644
@@ -789,13 +789,26 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
 	struct tpm_cmd_t cmd;
 	int rc;
 	struct tpm_chip *chip;
+	int max_active_banks = ARRAY_SIZE(chip->active_banks);
+	struct tpm2_digest digest_list[max_active_banks];
+	u32 count = 0;
+	int i;
 
 	chip = tpm_chip_find_get(chip_num);
 	if (chip == NULL)
 		return -ENODEV;
 
 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
-		rc = tpm2_pcr_extend(chip, pcr_idx, hash);
+		memset(digest_list, 0, sizeof(digest_list));
+
+		for (i = 0; (chip->active_banks[i] != TPM2_ALG_ERROR) &&
+		     (i < max_active_banks); i++) {
+			digest_list[i].alg_id = chip->active_banks[i];
+			memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
+			count++;
+		}
+
+		rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
 		tpm_put_ops(chip);
 		return rc;
 	}
drivers/char/tpm/tpm.h +8 -1
index db0398a..4b7eca9 100644
@@ -34,6 +34,7 @@
 #include <linux/acpi.h>
 #include <linux/cdev.h>
 #include <linux/highmem.h>
+#include <crypto/hash_info.h>
 
 #include "tpm_eventlog.h"
 
@@ -380,6 +381,11 @@ struct tpm_cmd_t {
 	tpm_cmd_params	params;
 } __packed;
 
+struct tpm2_digest {
+	u16 alg_id;
+	u8 digest[SHA512_DIGEST_SIZE];
+} __packed;
+
 /* A string buffer type for constructing TPM commands. This is based on the
  * ideas of string buffer code in security/keys/trusted.h but is heap based
  * in order to keep the stack usage minimal.
@@ -529,7 +535,8 @@ static inline inline u32 tpm2_rc_value(u32 rc)
 }
 
 int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
-int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash);
+int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
+		    struct tpm2_digest *digests);
 int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max);
 int tpm2_seal_trusted(struct tpm_chip *chip,
 		      struct trusted_key_payload *payload,
drivers/char/tpm/tpm2-cmd.c +49 -42

133 lines not on this page · raw message

Signature · 6 lines
2.5.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot