ima: allocate field pointers array on demand in template_desc_init_fields()
[firefly-linux-kernel-4.4.55.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 <crypto/hash_info.h>
19
20 #include "ima.h"
21 #include "ima_template_lib.h"
22
23 static struct ima_template_desc defined_templates[] = {
24         {.name = IMA_TEMPLATE_IMA_NAME, .fmt = IMA_TEMPLATE_IMA_FMT},
25         {.name = "ima-ng", .fmt = "d-ng|n-ng"},
26         {.name = "ima-sig", .fmt = "d-ng|n-ng|sig"},
27 };
28
29 static struct ima_template_field supported_fields[] = {
30         {.field_id = "d", .field_init = ima_eventdigest_init,
31          .field_show = ima_show_template_digest},
32         {.field_id = "n", .field_init = ima_eventname_init,
33          .field_show = ima_show_template_string},
34         {.field_id = "d-ng", .field_init = ima_eventdigest_ng_init,
35          .field_show = ima_show_template_digest_ng},
36         {.field_id = "n-ng", .field_init = ima_eventname_ng_init,
37          .field_show = ima_show_template_string},
38         {.field_id = "sig", .field_init = ima_eventsig_init,
39          .field_show = ima_show_template_sig},
40 };
41
42 static struct ima_template_desc *ima_template;
43 static struct ima_template_desc *lookup_template_desc(const char *name);
44
45 static int __init ima_template_setup(char *str)
46 {
47         struct ima_template_desc *template_desc;
48         int template_len = strlen(str);
49
50         /*
51          * Verify that a template with the supplied name exists.
52          * If not, use CONFIG_IMA_DEFAULT_TEMPLATE.
53          */
54         template_desc = lookup_template_desc(str);
55         if (!template_desc) {
56                 pr_err("template %s not found, using %s\n",
57                        str, CONFIG_IMA_DEFAULT_TEMPLATE);
58                 return 1;
59         }
60
61         /*
62          * Verify whether the current hash algorithm is supported
63          * by the 'ima' template.
64          */
65         if (template_len == 3 && strcmp(str, IMA_TEMPLATE_IMA_NAME) == 0 &&
66             ima_hash_algo != HASH_ALGO_SHA1 && ima_hash_algo != HASH_ALGO_MD5) {
67                 pr_err("template does not support hash alg\n");
68                 return 1;
69         }
70
71         ima_template = template_desc;
72         return 1;
73 }
74 __setup("ima_template=", ima_template_setup);
75
76 static struct ima_template_desc *lookup_template_desc(const char *name)
77 {
78         int i;
79
80         for (i = 0; i < ARRAY_SIZE(defined_templates); i++) {
81                 if (strcmp(defined_templates[i].name, name) == 0)
82                         return defined_templates + i;
83         }
84
85         return NULL;
86 }
87
88 static struct ima_template_field *lookup_template_field(const char *field_id)
89 {
90         int i;
91
92         for (i = 0; i < ARRAY_SIZE(supported_fields); i++)
93                 if (strncmp(supported_fields[i].field_id, field_id,
94                             IMA_TEMPLATE_FIELD_ID_MAX_LEN) == 0)
95                         return &supported_fields[i];
96         return NULL;
97 }
98
99 static int template_fmt_size(const char *template_fmt)
100 {
101         char c;
102         int template_fmt_len = strlen(template_fmt);
103         int i = 0, j = 0;
104
105         while (i < template_fmt_len) {
106                 c = template_fmt[i];
107                 if (c == '|')
108                         j++;
109                 i++;
110         }
111
112         return j + 1;
113 }
114
115 static int template_desc_init_fields(const char *template_fmt,
116                                      struct ima_template_field ***fields,
117                                      int *num_fields)
118 {
119         const char *template_fmt_ptr;
120         struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
121         int template_num_fields = template_fmt_size(template_fmt);
122         int i, len;
123
124         if (template_num_fields > IMA_TEMPLATE_NUM_FIELDS_MAX) {
125                 pr_err("format string '%s' contains too many fields\n",
126                        template_fmt);
127                 return -EINVAL;
128         }
129
130         for (i = 0, template_fmt_ptr = template_fmt; i < template_num_fields;
131              i++, template_fmt_ptr += len + 1) {
132                 char tmp_field_id[IMA_TEMPLATE_FIELD_ID_MAX_LEN + 1];
133
134                 len = strchrnul(template_fmt_ptr, '|') - template_fmt_ptr;
135                 if (len == 0 || len > IMA_TEMPLATE_FIELD_ID_MAX_LEN) {
136                         pr_err("Invalid field with length %d\n", len);
137                         return -EINVAL;
138                 }
139
140                 memcpy(tmp_field_id, template_fmt_ptr, len);
141                 tmp_field_id[len] = '\0';
142                 found_fields[i] = lookup_template_field(tmp_field_id);
143                 if (!found_fields[i]) {
144                         pr_err("field '%s' not found\n", tmp_field_id);
145                         return -ENOENT;
146                 }
147         }
148
149         *fields = kmalloc_array(i, sizeof(*fields), GFP_KERNEL);
150         if (*fields == NULL)
151                 return -ENOMEM;
152
153         memcpy(*fields, found_fields, i * sizeof(*fields));
154         *num_fields = i;
155         return 0;
156 }
157
158 struct ima_template_desc *ima_template_desc_current(void)
159 {
160         if (!ima_template)
161                 ima_template =
162                     lookup_template_desc(CONFIG_IMA_DEFAULT_TEMPLATE);
163         return ima_template;
164 }
165
166 int __init ima_init_template(void)
167 {
168         struct ima_template_desc *template = ima_template_desc_current();
169         int result;
170
171         result = template_desc_init_fields(template->fmt,
172                                            &(template->fields),
173                                            &(template->num_fields));
174         if (result < 0)
175                 pr_err("template %s init failed, result: %d\n",
176                        (strlen(template->name) ?
177                        template->name : template->fmt), result);
178
179         return result;
180 }