]> asedeno.scripts.mit.edu Git - linux.git/blob - security/integrity/ima/ima_kexec.c
2c4824ac1ce14436343dcc1aa360b2184c30eb17
[linux.git] / security / integrity / ima / ima_kexec.c
1 /*
2  * Copyright (C) 2016 IBM Corporation
3  *
4  * Authors:
5  * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>
6  * Mimi Zohar <zohar@linux.vnet.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 #include <linux/seq_file.h>
14 #include <linux/vmalloc.h>
15 #include <linux/kexec.h>
16 #include "ima.h"
17
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
132 /*
133  * Restore the measurement list from the previous kernel.
134  */
135 void ima_load_kexec_buffer(void)
136 {
137         void *kexec_buffer = NULL;
138         size_t kexec_buffer_size = 0;
139         int rc;
140
141         rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size);
142         switch (rc) {
143         case 0:
144                 rc = ima_restore_measurement_list(kexec_buffer_size,
145                                                   kexec_buffer);
146                 if (rc != 0)
147                         pr_err("Failed to restore the measurement list: %d\n",
148                                 rc);
149
150                 ima_free_kexec_buffer();
151                 break;
152         case -ENOTSUPP:
153                 pr_debug("Restoring the measurement list not supported\n");
154                 break;
155         case -ENOENT:
156                 pr_debug("No measurement list to restore\n");
157                 break;
158         default:
159                 pr_debug("Error restoring the measurement list: %d\n", rc);
160         }
161 }