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