blob: e0b01c96234cfeeea8cb5ed9188c7cd9c7faaef7 [file] [log] [blame]
Joel Stanleya1fccbf2020-06-23 17:25:56 +09301From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2From: Nayna Jain <nayna@linux.ibm.com>
3Date: Sun, 10 Nov 2019 21:10:35 -0600
Joel Stanleycb9bf572020-09-29 16:18:12 +09304Subject: [PATCH 12/19] x86/efi: move common keyring handler functions to new
Joel Stanleya1fccbf2020-06-23 17:25:56 +09305 file
6
7The handlers to add the keys to the .platform keyring and blacklisted
8hashes to the .blacklist keyring is common for both the uefi and powerpc
9mechanisms of loading the keys/hashes from the firmware.
10
11This patch moves the common code from load_uefi.c to keyring_handler.c
12
13Signed-off-by: Nayna Jain <nayna@linux.ibm.com>
14Acked-by: Mimi Zohar <zohar@linux.ibm.com>
15Signed-off-by: Eric Richter <erichte@linux.ibm.com>
16Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
17Link: https://lore.kernel.org/r/1573441836-3632-4-git-send-email-nayna@linux.ibm.com
18(cherry picked from commit ad723674d6758478829ee766e3f1a2a24d56236f)
19Signed-off-by: Joel Stanley <joel@jms.id.au>
20---
21 security/integrity/Makefile | 3 +-
22 .../platform_certs/keyring_handler.c | 80 +++++++++++++++++++
23 .../platform_certs/keyring_handler.h | 32 ++++++++
24 security/integrity/platform_certs/load_uefi.c | 67 +---------------
25 4 files changed, 115 insertions(+), 67 deletions(-)
26 create mode 100644 security/integrity/platform_certs/keyring_handler.c
27 create mode 100644 security/integrity/platform_certs/keyring_handler.h
28
29diff --git a/security/integrity/Makefile b/security/integrity/Makefile
30index 35e6ca773734..351c9662994b 100644
31--- a/security/integrity/Makefile
32+++ b/security/integrity/Makefile
33@@ -11,7 +11,8 @@ integrity-$(CONFIG_INTEGRITY_SIGNATURE) += digsig.o
34 integrity-$(CONFIG_INTEGRITY_ASYMMETRIC_KEYS) += digsig_asymmetric.o
35 integrity-$(CONFIG_INTEGRITY_PLATFORM_KEYRING) += platform_certs/platform_keyring.o
36 integrity-$(CONFIG_LOAD_UEFI_KEYS) += platform_certs/efi_parser.o \
37- platform_certs/load_uefi.o
38+ platform_certs/load_uefi.o \
39+ platform_certs/keyring_handler.o
40 integrity-$(CONFIG_LOAD_IPL_KEYS) += platform_certs/load_ipl_s390.o
41
42 obj-$(CONFIG_IMA) += ima/
43diff --git a/security/integrity/platform_certs/keyring_handler.c b/security/integrity/platform_certs/keyring_handler.c
44new file mode 100644
45index 000000000000..c5ba695c10e3
46--- /dev/null
47+++ b/security/integrity/platform_certs/keyring_handler.c
48@@ -0,0 +1,80 @@
49+// SPDX-License-Identifier: GPL-2.0
50+
51+#include <linux/kernel.h>
52+#include <linux/sched.h>
53+#include <linux/cred.h>
54+#include <linux/err.h>
55+#include <linux/efi.h>
56+#include <linux/slab.h>
57+#include <keys/asymmetric-type.h>
58+#include <keys/system_keyring.h>
59+#include "../integrity.h"
60+
61+static efi_guid_t efi_cert_x509_guid __initdata = EFI_CERT_X509_GUID;
62+static efi_guid_t efi_cert_x509_sha256_guid __initdata =
63+ EFI_CERT_X509_SHA256_GUID;
64+static efi_guid_t efi_cert_sha256_guid __initdata = EFI_CERT_SHA256_GUID;
65+
66+/*
67+ * Blacklist a hash.
68+ */
69+static __init void uefi_blacklist_hash(const char *source, const void *data,
70+ size_t len, const char *type,
71+ size_t type_len)
72+{
73+ char *hash, *p;
74+
75+ hash = kmalloc(type_len + len * 2 + 1, GFP_KERNEL);
76+ if (!hash)
77+ return;
78+ p = memcpy(hash, type, type_len);
79+ p += type_len;
80+ bin2hex(p, data, len);
81+ p += len * 2;
82+ *p = 0;
83+
84+ mark_hash_blacklisted(hash);
85+ kfree(hash);
86+}
87+
88+/*
89+ * Blacklist an X509 TBS hash.
90+ */
91+static __init void uefi_blacklist_x509_tbs(const char *source,
92+ const void *data, size_t len)
93+{
94+ uefi_blacklist_hash(source, data, len, "tbs:", 4);
95+}
96+
97+/*
98+ * Blacklist the hash of an executable.
99+ */
100+static __init void uefi_blacklist_binary(const char *source,
101+ const void *data, size_t len)
102+{
103+ uefi_blacklist_hash(source, data, len, "bin:", 4);
104+}
105+
106+/*
107+ * Return the appropriate handler for particular signature list types found in
108+ * the UEFI db and MokListRT tables.
109+ */
110+__init efi_element_handler_t get_handler_for_db(const efi_guid_t *sig_type)
111+{
112+ if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
113+ return add_to_platform_keyring;
114+ return 0;
115+}
116+
117+/*
118+ * Return the appropriate handler for particular signature list types found in
119+ * the UEFI dbx and MokListXRT tables.
120+ */
121+__init efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type)
122+{
123+ if (efi_guidcmp(*sig_type, efi_cert_x509_sha256_guid) == 0)
124+ return uefi_blacklist_x509_tbs;
125+ if (efi_guidcmp(*sig_type, efi_cert_sha256_guid) == 0)
126+ return uefi_blacklist_binary;
127+ return 0;
128+}
129diff --git a/security/integrity/platform_certs/keyring_handler.h b/security/integrity/platform_certs/keyring_handler.h
130new file mode 100644
131index 000000000000..2462bfa08fe3
132--- /dev/null
133+++ b/security/integrity/platform_certs/keyring_handler.h
134@@ -0,0 +1,32 @@
135+/* SPDX-License-Identifier: GPL-2.0 */
136+
137+#ifndef PLATFORM_CERTS_INTERNAL_H
138+#define PLATFORM_CERTS_INTERNAL_H
139+
140+#include <linux/efi.h>
141+
142+void blacklist_hash(const char *source, const void *data,
143+ size_t len, const char *type,
144+ size_t type_len);
145+
146+/*
147+ * Blacklist an X509 TBS hash.
148+ */
149+void blacklist_x509_tbs(const char *source, const void *data, size_t len);
150+
151+/*
152+ * Blacklist the hash of an executable.
153+ */
154+void blacklist_binary(const char *source, const void *data, size_t len);
155+
156+/*
157+ * Return the handler for particular signature list types found in the db.
158+ */
159+efi_element_handler_t get_handler_for_db(const efi_guid_t *sig_type);
160+
161+/*
162+ * Return the handler for particular signature list types found in the dbx.
163+ */
164+efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type);
165+
166+#endif
167diff --git a/security/integrity/platform_certs/load_uefi.c b/security/integrity/platform_certs/load_uefi.c
168index 020fc7a11ef0..aa874d84e413 100644
169--- a/security/integrity/platform_certs/load_uefi.c
170+++ b/security/integrity/platform_certs/load_uefi.c
171@@ -9,6 +9,7 @@
172 #include <keys/asymmetric-type.h>
173 #include <keys/system_keyring.h>
174 #include "../integrity.h"
175+#include "keyring_handler.h"
176
177 static efi_guid_t efi_cert_x509_guid __initdata = EFI_CERT_X509_GUID;
178 static efi_guid_t efi_cert_x509_sha256_guid __initdata =
179@@ -69,72 +70,6 @@ static __init void *get_cert_list(efi_char16_t *name, efi_guid_t *guid,
180 return db;
181 }
182
183-/*
184- * Blacklist a hash.
185- */
186-static __init void uefi_blacklist_hash(const char *source, const void *data,
187- size_t len, const char *type,
188- size_t type_len)
189-{
190- char *hash, *p;
191-
192- hash = kmalloc(type_len + len * 2 + 1, GFP_KERNEL);
193- if (!hash)
194- return;
195- p = memcpy(hash, type, type_len);
196- p += type_len;
197- bin2hex(p, data, len);
198- p += len * 2;
199- *p = 0;
200-
201- mark_hash_blacklisted(hash);
202- kfree(hash);
203-}
204-
205-/*
206- * Blacklist an X509 TBS hash.
207- */
208-static __init void uefi_blacklist_x509_tbs(const char *source,
209- const void *data, size_t len)
210-{
211- uefi_blacklist_hash(source, data, len, "tbs:", 4);
212-}
213-
214-/*
215- * Blacklist the hash of an executable.
216- */
217-static __init void uefi_blacklist_binary(const char *source,
218- const void *data, size_t len)
219-{
220- uefi_blacklist_hash(source, data, len, "bin:", 4);
221-}
222-
223-/*
224- * Return the appropriate handler for particular signature list types found in
225- * the UEFI db and MokListRT tables.
226- */
227-static __init efi_element_handler_t get_handler_for_db(const efi_guid_t *
228- sig_type)
229-{
230- if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
231- return add_to_platform_keyring;
232- return 0;
233-}
234-
235-/*
236- * Return the appropriate handler for particular signature list types found in
237- * the UEFI dbx and MokListXRT tables.
238- */
239-static __init efi_element_handler_t get_handler_for_dbx(const efi_guid_t *
240- sig_type)
241-{
242- if (efi_guidcmp(*sig_type, efi_cert_x509_sha256_guid) == 0)
243- return uefi_blacklist_x509_tbs;
244- if (efi_guidcmp(*sig_type, efi_cert_sha256_guid) == 0)
245- return uefi_blacklist_binary;
246- return 0;
247-}
248-
249 /*
250 * Load the certs contained in the UEFI databases into the platform trusted
251 * keyring and the UEFI blacklisted X.509 cert SHA256 hashes into the blacklist