2019-05-27 08:55:01 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-12-19 16:22:35 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 IBM Corporation
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>
|
|
|
|
* Mimi Zohar <zohar@linux.vnet.ibm.com>
|
|
|
|
*/
|
2018-04-24 16:30:38 +02:00
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/kexec.h>
|
2021-02-21 09:49:27 -08:00
|
|
|
#include <linux/of.h>
|
2021-06-10 10:15:52 -07:00
|
|
|
#include <linux/ima.h>
|
2025-04-21 15:25:11 -07:00
|
|
|
#include <linux/reboot.h>
|
|
|
|
#include <asm/page.h>
|
2016-12-19 16:22:35 -08:00
|
|
|
#include "ima.h"
|
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
#ifdef CONFIG_IMA_KEXEC
|
ima: measure kexec load and exec events as critical data
The amount of memory allocated at kexec load, even with the extra memory
allocated, might not be large enough for the entire measurement list. The
indeterminate interval between kexec 'load' and 'execute' could exacerbate
this problem.
Define two new IMA events, 'kexec_load' and 'kexec_execute', to be
measured as critical data at kexec 'load' and 'execute' respectively.
Report the allocated kexec segment size, IMA binary log size and the
runtime measurements count as part of those events.
These events, and the values reported through them, serve as markers in
the IMA log to verify the IMA events are captured during kexec soft
reboot. The presence of a 'kexec_load' event in between the last two
'boot_aggregate' events in the IMA log implies this is a kexec soft
reboot, and not a cold-boot. And the absence of 'kexec_execute' event
after kexec soft reboot implies missing events in that window which
results in inconsistency with TPM PCR quotes, necessitating a cold boot
for a successful remote attestation.
These critical data events are displayed as hex encoded ascii in the
ascii_runtime_measurement_list. Verifying the critical data hash requires
calculating the hash of the decoded ascii string.
For example, to verify the 'kexec_load' data hash:
sudo cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements
| grep kexec_load | cut -d' ' -f 6 | xxd -r -p | sha256sum
To verify the 'kexec_execute' data hash:
sudo cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements
| grep kexec_execute | cut -d' ' -f 6 | xxd -r -p | sha256sum
Co-developed-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: Steven Chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Acked-by: Baoquan He <bhe@redhat.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com> # ppc64/kvm
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2025-04-21 15:25:15 -07:00
|
|
|
#define IMA_KEXEC_EVENT_LEN 256
|
|
|
|
|
2025-04-21 15:25:11 -07:00
|
|
|
static bool ima_kexec_update_registered;
|
2025-04-21 15:25:08 -07:00
|
|
|
static struct seq_file ima_kexec_file;
|
2025-04-21 15:25:12 -07:00
|
|
|
static size_t kexec_segment_size;
|
2025-04-21 15:25:11 -07:00
|
|
|
static void *ima_kexec_buffer;
|
2025-04-21 15:25:08 -07:00
|
|
|
|
|
|
|
static void ima_free_kexec_file_buf(struct seq_file *sf)
|
|
|
|
{
|
|
|
|
vfree(sf->buf);
|
|
|
|
sf->buf = NULL;
|
|
|
|
sf->size = 0;
|
|
|
|
sf->read_pos = 0;
|
|
|
|
sf->count = 0;
|
|
|
|
}
|
|
|
|
|
ima: measure kexec load and exec events as critical data
The amount of memory allocated at kexec load, even with the extra memory
allocated, might not be large enough for the entire measurement list. The
indeterminate interval between kexec 'load' and 'execute' could exacerbate
this problem.
Define two new IMA events, 'kexec_load' and 'kexec_execute', to be
measured as critical data at kexec 'load' and 'execute' respectively.
Report the allocated kexec segment size, IMA binary log size and the
runtime measurements count as part of those events.
These events, and the values reported through them, serve as markers in
the IMA log to verify the IMA events are captured during kexec soft
reboot. The presence of a 'kexec_load' event in between the last two
'boot_aggregate' events in the IMA log implies this is a kexec soft
reboot, and not a cold-boot. And the absence of 'kexec_execute' event
after kexec soft reboot implies missing events in that window which
results in inconsistency with TPM PCR quotes, necessitating a cold boot
for a successful remote attestation.
These critical data events are displayed as hex encoded ascii in the
ascii_runtime_measurement_list. Verifying the critical data hash requires
calculating the hash of the decoded ascii string.
For example, to verify the 'kexec_load' data hash:
sudo cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements
| grep kexec_load | cut -d' ' -f 6 | xxd -r -p | sha256sum
To verify the 'kexec_execute' data hash:
sudo cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements
| grep kexec_execute | cut -d' ' -f 6 | xxd -r -p | sha256sum
Co-developed-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: Steven Chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Acked-by: Baoquan He <bhe@redhat.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com> # ppc64/kvm
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2025-04-21 15:25:15 -07:00
|
|
|
void ima_measure_kexec_event(const char *event_name)
|
|
|
|
{
|
|
|
|
char ima_kexec_event[IMA_KEXEC_EVENT_LEN];
|
|
|
|
size_t buf_size = 0;
|
|
|
|
long len;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
buf_size = ima_get_binary_runtime_size();
|
|
|
|
len = atomic_long_read(&ima_htable.len);
|
|
|
|
|
|
|
|
n = scnprintf(ima_kexec_event, IMA_KEXEC_EVENT_LEN,
|
|
|
|
"kexec_segment_size=%lu;ima_binary_runtime_size=%lu;"
|
|
|
|
"ima_runtime_measurements_count=%ld;",
|
|
|
|
kexec_segment_size, buf_size, len);
|
|
|
|
|
|
|
|
ima_measure_critical_data("ima_kexec", event_name, ima_kexec_event, n, false, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2025-04-21 15:25:08 -07:00
|
|
|
static int ima_alloc_kexec_file_buf(size_t segment_size)
|
|
|
|
{
|
2025-04-21 15:25:13 -07:00
|
|
|
/*
|
|
|
|
* kexec 'load' may be called multiple times.
|
|
|
|
* Free and realloc the buffer only if the segment_size is
|
|
|
|
* changed from the previous kexec 'load' call.
|
|
|
|
*/
|
|
|
|
if (ima_kexec_file.buf && ima_kexec_file.size == segment_size)
|
|
|
|
goto out;
|
|
|
|
|
2025-04-21 15:25:08 -07:00
|
|
|
ima_free_kexec_file_buf(&ima_kexec_file);
|
|
|
|
|
|
|
|
/* segment size can't change between kexec load and execute */
|
|
|
|
ima_kexec_file.buf = vmalloc(segment_size);
|
|
|
|
if (!ima_kexec_file.buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ima_kexec_file.size = segment_size;
|
2025-04-21 15:25:13 -07:00
|
|
|
|
|
|
|
out:
|
2025-04-21 15:25:08 -07:00
|
|
|
ima_kexec_file.read_pos = 0;
|
|
|
|
ima_kexec_file.count = sizeof(struct ima_kexec_hdr); /* reserved space */
|
ima: measure kexec load and exec events as critical data
The amount of memory allocated at kexec load, even with the extra memory
allocated, might not be large enough for the entire measurement list. The
indeterminate interval between kexec 'load' and 'execute' could exacerbate
this problem.
Define two new IMA events, 'kexec_load' and 'kexec_execute', to be
measured as critical data at kexec 'load' and 'execute' respectively.
Report the allocated kexec segment size, IMA binary log size and the
runtime measurements count as part of those events.
These events, and the values reported through them, serve as markers in
the IMA log to verify the IMA events are captured during kexec soft
reboot. The presence of a 'kexec_load' event in between the last two
'boot_aggregate' events in the IMA log implies this is a kexec soft
reboot, and not a cold-boot. And the absence of 'kexec_execute' event
after kexec soft reboot implies missing events in that window which
results in inconsistency with TPM PCR quotes, necessitating a cold boot
for a successful remote attestation.
These critical data events are displayed as hex encoded ascii in the
ascii_runtime_measurement_list. Verifying the critical data hash requires
calculating the hash of the decoded ascii string.
For example, to verify the 'kexec_load' data hash:
sudo cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements
| grep kexec_load | cut -d' ' -f 6 | xxd -r -p | sha256sum
To verify the 'kexec_execute' data hash:
sudo cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements
| grep kexec_execute | cut -d' ' -f 6 | xxd -r -p | sha256sum
Co-developed-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: Tushar Sugandhi <tusharsu@linux.microsoft.com>
Signed-off-by: Steven Chen <chenste@linux.microsoft.com>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Acked-by: Baoquan He <bhe@redhat.com>
Tested-by: Stefan Berger <stefanb@linux.ibm.com> # ppc64/kvm
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
2025-04-21 15:25:15 -07:00
|
|
|
ima_measure_kexec_event("kexec_load");
|
2025-04-21 15:25:08 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer,
|
|
|
|
unsigned long segment_size)
|
|
|
|
{
|
|
|
|
struct ima_queue_entry *qe;
|
2016-12-19 16:22:57 -08:00
|
|
|
struct ima_kexec_hdr khdr;
|
2016-12-19 16:22:48 -08:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* segment size can't change between kexec load and execute */
|
2025-04-21 15:25:07 -07:00
|
|
|
if (!ima_kexec_file.buf) {
|
2025-04-21 15:25:08 -07:00
|
|
|
pr_err("Kexec file buf not allocated\n");
|
|
|
|
return -EINVAL;
|
2016-12-19 16:22:48 -08:00
|
|
|
}
|
|
|
|
|
2016-12-19 16:22:57 -08:00
|
|
|
memset(&khdr, 0, sizeof(khdr));
|
|
|
|
khdr.version = 1;
|
2024-11-21 01:57:12 -08:00
|
|
|
/* This is an append-only list, no need to hold the RCU read lock */
|
|
|
|
list_for_each_entry_rcu(qe, &ima_measurements, later, true) {
|
2025-04-21 15:25:07 -07:00
|
|
|
if (ima_kexec_file.count < ima_kexec_file.size) {
|
2016-12-19 16:22:48 -08:00
|
|
|
khdr.count++;
|
2025-04-21 15:25:07 -07:00
|
|
|
ima_measurements_show(&ima_kexec_file, qe);
|
2016-12-19 16:22:48 -08:00
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fill in reserved space with some buffer details
|
|
|
|
* (eg. version, buffer size, number of measurements)
|
|
|
|
*/
|
2025-04-21 15:25:07 -07:00
|
|
|
khdr.buffer_size = ima_kexec_file.count;
|
2016-12-19 16:22:57 -08:00
|
|
|
if (ima_canonical_fmt) {
|
|
|
|
khdr.version = cpu_to_le16(khdr.version);
|
|
|
|
khdr.count = cpu_to_le64(khdr.count);
|
|
|
|
khdr.buffer_size = cpu_to_le64(khdr.buffer_size);
|
|
|
|
}
|
2025-04-21 15:25:07 -07:00
|
|
|
memcpy(ima_kexec_file.buf, &khdr, sizeof(khdr));
|
2016-12-19 16:22:57 -08:00
|
|
|
|
2021-12-28 23:03:03 -03:00
|
|
|
print_hex_dump_debug("ima dump: ", DUMP_PREFIX_NONE, 16, 1,
|
2025-04-21 15:25:07 -07:00
|
|
|
ima_kexec_file.buf, ima_kexec_file.count < 100 ?
|
|
|
|
ima_kexec_file.count : 100,
|
2021-12-28 23:03:03 -03:00
|
|
|
true);
|
2016-12-19 16:22:48 -08:00
|
|
|
|
2025-04-21 15:25:07 -07:00
|
|
|
*buffer_size = ima_kexec_file.count;
|
|
|
|
*buffer = ima_kexec_file.buf;
|
2025-04-21 15:25:12 -07:00
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called during kexec_file_load so that IMA can add a segment to the kexec
|
|
|
|
* image for the measurement list for the next kernel.
|
|
|
|
*
|
2023-08-07 10:52:06 +08:00
|
|
|
* This function assumes that kexec_lock is held.
|
2016-12-19 16:22:48 -08:00
|
|
|
*/
|
|
|
|
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 };
|
|
|
|
unsigned long binary_runtime_size;
|
2025-04-21 15:25:14 -07:00
|
|
|
unsigned long extra_memory;
|
2016-12-19 16:22:48 -08:00
|
|
|
|
|
|
|
/* use more understandable variable names than defined in kbuf */
|
2025-04-21 15:25:12 -07:00
|
|
|
size_t kexec_buffer_size = 0;
|
2016-12-19 16:22:48 -08:00
|
|
|
void *kexec_buffer = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2025-05-13 07:31:29 -07:00
|
|
|
if (image->type == KEXEC_TYPE_CRASH)
|
|
|
|
return;
|
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
/*
|
2025-04-21 15:25:14 -07:00
|
|
|
* Reserve extra memory for measurements added during kexec.
|
2016-12-19 16:22:48 -08:00
|
|
|
*/
|
2025-04-21 15:25:14 -07:00
|
|
|
if (CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB <= 0)
|
|
|
|
extra_memory = PAGE_SIZE / 2;
|
|
|
|
else
|
|
|
|
extra_memory = CONFIG_IMA_KEXEC_EXTRA_MEMORY_KB * 1024;
|
|
|
|
|
|
|
|
binary_runtime_size = ima_get_binary_runtime_size() + extra_memory;
|
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE)
|
|
|
|
kexec_segment_size = ULONG_MAX;
|
|
|
|
else
|
2025-04-21 15:25:14 -07:00
|
|
|
kexec_segment_size = ALIGN(binary_runtime_size, PAGE_SIZE);
|
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
if ((kexec_segment_size == ULONG_MAX) ||
|
2018-12-28 00:34:29 -08:00
|
|
|
((kexec_segment_size >> PAGE_SHIFT) > totalram_pages() / 2)) {
|
2016-12-19 16:22:48 -08:00
|
|
|
pr_err("Binary measurement list too large.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-04-21 15:25:08 -07:00
|
|
|
ret = ima_alloc_kexec_file_buf(kexec_segment_size);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("Not enough memory for the kexec measurement buffer.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
kbuf.buffer = kexec_buffer;
|
|
|
|
kbuf.bufsz = kexec_buffer_size;
|
|
|
|
kbuf.memsz = kexec_segment_size;
|
2025-04-21 15:25:10 -07:00
|
|
|
image->is_ima_segment_index_set = false;
|
2016-12-19 16:22:48 -08:00
|
|
|
ret = kexec_add_buffer(&kbuf);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Error passing over kexec measurement buffer.\n");
|
2021-02-04 09:49:50 -08:00
|
|
|
vfree(kexec_buffer);
|
2016-12-19 16:22:48 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-21 09:49:25 -08:00
|
|
|
image->ima_buffer_addr = kbuf.mem;
|
|
|
|
image->ima_buffer_size = kexec_segment_size;
|
2021-02-04 09:49:51 -08:00
|
|
|
image->ima_buffer = kexec_buffer;
|
2025-04-21 15:25:10 -07:00
|
|
|
image->ima_segment_index = image->nr_segments - 1;
|
|
|
|
image->is_ima_segment_index_set = true;
|
2021-02-04 09:49:51 -08:00
|
|
|
|
2023-12-13 13:57:42 +08:00
|
|
|
kexec_dprintk("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
|
|
|
|
kbuf.mem);
|
2016-12-19 16:22:48 -08:00
|
|
|
}
|
2025-04-21 15:25:11 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called during kexec execute so that IMA can update the measurement list.
|
|
|
|
*/
|
|
|
|
static int ima_update_kexec_buffer(struct notifier_block *self,
|
|
|
|
unsigned long action, void *data)
|
|
|
|
{
|
2025-04-21 15:25:12 -07:00
|
|
|
size_t buf_size = 0;
|
|
|
|
int ret = NOTIFY_OK;
|
|
|
|
void *buf = NULL;
|
|
|
|
|
|
|
|
if (!kexec_in_progress) {
|
|
|
|
pr_info("No kexec in progress.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ima_kexec_buffer) {
|
|
|
|
pr_err("Kexec buffer not set.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ima_dump_measurement_list(&buf_size, &buf, kexec_segment_size);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
pr_err("Dump measurements failed. Error:%d\n", ret);
|
|
|
|
|
|
|
|
if (buf_size != 0)
|
|
|
|
memcpy(ima_kexec_buffer, buf, buf_size);
|
|
|
|
|
|
|
|
kimage_unmap_segment(ima_kexec_buffer);
|
|
|
|
ima_kexec_buffer = NULL;
|
|
|
|
|
|
|
|
return ret;
|
2025-04-21 15:25:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block update_buffer_nb = {
|
|
|
|
.notifier_call = ima_update_kexec_buffer,
|
|
|
|
.priority = INT_MIN
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a mapping for the source pages that contain the IMA buffer
|
|
|
|
* so we can update it later.
|
|
|
|
*/
|
|
|
|
void ima_kexec_post_load(struct kimage *image)
|
|
|
|
{
|
|
|
|
if (ima_kexec_buffer) {
|
|
|
|
kimage_unmap_segment(ima_kexec_buffer);
|
|
|
|
ima_kexec_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!image->ima_buffer_addr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ima_kexec_buffer = kimage_map_segment(image,
|
|
|
|
image->ima_buffer_addr,
|
|
|
|
image->ima_buffer_size);
|
|
|
|
if (!ima_kexec_buffer) {
|
|
|
|
pr_err("Could not map measurements buffer.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ima_kexec_update_registered) {
|
|
|
|
register_reboot_notifier(&update_buffer_nb);
|
|
|
|
ima_kexec_update_registered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 16:22:48 -08:00
|
|
|
#endif /* IMA_KEXEC */
|
|
|
|
|
2016-12-19 16:22:35 -08:00
|
|
|
/*
|
|
|
|
* Restore the measurement list from the previous kernel.
|
|
|
|
*/
|
2022-06-30 08:36:12 +00:00
|
|
|
void __init ima_load_kexec_buffer(void)
|
2016-12-19 16:22:35 -08:00
|
|
|
{
|
|
|
|
void *kexec_buffer = NULL;
|
|
|
|
size_t kexec_buffer_size = 0;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size);
|
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
|
|
|
rc = ima_restore_measurement_list(kexec_buffer_size,
|
|
|
|
kexec_buffer);
|
|
|
|
if (rc != 0)
|
|
|
|
pr_err("Failed to restore the measurement list: %d\n",
|
|
|
|
rc);
|
|
|
|
|
|
|
|
ima_free_kexec_buffer();
|
|
|
|
break;
|
|
|
|
case -ENOTSUPP:
|
|
|
|
pr_debug("Restoring the measurement list not supported\n");
|
|
|
|
break;
|
|
|
|
case -ENOENT:
|
|
|
|
pr_debug("No measurement list to restore\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pr_debug("Error restoring the measurement list: %d\n", rc);
|
|
|
|
}
|
|
|
|
}
|