1 // SPDX-License-Identifier: GPL-2.0-only
3 * kexec: kexec_file_load system call
5 * Copyright (C) 2014 Red Hat Inc.
7 * Vivek Goyal <vgoyal@redhat.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/capability.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>
21 #include <linux/ima.h>
22 #include <crypto/hash.h>
23 #include <crypto/sha2.h>
24 #include <linux/elf.h>
25 #include <linux/elfcore.h>
26 #include <linux/kernel.h>
27 #include <linux/kernel_read_file.h>
28 #include <linux/syscalls.h>
29 #include <linux/vmalloc.h>
30 #include "kexec_internal.h"
32 #ifdef CONFIG_KEXEC_SIG
33 static bool sig_enforce = IS_ENABLED(CONFIG_KEXEC_SIG_FORCE);
35 void set_kexec_sig_enforced(void)
41 static int kexec_calculate_store_digests(struct kimage *image);
43 /* Maximum size in bytes for kernel/initrd files. */
44 #define KEXEC_FILE_SIZE_MAX min_t(s64, 4LL << 30, SSIZE_MAX)
47 * Currently this is the only default function that is exported as some
48 * architectures need it to do additional handlings.
49 * In the future, other default functions may be exported too if required.
51 int kexec_image_probe_default(struct kimage *image, void *buf,
52 unsigned long buf_len)
54 const struct kexec_file_ops * const *fops;
57 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
58 ret = (*fops)->probe(buf, buf_len);
68 static void *kexec_image_load_default(struct kimage *image)
70 if (!image->fops || !image->fops->load)
71 return ERR_PTR(-ENOEXEC);
73 return image->fops->load(image, image->kernel_buf,
74 image->kernel_buf_len, image->initrd_buf,
75 image->initrd_buf_len, image->cmdline_buf,
76 image->cmdline_buf_len);
79 int kexec_image_post_load_cleanup_default(struct kimage *image)
81 if (!image->fops || !image->fops->cleanup)
84 return image->fops->cleanup(image->image_loader_data);
88 * Free up memory used by kernel, initrd, and command line. This is temporary
89 * memory allocation which is not needed any more after these buffers have
90 * been loaded into separate segments and have been copied elsewhere.
92 void kimage_file_post_load_cleanup(struct kimage *image)
94 struct purgatory_info *pi = &image->purgatory_info;
96 vfree(image->kernel_buf);
97 image->kernel_buf = NULL;
99 vfree(image->initrd_buf);
100 image->initrd_buf = NULL;
102 kfree(image->cmdline_buf);
103 image->cmdline_buf = NULL;
105 vfree(pi->purgatory_buf);
106 pi->purgatory_buf = NULL;
111 #ifdef CONFIG_IMA_KEXEC
112 vfree(image->ima_buffer);
113 image->ima_buffer = NULL;
114 #endif /* CONFIG_IMA_KEXEC */
116 /* See if architecture has anything to cleanup post load */
117 arch_kimage_file_post_load_cleanup(image);
120 * Above call should have called into bootloader to free up
121 * any data stored in kimage->image_loader_data. It should
122 * be ok now to free it up.
124 kfree(image->image_loader_data);
125 image->image_loader_data = NULL;
127 kexec_file_dbg_print = false;
130 #ifdef CONFIG_KEXEC_SIG
131 #ifdef CONFIG_SIGNED_PE_FILE_VERIFICATION
132 int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
136 ret = verify_pefile_signature(kernel, kernel_len,
137 VERIFY_USE_SECONDARY_KEYRING,
138 VERIFYING_KEXEC_PE_SIGNATURE);
139 if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
140 ret = verify_pefile_signature(kernel, kernel_len,
141 VERIFY_USE_PLATFORM_KEYRING,
142 VERIFYING_KEXEC_PE_SIGNATURE);
148 static int kexec_image_verify_sig(struct kimage *image, void *buf,
149 unsigned long buf_len)
151 if (!image->fops || !image->fops->verify_sig) {
152 pr_debug("kernel loader does not support signature verification.\n");
153 return -EKEYREJECTED;
156 return image->fops->verify_sig(buf, buf_len);
160 kimage_validate_signature(struct kimage *image)
164 ret = kexec_image_verify_sig(image, image->kernel_buf,
165 image->kernel_buf_len);
169 pr_notice("Enforced kernel signature verification failed (%d).\n", ret);
174 * If IMA is guaranteed to appraise a signature on the kexec
175 * image, permit it even if the kernel is otherwise locked
178 if (!ima_appraise_signature(READING_KEXEC_IMAGE) &&
179 security_locked_down(LOCKDOWN_KEXEC))
182 pr_debug("kernel signature verification failed (%d).\n", ret);
190 * In file mode list of segments is prepared by kernel. Copy relevant
191 * data from user space, do error checking, prepare segment list
194 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
195 const char __user *cmdline_ptr,
196 unsigned long cmdline_len, unsigned flags)
201 ret = kernel_read_file_from_fd(kernel_fd, 0, &image->kernel_buf,
202 KEXEC_FILE_SIZE_MAX, NULL,
203 READING_KEXEC_IMAGE);
206 image->kernel_buf_len = ret;
208 /* Call arch image probe handlers */
209 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
210 image->kernel_buf_len);
214 #ifdef CONFIG_KEXEC_SIG
215 ret = kimage_validate_signature(image);
220 /* It is possible that there no initramfs is being loaded */
221 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
222 ret = kernel_read_file_from_fd(initrd_fd, 0, &image->initrd_buf,
223 KEXEC_FILE_SIZE_MAX, NULL,
224 READING_KEXEC_INITRAMFS);
227 image->initrd_buf_len = ret;
232 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len);
233 if (IS_ERR(image->cmdline_buf)) {
234 ret = PTR_ERR(image->cmdline_buf);
235 image->cmdline_buf = NULL;
239 image->cmdline_buf_len = cmdline_len;
241 /* command line should be a string with last byte null */
242 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
247 ima_kexec_cmdline(kernel_fd, image->cmdline_buf,
248 image->cmdline_buf_len - 1);
251 /* IMA needs to pass the measurement list to the next kernel. */
252 ima_add_kexec_buffer(image);
254 /* Call image load handler */
255 ldata = kexec_image_load_default(image);
258 ret = PTR_ERR(ldata);
262 image->image_loader_data = ldata;
264 /* In case of error, free up all allocated memory in this function */
266 kimage_file_post_load_cleanup(image);
271 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
272 int initrd_fd, const char __user *cmdline_ptr,
273 unsigned long cmdline_len, unsigned long flags)
276 struct kimage *image;
277 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
279 image = do_kimage_alloc_init();
283 kexec_file_dbg_print = !!(flags & KEXEC_FILE_DEBUG);
284 image->file_mode = 1;
286 if (kexec_on_panic) {
287 /* Enable special crash kernel control page alloc policy. */
288 image->control_page = crashk_res.start;
289 image->type = KEXEC_TYPE_CRASH;
292 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
293 cmdline_ptr, cmdline_len, flags);
297 ret = sanity_check_segment_list(image);
299 goto out_free_post_load_bufs;
302 image->control_code_page = kimage_alloc_control_pages(image,
303 get_order(KEXEC_CONTROL_PAGE_SIZE));
304 if (!image->control_code_page) {
305 pr_err("Could not allocate control_code_buffer\n");
306 goto out_free_post_load_bufs;
309 if (!kexec_on_panic) {
310 image->swap_page = kimage_alloc_control_pages(image, 0);
311 if (!image->swap_page) {
312 pr_err("Could not allocate swap buffer\n");
313 goto out_free_control_pages;
319 out_free_control_pages:
320 kimage_free_page_list(&image->control_pages);
321 out_free_post_load_bufs:
322 kimage_file_post_load_cleanup(image);
328 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
329 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
330 unsigned long, flags)
332 int image_type = (flags & KEXEC_FILE_ON_CRASH) ?
333 KEXEC_TYPE_CRASH : KEXEC_TYPE_DEFAULT;
334 struct kimage **dest_image, *image;
337 /* We only trust the superuser with rebooting the system. */
338 if (!kexec_load_permitted(image_type))
341 /* Make sure we have a legal set of flags */
342 if (flags != (flags & KEXEC_FILE_FLAGS))
347 if (!kexec_trylock())
350 if (image_type == KEXEC_TYPE_CRASH) {
351 dest_image = &kexec_crash_image;
352 if (kexec_crash_image)
353 arch_kexec_unprotect_crashkres();
355 dest_image = &kexec_image;
358 if (flags & KEXEC_FILE_UNLOAD)
362 * In case of crash, new kernel gets loaded in reserved region. It is
363 * same memory where old crash kernel might be loaded. Free any
364 * current crash dump kernel before we corrupt it.
366 if (flags & KEXEC_FILE_ON_CRASH)
367 kimage_free(xchg(&kexec_crash_image, NULL));
369 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
374 ret = machine_kexec_prepare(image);
379 * Some architecture(like S390) may touch the crash memory before
380 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
382 ret = kimage_crash_copy_vmcoreinfo(image);
386 ret = kexec_calculate_store_digests(image);
390 for (i = 0; i < image->nr_segments; i++) {
391 struct kexec_segment *ksegment;
393 ksegment = &image->segment[i];
394 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
395 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
398 ret = kimage_load_segment(image, &image->segment[i]);
403 kimage_terminate(image);
405 ret = machine_kexec_post_load(image);
410 * Free up any temporary buffers allocated which are not needed
411 * after image has been loaded
413 kimage_file_post_load_cleanup(image);
415 image = xchg(dest_image, image);
417 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
418 arch_kexec_protect_crashkres();
425 static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
426 struct kexec_buf *kbuf)
428 struct kimage *image = kbuf->image;
429 unsigned long temp_start, temp_end;
431 temp_end = min(end, kbuf->buf_max);
432 temp_start = temp_end - kbuf->memsz;
435 /* align down start */
436 temp_start = temp_start & (~(kbuf->buf_align - 1));
438 if (temp_start < start || temp_start < kbuf->buf_min)
441 temp_end = temp_start + kbuf->memsz - 1;
444 * Make sure this does not conflict with any of existing
447 if (kimage_is_destination_range(image, temp_start, temp_end)) {
448 temp_start = temp_start - PAGE_SIZE;
452 /* We found a suitable memory range */
456 /* If we are here, we found a suitable memory range */
457 kbuf->mem = temp_start;
459 /* Success, stop navigating through remaining System RAM ranges */
463 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
464 struct kexec_buf *kbuf)
466 struct kimage *image = kbuf->image;
467 unsigned long temp_start, temp_end;
469 temp_start = max(start, kbuf->buf_min);
472 temp_start = ALIGN(temp_start, kbuf->buf_align);
473 temp_end = temp_start + kbuf->memsz - 1;
475 if (temp_end > end || temp_end > kbuf->buf_max)
478 * Make sure this does not conflict with any of existing
481 if (kimage_is_destination_range(image, temp_start, temp_end)) {
482 temp_start = temp_start + PAGE_SIZE;
486 /* We found a suitable memory range */
490 /* If we are here, we found a suitable memory range */
491 kbuf->mem = temp_start;
493 /* Success, stop navigating through remaining System RAM ranges */
497 static int locate_mem_hole_callback(struct resource *res, void *arg)
499 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
500 u64 start = res->start, end = res->end;
501 unsigned long sz = end - start + 1;
503 /* Returning 0 will take to next memory range */
505 /* Don't use memory that will be detected and handled by a driver. */
506 if (res->flags & IORESOURCE_SYSRAM_DRIVER_MANAGED)
509 if (sz < kbuf->memsz)
512 if (end < kbuf->buf_min || start > kbuf->buf_max)
516 * Allocate memory top down with-in ram range. Otherwise bottom up
520 return locate_mem_hole_top_down(start, end, kbuf);
521 return locate_mem_hole_bottom_up(start, end, kbuf);
524 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
525 static int kexec_walk_memblock(struct kexec_buf *kbuf,
526 int (*func)(struct resource *, void *))
530 phys_addr_t mstart, mend;
531 struct resource res = { };
533 if (kbuf->image->type == KEXEC_TYPE_CRASH)
534 return func(&crashk_res, kbuf);
537 * Using MEMBLOCK_NONE will properly skip MEMBLOCK_DRIVER_MANAGED. See
538 * IORESOURCE_SYSRAM_DRIVER_MANAGED handling in
539 * locate_mem_hole_callback().
541 if (kbuf->top_down) {
542 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE,
543 &mstart, &mend, NULL) {
545 * In memblock, end points to the first byte after the
546 * range while in kexec, end points to the last byte
551 ret = func(&res, kbuf);
556 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
557 &mstart, &mend, NULL) {
559 * In memblock, end points to the first byte after the
560 * range while in kexec, end points to the last byte
565 ret = func(&res, kbuf);
574 static int kexec_walk_memblock(struct kexec_buf *kbuf,
575 int (*func)(struct resource *, void *))
582 * kexec_walk_resources - call func(data) on free memory regions
583 * @kbuf: Context info for the search. Also passed to @func.
584 * @func: Function to call for each memory region.
586 * Return: The memory walk will stop when func returns a non-zero value
587 * and that value will be returned. If all free regions are visited without
588 * func returning non-zero, then zero will be returned.
590 static int kexec_walk_resources(struct kexec_buf *kbuf,
591 int (*func)(struct resource *, void *))
593 if (kbuf->image->type == KEXEC_TYPE_CRASH)
594 return walk_iomem_res_desc(crashk_res.desc,
595 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
596 crashk_res.start, crashk_res.end,
598 else if (kbuf->top_down)
599 return walk_system_ram_res_rev(0, ULONG_MAX, kbuf, func);
601 return walk_system_ram_res(0, ULONG_MAX, kbuf, func);
605 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel
606 * @kbuf: Parameters for the memory search.
608 * On success, kbuf->mem will have the start address of the memory region found.
610 * Return: 0 on success, negative errno on error.
612 int kexec_locate_mem_hole(struct kexec_buf *kbuf)
616 /* Arch knows where to place */
617 if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN)
620 if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK))
621 ret = kexec_walk_resources(kbuf, locate_mem_hole_callback);
623 ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback);
625 return ret == 1 ? 0 : -EADDRNOTAVAIL;
629 * kexec_add_buffer - place a buffer in a kexec segment
630 * @kbuf: Buffer contents and memory parameters.
632 * This function assumes that kexec_lock is held.
633 * On successful return, @kbuf->mem will have the physical address of
634 * the buffer in memory.
636 * Return: 0 on success, negative errno on error.
638 int kexec_add_buffer(struct kexec_buf *kbuf)
640 struct kexec_segment *ksegment;
643 /* Currently adding segment this way is allowed only in file mode */
644 if (!kbuf->image->file_mode)
647 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX)
651 * Make sure we are not trying to add buffer after allocating
652 * control pages. All segments need to be placed first before
653 * any control pages are allocated. As control page allocation
654 * logic goes through list of segments to make sure there are
655 * no destination overlaps.
657 if (!list_empty(&kbuf->image->control_pages)) {
662 /* Ensure minimum alignment needed for segments. */
663 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE);
664 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE);
666 /* Walk the RAM ranges and allocate a suitable range for the buffer */
667 ret = arch_kexec_locate_mem_hole(kbuf);
671 /* Found a suitable memory range */
672 ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
673 ksegment->kbuf = kbuf->buffer;
674 ksegment->bufsz = kbuf->bufsz;
675 ksegment->mem = kbuf->mem;
676 ksegment->memsz = kbuf->memsz;
677 kbuf->image->nr_segments++;
681 /* Calculate and store the digest of segments */
682 static int kexec_calculate_store_digests(struct kimage *image)
684 struct crypto_shash *tfm;
685 struct shash_desc *desc;
686 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
687 size_t desc_size, nullsz;
690 struct kexec_sha_region *sha_regions;
691 struct purgatory_info *pi = &image->purgatory_info;
693 if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY))
696 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
697 zero_buf_sz = PAGE_SIZE;
699 tfm = crypto_alloc_shash("sha256", 0, 0);
705 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
706 desc = kzalloc(desc_size, GFP_KERNEL);
712 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
713 sha_regions = vzalloc(sha_region_sz);
721 ret = crypto_shash_init(desc);
723 goto out_free_sha_regions;
725 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
728 goto out_free_sha_regions;
731 for (j = i = 0; i < image->nr_segments; i++) {
732 struct kexec_segment *ksegment;
734 #ifdef CONFIG_CRASH_HOTPLUG
735 /* Exclude elfcorehdr segment to allow future changes via hotplug */
736 if (j == image->elfcorehdr_index)
740 ksegment = &image->segment[i];
742 * Skip purgatory as it will be modified once we put digest
745 if (ksegment->kbuf == pi->purgatory_buf)
748 ret = crypto_shash_update(desc, ksegment->kbuf,
754 * Assume rest of the buffer is filled with zero and
755 * update digest accordingly.
757 nullsz = ksegment->memsz - ksegment->bufsz;
759 unsigned long bytes = nullsz;
761 if (bytes > zero_buf_sz)
763 ret = crypto_shash_update(desc, zero_buf, bytes);
772 sha_regions[j].start = ksegment->mem;
773 sha_regions[j].len = ksegment->memsz;
778 ret = crypto_shash_final(desc, digest);
780 goto out_free_digest;
781 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
782 sha_regions, sha_region_sz, 0);
784 goto out_free_digest;
786 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
787 digest, SHA256_DIGEST_SIZE, 0);
789 goto out_free_digest;
794 out_free_sha_regions:
804 #ifdef CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY
806 * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory.
807 * @pi: Purgatory to be loaded.
808 * @kbuf: Buffer to setup.
810 * Allocates the memory needed for the buffer. Caller is responsible to free
811 * the memory after use.
813 * Return: 0 on success, negative errno on error.
815 static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi,
816 struct kexec_buf *kbuf)
818 const Elf_Shdr *sechdrs;
819 unsigned long bss_align;
820 unsigned long bss_sz;
824 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
825 kbuf->buf_align = bss_align = 1;
826 kbuf->bufsz = bss_sz = 0;
828 for (i = 0; i < pi->ehdr->e_shnum; i++) {
829 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
832 align = sechdrs[i].sh_addralign;
833 if (sechdrs[i].sh_type != SHT_NOBITS) {
834 if (kbuf->buf_align < align)
835 kbuf->buf_align = align;
836 kbuf->bufsz = ALIGN(kbuf->bufsz, align);
837 kbuf->bufsz += sechdrs[i].sh_size;
839 if (bss_align < align)
841 bss_sz = ALIGN(bss_sz, align);
842 bss_sz += sechdrs[i].sh_size;
845 kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align);
846 kbuf->memsz = kbuf->bufsz + bss_sz;
847 if (kbuf->buf_align < bss_align)
848 kbuf->buf_align = bss_align;
850 kbuf->buffer = vzalloc(kbuf->bufsz);
853 pi->purgatory_buf = kbuf->buffer;
855 ret = kexec_add_buffer(kbuf);
861 vfree(pi->purgatory_buf);
862 pi->purgatory_buf = NULL;
867 * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer.
868 * @pi: Purgatory to be loaded.
869 * @kbuf: Buffer prepared to store purgatory.
871 * Allocates the memory needed for the buffer. Caller is responsible to free
872 * the memory after use.
874 * Return: 0 on success, negative errno on error.
876 static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi,
877 struct kexec_buf *kbuf)
879 unsigned long bss_addr;
880 unsigned long offset;
886 * The section headers in kexec_purgatory are read-only. In order to
887 * have them modifiable make a temporary copy.
889 sechdrs_size = array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum);
890 sechdrs = vzalloc(sechdrs_size);
893 memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff, sechdrs_size);
894 pi->sechdrs = sechdrs;
897 bss_addr = kbuf->mem + kbuf->bufsz;
898 kbuf->image->start = pi->ehdr->e_entry;
900 for (i = 0; i < pi->ehdr->e_shnum; i++) {
904 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
907 align = sechdrs[i].sh_addralign;
908 if (sechdrs[i].sh_type == SHT_NOBITS) {
909 bss_addr = ALIGN(bss_addr, align);
910 sechdrs[i].sh_addr = bss_addr;
911 bss_addr += sechdrs[i].sh_size;
915 offset = ALIGN(offset, align);
918 * Check if the segment contains the entry point, if so,
919 * calculate the value of image->start based on it.
920 * If the compiler has produced more than one .text section
921 * (Eg: .text.hot), they are generally after the main .text
922 * section, and they shall not be used to calculate
923 * image->start. So do not re-calculate image->start if it
924 * is not set to the initial value, and warn the user so they
925 * have a chance to fix their purgatory's linker script.
927 if (sechdrs[i].sh_flags & SHF_EXECINSTR &&
928 pi->ehdr->e_entry >= sechdrs[i].sh_addr &&
929 pi->ehdr->e_entry < (sechdrs[i].sh_addr
930 + sechdrs[i].sh_size) &&
931 !WARN_ON(kbuf->image->start != pi->ehdr->e_entry)) {
932 kbuf->image->start -= sechdrs[i].sh_addr;
933 kbuf->image->start += kbuf->mem + offset;
936 src = (void *)pi->ehdr + sechdrs[i].sh_offset;
937 dst = pi->purgatory_buf + offset;
938 memcpy(dst, src, sechdrs[i].sh_size);
940 sechdrs[i].sh_addr = kbuf->mem + offset;
941 sechdrs[i].sh_offset = offset;
942 offset += sechdrs[i].sh_size;
948 static int kexec_apply_relocations(struct kimage *image)
951 struct purgatory_info *pi = &image->purgatory_info;
952 const Elf_Shdr *sechdrs;
954 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
956 for (i = 0; i < pi->ehdr->e_shnum; i++) {
957 const Elf_Shdr *relsec;
958 const Elf_Shdr *symtab;
961 relsec = sechdrs + i;
963 if (relsec->sh_type != SHT_RELA &&
964 relsec->sh_type != SHT_REL)
968 * For section of type SHT_RELA/SHT_REL,
969 * ->sh_link contains section header index of associated
970 * symbol table. And ->sh_info contains section header
971 * index of section to which relocations apply.
973 if (relsec->sh_info >= pi->ehdr->e_shnum ||
974 relsec->sh_link >= pi->ehdr->e_shnum)
977 section = pi->sechdrs + relsec->sh_info;
978 symtab = sechdrs + relsec->sh_link;
980 if (!(section->sh_flags & SHF_ALLOC))
984 * symtab->sh_link contain section header index of associated
987 if (symtab->sh_link >= pi->ehdr->e_shnum)
988 /* Invalid section number? */
992 * Respective architecture needs to provide support for applying
993 * relocations of type SHT_RELA/SHT_REL.
995 if (relsec->sh_type == SHT_RELA)
996 ret = arch_kexec_apply_relocations_add(pi, section,
998 else if (relsec->sh_type == SHT_REL)
999 ret = arch_kexec_apply_relocations(pi, section,
1009 * kexec_load_purgatory - Load and relocate the purgatory object.
1010 * @image: Image to add the purgatory to.
1011 * @kbuf: Memory parameters to use.
1013 * Allocates the memory needed for image->purgatory_info.sechdrs and
1014 * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible
1015 * to free the memory after use.
1017 * Return: 0 on success, negative errno on error.
1019 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf)
1021 struct purgatory_info *pi = &image->purgatory_info;
1024 if (kexec_purgatory_size <= 0)
1027 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory;
1029 ret = kexec_purgatory_setup_kbuf(pi, kbuf);
1033 ret = kexec_purgatory_setup_sechdrs(pi, kbuf);
1037 ret = kexec_apply_relocations(image);
1046 vfree(pi->purgatory_buf);
1047 pi->purgatory_buf = NULL;
1052 * kexec_purgatory_find_symbol - find a symbol in the purgatory
1053 * @pi: Purgatory to search in.
1054 * @name: Name of the symbol.
1056 * Return: pointer to symbol in read-only symtab on success, NULL on error.
1058 static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
1061 const Elf_Shdr *sechdrs;
1062 const Elf_Ehdr *ehdr;
1063 const Elf_Sym *syms;
1071 sechdrs = (void *)ehdr + ehdr->e_shoff;
1073 for (i = 0; i < ehdr->e_shnum; i++) {
1074 if (sechdrs[i].sh_type != SHT_SYMTAB)
1077 if (sechdrs[i].sh_link >= ehdr->e_shnum)
1078 /* Invalid strtab section number */
1080 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset;
1081 syms = (void *)ehdr + sechdrs[i].sh_offset;
1083 /* Go through symbols for a match */
1084 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
1085 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
1088 if (strcmp(strtab + syms[k].st_name, name) != 0)
1091 if (syms[k].st_shndx == SHN_UNDEF ||
1092 syms[k].st_shndx >= ehdr->e_shnum) {
1093 pr_debug("Symbol: %s has bad section index %d.\n",
1094 name, syms[k].st_shndx);
1098 /* Found the symbol we are looking for */
1106 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
1108 struct purgatory_info *pi = &image->purgatory_info;
1112 sym = kexec_purgatory_find_symbol(pi, name);
1114 return ERR_PTR(-EINVAL);
1116 sechdr = &pi->sechdrs[sym->st_shndx];
1119 * Returns the address where symbol will finally be loaded after
1120 * kexec_load_segment()
1122 return (void *)(sechdr->sh_addr + sym->st_value);
1126 * Get or set value of a symbol. If "get_value" is true, symbol value is
1127 * returned in buf otherwise symbol value is set based on value in buf.
1129 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
1130 void *buf, unsigned int size, bool get_value)
1132 struct purgatory_info *pi = &image->purgatory_info;
1137 sym = kexec_purgatory_find_symbol(pi, name);
1141 if (sym->st_size != size) {
1142 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
1143 name, (unsigned long)sym->st_size, size);
1147 sec = pi->sechdrs + sym->st_shndx;
1149 if (sec->sh_type == SHT_NOBITS) {
1150 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
1151 get_value ? "get" : "set");
1155 sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value;
1158 memcpy((void *)buf, sym_buf, size);
1160 memcpy((void *)sym_buf, buf, size);
1164 #endif /* CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY */