GNU Linux-libre 4.14.290-gnu1
[releases.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         int ret = 0;
26
27         /* segment size can't change between kexec load and execute */
28         file.buf = vmalloc(segment_size);
29         if (!file.buf) {
30                 ret = -ENOMEM;
31                 goto out;
32         }
33
34         file.size = segment_size;
35         file.read_pos = 0;
36         file.count = sizeof(khdr);      /* reserved space */
37
38         memset(&khdr, 0, sizeof(khdr));
39         khdr.version = 1;
40         list_for_each_entry_rcu(qe, &ima_measurements, later) {
41                 if (file.count < file.size) {
42                         khdr.count++;
43                         ima_measurements_show(&file, qe);
44                 } else {
45                         ret = -EINVAL;
46                         break;
47                 }
48         }
49
50         if (ret < 0)
51                 goto out;
52
53         /*
54          * fill in reserved space with some buffer details
55          * (eg. version, buffer size, number of measurements)
56          */
57         khdr.buffer_size = file.count;
58         if (ima_canonical_fmt) {
59                 khdr.version = cpu_to_le16(khdr.version);
60                 khdr.count = cpu_to_le64(khdr.count);
61                 khdr.buffer_size = cpu_to_le64(khdr.buffer_size);
62         }
63         memcpy(file.buf, &khdr, sizeof(khdr));
64
65         print_hex_dump(KERN_DEBUG, "ima dump: ", DUMP_PREFIX_NONE,
66                         16, 1, file.buf,
67                         file.count < 100 ? file.count : 100, true);
68
69         *buffer_size = file.count;
70         *buffer = file.buf;
71 out:
72         if (ret == -EINVAL)
73                 vfree(file.buf);
74         return ret;
75 }
76
77 /*
78  * Called during kexec_file_load so that IMA can add a segment to the kexec
79  * image for the measurement list for the next kernel.
80  *
81  * This function assumes that kexec_mutex is held.
82  */
83 void ima_add_kexec_buffer(struct kimage *image)
84 {
85         struct kexec_buf kbuf = { .image = image, .buf_align = PAGE_SIZE,
86                                   .buf_min = 0, .buf_max = ULONG_MAX,
87                                   .top_down = true };
88         unsigned long binary_runtime_size;
89
90         /* use more understandable variable names than defined in kbuf */
91         void *kexec_buffer = NULL;
92         size_t kexec_buffer_size;
93         size_t kexec_segment_size;
94         int ret;
95
96         /*
97          * Reserve an extra half page of memory for additional measurements
98          * added during the kexec load.
99          */
100         binary_runtime_size = ima_get_binary_runtime_size();
101         if (binary_runtime_size >= ULONG_MAX - PAGE_SIZE)
102                 kexec_segment_size = ULONG_MAX;
103         else
104                 kexec_segment_size = ALIGN(ima_get_binary_runtime_size() +
105                                            PAGE_SIZE / 2, PAGE_SIZE);
106         if ((kexec_segment_size == ULONG_MAX) ||
107             ((kexec_segment_size >> PAGE_SHIFT) > totalram_pages / 2)) {
108                 pr_err("Binary measurement list too large.\n");
109                 return;
110         }
111
112         ima_dump_measurement_list(&kexec_buffer_size, &kexec_buffer,
113                                   kexec_segment_size);
114         if (!kexec_buffer) {
115                 pr_err("Not enough memory for the kexec measurement buffer.\n");
116                 return;
117         }
118
119         kbuf.buffer = kexec_buffer;
120         kbuf.bufsz = kexec_buffer_size;
121         kbuf.memsz = kexec_segment_size;
122         ret = kexec_add_buffer(&kbuf);
123         if (ret) {
124                 pr_err("Error passing over kexec measurement buffer.\n");
125                 vfree(kexec_buffer);
126                 return;
127         }
128
129         ret = arch_ima_add_kexec_buffer(image, kbuf.mem, kexec_segment_size);
130         if (ret) {
131                 pr_err("Error passing over kexec measurement buffer.\n");
132                 return;
133         }
134
135         image->ima_buffer = kexec_buffer;
136
137         pr_debug("kexec measurement buffer for the loaded kernel at 0x%lx.\n",
138                  kbuf.mem);
139 }
140 #endif /* IMA_KEXEC */
141
142 /*
143  * Restore the measurement list from the previous kernel.
144  */
145 void ima_load_kexec_buffer(void)
146 {
147         void *kexec_buffer = NULL;
148         size_t kexec_buffer_size = 0;
149         int rc;
150
151         rc = ima_get_kexec_buffer(&kexec_buffer, &kexec_buffer_size);
152         switch (rc) {
153         case 0:
154                 rc = ima_restore_measurement_list(kexec_buffer_size,
155                                                   kexec_buffer);
156                 if (rc != 0)
157                         pr_err("Failed to restore the measurement list: %d\n",
158                                 rc);
159
160                 ima_free_kexec_buffer();
161                 break;
162         case -ENOTSUPP:
163                 pr_debug("Restoring the measurement list not supported\n");
164                 break;
165         case -ENOENT:
166                 pr_debug("No measurement list to restore\n");
167                 break;
168         default:
169                 pr_debug("Error restoring the measurement list: %d\n", rc);
170         }
171 }