Skip to content

Commit 6d94809

Browse files
robertosassumimizohar
authored andcommitted
ima: Allocate and initialize tfm for each PCR bank
This patch creates a crypto_shash structure for each allocated PCR bank and for SHA1 if a bank with that algorithm is not currently allocated. Reported-by: kbuild test robot <[email protected]> Signed-off-by: Roberto Sassu <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent aa724fe commit 6d94809

File tree

1 file changed

+119
-26
lines changed

1 file changed

+119
-26
lines changed

security/integrity/ima/ima_crypto.c

Lines changed: 119 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,21 @@ MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size");
5757
static struct crypto_shash *ima_shash_tfm;
5858
static struct crypto_ahash *ima_ahash_tfm;
5959

60+
struct ima_algo_desc {
61+
struct crypto_shash *tfm;
62+
enum hash_algo algo;
63+
};
64+
6065
int ima_sha1_idx __ro_after_init;
6166
/*
6267
* Additional number of slots reserved, as needed, for SHA1
6368
* and IMA default algo.
6469
*/
65-
int ima_extra_slots __ro_after_init = 1;
70+
int ima_extra_slots __ro_after_init;
6671

67-
int __init ima_init_crypto(void)
72+
static struct ima_algo_desc *ima_algo_array;
73+
74+
static int __init ima_init_ima_crypto(void)
6875
{
6976
long rc;
7077

@@ -83,26 +90,121 @@ int __init ima_init_crypto(void)
8390
static struct crypto_shash *ima_alloc_tfm(enum hash_algo algo)
8491
{
8592
struct crypto_shash *tfm = ima_shash_tfm;
86-
int rc;
93+
int rc, i;
8794

8895
if (algo < 0 || algo >= HASH_ALGO__LAST)
8996
algo = ima_hash_algo;
9097

91-
if (algo != ima_hash_algo) {
92-
tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
93-
if (IS_ERR(tfm)) {
94-
rc = PTR_ERR(tfm);
95-
pr_err("Can not allocate %s (reason: %d)\n",
96-
hash_algo_name[algo], rc);
97-
}
98+
if (algo == ima_hash_algo)
99+
return tfm;
100+
101+
for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++)
102+
if (ima_algo_array[i].tfm && ima_algo_array[i].algo == algo)
103+
return ima_algo_array[i].tfm;
104+
105+
tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
106+
if (IS_ERR(tfm)) {
107+
rc = PTR_ERR(tfm);
108+
pr_err("Can not allocate %s (reason: %d)\n",
109+
hash_algo_name[algo], rc);
98110
}
99111
return tfm;
100112
}
101113

114+
int __init ima_init_crypto(void)
115+
{
116+
enum hash_algo algo;
117+
long rc;
118+
int i;
119+
120+
rc = ima_init_ima_crypto();
121+
if (rc)
122+
return rc;
123+
124+
ima_sha1_idx = -1;
125+
126+
for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
127+
algo = ima_tpm_chip->allocated_banks[i].crypto_id;
128+
if (algo == HASH_ALGO_SHA1)
129+
ima_sha1_idx = i;
130+
}
131+
132+
if (ima_sha1_idx < 0)
133+
ima_sha1_idx = NR_BANKS(ima_tpm_chip) + ima_extra_slots++;
134+
135+
ima_algo_array = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
136+
sizeof(*ima_algo_array), GFP_KERNEL);
137+
if (!ima_algo_array) {
138+
rc = -ENOMEM;
139+
goto out;
140+
}
141+
142+
for (i = 0; i < NR_BANKS(ima_tpm_chip); i++) {
143+
algo = ima_tpm_chip->allocated_banks[i].crypto_id;
144+
ima_algo_array[i].algo = algo;
145+
146+
/* unknown TPM algorithm */
147+
if (algo == HASH_ALGO__LAST)
148+
continue;
149+
150+
if (algo == ima_hash_algo) {
151+
ima_algo_array[i].tfm = ima_shash_tfm;
152+
continue;
153+
}
154+
155+
ima_algo_array[i].tfm = ima_alloc_tfm(algo);
156+
if (IS_ERR(ima_algo_array[i].tfm)) {
157+
if (algo == HASH_ALGO_SHA1) {
158+
rc = PTR_ERR(ima_algo_array[i].tfm);
159+
ima_algo_array[i].tfm = NULL;
160+
goto out_array;
161+
}
162+
163+
ima_algo_array[i].tfm = NULL;
164+
}
165+
}
166+
167+
if (ima_sha1_idx >= NR_BANKS(ima_tpm_chip)) {
168+
if (ima_hash_algo == HASH_ALGO_SHA1) {
169+
ima_algo_array[ima_sha1_idx].tfm = ima_shash_tfm;
170+
} else {
171+
ima_algo_array[ima_sha1_idx].tfm =
172+
ima_alloc_tfm(HASH_ALGO_SHA1);
173+
if (IS_ERR(ima_algo_array[ima_sha1_idx].tfm)) {
174+
rc = PTR_ERR(ima_algo_array[ima_sha1_idx].tfm);
175+
goto out_array;
176+
}
177+
}
178+
179+
ima_algo_array[ima_sha1_idx].algo = HASH_ALGO_SHA1;
180+
}
181+
182+
return 0;
183+
out_array:
184+
for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++) {
185+
if (!ima_algo_array[i].tfm ||
186+
ima_algo_array[i].tfm == ima_shash_tfm)
187+
continue;
188+
189+
crypto_free_shash(ima_algo_array[i].tfm);
190+
}
191+
out:
192+
crypto_free_shash(ima_shash_tfm);
193+
return rc;
194+
}
195+
102196
static void ima_free_tfm(struct crypto_shash *tfm)
103197
{
104-
if (tfm != ima_shash_tfm)
105-
crypto_free_shash(tfm);
198+
int i;
199+
200+
if (tfm == ima_shash_tfm)
201+
return;
202+
203+
for (i = 0; i < NR_BANKS(ima_tpm_chip) + ima_extra_slots; i++)
204+
if (ima_algo_array[i].tfm == tfm)
205+
return;
206+
207+
crypto_free_shash(tfm);
106208
}
107209

108210
/**
@@ -472,14 +574,14 @@ int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
472574
*/
473575
static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
474576
struct ima_template_entry *entry,
475-
struct crypto_shash *tfm)
577+
int tfm_idx)
476578
{
477-
SHASH_DESC_ON_STACK(shash, tfm);
579+
SHASH_DESC_ON_STACK(shash, ima_algo_array[tfm_idx].tfm);
478580
struct ima_template_desc *td = entry->template_desc;
479581
int num_fields = entry->template_desc->num_fields;
480582
int rc, i;
481583

482-
shash->tfm = tfm;
584+
shash->tfm = ima_algo_array[tfm_idx].tfm;
483585

484586
rc = crypto_shash_init(shash);
485587
if (rc != 0)
@@ -509,26 +611,17 @@ static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
509611
}
510612

511613
if (!rc)
512-
rc = crypto_shash_final(shash,
513-
entry->digests[ima_sha1_idx].digest);
614+
rc = crypto_shash_final(shash, entry->digests[tfm_idx].digest);
514615

515616
return rc;
516617
}
517618

518619
int ima_calc_field_array_hash(struct ima_field_data *field_data,
519620
struct ima_template_entry *entry)
520621
{
521-
struct crypto_shash *tfm;
522622
int rc;
523623

524-
tfm = ima_alloc_tfm(HASH_ALGO_SHA1);
525-
if (IS_ERR(tfm))
526-
return PTR_ERR(tfm);
527-
528-
rc = ima_calc_field_array_hash_tfm(field_data, entry, tfm);
529-
530-
ima_free_tfm(tfm);
531-
624+
rc = ima_calc_field_array_hash_tfm(field_data, entry, ima_sha1_idx);
532625
return rc;
533626
}
534627

0 commit comments

Comments
 (0)