blob: 20cdcccea16db445c1b31a811d3ca42903016241 [file] [log] [blame]
Dave Younga43cac02015-09-09 15:38:51 -07001/*
2 * kexec: kexec_file_load system call
3 *
4 * Copyright (C) 2014 Red Hat Inc.
5 * Authors:
6 * Vivek Goyal <vgoyal@redhat.com>
7 *
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
10 */
11
Minfei Huangde90a6b2015-11-06 16:32:45 -080012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Dave Younga43cac02015-09-09 15:38:51 -070014#include <linux/capability.h>
15#include <linux/mm.h>
16#include <linux/file.h>
17#include <linux/slab.h>
18#include <linux/kexec.h>
19#include <linux/mutex.h>
20#include <linux/list.h>
Mimi Zoharb804def2016-01-14 20:59:14 -050021#include <linux/fs.h>
Dave Younga43cac02015-09-09 15:38:51 -070022#include <crypto/hash.h>
23#include <crypto/sha.h>
24#include <linux/syscalls.h>
25#include <linux/vmalloc.h>
26#include "kexec_internal.h"
27
Dave Younga43cac02015-09-09 15:38:51 -070028static int kexec_calculate_store_digests(struct kimage *image);
29
Dave Younga43cac02015-09-09 15:38:51 -070030/* Architectures can provide this probe function */
31int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
32 unsigned long buf_len)
33{
34 return -ENOEXEC;
35}
36
37void * __weak arch_kexec_kernel_image_load(struct kimage *image)
38{
39 return ERR_PTR(-ENOEXEC);
40}
41
42int __weak arch_kimage_file_post_load_cleanup(struct kimage *image)
43{
44 return -EINVAL;
45}
46
Xunlei Pang978e30c2016-01-20 15:00:36 -080047#ifdef CONFIG_KEXEC_VERIFY_SIG
Dave Younga43cac02015-09-09 15:38:51 -070048int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,
49 unsigned long buf_len)
50{
51 return -EKEYREJECTED;
52}
Xunlei Pang978e30c2016-01-20 15:00:36 -080053#endif
Dave Younga43cac02015-09-09 15:38:51 -070054
55/* Apply relocations of type RELA */
56int __weak
57arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
58 unsigned int relsec)
59{
60 pr_err("RELA relocation unsupported.\n");
61 return -ENOEXEC;
62}
63
64/* Apply relocations of type REL */
65int __weak
66arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
67 unsigned int relsec)
68{
69 pr_err("REL relocation unsupported.\n");
70 return -ENOEXEC;
71}
72
73/*
74 * Free up memory used by kernel, initrd, and command line. This is temporary
75 * memory allocation which is not needed any more after these buffers have
76 * been loaded into separate segments and have been copied elsewhere.
77 */
78void kimage_file_post_load_cleanup(struct kimage *image)
79{
80 struct purgatory_info *pi = &image->purgatory_info;
81
82 vfree(image->kernel_buf);
83 image->kernel_buf = NULL;
84
85 vfree(image->initrd_buf);
86 image->initrd_buf = NULL;
87
88 kfree(image->cmdline_buf);
89 image->cmdline_buf = NULL;
90
91 vfree(pi->purgatory_buf);
92 pi->purgatory_buf = NULL;
93
94 vfree(pi->sechdrs);
95 pi->sechdrs = NULL;
96
97 /* See if architecture has anything to cleanup post load */
98 arch_kimage_file_post_load_cleanup(image);
99
100 /*
101 * Above call should have called into bootloader to free up
102 * any data stored in kimage->image_loader_data. It should
103 * be ok now to free it up.
104 */
105 kfree(image->image_loader_data);
106 image->image_loader_data = NULL;
107}
108
109/*
110 * In file mode list of segments is prepared by kernel. Copy relevant
111 * data from user space, do error checking, prepare segment list
112 */
113static int
114kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
115 const char __user *cmdline_ptr,
116 unsigned long cmdline_len, unsigned flags)
117{
118 int ret = 0;
119 void *ldata;
Mimi Zoharb804def2016-01-14 20:59:14 -0500120 loff_t size;
Dave Younga43cac02015-09-09 15:38:51 -0700121
Mimi Zoharb804def2016-01-14 20:59:14 -0500122 ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf,
123 &size, INT_MAX, READING_KEXEC_IMAGE);
Dave Younga43cac02015-09-09 15:38:51 -0700124 if (ret)
125 return ret;
Mimi Zoharb804def2016-01-14 20:59:14 -0500126 image->kernel_buf_len = size;
Dave Younga43cac02015-09-09 15:38:51 -0700127
128 /* Call arch image probe handlers */
129 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
130 image->kernel_buf_len);
Dave Younga43cac02015-09-09 15:38:51 -0700131 if (ret)
132 goto out;
133
134#ifdef CONFIG_KEXEC_VERIFY_SIG
135 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf,
136 image->kernel_buf_len);
137 if (ret) {
138 pr_debug("kernel signature verification failed.\n");
139 goto out;
140 }
141 pr_debug("kernel signature verification successful.\n");
142#endif
143 /* It is possible that there no initramfs is being loaded */
144 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) {
Mimi Zoharb804def2016-01-14 20:59:14 -0500145 ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf,
146 &size, INT_MAX,
147 READING_KEXEC_INITRAMFS);
Dave Younga43cac02015-09-09 15:38:51 -0700148 if (ret)
149 goto out;
Mimi Zoharb804def2016-01-14 20:59:14 -0500150 image->initrd_buf_len = size;
Dave Younga43cac02015-09-09 15:38:51 -0700151 }
152
153 if (cmdline_len) {
154 image->cmdline_buf = kzalloc(cmdline_len, GFP_KERNEL);
155 if (!image->cmdline_buf) {
156 ret = -ENOMEM;
157 goto out;
158 }
159
160 ret = copy_from_user(image->cmdline_buf, cmdline_ptr,
161 cmdline_len);
162 if (ret) {
163 ret = -EFAULT;
164 goto out;
165 }
166
167 image->cmdline_buf_len = cmdline_len;
168
169 /* command line should be a string with last byte null */
170 if (image->cmdline_buf[cmdline_len - 1] != '\0') {
171 ret = -EINVAL;
172 goto out;
173 }
174 }
175
176 /* Call arch image load handlers */
177 ldata = arch_kexec_kernel_image_load(image);
178
179 if (IS_ERR(ldata)) {
180 ret = PTR_ERR(ldata);
181 goto out;
182 }
183
184 image->image_loader_data = ldata;
185out:
186 /* In case of error, free up all allocated memory in this function */
187 if (ret)
188 kimage_file_post_load_cleanup(image);
189 return ret;
190}
191
192static int
193kimage_file_alloc_init(struct kimage **rimage, int kernel_fd,
194 int initrd_fd, const char __user *cmdline_ptr,
195 unsigned long cmdline_len, unsigned long flags)
196{
197 int ret;
198 struct kimage *image;
199 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH;
200
201 image = do_kimage_alloc_init();
202 if (!image)
203 return -ENOMEM;
204
205 image->file_mode = 1;
206
207 if (kexec_on_panic) {
208 /* Enable special crash kernel control page alloc policy. */
209 image->control_page = crashk_res.start;
210 image->type = KEXEC_TYPE_CRASH;
211 }
212
213 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd,
214 cmdline_ptr, cmdline_len, flags);
215 if (ret)
216 goto out_free_image;
217
218 ret = sanity_check_segment_list(image);
219 if (ret)
220 goto out_free_post_load_bufs;
221
222 ret = -ENOMEM;
223 image->control_code_page = kimage_alloc_control_pages(image,
224 get_order(KEXEC_CONTROL_PAGE_SIZE));
225 if (!image->control_code_page) {
226 pr_err("Could not allocate control_code_buffer\n");
227 goto out_free_post_load_bufs;
228 }
229
230 if (!kexec_on_panic) {
231 image->swap_page = kimage_alloc_control_pages(image, 0);
232 if (!image->swap_page) {
233 pr_err("Could not allocate swap buffer\n");
234 goto out_free_control_pages;
235 }
236 }
237
238 *rimage = image;
239 return 0;
240out_free_control_pages:
241 kimage_free_page_list(&image->control_pages);
242out_free_post_load_bufs:
243 kimage_file_post_load_cleanup(image);
244out_free_image:
245 kfree(image);
246 return ret;
247}
248
249SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
250 unsigned long, cmdline_len, const char __user *, cmdline_ptr,
251 unsigned long, flags)
252{
253 int ret = 0, i;
254 struct kimage **dest_image, *image;
255
256 /* We only trust the superuser with rebooting the system. */
257 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
258 return -EPERM;
259
260 /* Make sure we have a legal set of flags */
261 if (flags != (flags & KEXEC_FILE_FLAGS))
262 return -EINVAL;
263
264 image = NULL;
265
266 if (!mutex_trylock(&kexec_mutex))
267 return -EBUSY;
268
269 dest_image = &kexec_image;
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700270 if (flags & KEXEC_FILE_ON_CRASH) {
Dave Younga43cac02015-09-09 15:38:51 -0700271 dest_image = &kexec_crash_image;
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700272 if (kexec_crash_image)
273 arch_kexec_unprotect_crashkres();
274 }
Dave Younga43cac02015-09-09 15:38:51 -0700275
276 if (flags & KEXEC_FILE_UNLOAD)
277 goto exchange;
278
279 /*
280 * In case of crash, new kernel gets loaded in reserved region. It is
281 * same memory where old crash kernel might be loaded. Free any
282 * current crash dump kernel before we corrupt it.
283 */
284 if (flags & KEXEC_FILE_ON_CRASH)
285 kimage_free(xchg(&kexec_crash_image, NULL));
286
287 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr,
288 cmdline_len, flags);
289 if (ret)
290 goto out;
291
292 ret = machine_kexec_prepare(image);
293 if (ret)
294 goto out;
295
296 ret = kexec_calculate_store_digests(image);
297 if (ret)
298 goto out;
299
300 for (i = 0; i < image->nr_segments; i++) {
301 struct kexec_segment *ksegment;
302
303 ksegment = &image->segment[i];
304 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n",
305 i, ksegment->buf, ksegment->bufsz, ksegment->mem,
306 ksegment->memsz);
307
308 ret = kimage_load_segment(image, &image->segment[i]);
309 if (ret)
310 goto out;
311 }
312
313 kimage_terminate(image);
314
315 /*
316 * Free up any temporary buffers allocated which are not needed
317 * after image has been loaded
318 */
319 kimage_file_post_load_cleanup(image);
320exchange:
321 image = xchg(dest_image, image);
322out:
Xunlei Pang9b492cf2016-05-23 16:24:10 -0700323 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image)
324 arch_kexec_protect_crashkres();
325
Dave Younga43cac02015-09-09 15:38:51 -0700326 mutex_unlock(&kexec_mutex);
327 kimage_free(image);
328 return ret;
329}
330
331static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
332 struct kexec_buf *kbuf)
333{
334 struct kimage *image = kbuf->image;
335 unsigned long temp_start, temp_end;
336
337 temp_end = min(end, kbuf->buf_max);
338 temp_start = temp_end - kbuf->memsz;
339
340 do {
341 /* align down start */
342 temp_start = temp_start & (~(kbuf->buf_align - 1));
343
344 if (temp_start < start || temp_start < kbuf->buf_min)
345 return 0;
346
347 temp_end = temp_start + kbuf->memsz - 1;
348
349 /*
350 * Make sure this does not conflict with any of existing
351 * segments
352 */
353 if (kimage_is_destination_range(image, temp_start, temp_end)) {
354 temp_start = temp_start - PAGE_SIZE;
355 continue;
356 }
357
358 /* We found a suitable memory range */
359 break;
360 } while (1);
361
362 /* If we are here, we found a suitable memory range */
363 kbuf->mem = temp_start;
364
365 /* Success, stop navigating through remaining System RAM ranges */
366 return 1;
367}
368
369static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end,
370 struct kexec_buf *kbuf)
371{
372 struct kimage *image = kbuf->image;
373 unsigned long temp_start, temp_end;
374
375 temp_start = max(start, kbuf->buf_min);
376
377 do {
378 temp_start = ALIGN(temp_start, kbuf->buf_align);
379 temp_end = temp_start + kbuf->memsz - 1;
380
381 if (temp_end > end || temp_end > kbuf->buf_max)
382 return 0;
383 /*
384 * Make sure this does not conflict with any of existing
385 * segments
386 */
387 if (kimage_is_destination_range(image, temp_start, temp_end)) {
388 temp_start = temp_start + PAGE_SIZE;
389 continue;
390 }
391
392 /* We found a suitable memory range */
393 break;
394 } while (1);
395
396 /* If we are here, we found a suitable memory range */
397 kbuf->mem = temp_start;
398
399 /* Success, stop navigating through remaining System RAM ranges */
400 return 1;
401}
402
403static int locate_mem_hole_callback(u64 start, u64 end, void *arg)
404{
405 struct kexec_buf *kbuf = (struct kexec_buf *)arg;
406 unsigned long sz = end - start + 1;
407
408 /* Returning 0 will take to next memory range */
409 if (sz < kbuf->memsz)
410 return 0;
411
412 if (end < kbuf->buf_min || start > kbuf->buf_max)
413 return 0;
414
415 /*
416 * Allocate memory top down with-in ram range. Otherwise bottom up
417 * allocation.
418 */
419 if (kbuf->top_down)
420 return locate_mem_hole_top_down(start, end, kbuf);
421 return locate_mem_hole_bottom_up(start, end, kbuf);
422}
423
424/*
425 * Helper function for placing a buffer in a kexec segment. This assumes
426 * that kexec_mutex is held.
427 */
428int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz,
429 unsigned long memsz, unsigned long buf_align,
430 unsigned long buf_min, unsigned long buf_max,
431 bool top_down, unsigned long *load_addr)
432{
433
434 struct kexec_segment *ksegment;
435 struct kexec_buf buf, *kbuf;
436 int ret;
437
438 /* Currently adding segment this way is allowed only in file mode */
439 if (!image->file_mode)
440 return -EINVAL;
441
442 if (image->nr_segments >= KEXEC_SEGMENT_MAX)
443 return -EINVAL;
444
445 /*
446 * Make sure we are not trying to add buffer after allocating
447 * control pages. All segments need to be placed first before
448 * any control pages are allocated. As control page allocation
449 * logic goes through list of segments to make sure there are
450 * no destination overlaps.
451 */
452 if (!list_empty(&image->control_pages)) {
453 WARN_ON(1);
454 return -EINVAL;
455 }
456
457 memset(&buf, 0, sizeof(struct kexec_buf));
458 kbuf = &buf;
459 kbuf->image = image;
460 kbuf->buffer = buffer;
461 kbuf->bufsz = bufsz;
462
463 kbuf->memsz = ALIGN(memsz, PAGE_SIZE);
464 kbuf->buf_align = max(buf_align, PAGE_SIZE);
465 kbuf->buf_min = buf_min;
466 kbuf->buf_max = buf_max;
467 kbuf->top_down = top_down;
468
469 /* Walk the RAM ranges and allocate a suitable range for the buffer */
470 if (image->type == KEXEC_TYPE_CRASH)
Toshi Kanif0f47112016-01-26 21:57:30 +0100471 ret = walk_iomem_res_desc(crashk_res.desc,
472 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY,
473 crashk_res.start, crashk_res.end, kbuf,
474 locate_mem_hole_callback);
Dave Younga43cac02015-09-09 15:38:51 -0700475 else
476 ret = walk_system_ram_res(0, -1, kbuf,
477 locate_mem_hole_callback);
478 if (ret != 1) {
479 /* A suitable memory range could not be found for buffer */
480 return -EADDRNOTAVAIL;
481 }
482
483 /* Found a suitable memory range */
484 ksegment = &image->segment[image->nr_segments];
485 ksegment->kbuf = kbuf->buffer;
486 ksegment->bufsz = kbuf->bufsz;
487 ksegment->mem = kbuf->mem;
488 ksegment->memsz = kbuf->memsz;
489 image->nr_segments++;
490 *load_addr = ksegment->mem;
491 return 0;
492}
493
494/* Calculate and store the digest of segments */
495static int kexec_calculate_store_digests(struct kimage *image)
496{
497 struct crypto_shash *tfm;
498 struct shash_desc *desc;
499 int ret = 0, i, j, zero_buf_sz, sha_region_sz;
500 size_t desc_size, nullsz;
501 char *digest;
502 void *zero_buf;
503 struct kexec_sha_region *sha_regions;
504 struct purgatory_info *pi = &image->purgatory_info;
505
506 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT);
507 zero_buf_sz = PAGE_SIZE;
508
509 tfm = crypto_alloc_shash("sha256", 0, 0);
510 if (IS_ERR(tfm)) {
511 ret = PTR_ERR(tfm);
512 goto out;
513 }
514
515 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
516 desc = kzalloc(desc_size, GFP_KERNEL);
517 if (!desc) {
518 ret = -ENOMEM;
519 goto out_free_tfm;
520 }
521
522 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region);
523 sha_regions = vzalloc(sha_region_sz);
Jia-Ju Bai9fe9bb12021-05-06 18:04:38 -0700524 if (!sha_regions) {
525 ret = -ENOMEM;
Dave Younga43cac02015-09-09 15:38:51 -0700526 goto out_free_desc;
Jia-Ju Bai9fe9bb12021-05-06 18:04:38 -0700527 }
Dave Younga43cac02015-09-09 15:38:51 -0700528
529 desc->tfm = tfm;
530 desc->flags = 0;
531
532 ret = crypto_shash_init(desc);
533 if (ret < 0)
534 goto out_free_sha_regions;
535
536 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
537 if (!digest) {
538 ret = -ENOMEM;
539 goto out_free_sha_regions;
540 }
541
542 for (j = i = 0; i < image->nr_segments; i++) {
543 struct kexec_segment *ksegment;
544
545 ksegment = &image->segment[i];
546 /*
547 * Skip purgatory as it will be modified once we put digest
548 * info in purgatory.
549 */
550 if (ksegment->kbuf == pi->purgatory_buf)
551 continue;
552
553 ret = crypto_shash_update(desc, ksegment->kbuf,
554 ksegment->bufsz);
555 if (ret)
556 break;
557
558 /*
559 * Assume rest of the buffer is filled with zero and
560 * update digest accordingly.
561 */
562 nullsz = ksegment->memsz - ksegment->bufsz;
563 while (nullsz) {
564 unsigned long bytes = nullsz;
565
566 if (bytes > zero_buf_sz)
567 bytes = zero_buf_sz;
568 ret = crypto_shash_update(desc, zero_buf, bytes);
569 if (ret)
570 break;
571 nullsz -= bytes;
572 }
573
574 if (ret)
575 break;
576
577 sha_regions[j].start = ksegment->mem;
578 sha_regions[j].len = ksegment->memsz;
579 j++;
580 }
581
582 if (!ret) {
583 ret = crypto_shash_final(desc, digest);
584 if (ret)
585 goto out_free_digest;
586 ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
587 sha_regions, sha_region_sz, 0);
588 if (ret)
589 goto out_free_digest;
590
591 ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
592 digest, SHA256_DIGEST_SIZE, 0);
593 if (ret)
594 goto out_free_digest;
595 }
596
597out_free_digest:
598 kfree(digest);
599out_free_sha_regions:
600 vfree(sha_regions);
601out_free_desc:
602 kfree(desc);
603out_free_tfm:
604 kfree(tfm);
605out:
606 return ret;
607}
608
609/* Actually load purgatory. Lot of code taken from kexec-tools */
610static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
611 unsigned long max, int top_down)
612{
613 struct purgatory_info *pi = &image->purgatory_info;
614 unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
615 unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
616 unsigned char *buf_addr, *src;
617 int i, ret = 0, entry_sidx = -1;
618 const Elf_Shdr *sechdrs_c;
619 Elf_Shdr *sechdrs = NULL;
620 void *purgatory_buf = NULL;
621
622 /*
623 * sechdrs_c points to section headers in purgatory and are read
624 * only. No modifications allowed.
625 */
626 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
627
628 /*
629 * We can not modify sechdrs_c[] and its fields. It is read only.
630 * Copy it over to a local copy where one can store some temporary
631 * data and free it at the end. We need to modify ->sh_addr and
632 * ->sh_offset fields to keep track of permanent and temporary
633 * locations of sections.
634 */
635 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
636 if (!sechdrs)
637 return -ENOMEM;
638
639 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
640
641 /*
642 * We seem to have multiple copies of sections. First copy is which
643 * is embedded in kernel in read only section. Some of these sections
644 * will be copied to a temporary buffer and relocated. And these
645 * sections will finally be copied to their final destination at
646 * segment load time.
647 *
648 * Use ->sh_offset to reflect section address in memory. It will
649 * point to original read only copy if section is not allocatable.
650 * Otherwise it will point to temporary copy which will be relocated.
651 *
652 * Use ->sh_addr to contain final address of the section where it
653 * will go during execution time.
654 */
655 for (i = 0; i < pi->ehdr->e_shnum; i++) {
656 if (sechdrs[i].sh_type == SHT_NOBITS)
657 continue;
658
659 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
660 sechdrs[i].sh_offset;
661 }
662
663 /*
664 * Identify entry point section and make entry relative to section
665 * start.
666 */
667 entry = pi->ehdr->e_entry;
668 for (i = 0; i < pi->ehdr->e_shnum; i++) {
669 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
670 continue;
671
672 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
673 continue;
674
675 /* Make entry section relative */
676 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
677 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
678 pi->ehdr->e_entry)) {
679 entry_sidx = i;
680 entry -= sechdrs[i].sh_addr;
681 break;
682 }
683 }
684
685 /* Determine how much memory is needed to load relocatable object. */
686 buf_align = 1;
687 bss_align = 1;
688 buf_sz = 0;
689 bss_sz = 0;
690
691 for (i = 0; i < pi->ehdr->e_shnum; i++) {
692 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
693 continue;
694
695 align = sechdrs[i].sh_addralign;
696 if (sechdrs[i].sh_type != SHT_NOBITS) {
697 if (buf_align < align)
698 buf_align = align;
699 buf_sz = ALIGN(buf_sz, align);
700 buf_sz += sechdrs[i].sh_size;
701 } else {
702 /* bss section */
703 if (bss_align < align)
704 bss_align = align;
705 bss_sz = ALIGN(bss_sz, align);
706 bss_sz += sechdrs[i].sh_size;
707 }
708 }
709
710 /* Determine the bss padding required to align bss properly */
711 bss_pad = 0;
712 if (buf_sz & (bss_align - 1))
713 bss_pad = bss_align - (buf_sz & (bss_align - 1));
714
715 memsz = buf_sz + bss_pad + bss_sz;
716
717 /* Allocate buffer for purgatory */
718 purgatory_buf = vzalloc(buf_sz);
719 if (!purgatory_buf) {
720 ret = -ENOMEM;
721 goto out;
722 }
723
724 if (buf_align < bss_align)
725 buf_align = bss_align;
726
727 /* Add buffer to segment list */
728 ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
729 buf_align, min, max, top_down,
730 &pi->purgatory_load_addr);
731 if (ret)
732 goto out;
733
734 /* Load SHF_ALLOC sections */
735 buf_addr = purgatory_buf;
736 load_addr = curr_load_addr = pi->purgatory_load_addr;
737 bss_addr = load_addr + buf_sz + bss_pad;
738
739 for (i = 0; i < pi->ehdr->e_shnum; i++) {
740 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
741 continue;
742
743 align = sechdrs[i].sh_addralign;
744 if (sechdrs[i].sh_type != SHT_NOBITS) {
745 curr_load_addr = ALIGN(curr_load_addr, align);
746 offset = curr_load_addr - load_addr;
747 /* We already modifed ->sh_offset to keep src addr */
748 src = (char *) sechdrs[i].sh_offset;
749 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
750
751 /* Store load address and source address of section */
752 sechdrs[i].sh_addr = curr_load_addr;
753
754 /*
755 * This section got copied to temporary buffer. Update
756 * ->sh_offset accordingly.
757 */
758 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
759
760 /* Advance to the next address */
761 curr_load_addr += sechdrs[i].sh_size;
762 } else {
763 bss_addr = ALIGN(bss_addr, align);
764 sechdrs[i].sh_addr = bss_addr;
765 bss_addr += sechdrs[i].sh_size;
766 }
767 }
768
769 /* Update entry point based on load address of text section */
770 if (entry_sidx >= 0)
771 entry += sechdrs[entry_sidx].sh_addr;
772
773 /* Make kernel jump to purgatory after shutdown */
774 image->start = entry;
775
776 /* Used later to get/set symbol values */
777 pi->sechdrs = sechdrs;
778
779 /*
780 * Used later to identify which section is purgatory and skip it
781 * from checksumming.
782 */
783 pi->purgatory_buf = purgatory_buf;
784 return ret;
785out:
786 vfree(sechdrs);
787 vfree(purgatory_buf);
788 return ret;
789}
790
791static int kexec_apply_relocations(struct kimage *image)
792{
793 int i, ret;
794 struct purgatory_info *pi = &image->purgatory_info;
795 Elf_Shdr *sechdrs = pi->sechdrs;
796
797 /* Apply relocations */
798 for (i = 0; i < pi->ehdr->e_shnum; i++) {
799 Elf_Shdr *section, *symtab;
800
801 if (sechdrs[i].sh_type != SHT_RELA &&
802 sechdrs[i].sh_type != SHT_REL)
803 continue;
804
805 /*
806 * For section of type SHT_RELA/SHT_REL,
807 * ->sh_link contains section header index of associated
808 * symbol table. And ->sh_info contains section header
809 * index of section to which relocations apply.
810 */
811 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
812 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
813 return -ENOEXEC;
814
815 section = &sechdrs[sechdrs[i].sh_info];
816 symtab = &sechdrs[sechdrs[i].sh_link];
817
818 if (!(section->sh_flags & SHF_ALLOC))
819 continue;
820
821 /*
822 * symtab->sh_link contain section header index of associated
823 * string table.
824 */
825 if (symtab->sh_link >= pi->ehdr->e_shnum)
826 /* Invalid section number? */
827 continue;
828
829 /*
830 * Respective architecture needs to provide support for applying
831 * relocations of type SHT_RELA/SHT_REL.
832 */
833 if (sechdrs[i].sh_type == SHT_RELA)
834 ret = arch_kexec_apply_relocations_add(pi->ehdr,
835 sechdrs, i);
836 else if (sechdrs[i].sh_type == SHT_REL)
837 ret = arch_kexec_apply_relocations(pi->ehdr,
838 sechdrs, i);
839 if (ret)
840 return ret;
841 }
842
843 return 0;
844}
845
846/* Load relocatable purgatory object and relocate it appropriately */
847int kexec_load_purgatory(struct kimage *image, unsigned long min,
848 unsigned long max, int top_down,
849 unsigned long *load_addr)
850{
851 struct purgatory_info *pi = &image->purgatory_info;
852 int ret;
853
854 if (kexec_purgatory_size <= 0)
855 return -EINVAL;
856
857 if (kexec_purgatory_size < sizeof(Elf_Ehdr))
858 return -ENOEXEC;
859
860 pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
861
862 if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
863 || pi->ehdr->e_type != ET_REL
864 || !elf_check_arch(pi->ehdr)
865 || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
866 return -ENOEXEC;
867
868 if (pi->ehdr->e_shoff >= kexec_purgatory_size
869 || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
870 kexec_purgatory_size - pi->ehdr->e_shoff))
871 return -ENOEXEC;
872
873 ret = __kexec_load_purgatory(image, min, max, top_down);
874 if (ret)
875 return ret;
876
877 ret = kexec_apply_relocations(image);
878 if (ret)
879 goto out;
880
881 *load_addr = pi->purgatory_load_addr;
882 return 0;
883out:
884 vfree(pi->sechdrs);
Thiago Jung Bauermann070c43e2016-09-01 16:14:44 -0700885 pi->sechdrs = NULL;
886
Dave Younga43cac02015-09-09 15:38:51 -0700887 vfree(pi->purgatory_buf);
Thiago Jung Bauermann070c43e2016-09-01 16:14:44 -0700888 pi->purgatory_buf = NULL;
Dave Younga43cac02015-09-09 15:38:51 -0700889 return ret;
890}
891
892static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
893 const char *name)
894{
895 Elf_Sym *syms;
896 Elf_Shdr *sechdrs;
897 Elf_Ehdr *ehdr;
898 int i, k;
899 const char *strtab;
900
901 if (!pi->sechdrs || !pi->ehdr)
902 return NULL;
903
904 sechdrs = pi->sechdrs;
905 ehdr = pi->ehdr;
906
907 for (i = 0; i < ehdr->e_shnum; i++) {
908 if (sechdrs[i].sh_type != SHT_SYMTAB)
909 continue;
910
911 if (sechdrs[i].sh_link >= ehdr->e_shnum)
912 /* Invalid strtab section number */
913 continue;
914 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
915 syms = (Elf_Sym *)sechdrs[i].sh_offset;
916
917 /* Go through symbols for a match */
918 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
919 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
920 continue;
921
922 if (strcmp(strtab + syms[k].st_name, name) != 0)
923 continue;
924
925 if (syms[k].st_shndx == SHN_UNDEF ||
926 syms[k].st_shndx >= ehdr->e_shnum) {
927 pr_debug("Symbol: %s has bad section index %d.\n",
928 name, syms[k].st_shndx);
929 return NULL;
930 }
931
932 /* Found the symbol we are looking for */
933 return &syms[k];
934 }
935 }
936
937 return NULL;
938}
939
940void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
941{
942 struct purgatory_info *pi = &image->purgatory_info;
943 Elf_Sym *sym;
944 Elf_Shdr *sechdr;
945
946 sym = kexec_purgatory_find_symbol(pi, name);
947 if (!sym)
948 return ERR_PTR(-EINVAL);
949
950 sechdr = &pi->sechdrs[sym->st_shndx];
951
952 /*
953 * Returns the address where symbol will finally be loaded after
954 * kexec_load_segment()
955 */
956 return (void *)(sechdr->sh_addr + sym->st_value);
957}
958
959/*
960 * Get or set value of a symbol. If "get_value" is true, symbol value is
961 * returned in buf otherwise symbol value is set based on value in buf.
962 */
963int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
964 void *buf, unsigned int size, bool get_value)
965{
966 Elf_Sym *sym;
967 Elf_Shdr *sechdrs;
968 struct purgatory_info *pi = &image->purgatory_info;
969 char *sym_buf;
970
971 sym = kexec_purgatory_find_symbol(pi, name);
972 if (!sym)
973 return -EINVAL;
974
975 if (sym->st_size != size) {
976 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
977 name, (unsigned long)sym->st_size, size);
978 return -EINVAL;
979 }
980
981 sechdrs = pi->sechdrs;
982
983 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
984 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
985 get_value ? "get" : "set");
986 return -EINVAL;
987 }
988
989 sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
990 sym->st_value;
991
992 if (get_value)
993 memcpy((void *)buf, sym_buf, size);
994 else
995 memcpy((void *)sym_buf, buf, size);
996
997 return 0;
998}