kexec.c 8.0 KB

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