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