blob: 38f76653ed7a5ea98a0af4556fc0deca901a2d40 [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);
524 if (!sha_regions)
525 goto out_free_desc;
526
527 desc->tfm = tfm;
528 desc->flags = 0;
529
530 ret = crypto_shash_init(desc);
531 if (ret < 0)
532 goto out_free_sha_regions;
533
534 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
535 if (!digest) {
536 ret = -ENOMEM;
537 goto out_free_sha_regions;
538 }
539
540 for (j = i = 0; i < image->nr_segments; i++) {
541 struct kexec_segment *ksegment;
542
543 ksegment = &image->segment[i];
544 /*
545 * Skip purgatory as it will be modified once we put digest
546 * info in purgatory.
547 */
548 if (ksegment->kbuf == pi->purgatory_buf)
549 continue;
550
551 ret = crypto_shash_update(desc, ksegment->kbuf,
552 ksegment->bufsz);
553 if (ret)
554 break;
555
556 /*
557 * Assume rest of the buffer is filled with zero and
558 * update digest accordingly.
559 */
560 nullsz = ksegment->memsz - ksegment->bufsz;
561 while (nullsz) {
562 unsigned long bytes = nullsz;
563
564 if (bytes > zero_buf_sz)
565 bytes = zero_buf_sz;
566 ret = crypto_shash_update(desc, zero_buf, bytes);
567 if (ret)
568 break;
569 nullsz -= bytes;
570 }
571
572 if (ret)
573 break;
574
575 sha_regions[j].start = ksegment->mem;
576 sha_regions[j].len = ksegment->memsz;
577 j++;
578 }
579
580 if (!ret) {
581 ret = crypto_shash_final(desc, digest);
582 if (ret)
583 goto out_free_digest;
584 ret = kexec_purgatory_get_set_symbol(image, "sha_regions",
585 sha_regions, sha_region_sz, 0);
586 if (ret)
587 goto out_free_digest;
588
589 ret = kexec_purgatory_get_set_symbol(image, "sha256_digest",
590 digest, SHA256_DIGEST_SIZE, 0);
591 if (ret)
592 goto out_free_digest;
593 }
594
595out_free_digest:
596 kfree(digest);
597out_free_sha_regions:
598 vfree(sha_regions);
599out_free_desc:
600 kfree(desc);
601out_free_tfm:
602 kfree(tfm);
603out:
604 return ret;
605}
606
607/* Actually load purgatory. Lot of code taken from kexec-tools */
608static int __kexec_load_purgatory(struct kimage *image, unsigned long min,
609 unsigned long max, int top_down)
610{
611 struct purgatory_info *pi = &image->purgatory_info;
612 unsigned long align, buf_align, bss_align, buf_sz, bss_sz, bss_pad;
613 unsigned long memsz, entry, load_addr, curr_load_addr, bss_addr, offset;
614 unsigned char *buf_addr, *src;
615 int i, ret = 0, entry_sidx = -1;
616 const Elf_Shdr *sechdrs_c;
617 Elf_Shdr *sechdrs = NULL;
618 void *purgatory_buf = NULL;
619
620 /*
621 * sechdrs_c points to section headers in purgatory and are read
622 * only. No modifications allowed.
623 */
624 sechdrs_c = (void *)pi->ehdr + pi->ehdr->e_shoff;
625
626 /*
627 * We can not modify sechdrs_c[] and its fields. It is read only.
628 * Copy it over to a local copy where one can store some temporary
629 * data and free it at the end. We need to modify ->sh_addr and
630 * ->sh_offset fields to keep track of permanent and temporary
631 * locations of sections.
632 */
633 sechdrs = vzalloc(pi->ehdr->e_shnum * sizeof(Elf_Shdr));
634 if (!sechdrs)
635 return -ENOMEM;
636
637 memcpy(sechdrs, sechdrs_c, pi->ehdr->e_shnum * sizeof(Elf_Shdr));
638
639 /*
640 * We seem to have multiple copies of sections. First copy is which
641 * is embedded in kernel in read only section. Some of these sections
642 * will be copied to a temporary buffer and relocated. And these
643 * sections will finally be copied to their final destination at
644 * segment load time.
645 *
646 * Use ->sh_offset to reflect section address in memory. It will
647 * point to original read only copy if section is not allocatable.
648 * Otherwise it will point to temporary copy which will be relocated.
649 *
650 * Use ->sh_addr to contain final address of the section where it
651 * will go during execution time.
652 */
653 for (i = 0; i < pi->ehdr->e_shnum; i++) {
654 if (sechdrs[i].sh_type == SHT_NOBITS)
655 continue;
656
657 sechdrs[i].sh_offset = (unsigned long)pi->ehdr +
658 sechdrs[i].sh_offset;
659 }
660
661 /*
662 * Identify entry point section and make entry relative to section
663 * start.
664 */
665 entry = pi->ehdr->e_entry;
666 for (i = 0; i < pi->ehdr->e_shnum; i++) {
667 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
668 continue;
669
670 if (!(sechdrs[i].sh_flags & SHF_EXECINSTR))
671 continue;
672
673 /* Make entry section relative */
674 if (sechdrs[i].sh_addr <= pi->ehdr->e_entry &&
675 ((sechdrs[i].sh_addr + sechdrs[i].sh_size) >
676 pi->ehdr->e_entry)) {
677 entry_sidx = i;
678 entry -= sechdrs[i].sh_addr;
679 break;
680 }
681 }
682
683 /* Determine how much memory is needed to load relocatable object. */
684 buf_align = 1;
685 bss_align = 1;
686 buf_sz = 0;
687 bss_sz = 0;
688
689 for (i = 0; i < pi->ehdr->e_shnum; i++) {
690 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
691 continue;
692
693 align = sechdrs[i].sh_addralign;
694 if (sechdrs[i].sh_type != SHT_NOBITS) {
695 if (buf_align < align)
696 buf_align = align;
697 buf_sz = ALIGN(buf_sz, align);
698 buf_sz += sechdrs[i].sh_size;
699 } else {
700 /* bss section */
701 if (bss_align < align)
702 bss_align = align;
703 bss_sz = ALIGN(bss_sz, align);
704 bss_sz += sechdrs[i].sh_size;
705 }
706 }
707
708 /* Determine the bss padding required to align bss properly */
709 bss_pad = 0;
710 if (buf_sz & (bss_align - 1))
711 bss_pad = bss_align - (buf_sz & (bss_align - 1));
712
713 memsz = buf_sz + bss_pad + bss_sz;
714
715 /* Allocate buffer for purgatory */
716 purgatory_buf = vzalloc(buf_sz);
717 if (!purgatory_buf) {
718 ret = -ENOMEM;
719 goto out;
720 }
721
722 if (buf_align < bss_align)
723 buf_align = bss_align;
724
725 /* Add buffer to segment list */
726 ret = kexec_add_buffer(image, purgatory_buf, buf_sz, memsz,
727 buf_align, min, max, top_down,
728 &pi->purgatory_load_addr);
729 if (ret)
730 goto out;
731
732 /* Load SHF_ALLOC sections */
733 buf_addr = purgatory_buf;
734 load_addr = curr_load_addr = pi->purgatory_load_addr;
735 bss_addr = load_addr + buf_sz + bss_pad;
736
737 for (i = 0; i < pi->ehdr->e_shnum; i++) {
738 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
739 continue;
740
741 align = sechdrs[i].sh_addralign;
742 if (sechdrs[i].sh_type != SHT_NOBITS) {
743 curr_load_addr = ALIGN(curr_load_addr, align);
744 offset = curr_load_addr - load_addr;
745 /* We already modifed ->sh_offset to keep src addr */
746 src = (char *) sechdrs[i].sh_offset;
747 memcpy(buf_addr + offset, src, sechdrs[i].sh_size);
748
749 /* Store load address and source address of section */
750 sechdrs[i].sh_addr = curr_load_addr;
751
752 /*
753 * This section got copied to temporary buffer. Update
754 * ->sh_offset accordingly.
755 */
756 sechdrs[i].sh_offset = (unsigned long)(buf_addr + offset);
757
758 /* Advance to the next address */
759 curr_load_addr += sechdrs[i].sh_size;
760 } else {
761 bss_addr = ALIGN(bss_addr, align);
762 sechdrs[i].sh_addr = bss_addr;
763 bss_addr += sechdrs[i].sh_size;
764 }
765 }
766
767 /* Update entry point based on load address of text section */
768 if (entry_sidx >= 0)
769 entry += sechdrs[entry_sidx].sh_addr;
770
771 /* Make kernel jump to purgatory after shutdown */
772 image->start = entry;
773
774 /* Used later to get/set symbol values */
775 pi->sechdrs = sechdrs;
776
777 /*
778 * Used later to identify which section is purgatory and skip it
779 * from checksumming.
780 */
781 pi->purgatory_buf = purgatory_buf;
782 return ret;
783out:
784 vfree(sechdrs);
785 vfree(purgatory_buf);
786 return ret;
787}
788
789static int kexec_apply_relocations(struct kimage *image)
790{
791 int i, ret;
792 struct purgatory_info *pi = &image->purgatory_info;
793 Elf_Shdr *sechdrs = pi->sechdrs;
794
795 /* Apply relocations */
796 for (i = 0; i < pi->ehdr->e_shnum; i++) {
797 Elf_Shdr *section, *symtab;
798
799 if (sechdrs[i].sh_type != SHT_RELA &&
800 sechdrs[i].sh_type != SHT_REL)
801 continue;
802
803 /*
804 * For section of type SHT_RELA/SHT_REL,
805 * ->sh_link contains section header index of associated
806 * symbol table. And ->sh_info contains section header
807 * index of section to which relocations apply.
808 */
809 if (sechdrs[i].sh_info >= pi->ehdr->e_shnum ||
810 sechdrs[i].sh_link >= pi->ehdr->e_shnum)
811 return -ENOEXEC;
812
813 section = &sechdrs[sechdrs[i].sh_info];
814 symtab = &sechdrs[sechdrs[i].sh_link];
815
816 if (!(section->sh_flags & SHF_ALLOC))
817 continue;
818
819 /*
820 * symtab->sh_link contain section header index of associated
821 * string table.
822 */
823 if (symtab->sh_link >= pi->ehdr->e_shnum)
824 /* Invalid section number? */
825 continue;
826
827 /*
828 * Respective architecture needs to provide support for applying
829 * relocations of type SHT_RELA/SHT_REL.
830 */
831 if (sechdrs[i].sh_type == SHT_RELA)
832 ret = arch_kexec_apply_relocations_add(pi->ehdr,
833 sechdrs, i);
834 else if (sechdrs[i].sh_type == SHT_REL)
835 ret = arch_kexec_apply_relocations(pi->ehdr,
836 sechdrs, i);
837 if (ret)
838 return ret;
839 }
840
841 return 0;
842}
843
844/* Load relocatable purgatory object and relocate it appropriately */
845int kexec_load_purgatory(struct kimage *image, unsigned long min,
846 unsigned long max, int top_down,
847 unsigned long *load_addr)
848{
849 struct purgatory_info *pi = &image->purgatory_info;
850 int ret;
851
852 if (kexec_purgatory_size <= 0)
853 return -EINVAL;
854
855 if (kexec_purgatory_size < sizeof(Elf_Ehdr))
856 return -ENOEXEC;
857
858 pi->ehdr = (Elf_Ehdr *)kexec_purgatory;
859
860 if (memcmp(pi->ehdr->e_ident, ELFMAG, SELFMAG) != 0
861 || pi->ehdr->e_type != ET_REL
862 || !elf_check_arch(pi->ehdr)
863 || pi->ehdr->e_shentsize != sizeof(Elf_Shdr))
864 return -ENOEXEC;
865
866 if (pi->ehdr->e_shoff >= kexec_purgatory_size
867 || (pi->ehdr->e_shnum * sizeof(Elf_Shdr) >
868 kexec_purgatory_size - pi->ehdr->e_shoff))
869 return -ENOEXEC;
870
871 ret = __kexec_load_purgatory(image, min, max, top_down);
872 if (ret)
873 return ret;
874
875 ret = kexec_apply_relocations(image);
876 if (ret)
877 goto out;
878
879 *load_addr = pi->purgatory_load_addr;
880 return 0;
881out:
882 vfree(pi->sechdrs);
Thiago Jung Bauermann070c43e2016-09-01 16:14:44 -0700883 pi->sechdrs = NULL;
884
Dave Younga43cac02015-09-09 15:38:51 -0700885 vfree(pi->purgatory_buf);
Thiago Jung Bauermann070c43e2016-09-01 16:14:44 -0700886 pi->purgatory_buf = NULL;
Dave Younga43cac02015-09-09 15:38:51 -0700887 return ret;
888}
889
890static Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi,
891 const char *name)
892{
893 Elf_Sym *syms;
894 Elf_Shdr *sechdrs;
895 Elf_Ehdr *ehdr;
896 int i, k;
897 const char *strtab;
898
899 if (!pi->sechdrs || !pi->ehdr)
900 return NULL;
901
902 sechdrs = pi->sechdrs;
903 ehdr = pi->ehdr;
904
905 for (i = 0; i < ehdr->e_shnum; i++) {
906 if (sechdrs[i].sh_type != SHT_SYMTAB)
907 continue;
908
909 if (sechdrs[i].sh_link >= ehdr->e_shnum)
910 /* Invalid strtab section number */
911 continue;
912 strtab = (char *)sechdrs[sechdrs[i].sh_link].sh_offset;
913 syms = (Elf_Sym *)sechdrs[i].sh_offset;
914
915 /* Go through symbols for a match */
916 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) {
917 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL)
918 continue;
919
920 if (strcmp(strtab + syms[k].st_name, name) != 0)
921 continue;
922
923 if (syms[k].st_shndx == SHN_UNDEF ||
924 syms[k].st_shndx >= ehdr->e_shnum) {
925 pr_debug("Symbol: %s has bad section index %d.\n",
926 name, syms[k].st_shndx);
927 return NULL;
928 }
929
930 /* Found the symbol we are looking for */
931 return &syms[k];
932 }
933 }
934
935 return NULL;
936}
937
938void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name)
939{
940 struct purgatory_info *pi = &image->purgatory_info;
941 Elf_Sym *sym;
942 Elf_Shdr *sechdr;
943
944 sym = kexec_purgatory_find_symbol(pi, name);
945 if (!sym)
946 return ERR_PTR(-EINVAL);
947
948 sechdr = &pi->sechdrs[sym->st_shndx];
949
950 /*
951 * Returns the address where symbol will finally be loaded after
952 * kexec_load_segment()
953 */
954 return (void *)(sechdr->sh_addr + sym->st_value);
955}
956
957/*
958 * Get or set value of a symbol. If "get_value" is true, symbol value is
959 * returned in buf otherwise symbol value is set based on value in buf.
960 */
961int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name,
962 void *buf, unsigned int size, bool get_value)
963{
964 Elf_Sym *sym;
965 Elf_Shdr *sechdrs;
966 struct purgatory_info *pi = &image->purgatory_info;
967 char *sym_buf;
968
969 sym = kexec_purgatory_find_symbol(pi, name);
970 if (!sym)
971 return -EINVAL;
972
973 if (sym->st_size != size) {
974 pr_err("symbol %s size mismatch: expected %lu actual %u\n",
975 name, (unsigned long)sym->st_size, size);
976 return -EINVAL;
977 }
978
979 sechdrs = pi->sechdrs;
980
981 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
982 pr_err("symbol %s is in a bss section. Cannot %s\n", name,
983 get_value ? "get" : "set");
984 return -EINVAL;
985 }
986
987 sym_buf = (unsigned char *)sechdrs[sym->st_shndx].sh_offset +
988 sym->st_value;
989
990 if (get_value)
991 memcpy((void *)buf, sym_buf, size);
992 else
993 memcpy((void *)sym_buf, buf, size);
994
995 return 0;
996}