1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kexec: kexec_file_load system call 4 * 5 * Copyright (C) 2014 Red Hat Inc. 6 * Authors: 7 * Vivek Goyal <vgoyal@redhat.com> 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/capability.h> 13 #include <linux/mm.h> 14 #include <linux/file.h> 15 #include <linux/slab.h> 16 #include <linux/kexec.h> 17 #include <linux/memblock.h> 18 #include <linux/mutex.h> 19 #include <linux/list.h> 20 #include <linux/fs.h> 21 #include <linux/ima.h> 22 #include <crypto/hash.h> 23 #include <crypto/sha.h> 24 #include <linux/elf.h> 25 #include <linux/elfcore.h> 26 #include <linux/kernel.h> 27 #include <linux/syscalls.h> 28 #include <linux/vmalloc.h> 29 #include "kexec_internal.h" 30 31 static int kexec_calculate_store_digests(struct kimage *image); 32 33 /* 34 * Currently this is the only default function that is exported as some 35 * architectures need it to do additional handlings. 36 * In the future, other default functions may be exported too if required. 37 */ 38 int kexec_image_probe_default(struct kimage *image, void *buf, 39 unsigned long buf_len) 40 { 41 const struct kexec_file_ops * const *fops; 42 int ret = -ENOEXEC; 43 44 for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) { 45 ret = (*fops)->probe(buf, buf_len); 46 if (!ret) { 47 image->fops = *fops; 48 return ret; 49 } 50 } 51 52 return ret; 53 } 54 55 /* Architectures can provide this probe function */ 56 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 57 unsigned long buf_len) 58 { 59 return kexec_image_probe_default(image, buf, buf_len); 60 } 61 62 static void *kexec_image_load_default(struct kimage *image) 63 { 64 if (!image->fops || !image->fops->load) 65 return ERR_PTR(-ENOEXEC); 66 67 return image->fops->load(image, image->kernel_buf, 68 image->kernel_buf_len, image->initrd_buf, 69 image->initrd_buf_len, image->cmdline_buf, 70 image->cmdline_buf_len); 71 } 72 73 void * __weak arch_kexec_kernel_image_load(struct kimage *image) 74 { 75 return kexec_image_load_default(image); 76 } 77 78 int kexec_image_post_load_cleanup_default(struct kimage *image) 79 { 80 if (!image->fops || !image->fops->cleanup) 81 return 0; 82 83 return image->fops->cleanup(image->image_loader_data); 84 } 85 86 int __weak arch_kimage_file_post_load_cleanup(struct kimage *image) 87 { 88 return kexec_image_post_load_cleanup_default(image); 89 } 90 91 #ifdef CONFIG_KEXEC_SIG 92 static int kexec_image_verify_sig_default(struct kimage *image, void *buf, 93 unsigned long buf_len) 94 { 95 if (!image->fops || !image->fops->verify_sig) { 96 pr_debug("kernel loader does not support signature verification.\n"); 97 return -EKEYREJECTED; 98 } 99 100 return image->fops->verify_sig(buf, buf_len); 101 } 102 103 int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf, 104 unsigned long buf_len) 105 { 106 return kexec_image_verify_sig_default(image, buf, buf_len); 107 } 108 #endif 109 110 /* 111 * arch_kexec_apply_relocations_add - apply relocations of type RELA 112 * @pi: Purgatory to be relocated. 113 * @section: Section relocations applying to. 114 * @relsec: Section containing RELAs. 115 * @symtab: Corresponding symtab. 116 * 117 * Return: 0 on success, negative errno on error. 118 */ 119 int __weak 120 arch_kexec_apply_relocations_add(struct purgatory_info *pi, Elf_Shdr *section, 121 const Elf_Shdr *relsec, const Elf_Shdr *symtab) 122 { 123 pr_err("RELA relocation unsupported.\n"); 124 return -ENOEXEC; 125 } 126 127 /* 128 * arch_kexec_apply_relocations - apply relocations of type REL 129 * @pi: Purgatory to be relocated. 130 * @section: Section relocations applying to. 131 * @relsec: Section containing RELs. 132 * @symtab: Corresponding symtab. 133 * 134 * Return: 0 on success, negative errno on error. 135 */ 136 int __weak 137 arch_kexec_apply_relocations(struct purgatory_info *pi, Elf_Shdr *section, 138 const Elf_Shdr *relsec, const Elf_Shdr *symtab) 139 { 140 pr_err("REL relocation unsupported.\n"); 141 return -ENOEXEC; 142 } 143 144 /* 145 * Free up memory used by kernel, initrd, and command line. This is temporary 146 * memory allocation which is not needed any more after these buffers have 147 * been loaded into separate segments and have been copied elsewhere. 148 */ 149 void kimage_file_post_load_cleanup(struct kimage *image) 150 { 151 struct purgatory_info *pi = &image->purgatory_info; 152 153 vfree(image->kernel_buf); 154 image->kernel_buf = NULL; 155 156 vfree(image->initrd_buf); 157 image->initrd_buf = NULL; 158 159 kfree(image->cmdline_buf); 160 image->cmdline_buf = NULL; 161 162 vfree(pi->purgatory_buf); 163 pi->purgatory_buf = NULL; 164 165 vfree(pi->sechdrs); 166 pi->sechdrs = NULL; 167 168 /* See if architecture has anything to cleanup post load */ 169 arch_kimage_file_post_load_cleanup(image); 170 171 /* 172 * Above call should have called into bootloader to free up 173 * any data stored in kimage->image_loader_data. It should 174 * be ok now to free it up. 175 */ 176 kfree(image->image_loader_data); 177 image->image_loader_data = NULL; 178 } 179 180 #ifdef CONFIG_KEXEC_SIG 181 static int 182 kimage_validate_signature(struct kimage *image) 183 { 184 const char *reason; 185 int ret; 186 187 ret = arch_kexec_kernel_verify_sig(image, image->kernel_buf, 188 image->kernel_buf_len); 189 switch (ret) { 190 case 0: 191 break; 192 193 /* Certain verification errors are non-fatal if we're not 194 * checking errors, provided we aren't mandating that there 195 * must be a valid signature. 196 */ 197 case -ENODATA: 198 reason = "kexec of unsigned image"; 199 goto decide; 200 case -ENOPKG: 201 reason = "kexec of image with unsupported crypto"; 202 goto decide; 203 case -ENOKEY: 204 reason = "kexec of image with unavailable key"; 205 decide: 206 if (IS_ENABLED(CONFIG_KEXEC_SIG_FORCE)) { 207 pr_notice("%s rejected\n", reason); 208 return ret; 209 } 210 211 /* If IMA is guaranteed to appraise a signature on the kexec 212 * image, permit it even if the kernel is otherwise locked 213 * down. 214 */ 215 if (!ima_appraise_signature(READING_KEXEC_IMAGE) && 216 security_locked_down(LOCKDOWN_KEXEC)) 217 return -EPERM; 218 219 return 0; 220 221 /* All other errors are fatal, including nomem, unparseable 222 * signatures and signature check failures - even if signatures 223 * aren't required. 224 */ 225 default: 226 pr_notice("kernel signature verification failed (%d).\n", ret); 227 } 228 229 return ret; 230 } 231 #endif 232 233 /* 234 * In file mode list of segments is prepared by kernel. Copy relevant 235 * data from user space, do error checking, prepare segment list 236 */ 237 static int 238 kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, 239 const char __user *cmdline_ptr, 240 unsigned long cmdline_len, unsigned flags) 241 { 242 int ret; 243 void *ldata; 244 loff_t size; 245 246 ret = kernel_read_file_from_fd(kernel_fd, &image->kernel_buf, 247 &size, INT_MAX, READING_KEXEC_IMAGE); 248 if (ret) 249 return ret; 250 image->kernel_buf_len = size; 251 252 /* IMA needs to pass the measurement list to the next kernel. */ 253 ima_add_kexec_buffer(image); 254 255 /* Call arch image probe handlers */ 256 ret = arch_kexec_kernel_image_probe(image, image->kernel_buf, 257 image->kernel_buf_len); 258 if (ret) 259 goto out; 260 261 #ifdef CONFIG_KEXEC_SIG 262 ret = kimage_validate_signature(image); 263 264 if (ret) 265 goto out; 266 #endif 267 /* It is possible that there no initramfs is being loaded */ 268 if (!(flags & KEXEC_FILE_NO_INITRAMFS)) { 269 ret = kernel_read_file_from_fd(initrd_fd, &image->initrd_buf, 270 &size, INT_MAX, 271 READING_KEXEC_INITRAMFS); 272 if (ret) 273 goto out; 274 image->initrd_buf_len = size; 275 } 276 277 if (cmdline_len) { 278 image->cmdline_buf = memdup_user(cmdline_ptr, cmdline_len); 279 if (IS_ERR(image->cmdline_buf)) { 280 ret = PTR_ERR(image->cmdline_buf); 281 image->cmdline_buf = NULL; 282 goto out; 283 } 284 285 image->cmdline_buf_len = cmdline_len; 286 287 /* command line should be a string with last byte null */ 288 if (image->cmdline_buf[cmdline_len - 1] != '\0') { 289 ret = -EINVAL; 290 goto out; 291 } 292 } 293 294 /* Call arch image load handlers */ 295 ldata = arch_kexec_kernel_image_load(image); 296 297 if (IS_ERR(ldata)) { 298 ret = PTR_ERR(ldata); 299 goto out; 300 } 301 302 image->image_loader_data = ldata; 303 out: 304 /* In case of error, free up all allocated memory in this function */ 305 if (ret) 306 kimage_file_post_load_cleanup(image); 307 return ret; 308 } 309 310 static int 311 kimage_file_alloc_init(struct kimage **rimage, int kernel_fd, 312 int initrd_fd, const char __user *cmdline_ptr, 313 unsigned long cmdline_len, unsigned long flags) 314 { 315 int ret; 316 struct kimage *image; 317 bool kexec_on_panic = flags & KEXEC_FILE_ON_CRASH; 318 319 image = do_kimage_alloc_init(); 320 if (!image) 321 return -ENOMEM; 322 323 image->file_mode = 1; 324 325 if (kexec_on_panic) { 326 /* Enable special crash kernel control page alloc policy. */ 327 image->control_page = crashk_res.start; 328 image->type = KEXEC_TYPE_CRASH; 329 } 330 331 ret = kimage_file_prepare_segments(image, kernel_fd, initrd_fd, 332 cmdline_ptr, cmdline_len, flags); 333 if (ret) 334 goto out_free_image; 335 336 ret = sanity_check_segment_list(image); 337 if (ret) 338 goto out_free_post_load_bufs; 339 340 ret = -ENOMEM; 341 image->control_code_page = kimage_alloc_control_pages(image, 342 get_order(KEXEC_CONTROL_PAGE_SIZE)); 343 if (!image->control_code_page) { 344 pr_err("Could not allocate control_code_buffer\n"); 345 goto out_free_post_load_bufs; 346 } 347 348 if (!kexec_on_panic) { 349 image->swap_page = kimage_alloc_control_pages(image, 0); 350 if (!image->swap_page) { 351 pr_err("Could not allocate swap buffer\n"); 352 goto out_free_control_pages; 353 } 354 } 355 356 *rimage = image; 357 return 0; 358 out_free_control_pages: 359 kimage_free_page_list(&image->control_pages); 360 out_free_post_load_bufs: 361 kimage_file_post_load_cleanup(image); 362 out_free_image: 363 kfree(image); 364 return ret; 365 } 366 367 SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd, 368 unsigned long, cmdline_len, const char __user *, cmdline_ptr, 369 unsigned long, flags) 370 { 371 int ret = 0, i; 372 struct kimage **dest_image, *image; 373 374 /* We only trust the superuser with rebooting the system. */ 375 if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) 376 return -EPERM; 377 378 /* Make sure we have a legal set of flags */ 379 if (flags != (flags & KEXEC_FILE_FLAGS)) 380 return -EINVAL; 381 382 image = NULL; 383 384 if (!mutex_trylock(&kexec_mutex)) 385 return -EBUSY; 386 387 dest_image = &kexec_image; 388 if (flags & KEXEC_FILE_ON_CRASH) { 389 dest_image = &kexec_crash_image; 390 if (kexec_crash_image) 391 arch_kexec_unprotect_crashkres(); 392 } 393 394 if (flags & KEXEC_FILE_UNLOAD) 395 goto exchange; 396 397 /* 398 * In case of crash, new kernel gets loaded in reserved region. It is 399 * same memory where old crash kernel might be loaded. Free any 400 * current crash dump kernel before we corrupt it. 401 */ 402 if (flags & KEXEC_FILE_ON_CRASH) 403 kimage_free(xchg(&kexec_crash_image, NULL)); 404 405 ret = kimage_file_alloc_init(&image, kernel_fd, initrd_fd, cmdline_ptr, 406 cmdline_len, flags); 407 if (ret) 408 goto out; 409 410 ret = machine_kexec_prepare(image); 411 if (ret) 412 goto out; 413 414 /* 415 * Some architecture(like S390) may touch the crash memory before 416 * machine_kexec_prepare(), we must copy vmcoreinfo data after it. 417 */ 418 ret = kimage_crash_copy_vmcoreinfo(image); 419 if (ret) 420 goto out; 421 422 ret = kexec_calculate_store_digests(image); 423 if (ret) 424 goto out; 425 426 for (i = 0; i < image->nr_segments; i++) { 427 struct kexec_segment *ksegment; 428 429 ksegment = &image->segment[i]; 430 pr_debug("Loading segment %d: buf=0x%p bufsz=0x%zx mem=0x%lx memsz=0x%zx\n", 431 i, ksegment->buf, ksegment->bufsz, ksegment->mem, 432 ksegment->memsz); 433 434 ret = kimage_load_segment(image, &image->segment[i]); 435 if (ret) 436 goto out; 437 } 438 439 kimage_terminate(image); 440 441 /* 442 * Free up any temporary buffers allocated which are not needed 443 * after image has been loaded 444 */ 445 kimage_file_post_load_cleanup(image); 446 exchange: 447 image = xchg(dest_image, image); 448 out: 449 if ((flags & KEXEC_FILE_ON_CRASH) && kexec_crash_image) 450 arch_kexec_protect_crashkres(); 451 452 mutex_unlock(&kexec_mutex); 453 kimage_free(image); 454 return ret; 455 } 456 457 static int locate_mem_hole_top_down(unsigned long start, unsigned long end, 458 struct kexec_buf *kbuf) 459 { 460 struct kimage *image = kbuf->image; 461 unsigned long temp_start, temp_end; 462 463 temp_end = min(end, kbuf->buf_max); 464 temp_start = temp_end - kbuf->memsz; 465 466 do { 467 /* align down start */ 468 temp_start = temp_start & (~(kbuf->buf_align - 1)); 469 470 if (temp_start < start || temp_start < kbuf->buf_min) 471 return 0; 472 473 temp_end = temp_start + kbuf->memsz - 1; 474 475 /* 476 * Make sure this does not conflict with any of existing 477 * segments 478 */ 479 if (kimage_is_destination_range(image, temp_start, temp_end)) { 480 temp_start = temp_start - PAGE_SIZE; 481 continue; 482 } 483 484 /* We found a suitable memory range */ 485 break; 486 } while (1); 487 488 /* If we are here, we found a suitable memory range */ 489 kbuf->mem = temp_start; 490 491 /* Success, stop navigating through remaining System RAM ranges */ 492 return 1; 493 } 494 495 static int locate_mem_hole_bottom_up(unsigned long start, unsigned long end, 496 struct kexec_buf *kbuf) 497 { 498 struct kimage *image = kbuf->image; 499 unsigned long temp_start, temp_end; 500 501 temp_start = max(start, kbuf->buf_min); 502 503 do { 504 temp_start = ALIGN(temp_start, kbuf->buf_align); 505 temp_end = temp_start + kbuf->memsz - 1; 506 507 if (temp_end > end || temp_end > kbuf->buf_max) 508 return 0; 509 /* 510 * Make sure this does not conflict with any of existing 511 * segments 512 */ 513 if (kimage_is_destination_range(image, temp_start, temp_end)) { 514 temp_start = temp_start + PAGE_SIZE; 515 continue; 516 } 517 518 /* We found a suitable memory range */ 519 break; 520 } while (1); 521 522 /* If we are here, we found a suitable memory range */ 523 kbuf->mem = temp_start; 524 525 /* Success, stop navigating through remaining System RAM ranges */ 526 return 1; 527 } 528 529 static int locate_mem_hole_callback(struct resource *res, void *arg) 530 { 531 struct kexec_buf *kbuf = (struct kexec_buf *)arg; 532 u64 start = res->start, end = res->end; 533 unsigned long sz = end - start + 1; 534 535 /* Returning 0 will take to next memory range */ 536 if (sz < kbuf->memsz) 537 return 0; 538 539 if (end < kbuf->buf_min || start > kbuf->buf_max) 540 return 0; 541 542 /* 543 * Allocate memory top down with-in ram range. Otherwise bottom up 544 * allocation. 545 */ 546 if (kbuf->top_down) 547 return locate_mem_hole_top_down(start, end, kbuf); 548 return locate_mem_hole_bottom_up(start, end, kbuf); 549 } 550 551 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK 552 static int kexec_walk_memblock(struct kexec_buf *kbuf, 553 int (*func)(struct resource *, void *)) 554 { 555 int ret = 0; 556 u64 i; 557 phys_addr_t mstart, mend; 558 struct resource res = { }; 559 560 if (kbuf->image->type == KEXEC_TYPE_CRASH) 561 return func(&crashk_res, kbuf); 562 563 if (kbuf->top_down) { 564 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, MEMBLOCK_NONE, 565 &mstart, &mend, NULL) { 566 /* 567 * In memblock, end points to the first byte after the 568 * range while in kexec, end points to the last byte 569 * in the range. 570 */ 571 res.start = mstart; 572 res.end = mend - 1; 573 ret = func(&res, kbuf); 574 if (ret) 575 break; 576 } 577 } else { 578 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, 579 &mstart, &mend, NULL) { 580 /* 581 * In memblock, end points to the first byte after the 582 * range while in kexec, end points to the last byte 583 * in the range. 584 */ 585 res.start = mstart; 586 res.end = mend - 1; 587 ret = func(&res, kbuf); 588 if (ret) 589 break; 590 } 591 } 592 593 return ret; 594 } 595 #else 596 static int kexec_walk_memblock(struct kexec_buf *kbuf, 597 int (*func)(struct resource *, void *)) 598 { 599 return 0; 600 } 601 #endif 602 603 /** 604 * kexec_walk_resources - call func(data) on free memory regions 605 * @kbuf: Context info for the search. Also passed to @func. 606 * @func: Function to call for each memory region. 607 * 608 * Return: The memory walk will stop when func returns a non-zero value 609 * and that value will be returned. If all free regions are visited without 610 * func returning non-zero, then zero will be returned. 611 */ 612 static int kexec_walk_resources(struct kexec_buf *kbuf, 613 int (*func)(struct resource *, void *)) 614 { 615 if (kbuf->image->type == KEXEC_TYPE_CRASH) 616 return walk_iomem_res_desc(crashk_res.desc, 617 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY, 618 crashk_res.start, crashk_res.end, 619 kbuf, func); 620 else 621 return walk_system_ram_res(0, ULONG_MAX, kbuf, func); 622 } 623 624 /** 625 * kexec_locate_mem_hole - find free memory for the purgatory or the next kernel 626 * @kbuf: Parameters for the memory search. 627 * 628 * On success, kbuf->mem will have the start address of the memory region found. 629 * 630 * Return: 0 on success, negative errno on error. 631 */ 632 int kexec_locate_mem_hole(struct kexec_buf *kbuf) 633 { 634 int ret; 635 636 /* Arch knows where to place */ 637 if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN) 638 return 0; 639 640 if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) 641 ret = kexec_walk_resources(kbuf, locate_mem_hole_callback); 642 else 643 ret = kexec_walk_memblock(kbuf, locate_mem_hole_callback); 644 645 return ret == 1 ? 0 : -EADDRNOTAVAIL; 646 } 647 648 /** 649 * kexec_add_buffer - place a buffer in a kexec segment 650 * @kbuf: Buffer contents and memory parameters. 651 * 652 * This function assumes that kexec_mutex is held. 653 * On successful return, @kbuf->mem will have the physical address of 654 * the buffer in memory. 655 * 656 * Return: 0 on success, negative errno on error. 657 */ 658 int kexec_add_buffer(struct kexec_buf *kbuf) 659 { 660 661 struct kexec_segment *ksegment; 662 int ret; 663 664 /* Currently adding segment this way is allowed only in file mode */ 665 if (!kbuf->image->file_mode) 666 return -EINVAL; 667 668 if (kbuf->image->nr_segments >= KEXEC_SEGMENT_MAX) 669 return -EINVAL; 670 671 /* 672 * Make sure we are not trying to add buffer after allocating 673 * control pages. All segments need to be placed first before 674 * any control pages are allocated. As control page allocation 675 * logic goes through list of segments to make sure there are 676 * no destination overlaps. 677 */ 678 if (!list_empty(&kbuf->image->control_pages)) { 679 WARN_ON(1); 680 return -EINVAL; 681 } 682 683 /* Ensure minimum alignment needed for segments. */ 684 kbuf->memsz = ALIGN(kbuf->memsz, PAGE_SIZE); 685 kbuf->buf_align = max(kbuf->buf_align, PAGE_SIZE); 686 687 /* Walk the RAM ranges and allocate a suitable range for the buffer */ 688 ret = kexec_locate_mem_hole(kbuf); 689 if (ret) 690 return ret; 691 692 /* Found a suitable memory range */ 693 ksegment = &kbuf->image->segment[kbuf->image->nr_segments]; 694 ksegment->kbuf = kbuf->buffer; 695 ksegment->bufsz = kbuf->bufsz; 696 ksegment->mem = kbuf->mem; 697 ksegment->memsz = kbuf->memsz; 698 kbuf->image->nr_segments++; 699 return 0; 700 } 701 702 /* Calculate and store the digest of segments */ 703 static int kexec_calculate_store_digests(struct kimage *image) 704 { 705 struct crypto_shash *tfm; 706 struct shash_desc *desc; 707 int ret = 0, i, j, zero_buf_sz, sha_region_sz; 708 size_t desc_size, nullsz; 709 char *digest; 710 void *zero_buf; 711 struct kexec_sha_region *sha_regions; 712 struct purgatory_info *pi = &image->purgatory_info; 713 714 if (!IS_ENABLED(CONFIG_ARCH_HAS_KEXEC_PURGATORY)) 715 return 0; 716 717 zero_buf = __va(page_to_pfn(ZERO_PAGE(0)) << PAGE_SHIFT); 718 zero_buf_sz = PAGE_SIZE; 719 720 tfm = crypto_alloc_shash("sha256", 0, 0); 721 if (IS_ERR(tfm)) { 722 ret = PTR_ERR(tfm); 723 goto out; 724 } 725 726 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); 727 desc = kzalloc(desc_size, GFP_KERNEL); 728 if (!desc) { 729 ret = -ENOMEM; 730 goto out_free_tfm; 731 } 732 733 sha_region_sz = KEXEC_SEGMENT_MAX * sizeof(struct kexec_sha_region); 734 sha_regions = vzalloc(sha_region_sz); 735 if (!sha_regions) 736 goto out_free_desc; 737 738 desc->tfm = tfm; 739 740 ret = crypto_shash_init(desc); 741 if (ret < 0) 742 goto out_free_sha_regions; 743 744 digest = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL); 745 if (!digest) { 746 ret = -ENOMEM; 747 goto out_free_sha_regions; 748 } 749 750 for (j = i = 0; i < image->nr_segments; i++) { 751 struct kexec_segment *ksegment; 752 753 ksegment = &image->segment[i]; 754 /* 755 * Skip purgatory as it will be modified once we put digest 756 * info in purgatory. 757 */ 758 if (ksegment->kbuf == pi->purgatory_buf) 759 continue; 760 761 ret = crypto_shash_update(desc, ksegment->kbuf, 762 ksegment->bufsz); 763 if (ret) 764 break; 765 766 /* 767 * Assume rest of the buffer is filled with zero and 768 * update digest accordingly. 769 */ 770 nullsz = ksegment->memsz - ksegment->bufsz; 771 while (nullsz) { 772 unsigned long bytes = nullsz; 773 774 if (bytes > zero_buf_sz) 775 bytes = zero_buf_sz; 776 ret = crypto_shash_update(desc, zero_buf, bytes); 777 if (ret) 778 break; 779 nullsz -= bytes; 780 } 781 782 if (ret) 783 break; 784 785 sha_regions[j].start = ksegment->mem; 786 sha_regions[j].len = ksegment->memsz; 787 j++; 788 } 789 790 if (!ret) { 791 ret = crypto_shash_final(desc, digest); 792 if (ret) 793 goto out_free_digest; 794 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions", 795 sha_regions, sha_region_sz, 0); 796 if (ret) 797 goto out_free_digest; 798 799 ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest", 800 digest, SHA256_DIGEST_SIZE, 0); 801 if (ret) 802 goto out_free_digest; 803 } 804 805 out_free_digest: 806 kfree(digest); 807 out_free_sha_regions: 808 vfree(sha_regions); 809 out_free_desc: 810 kfree(desc); 811 out_free_tfm: 812 kfree(tfm); 813 out: 814 return ret; 815 } 816 817 #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY 818 /* 819 * kexec_purgatory_setup_kbuf - prepare buffer to load purgatory. 820 * @pi: Purgatory to be loaded. 821 * @kbuf: Buffer to setup. 822 * 823 * Allocates the memory needed for the buffer. Caller is responsible to free 824 * the memory after use. 825 * 826 * Return: 0 on success, negative errno on error. 827 */ 828 static int kexec_purgatory_setup_kbuf(struct purgatory_info *pi, 829 struct kexec_buf *kbuf) 830 { 831 const Elf_Shdr *sechdrs; 832 unsigned long bss_align; 833 unsigned long bss_sz; 834 unsigned long align; 835 int i, ret; 836 837 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff; 838 kbuf->buf_align = bss_align = 1; 839 kbuf->bufsz = bss_sz = 0; 840 841 for (i = 0; i < pi->ehdr->e_shnum; i++) { 842 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 843 continue; 844 845 align = sechdrs[i].sh_addralign; 846 if (sechdrs[i].sh_type != SHT_NOBITS) { 847 if (kbuf->buf_align < align) 848 kbuf->buf_align = align; 849 kbuf->bufsz = ALIGN(kbuf->bufsz, align); 850 kbuf->bufsz += sechdrs[i].sh_size; 851 } else { 852 if (bss_align < align) 853 bss_align = align; 854 bss_sz = ALIGN(bss_sz, align); 855 bss_sz += sechdrs[i].sh_size; 856 } 857 } 858 kbuf->bufsz = ALIGN(kbuf->bufsz, bss_align); 859 kbuf->memsz = kbuf->bufsz + bss_sz; 860 if (kbuf->buf_align < bss_align) 861 kbuf->buf_align = bss_align; 862 863 kbuf->buffer = vzalloc(kbuf->bufsz); 864 if (!kbuf->buffer) 865 return -ENOMEM; 866 pi->purgatory_buf = kbuf->buffer; 867 868 ret = kexec_add_buffer(kbuf); 869 if (ret) 870 goto out; 871 872 return 0; 873 out: 874 vfree(pi->purgatory_buf); 875 pi->purgatory_buf = NULL; 876 return ret; 877 } 878 879 /* 880 * kexec_purgatory_setup_sechdrs - prepares the pi->sechdrs buffer. 881 * @pi: Purgatory to be loaded. 882 * @kbuf: Buffer prepared to store purgatory. 883 * 884 * Allocates the memory needed for the buffer. Caller is responsible to free 885 * the memory after use. 886 * 887 * Return: 0 on success, negative errno on error. 888 */ 889 static int kexec_purgatory_setup_sechdrs(struct purgatory_info *pi, 890 struct kexec_buf *kbuf) 891 { 892 unsigned long bss_addr; 893 unsigned long offset; 894 Elf_Shdr *sechdrs; 895 int i; 896 897 /* 898 * The section headers in kexec_purgatory are read-only. In order to 899 * have them modifiable make a temporary copy. 900 */ 901 sechdrs = vzalloc(array_size(sizeof(Elf_Shdr), pi->ehdr->e_shnum)); 902 if (!sechdrs) 903 return -ENOMEM; 904 memcpy(sechdrs, (void *)pi->ehdr + pi->ehdr->e_shoff, 905 pi->ehdr->e_shnum * sizeof(Elf_Shdr)); 906 pi->sechdrs = sechdrs; 907 908 offset = 0; 909 bss_addr = kbuf->mem + kbuf->bufsz; 910 kbuf->image->start = pi->ehdr->e_entry; 911 912 for (i = 0; i < pi->ehdr->e_shnum; i++) { 913 unsigned long align; 914 void *src, *dst; 915 916 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 917 continue; 918 919 align = sechdrs[i].sh_addralign; 920 if (sechdrs[i].sh_type == SHT_NOBITS) { 921 bss_addr = ALIGN(bss_addr, align); 922 sechdrs[i].sh_addr = bss_addr; 923 bss_addr += sechdrs[i].sh_size; 924 continue; 925 } 926 927 offset = ALIGN(offset, align); 928 if (sechdrs[i].sh_flags & SHF_EXECINSTR && 929 pi->ehdr->e_entry >= sechdrs[i].sh_addr && 930 pi->ehdr->e_entry < (sechdrs[i].sh_addr 931 + sechdrs[i].sh_size)) { 932 kbuf->image->start -= sechdrs[i].sh_addr; 933 kbuf->image->start += kbuf->mem + offset; 934 } 935 936 src = (void *)pi->ehdr + sechdrs[i].sh_offset; 937 dst = pi->purgatory_buf + offset; 938 memcpy(dst, src, sechdrs[i].sh_size); 939 940 sechdrs[i].sh_addr = kbuf->mem + offset; 941 sechdrs[i].sh_offset = offset; 942 offset += sechdrs[i].sh_size; 943 } 944 945 return 0; 946 } 947 948 static int kexec_apply_relocations(struct kimage *image) 949 { 950 int i, ret; 951 struct purgatory_info *pi = &image->purgatory_info; 952 const Elf_Shdr *sechdrs; 953 954 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff; 955 956 for (i = 0; i < pi->ehdr->e_shnum; i++) { 957 const Elf_Shdr *relsec; 958 const Elf_Shdr *symtab; 959 Elf_Shdr *section; 960 961 relsec = sechdrs + i; 962 963 if (relsec->sh_type != SHT_RELA && 964 relsec->sh_type != SHT_REL) 965 continue; 966 967 /* 968 * For section of type SHT_RELA/SHT_REL, 969 * ->sh_link contains section header index of associated 970 * symbol table. And ->sh_info contains section header 971 * index of section to which relocations apply. 972 */ 973 if (relsec->sh_info >= pi->ehdr->e_shnum || 974 relsec->sh_link >= pi->ehdr->e_shnum) 975 return -ENOEXEC; 976 977 section = pi->sechdrs + relsec->sh_info; 978 symtab = sechdrs + relsec->sh_link; 979 980 if (!(section->sh_flags & SHF_ALLOC)) 981 continue; 982 983 /* 984 * symtab->sh_link contain section header index of associated 985 * string table. 986 */ 987 if (symtab->sh_link >= pi->ehdr->e_shnum) 988 /* Invalid section number? */ 989 continue; 990 991 /* 992 * Respective architecture needs to provide support for applying 993 * relocations of type SHT_RELA/SHT_REL. 994 */ 995 if (relsec->sh_type == SHT_RELA) 996 ret = arch_kexec_apply_relocations_add(pi, section, 997 relsec, symtab); 998 else if (relsec->sh_type == SHT_REL) 999 ret = arch_kexec_apply_relocations(pi, section, 1000 relsec, symtab); 1001 if (ret) 1002 return ret; 1003 } 1004 1005 return 0; 1006 } 1007 1008 /* 1009 * kexec_load_purgatory - Load and relocate the purgatory object. 1010 * @image: Image to add the purgatory to. 1011 * @kbuf: Memory parameters to use. 1012 * 1013 * Allocates the memory needed for image->purgatory_info.sechdrs and 1014 * image->purgatory_info.purgatory_buf/kbuf->buffer. Caller is responsible 1015 * to free the memory after use. 1016 * 1017 * Return: 0 on success, negative errno on error. 1018 */ 1019 int kexec_load_purgatory(struct kimage *image, struct kexec_buf *kbuf) 1020 { 1021 struct purgatory_info *pi = &image->purgatory_info; 1022 int ret; 1023 1024 if (kexec_purgatory_size <= 0) 1025 return -EINVAL; 1026 1027 pi->ehdr = (const Elf_Ehdr *)kexec_purgatory; 1028 1029 ret = kexec_purgatory_setup_kbuf(pi, kbuf); 1030 if (ret) 1031 return ret; 1032 1033 ret = kexec_purgatory_setup_sechdrs(pi, kbuf); 1034 if (ret) 1035 goto out_free_kbuf; 1036 1037 ret = kexec_apply_relocations(image); 1038 if (ret) 1039 goto out; 1040 1041 return 0; 1042 out: 1043 vfree(pi->sechdrs); 1044 pi->sechdrs = NULL; 1045 out_free_kbuf: 1046 vfree(pi->purgatory_buf); 1047 pi->purgatory_buf = NULL; 1048 return ret; 1049 } 1050 1051 /* 1052 * kexec_purgatory_find_symbol - find a symbol in the purgatory 1053 * @pi: Purgatory to search in. 1054 * @name: Name of the symbol. 1055 * 1056 * Return: pointer to symbol in read-only symtab on success, NULL on error. 1057 */ 1058 static const Elf_Sym *kexec_purgatory_find_symbol(struct purgatory_info *pi, 1059 const char *name) 1060 { 1061 const Elf_Shdr *sechdrs; 1062 const Elf_Ehdr *ehdr; 1063 const Elf_Sym *syms; 1064 const char *strtab; 1065 int i, k; 1066 1067 if (!pi->ehdr) 1068 return NULL; 1069 1070 ehdr = pi->ehdr; 1071 sechdrs = (void *)ehdr + ehdr->e_shoff; 1072 1073 for (i = 0; i < ehdr->e_shnum; i++) { 1074 if (sechdrs[i].sh_type != SHT_SYMTAB) 1075 continue; 1076 1077 if (sechdrs[i].sh_link >= ehdr->e_shnum) 1078 /* Invalid strtab section number */ 1079 continue; 1080 strtab = (void *)ehdr + sechdrs[sechdrs[i].sh_link].sh_offset; 1081 syms = (void *)ehdr + sechdrs[i].sh_offset; 1082 1083 /* Go through symbols for a match */ 1084 for (k = 0; k < sechdrs[i].sh_size/sizeof(Elf_Sym); k++) { 1085 if (ELF_ST_BIND(syms[k].st_info) != STB_GLOBAL) 1086 continue; 1087 1088 if (strcmp(strtab + syms[k].st_name, name) != 0) 1089 continue; 1090 1091 if (syms[k].st_shndx == SHN_UNDEF || 1092 syms[k].st_shndx >= ehdr->e_shnum) { 1093 pr_debug("Symbol: %s has bad section index %d.\n", 1094 name, syms[k].st_shndx); 1095 return NULL; 1096 } 1097 1098 /* Found the symbol we are looking for */ 1099 return &syms[k]; 1100 } 1101 } 1102 1103 return NULL; 1104 } 1105 1106 void *kexec_purgatory_get_symbol_addr(struct kimage *image, const char *name) 1107 { 1108 struct purgatory_info *pi = &image->purgatory_info; 1109 const Elf_Sym *sym; 1110 Elf_Shdr *sechdr; 1111 1112 sym = kexec_purgatory_find_symbol(pi, name); 1113 if (!sym) 1114 return ERR_PTR(-EINVAL); 1115 1116 sechdr = &pi->sechdrs[sym->st_shndx]; 1117 1118 /* 1119 * Returns the address where symbol will finally be loaded after 1120 * kexec_load_segment() 1121 */ 1122 return (void *)(sechdr->sh_addr + sym->st_value); 1123 } 1124 1125 /* 1126 * Get or set value of a symbol. If "get_value" is true, symbol value is 1127 * returned in buf otherwise symbol value is set based on value in buf. 1128 */ 1129 int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name, 1130 void *buf, unsigned int size, bool get_value) 1131 { 1132 struct purgatory_info *pi = &image->purgatory_info; 1133 const Elf_Sym *sym; 1134 Elf_Shdr *sec; 1135 char *sym_buf; 1136 1137 sym = kexec_purgatory_find_symbol(pi, name); 1138 if (!sym) 1139 return -EINVAL; 1140 1141 if (sym->st_size != size) { 1142 pr_err("symbol %s size mismatch: expected %lu actual %u\n", 1143 name, (unsigned long)sym->st_size, size); 1144 return -EINVAL; 1145 } 1146 1147 sec = pi->sechdrs + sym->st_shndx; 1148 1149 if (sec->sh_type == SHT_NOBITS) { 1150 pr_err("symbol %s is in a bss section. Cannot %s\n", name, 1151 get_value ? "get" : "set"); 1152 return -EINVAL; 1153 } 1154 1155 sym_buf = (char *)pi->purgatory_buf + sec->sh_offset + sym->st_value; 1156 1157 if (get_value) 1158 memcpy((void *)buf, sym_buf, size); 1159 else 1160 memcpy((void *)sym_buf, buf, size); 1161 1162 return 0; 1163 } 1164 #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */ 1165 1166 int crash_exclude_mem_range(struct crash_mem *mem, 1167 unsigned long long mstart, unsigned long long mend) 1168 { 1169 int i, j; 1170 unsigned long long start, end; 1171 struct crash_mem_range temp_range = {0, 0}; 1172 1173 for (i = 0; i < mem->nr_ranges; i++) { 1174 start = mem->ranges[i].start; 1175 end = mem->ranges[i].end; 1176 1177 if (mstart > end || mend < start) 1178 continue; 1179 1180 /* Truncate any area outside of range */ 1181 if (mstart < start) 1182 mstart = start; 1183 if (mend > end) 1184 mend = end; 1185 1186 /* Found completely overlapping range */ 1187 if (mstart == start && mend == end) { 1188 mem->ranges[i].start = 0; 1189 mem->ranges[i].end = 0; 1190 if (i < mem->nr_ranges - 1) { 1191 /* Shift rest of the ranges to left */ 1192 for (j = i; j < mem->nr_ranges - 1; j++) { 1193 mem->ranges[j].start = 1194 mem->ranges[j+1].start; 1195 mem->ranges[j].end = 1196 mem->ranges[j+1].end; 1197 } 1198 } 1199 mem->nr_ranges--; 1200 return 0; 1201 } 1202 1203 if (mstart > start && mend < end) { 1204 /* Split original range */ 1205 mem->ranges[i].end = mstart - 1; 1206 temp_range.start = mend + 1; 1207 temp_range.end = end; 1208 } else if (mstart != start) 1209 mem->ranges[i].end = mstart - 1; 1210 else 1211 mem->ranges[i].start = mend + 1; 1212 break; 1213 } 1214 1215 /* If a split happened, add the split to array */ 1216 if (!temp_range.end) 1217 return 0; 1218 1219 /* Split happened */ 1220 if (i == mem->max_nr_ranges - 1) 1221 return -ENOMEM; 1222 1223 /* Location where new range should go */ 1224 j = i + 1; 1225 if (j < mem->nr_ranges) { 1226 /* Move over all ranges one slot towards the end */ 1227 for (i = mem->nr_ranges - 1; i >= j; i--) 1228 mem->ranges[i + 1] = mem->ranges[i]; 1229 } 1230 1231 mem->ranges[j].start = temp_range.start; 1232 mem->ranges[j].end = temp_range.end; 1233 mem->nr_ranges++; 1234 return 0; 1235 } 1236 1237 int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map, 1238 void **addr, unsigned long *sz) 1239 { 1240 Elf64_Ehdr *ehdr; 1241 Elf64_Phdr *phdr; 1242 unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; 1243 unsigned char *buf; 1244 unsigned int cpu, i; 1245 unsigned long long notes_addr; 1246 unsigned long mstart, mend; 1247 1248 /* extra phdr for vmcoreinfo elf note */ 1249 nr_phdr = nr_cpus + 1; 1250 nr_phdr += mem->nr_ranges; 1251 1252 /* 1253 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping 1254 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64). 1255 * I think this is required by tools like gdb. So same physical 1256 * memory will be mapped in two elf headers. One will contain kernel 1257 * text virtual addresses and other will have __va(physical) addresses. 1258 */ 1259 1260 nr_phdr++; 1261 elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); 1262 elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); 1263 1264 buf = vzalloc(elf_sz); 1265 if (!buf) 1266 return -ENOMEM; 1267 1268 ehdr = (Elf64_Ehdr *)buf; 1269 phdr = (Elf64_Phdr *)(ehdr + 1); 1270 memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 1271 ehdr->e_ident[EI_CLASS] = ELFCLASS64; 1272 ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 1273 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 1274 ehdr->e_ident[EI_OSABI] = ELF_OSABI; 1275 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 1276 ehdr->e_type = ET_CORE; 1277 ehdr->e_machine = ELF_ARCH; 1278 ehdr->e_version = EV_CURRENT; 1279 ehdr->e_phoff = sizeof(Elf64_Ehdr); 1280 ehdr->e_ehsize = sizeof(Elf64_Ehdr); 1281 ehdr->e_phentsize = sizeof(Elf64_Phdr); 1282 1283 /* Prepare one phdr of type PT_NOTE for each present cpu */ 1284 for_each_present_cpu(cpu) { 1285 phdr->p_type = PT_NOTE; 1286 notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); 1287 phdr->p_offset = phdr->p_paddr = notes_addr; 1288 phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); 1289 (ehdr->e_phnum)++; 1290 phdr++; 1291 } 1292 1293 /* Prepare one PT_NOTE header for vmcoreinfo */ 1294 phdr->p_type = PT_NOTE; 1295 phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); 1296 phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; 1297 (ehdr->e_phnum)++; 1298 phdr++; 1299 1300 /* Prepare PT_LOAD type program header for kernel text region */ 1301 if (kernel_map) { 1302 phdr->p_type = PT_LOAD; 1303 phdr->p_flags = PF_R|PF_W|PF_X; 1304 phdr->p_vaddr = (Elf64_Addr)_text; 1305 phdr->p_filesz = phdr->p_memsz = _end - _text; 1306 phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); 1307 ehdr->e_phnum++; 1308 phdr++; 1309 } 1310 1311 /* Go through all the ranges in mem->ranges[] and prepare phdr */ 1312 for (i = 0; i < mem->nr_ranges; i++) { 1313 mstart = mem->ranges[i].start; 1314 mend = mem->ranges[i].end; 1315 1316 phdr->p_type = PT_LOAD; 1317 phdr->p_flags = PF_R|PF_W|PF_X; 1318 phdr->p_offset = mstart; 1319 1320 phdr->p_paddr = mstart; 1321 phdr->p_vaddr = (unsigned long long) __va(mstart); 1322 phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; 1323 phdr->p_align = 0; 1324 ehdr->e_phnum++; 1325 phdr++; 1326 pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", 1327 phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, 1328 ehdr->e_phnum, phdr->p_offset); 1329 } 1330 1331 *addr = buf; 1332 *sz = elf_sz; 1333 return 0; 1334 } 1335