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