ignatenkobrain / rpms / kernel

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