kexec.c 7.5 KB

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