3ff7c26
From e4c62c12635a371e43bd17e8d33a936668264491 Mon Sep 17 00:00:00 2001
6a91557
From: Dave Howells <dhowells@redhat.com>
3ff7c26
Date: Fri, 5 May 2017 08:21:58 +0100
3ff7c26
Subject: [PATCH 2/4] efi: Add an EFI signature blob parser
6a91557
3ff7c26
Add a function to parse an EFI signature blob looking for elements of
3ff7c26
interest.  A list is made up of a series of sublists, where all the
3ff7c26
elements in a sublist are of the same type, but sublists can be of
3ff7c26
different types.
3ff7c26
3ff7c26
For each sublist encountered, the function pointed to by the
3ff7c26
get_handler_for_guid argument is called with the type specifier GUID and
3ff7c26
returns either a pointer to a function to handle elements of that type or
3ff7c26
NULL if the type is not of interest.
3ff7c26
3ff7c26
If the sublist is of interest, each element is passed to the handler
3ff7c26
function in turn.
6a91557
6a91557
Signed-off-by: David Howells <dhowells@redhat.com>
6a91557
---
3ff7c26
 certs/Kconfig       |   8 ++++
3ff7c26
 certs/Makefile      |   1 +
3ff7c26
 certs/efi_parser.c  | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++
3ff7c26
 include/linux/efi.h |   9 +++++
3ff7c26
 4 files changed, 130 insertions(+)
3ff7c26
 create mode 100644 certs/efi_parser.c
3ff7c26
3ff7c26
diff --git a/certs/Kconfig b/certs/Kconfig
3ff7c26
index 6ce51ed..630ae09 100644
3ff7c26
--- a/certs/Kconfig
3ff7c26
+++ b/certs/Kconfig
3ff7c26
@@ -82,4 +82,12 @@ config SYSTEM_BLACKLIST_HASH_LIST
3ff7c26
 	  wrapper to incorporate the list into the kernel.  Each <hash> should
3ff7c26
 	  be a string of hex digits.
6a91557
6a91557
+config EFI_SIGNATURE_LIST_PARSER
6a91557
+	bool "EFI signature list parser"
6a91557
+	depends on EFI
6a91557
+	select X509_CERTIFICATE_PARSER
6a91557
+	help
6a91557
+	  This option provides support for parsing EFI signature lists for
6a91557
+	  X.509 certificates and turning them into keys.
6a91557
+
3ff7c26
 endmenu
3ff7c26
diff --git a/certs/Makefile b/certs/Makefile
3ff7c26
index 4119bb3..738151a 100644
3ff7c26
--- a/certs/Makefile
3ff7c26
+++ b/certs/Makefile
3ff7c26
@@ -9,6 +9,7 @@ obj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_hashes.o
3ff7c26
 else
3ff7c26
 obj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_nohashes.o
3ff7c26
 endif
6a91557
+obj-$(CONFIG_EFI_SIGNATURE_LIST_PARSER) += efi_parser.o
3ff7c26
3ff7c26
 ifeq ($(CONFIG_SYSTEM_TRUSTED_KEYRING),y)
3ff7c26
3ff7c26
diff --git a/certs/efi_parser.c b/certs/efi_parser.c
6a91557
new file mode 100644
3ff7c26
index 0000000..4e396f9
6a91557
--- /dev/null
3ff7c26
+++ b/certs/efi_parser.c
3ff7c26
@@ -0,0 +1,112 @@
6a91557
+/* EFI signature/key/certificate list parser
6a91557
+ *
3ff7c26
+ * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
6a91557
+ * Written by David Howells (dhowells@redhat.com)
6a91557
+ *
6a91557
+ * This program is free software; you can redistribute it and/or
6a91557
+ * modify it under the terms of the GNU General Public Licence
6a91557
+ * as published by the Free Software Foundation; either version
6a91557
+ * 2 of the Licence, or (at your option) any later version.
6a91557
+ */
6a91557
+
6a91557
+#define pr_fmt(fmt) "EFI: "fmt
6a91557
+#include <linux/module.h>
6a91557
+#include <linux/printk.h>
6a91557
+#include <linux/err.h>
6a91557
+#include <linux/efi.h>
6a91557
+
6a91557
+/**
6a91557
+ * parse_efi_signature_list - Parse an EFI signature list for certificates
3ff7c26
+ * @source: The source of the key
6a91557
+ * @data: The data blob to parse
6a91557
+ * @size: The size of the data blob
3ff7c26
+ * @get_handler_for_guid: Get the handler func for the sig type (or NULL)
3ff7c26
+ *
3ff7c26
+ * Parse an EFI signature list looking for elements of interest.  A list is
3ff7c26
+ * made up of a series of sublists, where all the elements in a sublist are of
3ff7c26
+ * the same type, but sublists can be of different types.
3ff7c26
+ *
3ff7c26
+ * For each sublist encountered, the @get_handler_for_guid function is called
3ff7c26
+ * with the type specifier GUID and returns either a pointer to a function to
3ff7c26
+ * handle elements of that type or NULL if the type is not of interest.
3ff7c26
+ *
3ff7c26
+ * If the sublist is of interest, each element is passed to the handler
3ff7c26
+ * function in turn.
3ff7c26
+ *
3ff7c26
+ * Error EBADMSG is returned if the list doesn't parse correctly and 0 is
3ff7c26
+ * returned if the list was parsed correctly.  No error can be returned from
3ff7c26
+ * the @get_handler_for_guid function or the element handler function it
3ff7c26
+ * returns.
6a91557
+ */
3ff7c26
+int __init parse_efi_signature_list(
3ff7c26
+	const char *source,
3ff7c26
+	const void *data, size_t size,
3ff7c26
+	efi_element_handler_t (*get_handler_for_guid)(const efi_guid_t *))
6a91557
+{
3ff7c26
+	efi_element_handler_t handler;
6a91557
+	unsigned offs = 0;
6a91557
+
6a91557
+	pr_devel("-->%s(,%zu)\n", __func__, size);
6a91557
+
6a91557
+	while (size > 0) {
6a91557
+		const efi_signature_data_t *elem;
3ff7c26
+		efi_signature_list_t list;
3ff7c26
+		size_t lsize, esize, hsize, elsize;
6a91557
+
6a91557
+		if (size < sizeof(list))
6a91557
+			return -EBADMSG;
6a91557
+
6a91557
+		memcpy(&list, data, sizeof(list));
6a91557
+		pr_devel("LIST[%04x] guid=%pUl ls=%x hs=%x ss=%x\n",
6a91557
+			 offs,
6a91557
+			 list.signature_type.b, list.signature_list_size,
6a91557
+			 list.signature_header_size, list.signature_size);
6a91557
+
6a91557
+		lsize = list.signature_list_size;
6a91557
+		hsize = list.signature_header_size;
6a91557
+		esize = list.signature_size;
6a91557
+		elsize = lsize - sizeof(list) - hsize;
6a91557
+
6a91557
+		if (lsize > size) {
6a91557
+			pr_devel("<--%s() = -EBADMSG [overrun @%x]\n",
6a91557
+				 __func__, offs);
6a91557
+			return -EBADMSG;
6a91557
+		}
3ff7c26
+
6a91557
+		if (lsize < sizeof(list) ||
6a91557
+		    lsize - sizeof(list) < hsize ||
6a91557
+		    esize < sizeof(*elem) ||
6a91557
+		    elsize < esize ||
6a91557
+		    elsize % esize != 0) {
6a91557
+			pr_devel("- bad size combo @%x\n", offs);
6a91557
+			return -EBADMSG;
6a91557
+		}
6a91557
+
3ff7c26
+		handler = get_handler_for_guid(&list.signature_type);
3ff7c26
+		if (!handler) {
6a91557
+			data += lsize;
6a91557
+			size -= lsize;
6a91557
+			offs += lsize;
6a91557
+			continue;
6a91557
+		}
6a91557
+
6a91557
+		data += sizeof(list) + hsize;
6a91557
+		size -= sizeof(list) + hsize;
6a91557
+		offs += sizeof(list) + hsize;
6a91557
+
6a91557
+		for (; elsize > 0; elsize -= esize) {
6a91557
+			elem = data;
6a91557
+
6a91557
+			pr_devel("ELEM[%04x]\n", offs);
3ff7c26
+			handler(source,
6a91557
+				&elem->signature_data,
3ff7c26
+				esize - sizeof(*elem));
6a91557
+
6a91557
+			data += esize;
6a91557
+			size -= esize;
6a91557
+			offs += esize;
6a91557
+		}
6a91557
+	}
6a91557
+
6a91557
+	return 0;
6a91557
+}
6a91557
diff --git a/include/linux/efi.h b/include/linux/efi.h
3ff7c26
index 3259ad6..08024c6 100644
6a91557
--- a/include/linux/efi.h
6a91557
+++ b/include/linux/efi.h
3ff7c26
@@ -1055,6 +1055,15 @@ extern int efi_memattr_apply_permissions(struct mm_struct *mm,
3fbe156
 char * __init efi_md_typeattr_format(char *buf, size_t size,
3fbe156
 				     const efi_memory_desc_t *md);
3ff7c26
3ff7c26
+
3ff7c26
+typedef void (*efi_element_handler_t)(const char *source,
3ff7c26
+				      const void *element_data,
3ff7c26
+				      size_t element_size);
3ff7c26
+extern int __init parse_efi_signature_list(
3ff7c26
+	const char *source,
3ff7c26
+	const void *data, size_t size,
3ff7c26
+	efi_element_handler_t (*get_handler_for_guid)(const efi_guid_t *));
6a91557
+
6a91557
 /**
6a91557
  * efi_range_is_wc - check the WC bit on an address range
6a91557
  * @start: starting kvirt address
18c8249
-- 
ea38f2f
2.9.3
18c8249