GNU Linux-libre 4.19.295-gnu1
[releases.git] / security / integrity / ima / ima_template.c
1 /*
2  * Copyright (C) 2013 Politecnico di Torino, Italy
3  *                    TORSEC group -- http://security.polito.it
4  *
5  * Author: Roberto Sassu <roberto.sassu@polito.it>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation, version 2 of the
10  * License.
11  *
12  * File: ima_template.c
13  *      Helpers to manage template descriptors.
14  */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18 #include <linux/rculist.h>
19 #include "ima.h"
20 #include "ima_template_lib.h"
21
22 enum header_fields { HDR_PCR, HDR_DIGEST, HDR_TEMPLATE_NAME,
23                      HDR_TEMPLATE_DATA, HDR__LAST };
24
25 static struct ima_template_desc builtin_templates[] = {
26         {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
27         {.name = "ima-ng", .fmt = "d-ng|n-ng"},
28         {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
29         {.name = "", .fmt = ""},        /* placeholder for a custom format */
30 };
31
32 static LIST_HEAD(defined_templates);
33 static DEFINE_SPINLOCK(template_list);
34 static int template_setup_done;
35
36 static struct ima_template_field supported_fields[] = {
37         {.field_id = "d", .field_init = ima_eventdigest_init,
38          .field_show = ima_show_template_digest},
39         {.field_id = "n", .field_init = ima_eventname_init,
40          .field_show = ima_show_template_string},
41         {.field_id = "d-ng", .field_init = ima_eventdigest_ng_init,
42          .field_show = ima_show_template_digest_ng},
43         {.field_id = "n-ng", .field_init = ima_eventname_ng_init,
44          .field_show = ima_show_template_string},
45         {.field_id = "sig", .field_init = ima_eventsig_init,
46          .field_show = ima_show_template_sig},
47 };
48 #define MAX_TEMPLATE_NAME_LEN 15
49
50 static struct ima_template_desc *ima_template;
51 static struct ima_template_desc *lookup_template_desc(const char *name);
52 static int template_desc_init_fields(const char *template_fmt,
53                                      struct ima_template_field ***fields,
54                                      int *num_fields);
55
56 static int __init ima_template_setup(char *str)
57 {
58         struct ima_template_desc *template_desc;
59         int template_len = strlen(str);
60
61         if (template_setup_done)
62                 return 1;
63
64         if (!ima_template)
65                 ima_init_template_list();
66
67         /*
68          * Verify that a template with the supplied name exists.
69          * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
70          */
71         template_desc = lookup_template_desc(str);
72         if (!template_desc) {
73                 pr_err("template %s not found, using %s\n",
74                        str, CONFIG_IMA_DEFAULT_TEMPLATE);
75                 return 1;
76         }
77
78         /*
79          * Verify whether the current hash algorithm is supported
80          * by the 'ima' template.
81          */
82         if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
83             ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
84                 pr_err("template does not support hash alg\n");
85                 return 1;
86         }
87
88         ima_template = template_desc;
89         template_setup_done = 1;
90         return 1;
91 }
92 __setup("ima_template=", ima_template_setup);
93
94 static int __init ima_template_fmt_setup(char *str)
95 {
96         int num_templates = ARRAY_SIZE(builtin_templates);
97
98         if (template_setup_done)
99                 return 1;
100
101         if (template_desc_init_fields(str, NULL, NULL) < 0) {
102                 pr_err("format string '%s' not valid, using template %s\n",
103                        str, CONFIG_IMA_DEFAULT_TEMPLATE);
104                 return 1;
105         }
106
107         builtin_templates[num_templates - 1].fmt = str;
108         ima_template = builtin_templates + num_templates - 1;
109         template_setup_done = 1;
110
111         return 1;
112 }
113 __setup("ima_template_fmt=", ima_template_fmt_setup);
114
115 static struct ima_template_desc *lookup_template_desc(const char *name)
116 {
117         struct ima_template_desc *template_desc;
118         int found = 0;
119
120         rcu_read_lock();
121         list_for_each_entry_rcu(template_desc, &defined_templates, list) {
122                 if ((strcmp(template_desc->name, name) == 0) ||
123                     (strcmp(template_desc->fmt, name) == 0)) {
124                         found = 1;
125                         break;
126                 }
127         }
128         rcu_read_unlock();
129         return found ? template_desc : NULL;
130 }
131
132 static struct ima_template_field *lookup_template_field(const char *field_id)
133 {
134         int i;
135
136         for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
137                 if (strncmp(supported_fields[i].field_id, field_id,
138                             IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
139                         return &supported_fields[i];
140         return NULL;
141 }
142
143 static int template_fmt_size(const char *template_fmt)
144 {
145         char c;
146         int template_fmt_len = strlen(template_fmt);
147         int i = 0, j = 0;
148
149         while (i < template_fmt_len) {
150                 c = template_fmt[i];
151                 if (c == '|')
152                         j++;
153                 i++;
154         }
155
156         return j + 1;
157 }
158
159 static int template_desc_init_fields(const char *template_fmt,
160                                      struct ima_template_field ***fields,
161                                      int *num_fields)
162 {
163         const char *template_fmt_ptr;
164         struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
165         int template_num_fields;
166         int i, len;
167
168         if (num_fields && *num_fields > 0) /* already initialized? */
169                 return 0;
170
171         template_num_fields = template_fmt_size(template_fmt);
172
173         if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
174                 pr_err("format string '%s' contains too many fields\n",
175                        template_fmt);
176                 return -EINVAL;
177         }
178
179         for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
180              i++, template_fmt_ptr += len + 1) {
181                 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
182
183                 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
184                 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
185                         pr_err("Invalid field with length %d\n", len);
186                         return -EINVAL;
187                 }
188
189                 memcpy(tmp_field_id, template_fmt_ptr, len);
190                 tmp_field_id[len] = '\0';
191                 found_fields[i] = lookup_template_field(tmp_field_id);
192                 if (!found_fields[i]) {
193                         pr_err("field '%s' not found\n", tmp_field_id);
194                         return -ENOENT;
195                 }
196         }
197
198         if (fields && num_fields) {
199                 *fields = kmalloc_array(i, sizeof(**fields), GFP_KERNEL);
200                 if (*fields == NULL)
201                         return -ENOMEM;
202
203                 memcpy(*fields, found_fields, i * sizeof(**fields));
204                 *num_fields = i;
205         }
206
207         return 0;
208 }
209
210 void ima_init_template_list(void)
211 {
212         int i;
213
214         if (!list_empty(&defined_templates))
215                 return;
216
217         spin_lock(&template_list);
218         for (i = 0; i < ARRAY_SIZE(builtin_templates); i++) {
219                 list_add_tail_rcu(&builtin_templates[i].list,
220                                   &defined_templates);
221         }
222         spin_unlock(&template_list);
223 }
224
225 struct ima_template_desc *ima_template_desc_current(void)
226 {
227         if (!ima_template) {
228                 ima_init_template_list();
229                 ima_template =
230                     lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
231         }
232         return ima_template;
233 }
234
235 int __init ima_init_template(void)
236 {
237         struct ima_template_desc *template = ima_template_desc_current();
238         int result;
239
240         result = template_desc_init_fields(template->fmt,
241                                            &(template->fields),
242                                            &(template->num_fields));
243         if (result < 0)
244                 pr_err("template %s init failed, result: %d\n",
245                        (strlen(template->name) ?
246                        template->name : template->fmt), result);
247
248         return result;
249 }
250
251 static struct ima_template_desc *restore_template_fmt(char *template_name)
252 {
253         struct ima_template_desc *template_desc = NULL;
254         int ret;
255
256         ret = template_desc_init_fields(template_name, NULL, NULL);
257         if (ret < 0) {
258                 pr_err("attempting to initialize the template \"%s\" failed\n",
259                         template_name);
260                 goto out;
261         }
262
263         template_desc = kzalloc(sizeof(*template_desc), GFP_KERNEL);
264         if (!template_desc)
265                 goto out;
266
267         template_desc->name = "";
268         template_desc->fmt = kstrdup(template_name, GFP_KERNEL);
269         if (!template_desc->fmt) {
270                 kfree(template_desc);
271                 template_desc = NULL;
272                 goto out;
273         }
274
275         spin_lock(&template_list);
276         list_add_tail_rcu(&template_desc->list, &defined_templates);
277         spin_unlock(&template_list);
278 out:
279         return template_desc;
280 }
281
282 static 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 {
287         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
296         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
305         (*entry)->template_desc = template_desc;
306         for (i = 0; i < template_desc->num_fields; i++) {
307                 struct ima_field_data *field_data = &(*entry)->template_data[i];
308                 u8 *data = field_data->data;
309
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                 }
316                 memcpy((*entry)->template_data[i].data, data, field_data->len);
317                 (*entry)->template_data_len += sizeof(field_data->len);
318                 (*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. */
330 int ima_restore_measurement_list(loff_t size, void *buf)
331 {
332         char template_name[MAX_TEMPLATE_NAME_LEN];
333
334         struct ima_kexec_hdr *khdr = buf;
335         struct ima_field_data hdr[HDR__LAST] = {
336                 [HDR_PCR] = {.len = sizeof(u32)},
337                 [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
338         };
339
340         void *bufp = buf + sizeof(*khdr);
341         void *bufendp;
342         struct ima_template_entry *entry;
343         struct ima_template_desc *template_desc;
344         DECLARE_BITMAP(hdr_mask, HDR__LAST);
345         unsigned long count = 0;
346         int ret = 0;
347
348         if (!buf || size < sizeof(*khdr))
349                 return 0;
350
351         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
357         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
367         bitmap_zero(hdr_mask, HDR__LAST);
368         bitmap_set(hdr_mask, HDR_PCR, 1);
369         bitmap_set(hdr_mask, HDR_DIGEST, 1);
370
371         /*
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          */
376         bufendp = buf + khdr->buffer_size;
377         while ((bufp < bufendp) && (count++ < khdr->count)) {
378                 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)
384                         break;
385
386                 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
387                         pr_err("attempting to restore a template name that is too long\n");
388                         ret = -EINVAL;
389                         break;
390                 }
391
392                 /* template name is not null terminated */
393                 memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
394                        hdr[HDR_TEMPLATE_NAME].len);
395                 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
396
397                 if (strcmp(template_name, "ima") == 0) {
398                         pr_err("attempting to restore an unsupported template \"%s\" failed\n",
399                                template_name);
400                         ret = -EINVAL;
401                         break;
402                 }
403
404                 template_desc = lookup_template_desc(template_name);
405                 if (!template_desc) {
406                         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) {
419                         pr_err("attempting to restore the template fmt \"%s\" failed\n",
420                                template_desc->fmt);
421                         ret = -EINVAL;
422                         break;
423                 }
424
425                 ret = ima_restore_template_data(template_desc,
426                                                 hdr[HDR_TEMPLATE_DATA].data,
427                                                 hdr[HDR_TEMPLATE_DATA].len,
428                                                 &entry);
429                 if (ret < 0)
430                         break;
431
432                 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));
436                 ret = ima_restore_measurement_entry(entry);
437                 if (ret < 0)
438                         break;
439
440         }
441         return ret;
442 }