kexec: add KEXEC_ELF
[linux-block.git] / kernel / kexec_elf.c
CommitLineData
175fca3b
SS
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Load ELF vmlinux file for the kexec_file_load syscall.
4 *
5 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
6 * Copyright (C) 2004 IBM Corp.
7 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
8 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
9 * Copyright (C) 2016 IBM Corporation
10 *
11 * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
12 * Heavily modified for the kernel by
13 * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
14 */
15
16#define pr_fmt(fmt) "kexec_elf: " fmt
17
18#include <linux/elf.h>
19#include <linux/kexec.h>
20#include <linux/module.h>
21#include <linux/slab.h>
22#include <linux/types.h>
23
24#define PURGATORY_STACK_SIZE (16 * 1024)
25
26#define elf_addr_to_cpu elf64_to_cpu
27
28#ifndef Elf_Rel
29#define Elf_Rel Elf64_Rel
30#endif /* Elf_Rel */
31
32static inline bool elf_is_elf_file(const struct elfhdr *ehdr)
33{
34 return memcmp(ehdr->e_ident, ELFMAG, SELFMAG) == 0;
35}
36
37static uint64_t elf64_to_cpu(const struct elfhdr *ehdr, uint64_t value)
38{
39 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
40 value = le64_to_cpu(value);
41 else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
42 value = be64_to_cpu(value);
43
44 return value;
45}
46
47static uint16_t elf16_to_cpu(const struct elfhdr *ehdr, uint16_t value)
48{
49 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
50 value = le16_to_cpu(value);
51 else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
52 value = be16_to_cpu(value);
53
54 return value;
55}
56
57static uint32_t elf32_to_cpu(const struct elfhdr *ehdr, uint32_t value)
58{
59 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
60 value = le32_to_cpu(value);
61 else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
62 value = be32_to_cpu(value);
63
64 return value;
65}
66
67/**
68 * elf_is_ehdr_sane - check that it is safe to use the ELF header
69 * @buf_len: size of the buffer in which the ELF file is loaded.
70 */
71static bool elf_is_ehdr_sane(const struct elfhdr *ehdr, size_t buf_len)
72{
73 if (ehdr->e_phnum > 0 && ehdr->e_phentsize != sizeof(struct elf_phdr)) {
74 pr_debug("Bad program header size.\n");
75 return false;
76 } else if (ehdr->e_shnum > 0 &&
77 ehdr->e_shentsize != sizeof(struct elf_shdr)) {
78 pr_debug("Bad section header size.\n");
79 return false;
80 } else if (ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
81 ehdr->e_version != EV_CURRENT) {
82 pr_debug("Unknown ELF version.\n");
83 return false;
84 }
85
86 if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) {
87 size_t phdr_size;
88
89 /*
90 * e_phnum is at most 65535 so calculating the size of the
91 * program header cannot overflow.
92 */
93 phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum;
94
95 /* Sanity check the program header table location. */
96 if (ehdr->e_phoff + phdr_size < ehdr->e_phoff) {
97 pr_debug("Program headers at invalid location.\n");
98 return false;
99 } else if (ehdr->e_phoff + phdr_size > buf_len) {
100 pr_debug("Program headers truncated.\n");
101 return false;
102 }
103 }
104
105 if (ehdr->e_shoff > 0 && ehdr->e_shnum > 0) {
106 size_t shdr_size;
107
108 /*
109 * e_shnum is at most 65536 so calculating
110 * the size of the section header cannot overflow.
111 */
112 shdr_size = sizeof(struct elf_shdr) * ehdr->e_shnum;
113
114 /* Sanity check the section header table location. */
115 if (ehdr->e_shoff + shdr_size < ehdr->e_shoff) {
116 pr_debug("Section headers at invalid location.\n");
117 return false;
118 } else if (ehdr->e_shoff + shdr_size > buf_len) {
119 pr_debug("Section headers truncated.\n");
120 return false;
121 }
122 }
123
124 return true;
125}
126
127static int elf_read_ehdr(const char *buf, size_t len, struct elfhdr *ehdr)
128{
129 struct elfhdr *buf_ehdr;
130
131 if (len < sizeof(*buf_ehdr)) {
132 pr_debug("Buffer is too small to hold ELF header.\n");
133 return -ENOEXEC;
134 }
135
136 memset(ehdr, 0, sizeof(*ehdr));
137 memcpy(ehdr->e_ident, buf, sizeof(ehdr->e_ident));
138 if (!elf_is_elf_file(ehdr)) {
139 pr_debug("No ELF header magic.\n");
140 return -ENOEXEC;
141 }
142
143 if (ehdr->e_ident[EI_CLASS] != ELF_CLASS) {
144 pr_debug("Not a supported ELF class.\n");
145 return -ENOEXEC;
146 } else if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB &&
147 ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
148 pr_debug("Not a supported ELF data format.\n");
149 return -ENOEXEC;
150 }
151
152 buf_ehdr = (struct elfhdr *) buf;
153 if (elf16_to_cpu(ehdr, buf_ehdr->e_ehsize) != sizeof(*buf_ehdr)) {
154 pr_debug("Bad ELF header size.\n");
155 return -ENOEXEC;
156 }
157
158 ehdr->e_type = elf16_to_cpu(ehdr, buf_ehdr->e_type);
159 ehdr->e_machine = elf16_to_cpu(ehdr, buf_ehdr->e_machine);
160 ehdr->e_version = elf32_to_cpu(ehdr, buf_ehdr->e_version);
161 ehdr->e_entry = elf_addr_to_cpu(ehdr, buf_ehdr->e_entry);
162 ehdr->e_phoff = elf_addr_to_cpu(ehdr, buf_ehdr->e_phoff);
163 ehdr->e_shoff = elf_addr_to_cpu(ehdr, buf_ehdr->e_shoff);
164 ehdr->e_flags = elf32_to_cpu(ehdr, buf_ehdr->e_flags);
165 ehdr->e_phentsize = elf16_to_cpu(ehdr, buf_ehdr->e_phentsize);
166 ehdr->e_phnum = elf16_to_cpu(ehdr, buf_ehdr->e_phnum);
167 ehdr->e_shentsize = elf16_to_cpu(ehdr, buf_ehdr->e_shentsize);
168 ehdr->e_shnum = elf16_to_cpu(ehdr, buf_ehdr->e_shnum);
169 ehdr->e_shstrndx = elf16_to_cpu(ehdr, buf_ehdr->e_shstrndx);
170
171 return elf_is_ehdr_sane(ehdr, len) ? 0 : -ENOEXEC;
172}
173
174/**
175 * elf_is_phdr_sane - check that it is safe to use the program header
176 * @buf_len: size of the buffer in which the ELF file is loaded.
177 */
178static bool elf_is_phdr_sane(const struct elf_phdr *phdr, size_t buf_len)
179{
180
181 if (phdr->p_offset + phdr->p_filesz < phdr->p_offset) {
182 pr_debug("ELF segment location wraps around.\n");
183 return false;
184 } else if (phdr->p_offset + phdr->p_filesz > buf_len) {
185 pr_debug("ELF segment not in file.\n");
186 return false;
187 } else if (phdr->p_paddr + phdr->p_memsz < phdr->p_paddr) {
188 pr_debug("ELF segment address wraps around.\n");
189 return false;
190 }
191
192 return true;
193}
194
195static int elf_read_phdr(const char *buf, size_t len,
196 struct kexec_elf_info *elf_info,
197 int idx)
198{
199 /* Override the const in proghdrs, we are the ones doing the loading. */
200 struct elf_phdr *phdr = (struct elf_phdr *) &elf_info->proghdrs[idx];
201 const char *pbuf;
202 struct elf_phdr *buf_phdr;
203
204 pbuf = buf + elf_info->ehdr->e_phoff + (idx * sizeof(*buf_phdr));
205 buf_phdr = (struct elf_phdr *) pbuf;
206
207 phdr->p_type = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_type);
208 phdr->p_offset = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_offset);
209 phdr->p_paddr = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_paddr);
210 phdr->p_vaddr = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_vaddr);
211 phdr->p_flags = elf32_to_cpu(elf_info->ehdr, buf_phdr->p_flags);
212
213 /*
214 * The following fields have a type equivalent to Elf_Addr
215 * both in 32 bit and 64 bit ELF.
216 */
217 phdr->p_filesz = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_filesz);
218 phdr->p_memsz = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_memsz);
219 phdr->p_align = elf_addr_to_cpu(elf_info->ehdr, buf_phdr->p_align);
220
221 return elf_is_phdr_sane(phdr, len) ? 0 : -ENOEXEC;
222}
223
224/**
225 * elf_read_phdrs - read the program headers from the buffer
226 *
227 * This function assumes that the program header table was checked for sanity.
228 * Use elf_is_ehdr_sane() if it wasn't.
229 */
230static int elf_read_phdrs(const char *buf, size_t len,
231 struct kexec_elf_info *elf_info)
232{
233 size_t phdr_size, i;
234 const struct elfhdr *ehdr = elf_info->ehdr;
235
236 /*
237 * e_phnum is at most 65535 so calculating the size of the
238 * program header cannot overflow.
239 */
240 phdr_size = sizeof(struct elf_phdr) * ehdr->e_phnum;
241
242 elf_info->proghdrs = kzalloc(phdr_size, GFP_KERNEL);
243 if (!elf_info->proghdrs)
244 return -ENOMEM;
245
246 for (i = 0; i < ehdr->e_phnum; i++) {
247 int ret;
248
249 ret = elf_read_phdr(buf, len, elf_info, i);
250 if (ret) {
251 kfree(elf_info->proghdrs);
252 elf_info->proghdrs = NULL;
253 return ret;
254 }
255 }
256
257 return 0;
258}
259
260/**
261 * elf_is_shdr_sane - check that it is safe to use the section header
262 * @buf_len: size of the buffer in which the ELF file is loaded.
263 */
264static bool elf_is_shdr_sane(const struct elf_shdr *shdr, size_t buf_len)
265{
266 bool size_ok;
267
268 /* SHT_NULL headers have undefined values, so we can't check them. */
269 if (shdr->sh_type == SHT_NULL)
270 return true;
271
272 /* Now verify sh_entsize */
273 switch (shdr->sh_type) {
274 case SHT_SYMTAB:
275 size_ok = shdr->sh_entsize == sizeof(Elf_Sym);
276 break;
277 case SHT_RELA:
278 size_ok = shdr->sh_entsize == sizeof(Elf_Rela);
279 break;
280 case SHT_DYNAMIC:
281 size_ok = shdr->sh_entsize == sizeof(Elf_Dyn);
282 break;
283 case SHT_REL:
284 size_ok = shdr->sh_entsize == sizeof(Elf_Rel);
285 break;
286 case SHT_NOTE:
287 case SHT_PROGBITS:
288 case SHT_HASH:
289 case SHT_NOBITS:
290 default:
291 /*
292 * This is a section whose entsize requirements
293 * I don't care about. If I don't know about
294 * the section I can't care about it's entsize
295 * requirements.
296 */
297 size_ok = true;
298 break;
299 }
300
301 if (!size_ok) {
302 pr_debug("ELF section with wrong entry size.\n");
303 return false;
304 } else if (shdr->sh_addr + shdr->sh_size < shdr->sh_addr) {
305 pr_debug("ELF section address wraps around.\n");
306 return false;
307 }
308
309 if (shdr->sh_type != SHT_NOBITS) {
310 if (shdr->sh_offset + shdr->sh_size < shdr->sh_offset) {
311 pr_debug("ELF section location wraps around.\n");
312 return false;
313 } else if (shdr->sh_offset + shdr->sh_size > buf_len) {
314 pr_debug("ELF section not in file.\n");
315 return false;
316 }
317 }
318
319 return true;
320}
321
322static int elf_read_shdr(const char *buf, size_t len,
323 struct kexec_elf_info *elf_info,
324 int idx)
325{
326 struct elf_shdr *shdr = &elf_info->sechdrs[idx];
327 const struct elfhdr *ehdr = elf_info->ehdr;
328 const char *sbuf;
329 struct elf_shdr *buf_shdr;
330
331 sbuf = buf + ehdr->e_shoff + idx * sizeof(*buf_shdr);
332 buf_shdr = (struct elf_shdr *) sbuf;
333
334 shdr->sh_name = elf32_to_cpu(ehdr, buf_shdr->sh_name);
335 shdr->sh_type = elf32_to_cpu(ehdr, buf_shdr->sh_type);
336 shdr->sh_addr = elf_addr_to_cpu(ehdr, buf_shdr->sh_addr);
337 shdr->sh_offset = elf_addr_to_cpu(ehdr, buf_shdr->sh_offset);
338 shdr->sh_link = elf32_to_cpu(ehdr, buf_shdr->sh_link);
339 shdr->sh_info = elf32_to_cpu(ehdr, buf_shdr->sh_info);
340
341 /*
342 * The following fields have a type equivalent to Elf_Addr
343 * both in 32 bit and 64 bit ELF.
344 */
345 shdr->sh_flags = elf_addr_to_cpu(ehdr, buf_shdr->sh_flags);
346 shdr->sh_size = elf_addr_to_cpu(ehdr, buf_shdr->sh_size);
347 shdr->sh_addralign = elf_addr_to_cpu(ehdr, buf_shdr->sh_addralign);
348 shdr->sh_entsize = elf_addr_to_cpu(ehdr, buf_shdr->sh_entsize);
349
350 return elf_is_shdr_sane(shdr, len) ? 0 : -ENOEXEC;
351}
352
353/**
354 * elf_read_shdrs - read the section headers from the buffer
355 *
356 * This function assumes that the section header table was checked for sanity.
357 * Use elf_is_ehdr_sane() if it wasn't.
358 */
359static int elf_read_shdrs(const char *buf, size_t len,
360 struct kexec_elf_info *elf_info)
361{
362 size_t shdr_size, i;
363
364 /*
365 * e_shnum is at most 65536 so calculating
366 * the size of the section header cannot overflow.
367 */
368 shdr_size = sizeof(struct elf_shdr) * elf_info->ehdr->e_shnum;
369
370 elf_info->sechdrs = kzalloc(shdr_size, GFP_KERNEL);
371 if (!elf_info->sechdrs)
372 return -ENOMEM;
373
374 for (i = 0; i < elf_info->ehdr->e_shnum; i++) {
375 int ret;
376
377 ret = elf_read_shdr(buf, len, elf_info, i);
378 if (ret) {
379 kfree(elf_info->sechdrs);
380 elf_info->sechdrs = NULL;
381 return ret;
382 }
383 }
384
385 return 0;
386}
387
388/**
389 * elf_read_from_buffer - read ELF file and sets up ELF header and ELF info
390 * @buf: Buffer to read ELF file from.
391 * @len: Size of @buf.
392 * @ehdr: Pointer to existing struct which will be populated.
393 * @elf_info: Pointer to existing struct which will be populated.
394 *
395 * This function allows reading ELF files with different byte order than
396 * the kernel, byte-swapping the fields as needed.
397 *
398 * Return:
399 * On success returns 0, and the caller should call
400 * kexec_free_elf_info(elf_info) to free the memory allocated for the section
401 * and program headers.
402 */
403static int elf_read_from_buffer(const char *buf, size_t len,
404 struct elfhdr *ehdr,
405 struct kexec_elf_info *elf_info)
406{
407 int ret;
408
409 ret = elf_read_ehdr(buf, len, ehdr);
410 if (ret)
411 return ret;
412
413 elf_info->buffer = buf;
414 elf_info->ehdr = ehdr;
415 if (ehdr->e_phoff > 0 && ehdr->e_phnum > 0) {
416 ret = elf_read_phdrs(buf, len, elf_info);
417 if (ret)
418 return ret;
419 }
420 if (ehdr->e_shoff > 0 && ehdr->e_shnum > 0) {
421 ret = elf_read_shdrs(buf, len, elf_info);
422 if (ret) {
423 kfree(elf_info->proghdrs);
424 return ret;
425 }
426 }
427
428 return 0;
429}
430
431/**
432 * kexec_free_elf_info - free memory allocated by elf_read_from_buffer
433 */
434void kexec_free_elf_info(struct kexec_elf_info *elf_info)
435{
436 kfree(elf_info->proghdrs);
437 kfree(elf_info->sechdrs);
438 memset(elf_info, 0, sizeof(*elf_info));
439}
440/**
441 * kexec_build_elf_info - read ELF executable and check that we can use it
442 */
443int kexec_build_elf_info(const char *buf, size_t len, struct elfhdr *ehdr,
444 struct kexec_elf_info *elf_info)
445{
446 int i;
447 int ret;
448
449 ret = elf_read_from_buffer(buf, len, ehdr, elf_info);
450 if (ret)
451 return ret;
452
453 /* Big endian vmlinux has type ET_DYN. */
454 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
455 pr_err("Not an ELF executable.\n");
456 goto error;
457 } else if (!elf_info->proghdrs) {
458 pr_err("No ELF program header.\n");
459 goto error;
460 }
461
462 for (i = 0; i < ehdr->e_phnum; i++) {
463 /*
464 * Kexec does not support loading interpreters.
465 * In addition this check keeps us from attempting
466 * to kexec ordinay executables.
467 */
468 if (elf_info->proghdrs[i].p_type == PT_INTERP) {
469 pr_err("Requires an ELF interpreter.\n");
470 goto error;
471 }
472 }
473
474 return 0;
475error:
476 kexec_free_elf_info(elf_info);
477 return -ENOEXEC;
478}
479
480
481int kexec_elf_probe(const char *buf, unsigned long len)
482{
483 struct elfhdr ehdr;
484 struct kexec_elf_info elf_info;
485 int ret;
486
487 ret = kexec_build_elf_info(buf, len, &ehdr, &elf_info);
488 if (ret)
489 return ret;
490
491 kexec_free_elf_info(&elf_info);
492
493 return elf_check_arch(&ehdr) ? 0 : -ENOEXEC;
494}
495
496/**
497 * kexec_elf_load - load ELF executable image
498 * @lowest_load_addr: On return, will be the address where the first PT_LOAD
499 * section will be loaded in memory.
500 *
501 * Return:
502 * 0 on success, negative value on failure.
503 */
504int kexec_elf_load(struct kimage *image, struct elfhdr *ehdr,
505 struct kexec_elf_info *elf_info,
506 struct kexec_buf *kbuf,
507 unsigned long *lowest_load_addr)
508{
509 unsigned long base = 0, lowest_addr = UINT_MAX;
510 int ret;
511 size_t i;
512
513 /* Read in the PT_LOAD segments. */
514 for (i = 0; i < ehdr->e_phnum; i++) {
515 unsigned long load_addr;
516 size_t size;
517 const struct elf_phdr *phdr;
518
519 phdr = &elf_info->proghdrs[i];
520 if (phdr->p_type != PT_LOAD)
521 continue;
522
523 size = phdr->p_filesz;
524 if (size > phdr->p_memsz)
525 size = phdr->p_memsz;
526
527 kbuf->buffer = (void *) elf_info->buffer + phdr->p_offset;
528 kbuf->bufsz = size;
529 kbuf->memsz = phdr->p_memsz;
530 kbuf->buf_align = phdr->p_align;
531 kbuf->buf_min = phdr->p_paddr + base;
532 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
533 ret = kexec_add_buffer(kbuf);
534 if (ret)
535 goto out;
536 load_addr = kbuf->mem;
537
538 if (load_addr < lowest_addr)
539 lowest_addr = load_addr;
540 }
541
542 /* Update entry point to reflect new load address. */
543 ehdr->e_entry += base;
544
545 *lowest_load_addr = lowest_addr;
546 ret = 0;
547 out:
548 return ret;
549}