1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 IBM Corporation
6 * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>
7 * Mimi Zohar <zohar@linux.vnet.ibm.com>
10 #include <linux/seq_file.h>
11 #include <linux/vmalloc.h>
12 #include <linux/kexec.h>
15 #ifdef CONFIG_IMA_KEXEC
16 static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer,
17 unsigned long segment_size)
19 struct ima_queue_entry *qe;
21 struct ima_kexec_hdr khdr;
24 /* segment size can't change between kexec load and execute */
25 file.buf = vmalloc(segment_size);
31 file.size = segment_size;
33 file.count = sizeof(khdr); /* reserved space */
35 memset(&khdr, 0, sizeof(khdr));
37 list_for_each_entry_rcu(qe, &ima_measurements, later) {
38 if (file.count < file.size) {
40 ima_measurements_show(&file, qe);
51 * fill in reserved space with some buffer details
52 * (eg. version, buffer size, number of measurements)
54 khdr.buffer_size = file.count;
55 if (ima_canonical_fmt) {
56 khdr.version = cpu_to_le16(khdr.version);
57 khdr.count = cpu_to_le64(khdr.count);
58 khdr.buffer_size = cpu_to_le64(khdr.buffer_size);
60 memcpy(file.buf, &khdr, sizeof(khdr));
62 print_hex_dump(KERN_DEBUG, "ima dump: ", DUMP_PREFIX_NONE,
64 file.count < 100 ? file.count : 100, true);
66 *buffer_size = file.count;
75 * Called during kexec_file_load so that IMA can add a segment to the kexec
76 * image for the measurement list for the next kernel.
78 * This function assumes that kexec_mutex is held.
80 void ima_add_kexec_buffer(struct kimage *image)
82 struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE,
83 .buf_min = 0, .buf_max = ULONG_MAX,
85 unsigned long binary_runtime_size;
87 /* use more understandable variable names than defined in kbuf */
88 void *kexec_buffer = NULL;
89 size_t kexec_buffer_size;
90 size_t kexec_segment_size;
94 * Reserve an extra half page of memory for additional measurements
95 * added during the kexec load.
97 binary_runtime_size = ima_get_binary_runtime_size();
98 if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE)
99 kexec_segment_size = ULONG_MAX;
101 kexec_segment_size = ALIGN(ima_get_binary_runtime_size() +
102 PAGE_SIZE / 2, PAGE_SIZE);
103 if ((kexec_segment_size == ULONG_MAX) ||
104 ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages() / 2)) {
105 pr_err("Binary measurement list too large.\n");
109 ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
112 pr_err("Not enough memory for the kexec measurement buffer.\n");
116 kbuf.buffer = kexec_buffer;
117 kbuf.bufsz = kexec_buffer_size;
118 kbuf.memsz = kexec_segment_size;
119 ret = kexec_add_buffer(&kbuf);
121 pr_err("Error passing over kexec measurement buffer.\n");
125 ret = arch_ima_add_kexec_buffer(image, kbuf.mem, kexec_segment_size);
127 pr_err("Error passing over kexec measurement buffer.\n");
131 pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
134 #endif /* IMA_KEXEC */
137 * Restore the measurement list from the previous kernel.
139 void ima_load_kexec_buffer(void)
141 void *kexec_buffer = NULL;
142 size_t kexec_buffer_size = 0;
145 rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size);
148 rc = ima_restore_measurement_list(kexec_buffer_size,
151 pr_err("Failed to restore the measurement list: %d\n",
154 ima_free_kexec_buffer();
157 pr_debug("Restoring the measurement list not supported\n");
160 pr_debug("No measurement list to restore\n");
163 pr_debug("Error restoring the measurement list: %d\n", rc);