blob: b17998fa03f125b3c5e1d64593ec45be13b6d696 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * kexec: kexec_file_load system call
4 *
5 * Copyright (C) 2014 Red Hat Inc.
6 * Authors:
7 * Vivek Goyal <vgoyal@redhat.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/capability.h>
13#include <linux/mm.h>
14#include <linux/file.h>
15#include <linux/slab.h>
16#include <linux/kexec.h>
David Brazdil0f672f62019-12-10 10:32:29 +000017#include <linux/memblock.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018#include <linux/mutex.h>
19#include <linux/list.h>
20#include <linux/fs.h>
21#include <linux/ima.h>
22#include <crypto/hash.h>
23#include <crypto/sha.h>
24#include <linux/elf.h>
25#include <linux/elfcore.h>
26#include <linux/kernel.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027#include <linux/syscalls.h>
28#include <linux/vmalloc.h>
29#include "kexec_internal.h"
30
31static int kexec_calculate_store_digests(struct kimage *image);
32
33/*
34 * Currently this is the only default function that is exported as some
35 * architectures need it to do additional handlings.
36 * In the future, other default functions may be exported too if required.
37 */
38int kexec_image_probe_default(struct kimage *image, void *buf,
39 unsigned long buf_len)
40{
41 const struct kexec_file_ops * const *fops;
42 int ret = -ENOEXEC;
43
44 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
45 ret = (*fops)->probe(buf, buf_len);
46 if (!ret) {
47 image->fops = *fops;
48 return ret;
49 }
50 }
51
52 return ret;
53}
54
55/* Architectures can provide this probe function */
56int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
57 unsigned long buf_len)
58{
59 return kexec_image_probe_default(image, buf, buf_len);
60}
61
62static void *kexec_image_load_default(struct kimage *image)
63{
64 if (!image->fops || !image->fops->load)
65 return ERR_PTR(-ENOEXEC);
66
67 return image->fops->load(image, image->kernel_buf,
68 image->kernel_buf_len, image->initrd_buf,
69 image->initrd_buf_len, image->cmdline_buf,
70 image->cmdline_buf_len);
71}
72
73void * __weak arch_kexec_kernel_image_load(struct kimage *image)
74{
75 return kexec_image_load_default(image);
76}
77
David Brazdil0f672f62019-12-10 10:32:29 +000078int kexec_image_post_load_cleanup_default(struct kimage *image)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079{
80 if (!image->fops || !image->fops->cleanup)
81 return 0;
82
83 return image->fops->cleanup(image->image_loader_data);
84}
85
86int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
87{
88 return kexec_image_post_load_cleanup_default(image);
89}
90
David Brazdil0f672f62019-12-10 10:32:29 +000091#ifdef CONFIG_KEXEC_SIG
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
93 unsigned long buf_len)
94{
95 if (!image->fops || !image->fops->verify_sig) {
96 pr_debug("kernel loader does not support signature verification.\n");
97 return -EKEYREJECTED;
98 }
99
100 return image->fops->verify_sig(buf, buf_len);
101}
102
103int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
104 unsigned long buf_len)
105{
106 return kexec_image_verify_sig_default(image, buf, buf_len);
107}
108#endif
109
110/*
111 * arch_kexec_apply_relocations_add - apply relocations of type RELA
112 * @pi: Purgatory to be relocated.
113 * @section: Section relocations applying to.
114 * @relsec: Section containing RELAs.
115 * @symtab: Corresponding symtab.
116 *
117 * Return: 0 on success, negative errno on error.
118 */
119int __weak
120arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section,
121 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
122{
123 pr_err("RELA relocation unsupported.\n");
124 return -ENOEXEC;
125}
126
127/*
128 * arch_kexec_apply_relocations - apply relocations of type REL
129 * @pi: Purgatory to be relocated.
130 * @section: Section relocations applying to.
131 * @relsec: Section containing RELs.
132 * @symtab: Corresponding symtab.
133 *
134 * Return: 0 on success, negative errno on error.
135 */
136int __weak
137arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section,
138 const Elf_Shdr *relsec, const Elf_Shdr *symtab)
139{
140 pr_err("REL relocation unsupported.\n");
141 return -ENOEXEC;
142}
143
144/*
145 * Free up memory used by kernel, initrd, and command line. This is temporary
146 * memory allocation which is not needed any more after these buffers have
147 * been loaded into separate segments and have been copied elsewhere.
148 */
149void kimage_file_post_load_cleanup(struct kimage *image)
150{
151 struct purgatory_info *pi = &image->purgatory_info;
152
153 vfree(image->kernel_buf);
154 image->kernel_buf = NULL;
155
156 vfree(image->initrd_buf);
157 image->initrd_buf = NULL;
158
159 kfree(image->cmdline_buf);
160 image->cmdline_buf = NULL;
161
162 vfree(pi->purgatory_buf);
163 pi->purgatory_buf = NULL;
164
165 vfree(pi->sechdrs);
166 pi->sechdrs = NULL;
167
Olivier Deprez0e641232021-09-23 10:07:05 +0200168#ifdef CONFIG_IMA_KEXEC
169 vfree(image->ima_buffer);
170 image->ima_buffer = NULL;
171#endif /* CONFIG_IMA_KEXEC */
172
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000173 /* See if architecture has anything to cleanup post load */
174 arch_kimage_file_post_load_cleanup(image);
175
176 /*
177 * Above call should have called into bootloader to free up
178 * any data stored in kimage->image_loader_data. It should
179 * be ok now to free it up.
180 */
181 kfree(image->image_loader_data);
182 image->image_loader_data = NULL;
183}
184
David Brazdil0f672f62019-12-10 10:32:29 +0000185#ifdef CONFIG_KEXEC_SIG
186static int
187kimage_validate_signature(struct kimage *image)
188{
189 const char *reason;
190 int ret;
191
192 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
193 image->kernel_buf_len);
194 switch (ret) {
195 case 0:
196 break;
197
198 /* Certain verification errors are non-fatal if we're not
199 * checking errors, provided we aren't mandating that there
200 * must be a valid signature.
201 */
202 case -ENODATA:
203 reason = "kexec of unsigned image";
204 goto decide;
205 case -ENOPKG:
206 reason = "kexec of image with unsupported crypto";
207 goto decide;
208 case -ENOKEY:
209 reason = "kexec of image with unavailable key";
210 decide:
211 if (IS_ENABLED(CONFIG_KEXEC_SIG_FORCE)) {
212 pr_notice("%s rejected\n", reason);
213 return ret;
214 }
215
216 /* If IMA is guaranteed to appraise a signature on the kexec
217 * image, permit it even if the kernel is otherwise locked
218 * down.
219 */
220 if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
221 security_locked_down(LOCKDOWN_KEXEC))
222 return -EPERM;
223
224 return 0;
225
226 /* All other errors are fatal, including nomem, unparseable
227 * signatures and signature check failures - even if signatures
228 * aren't required.
229 */
230 default:
231 pr_notice("kernel signature verification failed (%d).\n", ret);
232 }
233
234 return ret;
235}
236#endif
237
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238/*
239 * In file mode list of segments is prepared by kernel. Copy relevant
240 * data from user space, do error checking, prepare segment list
241 */
242static int
243kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
244 const char __user *cmdline_ptr,
245 unsigned long cmdline_len, unsigned flags)
246{
David Brazdil0f672f62019-12-10 10:32:29 +0000247 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000248 void *ldata;
249 loff_t size;
250
251 ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
252 &size, INT_MAX, READING_KEXEC_IMAGE);
253 if (ret)
254 return ret;
255 image->kernel_buf_len = size;
256
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257 /* Call arch image probe handlers */
258 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
259 image->kernel_buf_len);
260 if (ret)
261 goto out;
262
David Brazdil0f672f62019-12-10 10:32:29 +0000263#ifdef CONFIG_KEXEC_SIG
264 ret = kimage_validate_signature(image);
265
266 if (ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268#endif
269 /* It is possible that there no initramfs is being loaded */
270 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
271 ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
272 &size, INT_MAX,
273 READING_KEXEC_INITRAMFS);
274 if (ret)
275 goto out;
276 image->initrd_buf_len = size;
277 }
278
279 if (cmdline_len) {
280 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
281 if (IS_ERR(image->cmdline_buf)) {
282 ret = PTR_ERR(image->cmdline_buf);
283 image->cmdline_buf = NULL;
284 goto out;
285 }
286
287 image->cmdline_buf_len = cmdline_len;
288
289 /* command line should be a string with last byte null */
290 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
291 ret = -EINVAL;
292 goto out;
293 }
David Brazdil0f672f62019-12-10 10:32:29 +0000294
295 ima_kexec_cmdline(image->cmdline_buf,
296 image->cmdline_buf_len - 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297 }
298
David Brazdil0f672f62019-12-10 10:32:29 +0000299 /* IMA needs to pass the measurement list to the next kernel. */
300 ima_add_kexec_buffer(image);
301
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302 /* Call arch image load handlers */
303 ldata = arch_kexec_kernel_image_load(image);
304
305 if (IS_ERR(ldata)) {
306 ret = PTR_ERR(ldata);
307 goto out;
308 }
309
310 image->image_loader_data = ldata;
311out:
312 /* In case of error, free up all allocated memory in this function */
313 if (ret)
314 kimage_file_post_load_cleanup(image);
315 return ret;
316}
317
318static int
319kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
320 int initrd_fd, const char __user *cmdline_ptr,
321 unsigned long cmdline_len, unsigned long flags)
322{
323 int ret;
324 struct kimage *image;
325 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
326
327 image = do_kimage_alloc_init();
328 if (!image)
329 return -ENOMEM;
330
331 image->file_mode = 1;
332
333 if (kexec_on_panic) {
334 /* Enable special crash kernel control page alloc policy. */
335 image->control_page = crashk_res.start;
336 image->type = KEXEC_TYPE_CRASH;
337 }
338
339 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
340 cmdline_ptr, cmdline_len, flags);
341 if (ret)
342 goto out_free_image;
343
344 ret = sanity_check_segment_list(image);
345 if (ret)
346 goto out_free_post_load_bufs;
347
348 ret = -ENOMEM;
349 image->control_code_page = kimage_alloc_control_pages(image,
350 get_order(KEXEC_CONTROL_PAGE_SIZE));
351 if (!image->control_code_page) {
352 pr_err("Could not allocate control_code_buffer\n");
353 goto out_free_post_load_bufs;
354 }
355
356 if (!kexec_on_panic) {
357 image->swap_page = kimage_alloc_control_pages(image, 0);
358 if (!image->swap_page) {
359 pr_err("Could not allocate swap buffer\n");
360 goto out_free_control_pages;
361 }
362 }
363
364 *rimage = image;
365 return 0;
366out_free_control_pages:
367 kimage_free_page_list(&image->control_pages);
368out_free_post_load_bufs:
369 kimage_file_post_load_cleanup(image);
370out_free_image:
371 kfree(image);
372 return ret;
373}
374
375SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
376 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
377 unsigned long, flags)
378{
379 int ret = 0, i;
380 struct kimage **dest_image, *image;
381
382 /* We only trust the superuser with rebooting the system. */
383 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
384 return -EPERM;
385
386 /* Make sure we have a legal set of flags */
387 if (flags != (flags & KEXEC_FILE_FLAGS))
388 return -EINVAL;
389
390 image = NULL;
391
392 if (!mutex_trylock(&kexec_mutex))
393 return -EBUSY;
394
395 dest_image = &kexec_image;
396 if (flags & KEXEC_FILE_ON_CRASH) {
397 dest_image = &kexec_crash_image;
398 if (kexec_crash_image)
399 arch_kexec_unprotect_crashkres();
400 }
401
402 if (flags & KEXEC_FILE_UNLOAD)
403 goto exchange;
404
405 /*
406 * In case of crash, new kernel gets loaded in reserved region. It is
407 * same memory where old crash kernel might be loaded. Free any
408 * current crash dump kernel before we corrupt it.
409 */
410 if (flags & KEXEC_FILE_ON_CRASH)
411 kimage_free(xchg(&kexec_crash_image, NULL));
412
413 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
414 cmdline_len, flags);
415 if (ret)
416 goto out;
417
418 ret = machine_kexec_prepare(image);
419 if (ret)
420 goto out;
421
422 /*
423 * Some architecture(like S390) may touch the crash memory before
424 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
425 */
426 ret = kimage_crash_copy_vmcoreinfo(image);
427 if (ret)
428 goto out;
429
430 ret = kexec_calculate_store_digests(image);
431 if (ret)
432 goto out;
433
434 for (i = 0; i < image->nr_segments; i++) {
435 struct kexec_segment *ksegment;
436
437 ksegment = &image->segment[i];
438 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
439 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
440 ksegment->memsz);
441
442 ret = kimage_load_segment(image, &image->segment[i]);
443 if (ret)
444 goto out;
445 }
446
447 kimage_terminate(image);
448
449 /*
450 * Free up any temporary buffers allocated which are not needed
451 * after image has been loaded
452 */
453 kimage_file_post_load_cleanup(image);
454exchange:
455 image = xchg(dest_image, image);
456out:
457 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
458 arch_kexec_protect_crashkres();
459
460 mutex_unlock(&kexec_mutex);
461 kimage_free(image);
462 return ret;
463}
464
465static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
466 struct kexec_buf *kbuf)
467{
468 struct kimage *image = kbuf->image;
469 unsigned long temp_start, temp_end;
470
471 temp_end = min(end, kbuf->buf_max);
472 temp_start = temp_end - kbuf->memsz;
473
474 do {
475 /* align down start */
476 temp_start = temp_start & (~(kbuf->buf_align - 1));
477
478 if (temp_start < start || temp_start < kbuf->buf_min)
479 return 0;
480
481 temp_end = temp_start + kbuf->memsz - 1;
482
483 /*
484 * Make sure this does not conflict with any of existing
485 * segments
486 */
487 if (kimage_is_destination_range(image, temp_start, temp_end)) {
488 temp_start = temp_start - PAGE_SIZE;
489 continue;
490 }
491
492 /* We found a suitable memory range */
493 break;
494 } while (1);
495
496 /* If we are here, we found a suitable memory range */
497 kbuf->mem = temp_start;
498
499 /* Success, stop navigating through remaining System RAM ranges */
500 return 1;
501}
502
503static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
504 struct kexec_buf *kbuf)
505{
506 struct kimage *image = kbuf->image;
507 unsigned long temp_start, temp_end;
508
509 temp_start = max(start, kbuf->buf_min);
510
511 do {
512 temp_start = ALIGN(temp_start, kbuf->buf_align);
513 temp_end = temp_start + kbuf->memsz - 1;
514
515 if (temp_end > end || temp_end > kbuf->buf_max)
516 return 0;
517 /*
518 * Make sure this does not conflict with any of existing
519 * segments
520 */
521 if (kimage_is_destination_range(image, temp_start, temp_end)) {
522 temp_start = temp_start + PAGE_SIZE;
523 continue;
524 }
525
526 /* We found a suitable memory range */
527 break;
528 } while (1);
529
530 /* If we are here, we found a suitable memory range */
531 kbuf->mem = temp_start;
532
533 /* Success, stop navigating through remaining System RAM ranges */
534 return 1;
535}
536
537static int locate_mem_hole_callback(struct resource *res, void *arg)
538{
539 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
540 u64 start = res->start, end = res->end;
541 unsigned long sz = end - start + 1;
542
543 /* Returning 0 will take to next memory range */
544 if (sz < kbuf->memsz)
545 return 0;
546
547 if (end < kbuf->buf_min || start > kbuf->buf_max)
548 return 0;
549
550 /*
551 * Allocate memory top down with-in ram range. Otherwise bottom up
552 * allocation.
553 */
554 if (kbuf->top_down)
555 return locate_mem_hole_top_down(start, end, kbuf);
556 return locate_mem_hole_bottom_up(start, end, kbuf);
557}
558
David Brazdil0f672f62019-12-10 10:32:29 +0000559#ifdef CONFIG_ARCH_KEEP_MEMBLOCK
560static int kexec_walk_memblock(struct kexec_buf *kbuf,
561 int (*func)(struct resource *, void *))
562{
563 int ret = 0;
564 u64 i;
565 phys_addr_t mstart, mend;
566 struct resource res = { };
567
568 if (kbuf->image->type == KEXEC_TYPE_CRASH)
569 return func(&crashk_res, kbuf);
570
571 if (kbuf->top_down) {
572 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
573 &mstart, &mend, NULL) {
574 /*
575 * In memblock, end points to the first byte after the
576 * range while in kexec, end points to the last byte
577 * in the range.
578 */
579 res.start = mstart;
580 res.end = mend - 1;
581 ret = func(&res, kbuf);
582 if (ret)
583 break;
584 }
585 } else {
586 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
587 &mstart, &mend, NULL) {
588 /*
589 * In memblock, end points to the first byte after the
590 * range while in kexec, end points to the last byte
591 * in the range.
592 */
593 res.start = mstart;
594 res.end = mend - 1;
595 ret = func(&res, kbuf);
596 if (ret)
597 break;
598 }
599 }
600
601 return ret;
602}
603#else
604static int kexec_walk_memblock(struct kexec_buf *kbuf,
605 int (*func)(struct resource *, void *))
606{
607 return 0;
608}
609#endif
610
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000611/**
David Brazdil0f672f62019-12-10 10:32:29 +0000612 * kexec_walk_resources - call func(data) on free memory regions
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000613 * @kbuf: Context info for the search. Also passed to @func.
614 * @func: Function to call for each memory region.
615 *
616 * Return: The memory walk will stop when func returns a non-zero value
617 * and that value will be returned. If all free regions are visited without
618 * func returning non-zero, then zero will be returned.
619 */
David Brazdil0f672f62019-12-10 10:32:29 +0000620static int kexec_walk_resources(struct kexec_buf *kbuf,
621 int (*func)(struct resource *, void *))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622{
623 if (kbuf->image->type == KEXEC_TYPE_CRASH)
624 return walk_iomem_res_desc(crashk_res.desc,
625 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
626 crashk_res.start, crashk_res.end,
627 kbuf, func);
628 else
629 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
630}
631
632/**
633 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
634 * @kbuf: Parameters for the memory search.
635 *
636 * On success, kbuf->mem will have the start address of the memory region found.
637 *
638 * Return: 0 on success, negative errno on error.
639 */
640int kexec_locate_mem_hole(struct kexec_buf *kbuf)
641{
642 int ret;
643
David Brazdil0f672f62019-12-10 10:32:29 +0000644 /* Arch knows where to place */
645 if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
646 return 0;
647
648 if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
649 ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
650 else
651 ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000652
653 return ret == 1 ? 0 : -EADDRNOTAVAIL;
654}
655
656/**
657 * kexec_add_buffer - place a buffer in a kexec segment
658 * @kbuf: Buffer contents and memory parameters.
659 *
660 * This function assumes that kexec_mutex is held.
661 * On successful return, @kbuf->mem will have the physical address of
662 * the buffer in memory.
663 *
664 * Return: 0 on success, negative errno on error.
665 */
666int kexec_add_buffer(struct kexec_buf *kbuf)
667{
668
669 struct kexec_segment *ksegment;
670 int ret;
671
672 /* Currently adding segment this way is allowed only in file mode */
673 if (!kbuf->image->file_mode)
674 return -EINVAL;
675
676 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
677 return -EINVAL;
678
679 /*
680 * Make sure we are not trying to add buffer after allocating
681 * control pages. All segments need to be placed first before
682 * any control pages are allocated. As control page allocation
683 * logic goes through list of segments to make sure there are
684 * no destination overlaps.
685 */
686 if (!list_empty(&kbuf->image->control_pages)) {
687 WARN_ON(1);
688 return -EINVAL;
689 }
690
691 /* Ensure minimum alignment needed for segments. */
692 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
693 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
694
695 /* Walk the RAM ranges and allocate a suitable range for the buffer */
696 ret = kexec_locate_mem_hole(kbuf);
697 if (ret)
698 return ret;
699
700 /* Found a suitable memory range */
701 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
702 ksegment->kbuf = kbuf->buffer;
703 ksegment->bufsz = kbuf->bufsz;
704 ksegment->mem = kbuf->mem;
705 ksegment->memsz = kbuf->memsz;
706 kbuf->image->nr_segments++;
707 return 0;
708}
709
710/* Calculate and store the digest of segments */
711static int kexec_calculate_store_digests(struct kimage *image)
712{
713 struct crypto_shash *tfm;
714 struct shash_desc *desc;
715 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
716 size_t desc_size, nullsz;
717 char *digest;
718 void *zero_buf;
719 struct kexec_sha_region *sha_regions;
720 struct purgatory_info *pi = &image->purgatory_info;
721
722 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
723 return 0;
724
725 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
726 zero_buf_sz = PAGE_SIZE;
727
728 tfm = crypto_alloc_shash("sha256", 0, 0);
729 if (IS_ERR(tfm)) {
730 ret = PTR_ERR(tfm);
731 goto out;
732 }
733
734 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
735 desc = kzalloc(desc_size, GFP_KERNEL);
736 if (!desc) {
737 ret = -ENOMEM;
738 goto out_free_tfm;
739 }
740
741 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
742 sha_regions = vzalloc(sha_region_sz);
Olivier Deprez0e641232021-09-23 10:07:05 +0200743 if (!sha_regions) {
744 ret = -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000745 goto out_free_desc;
Olivier Deprez0e641232021-09-23 10:07:05 +0200746 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000747
748 desc->tfm = tfm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000749
750 ret = crypto_shash_init(desc);
751 if (ret < 0)
752 goto out_free_sha_regions;
753
754 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
755 if (!digest) {
756 ret = -ENOMEM;
757 goto out_free_sha_regions;
758 }
759
760 for (j = i = 0; i < image->nr_segments; i++) {
761 struct kexec_segment *ksegment;
762
763 ksegment = &image->segment[i];
764 /*
765 * Skip purgatory as it will be modified once we put digest
766 * info in purgatory.
767 */
768 if (ksegment->kbuf == pi->purgatory_buf)
769 continue;
770
771 ret = crypto_shash_update(desc, ksegment->kbuf,
772 ksegment->bufsz);
773 if (ret)
774 break;
775
776 /*
777 * Assume rest of the buffer is filled with zero and
778 * update digest accordingly.
779 */
780 nullsz = ksegment->memsz - ksegment->bufsz;
781 while (nullsz) {
782 unsigned long bytes = nullsz;
783
784 if (bytes > zero_buf_sz)
785 bytes = zero_buf_sz;
786 ret = crypto_shash_update(desc, zero_buf, bytes);
787 if (ret)
788 break;
789 nullsz -= bytes;
790 }
791
792 if (ret)
793 break;
794
795 sha_regions[j].start = ksegment->mem;
796 sha_regions[j].len = ksegment->memsz;
797 j++;
798 }
799
800 if (!ret) {
801 ret = crypto_shash_final(desc, digest);
802 if (ret)
803 goto out_free_digest;
804 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
805 sha_regions, sha_region_sz, 0);
806 if (ret)
807 goto out_free_digest;
808
809 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
810 digest, SHA256_DIGEST_SIZE, 0);
811 if (ret)
812 goto out_free_digest;
813 }
814
815out_free_digest:
816 kfree(digest);
817out_free_sha_regions:
818 vfree(sha_regions);
819out_free_desc:
820 kfree(desc);
821out_free_tfm:
822 kfree(tfm);
823out:
824 return ret;
825}
826
827#ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
828/*
829 * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
830 * @pi: Purgatory to be loaded.
831 * @kbuf: Buffer to setup.
832 *
833 * Allocates the memory needed for the buffer. Caller is responsible to free
834 * the memory after use.
835 *
836 * Return: 0 on success, negative errno on error.
837 */
838static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
839 struct kexec_buf *kbuf)
840{
841 const Elf_Shdr *sechdrs;
842 unsigned long bss_align;
843 unsigned long bss_sz;
844 unsigned long align;
845 int i, ret;
846
847 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
848 kbuf->buf_align = bss_align = 1;
849 kbuf->bufsz = bss_sz = 0;
850
851 for (i = 0; i < pi->ehdr->e_shnum; i++) {
852 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
853 continue;
854
855 align = sechdrs[i].sh_addralign;
856 if (sechdrs[i].sh_type != SHT_NOBITS) {
857 if (kbuf->buf_align < align)
858 kbuf->buf_align = align;
859 kbuf->bufsz = ALIGN(kbuf->bufsz, align);
860 kbuf->bufsz += sechdrs[i].sh_size;
861 } else {
862 if (bss_align < align)
863 bss_align = align;
864 bss_sz = ALIGN(bss_sz, align);
865 bss_sz += sechdrs[i].sh_size;
866 }
867 }
868 kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
869 kbuf->memsz = kbuf->bufsz + bss_sz;
870 if (kbuf->buf_align < bss_align)
871 kbuf->buf_align = bss_align;
872
873 kbuf->buffer = vzalloc(kbuf->bufsz);
874 if (!kbuf->buffer)
875 return -ENOMEM;
876 pi->purgatory_buf = kbuf->buffer;
877
878 ret = kexec_add_buffer(kbuf);
879 if (ret)
880 goto out;
881
882 return 0;
883out:
884 vfree(pi->purgatory_buf);
885 pi->purgatory_buf = NULL;
886 return ret;
887}
888
889/*
890 * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
891 * @pi: Purgatory to be loaded.
892 * @kbuf: Buffer prepared to store purgatory.
893 *
894 * Allocates the memory needed for the buffer. Caller is responsible to free
895 * the memory after use.
896 *
897 * Return: 0 on success, negative errno on error.
898 */
899static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
900 struct kexec_buf *kbuf)
901{
902 unsigned long bss_addr;
903 unsigned long offset;
904 Elf_Shdr *sechdrs;
905 int i;
906
907 /*
908 * The section headers in kexec_purgatory are read-only. In order to
909 * have them modifiable make a temporary copy.
910 */
911 sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum));
912 if (!sechdrs)
913 return -ENOMEM;
914 memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff,
915 pi->ehdr->e_shnum * sizeof(Elf_Shdr));
916 pi->sechdrs = sechdrs;
917
918 offset = 0;
919 bss_addr = kbuf->mem + kbuf->bufsz;
920 kbuf->image->start = pi->ehdr->e_entry;
921
922 for (i = 0; i < pi->ehdr->e_shnum; i++) {
923 unsigned long align;
924 void *src, *dst;
925
926 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
927 continue;
928
929 align = sechdrs[i].sh_addralign;
930 if (sechdrs[i].sh_type == SHT_NOBITS) {
931 bss_addr = ALIGN(bss_addr, align);
932 sechdrs[i].sh_addr = bss_addr;
933 bss_addr += sechdrs[i].sh_size;
934 continue;
935 }
936
937 offset = ALIGN(offset, align);
938 if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
939 pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
940 pi->ehdr->e_entry < (sechdrs[i].sh_addr
941 + sechdrs[i].sh_size)) {
942 kbuf->image->start -= sechdrs[i].sh_addr;
943 kbuf->image->start += kbuf->mem + offset;
944 }
945
946 src = (void *)pi->ehdr + sechdrs[i].sh_offset;
947 dst = pi->purgatory_buf + offset;
948 memcpy(dst, src, sechdrs[i].sh_size);
949
950 sechdrs[i].sh_addr = kbuf->mem + offset;
951 sechdrs[i].sh_offset = offset;
952 offset += sechdrs[i].sh_size;
953 }
954
955 return 0;
956}
957
958static int kexec_apply_relocations(struct kimage *image)
959{
960 int i, ret;
961 struct purgatory_info *pi = &image->purgatory_info;
962 const Elf_Shdr *sechdrs;
963
964 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
965
966 for (i = 0; i < pi->ehdr->e_shnum; i++) {
967 const Elf_Shdr *relsec;
968 const Elf_Shdr *symtab;
969 Elf_Shdr *section;
970
971 relsec = sechdrs + i;
972
973 if (relsec->sh_type != SHT_RELA &&
974 relsec->sh_type != SHT_REL)
975 continue;
976
977 /*
978 * For section of type SHT_RELA/SHT_REL,
979 * ->sh_link contains section header index of associated
980 * symbol table. And ->sh_info contains section header
981 * index of section to which relocations apply.
982 */
983 if (relsec->sh_info >= pi->ehdr->e_shnum ||
984 relsec->sh_link >= pi->ehdr->e_shnum)
985 return -ENOEXEC;
986
987 section = pi->sechdrs + relsec->sh_info;
988 symtab = sechdrs + relsec->sh_link;
989
990 if (!(section->sh_flags & SHF_ALLOC))
991 continue;
992
993 /*
994 * symtab->sh_link contain section header index of associated
995 * string table.
996 */
997 if (symtab->sh_link >= pi->ehdr->e_shnum)
998 /* Invalid section number? */
999 continue;
1000
1001 /*
1002 * Respective architecture needs to provide support for applying
1003 * relocations of type SHT_RELA/SHT_REL.
1004 */
1005 if (relsec->sh_type == SHT_RELA)
1006 ret = arch_kexec_apply_relocations_add(pi, section,
1007 relsec, symtab);
1008 else if (relsec->sh_type == SHT_REL)
1009 ret = arch_kexec_apply_relocations(pi, section,
1010 relsec, symtab);
1011 if (ret)
1012 return ret;
1013 }
1014
1015 return 0;
1016}
1017
1018/*
1019 * kexec_load_purgatory - Load and relocate the purgatory object.
1020 * @image: Image to add the purgatory to.
1021 * @kbuf: Memory parameters to use.
1022 *
1023 * Allocates the memory needed for image->purgatory_info.sechdrs and
1024 * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
1025 * to free the memory after use.
1026 *
1027 * Return: 0 on success, negative errno on error.
1028 */
1029int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
1030{
1031 struct purgatory_info *pi = &image->purgatory_info;
1032 int ret;
1033
1034 if (kexec_purgatory_size <= 0)
1035 return -EINVAL;
1036
1037 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
1038
1039 ret = kexec_purgatory_setup_kbuf(pi, kbuf);
1040 if (ret)
1041 return ret;
1042
1043 ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
1044 if (ret)
1045 goto out_free_kbuf;
1046
1047 ret = kexec_apply_relocations(image);
1048 if (ret)
1049 goto out;
1050
1051 return 0;
1052out:
1053 vfree(pi->sechdrs);
1054 pi->sechdrs = NULL;
1055out_free_kbuf:
1056 vfree(pi->purgatory_buf);
1057 pi->purgatory_buf = NULL;
1058 return ret;
1059}
1060
1061/*
1062 * kexec_purgatory_find_symbol - find a symbol in the purgatory
1063 * @pi: Purgatory to search in.
1064 * @name: Name of the symbol.
1065 *
1066 * Return: pointer to symbol in read-only symtab on success, NULL on error.
1067 */
1068static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
1069 const char *name)
1070{
1071 const Elf_Shdr *sechdrs;
1072 const Elf_Ehdr *ehdr;
1073 const Elf_Sym *syms;
1074 const char *strtab;
1075 int i, k;
1076
1077 if (!pi->ehdr)
1078 return NULL;
1079
1080 ehdr = pi->ehdr;
1081 sechdrs = (void *)ehdr + ehdr->e_shoff;
1082
1083 for (i = 0; i < ehdr->e_shnum; i++) {
1084 if (sechdrs[i].sh_type != SHT_SYMTAB)
1085 continue;
1086
1087 if (sechdrs[i].sh_link >= ehdr->e_shnum)
1088 /* Invalid strtab section number */
1089 continue;
1090 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
1091 syms = (void *)ehdr + sechdrs[i].sh_offset;
1092
1093 /* Go through symbols for a match */
1094 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1095 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1096 continue;
1097
1098 if (strcmp(strtab + syms[k].st_name, name) != 0)
1099 continue;
1100
1101 if (syms[k].st_shndx == SHN_UNDEF ||
1102 syms[k].st_shndx >= ehdr->e_shnum) {
1103 pr_debug("Symbol: %s has bad section index %d.\n",
1104 name, syms[k].st_shndx);
1105 return NULL;
1106 }
1107
1108 /* Found the symbol we are looking for */
1109 return &syms[k];
1110 }
1111 }
1112
1113 return NULL;
1114}
1115
1116void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1117{
1118 struct purgatory_info *pi = &image->purgatory_info;
1119 const Elf_Sym *sym;
1120 Elf_Shdr *sechdr;
1121
1122 sym = kexec_purgatory_find_symbol(pi, name);
1123 if (!sym)
1124 return ERR_PTR(-EINVAL);
1125
1126 sechdr = &pi->sechdrs[sym->st_shndx];
1127
1128 /*
1129 * Returns the address where symbol will finally be loaded after
1130 * kexec_load_segment()
1131 */
1132 return (void *)(sechdr->sh_addr + sym->st_value);
1133}
1134
1135/*
1136 * Get or set value of a symbol. If "get_value" is true, symbol value is
1137 * returned in buf otherwise symbol value is set based on value in buf.
1138 */
1139int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1140 void *buf, unsigned int size, bool get_value)
1141{
1142 struct purgatory_info *pi = &image->purgatory_info;
1143 const Elf_Sym *sym;
1144 Elf_Shdr *sec;
1145 char *sym_buf;
1146
1147 sym = kexec_purgatory_find_symbol(pi, name);
1148 if (!sym)
1149 return -EINVAL;
1150
1151 if (sym->st_size != size) {
1152 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1153 name, (unsigned long)sym->st_size, size);
1154 return -EINVAL;
1155 }
1156
1157 sec = pi->sechdrs + sym->st_shndx;
1158
1159 if (sec->sh_type == SHT_NOBITS) {
1160 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1161 get_value ? "get" : "set");
1162 return -EINVAL;
1163 }
1164
1165 sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
1166
1167 if (get_value)
1168 memcpy((void *)buf, sym_buf, size);
1169 else
1170 memcpy((void *)sym_buf, buf, size);
1171
1172 return 0;
1173}
1174#endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
1175
1176int crash_exclude_mem_range(struct crash_mem *mem,
1177 unsigned long long mstart, unsigned long long mend)
1178{
1179 int i, j;
1180 unsigned long long start, end;
1181 struct crash_mem_range temp_range = {0, 0};
1182
1183 for (i = 0; i < mem->nr_ranges; i++) {
1184 start = mem->ranges[i].start;
1185 end = mem->ranges[i].end;
1186
1187 if (mstart > end || mend < start)
1188 continue;
1189
1190 /* Truncate any area outside of range */
1191 if (mstart < start)
1192 mstart = start;
1193 if (mend > end)
1194 mend = end;
1195
1196 /* Found completely overlapping range */
1197 if (mstart == start && mend == end) {
1198 mem->ranges[i].start = 0;
1199 mem->ranges[i].end = 0;
1200 if (i < mem->nr_ranges - 1) {
1201 /* Shift rest of the ranges to left */
1202 for (j = i; j < mem->nr_ranges - 1; j++) {
1203 mem->ranges[j].start =
1204 mem->ranges[j+1].start;
1205 mem->ranges[j].end =
1206 mem->ranges[j+1].end;
1207 }
1208 }
1209 mem->nr_ranges--;
1210 return 0;
1211 }
1212
1213 if (mstart > start && mend < end) {
1214 /* Split original range */
1215 mem->ranges[i].end = mstart - 1;
1216 temp_range.start = mend + 1;
1217 temp_range.end = end;
1218 } else if (mstart != start)
1219 mem->ranges[i].end = mstart - 1;
1220 else
1221 mem->ranges[i].start = mend + 1;
1222 break;
1223 }
1224
1225 /* If a split happened, add the split to array */
1226 if (!temp_range.end)
1227 return 0;
1228
1229 /* Split happened */
1230 if (i == mem->max_nr_ranges - 1)
1231 return -ENOMEM;
1232
1233 /* Location where new range should go */
1234 j = i + 1;
1235 if (j < mem->nr_ranges) {
1236 /* Move over all ranges one slot towards the end */
1237 for (i = mem->nr_ranges - 1; i >= j; i--)
1238 mem->ranges[i + 1] = mem->ranges[i];
1239 }
1240
1241 mem->ranges[j].start = temp_range.start;
1242 mem->ranges[j].end = temp_range.end;
1243 mem->nr_ranges++;
1244 return 0;
1245}
1246
1247int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
1248 void **addr, unsigned long *sz)
1249{
1250 Elf64_Ehdr *ehdr;
1251 Elf64_Phdr *phdr;
1252 unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
1253 unsigned char *buf;
1254 unsigned int cpu, i;
1255 unsigned long long notes_addr;
1256 unsigned long mstart, mend;
1257
1258 /* extra phdr for vmcoreinfo elf note */
1259 nr_phdr = nr_cpus + 1;
1260 nr_phdr += mem->nr_ranges;
1261
1262 /*
1263 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
1264 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
1265 * I think this is required by tools like gdb. So same physical
1266 * memory will be mapped in two elf headers. One will contain kernel
1267 * text virtual addresses and other will have __va(physical) addresses.
1268 */
1269
1270 nr_phdr++;
1271 elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
1272 elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
1273
1274 buf = vzalloc(elf_sz);
1275 if (!buf)
1276 return -ENOMEM;
1277
1278 ehdr = (Elf64_Ehdr *)buf;
1279 phdr = (Elf64_Phdr *)(ehdr + 1);
1280 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1281 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
1282 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1283 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1284 ehdr->e_ident[EI_OSABI] = ELF_OSABI;
1285 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
1286 ehdr->e_type = ET_CORE;
1287 ehdr->e_machine = ELF_ARCH;
1288 ehdr->e_version = EV_CURRENT;
1289 ehdr->e_phoff = sizeof(Elf64_Ehdr);
1290 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
1291 ehdr->e_phentsize = sizeof(Elf64_Phdr);
1292
1293 /* Prepare one phdr of type PT_NOTE for each present cpu */
1294 for_each_present_cpu(cpu) {
1295 phdr->p_type = PT_NOTE;
1296 notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
1297 phdr->p_offset = phdr->p_paddr = notes_addr;
1298 phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
1299 (ehdr->e_phnum)++;
1300 phdr++;
1301 }
1302
1303 /* Prepare one PT_NOTE header for vmcoreinfo */
1304 phdr->p_type = PT_NOTE;
1305 phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
1306 phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
1307 (ehdr->e_phnum)++;
1308 phdr++;
1309
1310 /* Prepare PT_LOAD type program header for kernel text region */
1311 if (kernel_map) {
1312 phdr->p_type = PT_LOAD;
1313 phdr->p_flags = PF_R|PF_W|PF_X;
1314 phdr->p_vaddr = (Elf64_Addr)_text;
1315 phdr->p_filesz = phdr->p_memsz = _end - _text;
1316 phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
1317 ehdr->e_phnum++;
1318 phdr++;
1319 }
1320
1321 /* Go through all the ranges in mem->ranges[] and prepare phdr */
1322 for (i = 0; i < mem->nr_ranges; i++) {
1323 mstart = mem->ranges[i].start;
1324 mend = mem->ranges[i].end;
1325
1326 phdr->p_type = PT_LOAD;
1327 phdr->p_flags = PF_R|PF_W|PF_X;
1328 phdr->p_offset = mstart;
1329
1330 phdr->p_paddr = mstart;
1331 phdr->p_vaddr = (unsigned long long) __va(mstart);
1332 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
1333 phdr->p_align = 0;
1334 ehdr->e_phnum++;
1335 phdr++;
1336 pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
1337 phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
1338 ehdr->e_phnum, phdr->p_offset);
1339 }
1340
1341 *addr = buf;
1342 *sz = elf_sz;
1343 return 0;
1344}