ima_kexec.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 IBM Corporation
  4. *
  5. * Authors:
  6. * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>
  7. * Mimi Zohar <zohar@linux.vnet.ibm.com>
  8. */
  9. #include <linux/seq_file.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/kexec.h>
  12. #include <linux/of.h>
  13. #include <linux/ima.h>
  14. #include <linux/mm.h>
  15. #include <linux/overflow.h>
  16. #include <linux/reboot.h>
  17. #include <asm/page.h>
  18. #include "ima.h"
  19. #ifdef CONFIG_IMA_KEXEC
  20. #define IMA_KEXEC_EVENT_LEN 256
  21. static bool ima_kexec_update_registered;
  22. static struct seq_file ima_kexec_file;
  23. static size_t kexec_segment_size;
  24. static void *ima_kexec_buffer;
  25. static void ima_free_kexec_file_buf(struct seq_file *sf)
  26. {
  27. vfree(sf->buf);
  28. sf->buf = NULL;
  29. sf->size = 0;
  30. sf->read_pos = 0;
  31. sf->count = 0;
  32. }
  33. void ima_measure_kexec_event(const char *event_name)
  34. {
  35. char ima_kexec_event[IMA_KEXEC_EVENT_LEN];
  36. size_t buf_size = 0;
  37. long len;
  38. int n;
  39. buf_size = ima_get_binary_runtime_size();
  40. len = atomic_long_read(&ima_htable.len);
  41. n = scnprintf(ima_kexec_event, IMA_KEXEC_EVENT_LEN,
  42. "kexec_segment_size=%lu;ima_binary_runtime_size=%lu;"
  43. "ima_runtime_measurements_count=%ld;",
  44. kexec_segment_size, buf_size, len);
  45. ima_measure_critical_data("ima_kexec", event_name, ima_kexec_event, n, false, NULL, 0);
  46. }
  47. static int ima_alloc_kexec_file_buf(size_t segment_size)
  48. {
  49. /*
  50. * kexec 'load' may be called multiple times.
  51. * Free and realloc the buffer only if the segment_size is
  52. * changed from the previous kexec 'load' call.
  53. */
  54. if (ima_kexec_file.buf && ima_kexec_file.size == segment_size)
  55. goto out;
  56. ima_free_kexec_file_buf(&ima_kexec_file);
  57. /* segment size can't change between kexec load and execute */
  58. ima_kexec_file.buf = vmalloc(segment_size);
  59. if (!ima_kexec_file.buf)
  60. return -ENOMEM;
  61. ima_kexec_file.size = segment_size;
  62. out:
  63. ima_kexec_file.read_pos = 0;
  64. ima_kexec_file.count = sizeof(struct ima_kexec_hdr); /* reserved space */
  65. ima_measure_kexec_event("kexec_load");
  66. return 0;
  67. }
  68. static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer,
  69. unsigned long segment_size)
  70. {
  71. struct ima_queue_entry *qe;
  72. struct ima_kexec_hdr khdr;
  73. int ret = 0;
  74. /* segment size can't change between kexec load and execute */
  75. if (!ima_kexec_file.buf) {
  76. pr_err("Kexec file buf not allocated\n");
  77. return -EINVAL;
  78. }
  79. memset(&khdr, 0, sizeof(khdr));
  80. khdr.version = 1;
  81. /* This is an append-only list, no need to hold the RCU read lock */
  82. list_for_each_entry_rcu(qe, &ima_measurements, later, true) {
  83. if (ima_kexec_file.count < ima_kexec_file.size) {
  84. khdr.count++;
  85. ima_measurements_show(&ima_kexec_file, qe);
  86. } else {
  87. ret = -EINVAL;
  88. break;
  89. }
  90. }
  91. /*
  92. * fill in reserved space with some buffer details
  93. * (eg. version, buffer size, number of measurements)
  94. */
  95. khdr.buffer_size = ima_kexec_file.count;
  96. if (ima_canonical_fmt) {
  97. khdr.version = cpu_to_le16(khdr.version);
  98. khdr.count = cpu_to_le64(khdr.count);
  99. khdr.buffer_size = cpu_to_le64(khdr.buffer_size);
  100. }
  101. memcpy(ima_kexec_file.buf, &khdr, sizeof(khdr));
  102. print_hex_dump_debug("ima dump: ", DUMP_PREFIX_NONE, 16, 1,
  103. ima_kexec_file.buf, ima_kexec_file.count < 100 ?
  104. ima_kexec_file.count : 100,
  105. true);
  106. *buffer_size = ima_kexec_file.count;
  107. *buffer = ima_kexec_file.buf;
  108. return ret;
  109. }
  110. /*
  111. * Called during kexec_file_load so that IMA can add a segment to the kexec
  112. * image for the measurement list for the next kernel.
  113. *
  114. * This function assumes that kexec_lock is held.
  115. */
  116. void ima_add_kexec_buffer(struct kimage *image)
  117. {
  118. struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE,
  119. .buf_min = 0, .buf_max = ULONG_MAX,
  120. .top_down = true };
  121. unsigned long binary_runtime_size;
  122. unsigned long extra_memory;
  123. /* use more understandable variable names than defined in kbuf */
  124. size_t kexec_buffer_size = 0;
  125. void *kexec_buffer = NULL;
  126. int ret;
  127. if (image->type == KEXEC_TYPE_CRASH)
  128. return;
  129. /*
  130. * Reserve extra memory for measurements added during kexec.
  131. */
  132. if (CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB <= 0)
  133. extra_memory = PAGE_SIZE / 2;
  134. else
  135. extra_memory = CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB * 1024;
  136. binary_runtime_size = ima_get_binary_runtime_size() + extra_memory;
  137. if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE)
  138. kexec_segment_size = ULONG_MAX;
  139. else
  140. kexec_segment_size = ALIGN(binary_runtime_size, PAGE_SIZE);
  141. if ((kexec_segment_size == ULONG_MAX) ||
  142. ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages() / 2)) {
  143. pr_err("Binary measurement list too large.\n");
  144. return;
  145. }
  146. ret = ima_alloc_kexec_file_buf(kexec_segment_size);
  147. if (ret < 0) {
  148. pr_err("Not enough memory for the kexec measurement buffer.\n");
  149. return;
  150. }
  151. kbuf.buffer = kexec_buffer;
  152. kbuf.bufsz = kexec_buffer_size;
  153. kbuf.memsz = kexec_segment_size;
  154. image->is_ima_segment_index_set = false;
  155. ret = kexec_add_buffer(&kbuf);
  156. if (ret) {
  157. pr_err("Error passing over kexec measurement buffer.\n");
  158. vfree(kexec_buffer);
  159. return;
  160. }
  161. image->ima_buffer_addr = kbuf.mem;
  162. image->ima_buffer_size = kexec_segment_size;
  163. image->ima_buffer = kexec_buffer;
  164. image->ima_segment_index = image->nr_segments - 1;
  165. image->is_ima_segment_index_set = true;
  166. kexec_dprintk("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
  167. kbuf.mem);
  168. }
  169. /*
  170. * Called during kexec execute so that IMA can update the measurement list.
  171. */
  172. static int ima_update_kexec_buffer(struct notifier_block *self,
  173. unsigned long action, void *data)
  174. {
  175. size_t buf_size = 0;
  176. int ret = NOTIFY_OK;
  177. void *buf = NULL;
  178. if (!kexec_in_progress) {
  179. pr_info("No kexec in progress.\n");
  180. return ret;
  181. }
  182. if (!ima_kexec_buffer) {
  183. pr_err("Kexec buffer not set.\n");
  184. return ret;
  185. }
  186. ret = ima_dump_measurement_list(&buf_size, &buf, kexec_segment_size);
  187. if (ret)
  188. pr_err("Dump measurements failed. Error:%d\n", ret);
  189. if (buf_size != 0)
  190. memcpy(ima_kexec_buffer, buf, buf_size);
  191. kimage_unmap_segment(ima_kexec_buffer);
  192. ima_kexec_buffer = NULL;
  193. return ret;
  194. }
  195. static struct notifier_block update_buffer_nb = {
  196. .notifier_call = ima_update_kexec_buffer,
  197. .priority = INT_MIN
  198. };
  199. /*
  200. * Create a mapping for the source pages that contain the IMA buffer
  201. * so we can update it later.
  202. */
  203. void ima_kexec_post_load(struct kimage *image)
  204. {
  205. if (ima_kexec_buffer) {
  206. kimage_unmap_segment(ima_kexec_buffer);
  207. ima_kexec_buffer = NULL;
  208. }
  209. if (!image->ima_buffer_addr)
  210. return;
  211. ima_kexec_buffer = kimage_map_segment(image, image->ima_segment_index);
  212. if (!ima_kexec_buffer) {
  213. pr_err("Could not map measurements buffer.\n");
  214. return;
  215. }
  216. if (!ima_kexec_update_registered) {
  217. register_reboot_notifier(&update_buffer_nb);
  218. ima_kexec_update_registered = true;
  219. }
  220. }
  221. #endif /* IMA_KEXEC */
  222. /*
  223. * Restore the measurement list from the previous kernel.
  224. */
  225. void __init ima_load_kexec_buffer(void)
  226. {
  227. void *kexec_buffer = NULL;
  228. size_t kexec_buffer_size = 0;
  229. int rc;
  230. rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size);
  231. switch (rc) {
  232. case 0:
  233. rc = ima_restore_measurement_list(kexec_buffer_size,
  234. kexec_buffer);
  235. if (rc != 0)
  236. pr_err("Failed to restore the measurement list: %d\n",
  237. rc);
  238. ima_free_kexec_buffer();
  239. break;
  240. case -ENOTSUPP:
  241. pr_debug("Restoring the measurement list not supported\n");
  242. break;
  243. case -ENOENT:
  244. pr_debug("No measurement list to restore\n");
  245. break;
  246. default:
  247. pr_debug("Error restoring the measurement list: %d\n", rc);
  248. }
  249. }
  250. /*
  251. * ima_validate_range - verify a physical buffer lies in addressable RAM
  252. * @phys: physical start address of the buffer from previous kernel
  253. * @size: size of the buffer
  254. *
  255. * On success return 0. On failure returns -EINVAL so callers can skip
  256. * restoring.
  257. */
  258. int ima_validate_range(phys_addr_t phys, size_t size)
  259. {
  260. unsigned long start_pfn, end_pfn;
  261. phys_addr_t end_phys;
  262. if (check_add_overflow(phys, (phys_addr_t)size - 1, &end_phys))
  263. return -EINVAL;
  264. start_pfn = PHYS_PFN(phys);
  265. end_pfn = PHYS_PFN(end_phys);
  266. #ifdef CONFIG_X86
  267. if (!pfn_range_is_mapped(start_pfn, end_pfn))
  268. #else
  269. if (!page_is_ram(start_pfn) || !page_is_ram(end_pfn))
  270. #endif
  271. {
  272. pr_warn("IMA: previous kernel measurement buffer %pa (size 0x%zx) lies outside available memory\n",
  273. &phys, size);
  274. return -EINVAL;
  275. }
  276. return 0;
  277. }