-
Notifications
You must be signed in to change notification settings - Fork 698
/
0003-Verify-SBAT-on-kexec.patch
382 lines (372 loc) · 9.77 KB
/
0003-Verify-SBAT-on-kexec.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
From 14af6bbafd771b48c507bfce3b7dc582d0b65c6f Mon Sep 17 00:00:00 2001
From: Alexey Makhalov <amakhalov@vmware.com>
Date: Mon, 9 Oct 2023 09:00:46 -0700
Subject: [PATCH 3/3] Verify SBAT on kexec
To extend Secure Boot trusted chain to next executed kernel.
Introduce a logic to verify .sbat section of new bzImage against
SBAT entries from EFI variable used by shim.
[ KN: Simplify SBAT verification. Only call routine when UEFI
secure boot is enabled. Get SBAT data from MOK tables if
cannot fetch from EFI. ]
Signed-off-by: Alexey Makhalov <amakhalov@vmware.com>
Signed-off-by: Kuntal Nayak <kuntal.nayak@broadcom.com>
---
crypto/asymmetric_keys/verify_pefile.c | 24 +++
include/linux/verification.h | 11 ++
kernel/kexec_file.c | 8 +
security/Kconfig | 12 ++
security/Makefile | 1 +
security/sbat.c | 233 +++++++++++++++++++++++++
6 files changed, 289 insertions(+)
create mode 100644 security/sbat.c
diff --git a/crypto/asymmetric_keys/verify_pefile.c b/crypto/asymmetric_keys/verify_pefile.c
index 22beaf221..7813de5cf 100644
--- a/crypto/asymmetric_keys/verify_pefile.c
+++ b/crypto/asymmetric_keys/verify_pefile.c
@@ -454,3 +454,27 @@ int verify_pefile_signature(const void *pebuf, unsigned pelen,
kfree_sensitive(ctx.digest);
return ret;
}
+
+int pefile_find_section(const char *section_name, const void *pebuf,
+ unsigned pelen, char **section, unsigned *len)
+{
+ struct pefile_context ctx;
+ int ret, i;
+
+ memset(&ctx, 0, sizeof(ctx));
+ ret = pefile_parse_binary(pebuf, pelen, &ctx);
+ if (ret < 0)
+ return ret;
+
+ for (i = 0; i < ctx.n_sections; i++) {
+ struct section_header s = ctx.secs[i];
+
+ if (!strcmp(section_name, s.name)) {
+ *section = (char *)pebuf + s.data_addr;
+ *len = s.raw_data_size;
+ return 0;
+ }
+ }
+
+ return -ENODATA;
+}
diff --git a/include/linux/verification.h b/include/linux/verification.h
index cb2d47f28..dfebedf56 100644
--- a/include/linux/verification.h
+++ b/include/linux/verification.h
@@ -67,6 +67,17 @@ extern int verify_pkcs7_message_sig(const void *data, size_t len,
extern int verify_pefile_signature(const void *pebuf, unsigned pelen,
struct key *trusted_keys,
enum key_being_used_for usage);
+extern int pefile_find_section(const char *section_name, const void *pebuf,
+ unsigned pelen, char **section, unsigned *len);
+#endif
+
+#ifdef CONFIG_SECURITY_SBAT_VERIFY
+extern int verify_pefile_sbat(const char *pebuf, unsigned long pelen);
+#else
+static inline int verify_pefile_sbat(const char *pebuf, unsigned long pelen)
+{
+ return 0;
+}
#endif
#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 6d787ae9d..9b673e555 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -27,6 +27,8 @@
#include <linux/kernel_read_file.h>
#include <linux/syscalls.h>
#include <linux/vmalloc.h>
+#include <linux/lockdown.h>
+
#include "kexec_internal.h"
#ifdef CONFIG_KEXEC_SIG
@@ -131,6 +133,12 @@ int kexec_kernel_verify_pe_sig(const char *kernel, unsigned long kernel_len)
{
int ret;
+ if (secureboot_enabled()) {
+ ret = verify_pefile_sbat(kernel, kernel_len);
+ if (ret)
+ return ret;
+ }
+
ret = verify_pefile_signature(kernel, kernel_len,
VERIFY_USE_SECONDARY_KEYRING,
VERIFYING_KEXEC_PE_SIGNATURE);
diff --git a/security/Kconfig b/security/Kconfig
index 536689678..371cc82d1 100644
--- a/security/Kconfig
+++ b/security/Kconfig
@@ -218,6 +218,18 @@ config SECURITY_SBAT
See <https://github.com/rhboot/shim/blob/main/SBAT.md> for more information
about SBAT.
+config SECURITY_SBAT_VERIFY
+ bool "Verify .sbat section of kexec bzImage"
+ depends on SECURITY
+ depends on SIGNED_PE_FILE_VERIFICATION
+ depends on EFI
+ help
+ If set, kexec will perform .sbat generations verification in addition to
+ image signature verification.
+
+ See <https://github.com/rhboot/shim/blob/main/SBAT.md> for more information
+ about SBAT.
+
source "security/landlock/Kconfig"
source "security/integrity/Kconfig"
diff --git a/security/Makefile b/security/Makefile
index 18121f8f8..38da41d50 100644
--- a/security/Makefile
+++ b/security/Makefile
@@ -12,6 +12,7 @@ obj-$(CONFIG_MMU) += min_addr.o
# Object file lists
obj-$(CONFIG_SECURITY) += security.o
obj-$(CONFIG_SECURITYFS) += inode.o
+obj-$(CONFIG_SECURITY_SBAT_VERIFY) += sbat.o
obj-$(CONFIG_SECURITY_SELINUX) += selinux/
obj-$(CONFIG_SECURITY_SMACK) += smack/
obj-$(CONFIG_SECURITY) += lsm_audit.o
diff --git a/security/sbat.c b/security/sbat.c
new file mode 100644
index 000000000..75571fc08
--- /dev/null
+++ b/security/sbat.c
@@ -0,0 +1,233 @@
+#define pr_fmt(fmt) "SBAT: "fmt
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/verification.h>
+#include <linux/efi.h>
+
+/* SHIM variables */
+static efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
+static efi_char16_t shim_SbatLevel_name[] = L"SbatLevelRT";
+
+#define FIELDS 6
+
+struct sbat_entry {
+ union {
+ const char *field[FIELDS];
+ struct {
+ const char *name;
+ const char *genstr;
+ const char *vendor_name;
+ const char *vendor_package_name;
+ const char *vendor_version;
+ const char *vendor_url;
+ };
+ };
+};
+
+struct sbat {
+ struct sbat_entry *entries;
+ int n_entries;
+ char data[];
+};
+
+static void free_sbat(struct sbat *s)
+{
+ kfree(s->entries);
+ kfree(s);
+};
+
+static struct sbat* parse_sbat(struct sbat *s, const unsigned size, bool full_parse)
+{
+ int i, e, f;
+
+ s->data[size] = '\0';
+ s->n_entries = 0;
+ for (i = 0; s->data[i] != '\0'; i++)
+ if (s->data[i] == '\xA')
+ s->n_entries++;
+
+ s->entries = kcalloc(s->n_entries, sizeof(struct sbat_entry), GFP_KERNEL);
+ if (!s->entries)
+ return ERR_PTR(-ENOMEM);
+
+ e = 0; f = 0;
+ s->entries[e].field[f] = &s->data[0];
+ for (i = 0; s->data[i] != '\0'; i++) {
+ if (s->data[i] == ',') {
+ if (!full_parse && f == 2)
+ continue;
+ f++;
+ /* Too many fields per line */
+ if (f == FIELDS)
+ goto error;
+ s->data[i++] = '\0';
+ /* Corrupted SBAT ? */
+ if (i >= size)
+ goto error;
+ s->entries[e].field[f] = &s->data[i];
+ }
+ else if (s->data[i] == '\xA') {
+ if (full_parse && f != FIELDS - 1)
+ goto error;
+ /* The line must have at least 2 entries */
+ if (!f)
+ goto error;
+
+ s->data[i++] = '\0';
+ e++;
+ f = 0;
+ if (e < s->n_entries)
+ s->entries[e].field[f] = &s->data[i];
+ }
+ }
+ /* Corrupted SBAT ? */
+ if (e != s->n_entries || f)
+ goto error;
+
+ return s;
+
+error:
+ kfree(s->entries);
+ return ERR_PTR(-EILSEQ);
+}
+
+struct sbat* get_sbat_from_pefile(const char *pebuf, unsigned long pelen)
+{
+ struct sbat *s, *out;
+ char *sbat_data;
+ unsigned size;
+ int ret;
+
+ ret = pefile_find_section(".sbat", pebuf, pelen, &sbat_data, &size);
+ if (ret < 0) {
+ pr_warn("PE binary has no .sbat section\n");
+ return ERR_PTR(ret);
+ }
+
+ if (!size) {
+ pr_warn("PE binary has empty .sbat section\n");
+ return ERR_PTR(-ENODATA);
+ }
+
+ s = kzalloc(sizeof(struct sbat) + size + 1, GFP_KERNEL);
+ if (!s)
+ return ERR_PTR(-ENOMEM);
+ memcpy(&s->data, sbat_data, size);
+
+ out = parse_sbat(s, size, true);
+ if (IS_ERR(out))
+ kfree(s);
+
+ return out;
+}
+
+struct sbat* get_sbat_from_efi(void)
+{
+ efi_status_t status;
+ struct sbat *s, *out;
+ unsigned long size = 0;
+ struct efi_mokvar_table_entry *mokvar_entry;
+
+ if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
+ pr_info("EFI Runtime Services are disabled!\n");
+ return ERR_PTR(-ENOPROTOOPT);
+ }
+
+ status = efi.get_variable(shim_SbatLevel_name, &shim_guid,
+ NULL, &size, NULL);
+
+ if (status != EFI_BUFFER_TOO_SMALL)
+ goto fetch_mok;
+
+ s = kzalloc(sizeof(struct sbat) + size + 1, GFP_KERNEL);
+ if (!s)
+ return ERR_PTR(-ENOMEM);
+
+ status = efi.get_variable(shim_SbatLevel_name, &shim_guid,
+ NULL, &size, s->data);
+ if (status == EFI_SUCCESS)
+ goto parse_sbat;
+
+ kfree(s);
+
+fetch_mok:
+ mokvar_entry = efi_mokvar_entry_find("SbatLevelRT");
+ if (!mokvar_entry || !mokvar_entry->data_size)
+ return ERR_PTR(-ENODATA);
+
+ size = mokvar_entry->data_size;
+ s = kzalloc(sizeof(struct sbat) + size + 1, GFP_KERNEL);
+ if (!s)
+ return ERR_PTR(-ENOMEM);
+
+ memcpy(&s->data, mokvar_entry->data, size);
+
+parse_sbat:
+ out = parse_sbat(s, size, false);
+ if (IS_ERR(out))
+ kfree(s);
+
+ return out;
+}
+
+int verify_pefile_sbat(const char *pebuf, unsigned long pelen)
+{
+ struct sbat *s, *sl;
+ int ret = 0;
+ int i, j;
+ unsigned int sbat_gen, sbat_var_gen;
+
+ s = get_sbat_from_pefile(pebuf, pelen);
+ if (IS_ERR(s))
+ return PTR_ERR(s);
+
+ sl = get_sbat_from_efi();
+ if (IS_ERR_OR_NULL(sl)) {
+ free_sbat(s);
+ return PTR_ERR(sl);
+ }
+
+ pr_debug("SBAT section data\n");
+ for (i = 0; i < s->n_entries; i++)
+ pr_debug("%s, %s, %s, %s, %s, %s\n",
+ s->entries[i].name,
+ s->entries[i].genstr,
+ s->entries[i].vendor_name,
+ s->entries[i].vendor_package_name,
+ s->entries[i].vendor_version,
+ s->entries[i].vendor_url);
+
+ pr_debug("SBAT EFI data\n");
+ for (i = 0; i < sl->n_entries; i++)
+ pr_debug("%s, %s\n",
+ sl->entries[i].name,
+ sl->entries[i].genstr);
+
+ for (i = 0; i < s->n_entries; i++) {
+ struct sbat_entry *e = &s->entries[i];
+ for (j = 0; j < sl->n_entries; j++) {
+ struct sbat_entry *el = &sl->entries[j];
+ if (strcmp(e->name, el->name))
+ continue;
+ if (kstrtouint(e->genstr, 0, &sbat_gen) < 0 ||
+ kstrtouint(el->genstr, 0, &sbat_var_gen) < 0) {
+ ret = -EILSEQ;
+ goto quit;
+ }
+ pr_debug("Component %s has a matching SBAT variable entry, verifying\n",
+ e->name);
+ if (sbat_gen < sbat_var_gen) {
+ pr_warn("Image did not pass SBAT verification\n");
+ ret = -EKEYREVOKED;
+ goto quit;
+ }
+ }
+ }
+
+quit:
+ pr_info("Finished verifying SBAT data: %d\n", ret);
+ free_sbat(s);
+ free_sbat(sl);
+ return ret;
+}
--
2.39.0