GNU Linux-libre 5.10.153-gnu1
[releases.git] / security / integrity / ima / ima_template.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Politecnico di Torino, Italy
4  *                    TORSEC group -- https://security.polito.it
5  *
6  * Author: Roberto Sassu <roberto.sassu@polito.it>
7  *
8  * File: ima_template.c
9  *      Helpers to manage template descriptors.
10  */
11
12 #include <linux/rculist.h>
13 #include "ima.h"
14 #include "ima_template_lib.h"
15
16 enum header_fields { HDR_PCR, HDR_DIGEST, HDR_TEMPLATE_NAME,
17                      HDR_TEMPLATE_DATA, HDR__LAST };
18
19 static struct ima_template_desc builtin_templates[] = {
20         {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
21         {.name = "ima-ng", .fmt = "d-ng|n-ng"},
22         {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
23         {.name = "ima-buf", .fmt = "d-ng|n-ng|buf"},
24         {.name = "ima-modsig", .fmt = "d-ng|n-ng|sig|d-modsig|modsig"},
25         {.name = "", .fmt = ""},        /* placeholder for a custom format */
26 };
27
28 static LIST_HEAD(defined_templates);
29 static DEFINE_SPINLOCK(template_list);
30 static int template_setup_done;
31
32 static const struct ima_template_field supported_fields[] = {
33         {.field_id = "d", .field_init = ima_eventdigest_init,
34          .field_show = ima_show_template_digest},
35         {.field_id = "n", .field_init = ima_eventname_init,
36          .field_show = ima_show_template_string},
37         {.field_id = "d-ng", .field_init = ima_eventdigest_ng_init,
38          .field_show = ima_show_template_digest_ng},
39         {.field_id = "n-ng", .field_init = ima_eventname_ng_init,
40          .field_show = ima_show_template_string},
41         {.field_id = "sig", .field_init = ima_eventsig_init,
42          .field_show = ima_show_template_sig},
43         {.field_id = "buf", .field_init = ima_eventbuf_init,
44          .field_show = ima_show_template_buf},
45         {.field_id = "d-modsig", .field_init = ima_eventdigest_modsig_init,
46          .field_show = ima_show_template_digest_ng},
47         {.field_id = "modsig", .field_init = ima_eventmodsig_init,
48          .field_show = ima_show_template_sig},
49 };
50
51 /*
52  * Used when restoring measurements carried over from a kexec. 'd' and 'n' don't
53  * need to be accounted for since they shouldn't be defined in the same template
54  * description as 'd-ng' and 'n-ng' respectively.
55  */
56 #define MAX_TEMPLATE_NAME_LEN sizeof("d-ng|n-ng|sig|buf|d-modisg|modsig")
57
58 static struct ima_template_desc *ima_template;
59
60 /**
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  */
67 bool 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
79 static int __init ima_template_setup(char *str)
80 {
81         struct ima_template_desc *template_desc;
82         int template_len = strlen(str);
83
84         if (template_setup_done)
85                 return 1;
86
87         if (!ima_template)
88                 ima_init_template_list();
89
90         /*
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);
95         if (!template_desc) {
96                 pr_err("template %s not found, using %s\n",
97                        str, CONFIG_IMA_DEFAULT_TEMPLATE);
98                 return 1;
99         }
100
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) {
107                 pr_err("template does not support hash alg\n");
108                 return 1;
109         }
110
111         ima_template = template_desc;
112         template_setup_done = 1;
113         return 1;
114 }
115 __setup("ima_template=", ima_template_setup);
116
117 static int __init ima_template_fmt_setup(char *str)
118 {
119         int num_templates = ARRAY_SIZE(builtin_templates);
120
121         if (template_setup_done)
122                 return 1;
123
124         if (template_desc_init_fields(str, NULL, NULL) < 0) {
125                 pr_err("format string '%s' not valid, using template %s\n",
126                        str, CONFIG_IMA_DEFAULT_TEMPLATE);
127                 return 1;
128         }
129
130         builtin_templates[num_templates - 1].fmt = str;
131         ima_template = builtin_templates + num_templates - 1;
132         template_setup_done = 1;
133
134         return 1;
135 }
136 __setup("ima_template_fmt=", ima_template_fmt_setup);
137
138 struct ima_template_desc *lookup_template_desc(const char *name)
139 {
140         struct ima_template_desc *template_desc;
141         int found = 0;
142
143         rcu_read_lock();
144         list_for_each_entry_rcu(template_desc, &defined_templates, list) {
145                 if ((strcmp(template_desc->name, name) == 0) ||
146                     (strcmp(template_desc->fmt, name) == 0)) {
147                         found = 1;
148                         break;
149                 }
150         }
151         rcu_read_unlock();
152         return found ? template_desc : NULL;
153 }
154
155 static const struct ima_template_field *
156 lookup_template_field(const char *field_id)
157 {
158         int i;
159
160         for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
161                 if (strncmp(supported_fields[i].field_id, field_id,
162                             IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
163                         return &supported_fields[i];
164         return NULL;
165 }
166
167 static int template_fmt_size(const char *template_fmt)
168 {
169         char c;
170         int template_fmt_len = strlen(template_fmt);
171         int i = 0, j = 0;
172
173         while (i < template_fmt_len) {
174                 c = template_fmt[i];
175                 if (c == '|')
176                         j++;
177                 i++;
178         }
179
180         return j + 1;
181 }
182
183 int template_desc_init_fields(const char *template_fmt,
184                               const struct ima_template_field ***fields,
185                               int *num_fields)
186 {
187         const char *template_fmt_ptr;
188         const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
189         int template_num_fields;
190         int i, len;
191
192         if (num_fields && *num_fields > 0) /* already initialized? */
193                 return 0;
194
195         template_num_fields = template_fmt_size(template_fmt);
196
197         if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
198                 pr_err("format string '%s' contains too many fields\n",
199                        template_fmt);
200                 return -EINVAL;
201         }
202
203         for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
204              i++, template_fmt_ptr += len + 1) {
205                 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
206
207                 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
208                 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
209                         pr_err("Invalid field with length %d\n", len);
210                         return -EINVAL;
211                 }
212
213                 memcpy(tmp_field_id, template_fmt_ptr, len);
214                 tmp_field_id[len] = '\0';
215                 found_fields[i] = lookup_template_field(tmp_field_id);
216                 if (!found_fields[i]) {
217                         pr_err("field '%s' not found\n", tmp_field_id);
218                         return -ENOENT;
219                 }
220         }
221
222         if (fields && num_fields) {
223                 *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
224                 if (*fields == NULL)
225                         return -ENOMEM;
226
227                 memcpy(*fields, found_fields, i * sizeof(*fields));
228                 *num_fields = i;
229         }
230
231         return 0;
232 }
233
234 void ima_init_template_list(void)
235 {
236         int i;
237
238         if (!list_empty(&defined_templates))
239                 return;
240
241         spin_lock(&template_list);
242         for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
243                 list_add_tail_rcu(&builtin_templates[i].list,
244                                   &defined_templates);
245         }
246         spin_unlock(&template_list);
247 }
248
249 struct ima_template_desc *ima_template_desc_current(void)
250 {
251         if (!ima_template) {
252                 ima_init_template_list();
253                 ima_template =
254                     lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
255         }
256         return ima_template;
257 }
258
259 int __init ima_init_template(void)
260 {
261         struct ima_template_desc *template = ima_template_desc_current();
262         int result;
263
264         result = template_desc_init_fields(template->fmt,
265                                            &(template->fields),
266                                            &(template->num_fields));
267         if (result < 0)
268                 pr_err("template %s init failed, result: %d\n",
269                        (strlen(template->name) ?
270                        template->name : template->fmt), result);
271
272         return result;
273 }
274
275 static struct ima_template_desc *restore_template_fmt(char *template_name)
276 {
277         struct ima_template_desc *template_desc = NULL;
278         int ret;
279
280         ret = template_desc_init_fields(template_name, NULL, NULL);
281         if (ret < 0) {
282                 pr_err("attempting to initialize the template \"%s\" failed\n",
283                         template_name);
284                 goto out;
285         }
286
287         template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
288         if (!template_desc)
289                 goto out;
290
291         template_desc->name = "";
292         template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
293         if (!template_desc->fmt)
294                 goto out;
295
296         spin_lock(&template_list);
297         list_add_tail_rcu(&template_desc->list, &defined_templates);
298         spin_unlock(&template_list);
299 out:
300         return template_desc;
301 }
302
303 static int ima_restore_template_data(struct ima_template_desc *template_desc,
304                                      void *template_data,
305                                      int template_data_size,
306                                      struct ima_template_entry **entry)
307 {
308         struct tpm_digest *digests;
309         int ret = 0;
310         int i;
311
312         *entry = kzalloc(struct_size(*entry, template_data,
313                                      template_desc->num_fields), GFP_NOFS);
314         if (!*entry)
315                 return -ENOMEM;
316
317         digests = kcalloc(NR_BANKS(ima_tpm_chip) + ima_extra_slots,
318                           sizeof(*digests), GFP_NOFS);
319         if (!digests) {
320                 kfree(*entry);
321                 return -ENOMEM;
322         }
323
324         (*entry)->digests = digests;
325
326         ret = ima_parse_buf(template_data, template_data + template_data_size,
327                             NULL, template_desc->num_fields,
328                             (*entry)->template_data, NULL, NULL,
329                             ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
330         if (ret < 0) {
331                 kfree((*entry)->digests);
332                 kfree(*entry);
333                 return ret;
334         }
335
336         (*entry)->template_desc = template_desc;
337         for (i = 0; i < template_desc->num_fields; i++) {
338                 struct ima_field_data *field_data = &(*entry)->template_data[i];
339                 u8 *data = field_data->data;
340
341                 (*entry)->template_data[i].data =
342                         kzalloc(field_data->len + 1, GFP_KERNEL);
343                 if (!(*entry)->template_data[i].data) {
344                         ret = -ENOMEM;
345                         break;
346                 }
347                 memcpy((*entry)->template_data[i].data, data, field_data->len);
348                 (*entry)->template_data_len += sizeof(field_data->len);
349                 (*entry)->template_data_len += field_data->len;
350         }
351
352         if (ret < 0) {
353                 ima_free_template_entry(*entry);
354                 *entry = NULL;
355         }
356
357         return ret;
358 }
359
360 /* Restore the serialized binary measurement list without extending PCRs. */
361 int ima_restore_measurement_list(loff_t size, void *buf)
362 {
363         char template_name[MAX_TEMPLATE_NAME_LEN];
364         unsigned char zero[TPM_DIGEST_SIZE] = { 0 };
365
366         struct ima_kexec_hdr *khdr = buf;
367         struct ima_field_data hdr[HDR__LAST] = {
368                 [HDR_PCR] = {.len = sizeof(u32)},
369                 [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
370         };
371
372         void *bufp = buf + sizeof(*khdr);
373         void *bufendp;
374         struct ima_template_entry *entry;
375         struct ima_template_desc *template_desc;
376         DECLARE_BITMAP(hdr_mask, HDR__LAST);
377         unsigned long count = 0;
378         int ret = 0;
379
380         if (!buf || size < sizeof(*khdr))
381                 return 0;
382
383         if (ima_canonical_fmt) {
384                 khdr->version = le16_to_cpu(khdr->version);
385                 khdr->count = le64_to_cpu(khdr->count);
386                 khdr->buffer_size = le64_to_cpu(khdr->buffer_size);
387         }
388
389         if (khdr->version != 1) {
390                 pr_err("attempting to restore a incompatible measurement list");
391                 return -EINVAL;
392         }
393
394         if (khdr->count > ULONG_MAX - 1) {
395                 pr_err("attempting to restore too many measurements");
396                 return -EINVAL;
397         }
398
399         bitmap_zero(hdr_mask, HDR__LAST);
400         bitmap_set(hdr_mask, HDR_PCR, 1);
401         bitmap_set(hdr_mask, HDR_DIGEST, 1);
402
403         /*
404          * ima kexec buffer prefix: version, buffer size, count
405          * v1 format: pcr, digest, template-name-len, template-name,
406          *            template-data-size, template-data
407          */
408         bufendp = buf + khdr->buffer_size;
409         while ((bufp < bufendp) && (count++ < khdr->count)) {
410                 int enforce_mask = ENFORCE_FIELDS;
411
412                 enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
413                 ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
414                                     hdr_mask, enforce_mask, "entry header");
415                 if (ret < 0)
416                         break;
417
418                 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
419                         pr_err("attempting to restore a template name that is too long\n");
420                         ret = -EINVAL;
421                         break;
422                 }
423
424                 /* template name is not null terminated */
425                 memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
426                        hdr[HDR_TEMPLATE_NAME].len);
427                 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
428
429                 if (strcmp(template_name, "ima") == 0) {
430                         pr_err("attempting to restore an unsupported template \"%s\" failed\n",
431                                template_name);
432                         ret = -EINVAL;
433                         break;
434                 }
435
436                 template_desc = lookup_template_desc(template_name);
437                 if (!template_desc) {
438                         template_desc = restore_template_fmt(template_name);
439                         if (!template_desc)
440                                 break;
441                 }
442
443                 /*
444                  * Only the running system's template format is initialized
445                  * on boot.  As needed, initialize the other template formats.
446                  */
447                 ret = template_desc_init_fields(template_desc->fmt,
448                                                 &(template_desc->fields),
449                                                 &(template_desc->num_fields));
450                 if (ret < 0) {
451                         pr_err("attempting to restore the template fmt \"%s\" failed\n",
452                                template_desc->fmt);
453                         ret = -EINVAL;
454                         break;
455                 }
456
457                 ret = ima_restore_template_data(template_desc,
458                                                 hdr[HDR_TEMPLATE_DATA].data,
459                                                 hdr[HDR_TEMPLATE_DATA].len,
460                                                 &entry);
461                 if (ret < 0)
462                         break;
463
464                 if (memcmp(hdr[HDR_DIGEST].data, zero, sizeof(zero))) {
465                         ret = ima_calc_field_array_hash(
466                                                 &entry->template_data[0],
467                                                 entry);
468                         if (ret < 0) {
469                                 pr_err("cannot calculate template digest\n");
470                                 ret = -EINVAL;
471                                 break;
472                         }
473                 }
474
475                 entry->pcr = !ima_canonical_fmt ? *(u32 *)(hdr[HDR_PCR].data) :
476                              le32_to_cpu(*(u32 *)(hdr[HDR_PCR].data));
477                 ret = ima_restore_measurement_entry(entry);
478                 if (ret < 0)
479                         break;
480
481         }
482         return ret;
483 }