@@ -11,6 +11,7 @@
#define _LINUX_IMA_H
#include <linux/fs.h>
+#include <linux/kexec.h>
struct linux_binprm;
enum ima_buffer_id {
@@ -35,6 +36,14 @@ extern int ima_add_measurement_check(const char *hashname, u8 *digest,
loff_t size, enum ima_buffer_id buffer_id,
char *hint);
+#ifdef CONFIG_IMA_KEXEC
+extern void ima_add_kexec_buffer(struct kimage *image);
+#else
+static inline void ima_add_kexec_buffer(struct kimage *image)
+{
+}
+#endif
+
#else
static inline int ima_bprm_check(struct linux_binprm *bprm)
{
@@ -85,6 +94,12 @@ static inline int ima_add_measurement_check(const char *hashname, u8 *digest,
{
return 0;
}
+
+#ifdef CONFIG_IMA_KEXEC
+static inline void ima_add_kexec_buffer(struct kimage *image)
+{
+}
+#endif
#endif /* CONFIG_IMA */
#ifdef CONFIG_IMA_APPRAISE
@@ -202,6 +202,9 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
return ret;
image->kernel_buf_len = size;
+ /* IMA needs to pass the measurement list to the next kernel. */
+ ima_add_kexec_buffer(image);
+
/* Call arch image probe handlers */
ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
image->kernel_buf_len);
@@ -23,6 +23,11 @@
#include "ima.h"
+#ifdef CONFIG_IMA_KEXEC
+/* Physical address of the measurement buffer in the next kernel. */
+static unsigned long kexec_buffer_load_addr;
+static size_t kexec_segment_size;
+
static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer,
unsigned long segment_size)
{
@@ -75,6 +80,84 @@ out:
}
/*
+ * Called during kexec execute so that IMA can save the measurement list.
+ */
+static int ima_update_kexec_buffer(struct notifier_block *self,
+ unsigned long action, void *data)
+{
+ void *kexec_buffer = NULL;
+ size_t kexec_buffer_size;
+ int ret;
+
+ if (!kexec_in_progress)
+ return NOTIFY_OK;
+
+ kexec_buffer_size = ima_get_binary_runtime_size();
+ if (kexec_buffer_size >
+ (kexec_segment_size - sizeof(struct ima_kexec_hdr))) {
+ pr_err("Binary measurement list grew too large.\n");
+ goto out;
+ }
+
+ ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
+ kexec_segment_size);
+ if (!kexec_buffer) {
+ pr_err("Not enough memory for the kexec measurement buffer.\n");
+ goto out;
+ }
+ ret = kexec_update_segment(kexec_buffer, kexec_buffer_size,
+ kexec_buffer_load_addr, kexec_segment_size);
+ if (ret)
+ pr_err("Error updating kexec buffer: %d\n", ret);
+out:
+ return NOTIFY_OK;
+}
+
+struct notifier_block update_buffer_nb = {
+ .notifier_call = ima_update_kexec_buffer,
+};
+
+/*
+ * Called during kexec_file_load so that IMA can add a segment to the kexec
+ * image for the measurement list for the next kernel.
+ */
+void ima_add_kexec_buffer(struct kimage *image)
+{
+ struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE,
+ .buf_min = 0, .buf_max = ULONG_MAX,
+ .top_down = true };
+ int ret;
+
+ if (!kexec_can_hand_over_buffer())
+ return;
+
+ kexec_segment_size = ALIGN(ima_get_binary_runtime_size() + PAGE_SIZE,
+ PAGE_SIZE);
+
+ if (kexec_segment_size >= (ULONG_MAX - sizeof(long))) {
+ pr_err("Binary measurement list too large.\n");
+ return;
+ }
+
+ /* Ask not to checksum the segment, we will update it later. */
+ kbuf.buffer = NULL;
+ kbuf.bufsz = 0;
+ kbuf.memsz = kexec_segment_size;
+ ret = kexec_add_handover_buffer(&kbuf, false);
+ if (ret) {
+ pr_err("Error passing over kexec measurement buffer.\n");
+ return;
+ }
+ kexec_buffer_load_addr = kbuf.mem;
+
+ register_reboot_notifier(&update_buffer_nb);
+
+ pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
+ kexec_buffer_load_addr);
+}
+#endif /* IMA_KEXEC */
+
+/*
* Restore the measurement list from the previous kernel.
*/
void ima_load_kexec_buffer(void)