Skip to content

Commit 7b8589c

Browse files
Mimi Zohartorvalds
authored andcommitted
ima: on soft reboot, save the measurement list
The TPM PCRs are only reset on a hard reboot. In order to validate a TPM's quote after a soft reboot (eg. kexec -e), the IMA measurement list of the running kernel must be saved and restored on boot. This patch uses the kexec buffer passing mechanism to pass the serialized IMA binary_runtime_measurements to the next kernel. Link: http://lkml.kernel.org/r/1480554346-29071-7-git-send-email-zohar@linux.vnet.ibm.com Signed-off-by: Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com> Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com> Acked-by: "Eric W. Biederman" <ebiederm@xmission.com> Acked-by: Dmitry Kasatkin <dmitry.kasatkin@gmail.com> Cc: Andreas Steffen <andreas.steffen@strongswan.org> Cc: Josh Sklar <sklar@linux.vnet.ibm.com> Cc: Dave Young <dyoung@redhat.com> Cc: Vivek Goyal <vgoyal@redhat.com> Cc: Baoquan He <bhe@redhat.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Stewart Smith <stewart@linux.vnet.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ab6b1d1 commit 7b8589c

File tree

5 files changed

+135
-1
lines changed

5 files changed

+135
-1
lines changed

include/linux/ima.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LINUX_IMA_H
1212

1313
#include <linux/fs.h>
14+
#include <linux/kexec.h>
1415
struct linux_binprm;
1516

1617
#ifdef CONFIG_IMA
@@ -23,6 +24,10 @@ extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
2324
enum kernel_read_file_id id);
2425
extern void ima_post_path_mknod(struct dentry *dentry);
2526

27+
#ifdef CONFIG_IMA_KEXEC
28+
extern void ima_add_kexec_buffer(struct kimage *image);
29+
#endif
30+
2631
#else
2732
static inline int ima_bprm_check(struct linux_binprm *bprm)
2833
{
@@ -62,6 +67,13 @@ static inline void ima_post_path_mknod(struct dentry *dentry)
6267

6368
#endif /* CONFIG_IMA */
6469

70+
#ifndef CONFIG_IMA_KEXEC
71+
struct kimage;
72+
73+
static inline void ima_add_kexec_buffer(struct kimage *image)
74+
{}
75+
#endif
76+
6577
#ifdef CONFIG_IMA_APPRAISE
6678
extern void ima_inode_post_setattr(struct dentry *dentry);
6779
extern int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name,

kernel/kexec_file.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/mutex.h>
2020
#include <linux/list.h>
2121
#include <linux/fs.h>
22+
#include <linux/ima.h>
2223
#include <crypto/hash.h>
2324
#include <crypto/sha.h>
2425
#include <linux/syscalls.h>
@@ -132,6 +133,9 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,
132133
return ret;
133134
image->kernel_buf_len = size;
134135

136+
/* IMA needs to pass the measurement list to the next kernel. */
137+
ima_add_kexec_buffer(image);
138+
135139
/* Call arch image probe handlers */
136140
ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,
137141
image->kernel_buf_len);

security/integrity/ima/ima.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ void ima_print_digest(struct seq_file *m, u8 *digest, u32 size);
143143
struct ima_template_desc *ima_template_desc_current(void);
144144
int ima_restore_measurement_entry(struct ima_template_entry *entry);
145145
int ima_restore_measurement_list(loff_t bufsize, void *buf);
146+
int ima_measurements_show(struct seq_file *m, void *v);
146147
unsigned long ima_get_binary_runtime_size(void);
147148
int ima_init_template(void);
148149

security/integrity/ima/ima_fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void ima_putc(struct seq_file *m, void *data, int datalen)
116116
* [eventdata length]
117117
* eventdata[n]=template specific data
118118
*/
119-
static int ima_measurements_show(struct seq_file *m, void *v)
119+
int ima_measurements_show(struct seq_file *m, void *v)
120120
{
121121
/* the list never shrinks, so we don't need a lock here */
122122
struct ima_queue_entry *qe = v;

security/integrity/ima/ima_kexec.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,125 @@
1010
* the Free Software Foundation; either version 2 of the License, or
1111
* (at your option) any later version.
1212
*/
13+
#include <linux/seq_file.h>
14+
#include <linux/vmalloc.h>
15+
#include <linux/kexec.h>
1316
#include "ima.h"
1417

18+
#ifdef CONFIG_IMA_KEXEC
19+
static int ima_dump_measurement_list(unsigned long *buffer_size, void **buffer,
20+
unsigned long segment_size)
21+
{
22+
struct ima_queue_entry *qe;
23+
struct seq_file file;
24+
struct ima_kexec_hdr khdr = {
25+
.version = 1, .buffer_size = 0, .count = 0};
26+
int ret = 0;
27+
28+
/* segment size can't change between kexec load and execute */
29+
file.buf = vmalloc(segment_size);
30+
if (!file.buf) {
31+
ret = -ENOMEM;
32+
goto out;
33+
}
34+
35+
file.size = segment_size;
36+
file.read_pos = 0;
37+
file.count = sizeof(khdr); /* reserved space */
38+
39+
list_for_each_entry_rcu(qe, &ima_measurements, later) {
40+
if (file.count < file.size) {
41+
khdr.count++;
42+
ima_measurements_show(&file, qe);
43+
} else {
44+
ret = -EINVAL;
45+
break;
46+
}
47+
}
48+
49+
if (ret < 0)
50+
goto out;
51+
52+
/*
53+
* fill in reserved space with some buffer details
54+
* (eg. version, buffer size, number of measurements)
55+
*/
56+
khdr.buffer_size = file.count;
57+
memcpy(file.buf, &khdr, sizeof(khdr));
58+
print_hex_dump(KERN_DEBUG, "ima dump: ", DUMP_PREFIX_NONE,
59+
16, 1, file.buf,
60+
file.count < 100 ? file.count : 100, true);
61+
62+
*buffer_size = file.count;
63+
*buffer = file.buf;
64+
out:
65+
if (ret == -EINVAL)
66+
vfree(file.buf);
67+
return ret;
68+
}
69+
70+
/*
71+
* Called during kexec_file_load so that IMA can add a segment to the kexec
72+
* image for the measurement list for the next kernel.
73+
*
74+
* This function assumes that kexec_mutex is held.
75+
*/
76+
void ima_add_kexec_buffer(struct kimage *image)
77+
{
78+
struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE,
79+
.buf_min = 0, .buf_max = ULONG_MAX,
80+
.top_down = true };
81+
unsigned long binary_runtime_size;
82+
83+
/* use more understandable variable names than defined in kbuf */
84+
void *kexec_buffer = NULL;
85+
size_t kexec_buffer_size;
86+
size_t kexec_segment_size;
87+
int ret;
88+
89+
/*
90+
* Reserve an extra half page of memory for additional measurements
91+
* added during the kexec load.
92+
*/
93+
binary_runtime_size = ima_get_binary_runtime_size();
94+
if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE)
95+
kexec_segment_size = ULONG_MAX;
96+
else
97+
kexec_segment_size = ALIGN(ima_get_binary_runtime_size() +
98+
PAGE_SIZE / 2, PAGE_SIZE);
99+
if ((kexec_segment_size == ULONG_MAX) ||
100+
((kexec_segment_size >> PAGE_SHIFT) > totalram_pages / 2)) {
101+
pr_err("Binary measurement list too large.\n");
102+
return;
103+
}
104+
105+
ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
106+
kexec_segment_size);
107+
if (!kexec_buffer) {
108+
pr_err("Not enough memory for the kexec measurement buffer.\n");
109+
return;
110+
}
111+
112+
kbuf.buffer = kexec_buffer;
113+
kbuf.bufsz = kexec_buffer_size;
114+
kbuf.memsz = kexec_segment_size;
115+
ret = kexec_add_buffer(&kbuf);
116+
if (ret) {
117+
pr_err("Error passing over kexec measurement buffer.\n");
118+
return;
119+
}
120+
121+
ret = arch_ima_add_kexec_buffer(image, kbuf.mem, kexec_segment_size);
122+
if (ret) {
123+
pr_err("Error passing over kexec measurement buffer.\n");
124+
return;
125+
}
126+
127+
pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
128+
kbuf.mem);
129+
}
130+
#endif /* IMA_KEXEC */
131+
15132
/*
16133
* Restore the measurement list from the previous kernel.
17134
*/

0 commit comments

Comments
 (0)