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