GNU Linux-libre 4.14.290-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                 goto out;
271
272         spin_lock(&template_list);
273         list_add_tail_rcu(&template_desc->list, &defined_templates);
274         spin_unlock(&template_list);
275 out:
276         return template_desc;
277 }
278
279 static int ima_restore_template_data(struct ima_template_desc *template_desc,
280                                      void *template_data,
281                                      int template_data_size,
282                                      struct ima_template_entry **entry)
283 {
284         int ret = 0;
285         int i;
286
287         *entry = kzalloc(sizeof(**entry) +
288                     template_desc->num_fields * sizeof(struct ima_field_data),
289                     GFP_NOFS);
290         if (!*entry)
291                 return -ENOMEM;
292
293         ret = ima_parse_buf(template_data, template_data + template_data_size,
294                             NULL, template_desc->num_fields,
295                             (*entry)->template_data, NULL, NULL,
296                             ENFORCE_FIELDS | ENFORCE_BUFEND, "template data");
297         if (ret < 0) {
298                 kfree(*entry);
299                 return ret;
300         }
301
302         (*entry)->template_desc = template_desc;
303         for (i = 0; i < template_desc->num_fields; i++) {
304                 struct ima_field_data *field_data = &(*entry)->template_data[i];
305                 u8 *data = field_data->data;
306
307                 (*entry)->template_data[i].data =
308                         kzalloc(field_data->len + 1, GFP_KERNEL);
309                 if (!(*entry)->template_data[i].data) {
310                         ret = -ENOMEM;
311                         break;
312                 }
313                 memcpy((*entry)->template_data[i].data, data, field_data->len);
314                 (*entry)->template_data_len += sizeof(field_data->len);
315                 (*entry)->template_data_len += field_data->len;
316         }
317
318         if (ret < 0) {
319                 ima_free_template_entry(*entry);
320                 *entry = NULL;
321         }
322
323         return ret;
324 }
325
326 /* Restore the serialized binary measurement list without extending PCRs. */
327 int ima_restore_measurement_list(loff_t size, void *buf)
328 {
329         char template_name[MAX_TEMPLATE_NAME_LEN];
330
331         struct ima_kexec_hdr *khdr = buf;
332         struct ima_field_data hdr[HDR__LAST] = {
333                 [HDR_PCR] = {.len = sizeof(u32)},
334                 [HDR_DIGEST] = {.len = TPM_DIGEST_SIZE},
335         };
336
337         void *bufp = buf + sizeof(*khdr);
338         void *bufendp;
339         struct ima_template_entry *entry;
340         struct ima_template_desc *template_desc;
341         DECLARE_BITMAP(hdr_mask, HDR__LAST);
342         unsigned long count = 0;
343         int ret = 0;
344
345         if (!buf || size < sizeof(*khdr))
346                 return 0;
347
348         if (ima_canonical_fmt) {
349                 khdr->version = le16_to_cpu(khdr->version);
350                 khdr->count = le64_to_cpu(khdr->count);
351                 khdr->buffer_size = le64_to_cpu(khdr->buffer_size);
352         }
353
354         if (khdr->version != 1) {
355                 pr_err("attempting to restore a incompatible measurement list");
356                 return -EINVAL;
357         }
358
359         if (khdr->count > ULONG_MAX - 1) {
360                 pr_err("attempting to restore too many measurements");
361                 return -EINVAL;
362         }
363
364         bitmap_zero(hdr_mask, HDR__LAST);
365         bitmap_set(hdr_mask, HDR_PCR, 1);
366         bitmap_set(hdr_mask, HDR_DIGEST, 1);
367
368         /*
369          * ima kexec buffer prefix: version, buffer size, count
370          * v1 format: pcr, digest, template-name-len, template-name,
371          *            template-data-size, template-data
372          */
373         bufendp = buf + khdr->buffer_size;
374         while ((bufp < bufendp) && (count++ < khdr->count)) {
375                 int enforce_mask = ENFORCE_FIELDS;
376
377                 enforce_mask |= (count == khdr->count) ? ENFORCE_BUFEND : 0;
378                 ret = ima_parse_buf(bufp, bufendp, &bufp, HDR__LAST, hdr, NULL,
379                                     hdr_mask, enforce_mask, "entry header");
380                 if (ret < 0)
381                         break;
382
383                 if (hdr[HDR_TEMPLATE_NAME].len >= MAX_TEMPLATE_NAME_LEN) {
384                         pr_err("attempting to restore a template name \
385                                 that is too long\n");
386                         ret = -EINVAL;
387                         break;
388                 }
389
390                 /* template name is not null terminated */
391                 memcpy(template_name, hdr[HDR_TEMPLATE_NAME].data,
392                        hdr[HDR_TEMPLATE_NAME].len);
393                 template_name[hdr[HDR_TEMPLATE_NAME].len] = 0;
394
395                 if (strcmp(template_name, "ima") == 0) {
396                         pr_err("attempting to restore an unsupported \
397                                 template \"%s\" failed\n", template_name);
398                         ret = -EINVAL;
399                         break;
400                 }
401
402                 template_desc = lookup_template_desc(template_name);
403                 if (!template_desc) {
404                         template_desc = restore_template_fmt(template_name);
405                         if (!template_desc)
406                                 break;
407                 }
408
409                 /*
410                  * Only the running system's template format is initialized
411                  * on boot.  As needed, initialize the other template formats.
412                  */
413                 ret = template_desc_init_fields(template_desc->fmt,
414                                                 &(template_desc->fields),
415                                                 &(template_desc->num_fields));
416                 if (ret < 0) {
417                         pr_err("attempting to restore the template fmt \"%s\" \
418                                 failed\n", template_desc->fmt);
419                         ret = -EINVAL;
420                         break;
421                 }
422
423                 ret = ima_restore_template_data(template_desc,
424                                                 hdr[HDR_TEMPLATE_DATA].data,
425                                                 hdr[HDR_TEMPLATE_DATA].len,
426                                                 &entry);
427                 if (ret < 0)
428                         break;
429
430                 memcpy(entry->digest, hdr[HDR_DIGEST].data,
431                        hdr[HDR_DIGEST].len);
432                 entry->pcr = !ima_canonical_fmt ? *(hdr[HDR_PCR].data) :
433                              le32_to_cpu(*(hdr[HDR_PCR].data));
434                 ret = ima_restore_measurement_entry(entry);
435                 if (ret < 0)
436                         break;
437
438         }
439         return ret;
440 }