cddccb2
From 90dc66270b02981b19a085c6a9184e3452b7b3e8 Mon Sep 17 00:00:00 2001
6a91557
From: Josh Boyer <jwboyer@fedoraproject.org>
cddccb2
Date: Fri, 5 May 2017 08:21:59 +0100
cddccb2
Subject: [PATCH 3/4] MODSIGN: Import certificates from UEFI Secure Boot
6a91557
6a91557
Secure Boot stores a list of allowed certificates in the 'db' variable.
6a91557
This imports those certificates into the system trusted keyring.  This
6a91557
allows for a third party signing certificate to be used in conjunction
6a91557
with signed modules.  By importing the public certificate into the 'db'
6a91557
variable, a user can allow a module signed with that certificate to
6a91557
load.  The shim UEFI bootloader has a similar certificate list stored
6a91557
in the 'MokListRT' variable.  We import those as well.
6a91557
cddccb2
Secure Boot also maintains a list of disallowed certificates in the 'dbx'
cddccb2
variable.  We load those certificates into the newly introduced system
cddccb2
blacklist keyring and forbid any module signed with those from loading and
cddccb2
forbid the use within the kernel of any key with a matching hash.
cddccb2
cddccb2
This facility is enabled by setting CONFIG_LOAD_UEFI_KEYS.
6a91557
6a91557
Signed-off-by: Josh Boyer <jwboyer@fedoraproject.org>
cddccb2
Signed-off-by: David Howells <dhowells@redhat.com>
6a91557
---
cddccb2
 certs/Kconfig     |  16 ++++++
cddccb2
 certs/Makefile    |   4 ++
cddccb2
 certs/load_uefi.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
cddccb2
 3 files changed, 188 insertions(+)
cddccb2
 create mode 100644 certs/load_uefi.c
6a91557
cddccb2
diff --git a/certs/Kconfig b/certs/Kconfig
cddccb2
index 630ae09..edf9f75 100644
cddccb2
--- a/certs/Kconfig
cddccb2
+++ b/certs/Kconfig
cddccb2
@@ -90,4 +90,20 @@ config EFI_SIGNATURE_LIST_PARSER
cddccb2
 	  This option provides support for parsing EFI signature lists for
cddccb2
 	  X.509 certificates and turning them into keys.
cddccb2
cddccb2
+config LOAD_UEFI_KEYS
cddccb2
+	bool "Load certs and blacklist from UEFI db for module checking"
cddccb2
+	depends on SYSTEM_BLACKLIST_KEYRING
cddccb2
+	depends on SECONDARY_TRUSTED_KEYRING
cddccb2
+	depends on EFI
cddccb2
+	depends on EFI_SIGNATURE_LIST_PARSER
cddccb2
+	help
cddccb2
+	  If the kernel is booted in secure boot mode, this option will cause
cddccb2
+	  the kernel to load the certificates from the UEFI db and MokListRT
cddccb2
+	  into the secondary trusted keyring.  It will also load any X.509
cddccb2
+	  SHA256 hashes in the dbx list into the blacklist.
cddccb2
+
cddccb2
+	  The effect of this is that, if the kernel is booted in secure boot
cddccb2
+	  mode, modules signed with UEFI-stored keys will be permitted to be
cddccb2
+	  loaded and keys that match the blacklist will be rejected.
cddccb2
+
cddccb2
 endmenu
cddccb2
diff --git a/certs/Makefile b/certs/Makefile
cddccb2
index 738151a..a5e057a 100644
cddccb2
--- a/certs/Makefile
cddccb2
+++ b/certs/Makefile
cddccb2
@@ -11,6 +11,10 @@ obj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_nohashes.o
cddccb2
 endif
cddccb2
 obj-$(CONFIG_EFI_SIGNATURE_LIST_PARSER) += efi_parser.o
b88f184
cddccb2
+obj-$(CONFIG_LOAD_UEFI_KEYS) += load_uefi.o
cddccb2
+$(obj)/load_uefi.o: KBUILD_CFLAGS += -fshort-wchar
b88f184
+
cddccb2
+
cddccb2
 ifeq ($(CONFIG_SYSTEM_TRUSTED_KEYRING),y)
b88f184
cddccb2
 $(eval $(call config_filename,SYSTEM_TRUSTED_KEYS))
cddccb2
diff --git a/certs/load_uefi.c b/certs/load_uefi.c
6a91557
new file mode 100644
cddccb2
index 0000000..b44e464
6a91557
--- /dev/null
cddccb2
+++ b/certs/load_uefi.c
cddccb2
@@ -0,0 +1,168 @@
6a91557
+#include <linux/kernel.h>
6a91557
+#include <linux/sched.h>
6a91557
+#include <linux/cred.h>
6a91557
+#include <linux/err.h>
6a91557
+#include <linux/efi.h>
6a91557
+#include <linux/slab.h>
6a91557
+#include <keys/asymmetric-type.h>
6a91557
+#include <keys/system_keyring.h>
cddccb2
+#include "internal.h"
cddccb2
+
cddccb2
+static __initdata efi_guid_t efi_cert_x509_guid = EFI_CERT_X509_GUID;
cddccb2
+static __initdata efi_guid_t efi_cert_x509_sha256_guid = EFI_CERT_X509_SHA256_GUID;
cddccb2
+static __initdata efi_guid_t efi_cert_sha256_guid = EFI_CERT_SHA256_GUID;
6a91557
+
cddccb2
+/*
cddccb2
+ * Get a certificate list blob from the named EFI variable.
cddccb2
+ */
cddccb2
+static __init void *get_cert_list(efi_char16_t *name, efi_guid_t *guid,
cddccb2
+				  unsigned long *size)
6a91557
+{
6a91557
+	efi_status_t status;
6a91557
+	unsigned long lsize = 4;
6a91557
+	unsigned long tmpdb[4];
cddccb2
+	void *db;
6a91557
+
6a91557
+	status = efi.get_variable(name, guid, NULL, &lsize, &tmpdb);
6a91557
+	if (status != EFI_BUFFER_TOO_SMALL) {
6a91557
+		pr_err("Couldn't get size: 0x%lx\n", status);
6a91557
+		return NULL;
6a91557
+	}
6a91557
+
6a91557
+	db = kmalloc(lsize, GFP_KERNEL);
6a91557
+	if (!db) {
6a91557
+		pr_err("Couldn't allocate memory for uefi cert list\n");
cddccb2
+		return NULL;
6a91557
+	}
6a91557
+
6a91557
+	status = efi.get_variable(name, guid, NULL, &lsize, db);
6a91557
+	if (status != EFI_SUCCESS) {
6a91557
+		kfree(db);
6a91557
+		pr_err("Error reading db var: 0x%lx\n", status);
cddccb2
+		return NULL;
6a91557
+	}
cddccb2
+
6a91557
+	*size = lsize;
6a91557
+	return db;
6a91557
+}
6a91557
+
6a91557
+/*
cddccb2
+ * Blacklist an X509 TBS hash.
cddccb2
+ */
cddccb2
+static __init void uefi_blacklist_x509_tbs(const char *source,
cddccb2
+					   const void *data, size_t len)
cddccb2
+{
cddccb2
+	char *hash, *p;
cddccb2
+
cddccb2
+	hash = kmalloc(4 + len * 2 + 1, GFP_KERNEL);
cddccb2
+	if (!hash)
cddccb2
+		return;
cddccb2
+	p = memcpy(hash, "tbs:", 4);
cddccb2
+	p += 4;
cddccb2
+	bin2hex(p, data, len);
cddccb2
+	p += len * 2;
cddccb2
+	*p = 0;
cddccb2
+
cddccb2
+	mark_hash_blacklisted(hash);
cddccb2
+	kfree(hash);
cddccb2
+}
cddccb2
+
cddccb2
+/*
cddccb2
+ * Blacklist the hash of an executable.
cddccb2
+ */
cddccb2
+static __init void uefi_blacklist_binary(const char *source,
cddccb2
+					 const void *data, size_t len)
cddccb2
+{
cddccb2
+	char *hash, *p;
cddccb2
+
cddccb2
+	hash = kmalloc(4 + len * 2 + 1, GFP_KERNEL);
cddccb2
+	if (!hash)
cddccb2
+		return;
cddccb2
+	p = memcpy(hash, "bin:", 4);
cddccb2
+	p += 4;
cddccb2
+	bin2hex(p, data, len);
cddccb2
+	p += len * 2;
cddccb2
+	*p = 0;
cddccb2
+
cddccb2
+	mark_hash_blacklisted(hash);
cddccb2
+	kfree(hash);
cddccb2
+}
cddccb2
+
cddccb2
+/*
cddccb2
+ * Return the appropriate handler for particular signature list types found in
cddccb2
+ * the UEFI db and MokListRT tables.
cddccb2
+ */
cddccb2
+static __init efi_element_handler_t get_handler_for_db(const efi_guid_t *sig_type)
cddccb2
+{
cddccb2
+	if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
cddccb2
+		return add_trusted_secondary_key;
cddccb2
+	return 0;
cddccb2
+}
cddccb2
+
cddccb2
+/*
cddccb2
+ * Return the appropriate handler for particular signature list types found in
cddccb2
+ * the UEFI dbx and MokListXRT tables.
cddccb2
+ */
cddccb2
+static __init efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type)
cddccb2
+{
cddccb2
+	if (efi_guidcmp(*sig_type, efi_cert_x509_sha256_guid) == 0)
cddccb2
+		return uefi_blacklist_x509_tbs;
cddccb2
+	if (efi_guidcmp(*sig_type, efi_cert_sha256_guid) == 0)
cddccb2
+		return uefi_blacklist_binary;
cddccb2
+	return 0;
cddccb2
+}
cddccb2
+
cddccb2
+/*
cddccb2
+ * Load the certs contained in the UEFI databases
cddccb2
+ */
6a91557
+static int __init load_uefi_certs(void)
6a91557
+{
6a91557
+	efi_guid_t secure_var = EFI_IMAGE_SECURITY_DATABASE_GUID;
6a91557
+	efi_guid_t mok_var = EFI_SHIM_LOCK_GUID;
6a91557
+	void *db = NULL, *dbx = NULL, *mok = NULL;
6a91557
+	unsigned long dbsize = 0, dbxsize = 0, moksize = 0;
6a91557
+	int rc = 0;
6a91557
+
cddccb2
+	if (!efi.get_variable)
cddccb2
+		return false;
42d2805
+
6a91557
+	/* Get db, MokListRT, and dbx.  They might not exist, so it isn't
6a91557
+	 * an error if we can't get them.
6a91557
+	 */
6a91557
+	db = get_cert_list(L"db", &secure_var, &dbsize);
6a91557
+	if (!db) {
6a91557
+		pr_err("MODSIGN: Couldn't get UEFI db list\n");
6a91557
+	} else {
cddccb2
+		rc = parse_efi_signature_list("UEFI:db",
cddccb2
+					      db, dbsize, get_handler_for_db);
6a91557
+		if (rc)
6a91557
+			pr_err("Couldn't parse db signatures: %d\n", rc);
6a91557
+		kfree(db);
6a91557
+	}
6a91557
+
6a91557
+	mok = get_cert_list(L"MokListRT", &mok_var, &moksize);
6a91557
+	if (!mok) {
6a91557
+		pr_info("MODSIGN: Couldn't get UEFI MokListRT\n");
6a91557
+	} else {
cddccb2
+		rc = parse_efi_signature_list("UEFI:MokListRT",
cddccb2
+					      mok, moksize, get_handler_for_db);
6a91557
+		if (rc)
6a91557
+			pr_err("Couldn't parse MokListRT signatures: %d\n", rc);
6a91557
+		kfree(mok);
6a91557
+	}
6a91557
+
6a91557
+	dbx = get_cert_list(L"dbx", &secure_var, &dbxsize);
6a91557
+	if (!dbx) {
6a91557
+		pr_info("MODSIGN: Couldn't get UEFI dbx list\n");
6a91557
+	} else {
cddccb2
+		rc = parse_efi_signature_list("UEFI:dbx",
cddccb2
+					      dbx, dbxsize,
cddccb2
+					      get_handler_for_dbx);
6a91557
+		if (rc)
6a91557
+			pr_err("Couldn't parse dbx signatures: %d\n", rc);
6a91557
+		kfree(dbx);
6a91557
+	}
6a91557
+
6a91557
+	return rc;
6a91557
+}
6a91557
+late_initcall(load_uefi_certs);
18c8249
-- 
ea38f2f
2.9.3
18c8249