kexec_elf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <[email protected]>
  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. if (image->type == KEXEC_TYPE_CRASH)
  38. buf.mem += crashk_res.start;
  39. buf.memsz = phdr->p_memsz;
  40. data->memsz = ALIGN(data->memsz, phdr->p_align) + buf.memsz;
  41. if (entry - phdr->p_paddr < phdr->p_memsz) {
  42. data->kernel_buf = buf.buffer;
  43. data->kernel_mem = buf.mem;
  44. data->parm = buf.buffer + PARMAREA;
  45. }
  46. ipl_report_add_component(data->report, &buf,
  47. IPL_RB_COMPONENT_FLAG_SIGNED |
  48. IPL_RB_COMPONENT_FLAG_VERIFIED,
  49. IPL_RB_CERT_UNKNOWN);
  50. ret = kexec_add_buffer(&buf);
  51. if (ret)
  52. return ret;
  53. }
  54. return data->memsz ? 0 : -EINVAL;
  55. }
  56. static void *s390_elf_load(struct kimage *image,
  57. char *kernel, unsigned long kernel_len,
  58. char *initrd, unsigned long initrd_len,
  59. char *cmdline, unsigned long cmdline_len)
  60. {
  61. const Elf_Ehdr *ehdr;
  62. const Elf_Phdr *phdr;
  63. size_t size;
  64. int i;
  65. /* image->fobs->probe already checked for valid ELF magic number. */
  66. ehdr = (Elf_Ehdr *)kernel;
  67. if (ehdr->e_type != ET_EXEC ||
  68. ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
  69. !elf_check_arch(ehdr))
  70. return ERR_PTR(-EINVAL);
  71. if (!ehdr->e_phnum || ehdr->e_phentsize != sizeof(Elf_Phdr))
  72. return ERR_PTR(-EINVAL);
  73. size = ehdr->e_ehsize + ehdr->e_phoff;
  74. size += ehdr->e_phentsize * ehdr->e_phnum;
  75. if (size > kernel_len)
  76. return ERR_PTR(-EINVAL);
  77. phdr = (void *)ehdr + ehdr->e_phoff;
  78. size = ALIGN(size, phdr->p_align);
  79. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  80. if (phdr->p_type == PT_INTERP)
  81. return ERR_PTR(-EINVAL);
  82. if (phdr->p_offset > kernel_len)
  83. return ERR_PTR(-EINVAL);
  84. size += ALIGN(phdr->p_filesz, phdr->p_align);
  85. }
  86. if (size > kernel_len)
  87. return ERR_PTR(-EINVAL);
  88. return kexec_file_add_components(image, kexec_file_add_kernel_elf);
  89. }
  90. static int s390_elf_probe(const char *buf, unsigned long len)
  91. {
  92. const Elf_Ehdr *ehdr;
  93. if (len < sizeof(Elf_Ehdr))
  94. return -ENOEXEC;
  95. ehdr = (Elf_Ehdr *)buf;
  96. /* Only check the ELF magic number here and do proper validity check
  97. * in the loader. Any check here that fails would send the erroneous
  98. * ELF file to the image loader that does not care what it gets.
  99. * (Most likely) causing behavior not intended by the user.
  100. */
  101. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
  102. return -ENOEXEC;
  103. return 0;
  104. }
  105. const struct kexec_file_ops s390_kexec_elf_ops = {
  106. .probe = s390_elf_probe,
  107. .load = s390_elf_load,
  108. #ifdef CONFIG_KEXEC_SIG
  109. .verify_sig = s390_verify_sig,
  110. #endif /* CONFIG_KEXEC_SIG */
  111. };