Commit 6f1a1d10 authored by Roberto Sassu's avatar Roberto Sassu Committed by Mimi Zohar

ima: Switch to ima_hash_algo for boot aggregate

boot_aggregate is the first entry of IMA measurement list. Its purpose is
to link pre-boot measurements to IMA measurements. As IMA was designed to
work with a TPM 1.2, the SHA1 PCR bank was always selected even if a
TPM 2.0 with support for stronger hash algorithms is available.

This patch first tries to find a PCR bank with the IMA default hash
algorithm. If it does not find it, it selects the SHA256 PCR bank for
TPM 2.0 and SHA1 for TPM 1.2. Ultimately, it selects SHA1 also for TPM 2.0
if the SHA256 PCR bank is not found.

If none of the PCR banks above can be found, boot_aggregate file digest is
filled with zeros, as for TPM bypass, making it impossible to perform a
remote attestation of the system.

Cc: stable@vger.kernel.org # 5.1.x
Fixes: 879b5892 ("tpm: retrieve digest size of unknown algorithms with PCR read")
Reported-by: default avatarJerry Snitselaar <jsnitsel@redhat.com>
Suggested-by: default avatarJames Bottomley <James.Bottomley@HansenPartnership.com>
Signed-off-by: default avatarRoberto Sassu <roberto.sassu@huawei.com>
Signed-off-by: default avatarMimi Zohar <zohar@linux.ibm.com>
parent ae83d0b4
...@@ -655,18 +655,29 @@ static void __init ima_pcrread(u32 idx, struct tpm_digest *d) ...@@ -655,18 +655,29 @@ static void __init ima_pcrread(u32 idx, struct tpm_digest *d)
} }
/* /*
* 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, static int __init ima_calc_boot_aggregate_tfm(char *digest, u16 alg_id,
struct crypto_shash *tfm) struct crypto_shash *tfm)
{ {
struct tpm_digest d = { .alg_id = TPM_ALG_SHA1, .digest = {0} }; struct tpm_digest d = { .alg_id = alg_id, .digest = {0} };
int rc; int rc;
u32 i; u32 i;
SHASH_DESC_ON_STACK(shash, tfm); SHASH_DESC_ON_STACK(shash, tfm);
shash->tfm = tfm; shash->tfm = tfm;
pr_devel("calculating the boot-aggregate based on TPM bank: %04x\n",
d.alg_id);
rc = crypto_shash_init(shash); rc = crypto_shash_init(shash);
if (rc != 0) if (rc != 0)
return rc; return rc;
...@@ -675,7 +686,8 @@ static int __init ima_calc_boot_aggregate_tfm(char *digest, ...@@ -675,7 +686,8 @@ static int __init ima_calc_boot_aggregate_tfm(char *digest,
for (i = TPM_PCR0; i < TPM_PCR8; i++) { for (i = TPM_PCR0; i < TPM_PCR8; i++) {
ima_pcrread(i, &d); ima_pcrread(i, &d);
/* now accumulate with current aggregate */ /* now accumulate with current aggregate */
rc = crypto_shash_update(shash, d.digest, TPM_DIGEST_SIZE); rc = crypto_shash_update(shash, d.digest,
crypto_shash_digestsize(tfm));
} }
if (!rc) if (!rc)
crypto_shash_final(shash, digest); crypto_shash_final(shash, digest);
...@@ -685,14 +697,37 @@ static int __init ima_calc_boot_aggregate_tfm(char *digest, ...@@ -685,14 +697,37 @@ static int __init ima_calc_boot_aggregate_tfm(char *digest,
int __init ima_calc_boot_aggregate(struct ima_digest_data *hash) int __init ima_calc_boot_aggregate(struct ima_digest_data *hash)
{ {
struct crypto_shash *tfm; struct crypto_shash *tfm;
int rc; u16 crypto_id, alg_id;
int rc, i, bank_idx = -1;
for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
if (crypto_id == hash->algo) {
bank_idx = i;
break;
}
if (crypto_id == HASH_ALGO_SHA256)
bank_idx = i;
if (bank_idx == -1 && crypto_id == HASH_ALGO_SHA1)
bank_idx = i;
}
if (bank_idx == -1) {
pr_err("No suitable TPM algorithm for boot aggregate\n");
return 0;
}
hash->algo = ima_tpm_chip->allocated_banks[bank_idx].crypto_id;
tfm = ima_alloc_tfm(hash->algo); tfm = ima_alloc_tfm(hash->algo);
if (IS_ERR(tfm)) if (IS_ERR(tfm))
return PTR_ERR(tfm); return PTR_ERR(tfm);
hash->length = crypto_shash_digestsize(tfm); hash->length = crypto_shash_digestsize(tfm);
rc = ima_calc_boot_aggregate_tfm(hash->digest, tfm); alg_id = ima_tpm_chip->allocated_banks[bank_idx].alg_id;
rc = ima_calc_boot_aggregate_tfm(hash->digest, alg_id, tfm);
ima_free_tfm(tfm); ima_free_tfm(tfm);
......
...@@ -25,7 +25,7 @@ struct tpm_chip *ima_tpm_chip; ...@@ -25,7 +25,7 @@ struct tpm_chip *ima_tpm_chip;
/* Add the boot aggregate to the IMA measurement list and extend /* Add the boot aggregate to the IMA measurement list and extend
* the PCR register. * the PCR register.
* *
* Calculate the boot aggregate, a SHA1 over tpm registers 0-7, * Calculate the boot aggregate, a hash over tpm registers 0-7,
* assuming a TPM chip exists, and zeroes if the TPM chip does not * assuming a TPM chip exists, and zeroes if the TPM chip does not
* exist. Add the boot aggregate measurement to the measurement * exist. Add the boot aggregate measurement to the measurement
* list and extend the PCR register. * list and extend the PCR register.
...@@ -49,15 +49,27 @@ static int __init ima_add_boot_aggregate(void) ...@@ -49,15 +49,27 @@ static int __init ima_add_boot_aggregate(void)
int violation = 0; int violation = 0;
struct { struct {
struct ima_digest_data hdr; struct ima_digest_data hdr;
char digest[TPM_DIGEST_SIZE]; char digest[TPM_MAX_DIGEST_SIZE];
} hash; } hash;
memset(iint, 0, sizeof(*iint)); memset(iint, 0, sizeof(*iint));
memset(&hash, 0, sizeof(hash)); memset(&hash, 0, sizeof(hash));
iint->ima_hash = &hash.hdr; iint->ima_hash = &hash.hdr;
iint->ima_hash->algo = HASH_ALGO_SHA1; iint->ima_hash->algo = ima_hash_algo;
iint->ima_hash->length = SHA1_DIGEST_SIZE; iint->ima_hash->length = hash_digest_size[ima_hash_algo];
/*
* With TPM 2.0 hash agility, TPM chips could support multiple TPM
* PCR banks, allowing firmware to configure and enable different
* banks. The SHA1 bank is not necessarily enabled.
*
* 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. Otherwise, use the
* TCG required banks - SHA256 for TPM 2.0, SHA1 for TPM 1.2.
* Ultimately select SHA1 also for TPM 2.0 if the SHA256 PCR bank
* is not found.
*/
if (ima_tpm_chip) { if (ima_tpm_chip) {
result = ima_calc_boot_aggregate(&hash.hdr); result = ima_calc_boot_aggregate(&hash.hdr);
if (result < 0) { if (result < 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment