blob: 694560396be05719bce56b8c8506812c9d861ee3 [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 Sassu88016de2021-06-03 15:17:05 +020025 {.name = "evm-sig",
26 .fmt = "d-ng|n-ng|evmsig|xattrnames|xattrlengths|xattrvalues|iuid|igid|imode"},
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 Sassu026d7fc2021-05-14 17:27:52 +020050 {.field_id = "evmsig", .field_init = ima_eventevmsig_init,
51 .field_show = ima_show_template_sig},
Roberto Sassu7dcfeac2021-05-28 09:38:07 +020052 {.field_id = "iuid", .field_init = ima_eventinodeuid_init,
53 .field_show = ima_show_template_uint},
54 {.field_id = "igid", .field_init = ima_eventinodegid_init,
55 .field_show = ima_show_template_uint},
Roberto Sassuf8216f62021-05-28 09:38:08 +020056 {.field_id = "imode", .field_init = ima_eventinodemode_init,
57 .field_show = ima_show_template_uint},
Roberto Sassu8314b672021-06-01 10:23:38 +020058 {.field_id = "xattrnames",
59 .field_init = ima_eventinodexattrnames_init,
60 .field_show = ima_show_template_string},
61 {.field_id = "xattrlengths",
62 .field_init = ima_eventinodexattrlengths_init,
63 .field_show = ima_show_template_sig},
64 {.field_id = "xattrvalues",
65 .field_init = ima_eventinodexattrvalues_init,
66 .field_show = ima_show_template_sig},
Roberto Sassuadf53a72013-06-07 12:16:29 +020067};
Thiago Jung Bauermann337619e2019-06-27 20:25:46 -030068
69/*
70 * Used when restoring measurements carried over from a kexec. 'd' and 'n' don't
71 * need to be accounted for since they shouldn't be defined in the same template
72 * description as 'd-ng' and 'n-ng' respectively.
73 */
Roberto Sassu88016de2021-06-03 15:17:05 +020074#define MAX_TEMPLATE_NAME_LEN \
75 sizeof("d-ng|n-ng|evmsig|xattrnames|xattrlengths|xattrvalues|iuid|igid|imode")
Roberto Sassuadf53a72013-06-07 12:16:29 +020076
Roberto Sassua71dc652013-06-07 12:16:33 +020077static struct ima_template_desc *ima_template;
Lakshmi Ramasubramaniandea87d02020-11-12 12:39:59 -080078static struct ima_template_desc *ima_buf_template;
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020079
Thiago Jung Bauermanne5092252019-06-27 23:19:33 -030080/**
81 * ima_template_has_modsig - Check whether template has modsig-related fields.
82 * @ima_template: IMA template to check.
83 *
84 * Tells whether the given template has fields referencing a file's appended
85 * signature.
86 */
87bool ima_template_has_modsig(const struct ima_template_desc *ima_template)
88{
89 int i;
90
91 for (i = 0; i < ima_template->num_fields; i++)
92 if (!strcmp(ima_template->fields[i]->field_id, "modsig") ||
93 !strcmp(ima_template->fields[i]->field_id, "d-modsig"))
94 return true;
95
96 return false;
97}
98
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +020099static int __init ima_template_setup(char *str)
100{
101 struct ima_template_desc *template_desc;
102 int template_len = strlen(str);
103
Roberto Sassuc2426d22014-10-13 14:08:42 +0200104 if (ima_template)
105 return 1;
106
Mimi Zohar3f23d622016-12-19 16:22:51 -0800107 ima_init_template_list();
108
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +0200109 /*
110 * Verify that a template with the supplied name exists.
111 * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
112 */
113 template_desc = lookup_template_desc(str);
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200114 if (!template_desc) {
115 pr_err("template %s not found, using %s\n",
116 str, CONFIG_IMA_DEFAULT_TEMPLATE);
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +0200117 return 1;
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200118 }
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +0200119
120 /*
121 * Verify whether the current hash algorithm is supported
122 * by the 'ima' template.
123 */
124 if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
125 ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
Joe Perches20ee4512014-02-24 13:59:56 -0800126 pr_err("template does not support hash alg\n");
Roberto Sassu9b9d4ce2013-06-07 12:16:35 +0200127 return 1;
128 }
129
130 ima_template = template_desc;
131 return 1;
132}
133__setup("ima_template=", ima_template_setup);
Roberto Sassua71dc652013-06-07 12:16:33 +0200134
Roberto Sassuc2426d22014-10-13 14:08:42 +0200135static int __init ima_template_fmt_setup(char *str)
136{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800137 int num_templates = ARRAY_SIZE(builtin_templates);
Roberto Sassuc2426d22014-10-13 14:08:42 +0200138
139 if (ima_template)
140 return 1;
141
142 if (template_desc_init_fields(str, NULL, NULL) < 0) {
143 pr_err("format string '%s' not valid, using template %s\n",
144 str, CONFIG_IMA_DEFAULT_TEMPLATE);
145 return 1;
146 }
147
Mimi Zohar3f23d622016-12-19 16:22:51 -0800148 builtin_templates[num_templates - 1].fmt = str;
149 ima_template = builtin_templates + num_templates - 1;
150
Roberto Sassuc2426d22014-10-13 14:08:42 +0200151 return 1;
152}
153__setup("ima_template_fmt=", ima_template_fmt_setup);
154
Matthew Garrett19453ce02019-06-19 15:46:11 -0700155struct ima_template_desc *lookup_template_desc(const char *name)
Roberto Sassua71dc652013-06-07 12:16:33 +0200156{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800157 struct ima_template_desc *template_desc;
158 int found = 0;
Roberto Sassua71dc652013-06-07 12:16:33 +0200159
Mimi Zohar3f23d622016-12-19 16:22:51 -0800160 rcu_read_lock();
161 list_for_each_entry_rcu(template_desc, &defined_templates, list) {
162 if ((strcmp(template_desc->name, name) == 0) ||
163 (strcmp(template_desc->fmt, name) == 0)) {
164 found = 1;
165 break;
166 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200167 }
Mimi Zohar3f23d622016-12-19 16:22:51 -0800168 rcu_read_unlock();
169 return found ? template_desc : NULL;
Roberto Sassua71dc652013-06-07 12:16:33 +0200170}
171
Eric Biggersb2724d52018-09-07 13:22:23 -0700172static const struct ima_template_field *
173lookup_template_field(const char *field_id)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200174{
175 int i;
176
177 for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
178 if (strncmp(supported_fields[i].field_id, field_id,
179 IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
180 return &supported_fields[i];
181 return NULL;
182}
183
Roberto Sassudbc335d2013-11-25 20:18:52 +0100184static int template_fmt_size(const char *template_fmt)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200185{
186 char c;
187 int template_fmt_len = strlen(template_fmt);
188 int i = 0, j = 0;
189
190 while (i < template_fmt_len) {
191 c = template_fmt[i];
192 if (c == '|')
193 j++;
194 i++;
195 }
196
197 return j + 1;
198}
199
Matthew Garrett19453ce02019-06-19 15:46:11 -0700200int template_desc_init_fields(const char *template_fmt,
201 const struct ima_template_field ***fields,
202 int *num_fields)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200203{
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200204 const char *template_fmt_ptr;
Eric Biggersb2724d52018-09-07 13:22:23 -0700205 const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
Mimi Zoharc7d09362016-12-19 16:22:54 -0800206 int template_num_fields;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200207 int i, len;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200208
Mimi Zoharc7d09362016-12-19 16:22:54 -0800209 if (num_fields && *num_fields > 0) /* already initialized? */
210 return 0;
211
212 template_num_fields = template_fmt_size(template_fmt);
213
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200214 if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
215 pr_err("format string '%s' contains too many fields\n",
216 template_fmt);
Roberto Sassuadf53a72013-06-07 12:16:29 +0200217 return -EINVAL;
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200218 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200219
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200220 for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
221 i++, template_fmt_ptr += len + 1) {
222 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
Roberto Sassuadf53a72013-06-07 12:16:29 +0200223
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200224 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
225 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
226 pr_err("Invalid field with length %d\n", len);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200227 return -EINVAL;
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200228 }
229
230 memcpy(tmp_field_id, template_fmt_ptr, len);
231 tmp_field_id[len] = '\0';
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200232 found_fields[i] = lookup_template_field(tmp_field_id);
233 if (!found_fields[i]) {
Roberto Sassu9f3166b2014-10-13 14:08:40 +0200234 pr_err("field '%s' not found\n", tmp_field_id);
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200235 return -ENOENT;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200236 }
Roberto Sassuadf53a72013-06-07 12:16:29 +0200237 }
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200238
Roberto Sassuc2426d22014-10-13 14:08:42 +0200239 if (fields && num_fields) {
240 *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
241 if (*fields == NULL)
242 return -ENOMEM;
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200243
Roberto Sassuc2426d22014-10-13 14:08:42 +0200244 memcpy(*fields, found_fields, i * sizeof(*fields));
245 *num_fields = i;
246 }
247
Roberto Sassu1bd7fac2014-10-13 14:08:41 +0200248 return 0;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200249}
250
Mimi Zohar3f23d622016-12-19 16:22:51 -0800251void ima_init_template_list(void)
252{
253 int i;
254
255 if (!list_empty(&defined_templates))
256 return;
257
258 spin_lock(&template_list);
259 for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
260 list_add_tail_rcu(&builtin_templates[i].list,
261 &defined_templates);
262 }
263 spin_unlock(&template_list);
264}
265
Roberto Sassua71dc652013-06-07 12:16:33 +0200266struct ima_template_desc *ima_template_desc_current(void)
267{
Mimi Zohar3f23d622016-12-19 16:22:51 -0800268 if (!ima_template) {
269 ima_init_template_list();
Mimi Zohar42865872013-06-07 12:16:34 +0200270 ima_template =
271 lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
Mimi Zohar3f23d622016-12-19 16:22:51 -0800272 }
Roberto Sassua71dc652013-06-07 12:16:33 +0200273 return ima_template;
274}
275
Lakshmi Ramasubramaniandea87d02020-11-12 12:39:59 -0800276struct ima_template_desc *ima_template_desc_buf(void)
277{
278 if (!ima_buf_template) {
279 ima_init_template_list();
280 ima_buf_template = lookup_template_desc("ima-buf");
281 }
282 return ima_buf_template;
283}
284
Dmitry Kasatkine4a9c512014-09-03 10:19:58 +0300285int __init ima_init_template(void)
Roberto Sassuadf53a72013-06-07 12:16:29 +0200286{
Dmitry Kasatkinb4148db2014-05-08 11:23:53 +0300287 struct ima_template_desc *template = ima_template_desc_current();
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200288 int result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200289
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200290 result = template_desc_init_fields(template->fmt,
291 &(template->fields),
292 &(template->num_fields));
Lakshmi Ramasubramaniandea87d02020-11-12 12:39:59 -0800293 if (result < 0) {
294 pr_err("template %s init failed, result: %d\n",
295 (strlen(template->name) ?
296 template->name : template->fmt), result);
297 return result;
298 }
299
300 template = ima_template_desc_buf();
301 if (!template) {
302 pr_err("Failed to get ima-buf template\n");
303 return -EINVAL;
304 }
305
306 result = template_desc_init_fields(template->fmt,
307 &(template->fields),
308 &(template->num_fields));
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200309 if (result < 0)
Roberto Sassu7dbdb422014-10-13 14:08:39 +0200310 pr_err("template %s init failed, result: %d\n",
311 (strlen(template->name) ?
312 template->name : template->fmt), result);
Roberto Sassu71fed2e2014-10-13 14:08:38 +0200313
314 return result;
Roberto Sassuadf53a72013-06-07 12:16:29 +0200315}
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800316
Mimi Zoharc7d09362016-12-19 16:22:54 -0800317static struct ima_template_desc *restore_template_fmt(char *template_name)
318{
319 struct ima_template_desc *template_desc = NULL;
320 int ret;
321
322 ret = template_desc_init_fields(template_name, NULL, NULL);
323 if (ret < 0) {
324 pr_err("attempting to initialize the template \"%s\" failed\n",
325 template_name);
326 goto out;
327 }
328
329 template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
330 if (!template_desc)
331 goto out;
332
333 template_desc->name = "";
334 template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
335 if (!template_desc->fmt)
336 goto out;
337
338 spin_lock(&template_list);
339 list_add_tail_rcu(&template_desc->list, &defined_templates);
340 spin_unlock(&template_list);
341out:
342 return template_desc;
343}
344
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800345static int ima_restore_template_data(struct ima_template_desc *template_desc,
346 void *template_data,
347 int template_data_size,
348 struct ima_template_entry **entry)
349{
Roberto Sassuaa724fe2020-03-25 11:47:09 +0100350 struct tpm_digest *digests;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800351 int ret = 0;
352 int i;
353
Gustavo A. R. Silvafa5b5712019-05-29 11:53:43 -0500354 *entry = kzalloc(struct_size(*entry, template_data,
355 template_desc->num_fields), GFP_NOFS);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800356 if (!*entry)
357 return -ENOMEM;
358
Roberto Sassuaa724fe2020-03-25 11:47:09 +0100359 digests = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
360 sizeof(*digests), GFP_NOFS);
361 if (!digests) {
362 kfree(*entry);
363 return -ENOMEM;
364 }
365
366 (*entry)->digests = digests;
367
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200368 ret = ima_parse_buf(template_data, template_data + template_data_size,
369 NULL, template_desc->num_fields,
370 (*entry)->template_data, NULL, NULL,
371 ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
372 if (ret < 0) {
Roberto Sassuaa724fe2020-03-25 11:47:09 +0100373 kfree((*entry)->digests);
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200374 kfree(*entry);
375 return ret;
376 }
377
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800378 (*entry)->template_desc = template_desc;
379 for (i = 0; i < template_desc->num_fields; i++) {
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200380 struct ima_field_data *field_data = &(*entry)->template_data[i];
381 u8 *data = field_data->data;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800382
383 (*entry)->template_data[i].data =
384 kzalloc(field_data->len + 1, GFP_KERNEL);
385 if (!(*entry)->template_data[i].data) {
386 ret = -ENOMEM;
387 break;
388 }
Roberto Sassu28a8dc42017-05-16 14:53:43 +0200389 memcpy((*entry)->template_data[i].data, data, field_data->len);
390 (*entry)->template_data_len += sizeof(field_data->len);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800391 (*entry)->template_data_len += field_data->len;
392 }
393
394 if (ret < 0) {
395 ima_free_template_entry(*entry);
396 *entry = NULL;
397 }
398
399 return ret;
400}
401
402/* Restore the serialized binary measurement list without extending PCRs. */
403int ima_restore_measurement_list(loff_t size, void *buf)
404{
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800405 char template_name[MAX_TEMPLATE_NAME_LEN];
Roberto Sassu1ea973d2020-03-25 11:53:50 +0100406 unsigned char zero[TPM_DIGEST_SIZE] = { 0 };
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800407
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800408 struct ima_kexec_hdr *khdr = buf;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200409 struct ima_field_data hdr[HDR__LAST] = {
410 [HDR_PCR] = {.len = sizeof(u32)},
411 [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
412 };
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800413
414 void *bufp = buf + sizeof(*khdr);
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800415 void *bufendp;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800416 struct ima_template_entry *entry;
417 struct ima_template_desc *template_desc;
Roberto Sassu47fdee62017-05-16 14:53:42 +0200418 DECLARE_BITMAP(hdr_mask, HDR__LAST);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800419 unsigned long count = 0;
420 int ret = 0;
421
422 if (!buf || size < sizeof(*khdr))
423 return 0;
424
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800425 if (ima_canonical_fmt) {
Roberto Sassu24c9ae22021-06-08 14:31:21 +0200426 khdr->version = le16_to_cpu((__force __le16)khdr->version);
427 khdr->count = le64_to_cpu((__force __le64)khdr->count);
428 khdr->buffer_size = le64_to_cpu((__force __le64)khdr->buffer_size);
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800429 }
430
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800431 if (khdr->version != 1) {
432 pr_err("attempting to restore a incompatible measurement list");
433 return -EINVAL;
434 }
435
436 if (khdr->count > ULONG_MAX - 1) {
437 pr_err("attempting to restore too many measurements");
438 return -EINVAL;
439 }
440
Roberto Sassu47fdee62017-05-16 14:53:42 +0200441 bitmap_zero(hdr_mask, HDR__LAST);
442 bitmap_set(hdr_mask, HDR_PCR, 1);
443 bitmap_set(hdr_mask, HDR_DIGEST, 1);
444
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800445 /*
446 * ima kexec buffer prefix: version, buffer size, count
447 * v1 format: pcr, digest, template-name-len, template-name,
448 * template-data-size, template-data
449 */
Mimi Zohard68a6fe2016-12-19 16:22:57 -0800450 bufendp = buf + khdr->buffer_size;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800451 while ((bufp < bufendp) && (count++ < khdr->count)) {
Roberto Sassu47fdee62017-05-16 14:53:42 +0200452 int enforce_mask = ENFORCE_FIELDS;
453
454 enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
455 ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
456 hdr_mask, enforce_mask, "entry header");
457 if (ret < 0)
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800458 break;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800459
Roberto Sassu47fdee62017-05-16 14:53:42 +0200460 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800461 pr_err("attempting to restore a template name that is too long\n");
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800462 ret = -EINVAL;
463 break;
464 }
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800465
466 /* template name is not null terminated */
Roberto Sassu47fdee62017-05-16 14:53:42 +0200467 memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
468 hdr[HDR_TEMPLATE_NAME].len);
469 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800470
471 if (strcmp(template_name, "ima") == 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800472 pr_err("attempting to restore an unsupported template \"%s\" failed\n",
473 template_name);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800474 ret = -EINVAL;
475 break;
476 }
477
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800478 template_desc = lookup_template_desc(template_name);
479 if (!template_desc) {
Mimi Zoharc7d09362016-12-19 16:22:54 -0800480 template_desc = restore_template_fmt(template_name);
481 if (!template_desc)
482 break;
483 }
484
485 /*
486 * Only the running system's template format is initialized
487 * on boot. As needed, initialize the other template formats.
488 */
489 ret = template_desc_init_fields(template_desc->fmt,
490 &(template_desc->fields),
491 &(template_desc->num_fields));
492 if (ret < 0) {
Joe Perches72bf83b2017-11-16 07:27:29 -0800493 pr_err("attempting to restore the template fmt \"%s\" failed\n",
494 template_desc->fmt);
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800495 ret = -EINVAL;
496 break;
497 }
498
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800499 ret = ima_restore_template_data(template_desc,
Roberto Sassu47fdee62017-05-16 14:53:42 +0200500 hdr[HDR_TEMPLATE_DATA].data,
501 hdr[HDR_TEMPLATE_DATA].len,
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800502 &entry);
503 if (ret < 0)
504 break;
505
Roberto Sassu1ea973d2020-03-25 11:53:50 +0100506 if (memcmp(hdr[HDR_DIGEST].data, zero, sizeof(zero))) {
507 ret = ima_calc_field_array_hash(
508 &entry->template_data[0],
509 entry);
510 if (ret < 0) {
511 pr_err("cannot calculate template digest\n");
512 ret = -EINVAL;
513 break;
514 }
515 }
516
Li Huafei7990cca2021-03-03 11:28:24 +0800517 entry->pcr = !ima_canonical_fmt ? *(u32 *)(hdr[HDR_PCR].data) :
Roberto Sassu24c9ae22021-06-08 14:31:21 +0200518 le32_to_cpu(*(__le32 *)(hdr[HDR_PCR].data));
Mimi Zohar94c3aac2016-12-19 16:22:35 -0800519 ret = ima_restore_measurement_entry(entry);
520 if (ret < 0)
521 break;
522
523 }
524 return ret;
525}