1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 IBM Corporation
10 #include <linux/seq_file.h>
11 #include <linux/vmalloc.h>
12 #include <linux/kexec.h>
14 #include <linux/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)
21 struct ima_queue_entry *qe;
23 struct ima_kexec_hdr khdr;
26 /* segment size can't change between kexec load and execute */
27 file.buf = vmalloc(segment_size);
33 file.size = segment_size;
35 file.count = sizeof(khdr); /* reserved space */
37 memset(&khdr, 0, sizeof(khdr));
39 list_for_each_entry_rcu(qe, &ima_measurements, later) {
40 if (file.count < file.size) {
42 ima_measurements_show(&file, qe);
53 * fill in reserved space with some buffer details
54 * (eg. version, buffer size, number of measurements)
56 khdr.buffer_size = file.count;
57 if (ima_canonical_fmt) {
58 khdr.version = cpu_to_le16(khdr.version);
59 khdr.count = cpu_to_le64(khdr.count);
60 khdr.buffer_size = cpu_to_le64(khdr.buffer_size);
62 memcpy(file.buf, &khdr, sizeof(khdr));
64 print_hex_dump(KERN_DEBUG, "ima dump: ", DUMP_PREFIX_NONE,
66 file.count < 100 ? file.count : 100, true);
68 *buffer_size = file.count;
77 * Called during kexec_file_load so that IMA can add a segment to the kexec
78 * image for the measurement list for the next kernel.
80 * This function assumes that kexec_mutex is held.
82 void ima_add_kexec_buffer(struct kimage *image)
84 struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE,
85 .buf_min = 0, .buf_max = ULONG_MAX,
87 unsigned long binary_runtime_size;
89 /* use more understandable variable names than defined in kbuf */
90 void *kexec_buffer = NULL;
91 size_t kexec_buffer_size;
92 size_t kexec_segment_size;
96 * Reserve an extra half page of memory for additional measurements
97 * added during the kexec load.
99 binary_runtime_size = ima_get_binary_runtime_size();
100 if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE)
101 kexec_segment_size = ULONG_MAX;
103 kexec_segment_size = ALIGN(ima_get_binary_runtime_size() +
104 PAGE_SIZE / 2, PAGE_SIZE);
105 if ((kexec_segment_size == ULONG_MAX) ||
106 ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages() / 2)) {
107 pr_err("Binary measurement list too large.\n");
111 ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
114 pr_err("Not enough memory for the kexec measurement buffer.\n");
118 kbuf.buffer = kexec_buffer;
119 kbuf.bufsz = kexec_buffer_size;
120 kbuf.memsz = kexec_segment_size;
121 ret = kexec_add_buffer(&kbuf);
123 pr_err("Error passing over kexec measurement buffer.\n");
128 image->ima_buffer_addr = kbuf.mem;
129 image->ima_buffer_size = kexec_segment_size;
130 image->ima_buffer = kexec_buffer;
132 pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
135 #endif /* IMA_KEXEC */
138 * Restore the measurement list from the previous kernel.
140 void ima_load_kexec_buffer(void)
142 void *kexec_buffer = NULL;
143 size_t kexec_buffer_size = 0;
146 rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size);
149 rc = ima_restore_measurement_list(kexec_buffer_size,
152 pr_err("Failed to restore the measurement list: %d\n",
155 ima_free_kexec_buffer();
158 pr_debug("Restoring the measurement list not supported\n");
161 pr_debug("No measurement list to restore\n");
164 pr_debug("Error restoring the measurement list: %d\n", rc);