blob: da8cf2d3fd647fb87c82ae1b5e782fa226d85d9e [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
Alexander A. Klimovc9fecf52020-07-05 23:45:12 +02004 * TORSEC group -- https://security.polito.it
Roberto Sassuadf53a72013-06-07 12:16:29 +02005 *
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
Mimi Zohar3f23d622016-12-19 16:22:51 -080012#include <linux/rculist.h>
Roberto Sassuadf53a72013-06-07 12:16:29 +020013#include "ima.h"
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020014#include "ima_template_lib.h"
Roberto Sassuadf53a72013-06-07 12:16:29 +020015
Roberto Sassu47fdee62017-05-16 14:53:42 +020016enum header_fields { HDR_PCR, HDR_DIGEST, HDR_TEMPLATE_NAME,
17 HDR_TEMPLATE_DATA, HDR__LAST };
18
Mimi Zohar3f23d622016-12-19 16:22:51 -080019static struct ima_template_desc builtin_templates[] = {
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020020 {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020021 {.name = "ima-ng", .fmt = "d-ng|n-ng"},
22 {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
Prakhar Srivastava86b4da82019-06-23 23:23:30 -070023 {.name = "ima-buf", .fmt = "d-ng|n-ng|buf"},
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -030024 {.name = "ima-modsig", .fmt = "d-ng|n-ng|sig|d-modsig|modsig"},
Roberto Sassuc2426d22014-10-13 14:08:42 +020025 {.name = "", .fmt = ""}, /* placeholder for a custom format */
Roberto Sassuadf53a72013-06-07 12:16:29 +020026};
27
Mimi Zohar3f23d622016-12-19 16:22:51 -080028static LIST_HEAD(defined_templates);
29static DEFINE_SPINLOCK(template_list);
30
Eric Biggersb2724d52018-09-07 13:22:23 -070031static const struct ima_template_field supported_fields[] = {
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020032 {.field_id = "d", .field_init = ima_eventdigest_init,
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020033 .field_show = ima_show_template_digest},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020034 {.field_id = "n", .field_init = ima_eventname_init,
Roberto Sassu3ce1217d2013-06-07 12:16:30 +020035 .field_show = ima_show_template_string},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020036 {.field_id = "d-ng", .field_init = ima_eventdigest_ng_init,
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020037 .field_show = ima_show_template_digest_ng},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020038 {.field_id = "n-ng", .field_init = ima_eventname_ng_init,
Roberto Sassu4d7aeee72013-06-07 12:16:32 +020039 .field_show = ima_show_template_string},
Dmitry Kasatkin2bb930a2014-03-04 18:04:20 +020040 {.field_id = "sig", .field_init = ima_eventsig_init,
Mimi Zoharbcbc9b0c2013-07-23 11:15:00 -040041 .field_show = ima_show_template_sig},
Prakhar Srivastava86b4da82019-06-23 23:23:30 -070042 {.field_id = "buf", .field_init = ima_eventbuf_init,
43 .field_show = ima_show_template_buf},
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -030044 {.field_id = "d-modsig", .field_init = ima_eventdigest_modsig_init,
45 .field_show = ima_show_template_digest_ng},
46 {.field_id = "modsig", .field_init = ima_eventmodsig_init,
47 .field_show = ima_show_template_sig},
Roberto Sassuadf53a72013-06-07 12:16:29 +020048};
Thiago Jung Bauermann337619e2019-06-27 20:25:46 -030049
50/*
51 * Used when restoring measurements carried over from a kexec. 'd' and 'n' don't
52 * need to be accounted for since they shouldn't be defined in the same template
53 * description as 'd-ng' and 'n-ng' respectively.
54 */
Thiago Jung Bauermann3878d502019-06-27 23:19:32 -030055#define MAX_TEMPLATE_NAME_LEN sizeof("d-ng|n-ng|sig|buf|d-modisg|modsig")
Roberto Sassuadf53a72013-06-07 12:16:29 +020056
Roberto Sassua71dc652013-06-07 12:16:33 +020057static struct ima_template_desc *ima_template;
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020058
Thiago Jung Bauermanne5092252019-06-27 23:19:33 -030059/**
60 * ima_template_has_modsig - Check whether template has modsig-related fields.
61 * @ima_template: IMA template to check.
62 *
63 * Tells whether the given template has fields referencing a file's appended
64 * signature.
65 */
66bool ima_template_has_modsig(const struct ima_template_desc *ima_template)
67{
68 int i;
69
70 for (i = 0; i < ima_template->num_fields; i++)
71 if (!strcmp(ima_template->fields[i]->field_id, "modsig") ||
72 !strcmp(ima_template->fields[i]->field_id, "d-modsig"))
73 return true;
74
75 return false;
76}
77
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020078static int __init ima_template_setup(char *str)
79{
80 struct ima_template_desc *template_desc;
81 int template_len = strlen(str);
82
Roberto Sassuc2426d22014-10-13 14:08:42 +020083 if (ima_template)
84 return 1;
85
Mimi Zohar3f23d622016-12-19 16:22:51 -080086 ima_init_template_list();
87
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020088 /*
89 * Verify that a template with the supplied name exists.
90 * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
91 */
92 template_desc = lookup_template_desc(str);
Roberto Sassu71fed2e2014-10-13 14:08:38 +020093 if (!template_desc) {
94 pr_err("template %s not found, using %s\n",
95 str, CONFIG_IMA_DEFAULT_TEMPLATE);
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020096 return 1;
Roberto Sassu71fed2e2014-10-13 14:08:38 +020097 }
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020098
99 /*
100 * Verify whether the current hash algorithm is supported
101 * by the 'ima' template.
102 */
103 if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
104 ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
Joe Perches20ee4512014-02-24 13:59:56 -0800105 pr_err("template does not support hash alg\n");
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +0200106 return 1;
107 }
108
109 ima_template = template_desc;
110 return 1;
111}
112__setup("ima_template=", ima_template_setup);
Roberto Sassua71dc652013-06-07 12:16:33 +0200113
Roberto Sassuc2426d22014-10-13 14:08:42 +0200114static int __init ima_template_fmt_setup(char *str)
115{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800116 int num_templates = ARRAY_SIZE(builtin_templates);
Roberto Sassuc2426d22014-10-13 14:08:42 +0200117
118 if (ima_template)
119 return 1;
120
121 if (template_desc_init_fields(str, NULL, NULL) < 0) {
122 pr_err("format string '%s' not valid, using template %s\n",
123 str, CONFIG_IMA_DEFAULT_TEMPLATE);
124 return 1;
125 }
126
Mimi Zohar3f23d622016-12-19 16:22:51 -0800127 builtin_templates[num_templates - 1].fmt = str;
128 ima_template = builtin_templates + num_templates - 1;
129
Roberto Sassuc2426d22014-10-13 14:08:42 +0200130 return 1;
131}
132__setup("ima_template_fmt=", ima_template_fmt_setup);
133
Matthew Garrett19453ce02019-06-19 15:46:11 -0700134struct ima_template_desc *lookup_template_desc(const char *name)
Roberto Sassua71dc652013-06-07 12:16:33 +0200135{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800136 struct ima_template_desc *template_desc;
137 int found = 0;
Roberto Sassua71dc652013-06-07 12:16:33 +0200138
Mimi Zohar3f23d622016-12-19 16:22:51 -0800139 rcu_read_lock();
140 list_for_each_entry_rcu(template_desc, &defined_templates, list) {
141 if ((strcmp(template_desc->name, name) == 0) ||
142 (strcmp(template_desc->fmt, name) == 0)) {
143 found = 1;
144 break;
145 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200146 }
Mimi Zohar3f23d622016-12-19 16:22:51 -0800147 rcu_read_unlock();
148 return found ? template_desc : NULL;
Roberto Sassua71dc652013-06-07 12:16:33 +0200149}
150
Eric Biggersb2724d52018-09-07 13:22:23 -0700151static const struct ima_template_field *
152lookup_template_field(const char *field_id)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200153{
154 int i;
155
156 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
157 if (strncmp(supported_fields[i].field_id, field_id,
158 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
159 return &supported_fields[i];
160 return NULL;
161}
162
Roberto Sassudbc335d2013-11-25 20:18:52 +0100163static int template_fmt_size(const char *template_fmt)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200164{
165 char c;
166 int template_fmt_len = strlen(template_fmt);
167 int i = 0, j = 0;
168
169 while (i < template_fmt_len) {
170 c = template_fmt[i];
171 if (c == '|')
172 j++;
173 i++;
174 }
175
176 return j + 1;
177}
178
Matthew Garrett19453ce02019-06-19 15:46:11 -0700179int template_desc_init_fields(const char *template_fmt,
180 const struct ima_template_field ***fields,
181 int *num_fields)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200182{
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200183 const char *template_fmt_ptr;
Eric Biggersb2724d52018-09-07 13:22:23 -0700184 const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
Mimi Zoharc7d09362016-12-19 16:22:54 -0800185 int template_num_fields;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200186 int i, len;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200187
Mimi Zoharc7d09362016-12-19 16:22:54 -0800188 if (num_fields && *num_fields > 0) /* already initialized? */
189 return 0;
190
191 template_num_fields = template_fmt_size(template_fmt);
192
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200193 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
194 pr_err("format string '%s' contains too many fields\n",
195 template_fmt);
Roberto Sassuadf53a72013-06-07 12:16:29 +0200196 return -EINVAL;
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200197 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200198
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200199 for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
200 i++, template_fmt_ptr += len + 1) {
201 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
Roberto Sassuadf53a72013-06-07 12:16:29 +0200202
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200203 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
204 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
205 pr_err("Invalid field with length %d\n", len);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200206 return -EINVAL;
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200207 }
208
209 memcpy(tmp_field_id, template_fmt_ptr, len);
210 tmp_field_id[len] = '\0';
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200211 found_fields[i] = lookup_template_field(tmp_field_id);
212 if (!found_fields[i]) {
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200213 pr_err("field '%s' not found\n", tmp_field_id);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200214 return -ENOENT;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200215 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200216 }
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200217
Roberto Sassuc2426d22014-10-13 14:08:42 +0200218 if (fields && num_fields) {
219 *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
220 if (*fields == NULL)
221 return -ENOMEM;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200222
Roberto Sassuc2426d22014-10-13 14:08:42 +0200223 memcpy(*fields, found_fields, i * sizeof(*fields));
224 *num_fields = i;
225 }
226
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200227 return 0;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200228}
229
Mimi Zohar3f23d622016-12-19 16:22:51 -0800230void ima_init_template_list(void)
231{
232 int i;
233
234 if (!list_empty(&defined_templates))
235 return;
236
237 spin_lock(&template_list);
238 for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
239 list_add_tail_rcu(&builtin_templates[i].list,
240 &defined_templates);
241 }
242 spin_unlock(&template_list);
243}
244
Roberto Sassua71dc652013-06-07 12:16:33 +0200245struct ima_template_desc *ima_template_desc_current(void)
246{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800247 if (!ima_template) {
248 ima_init_template_list();
Mimi Zohar42865872013-06-07 12:16:34 +0200249 ima_template =
250 lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
Mimi Zohar3f23d622016-12-19 16:22:51 -0800251 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200252 return ima_template;
253}
254
Dmitry Kasatkine4a9c512014-09-03 10:19:58 +0300255int __init ima_init_template(void)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200256{
Dmitry Kasatkinb4148db2014-05-08 11:23:53 +0300257 struct ima_template_desc *template = ima_template_desc_current();
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200258 int result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200259
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200260 result = template_desc_init_fields(template->fmt,
261 &(template->fields),
262 &(template->num_fields));
263 if (result < 0)
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200264 pr_err("template %s init failed, result: %d\n",
265 (strlen(template->name) ?
266 template->name : template->fmt), result);
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200267
268 return result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200269}
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800270
Mimi Zoharc7d09362016-12-19 16:22:54 -0800271static struct ima_template_desc *restore_template_fmt(char *template_name)
272{
273 struct ima_template_desc *template_desc = NULL;
274 int ret;
275
276 ret = template_desc_init_fields(template_name, NULL, NULL);
277 if (ret < 0) {
278 pr_err("attempting to initialize the template \"%s\" failed\n",
279 template_name);
280 goto out;
281 }
282
283 template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
284 if (!template_desc)
285 goto out;
286
287 template_desc->name = "";
288 template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
289 if (!template_desc->fmt)
290 goto out;
291
292 spin_lock(&template_list);
293 list_add_tail_rcu(&template_desc->list, &defined_templates);
294 spin_unlock(&template_list);
295out:
296 return template_desc;
297}
298
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800299static int ima_restore_template_data(struct ima_template_desc *template_desc,
300 void *template_data,
301 int template_data_size,
302 struct ima_template_entry **entry)
303{
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800304 int ret = 0;
305 int i;
306
Gustavo A. R. Silvafa5b5712019-05-29 11:53:43 -0500307 *entry = kzalloc(struct_size(*entry, template_data,
308 template_desc->num_fields), GFP_NOFS);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800309 if (!*entry)
310 return -ENOMEM;
311
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200312 ret = ima_parse_buf(template_data, template_data + template_data_size,
313 NULL, template_desc->num_fields,
314 (*entry)->template_data, NULL, NULL,
315 ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
316 if (ret < 0) {
317 kfree(*entry);
318 return ret;
319 }
320
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800321 (*entry)->template_desc = template_desc;
322 for (i = 0; i < template_desc->num_fields; i++) {
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200323 struct ima_field_data *field_data = &(*entry)->template_data[i];
324 u8 *data = field_data->data;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800325
326 (*entry)->template_data[i].data =
327 kzalloc(field_data->len + 1, GFP_KERNEL);
328 if (!(*entry)->template_data[i].data) {
329 ret = -ENOMEM;
330 break;
331 }
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200332 memcpy((*entry)->template_data[i].data, data, field_data->len);
333 (*entry)->template_data_len += sizeof(field_data->len);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800334 (*entry)->template_data_len += field_data->len;
335 }
336
337 if (ret < 0) {
338 ima_free_template_entry(*entry);
339 *entry = NULL;
340 }
341
342 return ret;
343}
344
345/* Restore the serialized binary measurement list without extending PCRs. */
346int ima_restore_measurement_list(loff_t size, void *buf)
347{
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800348 char template_name[MAX_TEMPLATE_NAME_LEN];
349
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800350 struct ima_kexec_hdr *khdr = buf;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200351 struct ima_field_data hdr[HDR__LAST] = {
352 [HDR_PCR] = {.len = sizeof(u32)},
353 [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
354 };
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800355
356 void *bufp = buf + sizeof(*khdr);
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800357 void *bufendp;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800358 struct ima_template_entry *entry;
359 struct ima_template_desc *template_desc;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200360 DECLARE_BITMAP(hdr_mask, HDR__LAST);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800361 unsigned long count = 0;
362 int ret = 0;
363
364 if (!buf || size < sizeof(*khdr))
365 return 0;
366
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800367 if (ima_canonical_fmt) {
368 khdr->version = le16_to_cpu(khdr->version);
369 khdr->count = le64_to_cpu(khdr->count);
370 khdr->buffer_size = le64_to_cpu(khdr->buffer_size);
371 }
372
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800373 if (khdr->version != 1) {
374 pr_err("attempting to restore a incompatible measurement list");
375 return -EINVAL;
376 }
377
378 if (khdr->count > ULONG_MAX - 1) {
379 pr_err("attempting to restore too many measurements");
380 return -EINVAL;
381 }
382
Roberto Sassu47fdee62017-05-16 14:53:42 +0200383 bitmap_zero(hdr_mask, HDR__LAST);
384 bitmap_set(hdr_mask, HDR_PCR, 1);
385 bitmap_set(hdr_mask, HDR_DIGEST, 1);
386
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800387 /*
388 * ima kexec buffer prefix: version, buffer size, count
389 * v1 format: pcr, digest, template-name-len, template-name,
390 * template-data-size, template-data
391 */
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800392 bufendp = buf + khdr->buffer_size;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800393 while ((bufp < bufendp) && (count++ < khdr->count)) {
Roberto Sassu47fdee62017-05-16 14:53:42 +0200394 int enforce_mask = ENFORCE_FIELDS;
395
396 enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
397 ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
398 hdr_mask, enforce_mask, "entry header");
399 if (ret < 0)
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800400 break;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800401
Roberto Sassu47fdee62017-05-16 14:53:42 +0200402 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800403 pr_err("attempting to restore a template name that is too long\n");
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800404 ret = -EINVAL;
405 break;
406 }
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800407
408 /* template name is not null terminated */
Roberto Sassu47fdee62017-05-16 14:53:42 +0200409 memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
410 hdr[HDR_TEMPLATE_NAME].len);
411 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800412
413 if (strcmp(template_name, "ima") == 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800414 pr_err("attempting to restore an unsupported template \"%s\" failed\n",
415 template_name);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800416 ret = -EINVAL;
417 break;
418 }
419
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800420 template_desc = lookup_template_desc(template_name);
421 if (!template_desc) {
Mimi Zoharc7d09362016-12-19 16:22:54 -0800422 template_desc = restore_template_fmt(template_name);
423 if (!template_desc)
424 break;
425 }
426
427 /*
428 * Only the running system's template format is initialized
429 * on boot. As needed, initialize the other template formats.
430 */
431 ret = template_desc_init_fields(template_desc->fmt,
432 &(template_desc->fields),
433 &(template_desc->num_fields));
434 if (ret < 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800435 pr_err("attempting to restore the template fmt \"%s\" failed\n",
436 template_desc->fmt);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800437 ret = -EINVAL;
438 break;
439 }
440
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800441 ret = ima_restore_template_data(template_desc,
Roberto Sassu47fdee62017-05-16 14:53:42 +0200442 hdr[HDR_TEMPLATE_DATA].data,
443 hdr[HDR_TEMPLATE_DATA].len,
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800444 &entry);
445 if (ret < 0)
446 break;
447
Roberto Sassu47fdee62017-05-16 14:53:42 +0200448 memcpy(entry->digest, hdr[HDR_DIGEST].data,
449 hdr[HDR_DIGEST].len);
450 entry->pcr = !ima_canonical_fmt ? *(hdr[HDR_PCR].data) :
451 le32_to_cpu(*(hdr[HDR_PCR].data));
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800452 ret = ima_restore_measurement_entry(entry);
453 if (ret < 0)
454 break;
455
456 }
457 return ret;
458}