tleleu / rpms / kernel

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