@@ -155,9 +155,14 @@ static int template_desc_init_fields(const char *template_fmt,
155
155
{
156
156
const char * template_fmt_ptr ;
157
157
struct ima_template_field * found_fields [IMA_TEMPLATE_NUM_FIELDS_MAX ];
158
- int template_num_fields = template_fmt_size ( template_fmt ) ;
158
+ int template_num_fields ;
159
159
int i , len ;
160
160
161
+ if (num_fields && * num_fields > 0 ) /* already initialized? */
162
+ return 0 ;
163
+
164
+ template_num_fields = template_fmt_size (template_fmt );
165
+
161
166
if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX ) {
162
167
pr_err ("format string '%s' contains too many fields\n" ,
163
168
template_fmt );
@@ -236,6 +241,34 @@ int __init ima_init_template(void)
236
241
return result ;
237
242
}
238
243
244
+ static struct ima_template_desc * restore_template_fmt (char * template_name )
245
+ {
246
+ struct ima_template_desc * template_desc = NULL ;
247
+ int ret ;
248
+
249
+ ret = template_desc_init_fields (template_name , NULL , NULL );
250
+ if (ret < 0 ) {
251
+ pr_err ("attempting to initialize the template \"%s\" failed\n" ,
252
+ template_name );
253
+ goto out ;
254
+ }
255
+
256
+ template_desc = kzalloc (sizeof (* template_desc ), GFP_KERNEL );
257
+ if (!template_desc )
258
+ goto out ;
259
+
260
+ template_desc -> name = "" ;
261
+ template_desc -> fmt = kstrdup (template_name , GFP_KERNEL );
262
+ if (!template_desc -> fmt )
263
+ goto out ;
264
+
265
+ spin_lock (& template_list );
266
+ list_add_tail_rcu (& template_desc -> list , & defined_templates );
267
+ spin_unlock (& template_list );
268
+ out :
269
+ return template_desc ;
270
+ }
271
+
239
272
static int ima_restore_template_data (struct ima_template_desc * template_desc ,
240
273
void * template_data ,
241
274
int template_data_size ,
@@ -373,10 +406,23 @@ int ima_restore_measurement_list(loff_t size, void *buf)
373
406
break ;
374
407
}
375
408
376
- /* get template format */
377
409
template_desc = lookup_template_desc (template_name );
378
410
if (!template_desc ) {
379
- pr_err ("template \"%s\" not found\n" , template_name );
411
+ template_desc = restore_template_fmt (template_name );
412
+ if (!template_desc )
413
+ break ;
414
+ }
415
+
416
+ /*
417
+ * Only the running system's template format is initialized
418
+ * on boot. As needed, initialize the other template formats.
419
+ */
420
+ ret = template_desc_init_fields (template_desc -> fmt ,
421
+ & (template_desc -> fields ),
422
+ & (template_desc -> num_fields ));
423
+ if (ret < 0 ) {
424
+ pr_err ("attempting to restore the template fmt \"%s\" \
425
+ failed\n" , template_desc -> fmt );
380
426
ret = - EINVAL ;
381
427
break ;
382
428
}
0 commit comments