kexec_elf.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/setup.h>
  13. static int kexec_file_add_elf_kernel(struct kimage *image,
  14. struct s390_load_data *data,
  15. char *kernel, unsigned long kernel_len)
  16. {
  17. struct kexec_buf buf;
  18. const Elf_Ehdr *ehdr;
  19. const Elf_Phdr *phdr;
  20. Elf_Addr entry;
  21. int i, ret;
  22. ehdr = (Elf_Ehdr *)kernel;
  23. buf.image = image;
  24. if (image->type == KEXEC_TYPE_CRASH)
  25. entry = STARTUP_KDUMP_OFFSET;
  26. else
  27. entry = ehdr->e_entry;
  28. phdr = (void *)ehdr + ehdr->e_phoff;
  29. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  30. if (phdr->p_type != PT_LOAD)
  31. continue;
  32. buf.buffer = kernel + phdr->p_offset;
  33. buf.bufsz = phdr->p_filesz;
  34. buf.mem = ALIGN(phdr->p_paddr, phdr->p_align);
  35. buf.memsz = phdr->p_memsz;
  36. if (entry - phdr->p_paddr < phdr->p_memsz) {
  37. data->kernel_buf = buf.buffer;
  38. data->memsz += STARTUP_NORMAL_OFFSET;
  39. buf.buffer += STARTUP_NORMAL_OFFSET;
  40. buf.bufsz -= STARTUP_NORMAL_OFFSET;
  41. buf.mem += STARTUP_NORMAL_OFFSET;
  42. buf.memsz -= STARTUP_NORMAL_OFFSET;
  43. }
  44. if (image->type == KEXEC_TYPE_CRASH)
  45. buf.mem += crashk_res.start;
  46. ret = kexec_add_buffer(&buf);
  47. if (ret)
  48. return ret;
  49. data->memsz = ALIGN(data->memsz, phdr->p_align) + buf.memsz;
  50. }
  51. return 0;
  52. }
  53. static void *s390_elf_load(struct kimage *image,
  54. char *kernel, unsigned long kernel_len,
  55. char *initrd, unsigned long initrd_len,
  56. char *cmdline, unsigned long cmdline_len)
  57. {
  58. struct s390_load_data data = {0};
  59. const Elf_Ehdr *ehdr;
  60. const Elf_Phdr *phdr;
  61. size_t size;
  62. int i, ret;
  63. /* image->fobs->probe already checked for valid ELF magic number. */
  64. ehdr = (Elf_Ehdr *)kernel;
  65. if (ehdr->e_type != ET_EXEC ||
  66. ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
  67. !elf_check_arch(ehdr))
  68. return ERR_PTR(-EINVAL);
  69. if (!ehdr->e_phnum || ehdr->e_phentsize != sizeof(Elf_Phdr))
  70. return ERR_PTR(-EINVAL);
  71. size = ehdr->e_ehsize + ehdr->e_phoff;
  72. size += ehdr->e_phentsize * ehdr->e_phnum;
  73. if (size > kernel_len)
  74. return ERR_PTR(-EINVAL);
  75. phdr = (void *)ehdr + ehdr->e_phoff;
  76. size = ALIGN(size, phdr->p_align);
  77. for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
  78. if (phdr->p_type == PT_INTERP)
  79. return ERR_PTR(-EINVAL);
  80. if (phdr->p_offset > kernel_len)
  81. return ERR_PTR(-EINVAL);
  82. size += ALIGN(phdr->p_filesz, phdr->p_align);
  83. }
  84. if (size > kernel_len)
  85. return ERR_PTR(-EINVAL);
  86. ret = kexec_file_add_elf_kernel(image, &data, kernel, kernel_len);
  87. if (ret)
  88. return ERR_PTR(ret);
  89. if (!data.memsz)
  90. return ERR_PTR(-EINVAL);
  91. if (initrd) {
  92. ret = kexec_file_add_initrd(image, &data, initrd, initrd_len);
  93. if (ret)
  94. return ERR_PTR(ret);
  95. }
  96. ret = kexec_file_add_purgatory(image, &data);
  97. if (ret)
  98. return ERR_PTR(ret);
  99. return kexec_file_update_kernel(image, &data);
  100. }
  101. static int s390_elf_probe(const char *buf, unsigned long len)
  102. {
  103. const Elf_Ehdr *ehdr;
  104. if (len < sizeof(Elf_Ehdr))
  105. return -ENOEXEC;
  106. ehdr = (Elf_Ehdr *)buf;
  107. /* Only check the ELF magic number here and do proper validity check
  108. * in the loader. Any check here that fails would send the erroneous
  109. * ELF file to the image loader that does not care what it gets.
  110. * (Most likely) causing behavior not intended by the user.
  111. */
  112. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0)
  113. return -ENOEXEC;
  114. return 0;
  115. }
  116. const struct kexec_file_ops s390_kexec_elf_ops = {
  117. .probe = s390_elf_probe,
  118. .load = s390_elf_load,
  119. };