1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ELF loader for kexec_file_load system call. 4 * 5 * Copyright IBM Corp. 2018 6 * 7 * Author(s): Philipp Rudo <prudo@linux.vnet.ibm.com> 8 */ 9 10 #include <linux/errno.h> 11 #include <linux/kernel.h> 12 #include <linux/kexec.h> 13 #include <asm/setup.h> 14 15 static int kexec_file_add_kernel_elf(struct kimage *image, 16 struct s390_load_data *data) 17 { 18 struct kexec_buf buf; 19 const Elf_Ehdr *ehdr; 20 const Elf_Phdr *phdr; 21 Elf_Addr entry; 22 void *kernel; 23 int i, ret; 24 25 kernel = image->kernel_buf; 26 ehdr = (Elf_Ehdr *)kernel; 27 buf.image = image; 28 if (image->type == KEXEC_TYPE_CRASH) 29 entry = STARTUP_KDUMP_OFFSET; 30 else 31 entry = ehdr->e_entry; 32 33 phdr = (void *)ehdr + ehdr->e_phoff; 34 for (i = 0; i < ehdr->e_phnum; i++, phdr++) { 35 if (phdr->p_type != PT_LOAD) 36 continue; 37 38 buf.buffer = kernel + phdr->p_offset; 39 buf.bufsz = phdr->p_filesz; 40 41 buf.mem = ALIGN(phdr->p_paddr, phdr->p_align); 42 if (image->type == KEXEC_TYPE_CRASH) 43 buf.mem += crashk_res.start; 44 buf.memsz = phdr->p_memsz; 45 data->memsz = ALIGN(data->memsz, phdr->p_align) + buf.memsz; 46 47 if (entry - phdr->p_paddr < phdr->p_memsz) { 48 data->kernel_buf = buf.buffer; 49 data->kernel_mem = buf.mem; 50 data->parm = buf.buffer + PARMAREA; 51 } 52 53 ret = kexec_add_buffer(&buf); 54 if (ret) 55 return ret; 56 } 57 58 return data->memsz ? 0 : -EINVAL; 59 } 60 61 static void *s390_elf_load(struct kimage *image, 62 char *kernel, unsigned long kernel_len, 63 char *initrd, unsigned long initrd_len, 64 char *cmdline, unsigned long cmdline_len) 65 { 66 const Elf_Ehdr *ehdr; 67 const Elf_Phdr *phdr; 68 size_t size; 69 int i; 70 71 /* image->fobs->probe already checked for valid ELF magic number. */ 72 ehdr = (Elf_Ehdr *)kernel; 73 74 if (ehdr->e_type != ET_EXEC || 75 ehdr->e_ident[EI_CLASS] != ELFCLASS64 || 76 !elf_check_arch(ehdr)) 77 return ERR_PTR(-EINVAL); 78 79 if (!ehdr->e_phnum || ehdr->e_phentsize != sizeof(Elf_Phdr)) 80 return ERR_PTR(-EINVAL); 81 82 size = ehdr->e_ehsize + ehdr->e_phoff; 83 size += ehdr->e_phentsize * ehdr->e_phnum; 84 if (size > kernel_len) 85 return ERR_PTR(-EINVAL); 86 87 phdr = (void *)ehdr + ehdr->e_phoff; 88 size = ALIGN(size, phdr->p_align); 89 for (i = 0; i < ehdr->e_phnum; i++, phdr++) { 90 if (phdr->p_type == PT_INTERP) 91 return ERR_PTR(-EINVAL); 92 93 if (phdr->p_offset > kernel_len) 94 return ERR_PTR(-EINVAL); 95 96 size += ALIGN(phdr->p_filesz, phdr->p_align); 97 } 98 99 if (size > kernel_len) 100 return ERR_PTR(-EINVAL); 101 102 return kexec_file_add_components(image, kexec_file_add_kernel_elf); 103 } 104 105 static int s390_elf_probe(const char *buf, unsigned long len) 106 { 107 const Elf_Ehdr *ehdr; 108 109 if (len < sizeof(Elf_Ehdr)) 110 return -ENOEXEC; 111 112 ehdr = (Elf_Ehdr *)buf; 113 114 /* Only check the ELF magic number here and do proper validity check 115 * in the loader. Any check here that fails would send the erroneous 116 * ELF file to the image loader that does not care what it gets. 117 * (Most likely) causing behavior not intended by the user. 118 */ 119 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) 120 return -ENOEXEC; 121 122 return 0; 123 } 124 125 const struct kexec_file_ops s390_kexec_elf_ops = { 126 .probe = s390_elf_probe, 127 .load = s390_elf_load, 128 }; 129