kexec_elf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ELF loader for kexec_file_load system call.
  4. *
  5. * Copyright IBM Corp. 2018
  6. *
  7. * Author(s): Philipp Rudo <prudo@linux.vnet.ibm.com>
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/kexec.h>
  12. #include <asm/ipl.h>
  13. #include <asm/setup.h>
  14. static int kexec_file_add_kernel_elf(struct kimage *image,
  15. struct s390_load_data *data)
  16. {
  17. struct kexec_buf buf;
  18. const Elf_Ehdr *ehdr;
  19. const Elf_Phdr *phdr;
  20. Elf_Addr entry;
  21. void *kernel;
  22. int i, ret;
  23. kernel = image->kernel_buf;
  24. ehdr = (Elf_Ehdr *)kernel;
  25. buf.image = image;
  26. if (image->type == KEXEC_TYPE_CRASH)
  27. entry = STARTUP_KDUMP_OFFSET;
  28. else
  29. entry = ehdr->e_entry;
  30. phdr = (void *)ehdr + ehdr->e_phoff;
  31. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  32. if (phdr->p_type != PT_LOAD)
  33. continue;
  34. buf.buffer = kernel + phdr->p_offset;
  35. buf.bufsz = phdr->p_filesz;
  36. buf.mem = ALIGN(phdr->p_paddr, phdr->p_align);
  37. #ifdef CONFIG_CRASH_DUMP
  38. if (image->type == KEXEC_TYPE_CRASH)
  39. buf.mem += crashk_res.start;
  40. #endif
  41. buf.memsz = phdr->p_memsz;
  42. data->memsz = ALIGN(data->memsz, phdr->p_align) + buf.memsz;
  43. if (entry - phdr->p_paddr < phdr->p_memsz) {
  44. data->kernel_buf = buf.buffer;
  45. data->kernel_mem = buf.mem;
  46. data->parm = buf.buffer + PARMAREA;
  47. }
  48. ipl_report_add_component(data->report, &buf,
  49. IPL_RB_COMPONENT_FLAG_SIGNED |
  50. IPL_RB_COMPONENT_FLAG_VERIFIED,
  51. IPL_RB_CERT_UNKNOWN);
  52. ret = kexec_add_buffer(&buf);
  53. if (ret)
  54. return ret;
  55. }
  56. return data->memsz ? 0 : -EINVAL;
  57. }
  58. static void *s390_elf_load(struct kimage *image,
  59. char *kernel, unsigned long kernel_len,
  60. char *initrd, unsigned long initrd_len,
  61. char *cmdline, unsigned long cmdline_len)
  62. {
  63. const Elf_Ehdr *ehdr;
  64. const Elf_Phdr *phdr;
  65. size_t size;
  66. int i;
  67. /* image->fobs->probe already checked for valid ELF magic number. */
  68. ehdr = (Elf_Ehdr *)kernel;
  69. if (ehdr->e_type != ET_EXEC ||
  70. ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
  71. !elf_check_arch(ehdr))
  72. return ERR_PTR(-EINVAL);
  73. if (!ehdr->e_phnum || ehdr->e_phentsize != sizeof(Elf_Phdr))
  74. return ERR_PTR(-EINVAL);
  75. size = ehdr->e_ehsize + ehdr->e_phoff;
  76. size += ehdr->e_phentsize * ehdr->e_phnum;
  77. if (size > kernel_len)
  78. return ERR_PTR(-EINVAL);
  79. phdr = (void *)ehdr + ehdr->e_phoff;
  80. size = ALIGN(size, phdr->p_align);
  81. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  82. if (phdr->p_type == PT_INTERP)
  83. return ERR_PTR(-EINVAL);
  84. if (phdr->p_offset > kernel_len)
  85. return ERR_PTR(-EINVAL);
  86. size += ALIGN(phdr->p_filesz, phdr->p_align);
  87. }
  88. if (size > kernel_len)
  89. return ERR_PTR(-EINVAL);
  90. return kexec_file_add_components(image, kexec_file_add_kernel_elf);
  91. }
  92. static int s390_elf_probe(const char *buf, unsigned long len)
  93. {
  94. const Elf_Ehdr *ehdr;
  95. if (len < sizeof(Elf_Ehdr))
  96. return -ENOEXEC;
  97. ehdr = (Elf_Ehdr *)buf;
  98. /* Only check the ELF magic number here and do proper validity check
  99. * in the loader. Any check here that fails would send the erroneous
  100. * ELF file to the image loader that does not care what it gets.
  101. * (Most likely) causing behavior not intended by the user.
  102. */
  103. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
  104. return -ENOEXEC;
  105. return 0;
  106. }
  107. const struct kexec_file_ops s390_kexec_elf_ops = {
  108. .probe = s390_elf_probe,
  109. .load = s390_elf_load,
  110. #ifdef CONFIG_KEXEC_SIG
  111. .verify_sig = s390_verify_sig,
  112. #endif /* CONFIG_KEXEC_SIG */
  113. };