c6ca7ce
From 90dc66270b02981b19a085c6a9184e3452b7b3e8 Mon Sep 17 00:00:00 2001
6a91557
From: Josh Boyer <jwboyer@fedoraproject.org>
c6ca7ce
Date: Fri, 5 May 2017 08:21:59 +0100
c6ca7ce
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
c6ca7ce
Secure Boot also maintains a list of disallowed certificates in the 'dbx'
c6ca7ce
variable.  We load those certificates into the newly introduced system
c6ca7ce
blacklist keyring and forbid any module signed with those from loading and
c6ca7ce
forbid the use within the kernel of any key with a matching hash.
c6ca7ce
c6ca7ce
This facility is enabled by setting CONFIG_LOAD_UEFI_KEYS.
6a91557
6a91557
Signed-off-by: Josh Boyer <jwboyer@fedoraproject.org>
c6ca7ce
Signed-off-by: David Howells <dhowells@redhat.com>
6a91557
---
c6ca7ce
 certs/Kconfig     |  16 ++++++
c6ca7ce
 certs/Makefile    |   4 ++
c6ca7ce
 certs/load_uefi.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
c6ca7ce
 3 files changed, 188 insertions(+)
c6ca7ce
 create mode 100644 certs/load_uefi.c
6a91557
c6ca7ce
diff --git a/certs/Kconfig b/certs/Kconfig
c6ca7ce
index 630ae09..edf9f75 100644
c6ca7ce
--- a/certs/Kconfig
c6ca7ce
+++ b/certs/Kconfig
c6ca7ce
@@ -90,4 +90,20 @@ config EFI_SIGNATURE_LIST_PARSER
c6ca7ce
 	  This option provides support for parsing EFI signature lists for
c6ca7ce
 	  X.509 certificates and turning them into keys.
c6ca7ce
c6ca7ce
+config LOAD_UEFI_KEYS
c6ca7ce
+	bool "Load certs and blacklist from UEFI db for module checking"
c6ca7ce
+	depends on SYSTEM_BLACKLIST_KEYRING
c6ca7ce
+	depends on SECONDARY_TRUSTED_KEYRING
c6ca7ce
+	depends on EFI
c6ca7ce
+	depends on EFI_SIGNATURE_LIST_PARSER
6a91557
+	help
c6ca7ce
+	  If the kernel is booted in secure boot mode, this option will cause
c6ca7ce
+	  the kernel to load the certificates from the UEFI db and MokListRT
c6ca7ce
+	  into the secondary trusted keyring.  It will also load any X.509
c6ca7ce
+	  SHA256 hashes in the dbx list into the blacklist.
c6ca7ce
+
c6ca7ce
+	  The effect of this is that, if the kernel is booted in secure boot
c6ca7ce
+	  mode, modules signed with UEFI-stored keys will be permitted to be
c6ca7ce
+	  loaded and keys that match the blacklist will be rejected.
c6ca7ce
+
c6ca7ce
 endmenu
c6ca7ce
diff --git a/certs/Makefile b/certs/Makefile
c6ca7ce
index 738151a..a5e057a 100644
c6ca7ce
--- a/certs/Makefile
c6ca7ce
+++ b/certs/Makefile
c6ca7ce
@@ -11,6 +11,10 @@ obj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_nohashes.o
c6ca7ce
 endif
c6ca7ce
 obj-$(CONFIG_EFI_SIGNATURE_LIST_PARSER) += efi_parser.o
c6ca7ce
c6ca7ce
+obj-$(CONFIG_LOAD_UEFI_KEYS) += load_uefi.o
c6ca7ce
+$(obj)/load_uefi.o: KBUILD_CFLAGS += -fshort-wchar
c6ca7ce
+
c6ca7ce
+
c6ca7ce
 ifeq ($(CONFIG_SYSTEM_TRUSTED_KEYRING),y)
c6ca7ce
c6ca7ce
 $(eval $(call config_filename,SYSTEM_TRUSTED_KEYS))
c6ca7ce
diff --git a/certs/load_uefi.c b/certs/load_uefi.c
6a91557
new file mode 100644
c6ca7ce
index 0000000..b44e464
6a91557
--- /dev/null
c6ca7ce
+++ b/certs/load_uefi.c
c6ca7ce
@@ -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>
c6ca7ce
+#include "internal.h"
6a91557
+
c6ca7ce
+static __initdata efi_guid_t efi_cert_x509_guid = EFI_CERT_X509_GUID;
c6ca7ce
+static __initdata efi_guid_t efi_cert_x509_sha256_guid = EFI_CERT_X509_SHA256_GUID;
c6ca7ce
+static __initdata efi_guid_t efi_cert_sha256_guid = EFI_CERT_SHA256_GUID;
c6ca7ce
+
c6ca7ce
+/*
c6ca7ce
+ * Get a certificate list blob from the named EFI variable.
c6ca7ce
+ */
c6ca7ce
+static __init void *get_cert_list(efi_char16_t *name, efi_guid_t *guid,
c6ca7ce
+				  unsigned long *size)
6a91557
+{
6a91557
+	efi_status_t status;
6a91557
+	unsigned long lsize = 4;
6a91557
+	unsigned long tmpdb[4];
c6ca7ce
+	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");
c6ca7ce
+		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);
c6ca7ce
+		return NULL;
6a91557
+	}
c6ca7ce
+
6a91557
+	*size = lsize;
6a91557
+	return db;
6a91557
+}
6a91557
+
6a91557
+/*
c6ca7ce
+ * Blacklist an X509 TBS hash.
c6ca7ce
+ */
c6ca7ce
+static __init void uefi_blacklist_x509_tbs(const char *source,
c6ca7ce
+					   const void *data, size_t len)
c6ca7ce
+{
c6ca7ce
+	char *hash, *p;
c6ca7ce
+
c6ca7ce
+	hash = kmalloc(4 + len * 2 + 1, GFP_KERNEL);
c6ca7ce
+	if (!hash)
c6ca7ce
+		return;
c6ca7ce
+	p = memcpy(hash, "tbs:", 4);
c6ca7ce
+	p += 4;
c6ca7ce
+	bin2hex(p, data, len);
c6ca7ce
+	p += len * 2;
c6ca7ce
+	*p = 0;
c6ca7ce
+
c6ca7ce
+	mark_hash_blacklisted(hash);
c6ca7ce
+	kfree(hash);
c6ca7ce
+}
c6ca7ce
+
c6ca7ce
+/*
c6ca7ce
+ * Blacklist the hash of an executable.
c6ca7ce
+ */
c6ca7ce
+static __init void uefi_blacklist_binary(const char *source,
c6ca7ce
+					 const void *data, size_t len)
c6ca7ce
+{
c6ca7ce
+	char *hash, *p;
c6ca7ce
+
c6ca7ce
+	hash = kmalloc(4 + len * 2 + 1, GFP_KERNEL);
c6ca7ce
+	if (!hash)
c6ca7ce
+		return;
c6ca7ce
+	p = memcpy(hash, "bin:", 4);
c6ca7ce
+	p += 4;
c6ca7ce
+	bin2hex(p, data, len);
c6ca7ce
+	p += len * 2;
c6ca7ce
+	*p = 0;
c6ca7ce
+
c6ca7ce
+	mark_hash_blacklisted(hash);
c6ca7ce
+	kfree(hash);
c6ca7ce
+}
c6ca7ce
+
c6ca7ce
+/*
c6ca7ce
+ * Return the appropriate handler for particular signature list types found in
c6ca7ce
+ * the UEFI db and MokListRT tables.
c6ca7ce
+ */
c6ca7ce
+static __init efi_element_handler_t get_handler_for_db(const efi_guid_t *sig_type)
c6ca7ce
+{
c6ca7ce
+	if (efi_guidcmp(*sig_type, efi_cert_x509_guid) == 0)
c6ca7ce
+		return add_trusted_secondary_key;
c6ca7ce
+	return 0;
c6ca7ce
+}
c6ca7ce
+
c6ca7ce
+/*
c6ca7ce
+ * Return the appropriate handler for particular signature list types found in
c6ca7ce
+ * the UEFI dbx and MokListXRT tables.
c6ca7ce
+ */
c6ca7ce
+static __init efi_element_handler_t get_handler_for_dbx(const efi_guid_t *sig_type)
c6ca7ce
+{
c6ca7ce
+	if (efi_guidcmp(*sig_type, efi_cert_x509_sha256_guid) == 0)
c6ca7ce
+		return uefi_blacklist_x509_tbs;
c6ca7ce
+	if (efi_guidcmp(*sig_type, efi_cert_sha256_guid) == 0)
c6ca7ce
+		return uefi_blacklist_binary;
c6ca7ce
+	return 0;
c6ca7ce
+}
c6ca7ce
+
c6ca7ce
+/*
c6ca7ce
+ * Load the certs contained in the UEFI databases
c6ca7ce
+ */
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
+
c6ca7ce
+	if (!efi.get_variable)
c6ca7ce
+		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 {
c6ca7ce
+		rc = parse_efi_signature_list("UEFI:db",
c6ca7ce
+					      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 {
c6ca7ce
+		rc = parse_efi_signature_list("UEFI:MokListRT",
c6ca7ce
+					      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 {
c6ca7ce
+		rc = parse_efi_signature_list("UEFI:dbx",
c6ca7ce
+					      dbx, dbxsize,
c6ca7ce
+					      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
-- 
0ad9385
2.9.3
18c8249