[PATCH v3 2/2] ima: support calculating the boot_aggregate based on different TPM banks

From: Mimi Zohar
Date: Thu Jan 30 2020 - 11:23:14 EST


Calculating the boot_aggregate attempts to read the TPM SHA1 bank,
assuming it is always enabled. With TPM 2.0 hash agility, TPM chips
could support multiple TPM PCR banks, allowing firmware to configure and
enable different banks.

Instead of hard coding the TPM 2.0 bank hash algorithm used for calculating
the boot-aggregate, use the same hash algorithm for reading the TPM PCRs as
for calculating the boot aggregate digest. Preference is given to the
configured IMA default hash algorithm.

For TPM 1.2 SHA1 is the only supported hash algorithm.

Reported-by: Jerry Snitselaar <jsnitsel@xxxxxxxxxx>
Suggested-by: Roberto Sassu <roberto.sassu@xxxxxxxxxx>
Signed-off-by: Mimi Zohar <zohar@xxxxxxxxxxxxx>
---
security/integrity/ima/ima_crypto.c | 45 ++++++++++++++++++++++++++++++++++++-
1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/security/integrity/ima/ima_crypto.c b/security/integrity/ima/ima_crypto.c
index 7967a6904851..a020aaefdea8 100644
--- a/security/integrity/ima/ima_crypto.c
+++ b/security/integrity/ima/ima_crypto.c
@@ -656,13 +656,34 @@ static void __init ima_pcrread(u32 idx, struct tpm_digest *d)
pr_err("Error Communicating to TPM chip\n");
}

+static enum hash_algo get_hash_algo(const char *algname)
+{
+ int i;
+
+ for (i = 0; i < HASH_ALGO__LAST; i++) {
+ if (strcmp(algname, hash_algo_name[i]) == 0)
+ break;
+ }
+
+ return i;
+}
+
/*
- * Calculate the boot aggregate hash
+ * The boot_aggregate is a cumulative hash over TPM registers 0 - 7. With
+ * TPM 1.2 the boot_aggregate was based on reading the SHA1 PCRs, but with
+ * TPM 2.0 hash agility, TPM chips could support multiple TPM PCR banks,
+ * allowing firmware to configure and enable different banks.
+ *
+ * Knowing which TPM bank is read to calculate the boot_aggregate digest
+ * needs to be conveyed to a verifier. For this reason, use the same
+ * hash algorithm for reading the TPM PCRs as for calculating the boot
+ * aggregate digest as stored in the measurement list.
*/
static int __init ima_calc_boot_aggregate_tfm(char *digest,
struct crypto_shash *tfm)
{
struct tpm_digest d = { .alg_id = TPM_ALG_SHA1, .digest = {0} };
+ enum hash_algo crypto_id;
int rc;
u32 i;
SHASH_DESC_ON_STACK(shash, tfm);
@@ -673,6 +694,28 @@ static int __init ima_calc_boot_aggregate_tfm(char *digest,
if (rc != 0)
return rc;

+ crypto_id = get_hash_algo(crypto_shash_alg_name(tfm));
+ if (crypto_id == HASH_ALGO__LAST) {
+ pr_devel("unknown hash algorithm (%s), failing to read PCRs.\n",
+ crypto_shash_alg_name(tfm));
+ return 0;
+ }
+
+ for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
+ if (ima_tpm_chip->allocated_banks[i].crypto_id == crypto_id) {
+ d.alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
+ break;
+ }
+ }
+ if (i == ima_tpm_chip->nr_allocated_banks) {
+ pr_devel("TPM %s bank not allocated, failing to read PCRs.\n",
+ crypto_shash_alg_name(tfm));
+ return 0;
+ }
+
+ pr_devel("calculating the boot-aggregregate based on TPM bank: %04x\n",
+ d.alg_id);
+
/* cumulative sha1 over tpm registers 0-7 */
for (i = TPM_PCR0; i < TPM_PCR8; i++) {
ima_pcrread(i, &d);
--
2.7.5