c796f87
From df7d76ae50f18d4465e59fdf7f19d3df44906cb5 Mon Sep 17 00:00:00 2001
962ea4f
From: Josh Boyer <jwboyer@fedoraproject.org>
962ea4f
Date: Mon, 21 Nov 2016 23:55:55 +0000
962ea4f
Subject: [PATCH 07/32] efi: Add EFI_SECURE_BOOT bit
962ea4f
962ea4f
UEFI machines can be booted in Secure Boot mode.  Add a EFI_SECURE_BOOT bit
962ea4f
that can be passed to efi_enabled() to find out whether secure boot is
962ea4f
enabled.
962ea4f
962ea4f
This will be used by the SysRq+x handler, registered by the x86 arch, to find
962ea4f
out whether secure boot mode is enabled so that it can be disabled.
962ea4f
962ea4f
Signed-off-by: Josh Boyer <jwboyer@fedoraproject.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
c796f87
 arch/x86/kernel/setup.c | 1 +
c796f87
 include/linux/efi.h     | 1 +
c796f87
 2 files changed, 2 insertions(+)
962ea4f
962ea4f
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
cd3596b
index 69780ed..447905e 100644
962ea4f
--- a/arch/x86/kernel/setup.c
962ea4f
+++ b/arch/x86/kernel/setup.c
cd3596b
@@ -1182,6 +1182,7 @@ void __init setup_arch(char **cmdline_p)
cd3596b
 			pr_info("Secure boot disabled\n");
cd3596b
 			break;
cd3596b
 		case efi_secureboot_mode_enabled:
962ea4f
+			set_bit(EFI_SECURE_BOOT, &efi.flags);
cd3596b
 			pr_info("Secure boot enabled\n");
cd3596b
 			break;
cd3596b
 		default:
962ea4f
diff --git a/include/linux/efi.h b/include/linux/efi.h
cd3596b
index 94d34e0..6049600 100644
962ea4f
--- a/include/linux/efi.h
962ea4f
+++ b/include/linux/efi.h
cd3596b
@@ -1069,6 +1069,7 @@ extern int __init efi_setup_pcdp_console(char *);
962ea4f
 #define EFI_DBG			8	/* Print additional debug info at runtime */
962ea4f
 #define EFI_NX_PE_DATA		9	/* Can runtime data regions be mapped non-executable? */
cd3596b
 #define EFI_MEM_ATTR		10	/* Did firmware publish an EFI_MEMORY_ATTRIBUTES table? */
cd3596b
+#define EFI_SECURE_BOOT		11	/* Are we in Secure Boot mode? */
c796f87
 
962ea4f
 #ifdef CONFIG_EFI
962ea4f
 /*
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From f05a90c19a9613d8d50597319ed91f691e25b689 Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
962ea4f
Date: Mon, 21 Nov 2016 23:36:17 +0000
962ea4f
Subject: [PATCH 09/32] Add the ability to lock down access to the running
962ea4f
 kernel image
962ea4f
962ea4f
Provide a single call to allow kernel code to determine whether the system
962ea4f
should be locked down, thereby disallowing various accesses that might
962ea4f
allow the running kernel image to be changed including the loading of
962ea4f
modules that aren't validly signed with a key we recognise, fiddling with
962ea4f
MSR registers and disallowing hibernation,
962ea4f
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 include/linux/kernel.h   |  9 +++++++++
962ea4f
 include/linux/security.h | 11 +++++++++++
962ea4f
 security/Kconfig         | 15 +++++++++++++++
962ea4f
 security/Makefile        |  3 +++
962ea4f
 security/lock_down.c     | 40 ++++++++++++++++++++++++++++++++++++++++
962ea4f
 5 files changed, 78 insertions(+)
962ea4f
 create mode 100644 security/lock_down.c
962ea4f
962ea4f
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
c796f87
index cb09238..3cd3be9 100644
962ea4f
--- a/include/linux/kernel.h
962ea4f
+++ b/include/linux/kernel.h
c796f87
@@ -273,6 +273,15 @@ extern int oops_may_print(void);
962ea4f
 void do_exit(long error_code) __noreturn;
962ea4f
 void complete_and_exit(struct completion *, long) __noreturn;
c796f87
 
962ea4f
+#ifdef CONFIG_LOCK_DOWN_KERNEL
962ea4f
+extern bool kernel_is_locked_down(void);
962ea4f
+#else
962ea4f
+static inline bool kernel_is_locked_down(void)
962ea4f
+{
962ea4f
+	return false;
962ea4f
+}
962ea4f
+#endif
962ea4f
+
962ea4f
 /* Internal, do not use. */
962ea4f
 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
962ea4f
 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
962ea4f
diff --git a/include/linux/security.h b/include/linux/security.h
c796f87
index d3868f2..187b74b 100644
962ea4f
--- a/include/linux/security.h
962ea4f
+++ b/include/linux/security.h
c796f87
@@ -1679,5 +1679,16 @@ static inline void free_secdata(void *secdata)
962ea4f
 { }
962ea4f
 #endif /* CONFIG_SECURITY */
c796f87
 
962ea4f
+#ifdef CONFIG_LOCK_DOWN_KERNEL
962ea4f
+extern void lock_kernel_down(void);
962ea4f
+#ifdef CONFIG_ALLOW_LOCKDOWN_LIFT
962ea4f
+extern void lift_kernel_lockdown(void);
962ea4f
+#endif
962ea4f
+#else
962ea4f
+static inline void lock_kernel_down(void)
962ea4f
+{
962ea4f
+}
962ea4f
+#endif
962ea4f
+
962ea4f
 #endif /* ! __LINUX_SECURITY_H */
c796f87
 
962ea4f
diff --git a/security/Kconfig b/security/Kconfig
c796f87
index d900f47..d9b391d 100644
962ea4f
--- a/security/Kconfig
962ea4f
+++ b/security/Kconfig
c796f87
@@ -193,6 +193,21 @@ config STATIC_USERMODEHELPER_PATH
c796f87
 	  If you wish for all usermode helper programs to be disabled,
c796f87
 	  specify an empty string here (i.e. "").
c796f87
 
962ea4f
+config LOCK_DOWN_KERNEL
962ea4f
+	bool "Allow the kernel to be 'locked down'"
962ea4f
+	help
962ea4f
+	  Allow the kernel to be locked down under certain circumstances, for
962ea4f
+	  instance if UEFI secure boot is enabled.  Locking down the kernel
962ea4f
+	  turns off various features that might otherwise allow access to the
962ea4f
+	  kernel image (eg. setting MSR registers).
962ea4f
+
962ea4f
+config ALLOW_LOCKDOWN_LIFT
962ea4f
+	bool
962ea4f
+	help
962ea4f
+	  Allow the lockdown on a kernel to be lifted, thereby restoring the
962ea4f
+	  ability of userspace to access the kernel image (eg. by SysRq+x under
962ea4f
+	  x86).
962ea4f
+
962ea4f
 source security/selinux/Kconfig
962ea4f
 source security/smack/Kconfig
962ea4f
 source security/tomoyo/Kconfig
962ea4f
diff --git a/security/Makefile b/security/Makefile
962ea4f
index f2d71cd..8c4a43e 100644
962ea4f
--- a/security/Makefile
962ea4f
+++ b/security/Makefile
962ea4f
@@ -29,3 +29,6 @@ obj-$(CONFIG_CGROUP_DEVICE)		+= device_cgroup.o
962ea4f
 # Object integrity file lists
962ea4f
 subdir-$(CONFIG_INTEGRITY)		+= integrity
962ea4f
 obj-$(CONFIG_INTEGRITY)			+= integrity/
962ea4f
+
962ea4f
+# Allow the kernel to be locked down
962ea4f
+obj-$(CONFIG_LOCK_DOWN_KERNEL)		+= lock_down.o
962ea4f
diff --git a/security/lock_down.c b/security/lock_down.c
962ea4f
new file mode 100644
962ea4f
index 0000000..5788c60
962ea4f
--- /dev/null
962ea4f
+++ b/security/lock_down.c
962ea4f
@@ -0,0 +1,40 @@
962ea4f
+/* Lock down the kernel
962ea4f
+ *
962ea4f
+ * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
962ea4f
+ * Written by David Howells (dhowells@redhat.com)
962ea4f
+ *
962ea4f
+ * This program is free software; you can redistribute it and/or
962ea4f
+ * modify it under the terms of the GNU General Public Licence
962ea4f
+ * as published by the Free Software Foundation; either version
962ea4f
+ * 2 of the Licence, or (at your option) any later version.
962ea4f
+ */
962ea4f
+
962ea4f
+#include <linux/security.h>
962ea4f
+#include <linux/export.h>
962ea4f
+
962ea4f
+static __read_mostly bool kernel_locked_down;
962ea4f
+
962ea4f
+/*
962ea4f
+ * Put the kernel into lock-down mode.
962ea4f
+ */
962ea4f
+void lock_kernel_down(void)
962ea4f
+{
962ea4f
+	kernel_locked_down = true;
962ea4f
+}
962ea4f
+
962ea4f
+/*
962ea4f
+ * Take the kernel out of lockdown mode.
962ea4f
+ */
962ea4f
+void lift_kernel_lockdown(void)
962ea4f
+{
962ea4f
+	kernel_locked_down = false;
962ea4f
+}
962ea4f
+
962ea4f
+/**
962ea4f
+ * kernel_is_locked_down - Find out if the kernel is locked down
962ea4f
+ */
962ea4f
+bool kernel_is_locked_down(void)
962ea4f
+{
962ea4f
+	return kernel_locked_down;
962ea4f
+}
962ea4f
+EXPORT_SYMBOL(kernel_is_locked_down);
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From fb6feb38e297260d050fc477c72683ac51d07ae3 Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
962ea4f
Date: Mon, 21 Nov 2016 23:55:55 +0000
962ea4f
Subject: [PATCH 10/32] efi: Lock down the kernel if booted in secure boot mode
962ea4f
962ea4f
UEFI Secure Boot provides a mechanism for ensuring that the firmware will
962ea4f
only load signed bootloaders and kernels.  Certain use cases may also
962ea4f
require that all kernel modules also be signed.  Add a configuration option
962ea4f
that to lock down the kernel - which includes requiring validly signed
962ea4f
modules - if the kernel is secure-booted.
962ea4f
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 arch/x86/Kconfig        | 12 ++++++++++++
962ea4f
 arch/x86/kernel/setup.c |  8 +++++++-
962ea4f
 2 files changed, 19 insertions(+), 1 deletion(-)
962ea4f
962ea4f
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
c796f87
index 874c123..a315974 100644
962ea4f
--- a/arch/x86/Kconfig
962ea4f
+++ b/arch/x86/Kconfig
c796f87
@@ -1816,6 +1816,18 @@ config EFI_MIXED
c796f87
 
962ea4f
 	   If unsure, say N.
c796f87
 
962ea4f
+config EFI_SECURE_BOOT_LOCK_DOWN
962ea4f
+	def_bool n
962ea4f
+	depends on EFI
962ea4f
+	prompt "Lock down the kernel when UEFI Secure Boot is enabled"
962ea4f
+	---help---
962ea4f
+	  UEFI Secure Boot provides a mechanism for ensuring that the firmware
962ea4f
+	  will only load signed bootloaders and kernels.  Certain use cases may
962ea4f
+	  also require that all kernel modules also be signed and that
962ea4f
+	  userspace is prevented from directly changing the running kernel
962ea4f
+	  image.  Say Y here to automatically lock down the kernel when a
962ea4f
+	  system boots with UEFI Secure Boot enabled.
962ea4f
+
962ea4f
 config SECCOMP
962ea4f
 	def_bool y
962ea4f
 	prompt "Enable seccomp to safely compute untrusted bytecode"
962ea4f
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
c796f87
index 447905e..d44e60e 100644
962ea4f
--- a/arch/x86/kernel/setup.c
962ea4f
+++ b/arch/x86/kernel/setup.c
962ea4f
@@ -69,6 +69,7 @@
962ea4f
 #include <linux/crash_dump.h>
962ea4f
 #include <linux/tboot.h>
962ea4f
 #include <linux/jiffies.h>
962ea4f
+#include <linux/security.h>
c796f87
 
a3e7d7a
 #include <linux/usb/xhci-dbgp.h>
962ea4f
 #include <video/edid.h>
c796f87
@@ -1183,7 +1184,12 @@ void __init setup_arch(char **cmdline_p)
962ea4f
 			break;
962ea4f
 		case efi_secureboot_mode_enabled:
962ea4f
 			set_bit(EFI_SECURE_BOOT, &efi.flags);
962ea4f
-			pr_info("Secure boot enabled\n");
962ea4f
+			if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT_LOCK_DOWN)) {
962ea4f
+				lock_kernel_down();
962ea4f
+				pr_info("Secure boot enabled and kernel locked down\n");
962ea4f
+			} else {
962ea4f
+				pr_info("Secure boot enabled\n");
962ea4f
+			}
962ea4f
 			break;
962ea4f
 		default:
962ea4f
 			pr_info("Secure boot could not be determined\n");
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 7182f2f5b254d6dc6d3105d2f99219a76adf9de0 Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
962ea4f
Date: Wed, 23 Nov 2016 13:22:22 +0000
962ea4f
Subject: [PATCH 11/32] Enforce module signatures if the kernel is locked down
962ea4f
962ea4f
If the kernel is locked down, require that all modules have valid
962ea4f
signatures that we can verify.
962ea4f
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 kernel/module.c | 2 +-
962ea4f
 1 file changed, 1 insertion(+), 1 deletion(-)
962ea4f
962ea4f
diff --git a/kernel/module.c b/kernel/module.c
c796f87
index 7eba6de..3331f2e 100644
962ea4f
--- a/kernel/module.c
962ea4f
+++ b/kernel/module.c
c796f87
@@ -2756,7 +2756,7 @@ static int module_sig_check(struct load_info *info, int flags)
962ea4f
 	}
c796f87
 
962ea4f
 	/* Not having a signature is only an error if we're strict. */
962ea4f
-	if (err == -ENOKEY && !sig_enforce)
962ea4f
+	if (err == -ENOKEY && !sig_enforce && !kernel_is_locked_down())
962ea4f
 		err = 0;
c796f87
 
962ea4f
 	return err;
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 7e97c58bcd0b4c082b889fb093a2779147532b9f Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:16 +0000
962ea4f
Subject: [PATCH 12/32] Restrict /dev/mem and /dev/kmem when the kernel is
962ea4f
 locked down
962ea4f
962ea4f
Allowing users to write to address space makes it possible for the kernel to
962ea4f
be subverted, avoiding module loading restrictions.  Prevent this when the
962ea4f
kernel has been locked down.
962ea4f
962ea4f
Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/char/mem.c | 6 ++++++
962ea4f
 1 file changed, 6 insertions(+)
962ea4f
962ea4f
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
c796f87
index 6d9cc2d..f814404 100644
962ea4f
--- a/drivers/char/mem.c
962ea4f
+++ b/drivers/char/mem.c
962ea4f
@@ -163,6 +163,9 @@ static ssize_t write_mem(struct file *file, const char __user *buf,
962ea4f
 	if (p != *ppos)
962ea4f
 		return -EFBIG;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	if (!valid_phys_addr_range(p, count))
962ea4f
 		return -EFAULT;
c796f87
 
c796f87
@@ -513,6 +516,9 @@ static ssize_t write_kmem(struct file *file, const char __user *buf,
411d3b7
 	char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
411d3b7
 	int err = 0;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	if (p < (unsigned long) high_memory) {
962ea4f
 		unsigned long to_write = min_t(unsigned long, count,
962ea4f
 					       (unsigned long)high_memory - p);
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From b83b68a9a13120664eaabf21a7b3ff0b065bd5b2 Mon Sep 17 00:00:00 2001
962ea4f
From: Kyle McMartin <kyle@redhat.com>
962ea4f
Date: Mon, 21 Nov 2016 23:55:56 +0000
962ea4f
Subject: [PATCH 13/32] Add a sysrq option to exit secure boot mode
962ea4f
962ea4f
Make sysrq+x exit secure boot mode on x86_64, thereby allowing the running
962ea4f
kernel image to be modified.  This lifts the lockdown.
962ea4f
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 arch/x86/Kconfig            | 10 ++++++++++
962ea4f
 arch/x86/kernel/setup.c     | 31 +++++++++++++++++++++++++++++++
962ea4f
 drivers/input/misc/uinput.c |  1 +
962ea4f
 drivers/tty/sysrq.c         | 19 +++++++++++++------
962ea4f
 include/linux/input.h       |  5 +++++
962ea4f
 include/linux/sysrq.h       |  8 +++++++-
962ea4f
 kernel/debug/kdb/kdb_main.c |  2 +-
962ea4f
 7 files changed, 68 insertions(+), 8 deletions(-)
962ea4f
962ea4f
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
c796f87
index a315974..6931e68 100644
962ea4f
--- a/arch/x86/Kconfig
962ea4f
+++ b/arch/x86/Kconfig
c796f87
@@ -1828,6 +1828,16 @@ config EFI_SECURE_BOOT_LOCK_DOWN
962ea4f
 	  image.  Say Y here to automatically lock down the kernel when a
962ea4f
 	  system boots with UEFI Secure Boot enabled.
c796f87
 
962ea4f
+config EFI_ALLOW_SECURE_BOOT_EXIT
962ea4f
+	def_bool n
962ea4f
+	depends on EFI_SECURE_BOOT_LOCK_DOWN && MAGIC_SYSRQ
962ea4f
+	select ALLOW_LOCKDOWN_LIFT
962ea4f
+	prompt "Allow secure boot mode to be exited with SysRq+x on a keyboard"
962ea4f
+	---help---
962ea4f
+	  Allow secure boot mode to be exited and the kernel lockdown lifted by
962ea4f
+	  typing SysRq+x on a keyboard attached to the system (not permitted
962ea4f
+	  through procfs).
962ea4f
+
962ea4f
 config SECCOMP
962ea4f
 	def_bool y
962ea4f
 	prompt "Enable seccomp to safely compute untrusted bytecode"
962ea4f
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
c796f87
index d44e60e..f7635d0 100644
962ea4f
--- a/arch/x86/kernel/setup.c
962ea4f
+++ b/arch/x86/kernel/setup.c
962ea4f
@@ -71,6 +71,11 @@
962ea4f
 #include <linux/jiffies.h>
962ea4f
 #include <linux/security.h>
c796f87
 
962ea4f
+#include <linux/fips.h>
962ea4f
+#include <linux/cred.h>
962ea4f
+#include <linux/sysrq.h>
962ea4f
+#include <linux/init_task.h>
962ea4f
+
a3e7d7a
 #include <linux/usb/xhci-dbgp.h>
962ea4f
 #include <video/edid.h>
c796f87
 
c796f87
@@ -1328,6 +1333,32 @@ void __init i386_reserve_resources(void)
c796f87
 
962ea4f
 #endif /* CONFIG_X86_32 */
c796f87
 
962ea4f
+#ifdef CONFIG_EFI_ALLOW_SECURE_BOOT_EXIT
962ea4f
+
962ea4f
+static void sysrq_handle_secure_boot(int key)
962ea4f
+{
962ea4f
+	if (!efi_enabled(EFI_SECURE_BOOT))
962ea4f
+		return;
962ea4f
+
962ea4f
+	pr_info("Secure boot disabled\n");
962ea4f
+	lift_kernel_lockdown();
962ea4f
+}
962ea4f
+static struct sysrq_key_op secure_boot_sysrq_op = {
962ea4f
+	.handler	=	sysrq_handle_secure_boot,
962ea4f
+	.help_msg	=	"unSB(x)",
962ea4f
+	.action_msg	=	"Disabling Secure Boot restrictions",
962ea4f
+	.enable_mask	=	SYSRQ_DISABLE_USERSPACE,
962ea4f
+};
962ea4f
+static int __init secure_boot_sysrq(void)
962ea4f
+{
962ea4f
+	if (efi_enabled(EFI_SECURE_BOOT))
962ea4f
+		register_sysrq_key('x', &secure_boot_sysrq_op);
962ea4f
+	return 0;
962ea4f
+}
962ea4f
+late_initcall(secure_boot_sysrq);
962ea4f
+#endif /*CONFIG_EFI_ALLOW_SECURE_BOOT_EXIT*/
962ea4f
+
962ea4f
+
962ea4f
 static struct notifier_block kernel_offset_notifier = {
962ea4f
 	.notifier_call = dump_kernel_offset
962ea4f
 };
962ea4f
diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c
c796f87
index 022be0e..4a054a5 100644
962ea4f
--- a/drivers/input/misc/uinput.c
962ea4f
+++ b/drivers/input/misc/uinput.c
c796f87
@@ -387,6 +387,7 @@ static int uinput_allocate_device(struct uinput_device *udev)
962ea4f
 	if (!udev->dev)
962ea4f
 		return -ENOMEM;
c796f87
 
962ea4f
+	udev->dev->flags |= INPUTDEV_FLAGS_SYNTHETIC;
962ea4f
 	udev->dev->event = uinput_dev_event;
962ea4f
 	input_set_drvdata(udev->dev, udev);
c796f87
 
962ea4f
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
c796f87
index 7113674..e1addc3 100644
962ea4f
--- a/drivers/tty/sysrq.c
962ea4f
+++ b/drivers/tty/sysrq.c
962ea4f
@@ -479,6 +479,7 @@ static struct sysrq_key_op *sysrq_key_table[36] = {
962ea4f
 	/* x: May be registered on mips for TLB dump */
962ea4f
 	/* x: May be registered on ppc/powerpc for xmon */
962ea4f
 	/* x: May be registered on sparc64 for global PMU dump */
962ea4f
+	/* x: May be registered on x86_64 for disabling secure boot */
962ea4f
 	NULL,				/* x */
962ea4f
 	/* y: May be registered on sparc64 for global register dump */
962ea4f
 	NULL,				/* y */
962ea4f
@@ -522,7 +523,7 @@ static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
962ea4f
                 sysrq_key_table[i] = op_p;
962ea4f
 }
c796f87
 
962ea4f
-void __handle_sysrq(int key, bool check_mask)
962ea4f
+void __handle_sysrq(int key, unsigned int from)
962ea4f
 {
962ea4f
 	struct sysrq_key_op *op_p;
962ea4f
 	int orig_log_level;
962ea4f
@@ -542,11 +543,15 @@ void __handle_sysrq(int key, bool check_mask)
c796f87
 
962ea4f
         op_p = __sysrq_get_key_op(key);
962ea4f
         if (op_p) {
962ea4f
+		/* Ban synthetic events from some sysrq functionality */
962ea4f
+		if ((from == SYSRQ_FROM_PROC || from == SYSRQ_FROM_SYNTHETIC) &&
962ea4f
+		    op_p->enable_mask & SYSRQ_DISABLE_USERSPACE)
962ea4f
+			printk("This sysrq operation is disabled from userspace.\n");
962ea4f
 		/*
962ea4f
 		 * Should we check for enabled operations (/proc/sysrq-trigger
962ea4f
 		 * should not) and is the invoked operation enabled?
962ea4f
 		 */
962ea4f
-		if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
962ea4f
+		if (from == SYSRQ_FROM_KERNEL || sysrq_on_mask(op_p->enable_mask)) {
962ea4f
 			pr_cont("%s\n", op_p->action_msg);
962ea4f
 			console_loglevel = orig_log_level;
962ea4f
 			op_p->handler(key);
962ea4f
@@ -578,7 +583,7 @@ void __handle_sysrq(int key, bool check_mask)
962ea4f
 void handle_sysrq(int key)
962ea4f
 {
962ea4f
 	if (sysrq_on())
962ea4f
-		__handle_sysrq(key, true);
962ea4f
+		__handle_sysrq(key, SYSRQ_FROM_KERNEL);
962ea4f
 }
962ea4f
 EXPORT_SYMBOL(handle_sysrq);
c796f87
 
962ea4f
@@ -659,7 +664,7 @@ static void sysrq_do_reset(unsigned long _state)
962ea4f
 static void sysrq_handle_reset_request(struct sysrq_state *state)
962ea4f
 {
962ea4f
 	if (state->reset_requested)
962ea4f
-		__handle_sysrq(sysrq_xlate[KEY_B], false);
962ea4f
+		__handle_sysrq(sysrq_xlate[KEY_B], SYSRQ_FROM_KERNEL);
c796f87
 
962ea4f
 	if (sysrq_reset_downtime_ms)
962ea4f
 		mod_timer(&state->keyreset_timer,
962ea4f
@@ -810,8 +815,10 @@ static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
c796f87
 
962ea4f
 	default:
962ea4f
 		if (sysrq->active && value && value != 2) {
962ea4f
+			int from = sysrq->handle.dev->flags & INPUTDEV_FLAGS_SYNTHETIC ?
962ea4f
+					SYSRQ_FROM_SYNTHETIC : 0;
962ea4f
 			sysrq->need_reinject = false;
962ea4f
-			__handle_sysrq(sysrq_xlate[code], true);
962ea4f
+			__handle_sysrq(sysrq_xlate[code], from);
962ea4f
 		}
962ea4f
 		break;
962ea4f
 	}
962ea4f
@@ -1095,7 +1102,7 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
c796f87
 
962ea4f
 		if (get_user(c, buf))
962ea4f
 			return -EFAULT;
962ea4f
-		__handle_sysrq(c, false);
962ea4f
+		__handle_sysrq(c, SYSRQ_FROM_PROC);
962ea4f
 	}
c796f87
 
962ea4f
 	return count;
962ea4f
diff --git a/include/linux/input.h b/include/linux/input.h
962ea4f
index a65e3b2..8b03571 100644
962ea4f
--- a/include/linux/input.h
962ea4f
+++ b/include/linux/input.h
962ea4f
@@ -42,6 +42,7 @@ struct input_value {
962ea4f
  * @phys: physical path to the device in the system hierarchy
962ea4f
  * @uniq: unique identification code for the device (if device has it)
962ea4f
  * @id: id of the device (struct input_id)
962ea4f
+ * @flags: input device flags (SYNTHETIC, etc.)
962ea4f
  * @propbit: bitmap of device properties and quirks
962ea4f
  * @evbit: bitmap of types of events supported by the device (EV_KEY,
962ea4f
  *	EV_REL, etc.)
962ea4f
@@ -124,6 +125,8 @@ struct input_dev {
962ea4f
 	const char *uniq;
962ea4f
 	struct input_id id;
c796f87
 
962ea4f
+	unsigned int flags;
962ea4f
+
962ea4f
 	unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];
c796f87
 
962ea4f
 	unsigned long evbit[BITS_TO_LONGS(EV_CNT)];
962ea4f
@@ -190,6 +193,8 @@ struct input_dev {
962ea4f
 };
962ea4f
 #define to_input_dev(d) container_of(d, struct input_dev, dev)
c796f87
 
962ea4f
+#define	INPUTDEV_FLAGS_SYNTHETIC	0x000000001
962ea4f
+
962ea4f
 /*
962ea4f
  * Verify that we are in sync with input_device_id mod_devicetable.h #defines
962ea4f
  */
962ea4f
diff --git a/include/linux/sysrq.h b/include/linux/sysrq.h
962ea4f
index 387fa7d..f7c52a9 100644
962ea4f
--- a/include/linux/sysrq.h
962ea4f
+++ b/include/linux/sysrq.h
962ea4f
@@ -28,6 +28,8 @@
962ea4f
 #define SYSRQ_ENABLE_BOOT	0x0080
962ea4f
 #define SYSRQ_ENABLE_RTNICE	0x0100
c796f87
 
962ea4f
+#define SYSRQ_DISABLE_USERSPACE	0x00010000
962ea4f
+
962ea4f
 struct sysrq_key_op {
962ea4f
 	void (*handler)(int);
962ea4f
 	char *help_msg;
962ea4f
@@ -42,8 +44,12 @@ struct sysrq_key_op {
962ea4f
  * are available -- else NULL's).
962ea4f
  */
c796f87
 
962ea4f
+#define SYSRQ_FROM_KERNEL	0x0001
962ea4f
+#define SYSRQ_FROM_PROC		0x0002
962ea4f
+#define SYSRQ_FROM_SYNTHETIC	0x0004
962ea4f
+
962ea4f
 void handle_sysrq(int key);
962ea4f
-void __handle_sysrq(int key, bool check_mask);
962ea4f
+void __handle_sysrq(int key, unsigned int from);
962ea4f
 int register_sysrq_key(int key, struct sysrq_key_op *op);
962ea4f
 int unregister_sysrq_key(int key, struct sysrq_key_op *op);
962ea4f
 struct sysrq_key_op *__sysrq_get_key_op(int key);
962ea4f
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
c796f87
index ca18391..c4524b8 100644
962ea4f
--- a/kernel/debug/kdb/kdb_main.c
962ea4f
+++ b/kernel/debug/kdb/kdb_main.c
c796f87
@@ -1967,7 +1967,7 @@ static int kdb_sr(int argc, const char **argv)
962ea4f
 		return KDB_ARGCOUNT;
c796f87
 
962ea4f
 	kdb_trap_printk++;
962ea4f
-	__handle_sysrq(*argv[1], check_mask);
962ea4f
+	__handle_sysrq(*argv[1], check_mask ? SYSRQ_FROM_KERNEL : 0);
962ea4f
 	kdb_trap_printk--;
c796f87
 
962ea4f
 	return 0;
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 8884bd44932e595323fcddfb09c2a2a586134cdf Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:15 +0000
962ea4f
Subject: [PATCH 14/32] kexec: Disable at runtime if the kernel is locked down
962ea4f
962ea4f
kexec permits the loading and execution of arbitrary code in ring 0, which
962ea4f
is something that lock-down is meant to prevent. It makes sense to disable
962ea4f
kexec in this situation.
962ea4f
962ea4f
This does not affect kexec_file_load() which can check for a signature on the
962ea4f
image to be booted.
962ea4f
962ea4f
Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 kernel/kexec.c | 7 +++++++
962ea4f
 1 file changed, 7 insertions(+)
962ea4f
962ea4f
diff --git a/kernel/kexec.c b/kernel/kexec.c
962ea4f
index 980936a..46de8e6 100644
962ea4f
--- a/kernel/kexec.c
962ea4f
+++ b/kernel/kexec.c
962ea4f
@@ -194,6 +194,13 @@ SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
962ea4f
 		return -EPERM;
c796f87
 
962ea4f
 	/*
962ea4f
+	 * kexec can be used to circumvent module loading restrictions, so
962ea4f
+	 * prevent loading in that case
962ea4f
+	 */
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
+	/*
962ea4f
 	 * Verify we have a legal set of flags
962ea4f
 	 * This leaves us room for future extensions.
962ea4f
 	 */
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From cace563d1743c3d2faf1e46bd4df8e63e2310207 Mon Sep 17 00:00:00 2001
962ea4f
From: Dave Young <dyoung@redhat.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:15 +0000
962ea4f
Subject: [PATCH 15/32] Copy secure_boot flag in boot params across kexec
962ea4f
 reboot
962ea4f
962ea4f
Kexec reboot in case secure boot being enabled does not keep the secure
962ea4f
boot mode in new kernel, so later one can load unsigned kernel via legacy
962ea4f
kexec_load.  In this state, the system is missing the protections provided
962ea4f
by secure boot.
962ea4f
962ea4f
Adding a patch to fix this by retain the secure_boot flag in original
962ea4f
kernel.
962ea4f
962ea4f
secure_boot flag in boot_params is set in EFI stub, but kexec bypasses the
962ea4f
stub.  Fixing this issue by copying secure_boot flag across kexec reboot.
962ea4f
962ea4f
Signed-off-by: Dave Young <dyoung@redhat.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 arch/x86/kernel/kexec-bzimage64.c | 1 +
962ea4f
 1 file changed, 1 insertion(+)
962ea4f
962ea4f
diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c
c796f87
index d0a814a..3551bca 100644
962ea4f
--- a/arch/x86/kernel/kexec-bzimage64.c
962ea4f
+++ b/arch/x86/kernel/kexec-bzimage64.c
962ea4f
@@ -179,6 +179,7 @@ setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
962ea4f
 	if (efi_enabled(EFI_OLD_MEMMAP))
962ea4f
 		return 0;
c796f87
 
962ea4f
+	params->secure_boot = boot_params.secure_boot;
962ea4f
 	ei->efi_loader_signature = current_ei->efi_loader_signature;
962ea4f
 	ei->efi_systab = current_ei->efi_systab;
962ea4f
 	ei->efi_systab_hi = current_ei->efi_systab_hi;
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 08a3467acbc28bb469d1eebd0f5fd40b944d984a Mon Sep 17 00:00:00 2001
962ea4f
From: "Lee, Chun-Yi" <joeyli.kernel@gmail.com>
962ea4f
Date: Wed, 23 Nov 2016 13:49:19 +0000
962ea4f
Subject: [PATCH 16/32] kexec_file: Disable at runtime if securelevel has been
962ea4f
 set
962ea4f
962ea4f
When KEXEC_VERIFY_SIG is not enabled, kernel should not loads image
962ea4f
through kexec_file systemcall if securelevel has been set.
962ea4f
962ea4f
This code was showed in Matthew's patch but not in git:
962ea4f
https://lkml.org/lkml/2015/3/13/778
962ea4f
962ea4f
Cc: Matthew Garrett <mjg59@srcf.ucam.org>
962ea4f
Signed-off-by: Lee, Chun-Yi <jlee@suse.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 kernel/kexec_file.c | 6 ++++++
962ea4f
 1 file changed, 6 insertions(+)
962ea4f
962ea4f
diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
c796f87
index b56a558..003cade 100644
962ea4f
--- a/kernel/kexec_file.c
962ea4f
+++ b/kernel/kexec_file.c
c796f87
@@ -268,6 +268,12 @@ SYSCALL_DEFINE5(kexec_file_load, int, kernel_fd, int, initrd_fd,
962ea4f
 	if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
962ea4f
 		return -EPERM;
c796f87
 
962ea4f
+	/* Don't permit images to be loaded into trusted kernels if we're not
962ea4f
+	 * going to verify the signature on them
962ea4f
+	 */
962ea4f
+	if (!IS_ENABLED(CONFIG_KEXEC_VERIFY_SIG) && kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	/* Make sure we have a legal set of flags */
962ea4f
 	if (flags != (flags & KEXEC_FILE_FLAGS))
962ea4f
 		return -EINVAL;
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 925fd10d7a99a6f999dde76daf2b1ef1238b251a Mon Sep 17 00:00:00 2001
962ea4f
From: Josh Boyer <jwboyer@fedoraproject.org>
962ea4f
Date: Tue, 22 Nov 2016 08:46:15 +0000
962ea4f
Subject: [PATCH 17/32] hibernate: Disable when the kernel is locked down
962ea4f
962ea4f
There is currently no way to verify the resume image when returning
962ea4f
from hibernate.  This might compromise the signed modules trust model,
962ea4f
so until we can work with signed hibernate images we disable it when the
962ea4f
kernel is locked down.
962ea4f
962ea4f
Signed-off-by: Josh Boyer <jwboyer@fedoraproject.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 kernel/power/hibernate.c | 2 +-
962ea4f
 1 file changed, 1 insertion(+), 1 deletion(-)
962ea4f
962ea4f
diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c
c796f87
index 86385af..1629798 100644
962ea4f
--- a/kernel/power/hibernate.c
962ea4f
+++ b/kernel/power/hibernate.c
962ea4f
@@ -67,7 +67,7 @@ static const struct platform_hibernation_ops *hibernation_ops;
c796f87
 
962ea4f
 bool hibernation_available(void)
962ea4f
 {
962ea4f
-	return (nohibernate == 0);
962ea4f
+	return nohibernate == 0 && !kernel_is_locked_down();
962ea4f
 }
c796f87
 
962ea4f
 /**
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From b14fb5fcd60c84a09a15bd7ec974b6fbce68335c Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <mjg59@srcf.ucam.org>
962ea4f
Date: Wed, 23 Nov 2016 13:28:17 +0000
962ea4f
Subject: [PATCH 18/32] uswsusp: Disable when the kernel is locked down
962ea4f
962ea4f
uswsusp allows a user process to dump and then restore kernel state, which
962ea4f
makes it possible to modify the running kernel.  Disable this if the kernel
962ea4f
is locked down.
962ea4f
962ea4f
Signed-off-by: Matthew Garrett <mjg59@srcf.ucam.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 kernel/power/user.c | 3 +++
962ea4f
 1 file changed, 3 insertions(+)
962ea4f
962ea4f
diff --git a/kernel/power/user.c b/kernel/power/user.c
c796f87
index 22df9f7..e4b926d 100644
962ea4f
--- a/kernel/power/user.c
962ea4f
+++ b/kernel/power/user.c
962ea4f
@@ -52,6 +52,9 @@ static int snapshot_open(struct inode *inode, struct file *filp)
962ea4f
 	if (!hibernation_available())
962ea4f
 		return -EPERM;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	lock_system_sleep();
c796f87
 
962ea4f
 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From d8167273e7562e5aa866d2b65e25bb1962de972c Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:15 +0000
962ea4f
Subject: [PATCH 19/32] PCI: Lock down BAR access when the kernel is locked
962ea4f
 down
962ea4f
962ea4f
Any hardware that can potentially generate DMA has to be locked down in
962ea4f
order to avoid it being possible for an attacker to modify kernel code,
962ea4f
allowing them to circumvent disabled module loading or module signing.
962ea4f
Default to paranoid - in future we can potentially relax this for
962ea4f
sufficiently IOMMU-isolated devices.
962ea4f
962ea4f
Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/pci/pci-sysfs.c | 9 +++++++++
962ea4f
 drivers/pci/proc.c      | 8 +++++++-
962ea4f
 drivers/pci/syscall.c   | 2 +-
962ea4f
 3 files changed, 17 insertions(+), 2 deletions(-)
962ea4f
962ea4f
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
c796f87
index 0666287..b10992c 100644
962ea4f
--- a/drivers/pci/pci-sysfs.c
962ea4f
+++ b/drivers/pci/pci-sysfs.c
c796f87
@@ -718,6 +718,9 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
962ea4f
 	loff_t init_off = off;
962ea4f
 	u8 *data = (u8 *) buf;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	if (off > dev->cfg_size)
962ea4f
 		return 0;
962ea4f
 	if (off + count > dev->cfg_size) {
c796f87
@@ -1009,6 +1012,9 @@ static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr,
bd32781
 	enum pci_mmap_state mmap_type;
bd32781
 	struct resource *res = &pdev->resource[bar];
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
bd32781
 	if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
bd32781
 		return -EINVAL;
bd32781
 
c796f87
@@ -1108,6 +1114,9 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
962ea4f
 				     struct bin_attribute *attr, char *buf,
962ea4f
 				     loff_t off, size_t count)
962ea4f
 {
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	return pci_resource_io(filp, kobj, attr, buf, off, count, true);
962ea4f
 }
c796f87
 
962ea4f
diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c
c796f87
index f82710a..139d6f0 100644
962ea4f
--- a/drivers/pci/proc.c
962ea4f
+++ b/drivers/pci/proc.c
962ea4f
@@ -116,6 +116,9 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
962ea4f
 	int size = dev->cfg_size;
962ea4f
 	int cnt;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	if (pos >= size)
962ea4f
 		return 0;
962ea4f
 	if (nbytes >= size)
962ea4f
@@ -195,6 +198,9 @@ static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,
962ea4f
 #endif /* HAVE_PCI_MMAP */
962ea4f
 	int ret = 0;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	switch (cmd) {
962ea4f
 	case PCIIOC_CONTROLLER:
962ea4f
 		ret = pci_domain_nr(dev->bus);
962ea4f
@@ -233,7 +239,7 @@ static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
962ea4f
 	struct pci_filp_private *fpriv = file->private_data;
bd32781
 	int i, ret, write_combine = 0, res_bit = IORESOURCE_MEM;
c796f87
 
962ea4f
-	if (!capable(CAP_SYS_RAWIO))
962ea4f
+	if (!capable(CAP_SYS_RAWIO) || kernel_is_locked_down())
962ea4f
 		return -EPERM;
c796f87
 
bd32781
 	if (fpriv->mmap_state == pci_mmap_io) {
962ea4f
diff --git a/drivers/pci/syscall.c b/drivers/pci/syscall.c
c796f87
index 9bf993e..c095247 100644
962ea4f
--- a/drivers/pci/syscall.c
962ea4f
+++ b/drivers/pci/syscall.c
962ea4f
@@ -92,7 +92,7 @@ SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
962ea4f
 	u32 dword;
962ea4f
 	int err = 0;
c796f87
 
962ea4f
-	if (!capable(CAP_SYS_ADMIN))
962ea4f
+	if (!capable(CAP_SYS_ADMIN) || kernel_is_locked_down())
962ea4f
 		return -EPERM;
c796f87
 
962ea4f
 	dev = pci_get_bus_and_slot(bus, dfn);
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 169d13e0c5240ac6aad13aace5ecbdb3de2cfada Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:16 +0000
962ea4f
Subject: [PATCH 20/32] x86: Lock down IO port access when the kernel is locked
962ea4f
 down
962ea4f
962ea4f
IO port access would permit users to gain access to PCI configuration
962ea4f
registers, which in turn (on a lot of hardware) give access to MMIO
962ea4f
register space. This would potentially permit root to trigger arbitrary
962ea4f
DMA, so lock it down by default.
962ea4f
962ea4f
This also implicitly locks down the KDADDIO, KDDELIO, KDENABIO and
962ea4f
KDDISABIO console ioctls.
962ea4f
962ea4f
Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 arch/x86/kernel/ioport.c | 4 ++--
962ea4f
 drivers/char/mem.c       | 2 ++
962ea4f
 2 files changed, 4 insertions(+), 2 deletions(-)
962ea4f
962ea4f
diff --git a/arch/x86/kernel/ioport.c b/arch/x86/kernel/ioport.c
c796f87
index b01bc8517..4ccaace 100644
962ea4f
--- a/arch/x86/kernel/ioport.c
962ea4f
+++ b/arch/x86/kernel/ioport.c
c796f87
@@ -29,7 +29,7 @@ asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
c796f87
 
962ea4f
 	if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
962ea4f
 		return -EINVAL;
962ea4f
-	if (turn_on && !capable(CAP_SYS_RAWIO))
962ea4f
+	if (turn_on && (!capable(CAP_SYS_RAWIO) || kernel_is_locked_down()))
962ea4f
 		return -EPERM;
c796f87
 
962ea4f
 	/*
c796f87
@@ -113,7 +113,7 @@ SYSCALL_DEFINE1(iopl, unsigned int, level)
962ea4f
 		return -EINVAL;
962ea4f
 	/* Trying to gain more privileges? */
962ea4f
 	if (level > old) {
962ea4f
-		if (!capable(CAP_SYS_RAWIO))
962ea4f
+		if (!capable(CAP_SYS_RAWIO) || kernel_is_locked_down())
962ea4f
 			return -EPERM;
962ea4f
 	}
962ea4f
 	regs->flags = (regs->flags & ~X86_EFLAGS_IOPL) |
962ea4f
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
c796f87
index f814404..9afebb6 100644
962ea4f
--- a/drivers/char/mem.c
962ea4f
+++ b/drivers/char/mem.c
c796f87
@@ -741,6 +741,8 @@ static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
c796f87
 
962ea4f
 static int open_port(struct inode *inode, struct file *filp)
962ea4f
 {
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
 	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
962ea4f
 }
c796f87
 
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From efd24f7e9af624b62f39efce4f60b31f0ca643d8 Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:17 +0000
962ea4f
Subject: [PATCH 21/32] x86: Restrict MSR access when the kernel is locked down
962ea4f
962ea4f
Writing to MSRs should not be allowed if the kernel is locked down, since
962ea4f
it could lead to execution of arbitrary code in kernel mode.  Based on a
962ea4f
patch by Kees Cook.
962ea4f
962ea4f
Cc: Kees Cook <keescook@chromium.org>
962ea4f
Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 arch/x86/kernel/msr.c | 7 +++++++
962ea4f
 1 file changed, 7 insertions(+)
962ea4f
962ea4f
diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c
c796f87
index ef68880..fbcce02 100644
962ea4f
--- a/arch/x86/kernel/msr.c
962ea4f
+++ b/arch/x86/kernel/msr.c
c796f87
@@ -84,6 +84,9 @@ static ssize_t msr_write(struct file *file, const char __user *buf,
962ea4f
 	int err = 0;
962ea4f
 	ssize_t bytes = 0;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	if (count % 8)
962ea4f
 		return -EINVAL;	/* Invalid chunk size */
c796f87
 
c796f87
@@ -131,6 +134,10 @@ static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
962ea4f
 			err = -EBADF;
962ea4f
 			break;
962ea4f
 		}
962ea4f
+		if (kernel_is_locked_down()) {
962ea4f
+			err = -EPERM;
962ea4f
+			break;
962ea4f
+		}
962ea4f
 		if (copy_from_user(&regs, uregs, sizeof regs)) {
962ea4f
 			err = -EFAULT;
962ea4f
 			break;
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 62ebdffbcb0726b98562e9f1173a6d5967755764 Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:16 +0000
962ea4f
Subject: [PATCH 22/32] asus-wmi: Restrict debugfs interface when the kernel is
962ea4f
 locked down
962ea4f
962ea4f
We have no way of validating what all of the Asus WMI methods do on a given
962ea4f
machine - and there's a risk that some will allow hardware state to be
962ea4f
manipulated in such a way that arbitrary code can be executed in the
962ea4f
kernel, circumventing module loading restrictions.  Prevent that if the
962ea4f
kernel is locked down.
962ea4f
962ea4f
Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/platform/x86/asus-wmi.c | 9 +++++++++
962ea4f
 1 file changed, 9 insertions(+)
962ea4f
962ea4f
diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c
c796f87
index 43cb680..9be3e08 100644
962ea4f
--- a/drivers/platform/x86/asus-wmi.c
962ea4f
+++ b/drivers/platform/x86/asus-wmi.c
c796f87
@@ -1898,6 +1898,9 @@ static int show_dsts(struct seq_file *m, void *data)
962ea4f
 	int err;
962ea4f
 	u32 retval = -1;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
c796f87
 
962ea4f
 	if (err < 0)
c796f87
@@ -1914,6 +1917,9 @@ static int show_devs(struct seq_file *m, void *data)
962ea4f
 	int err;
962ea4f
 	u32 retval = -1;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
962ea4f
 				    &retval);
c796f87
 
c796f87
@@ -1938,6 +1944,9 @@ static int show_call(struct seq_file *m, void *data)
962ea4f
 	union acpi_object *obj;
962ea4f
 	acpi_status status;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
962ea4f
 				     1, asus->debug.method_id,
962ea4f
 				     &input, &output);
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 31f493d03dde1854fa9e540f87a751fbeeb41cb9 Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:16 +0000
962ea4f
Subject: [PATCH 23/32] ACPI: Limit access to custom_method when the kernel is
962ea4f
 locked down
962ea4f
962ea4f
custom_method effectively allows arbitrary access to system memory, making
962ea4f
it possible for an attacker to circumvent restrictions on module loading.
962ea4f
Disable it if the kernel is locked down.
962ea4f
962ea4f
Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/acpi/custom_method.c | 3 +++
962ea4f
 1 file changed, 3 insertions(+)
962ea4f
962ea4f
diff --git a/drivers/acpi/custom_method.c b/drivers/acpi/custom_method.c
962ea4f
index c68e724..e4d721c 100644
962ea4f
--- a/drivers/acpi/custom_method.c
962ea4f
+++ b/drivers/acpi/custom_method.c
962ea4f
@@ -29,6 +29,9 @@ static ssize_t cm_write(struct file *file, const char __user * user_buf,
962ea4f
 	struct acpi_table_header table;
962ea4f
 	acpi_status status;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	if (!(*ppos)) {
962ea4f
 		/* parse the table header to get the table length */
962ea4f
 		if (count <= sizeof(struct acpi_table_header))
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 1dec5dc62c78f78b9efe44aac3d6119d97b67017 Mon Sep 17 00:00:00 2001
962ea4f
From: Josh Boyer <jwboyer@redhat.com>
962ea4f
Date: Tue, 22 Nov 2016 08:46:16 +0000
962ea4f
Subject: [PATCH 24/32] acpi: Ignore acpi_rsdp kernel param when the kernel has
962ea4f
 been locked down
962ea4f
962ea4f
This option allows userspace to pass the RSDP address to the kernel, which
962ea4f
makes it possible for a user to circumvent any restrictions imposed on
962ea4f
loading modules.  Ignore the option when the kernel is locked down.
962ea4f
962ea4f
Signed-off-by: Josh Boyer <jwboyer@redhat.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/acpi/osl.c | 2 +-
962ea4f
 1 file changed, 1 insertion(+), 1 deletion(-)
962ea4f
962ea4f
diff --git a/drivers/acpi/osl.c b/drivers/acpi/osl.c
c796f87
index db78d35..d4d4ba3 100644
962ea4f
--- a/drivers/acpi/osl.c
962ea4f
+++ b/drivers/acpi/osl.c
c796f87
@@ -192,7 +192,7 @@ acpi_physical_address __init acpi_os_get_root_pointer(void)
962ea4f
 	acpi_physical_address pa = 0;
962ea4f
 
962ea4f
 #ifdef CONFIG_KEXEC
962ea4f
-	if (acpi_rsdp)
962ea4f
+	if (acpi_rsdp && !kernel_is_locked_down())
962ea4f
 		return acpi_rsdp;
962ea4f
 #endif
c796f87
 
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 96eaf04100d7365d93f898013175351c7d2702a6 Mon Sep 17 00:00:00 2001
962ea4f
From: Linn Crosetto <linn@hpe.com>
962ea4f
Date: Wed, 23 Nov 2016 13:32:27 +0000
962ea4f
Subject: [PATCH 25/32] acpi: Disable ACPI table override if the kernel is
962ea4f
 locked down
962ea4f
962ea4f
From the kernel documentation (initrd_table_override.txt):
962ea4f
962ea4f
  If the ACPI_INITRD_TABLE_OVERRIDE compile option is true, it is possible
962ea4f
  to override nearly any ACPI table provided by the BIOS with an
962ea4f
  instrumented, modified one.
962ea4f
962ea4f
When securelevel is set, the kernel should disallow any unauthenticated
962ea4f
changes to kernel space.  ACPI tables contain code invoked by the kernel,
962ea4f
so do not allow ACPI tables to be overridden if the kernel is locked down.
962ea4f
962ea4f
Signed-off-by: Linn Crosetto <linn@hpe.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/acpi/tables.c | 5 +++++
962ea4f
 1 file changed, 5 insertions(+)
962ea4f
962ea4f
diff --git a/drivers/acpi/tables.c b/drivers/acpi/tables.c
c796f87
index 2604189..601096d 100644
962ea4f
--- a/drivers/acpi/tables.c
962ea4f
+++ b/drivers/acpi/tables.c
c796f87
@@ -542,6 +542,11 @@ void __init acpi_table_upgrade(void)
962ea4f
 	if (table_nr == 0)
962ea4f
 		return;
c796f87
 
962ea4f
+	if (kernel_is_locked_down()) {
962ea4f
+		pr_notice("kernel is locked down, ignoring table override\n");
962ea4f
+		return;
962ea4f
+	}
962ea4f
+
962ea4f
 	acpi_tables_addr =
962ea4f
 		memblock_find_in_range(0, ACPI_TABLE_UPGRADE_MAX_PHYS,
962ea4f
 				       all_tables_size, PAGE_SIZE);
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 521979b819b853f7578ba8edef8b33bc2077026d Mon Sep 17 00:00:00 2001
962ea4f
From: Linn Crosetto <linn@hpe.com>
962ea4f
Date: Wed, 23 Nov 2016 13:39:41 +0000
962ea4f
Subject: [PATCH 26/32] acpi: Disable APEI error injection if the kernel is
962ea4f
 locked down
962ea4f
962ea4f
ACPI provides an error injection mechanism, EINJ, for debugging and testing
962ea4f
the ACPI Platform Error Interface (APEI) and other RAS features.  If
962ea4f
supported by the firmware, ACPI specification 5.0 and later provide for a
962ea4f
way to specify a physical memory address to which to inject the error.
962ea4f
962ea4f
Injecting errors through EINJ can produce errors which to the platform are
962ea4f
indistinguishable from real hardware errors.  This can have undesirable
962ea4f
side-effects, such as causing the platform to mark hardware as needing
962ea4f
replacement.
962ea4f
962ea4f
While it does not provide a method to load unauthenticated privileged code,
962ea4f
the effect of these errors may persist across reboots and affect trust in
962ea4f
the underlying hardware, so disable error injection through EINJ if
962ea4f
the kernel is locked down.
962ea4f
962ea4f
Signed-off-by: Linn Crosetto <linn@hpe.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/acpi/apei/einj.c | 3 +++
962ea4f
 1 file changed, 3 insertions(+)
962ea4f
962ea4f
diff --git a/drivers/acpi/apei/einj.c b/drivers/acpi/apei/einj.c
c796f87
index ec50c32..e082718 100644
962ea4f
--- a/drivers/acpi/apei/einj.c
962ea4f
+++ b/drivers/acpi/apei/einj.c
962ea4f
@@ -518,6 +518,9 @@ static int einj_error_inject(u32 type, u32 flags, u64 param1, u64 param2,
962ea4f
 	int rc;
962ea4f
 	u64 base_addr, size;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	/* If user manually set "flags", make sure it is legal */
962ea4f
 	if (flags && (flags &
962ea4f
 		~(SETWA_FLAGS_APICID|SETWA_FLAGS_MEM|SETWA_FLAGS_PCIE_SBDF)))
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From fe597dad1cba83345d62c4079c0d8861c426698c Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <mjg59@coreos.com>
962ea4f
Date: Wed, 23 Nov 2016 13:41:23 +0000
962ea4f
Subject: [PATCH 27/32] Enable cold boot attack mitigation
962ea4f
962ea4f
---
962ea4f
 arch/x86/boot/compressed/eboot.c | 28 ++++++++++++++++++++++++++++
962ea4f
 1 file changed, 28 insertions(+)
962ea4f
962ea4f
diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
c796f87
index 801c7a1..ef9409b 100644
962ea4f
--- a/arch/x86/boot/compressed/eboot.c
962ea4f
+++ b/arch/x86/boot/compressed/eboot.c
c796f87
@@ -604,6 +604,31 @@ void setup_graphics(struct boot_params *boot_params)
962ea4f
 	}
962ea4f
 }
c796f87
 
962ea4f
+#define MEMORY_ONLY_RESET_CONTROL_GUID \
962ea4f
+	EFI_GUID (0xe20939be, 0x32d4, 0x41be, 0xa1, 0x50, 0x89, 0x7f, 0x85, 0xd4, 0x98, 0x29)
962ea4f
+
962ea4f
+static void enable_reset_attack_mitigation(void)
962ea4f
+{
962ea4f
+	static const efi_guid_t var_guid = MEMORY_ONLY_RESET_CONTROL_GUID;
962ea4f
+	static const efi_char16_t MemoryOverwriteRequestControl_name[] = {
962ea4f
+		'M', 'e', 'm', 'o', 'r', 'y',
962ea4f
+		'O', 'v', 'e', 'r', 'w', 'r', 'i', 't', 'e',
962ea4f
+		'R', 'e', 'q', 'u', 'e', 's', 't',
962ea4f
+		'C', 'o', 'n', 't', 'r', 'o', 'l',
962ea4f
+		0
962ea4f
+	};
962ea4f
+	u8 val = 1;
962ea4f
+
962ea4f
+	/* Ignore the return value here - there's not really a lot we can do */
962ea4f
+	efi_call_runtime(set_variable,
962ea4f
+			(efi_char16_t *)MemoryOverwriteRequestControl_name,
962ea4f
+			(efi_guid_t *)&var_guid,
962ea4f
+			EFI_VARIABLE_NON_VOLATILE |
962ea4f
+			EFI_VARIABLE_BOOTSERVICE_ACCESS |
962ea4f
+			EFI_VARIABLE_RUNTIME_ACCESS,
962ea4f
+			sizeof(val), val);
962ea4f
+}
962ea4f
+
962ea4f
 /*
962ea4f
  * Because the x86 boot code expects to be passed a boot_params we
962ea4f
  * need to create one ourselves (usually the bootloader would create
c796f87
@@ -988,6 +1013,9 @@ struct boot_params *efi_main(struct efi_config *c,
962ea4f
 	else
962ea4f
 		setup_boot_services32(efi_early);
c796f87
 
962ea4f
+	/* Ask the firmware to clear memory if we don't have a clean shutdown */
962ea4f
+	enable_reset_attack_mitigation();
962ea4f
+
cd3596b
 	/*
cd3596b
 	 * If the boot loader gave us a value for secure_boot then we use that,
cd3596b
 	 * otherwise we ask the BIOS.
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 04fac895731801bc6cb26a6061f1c353c80a866c Mon Sep 17 00:00:00 2001
962ea4f
From: "Lee, Chun-Yi" <jlee@suse.com>
962ea4f
Date: Wed, 23 Nov 2016 13:52:16 +0000
962ea4f
Subject: [PATCH 28/32] bpf: Restrict kernel image access functions when the
962ea4f
 kernel is locked down
962ea4f
962ea4f
There are some bpf functions can be used to read kernel memory:
962ea4f
bpf_probe_read, bpf_probe_write_user and bpf_trace_printk.  These allow
962ea4f
private keys in kernel memory (e.g. the hibernation image signing key) to
962ea4f
be read by an eBPF program.  Prohibit those functions when the kernel is
962ea4f
locked down.
962ea4f
962ea4f
Signed-off-by: Lee, Chun-Yi <jlee@suse.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 kernel/trace/bpf_trace.c | 11 +++++++++++
962ea4f
 1 file changed, 11 insertions(+)
962ea4f
962ea4f
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
c796f87
index cee9802..7fde851 100644
962ea4f
--- a/kernel/trace/bpf_trace.c
962ea4f
+++ b/kernel/trace/bpf_trace.c
962ea4f
@@ -65,6 +65,11 @@ BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
962ea4f
 {
962ea4f
 	int ret;
c796f87
 
962ea4f
+	if (kernel_is_locked_down()) {
962ea4f
+		memset(dst, 0, size);
962ea4f
+		return -EPERM;
962ea4f
+	}
962ea4f
+
962ea4f
 	ret = probe_kernel_read(dst, unsafe_ptr, size);
962ea4f
 	if (unlikely(ret < 0))
962ea4f
 		memset(dst, 0, size);
962ea4f
@@ -84,6 +89,9 @@ static const struct bpf_func_proto bpf_probe_read_proto = {
962ea4f
 BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src,
962ea4f
 	   u32, size)
962ea4f
 {
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	/*
962ea4f
 	 * Ensure we're in user context which is safe for the helper to
962ea4f
 	 * run. This helper has no business in a kthread.
962ea4f
@@ -143,6 +151,9 @@ BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1,
962ea4f
 	if (fmt[--fmt_size] != 0)
962ea4f
 		return -EINVAL;
c796f87
 
962ea4f
+	if (kernel_is_locked_down())
962ea4f
+		return __trace_printk(1, fmt, 0, 0, 0);
962ea4f
+
962ea4f
 	/* check format string for allowed specifiers */
962ea4f
 	for (i = 0; i < fmt_size; i++) {
962ea4f
 		if ((!isprint(fmt[i]) && !isspace(fmt[i])) || !isascii(fmt[i]))
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 707d0e14c2c0d6c3139ff4bcb16ee22c462b4304 Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
962ea4f
Date: Tue, 22 Nov 2016 10:10:34 +0000
962ea4f
Subject: [PATCH 29/32] scsi: Lock down the eata driver
962ea4f
962ea4f
When the kernel is running in secure boot mode, we lock down the kernel to
962ea4f
prevent userspace from modifying the running kernel image.  Whilst this
962ea4f
includes prohibiting access to things like /dev/mem, it must also prevent
962ea4f
access by means of configuring driver modules in such a way as to cause a
962ea4f
device to access or modify the kernel image.
962ea4f
962ea4f
The eata driver takes a single string parameter that contains a slew of
962ea4f
settings, including hardware resource configuration.  Prohibit use of the
962ea4f
parameter if the kernel is locked down.
962ea4f
962ea4f
Suggested-by: One Thousand Gnomes <gnomes@lxorguk.ukuu.org.uk>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
cc: Dario Ballabio <ballabio_dario@emc.com>
962ea4f
cc: "James E.J. Bottomley" <jejb@linux.vnet.ibm.com>
962ea4f
cc: "Martin K. Petersen" <martin.petersen@oracle.com>
962ea4f
cc: linux-scsi@vger.kernel.org
962ea4f
---
962ea4f
 drivers/scsi/eata.c | 7 ++++++-
962ea4f
 1 file changed, 6 insertions(+), 1 deletion(-)
962ea4f
962ea4f
diff --git a/drivers/scsi/eata.c b/drivers/scsi/eata.c
c796f87
index 227dd2c..5c036d10 100644
962ea4f
--- a/drivers/scsi/eata.c
962ea4f
+++ b/drivers/scsi/eata.c
962ea4f
@@ -1552,8 +1552,13 @@ static int eata2x_detect(struct scsi_host_template *tpnt)
c796f87
 
962ea4f
 	tpnt->proc_name = "eata2x";
c796f87
 
962ea4f
-	if (strlen(boot_options))
962ea4f
+	if (strlen(boot_options)) {
962ea4f
+		if (kernel_is_locked_down()) {
962ea4f
+			pr_err("Command line-specified device addresses, irqs and dma channels are not permitted when the kernel is locked down\n");
962ea4f
+			return -EPERM;
962ea4f
+		}
962ea4f
 		option_setup(boot_options);
962ea4f
+	}
c796f87
 
962ea4f
 #if defined(MODULE)
962ea4f
 	/* io_port could have been modified when loading as a module */
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From 07e05e0b7d806a05eba55092630bb75a92311344 Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
962ea4f
Date: Fri, 25 Nov 2016 14:37:45 +0000
962ea4f
Subject: [PATCH 30/32] Prohibit PCMCIA CIS storage when the kernel is locked
962ea4f
 down
962ea4f
962ea4f
Prohibit replacement of the PCMCIA Card Information Structure when the
962ea4f
kernel is locked down.
962ea4f
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/pcmcia/cistpl.c | 5 +++++
962ea4f
 1 file changed, 5 insertions(+)
962ea4f
962ea4f
diff --git a/drivers/pcmcia/cistpl.c b/drivers/pcmcia/cistpl.c
962ea4f
index 55ef7d1..193e4f7 100644
962ea4f
--- a/drivers/pcmcia/cistpl.c
962ea4f
+++ b/drivers/pcmcia/cistpl.c
962ea4f
@@ -1578,6 +1578,11 @@ static ssize_t pccard_store_cis(struct file *filp, struct kobject *kobj,
962ea4f
 	struct pcmcia_socket *s;
962ea4f
 	int error;
c796f87
 
962ea4f
+	if (kernel_is_locked_down()) {
962ea4f
+		pr_err("Direct CIS storage isn't permitted when the kernel is locked down\n");
962ea4f
+		return -EPERM;
962ea4f
+	}
962ea4f
+
962ea4f
 	s = to_socket(container_of(kobj, struct device, kobj));
c796f87
 
962ea4f
 	if (off)
962ea4f
-- 
c796f87
2.7.4
962ea4f
c796f87
From cf1e877adbe35c5f72a9d85570b494aa3f35be6a Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
962ea4f
Date: Wed, 7 Dec 2016 10:28:39 +0000
962ea4f
Subject: [PATCH 31/32] Lock down TIOCSSERIAL
962ea4f
962ea4f
Lock down TIOCSSERIAL as that can be used to change the ioport and irq
962ea4f
settings on a serial port.  This only appears to be an issue for the serial
962ea4f
drivers that use the core serial code.  All other drivers seem to either
962ea4f
ignore attempts to change port/irq or give an error.
962ea4f
962ea4f
Reported-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
962ea4f
 drivers/tty/serial/serial_core.c | 6 ++++++
962ea4f
 1 file changed, 6 insertions(+)
962ea4f
962ea4f
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
c796f87
index 9939c3d..7c040e2 100644
962ea4f
--- a/drivers/tty/serial/serial_core.c
962ea4f
+++ b/drivers/tty/serial/serial_core.c
c796f87
@@ -820,6 +820,12 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
962ea4f
 	new_flags = new_info->flags;
962ea4f
 	old_custom_divisor = uport->custom_divisor;
c796f87
 
962ea4f
+	if ((change_port || change_irq) && kernel_is_locked_down()) {
962ea4f
+		pr_err("Using TIOCSSERIAL to change device addresses, irqs and dma channels is not permitted when the kernel is locked down\n");
962ea4f
+		retval = -EPERM;
962ea4f
+		goto exit;
962ea4f
+	}
962ea4f
+
962ea4f
 	if (!capable(CAP_SYS_ADMIN)) {
962ea4f
 		retval = -EPERM;
962ea4f
 		if (change_irq || change_port ||
962ea4f
-- 
c796f87
2.7.4
c796f87