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