b1f0987
From b96ff1fd9e94772fde7b58fd69969d1a1c87eb6d Mon Sep 17 00:00:00 2001
b1f0987
From: Dave Young <dyoung@redhat.com>
b1f0987
Date: Tue, 27 Feb 2018 10:04:51 +0000
b1f0987
Subject: [PATCH 07/31] Copy secure_boot flag in boot params across kexec
b1f0987
 reboot
b1f0987
b1f0987
Kexec reboot in case secure boot being enabled does not keep the secure
b1f0987
boot mode in new kernel, so later one can load unsigned kernel via legacy
b1f0987
kexec_load.  In this state, the system is missing the protections provided
b1f0987
by secure boot.
b1f0987
b1f0987
Adding a patch to fix this by retain the secure_boot flag in original
b1f0987
kernel.
b1f0987
b1f0987
secure_boot flag in boot_params is set in EFI stub, but kexec bypasses the
b1f0987
stub.  Fixing this issue by copying secure_boot flag across kexec reboot.
b1f0987
b1f0987
Signed-off-by: Dave Young <dyoung@redhat.com>
b1f0987
Signed-off-by: David Howells <dhowells@redhat.com>
b1f0987
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
b1f0987
cc: kexec@lists.infradead.org
b1f0987
---
b1f0987
 arch/x86/kernel/kexec-bzimage64.c | 1 +
b1f0987
 1 file changed, 1 insertion(+)
b1f0987
b1f0987
diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
b1f0987
index fb095ba0c02f..7d0fac5bcbbe 100644
b1f0987
--- a/arch/x86/kernel/kexec-bzimage64.c
b1f0987
+++ b/arch/x86/kernel/kexec-bzimage64.c
b1f0987
@@ -179,6 +179,7 @@ setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
b1f0987
 	if (efi_enabled(EFI_OLD_MEMMAP))
b1f0987
 		return 0;
b1f0987
b1f0987
+	params->secure_boot = boot_params.secure_boot;
b1f0987
 	ei->efi_loader_signature = current_ei->efi_loader_signature;
b1f0987
 	ei->efi_systab = current_ei->efi_systab;
b1f0987
 	ei->efi_systab_hi = current_ei->efi_systab_hi;
b1f0987
-- 
b1f0987
2.14.3
b1f0987
b1f0987
From 42b2c81c12a8e8139fc7252cf91151c37b5a0966 Mon Sep 17 00:00:00 2001
b1f0987
From: David Howells <dhowells@redhat.com>
b1f0987
Date: Tue, 27 Feb 2018 10:04:55 +0000
b1f0987
Subject: [PATCH 29/31] efi: Add an EFI_SECURE_BOOT flag to indicate secure
b1f0987
 boot mode
b1f0987
b1f0987
UEFI machines can be booted in Secure Boot mode.  Add an EFI_SECURE_BOOT
b1f0987
flag that can be passed to efi_enabled() to find out whether secure boot is
b1f0987
enabled.
b1f0987
b1f0987
Move the switch-statement in x86's setup_arch() that inteprets the
b1f0987
secure_boot boot parameter to generic code and set the bit there.
b1f0987
b1f0987
Suggested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
b1f0987
Signed-off-by: David Howells <dhowells@redhat.com>
b1f0987
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
b1f0987
cc: linux-efi@vger.kernel.org
b1f0987
---
b1f0987
 arch/x86/kernel/setup.c           | 14 +-------------
b1f0987
 drivers/firmware/efi/Makefile     |  1 +
b1f0987
 drivers/firmware/efi/secureboot.c | 38 ++++++++++++++++++++++++++++++++++++++
b1f0987
 include/linux/efi.h               | 16 ++++++++++------
b1f0987
 4 files changed, 50 insertions(+), 19 deletions(-)
b1f0987
 create mode 100644 drivers/firmware/efi/secureboot.c
b1f0987
b1f0987
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
b1f0987
index 1ae67e982af7..a7c240f00d78 100644
b1f0987
--- a/arch/x86/kernel/setup.c
b1f0987
+++ b/arch/x86/kernel/setup.c
b1f0987
@@ -1150,19 +1150,7 @@ void __init setup_arch(char **cmdline_p)
b1f0987
 	/* Allocate bigger log buffer */
b1f0987
 	setup_log_buf(1);
b1f0987
b1f0987
-	if (efi_enabled(EFI_BOOT)) {
b1f0987
-		switch (boot_params.secure_boot) {
b1f0987
-		case efi_secureboot_mode_disabled:
b1f0987
-			pr_info("Secure boot disabled\n");
b1f0987
-			break;
b1f0987
-		case efi_secureboot_mode_enabled:
b1f0987
-			pr_info("Secure boot enabled\n");
b1f0987
-			break;
b1f0987
-		default:
b1f0987
-			pr_info("Secure boot could not be determined\n");
b1f0987
-			break;
b1f0987
-		}
b1f0987
-	}
b1f0987
+	efi_set_secure_boot(boot_params.secure_boot);
b1f0987
b1f0987
 	reserve_initrd();
b1f0987
b1f0987
diff --git a/drivers/firmware/efi/Makefile b/drivers/firmware/efi/Makefile
b1f0987
index cb805374f4bc..da2b3e37b9f0 100644
b1f0987
--- a/drivers/firmware/efi/Makefile
b1f0987
+++ b/drivers/firmware/efi/Makefile
b1f0987
@@ -24,6 +24,7 @@ obj-$(CONFIG_EFI_FAKE_MEMMAP)		+= fake_mem.o
b1f0987
 obj-$(CONFIG_EFI_BOOTLOADER_CONTROL)	+= efibc.o
b1f0987
 obj-$(CONFIG_EFI_TEST)			+= test/
b1f0987
 obj-$(CONFIG_EFI_DEV_PATH_PARSER)	+= dev-path-parser.o
b1f0987
+obj-$(CONFIG_EFI)			+= secureboot.o
b1f0987
 obj-$(CONFIG_APPLE_PROPERTIES)		+= apple-properties.o
b1f0987
b1f0987
 arm-obj-$(CONFIG_EFI)			:= arm-init.o arm-runtime.o
b1f0987
diff --git a/drivers/firmware/efi/secureboot.c b/drivers/firmware/efi/secureboot.c
b1f0987
new file mode 100644
b1f0987
index 000000000000..9070055de0a1
b1f0987
--- /dev/null
b1f0987
+++ b/drivers/firmware/efi/secureboot.c
b1f0987
@@ -0,0 +1,38 @@
b1f0987
+/* Core kernel secure boot support.
b1f0987
+ *
b1f0987
+ * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
b1f0987
+ * Written by David Howells (dhowells@redhat.com)
b1f0987
+ *
b1f0987
+ * This program is free software; you can redistribute it and/or
b1f0987
+ * modify it under the terms of the GNU General Public Licence
b1f0987
+ * as published by the Free Software Foundation; either version
b1f0987
+ * 2 of the Licence, or (at your option) any later version.
b1f0987
+ */
b1f0987
+
b1f0987
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
b1f0987
+
b1f0987
+#include <linux/efi.h>
b1f0987
+#include <linux/kernel.h>
b1f0987
+#include <linux/printk.h>
b1f0987
+
b1f0987
+/*
b1f0987
+ * Decide what to do when UEFI secure boot mode is enabled.
b1f0987
+ */
b1f0987
+void __init efi_set_secure_boot(enum efi_secureboot_mode mode)
b1f0987
+{
b1f0987
+	if (efi_enabled(EFI_BOOT)) {
b1f0987
+		switch (mode) {
b1f0987
+		case efi_secureboot_mode_disabled:
b1f0987
+			pr_info("Secure boot disabled\n");
b1f0987
+			break;
b1f0987
+		case efi_secureboot_mode_enabled:
b1f0987
+			set_bit(EFI_SECURE_BOOT, &efi.flags);
b1f0987
+			pr_info("Secure boot enabled\n");
b1f0987
+			break;
b1f0987
+		default:
b1f0987
+			pr_warning("Secure boot could not be determined (mode %u)\n",
b1f0987
+				   mode);
b1f0987
+			break;
b1f0987
+		}
b1f0987
+	}
b1f0987
+}
b1f0987
diff --git a/include/linux/efi.h b/include/linux/efi.h
b1f0987
index f5083aa72eae..79da76d14ca3 100644
b1f0987
--- a/include/linux/efi.h
b1f0987
+++ b/include/linux/efi.h
b1f0987
@@ -1142,6 +1142,14 @@ extern int __init efi_setup_pcdp_console(char *);
b1f0987
 #define EFI_DBG			8	/* Print additional debug info at runtime */
b1f0987
 #define EFI_NX_PE_DATA		9	/* Can runtime data regions be mapped non-executable? */
b1f0987
 #define EFI_MEM_ATTR		10	/* Did firmware publish an EFI_MEMORY_ATTRIBUTES table? */
b1f0987
+#define EFI_SECURE_BOOT		11	/* Are we in Secure Boot mode? */
b1f0987
+
b1f0987
+enum efi_secureboot_mode {
b1f0987
+	efi_secureboot_mode_unset,
b1f0987
+	efi_secureboot_mode_unknown,
b1f0987
+	efi_secureboot_mode_disabled,
b1f0987
+	efi_secureboot_mode_enabled,
b1f0987
+};
b1f0987
b1f0987
 #ifdef CONFIG_EFI
b1f0987
 /*
b1f0987
@@ -1154,6 +1162,7 @@ static inline bool efi_enabled(int feature)
b1f0987
 extern void efi_reboot(enum reboot_mode reboot_mode, const char *__unused);
b1f0987
b1f0987
 extern bool efi_is_table_address(unsigned long phys_addr);
b1f0987
+extern void __init efi_set_secure_boot(enum efi_secureboot_mode mode);
b1f0987
 #else
b1f0987
 static inline bool efi_enabled(int feature)
b1f0987
 {
b1f0987
@@ -1172,6 +1181,7 @@ static inline bool efi_is_table_address(unsigned long phys_addr)
b1f0987
 {
b1f0987
 	return false;
b1f0987
 }
b1f0987
+static inline void efi_set_secure_boot(enum efi_secureboot_mode mode) {}
b1f0987
 #endif
b1f0987
b1f0987
 extern int efi_status_to_err(efi_status_t status);
b1f0987
@@ -1557,12 +1567,6 @@ efi_status_t efi_setup_gop(efi_system_table_t *sys_table_arg,
b1f0987
 bool efi_runtime_disabled(void);
b1f0987
 extern void efi_call_virt_check_flags(unsigned long flags, const char *call);
b1f0987
b1f0987
-enum efi_secureboot_mode {
b1f0987
-	efi_secureboot_mode_unset,
b1f0987
-	efi_secureboot_mode_unknown,
b1f0987
-	efi_secureboot_mode_disabled,
b1f0987
-	efi_secureboot_mode_enabled,
b1f0987
-};
b1f0987
 enum efi_secureboot_mode efi_get_secureboot(efi_system_table_t *sys_table);
b1f0987
b1f0987
 #ifdef CONFIG_RESET_ATTACK_MITIGATION
b1f0987
-- 
b1f0987
2.14.3
b1f0987
b1f0987
From d78bf678059f83e22bec8ada1a448e22b9b90203 Mon Sep 17 00:00:00 2001
b1f0987
From: David Howells <dhowells@redhat.com>
b1f0987
Date: Tue, 27 Feb 2018 10:04:55 +0000
b1f0987
Subject: [PATCH 30/31] efi: Lock down the kernel if booted in secure boot mode
b1f0987
b1f0987
UEFI Secure Boot provides a mechanism for ensuring that the firmware will
b1f0987
only load signed bootloaders and kernels.  Certain use cases may also
b1f0987
require that all kernel modules also be signed.  Add a configuration option
b1f0987
that to lock down the kernel - which includes requiring validly signed
b1f0987
modules - if the kernel is secure-booted.
b1f0987
b1f0987
Signed-off-by: David Howells <dhowells@redhat.com>
b1f0987
Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
b1f0987
cc: linux-efi@vger.kernel.org
b1f0987
---
b1f0987
 arch/x86/kernel/setup.c |  6 ++++--
b1f0987
 fs/debugfs/inode.c      |  2 +-
b1f0987
 security/Kconfig        | 14 ++++++++++++++
b1f0987
 security/lock_down.c    |  5 +++++
b1f0987
 4 files changed, 20 insertions(+), 3 deletions(-)
b1f0987
b1f0987
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
b1f0987
index a7c240f00d78..1277d1857c5c 100644
b1f0987
--- a/arch/x86/kernel/setup.c
b1f0987
+++ b/arch/x86/kernel/setup.c
b1f0987
@@ -64,6 +64,7 @@
b1f0987
 #include <linux/dma-mapping.h>
b1f0987
 #include <linux/ctype.h>
b1f0987
 #include <linux/uaccess.h>
b1f0987
+#include <linux/security.h>
b1f0987
b1f0987
 #include <linux/percpu.h>
b1f0987
 #include <linux/crash_dump.h>
b1f0987
@@ -997,6 +998,8 @@ void __init setup_arch(char **cmdline_p)
b1f0987
 	if (efi_enabled(EFI_BOOT))
b1f0987
 		efi_init();
b1f0987
b1f0987
+	efi_set_secure_boot(boot_params.secure_boot);
b1f0987
+
b1f0987
 	init_lockdown();
b1f0987
b1f0987
 	dmi_scan_machine();
b1f0987
@@ -1150,8 +1154,6 @@ void __init setup_arch(char **cmdline_p)
b1f0987
 	/* Allocate bigger log buffer */
b1f0987
 	setup_log_buf(1);
b1f0987
b1f0987
-	efi_set_secure_boot(boot_params.secure_boot);
b1f0987
-
b1f0987
 	reserve_initrd();
b1f0987
b1f0987
 	acpi_table_upgrade();
b1f0987
diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
b1f0987
index ce261e1765ff..7aff55b309a6 100644
b1f0987
--- a/fs/debugfs/inode.c
b1f0987
+++ b/fs/debugfs/inode.c
b1f0987
@@ -40,7 +40,7 @@ static bool debugfs_registered;
b1f0987
 static int debugfs_setattr(struct dentry *dentry, struct iattr *ia)
b1f0987
 {
b1f0987
 	if ((ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) &&
b1f0987
-	    kernel_is_locked_down("debugfs"))
b1f0987
+	    kernel_is_locked_down("changing perms in debugfs"))
b1f0987
 		return -EPERM;
b1f0987
 	return simple_setattr(dentry, ia);
b1f0987
 }
b1f0987
diff --git a/security/Kconfig b/security/Kconfig
b1f0987
index 461d5acc3616..13fdada1ffc2 100644
b1f0987
--- a/security/Kconfig
b1f0987
+++ b/security/Kconfig
b1f0987
@@ -248,6 +248,20 @@ config ALLOW_LOCKDOWN_LIFT_BY_SYSRQ
b1f0987
 	  Allow the lockdown on a kernel to be lifted, by pressing a SysRq key
b1f0987
 	  combination on a wired keyboard.  On x86, this is SysRq+x.
b1f0987
b1f0987
+config LOCK_DOWN_IN_EFI_SECURE_BOOT
b1f0987
+	bool "Lock down the kernel in EFI Secure Boot mode"
b1f0987
+	default n
b1f0987
+	select LOCK_DOWN_KERNEL
b1f0987
+	depends on EFI
b1f0987
+	help
b1f0987
+	  UEFI Secure Boot provides a mechanism for ensuring that the firmware
b1f0987
+	  will only load signed bootloaders and kernels.  Secure boot mode may
b1f0987
+	  be determined from EFI variables provided by the system firmware if
b1f0987
+	  not indicated by the boot parameters.
b1f0987
+
b1f0987
+	  Enabling this option turns on results in kernel lockdown being
b1f0987
+	  triggered if EFI Secure Boot is set.
b1f0987
+
b1f0987
b1f0987
 source security/selinux/Kconfig
b1f0987
 source security/smack/Kconfig
b1f0987
diff --git a/security/lock_down.c b/security/lock_down.c
b1f0987
index 2c6b00f0c229..527f7e51dc8d 100644
b1f0987
--- a/security/lock_down.c
b1f0987
+++ b/security/lock_down.c
b1f0987
@@ -12,6 +12,7 @@
b1f0987
 #include <linux/export.h>
b1f0987
 #include <linux/sched.h>
b1f0987
 #include <linux/sysrq.h>
b1f0987
+#include <linux/efi.h>
b1f0987
 #include <asm/setup.h>
b1f0987
b1f0987
 #ifndef CONFIG_LOCK_DOWN_MANDATORY
b1f0987
@@ -55,6 +55,10 @@ void __init init_lockdown(void)
b1f0987
 #ifdef CONFIG_LOCK_DOWN_MANDATORY
b1f0987
 	pr_notice("Kernel is locked down from config; see man kernel_lockdown.7\n");
b1f0987
 #endif
b1f0987
+#ifdef CONFIG_LOCK_DOWN_IN_EFI_SECURE_BOOT
b1f0987
+	if (efi_enabled(EFI_SECURE_BOOT))
b1f0987
+		lock_kernel_down("EFI secure boot");
b1f0987
+#endif
b1f0987
 }
b1f0987
b1f0987
 /**
b1f0987
-- 
b1f0987
2.14.3