ima_kexec.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2016 IBM Corporation
  3. *
  4. * Authors:
  5. * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>
  6. * Mimi Zohar <zohar@linux.vnet.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/seq_file.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/kexec.h>
  16. #include "ima.h"
  17. #ifdef CONFIG_IMA_KEXEC
  18. static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer,
  19. unsigned long segment_size)
  20. {
  21. struct ima_queue_entry *qe;
  22. struct seq_file file;
  23. struct ima_kexec_hdr khdr;
  24. int ret = 0;
  25. /* segment size can't change between kexec load and execute */
  26. file.buf = vmalloc(segment_size);
  27. if (!file.buf) {
  28. ret = -ENOMEM;
  29. goto out;
  30. }
  31. file.size = segment_size;
  32. file.read_pos = 0;
  33. file.count = sizeof(khdr); /* reserved space */
  34. memset(&khdr, 0, sizeof(khdr));
  35. khdr.version = 1;
  36. list_for_each_entry_rcu(qe, &ima_measurements, later) {
  37. if (file.count < file.size) {
  38. khdr.count++;
  39. ima_measurements_show(&file, qe);
  40. } else {
  41. ret = -EINVAL;
  42. break;
  43. }
  44. }
  45. if (ret < 0)
  46. goto out;
  47. /*
  48. * fill in reserved space with some buffer details
  49. * (eg. version, buffer size, number of measurements)
  50. */
  51. khdr.buffer_size = file.count;
  52. if (ima_canonical_fmt) {
  53. khdr.version = cpu_to_le16(khdr.version);
  54. khdr.count = cpu_to_le64(khdr.count);
  55. khdr.buffer_size = cpu_to_le64(khdr.buffer_size);
  56. }
  57. memcpy(file.buf, &khdr, sizeof(khdr));
  58. print_hex_dump(KERN_DEBUG, "ima dump: ", DUMP_PREFIX_NONE,
  59. 16, 1, file.buf,
  60. file.count < 100 ? file.count : 100, true);
  61. *buffer_size = file.count;
  62. *buffer = file.buf;
  63. out:
  64. if (ret == -EINVAL)
  65. vfree(file.buf);
  66. return ret;
  67. }
  68. /*
  69. * Called during kexec_file_load so that IMA can add a segment to the kexec
  70. * image for the measurement list for the next kernel.
  71. *
  72. * This function assumes that kexec_mutex is held.
  73. */
  74. void ima_add_kexec_buffer(struct kimage *image)
  75. {
  76. struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE,
  77. .buf_min = 0, .buf_max = ULONG_MAX,
  78. .top_down = true };
  79. unsigned long binary_runtime_size;
  80. /* use more understandable variable names than defined in kbuf */
  81. void *kexec_buffer = NULL;
  82. size_t kexec_buffer_size;
  83. size_t kexec_segment_size;
  84. int ret;
  85. /*
  86. * Reserve an extra half page of memory for additional measurements
  87. * added during the kexec load.
  88. */
  89. binary_runtime_size = ima_get_binary_runtime_size();
  90. if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE)
  91. kexec_segment_size = ULONG_MAX;
  92. else
  93. kexec_segment_size = ALIGN(ima_get_binary_runtime_size() +
  94. PAGE_SIZE / 2, PAGE_SIZE);
  95. if ((kexec_segment_size == ULONG_MAX) ||
  96. ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages / 2)) {
  97. pr_err("Binary measurement list too large.\n");
  98. return;
  99. }
  100. ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
  101. kexec_segment_size);
  102. if (!kexec_buffer) {
  103. pr_err("Not enough memory for the kexec measurement buffer.\n");
  104. return;
  105. }
  106. kbuf.buffer = kexec_buffer;
  107. kbuf.bufsz = kexec_buffer_size;
  108. kbuf.memsz = kexec_segment_size;
  109. ret = kexec_add_buffer(&kbuf);
  110. if (ret) {
  111. pr_err("Error passing over kexec measurement buffer.\n");
  112. vfree(kexec_buffer);
  113. return;
  114. }
  115. ret = arch_ima_add_kexec_buffer(image, kbuf.mem, kexec_segment_size);
  116. if (ret) {
  117. pr_err("Error passing over kexec measurement buffer.\n");
  118. return;
  119. }
  120. image->ima_buffer = kexec_buffer;
  121. pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
  122. kbuf.mem);
  123. }
  124. #endif /* IMA_KEXEC */
  125. /*
  126. * Restore the measurement list from the previous kernel.
  127. */
  128. void ima_load_kexec_buffer(void)
  129. {
  130. void *kexec_buffer = NULL;
  131. size_t kexec_buffer_size = 0;
  132. int rc;
  133. rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size);
  134. switch (rc) {
  135. case 0:
  136. rc = ima_restore_measurement_list(kexec_buffer_size,
  137. kexec_buffer);
  138. if (rc != 0)
  139. pr_err("Failed to restore the measurement list: %d\n",
  140. rc);
  141. ima_free_kexec_buffer();
  142. break;
  143. case -ENOTSUPP:
  144. pr_debug("Restoring the measurement list not supported\n");
  145. break;
  146. case -ENOENT:
  147. pr_debug("No measurement list to restore\n");
  148. break;
  149. default:
  150. pr_debug("Error restoring the measurement list: %d\n", rc);
  151. }
  152. }