kexec.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kexec.c - kexec_load system call
  4. * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/capability.h>
  8. #include <linux/mm.h>
  9. #include <linux/file.h>
  10. #include <linux/security.h>
  11. #include <linux/kexec.h>
  12. #include <linux/mutex.h>
  13. #include <linux/list.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/slab.h>
  17. #include "kexec_internal.h"
  18. static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
  19. unsigned long nr_segments,
  20. struct kexec_segment *segments,
  21. unsigned long flags)
  22. {
  23. int ret;
  24. struct kimage *image;
  25. bool kexec_on_panic = flags & KEXEC_ON_CRASH;
  26. #ifdef CONFIG_CRASH_DUMP
  27. if (kexec_on_panic) {
  28. /* Verify we have a valid entry point */
  29. if ((entry < phys_to_boot_phys(crashk_res.start)) ||
  30. (entry > phys_to_boot_phys(crashk_res.end)))
  31. return -EADDRNOTAVAIL;
  32. }
  33. #endif
  34. /* Allocate and initialize a controlling structure */
  35. image = do_kimage_alloc_init();
  36. if (!image)
  37. return -ENOMEM;
  38. image->start = entry;
  39. image->nr_segments = nr_segments;
  40. memcpy(image->segment, segments, nr_segments * sizeof(*segments));
  41. #ifdef CONFIG_CRASH_DUMP
  42. if (kexec_on_panic) {
  43. /* Enable special crash kernel control page alloc policy. */
  44. image->control_page = crashk_res.start;
  45. image->type = KEXEC_TYPE_CRASH;
  46. }
  47. #endif
  48. ret = sanity_check_segment_list(image);
  49. if (ret)
  50. goto out_free_image;
  51. /*
  52. * Find a location for the control code buffer, and add it
  53. * the vector of segments so that it's pages will also be
  54. * counted as destination pages.
  55. */
  56. ret = -ENOMEM;
  57. image->control_code_page = kimage_alloc_control_pages(image,
  58. get_order(KEXEC_CONTROL_PAGE_SIZE));
  59. if (!image->control_code_page) {
  60. pr_err("Could not allocate control_code_buffer\n");
  61. goto out_free_image;
  62. }
  63. if (!kexec_on_panic) {
  64. image->swap_page = kimage_alloc_control_pages(image, 0);
  65. if (!image->swap_page) {
  66. pr_err("Could not allocate swap buffer\n");
  67. goto out_free_control_pages;
  68. }
  69. }
  70. *rimage = image;
  71. return 0;
  72. out_free_control_pages:
  73. kimage_free_page_list(&image->control_pages);
  74. out_free_image:
  75. kfree(image);
  76. return ret;
  77. }
  78. static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
  79. struct kexec_segment *segments, unsigned long flags)
  80. {
  81. struct kimage **dest_image, *image;
  82. unsigned long i;
  83. int ret;
  84. /*
  85. * Because we write directly to the reserved memory region when loading
  86. * crash kernels we need a serialization here to prevent multiple crash
  87. * kernels from attempting to load simultaneously.
  88. */
  89. if (!kexec_trylock())
  90. return -EBUSY;
  91. #ifdef CONFIG_CRASH_DUMP
  92. if (flags & KEXEC_ON_CRASH) {
  93. dest_image = &kexec_crash_image;
  94. if (kexec_crash_image)
  95. arch_kexec_unprotect_crashkres();
  96. } else
  97. #endif
  98. dest_image = &kexec_image;
  99. if (nr_segments == 0) {
  100. /* Uninstall image */
  101. kimage_free(xchg(dest_image, NULL));
  102. ret = 0;
  103. goto out_unlock;
  104. }
  105. if (flags & KEXEC_ON_CRASH) {
  106. /*
  107. * Loading another kernel to switch to if this one
  108. * crashes. Free any current crash dump kernel before
  109. * we corrupt it.
  110. */
  111. kimage_free(xchg(&kexec_crash_image, NULL));
  112. }
  113. ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
  114. if (ret)
  115. goto out_unlock;
  116. if (flags & KEXEC_PRESERVE_CONTEXT)
  117. image->preserve_context = 1;
  118. #ifdef CONFIG_CRASH_HOTPLUG
  119. if ((flags & KEXEC_ON_CRASH) && arch_crash_hotplug_support(image, flags))
  120. image->hotplug_support = 1;
  121. #endif
  122. ret = machine_kexec_prepare(image);
  123. if (ret)
  124. goto out;
  125. /*
  126. * Some architecture(like S390) may touch the crash memory before
  127. * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
  128. */
  129. ret = kimage_crash_copy_vmcoreinfo(image);
  130. if (ret)
  131. goto out;
  132. for (i = 0; i < nr_segments; i++) {
  133. ret = kimage_load_segment(image, &image->segment[i]);
  134. if (ret)
  135. goto out;
  136. }
  137. kimage_terminate(image);
  138. ret = machine_kexec_post_load(image);
  139. if (ret)
  140. goto out;
  141. /* Install the new kernel and uninstall the old */
  142. image = xchg(dest_image, image);
  143. out:
  144. #ifdef CONFIG_CRASH_DUMP
  145. if ((flags & KEXEC_ON_CRASH) && kexec_crash_image)
  146. arch_kexec_protect_crashkres();
  147. #endif
  148. kimage_free(image);
  149. out_unlock:
  150. kexec_unlock();
  151. return ret;
  152. }
  153. /*
  154. * Exec Kernel system call: for obvious reasons only root may call it.
  155. *
  156. * This call breaks up into three pieces.
  157. * - A generic part which loads the new kernel from the current
  158. * address space, and very carefully places the data in the
  159. * allocated pages.
  160. *
  161. * - A generic part that interacts with the kernel and tells all of
  162. * the devices to shut down. Preventing on-going dmas, and placing
  163. * the devices in a consistent state so a later kernel can
  164. * reinitialize them.
  165. *
  166. * - A machine specific part that includes the syscall number
  167. * and then copies the image to it's final destination. And
  168. * jumps into the image at entry.
  169. *
  170. * kexec does not sync, or unmount filesystems so if you need
  171. * that to happen you need to do that yourself.
  172. */
  173. static inline int kexec_load_check(unsigned long nr_segments,
  174. unsigned long flags)
  175. {
  176. int image_type = (flags & KEXEC_ON_CRASH) ?
  177. KEXEC_TYPE_CRASH : KEXEC_TYPE_DEFAULT;
  178. int result;
  179. /* We only trust the superuser with rebooting the system. */
  180. if (!kexec_load_permitted(image_type))
  181. return -EPERM;
  182. /* Permit LSMs and IMA to fail the kexec */
  183. result = security_kernel_load_data(LOADING_KEXEC_IMAGE, false);
  184. if (result < 0)
  185. return result;
  186. /*
  187. * kexec can be used to circumvent module loading restrictions, so
  188. * prevent loading in that case
  189. */
  190. result = security_locked_down(LOCKDOWN_KEXEC);
  191. if (result)
  192. return result;
  193. /*
  194. * Verify we have a legal set of flags
  195. * This leaves us room for future extensions.
  196. */
  197. if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
  198. return -EINVAL;
  199. /* Put an artificial cap on the number
  200. * of segments passed to kexec_load.
  201. */
  202. if (nr_segments > KEXEC_SEGMENT_MAX)
  203. return -EINVAL;
  204. return 0;
  205. }
  206. SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
  207. struct kexec_segment __user *, segments, unsigned long, flags)
  208. {
  209. struct kexec_segment *ksegments;
  210. unsigned long result;
  211. result = kexec_load_check(nr_segments, flags);
  212. if (result)
  213. return result;
  214. /* Verify we are on the appropriate architecture */
  215. if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
  216. ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
  217. return -EINVAL;
  218. ksegments = memdup_array_user(segments, nr_segments, sizeof(ksegments[0]));
  219. if (IS_ERR(ksegments))
  220. return PTR_ERR(ksegments);
  221. result = do_kexec_load(entry, nr_segments, ksegments, flags);
  222. kfree(ksegments);
  223. return result;
  224. }
  225. #ifdef CONFIG_COMPAT
  226. COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
  227. compat_ulong_t, nr_segments,
  228. struct compat_kexec_segment __user *, segments,
  229. compat_ulong_t, flags)
  230. {
  231. struct compat_kexec_segment in;
  232. struct kexec_segment *ksegments;
  233. unsigned long i, result;
  234. result = kexec_load_check(nr_segments, flags);
  235. if (result)
  236. return result;
  237. /* Don't allow clients that don't understand the native
  238. * architecture to do anything.
  239. */
  240. if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
  241. return -EINVAL;
  242. ksegments = kmalloc_array(nr_segments, sizeof(ksegments[0]),
  243. GFP_KERNEL);
  244. if (!ksegments)
  245. return -ENOMEM;
  246. for (i = 0; i < nr_segments; i++) {
  247. result = copy_from_user(&in, &segments[i], sizeof(in));
  248. if (result)
  249. goto fail;
  250. ksegments[i].buf = compat_ptr(in.buf);
  251. ksegments[i].bufsz = in.bufsz;
  252. ksegments[i].mem = in.mem;
  253. ksegments[i].memsz = in.memsz;
  254. }
  255. result = do_kexec_load(entry, nr_segments, ksegments, flags);
  256. fail:
  257. kfree(ksegments);
  258. return result;
  259. }
  260. #endif