chengshan / rpms / kernel

Forked from rpms/kernel 2 years ago
Clone
Justin Vreeland 794d92
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
Justin Vreeland 794d92
From: David Howells <dhowells@redhat.com>
Justin Vreeland 794d92
Date: Tue, 27 Feb 2018 10:04:55 +0000
Justin Vreeland 794d92
Subject: [PATCH] efi: Add an EFI_SECURE_BOOT flag to indicate secure boot mode
Justin Vreeland 794d92
Justin Vreeland 794d92
UEFI machines can be booted in Secure Boot mode.  Add an EFI_SECURE_BOOT
Justin Vreeland 794d92
flag that can be passed to efi_enabled() to find out whether secure boot is
Justin Vreeland 794d92
enabled.
Justin Vreeland 794d92
Justin Vreeland 794d92
Move the switch-statement in x86's setup_arch() that inteprets the
Justin Vreeland 794d92
secure_boot boot parameter to generic code and set the bit there.
Justin Vreeland 794d92
Justin Vreeland 794d92
Upstream Status: RHEL only
Justin Vreeland 794d92
Suggested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Justin Vreeland 794d92
Signed-off-by: David Howells <dhowells@redhat.com>
Justin Vreeland 794d92
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Justin Vreeland 794d92
cc: linux-efi@vger.kernel.org
Justin Vreeland 794d92
[Rebased for context; efi_is_table_address was moved to arch/x86]
Justin Vreeland 794d92
Signed-off-by: Jeremy Cline <jcline@redhat.com>
Justin Vreeland 794d92
---
Justin Vreeland 794d92
 arch/x86/kernel/setup.c           | 14 +-----------
Justin Vreeland 794d92
 drivers/firmware/efi/Makefile     |  1 +
Justin Vreeland 794d92
 drivers/firmware/efi/secureboot.c | 38 +++++++++++++++++++++++++++++++
Justin Vreeland 794d92
 include/linux/efi.h               | 18 ++++++++++-----
Justin Vreeland 794d92
 4 files changed, 52 insertions(+), 19 deletions(-)
Justin Vreeland 794d92
 create mode 100644 drivers/firmware/efi/secureboot.c
Justin Vreeland 794d92
Justin Vreeland 794d92
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
Justin Vreeland 794d92
index 112efbef3414..c9de4b36ca51 100644
Justin Vreeland 794d92
--- a/arch/x86/kernel/setup.c
Justin Vreeland 794d92
+++ b/arch/x86/kernel/setup.c
Justin Vreeland 794d92
@@ -1255,19 +1255,7 @@ void __init setup_arch(char **cmdline_p)
Justin Vreeland 794d92
 	/* Allocate bigger log buffer */
Justin Vreeland 794d92
 	setup_log_buf(1);
Justin Vreeland 794d92
Justin Vreeland 794d92
-	if (efi_enabled(EFI_BOOT)) {
Justin Vreeland 794d92
-		switch (boot_params.secure_boot) {
Justin Vreeland 794d92
-		case efi_secureboot_mode_disabled:
Justin Vreeland 794d92
-			pr_info("Secure boot disabled\n");
Justin Vreeland 794d92
-			break;
Justin Vreeland 794d92
-		case efi_secureboot_mode_enabled:
Justin Vreeland 794d92
-			pr_info("Secure boot enabled\n");
Justin Vreeland 794d92
-			break;
Justin Vreeland 794d92
-		default:
Justin Vreeland 794d92
-			pr_info("Secure boot could not be determined\n");
Justin Vreeland 794d92
-			break;
Justin Vreeland 794d92
-		}
Justin Vreeland 794d92
-	}
Justin Vreeland 794d92
+	efi_set_secure_boot(boot_params.secure_boot);
Justin Vreeland 794d92
Justin Vreeland 794d92
 	reserve_initrd();
Justin Vreeland 794d92
Justin Vreeland 794d92
diff --git a/drivers/firmware/efi/Makefile b/drivers/firmware/efi/Makefile
Justin Vreeland 794d92
index 7a216984552b..f0ef02d733af 100644
Justin Vreeland 794d92
--- a/drivers/firmware/efi/Makefile
Justin Vreeland 794d92
+++ b/drivers/firmware/efi/Makefile
Justin Vreeland 794d92
@@ -25,6 +25,7 @@ obj-$(CONFIG_EFI_FAKE_MEMMAP)		+= fake_map.o
Justin Vreeland 794d92
 obj-$(CONFIG_EFI_BOOTLOADER_CONTROL)	+= efibc.o
Justin Vreeland 794d92
 obj-$(CONFIG_EFI_TEST)			+= test/
Justin Vreeland 794d92
 obj-$(CONFIG_EFI_DEV_PATH_PARSER)	+= dev-path-parser.o
Justin Vreeland 794d92
+obj-$(CONFIG_EFI)			+= secureboot.o
Justin Vreeland 794d92
 obj-$(CONFIG_APPLE_PROPERTIES)		+= apple-properties.o
Justin Vreeland 794d92
 obj-$(CONFIG_EFI_RCI2_TABLE)		+= rci2-table.o
Justin Vreeland 794d92
 obj-$(CONFIG_EFI_EMBEDDED_FIRMWARE)	+= embedded-firmware.o
Justin Vreeland 794d92
diff --git a/drivers/firmware/efi/secureboot.c b/drivers/firmware/efi/secureboot.c
Justin Vreeland 794d92
new file mode 100644
Justin Vreeland 794d92
index 000000000000..de0a3714a5d4
Justin Vreeland 794d92
--- /dev/null
Justin Vreeland 794d92
+++ b/drivers/firmware/efi/secureboot.c
Justin Vreeland 794d92
@@ -0,0 +1,38 @@
Justin Vreeland 794d92
+/* Core kernel secure boot support.
Justin Vreeland 794d92
+ *
Justin Vreeland 794d92
+ * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
Justin Vreeland 794d92
+ * Written by David Howells (dhowells@redhat.com)
Justin Vreeland 794d92
+ *
Justin Vreeland 794d92
+ * This program is free software; you can redistribute it and/or
Justin Vreeland 794d92
+ * modify it under the terms of the GNU General Public Licence
Justin Vreeland 794d92
+ * as published by the Free Software Foundation; either version
Justin Vreeland 794d92
+ * 2 of the Licence, or (at your option) any later version.
Justin Vreeland 794d92
+ */
Justin Vreeland 794d92
+
Justin Vreeland 794d92
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Justin Vreeland 794d92
+
Justin Vreeland 794d92
+#include <linux/efi.h>
Justin Vreeland 794d92
+#include <linux/kernel.h>
Justin Vreeland 794d92
+#include <linux/printk.h>
Justin Vreeland 794d92
+
Justin Vreeland 794d92
+/*
Justin Vreeland 794d92
+ * Decide what to do when UEFI secure boot mode is enabled.
Justin Vreeland 794d92
+ */
Justin Vreeland 794d92
+void __init efi_set_secure_boot(enum efi_secureboot_mode mode)
Justin Vreeland 794d92
+{
Justin Vreeland 794d92
+	if (efi_enabled(EFI_BOOT)) {
Justin Vreeland 794d92
+		switch (mode) {
Justin Vreeland 794d92
+		case efi_secureboot_mode_disabled:
Justin Vreeland 794d92
+			pr_info("Secure boot disabled\n");
Justin Vreeland 794d92
+			break;
Justin Vreeland 794d92
+		case efi_secureboot_mode_enabled:
Justin Vreeland 794d92
+			set_bit(EFI_SECURE_BOOT, &efi.flags);
Justin Vreeland 794d92
+			pr_info("Secure boot enabled\n");
Justin Vreeland 794d92
+			break;
Justin Vreeland 794d92
+		default:
Justin Vreeland 794d92
+			pr_warn("Secure boot could not be determined (mode %u)\n",
Justin Vreeland 794d92
+				   mode);
Justin Vreeland 794d92
+			break;
Justin Vreeland 794d92
+		}
Justin Vreeland 794d92
+	}
Justin Vreeland 794d92
+}
Justin Vreeland 794d92
diff --git a/include/linux/efi.h b/include/linux/efi.h
Justin Vreeland 794d92
index 92aa4697f558..1cdc5d8b6ac3 100644
Justin Vreeland 794d92
--- a/include/linux/efi.h
Justin Vreeland 794d92
+++ b/include/linux/efi.h
Justin Vreeland 794d92
@@ -785,6 +785,14 @@ extern int __init efi_setup_pcdp_console(char *);
Justin Vreeland 794d92
 #define EFI_MEM_ATTR		10	/* Did firmware publish an EFI_MEMORY_ATTRIBUTES table? */
Justin Vreeland 794d92
 #define EFI_MEM_NO_SOFT_RESERVE	11	/* Is the kernel configured to ignore soft reservations? */
Justin Vreeland 794d92
 #define EFI_PRESERVE_BS_REGIONS	12	/* Are EFI boot-services memory segments available? */
Justin Vreeland 794d92
+#define EFI_SECURE_BOOT		13	/* Are we in Secure Boot mode? */
Justin Vreeland 794d92
+
Justin Vreeland 794d92
+enum efi_secureboot_mode {
Justin Vreeland 794d92
+	efi_secureboot_mode_unset,
Justin Vreeland 794d92
+	efi_secureboot_mode_unknown,
Justin Vreeland 794d92
+	efi_secureboot_mode_disabled,
Justin Vreeland 794d92
+	efi_secureboot_mode_enabled,
Justin Vreeland 794d92
+};
Justin Vreeland 794d92
Justin Vreeland 794d92
 #ifdef CONFIG_EFI
Justin Vreeland 794d92
 /*
Justin Vreeland 794d92
@@ -796,6 +804,8 @@ static inline bool efi_enabled(int feature)
Justin Vreeland 794d92
 }
Justin Vreeland 794d92
 extern void efi_reboot(enum reboot_mode reboot_mode, const char *__unused);
Justin Vreeland 794d92
Justin Vreeland 794d92
+extern void __init efi_set_secure_boot(enum efi_secureboot_mode mode);
Justin Vreeland 794d92
+
Justin Vreeland 794d92
 bool __pure __efi_soft_reserve_enabled(void);
Justin Vreeland 794d92
Justin Vreeland 794d92
 static inline bool __pure efi_soft_reserve_enabled(void)
Justin Vreeland 794d92
@@ -822,6 +832,8 @@ efi_capsule_pending(int *reset_type)
Justin Vreeland 794d92
 	return false;
Justin Vreeland 794d92
 }
Justin Vreeland 794d92
Justin Vreeland 794d92
+static inline void efi_set_secure_boot(enum efi_secureboot_mode mode) {}
Justin Vreeland 794d92
+
Justin Vreeland 794d92
 static inline bool efi_soft_reserve_enabled(void)
Justin Vreeland 794d92
 {
Justin Vreeland 794d92
 	return false;
Justin Vreeland 794d92
@@ -1094,12 +1106,6 @@ static inline bool efi_runtime_disabled(void) { return true; }
Justin Vreeland 794d92
 extern void efi_call_virt_check_flags(unsigned long flags, const char *call);
Justin Vreeland 794d92
 extern unsigned long efi_call_virt_save_flags(void);
Justin Vreeland 794d92
Justin Vreeland 794d92
-enum efi_secureboot_mode {
Justin Vreeland 794d92
-	efi_secureboot_mode_unset,
Justin Vreeland 794d92
-	efi_secureboot_mode_unknown,
Justin Vreeland 794d92
-	efi_secureboot_mode_disabled,
Justin Vreeland 794d92
-	efi_secureboot_mode_enabled,
Justin Vreeland 794d92
-};
Justin Vreeland 794d92
 enum efi_secureboot_mode efi_get_secureboot(void);
Justin Vreeland 794d92
Justin Vreeland 794d92
 #ifdef CONFIG_RESET_ATTACK_MITIGATION
Justin Vreeland 794d92
-- 
Justin Vreeland 794d92
2.28.0
Justin Vreeland 794d92