[PATCH v2 3/3] ima: clear kexec state when replacing file image
Jérémy Jean
Jeremy.Jean at oss.cyber.gouv.fr
Thu Nov 12 04:52:47 PST 2026
ima_kexec_buffer remains mapped to the IMA segment of the installed
kexec_file_load image. KEXEC_FILE_UNLOAD and a successful normal
kexec_load can replace and free that image without clearing the mapping.
Executing a later kexec_load image can then write the measurement list
into the freed pages. The new image has an IMA segment size of zero, but
size does not establish ownership of the mapping.
Record the image whose segment is mapped and verify it before the
execute-time update. Clear the mapping and scratch state under kexec_lock
when a normal image is removed without another file image taking its
place.
Fixes: 9f0ec4b16f2b ("ima: kexec: move IMA log copy from kexec load to execute")
Cc: stable at vger.kernel.org
Signed-off-by: Jérémy Jean <Jeremy.Jean at oss.cyber.gouv.fr>
---
include/linux/ima.h | 1 +
kernel/kexec.c | 11 +++++++++++
kernel/kexec_file.c | 7 ++++++-
security/integrity/ima/ima_kexec.c | 24 ++++++++++++++++++++++++
4 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/include/linux/ima.h b/include/linux/ima.h
index 8e08baf16c2f..d61b7b7756b7 100644
--- a/include/linux/ima.h
+++ b/include/linux/ima.h
@@ -34,6 +34,7 @@ static inline void ima_appraise_parse_cmdline(void) {}
#ifdef CONFIG_IMA_KEXEC
extern void ima_add_kexec_buffer(struct kimage *image);
extern void ima_kexec_post_load(struct kimage *image);
+void ima_kexec_cleanup(void);
#else
static inline void ima_kexec_post_load(struct kimage *image) {}
#endif
diff --git a/kernel/kexec.c b/kernel/kexec.c
index 90756dc6339b..297f4657869b 100644
--- a/kernel/kexec.c
+++ b/kernel/kexec.c
@@ -11,6 +11,7 @@
#include <linux/file.h>
#include <linux/security.h>
#include <linux/kexec.h>
+#include <linux/ima.h>
#include <linux/mutex.h>
#include <linux/list.h>
#include <linux/syscalls.h>
@@ -114,6 +115,10 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
if (nr_segments == 0) {
/* Uninstall image */
+#ifdef CONFIG_IMA_KEXEC
+ if (!(flags & KEXEC_ON_CRASH))
+ ima_kexec_cleanup();
+#endif
kimage_free(xchg(dest_image, NULL));
ret = 0;
goto out_unlock;
@@ -163,6 +168,12 @@ static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
if (ret)
goto out;
+ /* A kexec_load image has no IMA measurement segment. */
+#ifdef CONFIG_IMA_KEXEC
+ if (!(flags & KEXEC_ON_CRASH))
+ ima_kexec_cleanup();
+#endif
+
/* Install the new kernel and uninstall the old */
image = xchg(dest_image, image);
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 59f9ba1b0cd7..95b3a9ab1e3b 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -401,8 +401,13 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
#endif
dest_image = &kexec_image;
- if (flags & KEXEC_FILE_UNLOAD)
+ if (flags & KEXEC_FILE_UNLOAD) {
+#ifdef CONFIG_IMA_KEXEC
+ if (image_type == KEXEC_TYPE_DEFAULT)
+ ima_kexec_cleanup();
+#endif
goto exchange;
+ }
/*
* In case of crash, new kernel gets loaded in reserved region. It is
diff --git a/security/integrity/ima/ima_kexec.c b/security/integrity/ima/ima_kexec.c
index 3aa8b1096f81..cc15aed8df48 100644
--- a/security/integrity/ima/ima_kexec.c
+++ b/security/integrity/ima/ima_kexec.c
@@ -25,6 +25,7 @@ static bool ima_kexec_update_registered;
static struct seq_file ima_kexec_file;
static size_t kexec_segment_size;
static void *ima_kexec_buffer;
+static struct kimage *ima_kexec_image;
static void ima_free_kexec_file_buf(struct seq_file *sf)
{
@@ -229,6 +230,11 @@ static int ima_update_kexec_buffer(struct notifier_block *self,
return ret;
}
+ if (ima_kexec_image != kexec_image) {
+ pr_err("Kexec buffer does not match current image.\n");
+ return ret;
+ }
+
ret = ima_dump_measurement_list(&buf_size, &buf, kexec_segment_size);
if (ret)
@@ -239,6 +245,7 @@ static int ima_update_kexec_buffer(struct notifier_block *self,
kimage_unmap_segment(ima_kexec_buffer);
ima_kexec_buffer = NULL;
+ ima_kexec_image = NULL;
return ret;
}
@@ -258,6 +265,7 @@ void ima_kexec_post_load(struct kimage *image)
kimage_unmap_segment(ima_kexec_buffer);
ima_kexec_buffer = NULL;
}
+ ima_kexec_image = image;
if (!image->ima_buffer_addr) {
kexec_segment_size = 0;
@@ -288,6 +296,22 @@ void ima_kexec_post_load(struct kimage *image)
}
}
+/*
+ * Drop the dump state before its kexec image is replaced.
+ *
+ * This function assumes that kexec_lock is held.
+ */
+void ima_kexec_cleanup(void)
+{
+ if (ima_kexec_buffer) {
+ kimage_unmap_segment(ima_kexec_buffer);
+ ima_kexec_buffer = NULL;
+ }
+ ima_kexec_image = NULL;
+ kexec_segment_size = 0;
+ ima_free_kexec_file_buf(&ima_kexec_file);
+}
+
#endif /* IMA_KEXEC */
/*
More information about the kexec
mailing list