~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/s390/kernel/kexec_elf.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php