@@ -57,14 +57,21 @@ MODULE_PARM_DESC(ahash_bufsize, "Maximum ahash buffer size");
57
57
static struct crypto_shash * ima_shash_tfm ;
58
58
static struct crypto_ahash * ima_ahash_tfm ;
59
59
60
+ struct ima_algo_desc {
61
+ struct crypto_shash * tfm ;
62
+ enum hash_algo algo ;
63
+ };
64
+
60
65
int ima_sha1_idx __ro_after_init ;
61
66
/*
62
67
* Additional number of slots reserved, as needed, for SHA1
63
68
* and IMA default algo.
64
69
*/
65
- int ima_extra_slots __ro_after_init = 1 ;
70
+ int ima_extra_slots __ro_after_init ;
66
71
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 )
68
75
{
69
76
long rc ;
70
77
@@ -83,26 +90,121 @@ int __init ima_init_crypto(void)
83
90
static struct crypto_shash * ima_alloc_tfm (enum hash_algo algo )
84
91
{
85
92
struct crypto_shash * tfm = ima_shash_tfm ;
86
- int rc ;
93
+ int rc , i ;
87
94
88
95
if (algo < 0 || algo >= HASH_ALGO__LAST )
89
96
algo = ima_hash_algo ;
90
97
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 );
98
110
}
99
111
return tfm ;
100
112
}
101
113
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
+
102
196
static void ima_free_tfm (struct crypto_shash * tfm )
103
197
{
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 );
106
208
}
107
209
108
210
/**
@@ -472,14 +574,14 @@ int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
472
574
*/
473
575
static int ima_calc_field_array_hash_tfm (struct ima_field_data * field_data ,
474
576
struct ima_template_entry * entry ,
475
- struct crypto_shash * tfm )
577
+ int tfm_idx )
476
578
{
477
- SHASH_DESC_ON_STACK (shash , tfm );
579
+ SHASH_DESC_ON_STACK (shash , ima_algo_array [ tfm_idx ]. tfm );
478
580
struct ima_template_desc * td = entry -> template_desc ;
479
581
int num_fields = entry -> template_desc -> num_fields ;
480
582
int rc , i ;
481
583
482
- shash -> tfm = tfm ;
584
+ shash -> tfm = ima_algo_array [ tfm_idx ]. tfm ;
483
585
484
586
rc = crypto_shash_init (shash );
485
587
if (rc != 0 )
@@ -509,26 +611,17 @@ static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
509
611
}
510
612
511
613
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 );
514
615
515
616
return rc ;
516
617
}
517
618
518
619
int ima_calc_field_array_hash (struct ima_field_data * field_data ,
519
620
struct ima_template_entry * entry )
520
621
{
521
- struct crypto_shash * tfm ;
522
622
int rc ;
523
623
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 );
532
625
return rc ;
533
626
}
534
627
0 commit comments