1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/key.h>
3 #include <linux/keyctl.h>
4 #include <keys/user-type.h>
5 #include <linux/crash_dump.h>
6 #include <linux/cc_platform.h>
7 #include <linux/configfs.h>
8 #include <linux/module.h>
9 #include <linux/sysfs.h>
10
11 #define KEY_NUM_MAX 128 /* maximum dm crypt keys */
12 #define KEY_SIZE_MAX 256 /* maximum dm crypt key size */
13 #define KEY_DESC_MAX_LEN 128 /* maximum dm crypt key description size */
14
15 static unsigned int key_count;
16
17 struct dm_crypt_key {
18 unsigned int key_size;
19 char key_desc[KEY_DESC_MAX_LEN];
20 u8 data[KEY_SIZE_MAX];
21 };
22
23 static struct keys_header {
24 unsigned int total_keys;
25 struct dm_crypt_key keys[] __counted_by(total_keys);
26 } *keys_header;
27
get_keys_header_size(size_t total_keys)28 static size_t get_keys_header_size(size_t total_keys)
29 {
30 return struct_size(keys_header, keys, total_keys);
31 }
32
33 unsigned long long dm_crypt_keys_addr;
34 EXPORT_SYMBOL_GPL(dm_crypt_keys_addr);
35
setup_dmcryptkeys(char * arg)36 static int __init setup_dmcryptkeys(char *arg)
37 {
38 char *end;
39
40 if (!arg)
41 return -EINVAL;
42 dm_crypt_keys_addr = memparse(arg, &end);
43 if (end > arg)
44 return 0;
45
46 dm_crypt_keys_addr = 0;
47 return -EINVAL;
48 }
49
50 early_param("dmcryptkeys", setup_dmcryptkeys);
51
52 /*
53 * Architectures may override this function to read dm crypt keys
54 */
dm_crypt_keys_read(char * buf,size_t count,u64 * ppos)55 ssize_t __weak dm_crypt_keys_read(char *buf, size_t count, u64 *ppos)
56 {
57 struct kvec kvec = { .iov_base = buf, .iov_len = count };
58 struct iov_iter iter;
59
60 iov_iter_kvec(&iter, READ, &kvec, 1, count);
61 return read_from_oldmem(&iter, count, ppos, cc_platform_has(CC_ATTR_MEM_ENCRYPT));
62 }
63
add_key_to_keyring(struct dm_crypt_key * dm_key,key_ref_t keyring_ref)64 static int add_key_to_keyring(struct dm_crypt_key *dm_key,
65 key_ref_t keyring_ref)
66 {
67 key_ref_t key_ref;
68 int r;
69
70 /* create or update the requested key and add it to the target keyring */
71 key_ref = key_create_or_update(keyring_ref, "user", dm_key->key_desc,
72 dm_key->data, dm_key->key_size,
73 KEY_USR_ALL, KEY_ALLOC_IN_QUOTA);
74
75 if (!IS_ERR(key_ref)) {
76 r = key_ref_to_ptr(key_ref)->serial;
77 key_ref_put(key_ref);
78 kexec_dprintk("Success adding key %s", dm_key->key_desc);
79 } else {
80 r = PTR_ERR(key_ref);
81 kexec_dprintk("Error when adding key");
82 }
83
84 key_ref_put(keyring_ref);
85 return r;
86 }
87
get_keys_from_kdump_reserved_memory(void)88 static void get_keys_from_kdump_reserved_memory(void)
89 {
90 struct keys_header *keys_header_loaded;
91
92 arch_kexec_unprotect_crashkres();
93
94 keys_header_loaded = kmap_local_page(pfn_to_page(
95 kexec_crash_image->dm_crypt_keys_addr >> PAGE_SHIFT));
96
97 memcpy(keys_header, keys_header_loaded, get_keys_header_size(key_count));
98 kunmap_local(keys_header_loaded);
99 arch_kexec_protect_crashkres();
100 }
101
restore_dm_crypt_keys_to_thread_keyring(void)102 static int restore_dm_crypt_keys_to_thread_keyring(void)
103 {
104 struct dm_crypt_key *key;
105 size_t keys_header_size;
106 key_ref_t keyring_ref;
107 u64 addr;
108
109 /* find the target keyring (which must be writable) */
110 keyring_ref =
111 lookup_user_key(KEY_SPEC_USER_KEYRING, 0x01, KEY_NEED_WRITE);
112 if (IS_ERR(keyring_ref)) {
113 kexec_dprintk("Failed to get the user keyring\n");
114 return PTR_ERR(keyring_ref);
115 }
116
117 addr = dm_crypt_keys_addr;
118 dm_crypt_keys_read((char *)&key_count, sizeof(key_count), &addr);
119 if (key_count > KEY_NUM_MAX) {
120 kexec_dprintk("Failed to read the number of dm-crypt keys\n");
121 return -1;
122 }
123
124 kexec_dprintk("There are %u keys\n", key_count);
125 addr = dm_crypt_keys_addr;
126
127 keys_header_size = get_keys_header_size(key_count);
128 keys_header = kzalloc(keys_header_size, GFP_KERNEL);
129 if (!keys_header)
130 return -ENOMEM;
131
132 dm_crypt_keys_read((char *)keys_header, keys_header_size, &addr);
133
134 for (int i = 0; i < keys_header->total_keys; i++) {
135 key = &keys_header->keys[i];
136 kexec_dprintk("Get key (size=%u)\n", key->key_size);
137 add_key_to_keyring(key, keyring_ref);
138 }
139
140 return 0;
141 }
142
read_key_from_user_keyring(struct dm_crypt_key * dm_key)143 static int read_key_from_user_keyring(struct dm_crypt_key *dm_key)
144 {
145 const struct user_key_payload *ukp;
146 struct key *key;
147 int ret = 0;
148
149 kexec_dprintk("Requesting logon key %s", dm_key->key_desc);
150 key = request_key(&key_type_logon, dm_key->key_desc, NULL);
151
152 if (IS_ERR(key)) {
153 pr_warn("No such logon key %s\n", dm_key->key_desc);
154 return PTR_ERR(key);
155 }
156
157 down_read(&key->sem);
158 ukp = user_key_payload_locked(key);
159 if (!ukp) {
160 ret = -EKEYREVOKED;
161 goto out;
162 }
163
164 if (ukp->datalen > KEY_SIZE_MAX) {
165 pr_err("Key size %u exceeds maximum (%u)\n", ukp->datalen, KEY_SIZE_MAX);
166 ret = -EINVAL;
167 goto out;
168 }
169
170 memcpy(dm_key->data, ukp->data, ukp->datalen);
171 dm_key->key_size = ukp->datalen;
172 kexec_dprintk("Get dm crypt key (size=%u) %s\n", dm_key->key_size,
173 dm_key->key_desc);
174
175 out:
176 up_read(&key->sem);
177 key_put(key);
178 return ret;
179 }
180
181 struct config_key {
182 struct config_item item;
183 const char *description;
184 };
185
to_config_key(struct config_item * item)186 static inline struct config_key *to_config_key(struct config_item *item)
187 {
188 return container_of(item, struct config_key, item);
189 }
190
config_key_description_show(struct config_item * item,char * page)191 static ssize_t config_key_description_show(struct config_item *item, char *page)
192 {
193 return sysfs_emit(page, "%s\n", to_config_key(item)->description);
194 }
195
config_key_description_store(struct config_item * item,const char * page,size_t count)196 static ssize_t config_key_description_store(struct config_item *item,
197 const char *page, size_t count)
198 {
199 struct config_key *config_key = to_config_key(item);
200 size_t len;
201 int ret;
202
203 ret = -EINVAL;
204 len = strcspn(page, "\n");
205
206 if (len > KEY_DESC_MAX_LEN) {
207 pr_err("The key description shouldn't exceed %u characters", KEY_DESC_MAX_LEN);
208 return ret;
209 }
210
211 if (!len)
212 return ret;
213
214 kfree(config_key->description);
215 ret = -ENOMEM;
216 config_key->description = kmemdup_nul(page, len, GFP_KERNEL);
217 if (!config_key->description)
218 return ret;
219
220 return count;
221 }
222
223 CONFIGFS_ATTR(config_key_, description);
224
225 static struct configfs_attribute *config_key_attrs[] = {
226 &config_key_attr_description,
227 NULL,
228 };
229
config_key_release(struct config_item * item)230 static void config_key_release(struct config_item *item)
231 {
232 kfree(to_config_key(item));
233 key_count--;
234 }
235
236 static const struct configfs_item_operations config_key_item_ops = {
237 .release = config_key_release,
238 };
239
240 static const struct config_item_type config_key_type = {
241 .ct_item_ops = &config_key_item_ops,
242 .ct_attrs = config_key_attrs,
243 .ct_owner = THIS_MODULE,
244 };
245
config_keys_make_item(struct config_group * group,const char * name)246 static struct config_item *config_keys_make_item(struct config_group *group,
247 const char *name)
248 {
249 struct config_key *config_key;
250
251 if (key_count > KEY_NUM_MAX) {
252 pr_err("Only %u keys at maximum to be created\n", KEY_NUM_MAX);
253 return ERR_PTR(-EINVAL);
254 }
255
256 config_key = kzalloc_obj(struct config_key);
257 if (!config_key)
258 return ERR_PTR(-ENOMEM);
259
260 config_item_init_type_name(&config_key->item, name, &config_key_type);
261
262 key_count++;
263
264 return &config_key->item;
265 }
266
config_keys_count_show(struct config_item * item,char * page)267 static ssize_t config_keys_count_show(struct config_item *item, char *page)
268 {
269 return sysfs_emit(page, "%d\n", key_count);
270 }
271
272 CONFIGFS_ATTR_RO(config_keys_, count);
273
274 static bool is_dm_key_reused;
275
config_keys_reuse_show(struct config_item * item,char * page)276 static ssize_t config_keys_reuse_show(struct config_item *item, char *page)
277 {
278 return sysfs_emit(page, "%d\n", is_dm_key_reused);
279 }
280
config_keys_reuse_store(struct config_item * item,const char * page,size_t count)281 static ssize_t config_keys_reuse_store(struct config_item *item,
282 const char *page, size_t count)
283 {
284 if (!kexec_crash_image || !kexec_crash_image->dm_crypt_keys_addr) {
285 kexec_dprintk(
286 "dm-crypt keys haven't be saved to crash-reserved memory\n");
287 return -EINVAL;
288 }
289
290 if (kstrtobool(page, &is_dm_key_reused))
291 return -EINVAL;
292
293 if (is_dm_key_reused)
294 get_keys_from_kdump_reserved_memory();
295
296 return count;
297 }
298
299 CONFIGFS_ATTR(config_keys_, reuse);
300
301 static struct configfs_attribute *config_keys_attrs[] = {
302 &config_keys_attr_count,
303 &config_keys_attr_reuse,
304 NULL,
305 };
306
307 /*
308 * Note that, since no extra work is required on ->drop_item(),
309 * no ->drop_item() is provided.
310 */
311 static const struct configfs_group_operations config_keys_group_ops = {
312 .make_item = config_keys_make_item,
313 };
314
315 static const struct config_item_type config_keys_type = {
316 .ct_group_ops = &config_keys_group_ops,
317 .ct_attrs = config_keys_attrs,
318 .ct_owner = THIS_MODULE,
319 };
320
321 static bool restore;
322
config_keys_restore_show(struct config_item * item,char * page)323 static ssize_t config_keys_restore_show(struct config_item *item, char *page)
324 {
325 return sysfs_emit(page, "%d\n", restore);
326 }
327
config_keys_restore_store(struct config_item * item,const char * page,size_t count)328 static ssize_t config_keys_restore_store(struct config_item *item,
329 const char *page, size_t count)
330 {
331 if (!restore)
332 restore_dm_crypt_keys_to_thread_keyring();
333
334 if (kstrtobool(page, &restore))
335 return -EINVAL;
336
337 return count;
338 }
339
340 CONFIGFS_ATTR(config_keys_, restore);
341
342 static struct configfs_attribute *kdump_config_keys_attrs[] = {
343 &config_keys_attr_restore,
344 NULL,
345 };
346
347 static const struct config_item_type kdump_config_keys_type = {
348 .ct_attrs = kdump_config_keys_attrs,
349 .ct_owner = THIS_MODULE,
350 };
351
352 static struct configfs_subsystem config_keys_subsys = {
353 .su_group = {
354 .cg_item = {
355 .ci_namebuf = "crash_dm_crypt_keys",
356 .ci_type = &config_keys_type,
357 },
358 },
359 };
360
build_keys_header(void)361 static int build_keys_header(void)
362 {
363 struct config_item *item = NULL;
364 struct config_key *key;
365 int i, r;
366
367 if (keys_header != NULL)
368 kvfree(keys_header);
369
370 keys_header = kzalloc(get_keys_header_size(key_count), GFP_KERNEL);
371 if (!keys_header)
372 return -ENOMEM;
373
374 keys_header->total_keys = key_count;
375
376 i = 0;
377 list_for_each_entry(item, &config_keys_subsys.su_group.cg_children,
378 ci_entry) {
379 if (item->ci_type != &config_key_type)
380 continue;
381
382 key = to_config_key(item);
383
384 if (!key->description) {
385 pr_warn("No key description for key %s\n", item->ci_name);
386 return -EINVAL;
387 }
388
389 strscpy(keys_header->keys[i].key_desc, key->description,
390 KEY_DESC_MAX_LEN);
391 r = read_key_from_user_keyring(&keys_header->keys[i]);
392 if (r != 0) {
393 kexec_dprintk("Failed to read key %s\n",
394 keys_header->keys[i].key_desc);
395 return r;
396 }
397 i++;
398 kexec_dprintk("Found key: %s\n", item->ci_name);
399 }
400
401 return 0;
402 }
403
crash_load_dm_crypt_keys(struct kimage * image)404 int crash_load_dm_crypt_keys(struct kimage *image)
405 {
406 struct kexec_buf kbuf = {
407 .image = image,
408 .buf_min = 0,
409 .buf_max = ULONG_MAX,
410 .top_down = false,
411 .random = true,
412 };
413 int r;
414
415
416 if (key_count <= 0) {
417 kexec_dprintk("No dm-crypt keys\n");
418 return 0;
419 }
420
421 if (!is_dm_key_reused) {
422 image->dm_crypt_keys_addr = 0;
423 r = build_keys_header();
424 if (r) {
425 pr_err("Failed to build dm-crypt keys header, ret=%d\n", r);
426 return r;
427 }
428 }
429
430 kbuf.buffer = keys_header;
431 kbuf.bufsz = get_keys_header_size(key_count);
432
433 kbuf.memsz = kbuf.bufsz;
434 kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
435 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
436 r = kexec_add_buffer(&kbuf);
437 if (r) {
438 pr_err("Failed to call kexec_add_buffer, ret=%d\n", r);
439 kvfree((void *)kbuf.buffer);
440 return r;
441 }
442 image->dm_crypt_keys_addr = kbuf.mem;
443 image->dm_crypt_keys_sz = kbuf.bufsz;
444 kexec_dprintk(
445 "Loaded dm crypt keys to kexec_buffer bufsz=0x%lx memsz=0x%lx\n",
446 kbuf.bufsz, kbuf.memsz);
447
448 return r;
449 }
450
configfs_dmcrypt_keys_init(void)451 static int __init configfs_dmcrypt_keys_init(void)
452 {
453 int ret;
454
455 if (is_kdump_kernel()) {
456 config_keys_subsys.su_group.cg_item.ci_type =
457 &kdump_config_keys_type;
458 }
459
460 config_group_init(&config_keys_subsys.su_group);
461 mutex_init(&config_keys_subsys.su_mutex);
462 ret = configfs_register_subsystem(&config_keys_subsys);
463 if (ret) {
464 pr_err("Error %d while registering subsystem %s\n", ret,
465 config_keys_subsys.su_group.cg_item.ci_namebuf);
466 goto out_unregister;
467 }
468
469 return 0;
470
471 out_unregister:
472 configfs_unregister_subsystem(&config_keys_subsys);
473
474 return ret;
475 }
476
477 module_init(configfs_dmcrypt_keys_init);
478