blob: e22e510ae92d46a6ac3dc5915371e20de47ad01d [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;
Lakshmi Ramasubramaniandea87d02020-11-12 12:39:59 -080058static struct ima_template_desc *ima_buf_template;
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020059
Thiago Jung Bauermanne5092252019-06-27 23:19:33 -030060/**
61 * ima_template_has_modsig - Check whether template has modsig-related fields.
62 * @ima_template: IMA template to check.
63 *
64 * Tells whether the given template has fields referencing a file's appended
65 * signature.
66 */
67bool ima_template_has_modsig(const struct ima_template_desc *ima_template)
68{
69 int i;
70
71 for (i = 0; i < ima_template->num_fields; i++)
72 if (!strcmp(ima_template->fields[i]->field_id, "modsig") ||
73 !strcmp(ima_template->fields[i]->field_id, "d-modsig"))
74 return true;
75
76 return false;
77}
78
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020079static int __init ima_template_setup(char *str)
80{
81 struct ima_template_desc *template_desc;
82 int template_len = strlen(str);
83
Roberto Sassuc2426d22014-10-13 14:08:42 +020084 if (ima_template)
85 return 1;
86
Mimi Zohar3f23d622016-12-19 16:22:51 -080087 ima_init_template_list();
88
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020089 /*
90 * Verify that a template with the supplied name exists.
91 * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
92 */
93 template_desc = lookup_template_desc(str);
Roberto Sassu71fed2e2014-10-13 14:08:38 +020094 if (!template_desc) {
95 pr_err("template %s not found, using %s\n",
96 str, CONFIG_IMA_DEFAULT_TEMPLATE);
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020097 return 1;
Roberto Sassu71fed2e2014-10-13 14:08:38 +020098 }
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020099
100 /*
101 * Verify whether the current hash algorithm is supported
102 * by the 'ima' template.
103 */
104 if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
105 ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
Joe Perches20ee4512014-02-24 13:59:56 -0800106 pr_err("template does not support hash alg\n");
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +0200107 return 1;
108 }
109
110 ima_template = template_desc;
111 return 1;
112}
113__setup("ima_template=", ima_template_setup);
Roberto Sassua71dc652013-06-07 12:16:33 +0200114
Roberto Sassuc2426d22014-10-13 14:08:42 +0200115static int __init ima_template_fmt_setup(char *str)
116{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800117 int num_templates = ARRAY_SIZE(builtin_templates);
Roberto Sassuc2426d22014-10-13 14:08:42 +0200118
119 if (ima_template)
120 return 1;
121
122 if (template_desc_init_fields(str, NULL, NULL) < 0) {
123 pr_err("format string '%s' not valid, using template %s\n",
124 str, CONFIG_IMA_DEFAULT_TEMPLATE);
125 return 1;
126 }
127
Mimi Zohar3f23d622016-12-19 16:22:51 -0800128 builtin_templates[num_templates - 1].fmt = str;
129 ima_template = builtin_templates + num_templates - 1;
130
Roberto Sassuc2426d22014-10-13 14:08:42 +0200131 return 1;
132}
133__setup("ima_template_fmt=", ima_template_fmt_setup);
134
Matthew Garrett19453ce02019-06-19 15:46:11 -0700135struct ima_template_desc *lookup_template_desc(const char *name)
Roberto Sassua71dc652013-06-07 12:16:33 +0200136{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800137 struct ima_template_desc *template_desc;
138 int found = 0;
Roberto Sassua71dc652013-06-07 12:16:33 +0200139
Mimi Zohar3f23d622016-12-19 16:22:51 -0800140 rcu_read_lock();
141 list_for_each_entry_rcu(template_desc, &defined_templates, list) {
142 if ((strcmp(template_desc->name, name) == 0) ||
143 (strcmp(template_desc->fmt, name) == 0)) {
144 found = 1;
145 break;
146 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200147 }
Mimi Zohar3f23d622016-12-19 16:22:51 -0800148 rcu_read_unlock();
149 return found ? template_desc : NULL;
Roberto Sassua71dc652013-06-07 12:16:33 +0200150}
151
Eric Biggersb2724d52018-09-07 13:22:23 -0700152static const struct ima_template_field *
153lookup_template_field(const char *field_id)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200154{
155 int i;
156
157 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
158 if (strncmp(supported_fields[i].field_id, field_id,
159 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
160 return &supported_fields[i];
161 return NULL;
162}
163
Roberto Sassudbc335d2013-11-25 20:18:52 +0100164static int template_fmt_size(const char *template_fmt)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200165{
166 char c;
167 int template_fmt_len = strlen(template_fmt);
168 int i = 0, j = 0;
169
170 while (i < template_fmt_len) {
171 c = template_fmt[i];
172 if (c == '|')
173 j++;
174 i++;
175 }
176
177 return j + 1;
178}
179
Matthew Garrett19453ce02019-06-19 15:46:11 -0700180int template_desc_init_fields(const char *template_fmt,
181 const struct ima_template_field ***fields,
182 int *num_fields)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200183{
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200184 const char *template_fmt_ptr;
Eric Biggersb2724d52018-09-07 13:22:23 -0700185 const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
Mimi Zoharc7d09362016-12-19 16:22:54 -0800186 int template_num_fields;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200187 int i, len;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200188
Mimi Zoharc7d09362016-12-19 16:22:54 -0800189 if (num_fields && *num_fields > 0) /* already initialized? */
190 return 0;
191
192 template_num_fields = template_fmt_size(template_fmt);
193
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200194 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
195 pr_err("format string '%s' contains too many fields\n",
196 template_fmt);
Roberto Sassuadf53a72013-06-07 12:16:29 +0200197 return -EINVAL;
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200198 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200199
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200200 for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
201 i++, template_fmt_ptr += len + 1) {
202 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
Roberto Sassuadf53a72013-06-07 12:16:29 +0200203
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200204 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
205 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
206 pr_err("Invalid field with length %d\n", len);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200207 return -EINVAL;
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200208 }
209
210 memcpy(tmp_field_id, template_fmt_ptr, len);
211 tmp_field_id[len] = '\0';
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200212 found_fields[i] = lookup_template_field(tmp_field_id);
213 if (!found_fields[i]) {
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200214 pr_err("field '%s' not found\n", tmp_field_id);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200215 return -ENOENT;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200216 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200217 }
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200218
Roberto Sassuc2426d22014-10-13 14:08:42 +0200219 if (fields && num_fields) {
220 *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
221 if (*fields == NULL)
222 return -ENOMEM;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200223
Roberto Sassuc2426d22014-10-13 14:08:42 +0200224 memcpy(*fields, found_fields, i * sizeof(*fields));
225 *num_fields = i;
226 }
227
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200228 return 0;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200229}
230
Mimi Zohar3f23d622016-12-19 16:22:51 -0800231void ima_init_template_list(void)
232{
233 int i;
234
235 if (!list_empty(&defined_templates))
236 return;
237
238 spin_lock(&template_list);
239 for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
240 list_add_tail_rcu(&builtin_templates[i].list,
241 &defined_templates);
242 }
243 spin_unlock(&template_list);
244}
245
Roberto Sassua71dc652013-06-07 12:16:33 +0200246struct ima_template_desc *ima_template_desc_current(void)
247{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800248 if (!ima_template) {
249 ima_init_template_list();
Mimi Zohar42865872013-06-07 12:16:34 +0200250 ima_template =
251 lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
Mimi Zohar3f23d622016-12-19 16:22:51 -0800252 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200253 return ima_template;
254}
255
Lakshmi Ramasubramaniandea87d02020-11-12 12:39:59 -0800256struct ima_template_desc *ima_template_desc_buf(void)
257{
258 if (!ima_buf_template) {
259 ima_init_template_list();
260 ima_buf_template = lookup_template_desc("ima-buf");
261 }
262 return ima_buf_template;
263}
264
Dmitry Kasatkine4a9c512014-09-03 10:19:58 +0300265int __init ima_init_template(void)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200266{
Dmitry Kasatkinb4148db2014-05-08 11:23:53 +0300267 struct ima_template_desc *template = ima_template_desc_current();
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200268 int result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200269
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200270 result = template_desc_init_fields(template->fmt,
271 &(template->fields),
272 &(template->num_fields));
Lakshmi Ramasubramaniandea87d02020-11-12 12:39:59 -0800273 if (result < 0) {
274 pr_err("template %s init failed, result: %d\n",
275 (strlen(template->name) ?
276 template->name : template->fmt), result);
277 return result;
278 }
279
280 template = ima_template_desc_buf();
281 if (!template) {
282 pr_err("Failed to get ima-buf template\n");
283 return -EINVAL;
284 }
285
286 result = template_desc_init_fields(template->fmt,
287 &(template->fields),
288 &(template->num_fields));
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200289 if (result < 0)
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200290 pr_err("template %s init failed, result: %d\n",
291 (strlen(template->name) ?
292 template->name : template->fmt), result);
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200293
294 return result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200295}
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800296
Mimi Zoharc7d09362016-12-19 16:22:54 -0800297static struct ima_template_desc *restore_template_fmt(char *template_name)
298{
299 struct ima_template_desc *template_desc = NULL;
300 int ret;
301
302 ret = template_desc_init_fields(template_name, NULL, NULL);
303 if (ret < 0) {
304 pr_err("attempting to initialize the template \"%s\" failed\n",
305 template_name);
306 goto out;
307 }
308
309 template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
310 if (!template_desc)
311 goto out;
312
313 template_desc->name = "";
314 template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
315 if (!template_desc->fmt)
316 goto out;
317
318 spin_lock(&template_list);
319 list_add_tail_rcu(&template_desc->list, &defined_templates);
320 spin_unlock(&template_list);
321out:
322 return template_desc;
323}
324
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800325static int ima_restore_template_data(struct ima_template_desc *template_desc,
326 void *template_data,
327 int template_data_size,
328 struct ima_template_entry **entry)
329{
Roberto Sassuaa724fe2020-03-25 11:47:09 +0100330 struct tpm_digest *digests;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800331 int ret = 0;
332 int i;
333
Gustavo A. R. Silvafa5b5712019-05-29 11:53:43 -0500334 *entry = kzalloc(struct_size(*entry, template_data,
335 template_desc->num_fields), GFP_NOFS);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800336 if (!*entry)
337 return -ENOMEM;
338
Roberto Sassuaa724fe2020-03-25 11:47:09 +0100339 digests = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
340 sizeof(*digests), GFP_NOFS);
341 if (!digests) {
342 kfree(*entry);
343 return -ENOMEM;
344 }
345
346 (*entry)->digests = digests;
347
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200348 ret = ima_parse_buf(template_data, template_data + template_data_size,
349 NULL, template_desc->num_fields,
350 (*entry)->template_data, NULL, NULL,
351 ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
352 if (ret < 0) {
Roberto Sassuaa724fe2020-03-25 11:47:09 +0100353 kfree((*entry)->digests);
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200354 kfree(*entry);
355 return ret;
356 }
357
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800358 (*entry)->template_desc = template_desc;
359 for (i = 0; i < template_desc->num_fields; i++) {
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200360 struct ima_field_data *field_data = &(*entry)->template_data[i];
361 u8 *data = field_data->data;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800362
363 (*entry)->template_data[i].data =
364 kzalloc(field_data->len + 1, GFP_KERNEL);
365 if (!(*entry)->template_data[i].data) {
366 ret = -ENOMEM;
367 break;
368 }
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200369 memcpy((*entry)->template_data[i].data, data, field_data->len);
370 (*entry)->template_data_len += sizeof(field_data->len);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800371 (*entry)->template_data_len += field_data->len;
372 }
373
374 if (ret < 0) {
375 ima_free_template_entry(*entry);
376 *entry = NULL;
377 }
378
379 return ret;
380}
381
382/* Restore the serialized binary measurement list without extending PCRs. */
383int ima_restore_measurement_list(loff_t size, void *buf)
384{
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800385 char template_name[MAX_TEMPLATE_NAME_LEN];
Roberto Sassu1ea973d2020-03-25 11:53:50 +0100386 unsigned char zero[TPM_DIGEST_SIZE] = { 0 };
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800387
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800388 struct ima_kexec_hdr *khdr = buf;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200389 struct ima_field_data hdr[HDR__LAST] = {
390 [HDR_PCR] = {.len = sizeof(u32)},
391 [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
392 };
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800393
394 void *bufp = buf + sizeof(*khdr);
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800395 void *bufendp;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800396 struct ima_template_entry *entry;
397 struct ima_template_desc *template_desc;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200398 DECLARE_BITMAP(hdr_mask, HDR__LAST);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800399 unsigned long count = 0;
400 int ret = 0;
401
402 if (!buf || size < sizeof(*khdr))
403 return 0;
404
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800405 if (ima_canonical_fmt) {
406 khdr->version = le16_to_cpu(khdr->version);
407 khdr->count = le64_to_cpu(khdr->count);
408 khdr->buffer_size = le64_to_cpu(khdr->buffer_size);
409 }
410
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800411 if (khdr->version != 1) {
412 pr_err("attempting to restore a incompatible measurement list");
413 return -EINVAL;
414 }
415
416 if (khdr->count > ULONG_MAX - 1) {
417 pr_err("attempting to restore too many measurements");
418 return -EINVAL;
419 }
420
Roberto Sassu47fdee62017-05-16 14:53:42 +0200421 bitmap_zero(hdr_mask, HDR__LAST);
422 bitmap_set(hdr_mask, HDR_PCR, 1);
423 bitmap_set(hdr_mask, HDR_DIGEST, 1);
424
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800425 /*
426 * ima kexec buffer prefix: version, buffer size, count
427 * v1 format: pcr, digest, template-name-len, template-name,
428 * template-data-size, template-data
429 */
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800430 bufendp = buf + khdr->buffer_size;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800431 while ((bufp < bufendp) && (count++ < khdr->count)) {
Roberto Sassu47fdee62017-05-16 14:53:42 +0200432 int enforce_mask = ENFORCE_FIELDS;
433
434 enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
435 ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
436 hdr_mask, enforce_mask, "entry header");
437 if (ret < 0)
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800438 break;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800439
Roberto Sassu47fdee62017-05-16 14:53:42 +0200440 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800441 pr_err("attempting to restore a template name that is too long\n");
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800442 ret = -EINVAL;
443 break;
444 }
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800445
446 /* template name is not null terminated */
Roberto Sassu47fdee62017-05-16 14:53:42 +0200447 memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
448 hdr[HDR_TEMPLATE_NAME].len);
449 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800450
451 if (strcmp(template_name, "ima") == 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800452 pr_err("attempting to restore an unsupported template \"%s\" failed\n",
453 template_name);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800454 ret = -EINVAL;
455 break;
456 }
457
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800458 template_desc = lookup_template_desc(template_name);
459 if (!template_desc) {
Mimi Zoharc7d09362016-12-19 16:22:54 -0800460 template_desc = restore_template_fmt(template_name);
461 if (!template_desc)
462 break;
463 }
464
465 /*
466 * Only the running system's template format is initialized
467 * on boot. As needed, initialize the other template formats.
468 */
469 ret = template_desc_init_fields(template_desc->fmt,
470 &(template_desc->fields),
471 &(template_desc->num_fields));
472 if (ret < 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800473 pr_err("attempting to restore the template fmt \"%s\" failed\n",
474 template_desc->fmt);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800475 ret = -EINVAL;
476 break;
477 }
478
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800479 ret = ima_restore_template_data(template_desc,
Roberto Sassu47fdee62017-05-16 14:53:42 +0200480 hdr[HDR_TEMPLATE_DATA].data,
481 hdr[HDR_TEMPLATE_DATA].len,
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800482 &entry);
483 if (ret < 0)
484 break;
485
Roberto Sassu1ea973d2020-03-25 11:53:50 +0100486 if (memcmp(hdr[HDR_DIGEST].data, zero, sizeof(zero))) {
487 ret = ima_calc_field_array_hash(
488 &entry->template_data[0],
489 entry);
490 if (ret < 0) {
491 pr_err("cannot calculate template digest\n");
492 ret = -EINVAL;
493 break;
494 }
495 }
496
Roberto Sassu47fdee62017-05-16 14:53:42 +0200497 entry->pcr = !ima_canonical_fmt ? *(hdr[HDR_PCR].data) :
498 le32_to_cpu(*(hdr[HDR_PCR].data));
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800499 ret = ima_restore_measurement_entry(entry);
500 if (ret < 0)
501 break;
502
503 }
504 return ret;
505}