cddccb2
From e4c62c12635a371e43bd17e8d33a936668264491 Mon Sep 17 00:00:00 2001
6a91557
From: Dave Howells <dhowells@redhat.com>
cddccb2
Date: Fri, 5 May 2017 08:21:58 +0100
cddccb2
Subject: [PATCH 2/4] efi: Add an EFI signature blob parser
6a91557
cddccb2
Add a function to parse an EFI signature blob looking for elements of
cddccb2
interest.  A list is made up of a series of sublists, where all the
cddccb2
elements in a sublist are of the same type, but sublists can be of
cddccb2
different types.
cddccb2
cddccb2
For each sublist encountered, the function pointed to by the
cddccb2
get_handler_for_guid argument is called with the type specifier GUID and
cddccb2
returns either a pointer to a function to handle elements of that type or
cddccb2
NULL if the type is not of interest.
cddccb2
cddccb2
If the sublist is of interest, each element is passed to the handler
cddccb2
function in turn.
6a91557
6a91557
Signed-off-by: David Howells <dhowells@redhat.com>
6a91557
---
cddccb2
 certs/Kconfig       |   8 ++++
cddccb2
 certs/Makefile      |   1 +
cddccb2
 certs/efi_parser.c  | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++
cddccb2
 include/linux/efi.h |   9 +++++
cddccb2
 4 files changed, 130 insertions(+)
cddccb2
 create mode 100644 certs/efi_parser.c
cddccb2
cddccb2
diff --git a/certs/Kconfig b/certs/Kconfig
cddccb2
index 6ce51ed..630ae09 100644
cddccb2
--- a/certs/Kconfig
cddccb2
+++ b/certs/Kconfig
cddccb2
@@ -82,4 +82,12 @@ config SYSTEM_BLACKLIST_HASH_LIST
cddccb2
 	  wrapper to incorporate the list into the kernel.  Each <hash> should
cddccb2
 	  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
+
cddccb2
 endmenu
cddccb2
diff --git a/certs/Makefile b/certs/Makefile
cddccb2
index 4119bb3..738151a 100644
cddccb2
--- a/certs/Makefile
cddccb2
+++ b/certs/Makefile
cddccb2
@@ -9,6 +9,7 @@ obj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_hashes.o
cddccb2
 else
cddccb2
 obj-$(CONFIG_SYSTEM_BLACKLIST_KEYRING) += blacklist_nohashes.o
cddccb2
 endif
6a91557
+obj-$(CONFIG_EFI_SIGNATURE_LIST_PARSER) += efi_parser.o
cddccb2
cddccb2
 ifeq ($(CONFIG_SYSTEM_TRUSTED_KEYRING),y)
cddccb2
cddccb2
diff --git a/certs/efi_parser.c b/certs/efi_parser.c
6a91557
new file mode 100644
cddccb2
index 0000000..4e396f9
6a91557
--- /dev/null
cddccb2
+++ b/certs/efi_parser.c
cddccb2
@@ -0,0 +1,112 @@
6a91557
+/* EFI signature/key/certificate list parser
6a91557
+ *
cddccb2
+ * 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
cddccb2
+ * @source: The source of the key
6a91557
+ * @data: The data blob to parse
6a91557
+ * @size: The size of the data blob
cddccb2
+ * @get_handler_for_guid: Get the handler func for the sig type (or NULL)
cddccb2
+ *
cddccb2
+ * Parse an EFI signature list looking for elements of interest.  A list is
cddccb2
+ * made up of a series of sublists, where all the elements in a sublist are of
cddccb2
+ * the same type, but sublists can be of different types.
cddccb2
+ *
cddccb2
+ * For each sublist encountered, the @get_handler_for_guid function is called
cddccb2
+ * with the type specifier GUID and returns either a pointer to a function to
cddccb2
+ * handle elements of that type or NULL if the type is not of interest.
cddccb2
+ *
cddccb2
+ * If the sublist is of interest, each element is passed to the handler
cddccb2
+ * function in turn.
cddccb2
+ *
cddccb2
+ * Error EBADMSG is returned if the list doesn't parse correctly and 0 is
cddccb2
+ * returned if the list was parsed correctly.  No error can be returned from
cddccb2
+ * the @get_handler_for_guid function or the element handler function it
cddccb2
+ * returns.
6a91557
+ */
cddccb2
+int __init parse_efi_signature_list(
cddccb2
+	const char *source,
cddccb2
+	const void *data, size_t size,
cddccb2
+	efi_element_handler_t (*get_handler_for_guid)(const efi_guid_t *))
6a91557
+{
cddccb2
+	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;
cddccb2
+		efi_signature_list_t list;
cddccb2
+		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
+		}
cddccb2
+
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
+
cddccb2
+		handler = get_handler_for_guid(&list.signature_type);
cddccb2
+		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);
cddccb2
+			handler(source,
6a91557
+				&elem->signature_data,
cddccb2
+				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
cddccb2
index 3259ad6..08024c6 100644
6a91557
--- a/include/linux/efi.h
6a91557
+++ b/include/linux/efi.h
cddccb2
@@ -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);
cddccb2
cddccb2
+
cddccb2
+typedef void (*efi_element_handler_t)(const char *source,
cddccb2
+				      const void *element_data,
cddccb2
+				      size_t element_size);
cddccb2
+extern int __init parse_efi_signature_list(
cddccb2
+	const char *source,
cddccb2
+	const void *data, size_t size,
cddccb2
+	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