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