Skip to content

Commit a74d719

Browse files
robertosassumimizohar
authored andcommitted
ima: Define and use a digest_size field in the ima_algo_desc structure
Add the digest_size field to the ima_algo_desc structure to determine the digest size from the correct source. If the hash algorithm is among allocated PCR banks, take the value from the TPM bank info (equal to the value from the crypto subsystem if the TPM algorithm is supported by it; otherwise, not exceding the size of the digest buffer in the tpm_digest structure, used by IMA). If the hash algorithm is SHA1, use the predefined value. Lastly, if the hash algorithm is the default one but not among the PCR banks, take the digest size from the crypto subsystem (the default hash algorithm is checked when parsing the ima_hash= command line option). Finally, use the new information to correctly show the template digest in ima_measurements_show() and ima_ascii_measurements_show(). Link: linux-integrity/linux#14 Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com> Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
1 parent 1984dc2 commit a74d719

3 files changed

Lines changed: 13 additions & 12 deletions

File tree

security/integrity/ima/ima.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extern atomic_t ima_setxattr_allowed_hash_algorithms;
5353
struct ima_algo_desc {
5454
struct crypto_shash *tfm;
5555
enum hash_algo algo;
56+
unsigned int digest_size;
5657
};
5758

5859
/* set during initialization */

security/integrity/ima/ima_crypto.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
109109

110110
int __init ima_init_crypto(void)
111111
{
112+
unsigned int digest_size;
112113
enum hash_algo algo;
113114
long rc;
114115
int i;
@@ -147,7 +148,9 @@ int __init ima_init_crypto(void)
147148

148149
for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
149150
algo = ima_tpm_chip->allocated_banks[i].crypto_id;
151+
digest_size = ima_tpm_chip->allocated_banks[i].digest_size;
150152
ima_algo_array[i].algo = algo;
153+
ima_algo_array[i].digest_size = digest_size;
151154

152155
/* unknown TPM algorithm */
153156
if (algo == HASH_ALGO__LAST)
@@ -183,12 +186,15 @@ int __init ima_init_crypto(void)
183186
}
184187

185188
ima_algo_array[ima_sha1_idx].algo = HASH_ALGO_SHA1;
189+
ima_algo_array[ima_sha1_idx].digest_size = SHA1_DIGEST_SIZE;
186190
}
187191

188192
if (ima_hash_algo_idx >= NR_BANKS(ima_tpm_chip) &&
189193
ima_hash_algo_idx != ima_sha1_idx) {
194+
digest_size = hash_digest_size[ima_hash_algo];
190195
ima_algo_array[ima_hash_algo_idx].tfm = ima_shash_tfm;
191196
ima_algo_array[ima_hash_algo_idx].algo = ima_hash_algo;
197+
ima_algo_array[ima_hash_algo_idx].digest_size = digest_size;
192198
}
193199

194200
return 0;

security/integrity/ima/ima_fs.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,12 @@ int ima_measurements_show(struct seq_file *m, void *v)
132132
char *template_name;
133133
u32 pcr, namelen, template_data_len; /* temporary fields */
134134
bool is_ima_template = false;
135-
enum hash_algo algo;
136135
int i, algo_idx;
137136

138137
algo_idx = ima_sha1_idx;
139-
algo = HASH_ALGO_SHA1;
140138

141-
if (m->file != NULL) {
139+
if (m->file != NULL)
142140
algo_idx = (unsigned long)file_inode(m->file)->i_private;
143-
algo = ima_algo_array[algo_idx].algo;
144-
}
145141

146142
/* get entry */
147143
e = qe->entry;
@@ -160,7 +156,8 @@ int ima_measurements_show(struct seq_file *m, void *v)
160156
ima_putc(m, &pcr, sizeof(e->pcr));
161157

162158
/* 2nd: template digest */
163-
ima_putc(m, e->digests[algo_idx].digest, hash_digest_size[algo]);
159+
ima_putc(m, e->digests[algo_idx].digest,
160+
ima_algo_array[algo_idx].digest_size);
164161

165162
/* 3rd: template name size */
166163
namelen = !ima_canonical_fmt ? strlen(template_name) :
@@ -229,16 +226,12 @@ static int ima_ascii_measurements_show(struct seq_file *m, void *v)
229226
struct ima_queue_entry *qe = v;
230227
struct ima_template_entry *e;
231228
char *template_name;
232-
enum hash_algo algo;
233229
int i, algo_idx;
234230

235231
algo_idx = ima_sha1_idx;
236-
algo = HASH_ALGO_SHA1;
237232

238-
if (m->file != NULL) {
233+
if (m->file != NULL)
239234
algo_idx = (unsigned long)file_inode(m->file)->i_private;
240-
algo = ima_algo_array[algo_idx].algo;
241-
}
242235

243236
/* get entry */
244237
e = qe->entry;
@@ -252,7 +245,8 @@ static int ima_ascii_measurements_show(struct seq_file *m, void *v)
252245
seq_printf(m, "%2d ", e->pcr);
253246

254247
/* 2nd: template hash */
255-
ima_print_digest(m, e->digests[algo_idx].digest, hash_digest_size[algo]);
248+
ima_print_digest(m, e->digests[algo_idx].digest,
249+
ima_algo_array[algo_idx].digest_size);
256250

257251
/* 3th: template name */
258252
seq_printf(m, " %s", template_name);

0 commit comments

Comments
 (0)