Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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