Skip to content

Commit 1ea973d

Browse files
robertosassumimizohar
authored andcommitted
ima: Calculate and extend PCR with digests in ima_template_entry
This patch modifies ima_calc_field_array_hash() to calculate a template digest for each allocated PCR bank and SHA1. It also passes the tpm_digest array of the template entry to ima_pcr_extend() or in case of a violation, the pre-initialized digests array filled with 0xff. Padding with zeros is still done if the mapping between TPM algorithm ID and crypto ID is unknown. This patch calculates again the template digest when a measurement list is restored. Copying only the SHA1 digest (due to the limitation of the current measurement list format) is not sufficient, as hash collision detection will be done on the digest calculated with the IMA default hash algorithm. Signed-off-by: Roberto Sassu <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 6d94809 commit 1ea973d

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

security/integrity/ima/ima_crypto.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,36 @@ static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
619619
int ima_calc_field_array_hash(struct ima_field_data *field_data,
620620
struct ima_template_entry *entry)
621621
{
622-
int rc;
622+
u16 alg_id;
623+
int rc, i;
623624

624625
rc = ima_calc_field_array_hash_tfm(field_data, entry, ima_sha1_idx);
626+
if (rc)
627+
return rc;
628+
629+
entry->digests[ima_sha1_idx].alg_id = TPM_ALG_SHA1;
630+
631+
for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {
632+
if (i == ima_sha1_idx)
633+
continue;
634+
635+
if (i < NR_BANKS(ima_tpm_chip)) {
636+
alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
637+
entry->digests[i].alg_id = alg_id;
638+
}
639+
640+
/* for unmapped TPM algorithms digest is still a padded SHA1 */
641+
if (!ima_algo_array[i].tfm) {
642+
memcpy(entry->digests[i].digest,
643+
entry->digests[ima_sha1_idx].digest,
644+
TPM_DIGEST_SIZE);
645+
continue;
646+
}
647+
648+
rc = ima_calc_field_array_hash_tfm(field_data, entry, i);
649+
if (rc)
650+
return rc;
651+
}
625652
return rc;
626653
}
627654

security/integrity/ima/ima_queue.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,14 @@ unsigned long ima_get_binary_runtime_size(void)
135135
return binary_runtime_size + sizeof(struct ima_kexec_hdr);
136136
};
137137

138-
static int ima_pcr_extend(const u8 *hash, int pcr)
138+
static int ima_pcr_extend(struct tpm_digest *digests_arg, int pcr)
139139
{
140140
int result = 0;
141-
int i;
142141

143142
if (!ima_tpm_chip)
144143
return result;
145144

146-
for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++)
147-
memcpy(digests[i].digest, hash, TPM_DIGEST_SIZE);
148-
149-
result = tpm_pcr_extend(ima_tpm_chip, pcr, digests);
145+
result = tpm_pcr_extend(ima_tpm_chip, pcr, digests_arg);
150146
if (result != 0)
151147
pr_err("Error Communicating to TPM chip, result: %d\n", result);
152148
return result;
@@ -164,16 +160,15 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation,
164160
const char *op, struct inode *inode,
165161
const unsigned char *filename)
166162
{
167-
u8 digest[TPM_DIGEST_SIZE];
163+
u8 *digest = entry->digests[ima_sha1_idx].digest;
164+
struct tpm_digest *digests_arg = entry->digests;
168165
const char *audit_cause = "hash_added";
169166
char tpm_audit_cause[AUDIT_CAUSE_LEN_MAX];
170167
int audit_info = 1;
171168
int result = 0, tpmresult = 0;
172169

173170
mutex_lock(&ima_extend_list_mutex);
174171
if (!violation) {
175-
memcpy(digest, entry->digests[ima_sha1_idx].digest,
176-
sizeof(digest));
177172
if (ima_lookup_digest_entry(digest, entry->pcr)) {
178173
audit_cause = "hash_exists";
179174
result = -EEXIST;
@@ -189,9 +184,9 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation,
189184
}
190185

191186
if (violation) /* invalidate pcr */
192-
memset(digest, 0xff, sizeof(digest));
187+
digests_arg = digests;
193188

194-
tpmresult = ima_pcr_extend(digest, entry->pcr);
189+
tpmresult = ima_pcr_extend(digests_arg, entry->pcr);
195190
if (tpmresult != 0) {
196191
snprintf(tpm_audit_cause, AUDIT_CAUSE_LEN_MAX, "TPM_error(%d)",
197192
tpmresult);
@@ -217,6 +212,8 @@ int ima_restore_measurement_entry(struct ima_template_entry *entry)
217212

218213
int __init ima_init_digests(void)
219214
{
215+
u16 digest_size;
216+
u16 crypto_id;
220217
int i;
221218

222219
if (!ima_tpm_chip)
@@ -227,8 +224,17 @@ int __init ima_init_digests(void)
227224
if (!digests)
228225
return -ENOMEM;
229226

230-
for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++)
227+
for (i = 0; i < ima_tpm_chip->nr_allocated_banks; i++) {
231228
digests[i].alg_id = ima_tpm_chip->allocated_banks[i].alg_id;
229+
digest_size = ima_tpm_chip->allocated_banks[i].digest_size;
230+
crypto_id = ima_tpm_chip->allocated_banks[i].crypto_id;
231+
232+
/* for unmapped TPM algorithms digest is still a padded SHA1 */
233+
if (crypto_id == HASH_ALGO__LAST)
234+
digest_size = SHA1_DIGEST_SIZE;
235+
236+
memset(digests[i].digest, 0xff, digest_size);
237+
}
232238

233239
return 0;
234240
}

security/integrity/ima/ima_template.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ static int ima_restore_template_data(struct ima_template_desc *template_desc,
357357
int ima_restore_measurement_list(loff_t size, void *buf)
358358
{
359359
char template_name[MAX_TEMPLATE_NAME_LEN];
360+
unsigned char zero[TPM_DIGEST_SIZE] = { 0 };
360361

361362
struct ima_kexec_hdr *khdr = buf;
362363
struct ima_field_data hdr[HDR__LAST] = {
@@ -456,8 +457,17 @@ int ima_restore_measurement_list(loff_t size, void *buf)
456457
if (ret < 0)
457458
break;
458459

459-
memcpy(entry->digests[ima_sha1_idx].digest,
460-
hdr[HDR_DIGEST].data, hdr[HDR_DIGEST].len);
460+
if (memcmp(hdr[HDR_DIGEST].data, zero, sizeof(zero))) {
461+
ret = ima_calc_field_array_hash(
462+
&entry->template_data[0],
463+
entry);
464+
if (ret < 0) {
465+
pr_err("cannot calculate template digest\n");
466+
ret = -EINVAL;
467+
break;
468+
}
469+
}
470+
461471
entry->pcr = !ima_canonical_fmt ? *(hdr[HDR_PCR].data) :
462472
le32_to_cpu(*(hdr[HDR_PCR].data));
463473
ret = ima_restore_measurement_entry(entry);

0 commit comments

Comments
 (0)