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