blob: 6aa6408603e33e3e6665bdfe6b84a9862cde5a29 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Roberto Sassuadf53a72013-06-07 12:16:29 +02002/*
3 * Copyright (C) 2013 Politecnico di Torino, Italy
4 * TORSEC group -- http://security.polito.it
5 *
6 * Author: Roberto Sassu <roberto.sassu@polito.it>
7 *
Roberto Sassuadf53a72013-06-07 12:16:29 +02008 * File: ima_template.c
9 * Helpers to manage template descriptors.
10 */
Joe Perches20ee4512014-02-24 13:59:56 -080011
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Mimi Zohar3f23d622016-12-19 16:22:51 -080014#include <linux/rculist.h>
Roberto Sassuadf53a72013-06-07 12:16:29 +020015#include "ima.h"
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020016#include "ima_template_lib.h"
Roberto Sassuadf53a72013-06-07 12:16:29 +020017
Roberto Sassu47fdee62017-05-16 14:53:42 +020018enum header_fields { HDR_PCR, HDR_DIGEST, HDR_TEMPLATE_NAME,
19 HDR_TEMPLATE_DATA, HDR__LAST };
20
Mimi Zohar3f23d622016-12-19 16:22:51 -080021static struct ima_template_desc builtin_templates[] = {
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020022 {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020023 {.name = "ima-ng", .fmt = "d-ng|n-ng"},
24 {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
Prakhar Srivastava86b4da82019-06-23 23:23:30 -070025 {.name = "ima-buf", .fmt = "d-ng|n-ng|buf"},
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -030026 {.name = "ima-modsig", .fmt = "d-ng|n-ng|sig|d-modsig|modsig"},
Roberto Sassuc2426d22014-10-13 14:08:42 +020027 {.name = "", .fmt = ""}, /* placeholder for a custom format */
Roberto Sassuadf53a72013-06-07 12:16:29 +020028};
29
Mimi Zohar3f23d622016-12-19 16:22:51 -080030static LIST_HEAD(defined_templates);
31static DEFINE_SPINLOCK(template_list);
32
Eric Biggersb2724d52018-09-07 13:22:23 -070033static const struct ima_template_field supported_fields[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020034 {.field_id = "d", .field_init = ima_eventdigest_init,
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020035 .field_show = ima_show_template_digest},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020036 {.field_id = "n", .field_init = ima_eventname_init,
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020037 .field_show = ima_show_template_string},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020038 {.field_id = "d-ng", .field_init = ima_eventdigest_ng_init,
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020039 .field_show = ima_show_template_digest_ng},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020040 {.field_id = "n-ng", .field_init = ima_eventname_ng_init,
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020041 .field_show = ima_show_template_string},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020042 {.field_id = "sig", .field_init = ima_eventsig_init,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -040043 .field_show = ima_show_template_sig},
Prakhar Srivastava86b4da82019-06-23 23:23:30 -070044 {.field_id = "buf", .field_init = ima_eventbuf_init,
45 .field_show = ima_show_template_buf},
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -030046 {.field_id = "d-modsig", .field_init = ima_eventdigest_modsig_init,
47 .field_show = ima_show_template_digest_ng},
48 {.field_id = "modsig", .field_init = ima_eventmodsig_init,
49 .field_show = ima_show_template_sig},
Roberto Sassuadf53a72013-06-07 12:16:29 +020050};
Thiago Jung Bauermann337619e2019-06-27 20:25:46 -030051
52/*
53 * Used when restoring measurements carried over from a kexec. 'd' and 'n' don't
54 * need to be accounted for since they shouldn't be defined in the same template
55 * description as 'd-ng' and 'n-ng' respectively.
56 */
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -030057#define MAX_TEMPLATE_NAME_LEN sizeof("d-ng|n-ng|sig|buf|d-modisg|modsig")
Roberto Sassuadf53a72013-06-07 12:16:29 +020058
Roberto Sassua71dc652013-06-07 12:16:33 +020059static struct ima_template_desc *ima_template;
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020060
Thiago Jung Bauermanne5092252019-06-27 23:19:33 -030061/**
62 * ima_template_has_modsig - Check whether template has modsig-related fields.
63 * @ima_template: IMA template to check.
64 *
65 * Tells whether the given template has fields referencing a file's appended
66 * signature.
67 */
68bool ima_template_has_modsig(const struct ima_template_desc *ima_template)
69{
70 int i;
71
72 for (i = 0; i < ima_template->num_fields; i++)
73 if (!strcmp(ima_template->fields[i]->field_id, "modsig") ||
74 !strcmp(ima_template->fields[i]->field_id, "d-modsig"))
75 return true;
76
77 return false;
78}
79
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020080static int __init ima_template_setup(char *str)
81{
82 struct ima_template_desc *template_desc;
83 int template_len = strlen(str);
84
Roberto Sassuc2426d22014-10-13 14:08:42 +020085 if (ima_template)
86 return 1;
87
Mimi Zohar3f23d622016-12-19 16:22:51 -080088 ima_init_template_list();
89
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020090 /*
91 * Verify that a template with the supplied name exists.
92 * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
93 */
94 template_desc = lookup_template_desc(str);
Roberto Sassu71fed2e2014-10-13 14:08:38 +020095 if (!template_desc) {
96 pr_err("template %s not found, using %s\n",
97 str, CONFIG_IMA_DEFAULT_TEMPLATE);
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020098 return 1;
Roberto Sassu71fed2e2014-10-13 14:08:38 +020099 }
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +0200100
101 /*
102 * Verify whether the current hash algorithm is supported
103 * by the 'ima' template.
104 */
105 if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
106 ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
Joe Perches20ee4512014-02-24 13:59:56 -0800107 pr_err("template does not support hash alg\n");
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +0200108 return 1;
109 }
110
111 ima_template = template_desc;
112 return 1;
113}
114__setup("ima_template=", ima_template_setup);
Roberto Sassua71dc652013-06-07 12:16:33 +0200115
Roberto Sassuc2426d22014-10-13 14:08:42 +0200116static int __init ima_template_fmt_setup(char *str)
117{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800118 int num_templates = ARRAY_SIZE(builtin_templates);
Roberto Sassuc2426d22014-10-13 14:08:42 +0200119
120 if (ima_template)
121 return 1;
122
123 if (template_desc_init_fields(str, NULL, NULL) < 0) {
124 pr_err("format string '%s' not valid, using template %s\n",
125 str, CONFIG_IMA_DEFAULT_TEMPLATE);
126 return 1;
127 }
128
Mimi Zohar3f23d622016-12-19 16:22:51 -0800129 builtin_templates[num_templates - 1].fmt = str;
130 ima_template = builtin_templates + num_templates - 1;
131
Roberto Sassuc2426d22014-10-13 14:08:42 +0200132 return 1;
133}
134__setup("ima_template_fmt=", ima_template_fmt_setup);
135
Matthew Garrett19453ce02019-06-19 15:46:11 -0700136struct ima_template_desc *lookup_template_desc(const char *name)
Roberto Sassua71dc652013-06-07 12:16:33 +0200137{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800138 struct ima_template_desc *template_desc;
139 int found = 0;
Roberto Sassua71dc652013-06-07 12:16:33 +0200140
Mimi Zohar3f23d622016-12-19 16:22:51 -0800141 rcu_read_lock();
142 list_for_each_entry_rcu(template_desc, &defined_templates, list) {
143 if ((strcmp(template_desc->name, name) == 0) ||
144 (strcmp(template_desc->fmt, name) == 0)) {
145 found = 1;
146 break;
147 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200148 }
Mimi Zohar3f23d622016-12-19 16:22:51 -0800149 rcu_read_unlock();
150 return found ? template_desc : NULL;
Roberto Sassua71dc652013-06-07 12:16:33 +0200151}
152
Eric Biggersb2724d52018-09-07 13:22:23 -0700153static const struct ima_template_field *
154lookup_template_field(const char *field_id)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200155{
156 int i;
157
158 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
159 if (strncmp(supported_fields[i].field_id, field_id,
160 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
161 return &supported_fields[i];
162 return NULL;
163}
164
Roberto Sassudbc335d2013-11-25 20:18:52 +0100165static int template_fmt_size(const char *template_fmt)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200166{
167 char c;
168 int template_fmt_len = strlen(template_fmt);
169 int i = 0, j = 0;
170
171 while (i < template_fmt_len) {
172 c = template_fmt[i];
173 if (c == '|')
174 j++;
175 i++;
176 }
177
178 return j + 1;
179}
180
Matthew Garrett19453ce02019-06-19 15:46:11 -0700181int template_desc_init_fields(const char *template_fmt,
182 const struct ima_template_field ***fields,
183 int *num_fields)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200184{
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200185 const char *template_fmt_ptr;
Eric Biggersb2724d52018-09-07 13:22:23 -0700186 const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
Mimi Zoharc7d09362016-12-19 16:22:54 -0800187 int template_num_fields;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200188 int i, len;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200189
Mimi Zoharc7d09362016-12-19 16:22:54 -0800190 if (num_fields && *num_fields > 0) /* already initialized? */
191 return 0;
192
193 template_num_fields = template_fmt_size(template_fmt);
194
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200195 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
196 pr_err("format string '%s' contains too many fields\n",
197 template_fmt);
Roberto Sassuadf53a72013-06-07 12:16:29 +0200198 return -EINVAL;
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200199 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200200
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200201 for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
202 i++, template_fmt_ptr += len + 1) {
203 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
Roberto Sassuadf53a72013-06-07 12:16:29 +0200204
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200205 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
206 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
207 pr_err("Invalid field with length %d\n", len);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200208 return -EINVAL;
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200209 }
210
211 memcpy(tmp_field_id, template_fmt_ptr, len);
212 tmp_field_id[len] = '\0';
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200213 found_fields[i] = lookup_template_field(tmp_field_id);
214 if (!found_fields[i]) {
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200215 pr_err("field '%s' not found\n", tmp_field_id);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200216 return -ENOENT;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200217 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200218 }
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200219
Roberto Sassuc2426d22014-10-13 14:08:42 +0200220 if (fields && num_fields) {
221 *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
222 if (*fields == NULL)
223 return -ENOMEM;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200224
Roberto Sassuc2426d22014-10-13 14:08:42 +0200225 memcpy(*fields, found_fields, i * sizeof(*fields));
226 *num_fields = i;
227 }
228
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200229 return 0;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200230}
231
Mimi Zohar3f23d622016-12-19 16:22:51 -0800232void ima_init_template_list(void)
233{
234 int i;
235
236 if (!list_empty(&defined_templates))
237 return;
238
239 spin_lock(&template_list);
240 for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
241 list_add_tail_rcu(&builtin_templates[i].list,
242 &defined_templates);
243 }
244 spin_unlock(&template_list);
245}
246
Roberto Sassua71dc652013-06-07 12:16:33 +0200247struct ima_template_desc *ima_template_desc_current(void)
248{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800249 if (!ima_template) {
250 ima_init_template_list();
Mimi Zohar42865872013-06-07 12:16:34 +0200251 ima_template =
252 lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
Mimi Zohar3f23d622016-12-19 16:22:51 -0800253 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200254 return ima_template;
255}
256
Dmitry Kasatkine4a9c512014-09-03 10:19:58 +0300257int __init ima_init_template(void)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200258{
Dmitry Kasatkinb4148db2014-05-08 11:23:53 +0300259 struct ima_template_desc *template = ima_template_desc_current();
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200260 int result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200261
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200262 result = template_desc_init_fields(template->fmt,
263 &(template->fields),
264 &(template->num_fields));
265 if (result < 0)
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200266 pr_err("template %s init failed, result: %d\n",
267 (strlen(template->name) ?
268 template->name : template->fmt), result);
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200269
270 return result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200271}
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800272
Mimi Zoharc7d09362016-12-19 16:22:54 -0800273static struct ima_template_desc *restore_template_fmt(char *template_name)
274{
275 struct ima_template_desc *template_desc = NULL;
276 int ret;
277
278 ret = template_desc_init_fields(template_name, NULL, NULL);
279 if (ret < 0) {
280 pr_err("attempting to initialize the template \"%s\" failed\n",
281 template_name);
282 goto out;
283 }
284
285 template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
286 if (!template_desc)
287 goto out;
288
289 template_desc->name = "";
290 template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
291 if (!template_desc->fmt)
292 goto out;
293
294 spin_lock(&template_list);
295 list_add_tail_rcu(&template_desc->list, &defined_templates);
296 spin_unlock(&template_list);
297out:
298 return template_desc;
299}
300
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800301static int ima_restore_template_data(struct ima_template_desc *template_desc,
302 void *template_data,
303 int template_data_size,
304 struct ima_template_entry **entry)
305{
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800306 int ret = 0;
307 int i;
308
Gustavo A. R. Silvafa5b5712019-05-29 11:53:43 -0500309 *entry = kzalloc(struct_size(*entry, template_data,
310 template_desc->num_fields), GFP_NOFS);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800311 if (!*entry)
312 return -ENOMEM;
313
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200314 ret = ima_parse_buf(template_data, template_data + template_data_size,
315 NULL, template_desc->num_fields,
316 (*entry)->template_data, NULL, NULL,
317 ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
318 if (ret < 0) {
319 kfree(*entry);
320 return ret;
321 }
322
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800323 (*entry)->template_desc = template_desc;
324 for (i = 0; i < template_desc->num_fields; i++) {
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200325 struct ima_field_data *field_data = &(*entry)->template_data[i];
326 u8 *data = field_data->data;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800327
328 (*entry)->template_data[i].data =
329 kzalloc(field_data->len + 1, GFP_KERNEL);
330 if (!(*entry)->template_data[i].data) {
331 ret = -ENOMEM;
332 break;
333 }
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200334 memcpy((*entry)->template_data[i].data, data, field_data->len);
335 (*entry)->template_data_len += sizeof(field_data->len);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800336 (*entry)->template_data_len += field_data->len;
337 }
338
339 if (ret < 0) {
340 ima_free_template_entry(*entry);
341 *entry = NULL;
342 }
343
344 return ret;
345}
346
347/* Restore the serialized binary measurement list without extending PCRs. */
348int ima_restore_measurement_list(loff_t size, void *buf)
349{
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800350 char template_name[MAX_TEMPLATE_NAME_LEN];
351
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800352 struct ima_kexec_hdr *khdr = buf;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200353 struct ima_field_data hdr[HDR__LAST] = {
354 [HDR_PCR] = {.len = sizeof(u32)},
355 [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
356 };
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800357
358 void *bufp = buf + sizeof(*khdr);
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800359 void *bufendp;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800360 struct ima_template_entry *entry;
361 struct ima_template_desc *template_desc;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200362 DECLARE_BITMAP(hdr_mask, HDR__LAST);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800363 unsigned long count = 0;
364 int ret = 0;
365
366 if (!buf || size < sizeof(*khdr))
367 return 0;
368
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800369 if (ima_canonical_fmt) {
370 khdr->version = le16_to_cpu(khdr->version);
371 khdr->count = le64_to_cpu(khdr->count);
372 khdr->buffer_size = le64_to_cpu(khdr->buffer_size);
373 }
374
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800375 if (khdr->version != 1) {
376 pr_err("attempting to restore a incompatible measurement list");
377 return -EINVAL;
378 }
379
380 if (khdr->count > ULONG_MAX - 1) {
381 pr_err("attempting to restore too many measurements");
382 return -EINVAL;
383 }
384
Roberto Sassu47fdee62017-05-16 14:53:42 +0200385 bitmap_zero(hdr_mask, HDR__LAST);
386 bitmap_set(hdr_mask, HDR_PCR, 1);
387 bitmap_set(hdr_mask, HDR_DIGEST, 1);
388
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800389 /*
390 * ima kexec buffer prefix: version, buffer size, count
391 * v1 format: pcr, digest, template-name-len, template-name,
392 * template-data-size, template-data
393 */
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800394 bufendp = buf + khdr->buffer_size;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800395 while ((bufp < bufendp) && (count++ < khdr->count)) {
Roberto Sassu47fdee62017-05-16 14:53:42 +0200396 int enforce_mask = ENFORCE_FIELDS;
397
398 enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
399 ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
400 hdr_mask, enforce_mask, "entry header");
401 if (ret < 0)
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800402 break;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800403
Roberto Sassu47fdee62017-05-16 14:53:42 +0200404 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800405 pr_err("attempting to restore a template name that is too long\n");
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800406 ret = -EINVAL;
407 break;
408 }
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800409
410 /* template name is not null terminated */
Roberto Sassu47fdee62017-05-16 14:53:42 +0200411 memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
412 hdr[HDR_TEMPLATE_NAME].len);
413 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800414
415 if (strcmp(template_name, "ima") == 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800416 pr_err("attempting to restore an unsupported template \"%s\" failed\n",
417 template_name);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800418 ret = -EINVAL;
419 break;
420 }
421
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800422 template_desc = lookup_template_desc(template_name);
423 if (!template_desc) {
Mimi Zoharc7d09362016-12-19 16:22:54 -0800424 template_desc = restore_template_fmt(template_name);
425 if (!template_desc)
426 break;
427 }
428
429 /*
430 * Only the running system's template format is initialized
431 * on boot. As needed, initialize the other template formats.
432 */
433 ret = template_desc_init_fields(template_desc->fmt,
434 &(template_desc->fields),
435 &(template_desc->num_fields));
436 if (ret < 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800437 pr_err("attempting to restore the template fmt \"%s\" failed\n",
438 template_desc->fmt);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800439 ret = -EINVAL;
440 break;
441 }
442
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800443 ret = ima_restore_template_data(template_desc,
Roberto Sassu47fdee62017-05-16 14:53:42 +0200444 hdr[HDR_TEMPLATE_DATA].data,
445 hdr[HDR_TEMPLATE_DATA].len,
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800446 &entry);
447 if (ret < 0)
448 break;
449
Roberto Sassu47fdee62017-05-16 14:53:42 +0200450 memcpy(entry->digest, hdr[HDR_DIGEST].data,
451 hdr[HDR_DIGEST].len);
452 entry->pcr = !ima_canonical_fmt ? *(hdr[HDR_PCR].data) :
453 le32_to_cpu(*(hdr[HDR_PCR].data));
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800454 ret = ima_restore_measurement_entry(entry);
455 if (ret < 0)
456 break;
457
458 }
459 return ret;
460}