blob: 88d494ca6248c1a26d5f188ef8c9afc6252e3bc4 [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
61static int __init ima_template_setup(char *str)
62{
63 struct ima_template_desc *template_desc;
64 int template_len = strlen(str);
65
Roberto Sassuc2426d22014-10-13 14:08:42 +020066 if (ima_template)
67 return 1;
68
Mimi Zohar3f23d622016-12-19 16:22:51 -080069 ima_init_template_list();
70
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020071 /*
72 * Verify that a template with the supplied name exists.
73 * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
74 */
75 template_desc = lookup_template_desc(str);
Roberto Sassu71fed2e2014-10-13 14:08:38 +020076 if (!template_desc) {
77 pr_err("template %s not found, using %s\n",
78 str, CONFIG_IMA_DEFAULT_TEMPLATE);
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020079 return 1;
Roberto Sassu71fed2e2014-10-13 14:08:38 +020080 }
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020081
82 /*
83 * Verify whether the current hash algorithm is supported
84 * by the 'ima' template.
85 */
86 if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
87 ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
Joe Perches20ee4512014-02-24 13:59:56 -080088 pr_err("template does not support hash alg\n");
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020089 return 1;
90 }
91
92 ima_template = template_desc;
93 return 1;
94}
95__setup("ima_template=", ima_template_setup);
Roberto Sassua71dc652013-06-07 12:16:33 +020096
Roberto Sassuc2426d22014-10-13 14:08:42 +020097static int __init ima_template_fmt_setup(char *str)
98{
Mimi Zohar3f23d622016-12-19 16:22:51 -080099 int num_templates = ARRAY_SIZE(builtin_templates);
Roberto Sassuc2426d22014-10-13 14:08:42 +0200100
101 if (ima_template)
102 return 1;
103
104 if (template_desc_init_fields(str, NULL, NULL) < 0) {
105 pr_err("format string '%s' not valid, using template %s\n",
106 str, CONFIG_IMA_DEFAULT_TEMPLATE);
107 return 1;
108 }
109
Mimi Zohar3f23d622016-12-19 16:22:51 -0800110 builtin_templates[num_templates - 1].fmt = str;
111 ima_template = builtin_templates + num_templates - 1;
112
Roberto Sassuc2426d22014-10-13 14:08:42 +0200113 return 1;
114}
115__setup("ima_template_fmt=", ima_template_fmt_setup);
116
Matthew Garrett19453ce02019-06-19 15:46:11 -0700117struct ima_template_desc *lookup_template_desc(const char *name)
Roberto Sassua71dc652013-06-07 12:16:33 +0200118{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800119 struct ima_template_desc *template_desc;
120 int found = 0;
Roberto Sassua71dc652013-06-07 12:16:33 +0200121
Mimi Zohar3f23d622016-12-19 16:22:51 -0800122 rcu_read_lock();
123 list_for_each_entry_rcu(template_desc, &defined_templates, list) {
124 if ((strcmp(template_desc->name, name) == 0) ||
125 (strcmp(template_desc->fmt, name) == 0)) {
126 found = 1;
127 break;
128 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200129 }
Mimi Zohar3f23d622016-12-19 16:22:51 -0800130 rcu_read_unlock();
131 return found ? template_desc : NULL;
Roberto Sassua71dc652013-06-07 12:16:33 +0200132}
133
Eric Biggersb2724d52018-09-07 13:22:23 -0700134static const struct ima_template_field *
135lookup_template_field(const char *field_id)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200136{
137 int i;
138
139 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
140 if (strncmp(supported_fields[i].field_id, field_id,
141 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
142 return &supported_fields[i];
143 return NULL;
144}
145
Roberto Sassudbc335d2013-11-25 20:18:52 +0100146static int template_fmt_size(const char *template_fmt)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200147{
148 char c;
149 int template_fmt_len = strlen(template_fmt);
150 int i = 0, j = 0;
151
152 while (i < template_fmt_len) {
153 c = template_fmt[i];
154 if (c == '|')
155 j++;
156 i++;
157 }
158
159 return j + 1;
160}
161
Matthew Garrett19453ce02019-06-19 15:46:11 -0700162int template_desc_init_fields(const char *template_fmt,
163 const struct ima_template_field ***fields,
164 int *num_fields)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200165{
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200166 const char *template_fmt_ptr;
Eric Biggersb2724d52018-09-07 13:22:23 -0700167 const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
Mimi Zoharc7d09362016-12-19 16:22:54 -0800168 int template_num_fields;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200169 int i, len;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200170
Mimi Zoharc7d09362016-12-19 16:22:54 -0800171 if (num_fields && *num_fields > 0) /* already initialized? */
172 return 0;
173
174 template_num_fields = template_fmt_size(template_fmt);
175
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200176 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
177 pr_err("format string '%s' contains too many fields\n",
178 template_fmt);
Roberto Sassuadf53a72013-06-07 12:16:29 +0200179 return -EINVAL;
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200180 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200181
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200182 for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
183 i++, template_fmt_ptr += len + 1) {
184 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
Roberto Sassuadf53a72013-06-07 12:16:29 +0200185
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200186 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
187 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
188 pr_err("Invalid field with length %d\n", len);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200189 return -EINVAL;
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200190 }
191
192 memcpy(tmp_field_id, template_fmt_ptr, len);
193 tmp_field_id[len] = '\0';
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200194 found_fields[i] = lookup_template_field(tmp_field_id);
195 if (!found_fields[i]) {
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200196 pr_err("field '%s' not found\n", tmp_field_id);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200197 return -ENOENT;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200198 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200199 }
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200200
Roberto Sassuc2426d22014-10-13 14:08:42 +0200201 if (fields && num_fields) {
202 *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
203 if (*fields == NULL)
204 return -ENOMEM;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200205
Roberto Sassuc2426d22014-10-13 14:08:42 +0200206 memcpy(*fields, found_fields, i * sizeof(*fields));
207 *num_fields = i;
208 }
209
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200210 return 0;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200211}
212
Mimi Zohar3f23d622016-12-19 16:22:51 -0800213void ima_init_template_list(void)
214{
215 int i;
216
217 if (!list_empty(&defined_templates))
218 return;
219
220 spin_lock(&template_list);
221 for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
222 list_add_tail_rcu(&builtin_templates[i].list,
223 &defined_templates);
224 }
225 spin_unlock(&template_list);
226}
227
Roberto Sassua71dc652013-06-07 12:16:33 +0200228struct ima_template_desc *ima_template_desc_current(void)
229{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800230 if (!ima_template) {
231 ima_init_template_list();
Mimi Zohar42865872013-06-07 12:16:34 +0200232 ima_template =
233 lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
Mimi Zohar3f23d622016-12-19 16:22:51 -0800234 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200235 return ima_template;
236}
237
Dmitry Kasatkine4a9c512014-09-03 10:19:58 +0300238int __init ima_init_template(void)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200239{
Dmitry Kasatkinb4148db2014-05-08 11:23:53 +0300240 struct ima_template_desc *template = ima_template_desc_current();
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200241 int result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200242
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200243 result = template_desc_init_fields(template->fmt,
244 &(template->fields),
245 &(template->num_fields));
246 if (result < 0)
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200247 pr_err("template %s init failed, result: %d\n",
248 (strlen(template->name) ?
249 template->name : template->fmt), result);
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200250
251 return result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200252}
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800253
Mimi Zoharc7d09362016-12-19 16:22:54 -0800254static struct ima_template_desc *restore_template_fmt(char *template_name)
255{
256 struct ima_template_desc *template_desc = NULL;
257 int ret;
258
259 ret = template_desc_init_fields(template_name, NULL, NULL);
260 if (ret < 0) {
261 pr_err("attempting to initialize the template \"%s\" failed\n",
262 template_name);
263 goto out;
264 }
265
266 template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
267 if (!template_desc)
268 goto out;
269
270 template_desc->name = "";
271 template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
272 if (!template_desc->fmt)
273 goto out;
274
275 spin_lock(&template_list);
276 list_add_tail_rcu(&template_desc->list, &defined_templates);
277 spin_unlock(&template_list);
278out:
279 return template_desc;
280}
281
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800282static int ima_restore_template_data(struct ima_template_desc *template_desc,
283 void *template_data,
284 int template_data_size,
285 struct ima_template_entry **entry)
286{
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800287 int ret = 0;
288 int i;
289
290 *entry = kzalloc(sizeof(**entry) +
291 template_desc->num_fields * sizeof(struct ima_field_data),
292 GFP_NOFS);
293 if (!*entry)
294 return -ENOMEM;
295
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200296 ret = ima_parse_buf(template_data, template_data + template_data_size,
297 NULL, template_desc->num_fields,
298 (*entry)->template_data, NULL, NULL,
299 ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
300 if (ret < 0) {
301 kfree(*entry);
302 return ret;
303 }
304
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800305 (*entry)->template_desc = template_desc;
306 for (i = 0; i < template_desc->num_fields; i++) {
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200307 struct ima_field_data *field_data = &(*entry)->template_data[i];
308 u8 *data = field_data->data;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800309
310 (*entry)->template_data[i].data =
311 kzalloc(field_data->len + 1, GFP_KERNEL);
312 if (!(*entry)->template_data[i].data) {
313 ret = -ENOMEM;
314 break;
315 }
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200316 memcpy((*entry)->template_data[i].data, data, field_data->len);
317 (*entry)->template_data_len += sizeof(field_data->len);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800318 (*entry)->template_data_len += field_data->len;
319 }
320
321 if (ret < 0) {
322 ima_free_template_entry(*entry);
323 *entry = NULL;
324 }
325
326 return ret;
327}
328
329/* Restore the serialized binary measurement list without extending PCRs. */
330int ima_restore_measurement_list(loff_t size, void *buf)
331{
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800332 char template_name[MAX_TEMPLATE_NAME_LEN];
333
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800334 struct ima_kexec_hdr *khdr = buf;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200335 struct ima_field_data hdr[HDR__LAST] = {
336 [HDR_PCR] = {.len = sizeof(u32)},
337 [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
338 };
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800339
340 void *bufp = buf + sizeof(*khdr);
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800341 void *bufendp;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800342 struct ima_template_entry *entry;
343 struct ima_template_desc *template_desc;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200344 DECLARE_BITMAP(hdr_mask, HDR__LAST);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800345 unsigned long count = 0;
346 int ret = 0;
347
348 if (!buf || size < sizeof(*khdr))
349 return 0;
350
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800351 if (ima_canonical_fmt) {
352 khdr->version = le16_to_cpu(khdr->version);
353 khdr->count = le64_to_cpu(khdr->count);
354 khdr->buffer_size = le64_to_cpu(khdr->buffer_size);
355 }
356
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800357 if (khdr->version != 1) {
358 pr_err("attempting to restore a incompatible measurement list");
359 return -EINVAL;
360 }
361
362 if (khdr->count > ULONG_MAX - 1) {
363 pr_err("attempting to restore too many measurements");
364 return -EINVAL;
365 }
366
Roberto Sassu47fdee62017-05-16 14:53:42 +0200367 bitmap_zero(hdr_mask, HDR__LAST);
368 bitmap_set(hdr_mask, HDR_PCR, 1);
369 bitmap_set(hdr_mask, HDR_DIGEST, 1);
370
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800371 /*
372 * ima kexec buffer prefix: version, buffer size, count
373 * v1 format: pcr, digest, template-name-len, template-name,
374 * template-data-size, template-data
375 */
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800376 bufendp = buf + khdr->buffer_size;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800377 while ((bufp < bufendp) && (count++ < khdr->count)) {
Roberto Sassu47fdee62017-05-16 14:53:42 +0200378 int enforce_mask = ENFORCE_FIELDS;
379
380 enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
381 ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
382 hdr_mask, enforce_mask, "entry header");
383 if (ret < 0)
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800384 break;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800385
Roberto Sassu47fdee62017-05-16 14:53:42 +0200386 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800387 pr_err("attempting to restore a template name that is too long\n");
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800388 ret = -EINVAL;
389 break;
390 }
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800391
392 /* template name is not null terminated */
Roberto Sassu47fdee62017-05-16 14:53:42 +0200393 memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
394 hdr[HDR_TEMPLATE_NAME].len);
395 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800396
397 if (strcmp(template_name, "ima") == 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800398 pr_err("attempting to restore an unsupported template \"%s\" failed\n",
399 template_name);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800400 ret = -EINVAL;
401 break;
402 }
403
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800404 template_desc = lookup_template_desc(template_name);
405 if (!template_desc) {
Mimi Zoharc7d09362016-12-19 16:22:54 -0800406 template_desc = restore_template_fmt(template_name);
407 if (!template_desc)
408 break;
409 }
410
411 /*
412 * Only the running system's template format is initialized
413 * on boot. As needed, initialize the other template formats.
414 */
415 ret = template_desc_init_fields(template_desc->fmt,
416 &(template_desc->fields),
417 &(template_desc->num_fields));
418 if (ret < 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800419 pr_err("attempting to restore the template fmt \"%s\" failed\n",
420 template_desc->fmt);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800421 ret = -EINVAL;
422 break;
423 }
424
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800425 ret = ima_restore_template_data(template_desc,
Roberto Sassu47fdee62017-05-16 14:53:42 +0200426 hdr[HDR_TEMPLATE_DATA].data,
427 hdr[HDR_TEMPLATE_DATA].len,
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800428 &entry);
429 if (ret < 0)
430 break;
431
Roberto Sassu47fdee62017-05-16 14:53:42 +0200432 memcpy(entry->digest, hdr[HDR_DIGEST].data,
433 hdr[HDR_DIGEST].len);
434 entry->pcr = !ima_canonical_fmt ? *(hdr[HDR_PCR].data) :
435 le32_to_cpu(*(hdr[HDR_PCR].data));
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800436 ret = ima_restore_measurement_entry(entry);
437 if (ret < 0)
438 break;
439
440 }
441 return ret;
442}