3220c13
From b5123d0553f4ed5e734f6457696cdd30228d1eee Mon Sep 17 00:00:00 2001
c67a869
From: David Howells <dhowells@redhat.com>
c67a869
Date: Tue, 27 Feb 2018 10:04:55 +0000
c67a869
Subject: [PATCH 29/31] efi: Add an EFI_SECURE_BOOT flag to indicate secure
c67a869
 boot mode
c67a869
c67a869
UEFI machines can be booted in Secure Boot mode.  Add an EFI_SECURE_BOOT
c67a869
flag that can be passed to efi_enabled() to find out whether secure boot is
c67a869
enabled.
c67a869
c67a869
Move the switch-statement in x86's setup_arch() that inteprets the
c67a869
secure_boot boot parameter to generic code and set the bit there.
c67a869
c67a869
Suggested-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
c67a869
Signed-off-by: David Howells <dhowells@redhat.com>
c67a869
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
c67a869
cc: linux-efi@vger.kernel.org
3220c13
Signed-off-by: Jeremy Cline <jcline@redhat.com>
c67a869
---
3220c13
 arch/x86/kernel/setup.c           | 14 +-----------
c67a869
 drivers/firmware/efi/Makefile     |  1 +
3220c13
 drivers/firmware/efi/secureboot.c | 38 +++++++++++++++++++++++++++++++
3220c13
 include/linux/efi.h               | 18 ++++++++++-----
3220c13
 4 files changed, 52 insertions(+), 19 deletions(-)
c67a869
 create mode 100644 drivers/firmware/efi/secureboot.c
c67a869
c67a869
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
3220c13
index b74e7bfed6ab..7da1712c89c9 100644
c67a869
--- a/arch/x86/kernel/setup.c
c67a869
+++ b/arch/x86/kernel/setup.c
3220c13
@@ -1158,19 +1158,7 @@ void __init setup_arch(char **cmdline_p)
c67a869
 	/* Allocate bigger log buffer */
c67a869
 	setup_log_buf(1);
3220c13
 
c67a869
-	if (efi_enabled(EFI_BOOT)) {
c67a869
-		switch (boot_params.secure_boot) {
c67a869
-		case efi_secureboot_mode_disabled:
c67a869
-			pr_info("Secure boot disabled\n");
c67a869
-			break;
c67a869
-		case efi_secureboot_mode_enabled:
c67a869
-			pr_info("Secure boot enabled\n");
c67a869
-			break;
c67a869
-		default:
c67a869
-			pr_info("Secure boot could not be determined\n");
c67a869
-			break;
c67a869
-		}
c67a869
-	}
c67a869
+	efi_set_secure_boot(boot_params.secure_boot);
3220c13
 
c67a869
 	reserve_initrd();
3220c13
 
c67a869
diff --git a/drivers/firmware/efi/Makefile b/drivers/firmware/efi/Makefile
3220c13
index 5f9f5039de50..7a0a6378203e 100644
c67a869
--- a/drivers/firmware/efi/Makefile
c67a869
+++ b/drivers/firmware/efi/Makefile
c67a869
@@ -24,6 +24,7 @@ obj-$(CONFIG_EFI_FAKE_MEMMAP)		+= fake_mem.o
c67a869
 obj-$(CONFIG_EFI_BOOTLOADER_CONTROL)	+= efibc.o
c67a869
 obj-$(CONFIG_EFI_TEST)			+= test/
c67a869
 obj-$(CONFIG_EFI_DEV_PATH_PARSER)	+= dev-path-parser.o
c67a869
+obj-$(CONFIG_EFI)			+= secureboot.o
c67a869
 obj-$(CONFIG_APPLE_PROPERTIES)		+= apple-properties.o
3220c13
 
c67a869
 arm-obj-$(CONFIG_EFI)			:= arm-init.o arm-runtime.o
c67a869
diff --git a/drivers/firmware/efi/secureboot.c b/drivers/firmware/efi/secureboot.c
c67a869
new file mode 100644
c67a869
index 000000000000..9070055de0a1
c67a869
--- /dev/null
c67a869
+++ b/drivers/firmware/efi/secureboot.c
c67a869
@@ -0,0 +1,38 @@
c67a869
+/* Core kernel secure boot support.
c67a869
+ *
c67a869
+ * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
c67a869
+ * Written by David Howells (dhowells@redhat.com)
c67a869
+ *
c67a869
+ * This program is free software; you can redistribute it and/or
c67a869
+ * modify it under the terms of the GNU General Public Licence
c67a869
+ * as published by the Free Software Foundation; either version
c67a869
+ * 2 of the Licence, or (at your option) any later version.
c67a869
+ */
c67a869
+
c67a869
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
c67a869
+
c67a869
+#include <linux/efi.h>
c67a869
+#include <linux/kernel.h>
c67a869
+#include <linux/printk.h>
c67a869
+
c67a869
+/*
c67a869
+ * Decide what to do when UEFI secure boot mode is enabled.
c67a869
+ */
c67a869
+void __init efi_set_secure_boot(enum efi_secureboot_mode mode)
c67a869
+{
c67a869
+	if (efi_enabled(EFI_BOOT)) {
c67a869
+		switch (mode) {
c67a869
+		case efi_secureboot_mode_disabled:
c67a869
+			pr_info("Secure boot disabled\n");
c67a869
+			break;
c67a869
+		case efi_secureboot_mode_enabled:
c67a869
+			set_bit(EFI_SECURE_BOOT, &efi.flags);
c67a869
+			pr_info("Secure boot enabled\n");
c67a869
+			break;
c67a869
+		default:
c67a869
+			pr_warning("Secure boot could not be determined (mode %u)\n",
c67a869
+				   mode);
c67a869
+			break;
c67a869
+		}
c67a869
+	}
c67a869
+}
c67a869
diff --git a/include/linux/efi.h b/include/linux/efi.h
3220c13
index 100ce4a4aff6..62361b647a75 100644
c67a869
--- a/include/linux/efi.h
c67a869
+++ b/include/linux/efi.h
3220c13
@@ -1155,6 +1155,14 @@ extern int __init efi_setup_pcdp_console(char *);
c67a869
 #define EFI_DBG			8	/* Print additional debug info at runtime */
c67a869
 #define EFI_NX_PE_DATA		9	/* Can runtime data regions be mapped non-executable? */
c67a869
 #define EFI_MEM_ATTR		10	/* Did firmware publish an EFI_MEMORY_ATTRIBUTES table? */
c67a869
+#define EFI_SECURE_BOOT		11	/* Are we in Secure Boot mode? */
c67a869
+
c67a869
+enum efi_secureboot_mode {
c67a869
+	efi_secureboot_mode_unset,
c67a869
+	efi_secureboot_mode_unknown,
c67a869
+	efi_secureboot_mode_disabled,
c67a869
+	efi_secureboot_mode_enabled,
c67a869
+};
3220c13
 
c67a869
 #ifdef CONFIG_EFI
c67a869
 /*
2d62e0d
@@ -1198,6 +1206,8 @@ static inline bool efi_enabled(int feature)
2d62e0d
 extern void efi_reboot(enum reboot_mode reboot_mode, const char *__unused);
2d62e0d
c67a869
 extern bool efi_is_table_address(unsigned long phys_addr);
3220c13
+
c67a869
+extern void __init efi_set_secure_boot(enum efi_secureboot_mode mode);
c67a869
 #else
c67a869
 static inline bool efi_enabled(int feature)
c67a869
 {
2d62e0d
@@ -1216,6 +1226,8 @@ static inline bool efi_is_table_address(unsigned long phys_addr)
c67a869
 {
2d62e0d
 	return false;
c67a869
 }
3220c13
+
c67a869
+static inline void efi_set_secure_boot(enum efi_secureboot_mode mode) {}
c67a869
 #endif
3220c13
 
c67a869
 extern int efi_status_to_err(efi_status_t status);
3220c13
@@ -1577,12 +1589,6 @@ efi_status_t efi_setup_gop(efi_system_table_t *sys_table_arg,
Jeremy Cline 6f9babc
 #endif
Jeremy Cline 6f9babc
 
c67a869
 extern void efi_call_virt_check_flags(unsigned long flags, const char *call);
Jeremy Cline 6f9babc
 extern unsigned long efi_call_virt_save_flags(void);
3220c13
 
c67a869
-enum efi_secureboot_mode {
c67a869
-	efi_secureboot_mode_unset,
c67a869
-	efi_secureboot_mode_unknown,
c67a869
-	efi_secureboot_mode_disabled,
c67a869
-	efi_secureboot_mode_enabled,
c67a869
-};
c67a869
 enum efi_secureboot_mode efi_get_secureboot(efi_system_table_t *sys_table);
3220c13
 
c67a869
 #ifdef CONFIG_RESET_ATTACK_MITIGATION
c67a869
-- 
3220c13
2.19.1
c67a869
c67a869
From d78bf678059f83e22bec8ada1a448e22b9b90203 Mon Sep 17 00:00:00 2001
c67a869
From: David Howells <dhowells@redhat.com>
c67a869
Date: Tue, 27 Feb 2018 10:04:55 +0000
c67a869
Subject: [PATCH 30/31] efi: Lock down the kernel if booted in secure boot mode
c67a869
c67a869
UEFI Secure Boot provides a mechanism for ensuring that the firmware will
c67a869
only load signed bootloaders and kernels.  Certain use cases may also
c67a869
require that all kernel modules also be signed.  Add a configuration option
c67a869
that to lock down the kernel - which includes requiring validly signed
c67a869
modules - if the kernel is secure-booted.
c67a869
c67a869
Signed-off-by: David Howells <dhowells@redhat.com>
c67a869
Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
c67a869
cc: linux-efi@vger.kernel.org
c67a869
---
c67a869
 arch/x86/kernel/setup.c |  6 ++++--
c67a869
 fs/debugfs/inode.c      |  2 +-
c67a869
 security/Kconfig        | 14 ++++++++++++++
c67a869
 security/lock_down.c    |  5 +++++
c67a869
 4 files changed, 20 insertions(+), 3 deletions(-)
c67a869
c67a869
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
Jeremy Cline 6f9babc
index adeee6329f55..27a54ec878bd 100644
c67a869
--- a/arch/x86/kernel/setup.c
c67a869
+++ b/arch/x86/kernel/setup.c
Jeremy Cline 6f9babc
@@ -65,6 +65,7 @@
c67a869
 #include <linux/dma-mapping.h>
c67a869
 #include <linux/ctype.h>
c67a869
 #include <linux/uaccess.h>
c67a869
+#include <linux/security.h>
Jeremy Cline 6f9babc
 
c67a869
 #include <linux/percpu.h>
c67a869
 #include <linux/crash_dump.h>
Jeremy Cline 6f9babc
@@ -1005,6 +1006,10 @@ void __init setup_arch(char **cmdline_p)
c67a869
 	if (efi_enabled(EFI_BOOT))
c67a869
 		efi_init();
Jeremy Cline 6f9babc
 
c67a869
+	efi_set_secure_boot(boot_params.secure_boot);
c67a869
+
Jeremy Cline 6f9babc
+	init_lockdown();
Jeremy Cline 6f9babc
+
5c2ab4e
 	dmi_setup();
5c2ab4e
 
5c2ab4e
 	/*
Jeremy Cline 6f9babc
@@ -1159,8 +1164,6 @@ void __init setup_arch(char **cmdline_p)
c67a869
 	/* Allocate bigger log buffer */
c67a869
 	setup_log_buf(1);
Jeremy Cline 6f9babc
 
c67a869
-	efi_set_secure_boot(boot_params.secure_boot);
c67a869
-
c67a869
 	reserve_initrd();
Jeremy Cline 6f9babc
 
c67a869
 	acpi_table_upgrade();
c67a869
diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
c67a869
index ce261e1765ff..7aff55b309a6 100644
c67a869
--- a/fs/debugfs/inode.c
c67a869
+++ b/fs/debugfs/inode.c
c67a869
@@ -40,7 +40,7 @@ static bool debugfs_registered;
c67a869
 static int debugfs_setattr(struct dentry *dentry, struct iattr *ia)
c67a869
 {
c67a869
 	if ((ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) &&
c67a869
-	    kernel_is_locked_down("debugfs"))
c67a869
+	    kernel_is_locked_down("changing perms in debugfs"))
c67a869
 		return -EPERM;
c67a869
 	return simple_setattr(dentry, ia);
c67a869
 }
c67a869
diff --git a/security/Kconfig b/security/Kconfig
Jeremy Cline 6f9babc
index 9c343f262bdd..30788bc47863 100644
c67a869
--- a/security/Kconfig
c67a869
+++ b/security/Kconfig
Jeremy Cline 6f9babc
@@ -244,6 +244,20 @@ config LOCK_DOWN_KERNEL_FORCE
5c2ab4e
           Allow the lockdown on a kernel to be lifted, by pressing a SysRq key
5c2ab4e
           combination on a wired keyboard.  On x86, this is SysRq+x.
Jeremy Cline 6f9babc
 
c67a869
+config LOCK_DOWN_IN_EFI_SECURE_BOOT
c67a869
+	bool "Lock down the kernel in EFI Secure Boot mode"
c67a869
+	default n
c67a869
+	select LOCK_DOWN_KERNEL
c67a869
+	depends on EFI
c67a869
+	help
c67a869
+	  UEFI Secure Boot provides a mechanism for ensuring that the firmware
c67a869
+	  will only load signed bootloaders and kernels.  Secure boot mode may
c67a869
+	  be determined from EFI variables provided by the system firmware if
c67a869
+	  not indicated by the boot parameters.
c67a869
+
c67a869
+	  Enabling this option turns on results in kernel lockdown being
c67a869
+	  triggered if EFI Secure Boot is set.
c67a869
+
2d62e0d
 source "security/selinux/Kconfig"
2d62e0d
 source "security/smack/Kconfig"
Jeremy Cline 6f9babc
 source "security/tomoyo/Kconfig"
c67a869
diff --git a/security/lock_down.c b/security/lock_down.c
Jeremy Cline 6f9babc
index ee00ca2677e7..bb4dc7838f3e 100644
c67a869
--- a/security/lock_down.c
c67a869
+++ b/security/lock_down.c
c67a869
@@ -12,6 +12,7 @@
Jeremy Cline 6f9babc
 
Jeremy Cline 6f9babc
 #include <linux/security.h>
c67a869
 #include <linux/export.h>
c67a869
+#include <linux/efi.h>
5c2ab4e
 #include <linux/sysrq.h>
5c2ab4e
 #include <asm/setup.h>
Jeremy Cline 6f9babc
 
Jeremy Cline 6f9babc
@@ -44,6 +45,10 @@ void __init init_lockdown(void)
Jeremy Cline 6f9babc
 #ifdef CONFIG_LOCK_DOWN_FORCE
Jeremy Cline 6f9babc
 	lock_kernel_down("Kernel configuration");
c67a869
 #endif
c67a869
+#ifdef CONFIG_LOCK_DOWN_IN_EFI_SECURE_BOOT
c67a869
+	if (efi_enabled(EFI_SECURE_BOOT))
15597c8
+		lock_kernel_down("EFI secure boot");
c67a869
+#endif
c67a869
 }
Jeremy Cline 6f9babc
 
c67a869
 /**
c67a869
-- 
c67a869
2.14.3