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