hulk inclusion category: bugfix bugzilla: https://atomgit.com/openeuler/kernel/issues/10427 -------------------------------- The functions `ima_calc_boot_aggregate_tfm` and `ima_calc_boot_aggregate` are tightly coupled with TPM device operations, particularly in handling boot aggregate measurements. Their current placement in ima_crypto.c suggests a generic cryptographic context, which misaligns with their actual dependency on TPM-specific logic (e.g., PCR extend operations and TPM command handling). This refactoring: 1. Relocates both functions to ima_tpm.c to better reflect their association with TPM subsystem interactions. 2. Updates function declarations and header inclusions to maintain compilation integrity. 3. Clarifies code organization by grouping TPM-related functionalities, enhancing maintainability and modularity. No functional changes are introduced; this is purely a structural improvement to align with subsystem boundaries. Signed-off-by: Zhao Yipeng <zhaoyipeng5@huawei.com> Signed-off-by: Gong Ruiqi <ruiqi.gong@huawei.com> --- security/integrity/ima/ima.h | 3 +- security/integrity/ima/ima_crypto.c | 99 +---------------------------- security/integrity/ima/ima_tpm.c | 92 ++++++++++++++++++++++++++- 3 files changed, 95 insertions(+), 99 deletions(-) diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index d33992a8824f..450073471472 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -152,7 +152,8 @@ int ima_calc_buffer_hash(const void *buf, loff_t len, struct ima_digest_data *hash); int ima_calc_field_array_hash(struct ima_field_data *field_data, struct ima_template_entry *entry); -int ima_calc_boot_aggregate(struct ima_digest_data *hash); +struct crypto_shash *ima_alloc_tfm(enum hash_algo algo); +void ima_free_tfm(struct crypto_shash *tfm); void ima_add_violation(struct file *file, const unsigned char *filename, struct integrity_iint_cache *iint, const char *op, const char *cause); diff --git a/security/integrity/ima/ima_crypto.c b/security/integrity/ima/ima_crypto.c index 17feaf90a980..e6588fa42d48 100644 --- a/security/integrity/ima/ima_crypto.c +++ b/security/integrity/ima/ima_crypto.c @@ -89,7 +89,7 @@ static int __init ima_init_ima_crypto(void) return 0; } -static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo) +struct crypto_shash *ima_alloc_tfm(enum hash_algo algo) { struct crypto_shash *tfm = ima_shash_tfm; int rc, i; @@ -212,7 +212,7 @@ int __init ima_init_crypto(void) return rc; } -static void ima_free_tfm(struct crypto_shash *tfm) +void ima_free_tfm(struct crypto_shash *tfm) { int i; @@ -777,98 +777,3 @@ int ima_calc_buffer_hash(const void *buf, loff_t len, return calc_buffer_shash(buf, len, 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 ima_calc_boot_aggregate_tfm(char *digest, u16 alg_id, - struct crypto_shash *tfm) -{ - struct tpm_digest d = { .alg_id = alg_id, .digest = {0} }; - int rc; - u32 i; - SHASH_DESC_ON_STACK(shash, tfm); - - shash->tfm = tfm; - - pr_devel("calculating the boot-aggregate based on TPM bank: %04x\n", - d.alg_id); - - rc = crypto_shash_init(shash); - if (rc != 0) - return rc; - - /* cumulative digest over TPM registers 0-7 */ - for (i = TPM_PCR0; i < TPM_PCR8; i++) { - ima_pcrread(i, &d); - /* now accumulate with current aggregate */ - rc = crypto_shash_update(shash, d.digest, - crypto_shash_digestsize(tfm)); - if (rc != 0) - return rc; - } - /* - * Extend cumulative digest over TPM registers 8-9, which contain - * measurement for the kernel command line (reg. 8) and image (reg. 9) - * in a typical PCR allocation. Registers 8-9 are only included in - * non-SHA1 boot_aggregate digests to avoid ambiguity. - */ - if (alg_id != TPM_ALG_SHA1) { - for (i = TPM_PCR8; i < TPM_PCR10; i++) { - ima_pcrread(i, &d); - rc = crypto_shash_update(shash, d.digest, - crypto_shash_digestsize(tfm)); - } - } - if (!rc) - rc = crypto_shash_final(shash, digest); - return rc; -} - -int ima_calc_boot_aggregate(struct ima_digest_data *hash) -{ - struct crypto_shash *tfm; - u16 crypto_id, alg_id; - int rc, i, bank_idx = -1; - - for (i = 0; i < NR_BANKS(ima_rot_inst); i++) { - crypto_id = ima_rot_inst->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_rot_inst->allocated_banks[bank_idx].crypto_id; - - tfm = ima_alloc_tfm(hash->algo); - if (IS_ERR(tfm)) - return PTR_ERR(tfm); - - hash->length = crypto_shash_digestsize(tfm); - alg_id = ima_rot_inst->allocated_banks[bank_idx].alg_id; - rc = ima_calc_boot_aggregate_tfm(hash->digest, alg_id, tfm); - - ima_free_tfm(tfm); - - return rc; -} diff --git a/security/integrity/ima/ima_tpm.c b/security/integrity/ima/ima_tpm.c index 5c3dea3e0719..07f353a7b1ad 100644 --- a/security/integrity/ima/ima_tpm.c +++ b/security/integrity/ima/ima_tpm.c @@ -57,7 +57,97 @@ int ima_tpm_extend(struct tpm_digest *digests_arg, const void *args) return ima_pcr_extend(digests_arg, pcr); } +/* + * 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 ima_calc_boot_aggregate_tfm(char *digest, u16 alg_id, + struct crypto_shash *tfm) +{ + struct tpm_digest d = { .alg_id = alg_id, .digest = {0} }; + int rc; + u32 i; + SHASH_DESC_ON_STACK(shash, tfm); + + shash->tfm = tfm; + + pr_devel("calculating the boot-aggregate based on TPM bank: %04x\n", + d.alg_id); + + rc = crypto_shash_init(shash); + if (rc != 0) + return rc; + + /* cumulative digest over TPM registers 0-7 */ + for (i = TPM_PCR0; i < TPM_PCR8; i++) { + ima_pcrread(i, &d); + /* now accumulate with current aggregate */ + rc = crypto_shash_update(shash, d.digest, + crypto_shash_digestsize(tfm)); + if (rc != 0) + return rc; + } + /* + * Extend cumulative digest over TPM registers 8-9, which contain + * measurement for the kernel command line (reg. 8) and image (reg. 9) + * in a typical PCR allocation. Registers 8-9 are only included in + * non-SHA1 boot_aggregate digests to avoid ambiguity. + */ + if (alg_id != TPM_ALG_SHA1) { + for (i = TPM_PCR8; i < TPM_PCR10; i++) { + ima_pcrread(i, &d); + rc = crypto_shash_update(shash, d.digest, + crypto_shash_digestsize(tfm)); + } + } + if (!rc) + rc = crypto_shash_final(shash, digest); + return rc; +} + int ima_tpm_calc_boot_aggregate(struct ima_digest_data *hash) { - return ima_calc_boot_aggregate(hash); + struct crypto_shash *tfm; + u16 crypto_id, alg_id; + int rc, i, bank_idx = -1; + + for (i = 0; i < NR_BANKS(ima_rot_inst); i++) { + crypto_id = ima_rot_inst->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_rot_inst->allocated_banks[bank_idx].crypto_id; + + tfm = ima_alloc_tfm(hash->algo); + if (IS_ERR(tfm)) + return PTR_ERR(tfm); + + hash->length = crypto_shash_digestsize(tfm); + alg_id = ima_rot_inst->allocated_banks[bank_idx].alg_id; + rc = ima_calc_boot_aggregate_tfm(hash->digest, alg_id, tfm); + + ima_free_tfm(tfm); + + return rc; } -- 2.43.0