Skip to content

Commit 2592677

Browse files
robertosassumimizohar
authored andcommitted
ima: Use ima_hash_algo for collision detection in the measurement list
Before calculating a digest for each PCR bank, collisions were detected with a SHA1 digest. This patch includes ima_hash_algo among the algorithms used to calculate the template digest and checks collisions on that digest. The position in the measurement entry array of the template digest calculated with the IMA default hash algorithm is stored in the ima_hash_algo_idx global variable and is determined at IMA initialization time. Signed-off-by: Roberto Sassu <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 1ea973d commit 2592677

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

security/integrity/ima/ima.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extern int ima_policy_flag;
5353
/* set during initialization */
5454
extern int ima_hash_algo;
5555
extern int ima_sha1_idx __ro_after_init;
56+
extern int ima_hash_algo_idx __ro_after_init;
5657
extern int ima_extra_slots __ro_after_init;
5758
extern int ima_appraise;
5859
extern struct tpm_chip *ima_tpm_chip;

security/integrity/ima/ima_crypto.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct ima_algo_desc {
6363
};
6464

6565
int ima_sha1_idx __ro_after_init;
66+
int ima_hash_algo_idx __ro_after_init;
6667
/*
6768
* Additional number of slots reserved, as needed, for SHA1
6869
* and IMA default algo.
@@ -122,15 +123,25 @@ int __init ima_init_crypto(void)
122123
return rc;
123124

124125
ima_sha1_idx = -1;
126+
ima_hash_algo_idx = -1;
125127

126128
for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
127129
algo = ima_tpm_chip->allocated_banks[i].crypto_id;
128130
if (algo == HASH_ALGO_SHA1)
129131
ima_sha1_idx = i;
132+
133+
if (algo == ima_hash_algo)
134+
ima_hash_algo_idx = i;
130135
}
131136

132-
if (ima_sha1_idx < 0)
137+
if (ima_sha1_idx < 0) {
133138
ima_sha1_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
139+
if (ima_hash_algo == HASH_ALGO_SHA1)
140+
ima_hash_algo_idx = ima_sha1_idx;
141+
}
142+
143+
if (ima_hash_algo_idx < 0)
144+
ima_hash_algo_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
134145

135146
ima_algo_array = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
136147
sizeof(*ima_algo_array), GFP_KERNEL);
@@ -179,6 +190,12 @@ int __init ima_init_crypto(void)
179190
ima_algo_array[ima_sha1_idx].algo = HASH_ALGO_SHA1;
180191
}
181192

193+
if (ima_hash_algo_idx >= NR_BANKS(ima_tpm_chip) &&
194+
ima_hash_algo_idx != ima_sha1_idx) {
195+
ima_algo_array[ima_hash_algo_idx].tfm = ima_shash_tfm;
196+
ima_algo_array[ima_hash_algo_idx].algo = ima_hash_algo;
197+
}
198+
182199
return 0;
183200
out_array:
184201
for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {

security/integrity/ima/ima_queue.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ static struct ima_queue_entry *ima_lookup_digest_entry(u8 *digest_value,
5555
key = ima_hash_key(digest_value);
5656
rcu_read_lock();
5757
hlist_for_each_entry_rcu(qe, &ima_htable.queue[key], hnext) {
58-
rc = memcmp(qe->entry->digests[ima_sha1_idx].digest,
59-
digest_value, TPM_DIGEST_SIZE);
58+
rc = memcmp(qe->entry->digests[ima_hash_algo_idx].digest,
59+
digest_value, hash_digest_size[ima_hash_algo]);
6060
if ((rc == 0) && (qe->entry->pcr == pcr)) {
6161
ret = qe;
6262
break;
@@ -108,7 +108,7 @@ static int ima_add_digest_entry(struct ima_template_entry *entry,
108108

109109
atomic_long_inc(&ima_htable.len);
110110
if (update_htable) {
111-
key = ima_hash_key(entry->digests[ima_sha1_idx].digest);
111+
key = ima_hash_key(entry->digests[ima_hash_algo_idx].digest);
112112
hlist_add_head_rcu(&qe->hnext, &ima_htable.queue[key]);
113113
}
114114

@@ -160,7 +160,7 @@ int ima_add_template_entry(struct ima_template_entry *entry, int violation,
160160
const char *op, struct inode *inode,
161161
const unsigned char *filename)
162162
{
163-
u8 *digest = entry->digests[ima_sha1_idx].digest;
163+
u8 *digest = entry->digests[ima_hash_algo_idx].digest;
164164
struct tpm_digest *digests_arg = entry->digests;
165165
const char *audit_cause = "hash_added";
166166
char tpm_audit_cause[AUDIT_CAUSE_LEN_MAX];

0 commit comments

Comments
 (0)