]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - kernel/kexec_file.c
kernel/kexec_file.c: remove checks in kexec_purgatory_load
[linux.git] / kernel / kexec_file.c
index e5bcd94c1efb13db5daa12d7dd188279ea25c1a1..81ba4f782486c83dd1886e377d89cf0df67f6401 100644 (file)
 #include <linux/ima.h>
 #include <crypto/hash.h>
 #include <crypto/sha.h>
+#include <linux/elf.h>
+#include <linux/elfcore.h>
+#include <linux/kernel.h>
+#include <linux/kexec.h>
+#include <linux/slab.h>
 #include <linux/syscalls.h>
 #include <linux/vmalloc.h>
 #include "kexec_internal.h"
 
 static int kexec_calculate_store_digests(struct kimage *image);
 
+/*
+ * Currently this is the only default function that is exported as some
+ * architectures need it to do additional handlings.
+ * In the future, other default functions may be exported too if required.
+ */
+int kexec_image_probe_default(struct kimage *image, void *buf,
+                             unsigned long buf_len)
+{
+       const struct kexec_file_ops * const *fops;
+       int ret = -ENOEXEC;
+
+       for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) {
+               ret = (*fops)->probe(buf, buf_len);
+               if (!ret) {
+                       image->fops = *fops;
+                       return ret;
+               }
+       }
+
+       return ret;
+}
+
 /* Architectures can provide this probe function */
 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
                                         unsigned long buf_len)
 {
-       return -ENOEXEC;
+       return kexec_image_probe_default(image, buf, buf_len);
+}
+
+static void *kexec_image_load_default(struct kimage *image)
+{
+       if (!image->fops || !image->fops->load)
+               return ERR_PTR(-ENOEXEC);
+
+       return image->fops->load(image, image->kernel_buf,
+                                image->kernel_buf_len, image->initrd_buf,
+                                image->initrd_buf_len, image->cmdline_buf,
+                                image->cmdline_buf_len);
 }
 
 void * __weak arch_kexec_kernel_image_load(struct kimage *image)
 {
-       return ERR_PTR(-ENOEXEC);
+       return kexec_image_load_default(image);
+}
+
+static int kexec_image_post_load_cleanup_default(struct kimage *image)
+{
+       if (!image->fops || !image->fops->cleanup)
+               return 0;
+
+       return image->fops->cleanup(image->image_loader_data);
 }
 
 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
 {
-       return -EINVAL;
+       return kexec_image_post_load_cleanup_default(image);
 }
 
 #ifdef CONFIG_KEXEC_VERIFY_SIG
+static int kexec_image_verify_sig_default(struct kimage *image, void *buf,
+                                         unsigned long buf_len)
+{
+       if (!image->fops || !image->fops->verify_sig) {
+               pr_debug("kernel loader does not support signature verification.\n");
+               return -EKEYREJECTED;
+       }
+
+       return image->fops->verify_sig(buf, buf_len);
+}
+
 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
                                        unsigned long buf_len)
 {
-       return -EKEYREJECTED;
+       return kexec_image_verify_sig_default(image, buf, buf_len);
 }
 #endif
 
@@ -532,6 +589,9 @@ static int kexec_calculate_store_digests(struct kimage *image)
        struct kexec_sha_region *sha_regions;
        struct purgatory_info *pi = &image->purgatory_info;
 
+       if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY))
+               return 0;
+
        zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
        zero_buf_sz = PAGE_SIZE;
 
@@ -633,6 +693,7 @@ static int kexec_calculate_store_digests(struct kimage *image)
        return ret;
 }
 
+#ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY
 /* Actually load purgatory. Lot of code taken from kexec-tools */
 static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
                                  unsigned long max, int top_down)
@@ -880,22 +941,8 @@ int kexec_load_purgatory(struct kimage *image, unsigned long min,
        if (kexec_purgatory_size <= 0)
                return -EINVAL;
 
-       if (kexec_purgatory_size < sizeof(Elf_Ehdr))
-               return -ENOEXEC;
-
        pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
 
-       if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
-           || pi->ehdr->e_type != ET_REL
-           || !elf_check_arch(pi->ehdr)
-           || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
-               return -ENOEXEC;
-
-       if (pi->ehdr->e_shoff >= kexec_purgatory_size
-           || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
-           kexec_purgatory_size - pi->ehdr->e_shoff))
-               return -ENOEXEC;
-
        ret = __kexec_load_purgatory(image, min, max, top_down);
        if (ret)
                return ret;
@@ -1022,3 +1069,174 @@ int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
 
        return 0;
 }
+#endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */
+
+int crash_exclude_mem_range(struct crash_mem *mem,
+                           unsigned long long mstart, unsigned long long mend)
+{
+       int i, j;
+       unsigned long long start, end;
+       struct crash_mem_range temp_range = {0, 0};
+
+       for (i = 0; i < mem->nr_ranges; i++) {
+               start = mem->ranges[i].start;
+               end = mem->ranges[i].end;
+
+               if (mstart > end || mend < start)
+                       continue;
+
+               /* Truncate any area outside of range */
+               if (mstart < start)
+                       mstart = start;
+               if (mend > end)
+                       mend = end;
+
+               /* Found completely overlapping range */
+               if (mstart == start && mend == end) {
+                       mem->ranges[i].start = 0;
+                       mem->ranges[i].end = 0;
+                       if (i < mem->nr_ranges - 1) {
+                               /* Shift rest of the ranges to left */
+                               for (j = i; j < mem->nr_ranges - 1; j++) {
+                                       mem->ranges[j].start =
+                                               mem->ranges[j+1].start;
+                                       mem->ranges[j].end =
+                                                       mem->ranges[j+1].end;
+                               }
+                       }
+                       mem->nr_ranges--;
+                       return 0;
+               }
+
+               if (mstart > start && mend < end) {
+                       /* Split original range */
+                       mem->ranges[i].end = mstart - 1;
+                       temp_range.start = mend + 1;
+                       temp_range.end = end;
+               } else if (mstart != start)
+                       mem->ranges[i].end = mstart - 1;
+               else
+                       mem->ranges[i].start = mend + 1;
+               break;
+       }
+
+       /* If a split happened, add the split to array */
+       if (!temp_range.end)
+               return 0;
+
+       /* Split happened */
+       if (i == mem->max_nr_ranges - 1)
+               return -ENOMEM;
+
+       /* Location where new range should go */
+       j = i + 1;
+       if (j < mem->nr_ranges) {
+               /* Move over all ranges one slot towards the end */
+               for (i = mem->nr_ranges - 1; i >= j; i--)
+                       mem->ranges[i + 1] = mem->ranges[i];
+       }
+
+       mem->ranges[j].start = temp_range.start;
+       mem->ranges[j].end = temp_range.end;
+       mem->nr_ranges++;
+       return 0;
+}
+
+int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map,
+                         void **addr, unsigned long *sz)
+{
+       Elf64_Ehdr *ehdr;
+       Elf64_Phdr *phdr;
+       unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
+       unsigned char *buf;
+       unsigned int cpu, i;
+       unsigned long long notes_addr;
+       unsigned long mstart, mend;
+
+       /* extra phdr for vmcoreinfo elf note */
+       nr_phdr = nr_cpus + 1;
+       nr_phdr += mem->nr_ranges;
+
+       /*
+        * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
+        * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
+        * I think this is required by tools like gdb. So same physical
+        * memory will be mapped in two elf headers. One will contain kernel
+        * text virtual addresses and other will have __va(physical) addresses.
+        */
+
+       nr_phdr++;
+       elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
+       elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
+
+       buf = vzalloc(elf_sz);
+       if (!buf)
+               return -ENOMEM;
+
+       ehdr = (Elf64_Ehdr *)buf;
+       phdr = (Elf64_Phdr *)(ehdr + 1);
+       memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
+       ehdr->e_ident[EI_CLASS] = ELFCLASS64;
+       ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
+       ehdr->e_ident[EI_VERSION] = EV_CURRENT;
+       ehdr->e_ident[EI_OSABI] = ELF_OSABI;
+       memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
+       ehdr->e_type = ET_CORE;
+       ehdr->e_machine = ELF_ARCH;
+       ehdr->e_version = EV_CURRENT;
+       ehdr->e_phoff = sizeof(Elf64_Ehdr);
+       ehdr->e_ehsize = sizeof(Elf64_Ehdr);
+       ehdr->e_phentsize = sizeof(Elf64_Phdr);
+
+       /* Prepare one phdr of type PT_NOTE for each present cpu */
+       for_each_present_cpu(cpu) {
+               phdr->p_type = PT_NOTE;
+               notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
+               phdr->p_offset = phdr->p_paddr = notes_addr;
+               phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
+               (ehdr->e_phnum)++;
+               phdr++;
+       }
+
+       /* Prepare one PT_NOTE header for vmcoreinfo */
+       phdr->p_type = PT_NOTE;
+       phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
+       phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
+       (ehdr->e_phnum)++;
+       phdr++;
+
+       /* Prepare PT_LOAD type program header for kernel text region */
+       if (kernel_map) {
+               phdr->p_type = PT_LOAD;
+               phdr->p_flags = PF_R|PF_W|PF_X;
+               phdr->p_vaddr = (Elf64_Addr)_text;
+               phdr->p_filesz = phdr->p_memsz = _end - _text;
+               phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
+               ehdr->e_phnum++;
+               phdr++;
+       }
+
+       /* Go through all the ranges in mem->ranges[] and prepare phdr */
+       for (i = 0; i < mem->nr_ranges; i++) {
+               mstart = mem->ranges[i].start;
+               mend = mem->ranges[i].end;
+
+               phdr->p_type = PT_LOAD;
+               phdr->p_flags = PF_R|PF_W|PF_X;
+               phdr->p_offset  = mstart;
+
+               phdr->p_paddr = mstart;
+               phdr->p_vaddr = (unsigned long long) __va(mstart);
+               phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
+               phdr->p_align = 0;
+               ehdr->e_phnum++;
+               phdr++;
+               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",
+                       phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
+                       ehdr->e_phnum, phdr->p_offset);
+       }
+
+       *addr = buf;
+       *sz = elf_sz;
+       return 0;
+}