8cf0063
From 73958cc1f78cfc69f3b1ec26a3406b3c45f6d202 Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Mon, 9 Apr 2018 09:52:45 +0100
8cf0063
Subject: [PATCH 01/24] Add the ability to lock down access to the running
006f5ba
 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
8cf0063
allow the running kernel image to be changed, including:
8cf0063
8cf0063
 - /dev/mem and similar
8cf0063
 - Loading of unauthorised modules
8cf0063
 - Fiddling with MSR registers
8cf0063
 - Suspend to disk managed by the kernel
8cf0063
 - Use of device DMA
8cf0063
8cf0063
Two kernel configuration options are provided:
8cf0063
8cf0063
 (*) CONFIG_LOCK_DOWN_KERNEL
8cf0063
8cf0063
     This makes lockdown available and applies it to all the points that
8cf0063
     need to be locked down if the mode is set.  Lockdown mode can be
8cf0063
     enabled by providing:
8cf0063
8cf0063
	lockdown=1
8cf0063
8cf0063
     on the command line.
8cf0063
8cf0063
 (*) CONFIG_LOCK_DOWN_MANDATORY
8cf0063
8cf0063
     This forces lockdown on at compile time, overriding the command line
8cf0063
     option.
8cf0063
8cf0063
init_lockdown() is used as a hook from which lockdown can be managed in
8cf0063
future.  It has to be called from arch setup code before things like ACPI
8cf0063
are enabled.
8cf0063
8cf0063
Note that, with the other changes in this series, if lockdown mode is
8cf0063
enabled, the kernel will not be able to use certain drivers as the ability
8cf0063
to manually configure hardware parameters would then be prohibited.  This
8cf0063
primarily applies to ISA hardware devices.
962ea4f
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
962ea4f
---
8cf0063
 arch/x86/kernel/setup.c |  2 ++
8cf0063
 include/linux/kernel.h  | 32 ++++++++++++++++++++++++
8cf0063
 security/Kconfig        | 23 ++++++++++++++++-
8cf0063
 security/Makefile       |  3 +++
8cf0063
 security/lock_down.c    | 65 +++++++++++++++++++++++++++++++++++++++++++++++++
8cf0063
 5 files changed, 124 insertions(+), 1 deletion(-)
962ea4f
 create mode 100644 security/lock_down.c
962ea4f
8cf0063
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
8cf0063
index 6285697b6e56..566f0f447053 100644
8cf0063
--- a/arch/x86/kernel/setup.c
8cf0063
+++ b/arch/x86/kernel/setup.c
8cf0063
@@ -996,6 +996,8 @@ void __init setup_arch(char **cmdline_p)
8cf0063
 	if (efi_enabled(EFI_BOOT))
8cf0063
 		efi_init();
8cf0063
8cf0063
+	init_lockdown();
8cf0063
+
8cf0063
 	dmi_scan_machine();
8cf0063
 	dmi_memdev_walk();
8cf0063
 	dmi_set_dump_stack_arch_desc();
962ea4f
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
8cf0063
index 4ae1dfd9bf05..7d085cca9cee 100644
962ea4f
--- a/include/linux/kernel.h
962ea4f
+++ b/include/linux/kernel.h
8cf0063
@@ -306,6 +306,38 @@ static inline void refcount_error_report(struct pt_regs *regs, const char *err)
59566d9
 { }
59566d9
 #endif
135abd0
962ea4f
+#ifdef CONFIG_LOCK_DOWN_KERNEL
8cf0063
+extern void __init init_lockdown(void);
59566d9
+extern bool __kernel_is_locked_down(const char *what, bool first);
962ea4f
+
8cf0063
+#ifndef CONFIG_LOCK_DOWN_MANDATORY
59566d9
+#define kernel_is_locked_down(what)					\
59566d9
+	({								\
59566d9
+		static bool message_given;				\
59566d9
+		bool locked_down = __kernel_is_locked_down(what, !message_given); \
59566d9
+		message_given = true;					\
59566d9
+		locked_down;						\
59566d9
+	})
8cf0063
+#else
8cf0063
+#define kernel_is_locked_down(what)					\
8cf0063
+	({								\
8cf0063
+		static bool message_given;				\
8cf0063
+		__kernel_is_locked_down(what, !message_given);		\
8cf0063
+		message_given = true;					\
8cf0063
+		true;							\
8cf0063
+	})
8cf0063
+#endif
962ea4f
+#else
135abd0
+static inline void __init init_lockdown(void)
962ea4f
+{
962ea4f
+}
8cf0063
+static inline bool __kernel_is_locked_down(const char *what, bool first)
8cf0063
+{
8cf0063
+	return false;
8cf0063
+}
8cf0063
+#define kernel_is_locked_down(what) ({ false; })
962ea4f
+#endif
962ea4f
+
8cf0063
 /* Internal, do not use. */
8cf0063
 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
8cf0063
 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
962ea4f
diff --git a/security/Kconfig b/security/Kconfig
8cf0063
index c4302067a3ad..a68e5bdebad5 100644
962ea4f
--- a/security/Kconfig
962ea4f
+++ b/security/Kconfig
8cf0063
@@ -231,6 +231,28 @@ config STATIC_USERMODEHELPER_PATH
c796f87
 	  If you wish for all usermode helper programs to be disabled,
c796f87
 	  specify an empty string here (i.e. "").
135abd0
962ea4f
+config LOCK_DOWN_KERNEL
962ea4f
+	bool "Allow the kernel to be 'locked down'"
962ea4f
+	help
8cf0063
+	  Allow the kernel to be locked down.  Locking down the kernel turns
8cf0063
+	  off various features that might otherwise allow access to the kernel
8cf0063
+	  image (eg. setting MSR registers).
8cf0063
+
8cf0063
+	  Note, however, that locking down your kernel will prevent some
8cf0063
+	  drivers from functioning because allowing manual configuration of
8cf0063
+	  hardware parameters is forbidden, lest a device be used to access the
8cf0063
+	  kernel by DMA.  This mostly applies to ISA devices.
8cf0063
+
8cf0063
+	  The kernel lockdown can be triggered by adding lockdown=1 to the
8cf0063
+	  kernel command line.
8cf0063
+
8cf0063
+config LOCK_DOWN_MANDATORY
8cf0063
+	bool "Make kernel lockdown mandatory"
8cf0063
+	depends on LOCK_DOWN_KERNEL
8cf0063
+	help
8cf0063
+	  Makes the lockdown non-negotiable.  It is always on and cannot be
8cf0063
+	  disabled.
962ea4f
+
962ea4f
 source security/selinux/Kconfig
962ea4f
 source security/smack/Kconfig
962ea4f
 source security/tomoyo/Kconfig
8cf0063
@@ -278,4 +300,3 @@ config DEFAULT_SECURITY
8cf0063
 	default "" if DEFAULT_SECURITY_DAC
8cf0063
8cf0063
 endmenu
8cf0063
-
962ea4f
diff --git a/security/Makefile b/security/Makefile
f20e0a3
index 4d2d3782ddef..507ac8c520ce 100644
962ea4f
--- a/security/Makefile
962ea4f
+++ b/security/Makefile
f20e0a3
@@ -30,3 +30,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
8cf0063
index 000000000000..f35ffdd096ad
962ea4f
--- /dev/null
962ea4f
+++ b/security/lock_down.c
8cf0063
@@ -0,0 +1,65 @@
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/export.h>
8cf0063
+#include <linux/sched.h>
962ea4f
+
8cf0063
+#ifndef CONFIG_LOCK_DOWN_MANDATORY
59566d9
+static __ro_after_init bool kernel_locked_down;
8cf0063
+#else
8cf0063
+#define kernel_locked_down true
8cf0063
+#endif
962ea4f
+
962ea4f
+/*
962ea4f
+ * Put the kernel into lock-down mode.
962ea4f
+ */
59566d9
+static void __init lock_kernel_down(const char *where)
59566d9
+{
8cf0063
+#ifndef CONFIG_LOCK_DOWN_MANDATORY
59566d9
+	if (!kernel_locked_down) {
59566d9
+		kernel_locked_down = true;
59566d9
+		pr_notice("Kernel is locked down from %s; see man kernel_lockdown.7\n",
59566d9
+			  where);
59566d9
+	}
8cf0063
+#endif
59566d9
+}
59566d9
+
59566d9
+static int __init lockdown_param(char *ignored)
962ea4f
+{
59566d9
+	lock_kernel_down("command line");
59566d9
+	return 0;
962ea4f
+}
962ea4f
+
59566d9
+early_param("lockdown", lockdown_param);
59566d9
+
962ea4f
+/*
59566d9
+ * Lock the kernel down from very early in the arch setup.  This must happen
59566d9
+ * prior to things like ACPI being initialised.
962ea4f
+ */
59566d9
+void __init init_lockdown(void)
962ea4f
+{
8cf0063
+#ifdef CONFIG_LOCK_DOWN_MANDATORY
8cf0063
+	pr_notice("Kernel is locked down from config; see man kernel_lockdown.7\n");
59566d9
+#endif
962ea4f
+}
962ea4f
+
962ea4f
+/**
962ea4f
+ * kernel_is_locked_down - Find out if the kernel is locked down
59566d9
+ * @what: Tag to use in notice generated if lockdown is in effect
962ea4f
+ */
59566d9
+bool __kernel_is_locked_down(const char *what, bool first)
962ea4f
+{
59566d9
+	if (what && first && kernel_locked_down)
8cf0063
+		pr_notice("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",
8cf0063
+			  current->comm, what);
962ea4f
+	return kernel_locked_down;
962ea4f
+}
59566d9
+EXPORT_SYMBOL(__kernel_is_locked_down);
135abd0
-- 
f20e0a3
2.14.3
962ea4f
8cf0063
From 13dada34d9aa56ac4ee5438c7ebefde2d30d5542 Mon Sep 17 00:00:00 2001
59566d9
From: Kyle McMartin <kyle@redhat.com>
8cf0063
Date: Mon, 9 Apr 2018 09:52:45 +0100
8cf0063
Subject: [PATCH 02/24] Add a SysRq option to lift kernel lockdown
962ea4f
59566d9
Make an option to provide a sysrq key that will lift the kernel lockdown,
59566d9
thereby allowing the running kernel image to be accessed and modified.
962ea4f
f20e0a3
On x86 this is triggered with SysRq+x, but this key may not be available on
f20e0a3
all arches, so it is set by setting LOCKDOWN_LIFT_KEY in asm/setup.h.
f20e0a3
Since this macro must be defined in an arch to be able to use this facility
f20e0a3
for that arch, the Kconfig option is restricted to arches that support it.
962ea4f
59566d9
Signed-off-by: Kyle McMartin <kyle@redhat.com>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
59566d9
cc: x86@kernel.org
962ea4f
---
135abd0
 arch/x86/include/asm/setup.h |  2 ++
135abd0
 drivers/input/misc/uinput.c  |  1 +
135abd0
 drivers/tty/sysrq.c          | 19 ++++++++++++------
135abd0
 include/linux/input.h        |  5 +++++
135abd0
 include/linux/sysrq.h        |  8 +++++++-
135abd0
 kernel/debug/kdb/kdb_main.c  |  2 +-
8cf0063
 security/Kconfig             | 11 +++++++++++
135abd0
 security/lock_down.c         | 47 ++++++++++++++++++++++++++++++++++++++++++++
8cf0063
 8 files changed, 87 insertions(+), 8 deletions(-)
59566d9
59566d9
diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
f20e0a3
index ae13bc974416..3108e297d87d 100644
59566d9
--- a/arch/x86/include/asm/setup.h
59566d9
+++ b/arch/x86/include/asm/setup.h
f20e0a3
@@ -9,6 +9,8 @@
59566d9
 #include <linux/linkage.h>
59566d9
 #include <asm/page_types.h>
135abd0
59566d9
+#define LOCKDOWN_LIFT_KEY 'x'
962ea4f
+
59566d9
 #ifdef __i386__
135abd0
59566d9
 #include <linux/pfn.h>
962ea4f
diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c
f20e0a3
index 96a887f33698..027c730631cc 100644
962ea4f
--- a/drivers/input/misc/uinput.c
962ea4f
+++ b/drivers/input/misc/uinput.c
f20e0a3
@@ -365,6 +365,7 @@ static int uinput_create_device(struct uinput_device *udev)
e1d1471
 		dev->flush = uinput_dev_flush;
e1d1471
 	}
f20e0a3
e1d1471
+	dev->flags |= INPUTDEV_FLAGS_SYNTHETIC;
e1d1471
 	dev->event = uinput_dev_event;
f20e0a3
962ea4f
 	input_set_drvdata(udev->dev, udev);
962ea4f
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
8cf0063
index 6364890575ec..ffeb3aa86cd1 100644
962ea4f
--- a/drivers/tty/sysrq.c
962ea4f
+++ b/drivers/tty/sysrq.c
f20e0a3
@@ -487,6 +487,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 */
f20e0a3
@@ -530,7 +531,7 @@ static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
962ea4f
                 sysrq_key_table[i] = op_p;
962ea4f
 }
135abd0
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;
f20e0a3
@@ -550,11 +551,15 @@ void __handle_sysrq(int key, bool check_mask)
135abd0
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);
f20e0a3
@@ -586,7 +591,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);
135abd0
f20e0a3
@@ -667,7 +672,7 @@ static void sysrq_do_reset(struct timer_list *t)
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);
135abd0
962ea4f
 	if (sysrq_reset_downtime_ms)
962ea4f
 		mod_timer(&state->keyreset_timer,
f20e0a3
@@ -818,8 +823,10 @@ static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
135abd0
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
 	}
f20e0a3
@@ -1102,7 +1109,7 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
135abd0
962ea4f
 		if (get_user(c, buf))
962ea4f
 			return -EFAULT;
962ea4f
-		__handle_sysrq(c, false);
962ea4f
+		__handle_sysrq(c, SYSRQ_FROM_PROC);
962ea4f
 	}
135abd0
962ea4f
 	return count;
962ea4f
diff --git a/include/linux/input.h b/include/linux/input.h
f20e0a3
index 7c7516eb7d76..38cd0ea72c37 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;
135abd0
962ea4f
+	unsigned int flags;
962ea4f
+
962ea4f
 	unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];
135abd0
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)
135abd0
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
f20e0a3
index 8c71874e8485..7de1f08b60a9 100644
962ea4f
--- a/include/linux/sysrq.h
962ea4f
+++ b/include/linux/sysrq.h
f20e0a3
@@ -29,6 +29,8 @@
962ea4f
 #define SYSRQ_ENABLE_BOOT	0x0080
962ea4f
 #define SYSRQ_ENABLE_RTNICE	0x0100
135abd0
962ea4f
+#define SYSRQ_DISABLE_USERSPACE	0x00010000
962ea4f
+
962ea4f
 struct sysrq_key_op {
962ea4f
 	void (*handler)(int);
962ea4f
 	char *help_msg;
f20e0a3
@@ -43,8 +45,12 @@ struct sysrq_key_op {
962ea4f
  * are available -- else NULL's).
962ea4f
  */
135abd0
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
f20e0a3
index dbb0781a0533..aae9a0f44058 100644
962ea4f
--- a/kernel/debug/kdb/kdb_main.c
962ea4f
+++ b/kernel/debug/kdb/kdb_main.c
59566d9
@@ -1970,7 +1970,7 @@ static int kdb_sr(int argc, const char **argv)
962ea4f
 		return KDB_ARGCOUNT;
135abd0
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--;
135abd0
962ea4f
 	return 0;
59566d9
diff --git a/security/Kconfig b/security/Kconfig
8cf0063
index a68e5bdebad5..46967ee77dfd 100644
59566d9
--- a/security/Kconfig
59566d9
+++ b/security/Kconfig
8cf0063
@@ -253,6 +253,17 @@ config LOCK_DOWN_MANDATORY
8cf0063
 	  Makes the lockdown non-negotiable.  It is always on and cannot be
8cf0063
 	  disabled.
135abd0
59566d9
+config ALLOW_LOCKDOWN_LIFT_BY_SYSRQ
59566d9
+	bool "Allow the kernel lockdown to be lifted by SysRq"
f20e0a3
+	depends on LOCK_DOWN_KERNEL
8cf0063
+	depends on !LOCK_DOWN_MANDATORY
f20e0a3
+	depends on MAGIC_SYSRQ
f20e0a3
+	depends on X86
59566d9
+	help
59566d9
+	  Allow the lockdown on a kernel to be lifted, by pressing a SysRq key
8cf0063
+	  combination on a wired keyboard.  On x86, this is SysRq+x.
59566d9
+
59566d9
+
59566d9
 source security/selinux/Kconfig
59566d9
 source security/smack/Kconfig
59566d9
 source security/tomoyo/Kconfig
59566d9
diff --git a/security/lock_down.c b/security/lock_down.c
8cf0063
index f35ffdd096ad..2615669dbf03 100644
59566d9
--- a/security/lock_down.c
59566d9
+++ b/security/lock_down.c
8cf0063
@@ -11,9 +11,15 @@
135abd0
59566d9
 #include <linux/export.h>
8cf0063
 #include <linux/sched.h>
59566d9
+#include <linux/sysrq.h>
135abd0
+#include <asm/setup.h>
135abd0
8cf0063
 #ifndef CONFIG_LOCK_DOWN_MANDATORY
135abd0
+#ifdef CONFIG_ALLOW_LOCKDOWN_LIFT_BY_SYSRQ
59566d9
+static __read_mostly bool kernel_locked_down;
59566d9
+#else
59566d9
 static __ro_after_init bool kernel_locked_down;
59566d9
+#endif
8cf0063
 #else
8cf0063
 #define kernel_locked_down true
8cf0063
 #endif
8cf0063
@@ -63,3 +69,44 @@ bool __kernel_is_locked_down(const char *what, bool first)
59566d9
 	return kernel_locked_down;
59566d9
 }
59566d9
 EXPORT_SYMBOL(__kernel_is_locked_down);
59566d9
+
135abd0
+#ifdef CONFIG_ALLOW_LOCKDOWN_LIFT_BY_SYSRQ
135abd0
+
59566d9
+/*
59566d9
+ * Take the kernel out of lockdown mode.
59566d9
+ */
59566d9
+static void lift_kernel_lockdown(void)
59566d9
+{
59566d9
+	pr_notice("Lifting lockdown\n");
59566d9
+	kernel_locked_down = false;
59566d9
+}
59566d9
+
59566d9
+/*
59566d9
+ * Allow lockdown to be lifted by pressing something like SysRq+x (and not by
59566d9
+ * echoing the appropriate letter into the sysrq-trigger file).
59566d9
+ */
59566d9
+static void sysrq_handle_lockdown_lift(int key)
59566d9
+{
59566d9
+	if (kernel_locked_down)
59566d9
+		lift_kernel_lockdown();
59566d9
+}
59566d9
+
59566d9
+static struct sysrq_key_op lockdown_lift_sysrq_op = {
59566d9
+	.handler	= sysrq_handle_lockdown_lift,
59566d9
+	.help_msg	= "unSB(x)",
59566d9
+	.action_msg	= "Disabling Secure Boot restrictions",
59566d9
+	.enable_mask	= SYSRQ_DISABLE_USERSPACE,
59566d9
+};
59566d9
+
59566d9
+static int __init lockdown_lift_sysrq(void)
59566d9
+{
59566d9
+	if (kernel_locked_down) {
59566d9
+		lockdown_lift_sysrq_op.help_msg[5] = LOCKDOWN_LIFT_KEY;
59566d9
+		register_sysrq_key(LOCKDOWN_LIFT_KEY, &lockdown_lift_sysrq_op);
59566d9
+	}
59566d9
+	return 0;
59566d9
+}
59566d9
+
59566d9
+late_initcall(lockdown_lift_sysrq);
59566d9
+
135abd0
+#endif /* CONFIG_ALLOW_LOCKDOWN_LIFT_BY_SYSRQ */
135abd0
-- 
f20e0a3
2.14.3
f20e0a3
8cf0063
From 2d534703537af95f601d3bdab11ee6ba8b3bc2dc Mon Sep 17 00:00:00 2001
f20e0a3
From: Mimi Zohar <zohar@linux.vnet.ibm.com>
8cf0063
Date: Mon, 9 Apr 2018 09:52:45 +0100
8cf0063
Subject: [PATCH 03/24] ima: require secure_boot rules in lockdown mode
f20e0a3
f20e0a3
Require the "secure_boot" rules, whether or not it is specified
f20e0a3
on the boot command line, for both the builtin and custom policies
f20e0a3
in secure boot lockdown mode.
f20e0a3
f20e0a3
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
f20e0a3
Signed-off-by: David Howells <dhowells@redhat.com>
f20e0a3
---
Jeremy Cline ee73ea2
 security/integrity/ima/ima_policy.c | 34 +++++++++++++++++++++++------
Jeremy Cline ee73ea2
 1 file changed, 27 insertions(+), 7 deletions(-)
f20e0a3
f20e0a3
diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c
Jeremy Cline ee73ea2
index 8c9499867c91..f8428f579924 100644
f20e0a3
--- a/security/integrity/ima/ima_policy.c
f20e0a3
+++ b/security/integrity/ima/ima_policy.c
Jeremy Cline ee73ea2
@@ -481,14 +481,21 @@ static int ima_appraise_flag(enum ima_hooks func)
f20e0a3
  */
f20e0a3
 void __init ima_init_policy(void)
f20e0a3
 {
f20e0a3
-	int i, measure_entries, appraise_entries, secure_boot_entries;
f20e0a3
+	int i;
f20e0a3
+	int measure_entries = 0;
f20e0a3
+	int appraise_entries = 0;
f20e0a3
+	int secure_boot_entries = 0;
f20e0a3
+	bool kernel_locked_down = __kernel_is_locked_down(NULL, false);
Jeremy Cline ee73ea2
 
f20e0a3
 	/* if !ima_policy set entries = 0 so we load NO default rules */
f20e0a3
-	measure_entries = ima_policy ? ARRAY_SIZE(dont_measure_rules) : 0;
f20e0a3
-	appraise_entries = ima_use_appraise_tcb ?
f20e0a3
-			 ARRAY_SIZE(default_appraise_rules) : 0;
f20e0a3
-	secure_boot_entries = ima_use_secure_boot ?
f20e0a3
-			ARRAY_SIZE(secure_boot_rules) : 0;
f20e0a3
+	if (ima_policy)
f20e0a3
+		measure_entries = ARRAY_SIZE(dont_measure_rules);
f20e0a3
+
f20e0a3
+	if (ima_use_appraise_tcb)
f20e0a3
+		appraise_entries = ARRAY_SIZE(default_appraise_rules);
f20e0a3
+
f20e0a3
+	if (ima_use_secure_boot || kernel_locked_down)
f20e0a3
+		secure_boot_entries = ARRAY_SIZE(secure_boot_rules);
Jeremy Cline ee73ea2
 
f20e0a3
 	for (i = 0; i < measure_entries; i++)
f20e0a3
 		list_add_tail(&dont_measure_rules[i].list, &ima_default_rules);
Jeremy Cline ee73ea2
@@ -509,12 +516,25 @@ void __init ima_init_policy(void)
4b8512e
 
f20e0a3
 	/*
Jeremy Cline ee73ea2
 	 * Insert the builtin "secure_boot" policy rules requiring file
Jeremy Cline ee73ea2
-	 * signatures, prior to any other appraise rules.
Jeremy Cline ee73ea2
+	 * signatures, prior to any other appraise rules.  In secure boot
Jeremy Cline ee73ea2
+	 * lock-down mode, also require these appraise rules for custom
Jeremy Cline ee73ea2
+	 * policies.
f20e0a3
 	 */
4b8512e
 	for (i = 0; i < secure_boot_entries; i++) {
f20e0a3
+		struct ima_rule_entry *entry;
f20e0a3
+
f20e0a3
+		/* Include for builtin policies */
4b8512e
 		list_add_tail(&secure_boot_rules[i].list, &ima_default_rules);
4b8512e
 		temp_ima_appraise |=
4b8512e
 		    ima_appraise_flag(secure_boot_rules[i].func);
f20e0a3
+
f20e0a3
+		/* Include for custom policies */
f20e0a3
+		if (kernel_locked_down) {
f20e0a3
+			entry = kmemdup(&secure_boot_rules[i], sizeof(*entry),
f20e0a3
+					GFP_KERNEL);
f20e0a3
+			if (entry)
f20e0a3
+				list_add_tail(&entry->list, &ima_policy_rules);
f20e0a3
+		}
4b8512e
 	}
4b8512e
 
Jeremy Cline ee73ea2
 	/*
f20e0a3
-- 
Jeremy Cline ee73ea2
2.17.1
f20e0a3
Jeremy Cline ee73ea2
From 980a380dc973c5a7745e4833aba368637a99df2e Mon Sep 17 00:00:00 2001
59566d9
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Mon, 9 Apr 2018 09:52:46 +0100
Jeremy Cline ee73ea2
Subject: [PATCH] Enforce module signatures if the kernel is locked down
59566d9
59566d9
If the kernel is locked down, require that all modules have valid
f20e0a3
signatures that we can verify or that IMA can validate the file.
f20e0a3
f20e0a3
I have adjusted the errors generated:
f20e0a3
f20e0a3
 (1) If there's no signature (ENODATA) or we can't check it (ENOPKG,
f20e0a3
     ENOKEY), then:
f20e0a3
f20e0a3
     (a) If signatures are enforced then EKEYREJECTED is returned.
f20e0a3
f20e0a3
     (b) If IMA will have validated the image, return 0 (okay).
f20e0a3
f20e0a3
     (c) If there's no signature or we can't check it, but the kernel is
f20e0a3
	 locked down then EPERM is returned (this is then consistent with
f20e0a3
	 other lockdown cases).
f20e0a3
f20e0a3
 (2) If the signature is unparseable (EBADMSG, EINVAL), the signature fails
f20e0a3
     the check (EKEYREJECTED) or a system error occurs (eg. ENOMEM), we
f20e0a3
     return the error we got.
f20e0a3
f20e0a3
Note that the X.509 code doesn't check for key expiry as the RTC might not
f20e0a3
be valid or might not have been transferred to the kernel's clock yet.
59566d9
59566d9
Signed-off-by: David Howells <dhowells@redhat.com>
f20e0a3
Reviewed-by: Jiri Bohac <jbohac@suse.cz>
f20e0a3
cc: "Lee, Chun-Yi" <jlee@suse.com>
f20e0a3
cc: James Morris <james.l.morris@oracle.com>
59566d9
---
Jeremy Cline ee73ea2
 kernel/module.c | 56 +++++++++++++++++++++++++++++++++++++------------
f20e0a3
 1 file changed, 43 insertions(+), 13 deletions(-)
59566d9
59566d9
diff --git a/kernel/module.c b/kernel/module.c
Jeremy Cline ee73ea2
index b046a32520d8..3bb0722c106e 100644
59566d9
--- a/kernel/module.c
59566d9
+++ b/kernel/module.c
f20e0a3
@@ -64,6 +64,7 @@
f20e0a3
 #include <linux/bsearch.h>
f20e0a3
 #include <linux/dynamic_debug.h>
f20e0a3
 #include <linux/audit.h>
f20e0a3
+#include <linux/ima.h>
f20e0a3
 #include <uapi/linux/module.h>
f20e0a3
 #include "module-internal.h"
Jeremy Cline ee73ea2
 
Jeremy Cline ee73ea2
@@ -2741,10 +2742,12 @@ static inline void kmemleak_load_module(const struct module *mod,
f20e0a3
 #endif
Jeremy Cline ee73ea2
 
f20e0a3
 #ifdef CONFIG_MODULE_SIG
f20e0a3
-static int module_sig_check(struct load_info *info, int flags)
f20e0a3
+static int module_sig_check(struct load_info *info, int flags,
f20e0a3
+			    bool can_do_ima_check)
f20e0a3
 {
f20e0a3
-	int err = -ENOKEY;
f20e0a3
+	int err = -ENODATA;
f20e0a3
 	const unsigned long markerlen = sizeof(MODULE_SIG_STRING) - 1;
f20e0a3
+	const char *reason;
f20e0a3
 	const void *mod = info->hdr;
Jeremy Cline ee73ea2
 
f20e0a3
 	/*
Jeremy Cline ee73ea2
@@ -2759,19 +2762,46 @@ static int module_sig_check(struct load_info *info, int flags)
Jeremy Cline ee73ea2
 		err = mod_verify_sig(mod, info);
59566d9
 	}
Jeremy Cline ee73ea2
 
f20e0a3
-	if (!err) {
f20e0a3
+	switch (err) {
f20e0a3
+	case 0:
f20e0a3
 		info->sig_ok = true;
f20e0a3
 		return 0;
f20e0a3
-	}
Jeremy Cline ee73ea2
 
f20e0a3
-	/* Not having a signature is only an error if we're strict. */
c60d151
-	if (err == -ENOKEY && !is_module_sig_enforced())
f20e0a3
-		err = 0;
f20e0a3
+		/* We don't permit modules to be loaded into trusted kernels
f20e0a3
+		 * without a valid signature on them, but if we're not
f20e0a3
+		 * enforcing, certain errors are non-fatal.
f20e0a3
+		 */
f20e0a3
+	case -ENODATA:
f20e0a3
+		reason = "Loading of unsigned module";
f20e0a3
+		goto decide;
f20e0a3
+	case -ENOPKG:
f20e0a3
+		reason = "Loading of module with unsupported crypto";
f20e0a3
+		goto decide;
f20e0a3
+	case -ENOKEY:
f20e0a3
+		reason = "Loading of module with unavailable key";
f20e0a3
+	decide:
f20e0a3
+		if (sig_enforce) {
f20e0a3
+			pr_notice("%s is rejected\n", reason);
f20e0a3
+			return -EKEYREJECTED;
f20e0a3
+		}
Jeremy Cline ee73ea2
 
f20e0a3
-	return err;
f20e0a3
+		if (can_do_ima_check && is_ima_appraise_enabled())
f20e0a3
+			return 0;
f20e0a3
+		if (kernel_is_locked_down(reason))
f20e0a3
+			return -EPERM;
f20e0a3
+		return 0;
f20e0a3
+
f20e0a3
+		/* All other errors are fatal, including nomem, unparseable
f20e0a3
+		 * signatures and signature check failures - even if signatures
f20e0a3
+		 * aren't required.
f20e0a3
+		 */
f20e0a3
+	default:
f20e0a3
+		return err;
f20e0a3
+	}
f20e0a3
 }
f20e0a3
 #else /* !CONFIG_MODULE_SIG */
f20e0a3
-static int module_sig_check(struct load_info *info, int flags)
f20e0a3
+static int module_sig_check(struct load_info *info, int flags,
f20e0a3
+			    bool can_do_ima_check)
f20e0a3
 {
f20e0a3
 	return 0;
f20e0a3
 }
Jeremy Cline ee73ea2
@@ -3620,7 +3650,7 @@ static int unknown_module_param_cb(char *param, char *val, const char *modname,
f20e0a3
 /* Allocate and load the module: note that size of section 0 is always
f20e0a3
    zero, and we rely on this for optional sections. */
f20e0a3
 static int load_module(struct load_info *info, const char __user *uargs,
f20e0a3
-		       int flags)
f20e0a3
+		       int flags, bool can_do_ima_check)
f20e0a3
 {
f20e0a3
 	struct module *mod;
Jeremy Cline ee73ea2
 	long err = 0;
Jeremy Cline ee73ea2
@@ -3639,7 +3669,7 @@ static int load_module(struct load_info *info, const char __user *uargs,
Jeremy Cline ee73ea2
 		goto free_copy;
Jeremy Cline ee73ea2
 	}
Jeremy Cline ee73ea2
 
f20e0a3
-	err = module_sig_check(info, flags);
f20e0a3
+	err = module_sig_check(info, flags, can_do_ima_check);
f20e0a3
 	if (err)
f20e0a3
 		goto free_copy;
Jeremy Cline ee73ea2
 
Jeremy Cline ee73ea2
@@ -3834,7 +3864,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
f20e0a3
 	if (err)
f20e0a3
 		return err;
Jeremy Cline ee73ea2
 
f20e0a3
-	return load_module(&info, uargs, 0);
f20e0a3
+	return load_module(&info, uargs, 0, false);
f20e0a3
 }
Jeremy Cline ee73ea2
 
f20e0a3
 SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
Jeremy Cline ee73ea2
@@ -3861,7 +3891,7 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
f20e0a3
 	info.hdr = hdr;
f20e0a3
 	info.len = size;
Jeremy Cline ee73ea2
 
f20e0a3
-	return load_module(&info, uargs, flags);
f20e0a3
+	return load_module(&info, uargs, flags, true);
f20e0a3
 }
Jeremy Cline ee73ea2
 
f20e0a3
 static inline int within(unsigned long addr, void *start, unsigned long size)
135abd0
-- 
Jeremy Cline ee73ea2
2.17.1
59566d9
8cf0063
From 7948946e19294e7560c81b177b2788d21ed79f59 Mon Sep 17 00:00:00 2001
f20e0a3
From: Matthew Garrett <mjg59@srcf.ucam.org>
8cf0063
Date: Mon, 9 Apr 2018 09:52:46 +0100
8cf0063
Subject: [PATCH 05/24] Restrict /dev/{mem,kmem,port} when the kernel is locked
006f5ba
 down
006f5ba
006f5ba
Allowing users to read and write to core kernel memory makes it possible
006f5ba
for the kernel to be subverted, avoiding module loading restrictions, and
006f5ba
also to steal cryptographic information.
006f5ba
006f5ba
Disallow /dev/mem and /dev/kmem from being opened this when the kernel has
006f5ba
been locked down to prevent this.
59566d9
006f5ba
Also disallow /dev/port from being opened to prevent raw ioport access and
006f5ba
thus DMA from being used to accomplish the same thing.
59566d9
f20e0a3
Signed-off-by: Matthew Garrett <mjg59@srcf.ucam.org>
59566d9
Signed-off-by: David Howells <dhowells@redhat.com>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
---
006f5ba
 drivers/char/mem.c | 2 ++
006f5ba
 1 file changed, 2 insertions(+)
59566d9
59566d9
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
8cf0063
index ffeb60d3434c..b2fca26e5765 100644
59566d9
--- a/drivers/char/mem.c
59566d9
+++ b/drivers/char/mem.c
f20e0a3
@@ -784,6 +784,8 @@ static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
135abd0
006f5ba
 static int open_port(struct inode *inode, struct file *filp)
006f5ba
 {
006f5ba
+	if (kernel_is_locked_down("/dev/mem,kmem,port"))
59566d9
+		return -EPERM;
006f5ba
 	return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
006f5ba
 }
135abd0
135abd0
-- 
f20e0a3
2.14.3
59566d9
8cf0063
From a19b6b9637f114388cc7087176860eee962cac79 Mon Sep 17 00:00:00 2001
f20e0a3
From: Matthew Garrett <mjg59@srcf.ucam.org>
8cf0063
Date: Mon, 9 Apr 2018 09:52:46 +0100
8cf0063
Subject: [PATCH 06/24] kexec_load: Disable at runtime if the kernel is locked
f20e0a3
 down
962ea4f
f20e0a3
The kexec_load() syscall permits the loading and execution of arbitrary
f20e0a3
code in ring 0, which is something that lock-down is meant to prevent. It
f20e0a3
makes sense to disable kexec_load() in this situation.
962ea4f
f20e0a3
This does not affect kexec_file_load() syscall which can check for a
f20e0a3
signature on the image to be booted.
962ea4f
f20e0a3
Signed-off-by: Matthew Garrett <mjg59@srcf.ucam.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
59566d9
Acked-by: Dave Young <dyoung@redhat.com>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
006f5ba
Reviewed-by: James Morris <james.l.morris@oracle.com>
59566d9
cc: kexec@lists.infradead.org
962ea4f
---
135abd0
 kernel/kexec.c | 7 +++++++
962ea4f
 1 file changed, 7 insertions(+)
962ea4f
962ea4f
diff --git a/kernel/kexec.c b/kernel/kexec.c
Jeremy Cline ee73ea2
index 68559808fdfa..041d505070e1 100644
962ea4f
--- a/kernel/kexec.c
962ea4f
+++ b/kernel/kexec.c
Jeremy Cline ee73ea2
@@ -202,6 +202,13 @@ static inline int kexec_load_check(unsigned long nr_segments,
f20e0a3
 	if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
962ea4f
 		return -EPERM;
Jeremy Cline ee73ea2
 
f20e0a3
+	/*
962ea4f
+	 * kexec can be used to circumvent module loading restrictions, so
962ea4f
+	 * prevent loading in that case
962ea4f
+	 */
59566d9
+	if (kernel_is_locked_down("kexec of unsigned images"))
962ea4f
+		return -EPERM;
962ea4f
+
Jeremy Cline ee73ea2
 	/* Permit LSMs and IMA to fail the kexec */
Jeremy Cline ee73ea2
 	result = security_kernel_load_data(LOADING_KEXEC_IMAGE);
Jeremy Cline ee73ea2
 	if (result < 0)
135abd0
-- 
Jeremy Cline ee73ea2
2.17.1
962ea4f
8cf0063
From aed8ee965258e3926be6aaeb57aef8a9a03c9989 Mon Sep 17 00:00:00 2001
962ea4f
From: Josh Boyer <jwboyer@fedoraproject.org>
8cf0063
Date: Mon, 9 Apr 2018 09:52:47 +0100
8cf0063
Subject: [PATCH 07/24] 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>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
cc: linux-pm@vger.kernel.org
962ea4f
---
135abd0
 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
8cf0063
index 5454cc639a8d..629f158f5a0c 100644
962ea4f
--- a/kernel/power/hibernate.c
962ea4f
+++ b/kernel/power/hibernate.c
59566d9
@@ -70,7 +70,7 @@ static const struct platform_hibernation_ops *hibernation_ops;
135abd0
962ea4f
 bool hibernation_available(void)
962ea4f
 {
962ea4f
-	return (nohibernate == 0);
59566d9
+	return nohibernate == 0 && !kernel_is_locked_down("Hibernation");
962ea4f
 }
962ea4f
135abd0
 /**
135abd0
-- 
f20e0a3
2.14.3
59566d9
8cf0063
From 8732c1663d7c0305ae01ba5a1ee4d2299b7b4612 Mon Sep 17 00:00:00 2001
962ea4f
From: Matthew Garrett <mjg59@srcf.ucam.org>
8cf0063
Date: Mon, 9 Apr 2018 09:52:47 +0100
8cf0063
Subject: [PATCH 08/24] 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>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
006f5ba
Reviewed-by: James Morris <james.l.morris@oracle.com>
59566d9
cc: linux-pm@vger.kernel.org
962ea4f
---
135abd0
 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
8cf0063
index 75c959de4b29..959b336d8eca 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;
135abd0
59566d9
+	if (kernel_is_locked_down("/dev/snapshot"))
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	lock_system_sleep();
962ea4f
135abd0
 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
135abd0
-- 
f20e0a3
2.14.3
59566d9
8cf0063
From 4f5f0aae410d1929872eec346954c85e3a85f4f3 Mon Sep 17 00:00:00 2001
f20e0a3
From: Matthew Garrett <mjg59@srcf.ucam.org>
8cf0063
Date: Mon, 9 Apr 2018 09:52:48 +0100
8cf0063
Subject: [PATCH 09/24] PCI: Lock down BAR access when the kernel is locked
135abd0
 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
f20e0a3
Signed-off-by: Matthew Garrett <mjg59@srcf.ucam.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
59566d9
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
cc: linux-pci@vger.kernel.org
962ea4f
---
135abd0
 drivers/pci/pci-sysfs.c | 9 +++++++++
135abd0
 drivers/pci/proc.c      | 9 ++++++++-
135abd0
 drivers/pci/syscall.c   | 3 ++-
59566d9
 3 files changed, 19 insertions(+), 2 deletions(-)
962ea4f
962ea4f
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
8cf0063
index 366d93af051d..1e149ec006a4 100644
962ea4f
--- a/drivers/pci/pci-sysfs.c
962ea4f
+++ b/drivers/pci/pci-sysfs.c
8cf0063
@@ -903,6 +903,9 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj,
962ea4f
 	loff_t init_off = off;
962ea4f
 	u8 *data = (u8 *) buf;
135abd0
59566d9
+	if (kernel_is_locked_down("Direct PCI access"))
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	if (off > dev->cfg_size)
962ea4f
 		return 0;
962ea4f
 	if (off + count > dev->cfg_size) {
8cf0063
@@ -1165,6 +1168,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];
135abd0
59566d9
+	if (kernel_is_locked_down("Direct PCI access"))
962ea4f
+		return -EPERM;
962ea4f
+
bd32781
 	if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
bd32781
 		return -EINVAL;
135abd0
8cf0063
@@ -1240,6 +1246,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
 {
59566d9
+	if (kernel_is_locked_down("Direct PCI access"))
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	return pci_resource_io(filp, kobj, attr, buf, off, count, true);
962ea4f
 }
135abd0
962ea4f
diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c
8cf0063
index 1ee8927a0635..469445a9019b 100644
962ea4f
--- a/drivers/pci/proc.c
962ea4f
+++ b/drivers/pci/proc.c
f20e0a3
@@ -117,6 +117,9 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
962ea4f
 	int size = dev->cfg_size;
962ea4f
 	int cnt;
135abd0
59566d9
+	if (kernel_is_locked_down("Direct PCI access"))
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	if (pos >= size)
962ea4f
 		return 0;
962ea4f
 	if (nbytes >= size)
f20e0a3
@@ -196,6 +199,9 @@ static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,
962ea4f
 #endif /* HAVE_PCI_MMAP */
962ea4f
 	int ret = 0;
135abd0
59566d9
+	if (kernel_is_locked_down("Direct PCI access"))
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	switch (cmd) {
962ea4f
 	case PCIIOC_CONTROLLER:
962ea4f
 		ret = pci_domain_nr(dev->bus);
f20e0a3
@@ -237,7 +243,8 @@ 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;
135abd0
962ea4f
-	if (!capable(CAP_SYS_RAWIO))
59566d9
+	if (!capable(CAP_SYS_RAWIO) ||
59566d9
+	    kernel_is_locked_down("Direct PCI access"))
962ea4f
 		return -EPERM;
135abd0
bd32781
 	if (fpriv->mmap_state == pci_mmap_io) {
962ea4f
diff --git a/drivers/pci/syscall.c b/drivers/pci/syscall.c
8cf0063
index d96626c614f5..b8a08d3166a1 100644
962ea4f
--- a/drivers/pci/syscall.c
962ea4f
+++ b/drivers/pci/syscall.c
8cf0063
@@ -90,7 +90,8 @@ SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
962ea4f
 	u32 dword;
962ea4f
 	int err = 0;
135abd0
962ea4f
-	if (!capable(CAP_SYS_ADMIN))
59566d9
+	if (!capable(CAP_SYS_ADMIN) ||
59566d9
+	    kernel_is_locked_down("Direct PCI access"))
962ea4f
 		return -EPERM;
962ea4f
bf681f6
 	dev = pci_get_domain_bus_and_slot(0, bus, dfn);
135abd0
-- 
f20e0a3
2.14.3
59566d9
8cf0063
From 677537cdec42804f1936b57ffaa6181f633bc015 Mon Sep 17 00:00:00 2001
f20e0a3
From: Matthew Garrett <mjg59@srcf.ucam.org>
8cf0063
Date: Mon, 9 Apr 2018 09:52:48 +0100
8cf0063
Subject: [PATCH 10/24] x86: Lock down IO port access when the kernel is locked
135abd0
 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
f20e0a3
Signed-off-by: Matthew Garrett <mjg59@srcf.ucam.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
59566d9
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
cc: x86@kernel.org
962ea4f
---
135abd0
 arch/x86/kernel/ioport.c | 6 ++++--
006f5ba
 1 file changed, 4 insertions(+), 2 deletions(-)
962ea4f
962ea4f
diff --git a/arch/x86/kernel/ioport.c b/arch/x86/kernel/ioport.c
8cf0063
index 0fe1c8782208..abc702a6ae9c 100644
962ea4f
--- a/arch/x86/kernel/ioport.c
962ea4f
+++ b/arch/x86/kernel/ioport.c
8cf0063
@@ -31,7 +31,8 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
135abd0
962ea4f
 	if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
962ea4f
 		return -EINVAL;
962ea4f
-	if (turn_on && !capable(CAP_SYS_RAWIO))
59566d9
+	if (turn_on && (!capable(CAP_SYS_RAWIO) ||
59566d9
+			kernel_is_locked_down("ioperm")))
962ea4f
 		return -EPERM;
135abd0
962ea4f
 	/*
8cf0063
@@ -126,7 +127,8 @@ 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))
59566d9
+		if (!capable(CAP_SYS_RAWIO) ||
59566d9
+		    kernel_is_locked_down("iopl"))
962ea4f
 			return -EPERM;
962ea4f
 	}
962ea4f
 	regs->flags = (regs->flags & ~X86_EFLAGS_IOPL) |
135abd0
-- 
f20e0a3
2.14.3
59566d9
8cf0063
From f005be07fababf8c698a556fe465871ad168c9d9 Mon Sep 17 00:00:00 2001
f20e0a3
From: Matthew Garrett <mjg59@srcf.ucam.org>
8cf0063
Date: Mon, 9 Apr 2018 09:52:48 +0100
8cf0063
Subject: [PATCH 11/24] x86/msr: Restrict MSR access when the kernel is locked
135abd0
 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
006f5ba
MSR accesses are logged for the purposes of building up a whitelist as per
006f5ba
Alan Cox's suggestion.
006f5ba
f20e0a3
Signed-off-by: Matthew Garrett <mjg59@srcf.ucam.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
59566d9
Acked-by: Kees Cook <keescook@chromium.org>
59566d9
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
cc: x86@kernel.org
962ea4f
---
006f5ba
 arch/x86/kernel/msr.c | 10 ++++++++++
006f5ba
 1 file changed, 10 insertions(+)
962ea4f
962ea4f
diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c
006f5ba
index ef688804f80d..dfb61d358196 100644
962ea4f
--- a/arch/x86/kernel/msr.c
962ea4f
+++ b/arch/x86/kernel/msr.c
006f5ba
@@ -84,6 +84,11 @@ static ssize_t msr_write(struct file *file, const char __user *buf,
962ea4f
 	int err = 0;
962ea4f
 	ssize_t bytes = 0;
135abd0
006f5ba
+	if (kernel_is_locked_down("Direct MSR access")) {
006f5ba
+		pr_info("Direct access to MSR %x\n", reg);
962ea4f
+		return -EPERM;
006f5ba
+	}
962ea4f
+
962ea4f
 	if (count % 8)
962ea4f
 		return -EINVAL;	/* Invalid chunk size */
135abd0
006f5ba
@@ -135,6 +140,11 @@ static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
006f5ba
 			err = -EFAULT;
962ea4f
 			break;
962ea4f
 		}
59566d9
+		if (kernel_is_locked_down("Direct MSR access")) {
006f5ba
+			pr_info("Direct access to MSR %x\n", regs[1]); /* Display %ecx */
962ea4f
+			err = -EPERM;
962ea4f
+			break;
962ea4f
+		}
006f5ba
 		err = wrmsr_safe_regs_on_cpu(cpu, regs);
006f5ba
 		if (err)
962ea4f
 			break;
135abd0
-- 
f20e0a3
2.14.3
135abd0
8cf0063
From 0a48b7c936757dda851ab2d3ecde7f6a79de7a5b Mon Sep 17 00:00:00 2001
f20e0a3
From: Matthew Garrett <mjg59@srcf.ucam.org>
8cf0063
Date: Mon, 9 Apr 2018 09:52:48 +0100
8cf0063
Subject: [PATCH 12/24] ACPI: Limit access to custom_method when the kernel is
135abd0
 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
f20e0a3
Signed-off-by: Matthew Garrett <mjg59@srcf.ucam.org>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
cc: linux-acpi@vger.kernel.org
962ea4f
---
135abd0
 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
8cf0063
index e967c1173ba3..a07fbe999eb6 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;
135abd0
59566d9
+	if (kernel_is_locked_down("ACPI custom methods"))
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))
135abd0
-- 
f20e0a3
2.14.3
962ea4f
8cf0063
From 2ed74b084366d7dba7b4a611ba13d99b82c4e11e Mon Sep 17 00:00:00 2001
962ea4f
From: Josh Boyer <jwboyer@redhat.com>
8cf0063
Date: Mon, 9 Apr 2018 09:52:49 +0100
8cf0063
Subject: [PATCH 13/24] acpi: Ignore acpi_rsdp kernel param when the kernel has
135abd0
 been locked down
962ea4f
962ea4f
This option allows userspace to pass the RSDP address to the kernel, which
59566d9
makes it possible for a user to modify the workings of hardware .  Reject
59566d9
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>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
cc: Dave Young <dyoung@redhat.com>
59566d9
cc: linux-acpi@vger.kernel.org
962ea4f
---
135abd0
 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
8cf0063
index 7ca41bf023c9..34e4ce7939f4 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)
71c4e80
 	acpi_physical_address pa;
135abd0
962ea4f
 #ifdef CONFIG_KEXEC
962ea4f
-	if (acpi_rsdp)
59566d9
+	if (acpi_rsdp && !kernel_is_locked_down("ACPI RSDP specification"))
962ea4f
 		return acpi_rsdp;
962ea4f
 #endif
71c4e80
 	pa = acpi_arch_get_root_pointer();
135abd0
-- 
f20e0a3
2.14.3
59566d9
8cf0063
From 7fb2ddf683c23cc4b227d7d75a5d039970ca910e Mon Sep 17 00:00:00 2001
962ea4f
From: Linn Crosetto <linn@hpe.com>
8cf0063
Date: Mon, 9 Apr 2018 09:52:49 +0100
8cf0063
Subject: [PATCH 14/24] acpi: Disable ACPI table override if the kernel is
135abd0
 locked down
962ea4f
135abd0
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>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
cc: linux-acpi@vger.kernel.org
962ea4f
---
135abd0
 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
8cf0063
index 849c4fb19b03..6c5ee7e66842 100644
962ea4f
--- a/drivers/acpi/tables.c
962ea4f
+++ b/drivers/acpi/tables.c
f20e0a3
@@ -527,6 +527,11 @@ void __init acpi_table_upgrade(void)
962ea4f
 	if (table_nr == 0)
962ea4f
 		return;
135abd0
59566d9
+	if (kernel_is_locked_down("ACPI table override")) {
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);
135abd0
-- 
f20e0a3
2.14.3
962ea4f
8cf0063
From d1ff6505c76cec9438217f2c284f024a1ac2ac59 Mon Sep 17 00:00:00 2001
962ea4f
From: Linn Crosetto <linn@hpe.com>
8cf0063
Date: Mon, 9 Apr 2018 09:52:50 +0100
8cf0063
Subject: [PATCH 15/24] acpi: Disable APEI error injection if the kernel is
135abd0
 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>
135abd0
Reviewed-by: "Lee, Chun-Yi" <jlee@suse.com>
59566d9
cc: linux-acpi@vger.kernel.org
962ea4f
---
135abd0
 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
59566d9
index b38737c83a24..6d71e1e97b20 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;
135abd0
59566d9
+	if (kernel_is_locked_down("ACPI error injection"))
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)))
135abd0
-- 
f20e0a3
2.14.3
962ea4f
8cf0063
From 3153be0328e3a752aacab95d503fbd460f517402 Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Wed, 4 Apr 2018 14:45:37 +0100
8cf0063
Subject: [PATCH 16/24] Prohibit PCMCIA CIS storage when the kernel is locked
135abd0
 down
962ea4f
962ea4f
Prohibit replacement of the PCMCIA Card Information Structure when the
962ea4f
kernel is locked down.
962ea4f
59566d9
Suggested-by: Dominik Brodowski <linux@dominikbrodowski.net>
962ea4f
Signed-off-by: David Howells <dhowells@redhat.com>
59566d9
cc: linux-pcmcia@lists.infradead.org
962ea4f
---
135abd0
 drivers/pcmcia/cistpl.c | 3 +++
59566d9
 1 file changed, 3 insertions(+)
962ea4f
962ea4f
diff --git a/drivers/pcmcia/cistpl.c b/drivers/pcmcia/cistpl.c
f20e0a3
index 102646fedb56..e46c948d7246 100644
962ea4f
--- a/drivers/pcmcia/cistpl.c
962ea4f
+++ b/drivers/pcmcia/cistpl.c
59566d9
@@ -1578,6 +1578,9 @@ static ssize_t pccard_store_cis(struct file *filp, struct kobject *kobj,
962ea4f
 	struct pcmcia_socket *s;
962ea4f
 	int error;
135abd0
59566d9
+	if (kernel_is_locked_down("Direct PCMCIA CIS storage"))
962ea4f
+		return -EPERM;
962ea4f
+
962ea4f
 	s = to_socket(container_of(kobj, struct device, kobj));
135abd0
962ea4f
 	if (off)
135abd0
-- 
f20e0a3
2.14.3
962ea4f
8cf0063
From 9fedc1427e8589edf2e16a481f8588711adba69a Mon Sep 17 00:00:00 2001
962ea4f
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Wed, 4 Apr 2018 14:45:37 +0100
8cf0063
Subject: [PATCH 17/24] 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>
59566d9
cc: Jiri Slaby <jslaby@suse.com>
962ea4f
---
135abd0
 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
8cf0063
index 0466f9f08a91..360f8e4416c4 100644
962ea4f
--- a/drivers/tty/serial/serial_core.c
962ea4f
+++ b/drivers/tty/serial/serial_core.c
f20e0a3
@@ -829,6 +829,12 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
7c0c57c
 	new_flags = (__force upf_t)new_info->flags;
962ea4f
 	old_custom_divisor = uport->custom_divisor;
135abd0
59566d9
+	if ((change_port || change_irq) &&
59566d9
+	    kernel_is_locked_down("Using TIOCSSERIAL to change device addresses, irqs and dma channels")) {
962ea4f
+		retval = -EPERM;
962ea4f
+		goto exit;
962ea4f
+	}
962ea4f
+
962ea4f
 	if (!capable(CAP_SYS_ADMIN)) {
962ea4f
 		retval = -EPERM;
962ea4f
 		if (change_irq || change_port ||
135abd0
-- 
f20e0a3
2.14.3
c796f87
8cf0063
From f8fd52e2b077ce5a993807f8fc6e27a17cf4d19f Mon Sep 17 00:00:00 2001
59566d9
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Wed, 4 Apr 2018 14:45:37 +0100
8cf0063
Subject: [PATCH 18/24] Lock down module params that specify hardware
135abd0
 parameters (eg. ioport)
59566d9
59566d9
Provided an annotation for module parameters that specify hardware
59566d9
parameters (such as io ports, iomem addresses, irqs, dma channels, fixed
59566d9
dma buffers and other types).
59566d9
59566d9
Suggested-by: Alan Cox <gnomes@lxorguk.ukuu.org.uk>
59566d9
Signed-off-by: David Howells <dhowells@redhat.com>
59566d9
---
135abd0
 kernel/params.c | 26 +++++++++++++++++++++-----
59566d9
 1 file changed, 21 insertions(+), 5 deletions(-)
59566d9
59566d9
diff --git a/kernel/params.c b/kernel/params.c
f20e0a3
index cc9108c2a1fd..2c08c4aa376b 100644
59566d9
--- a/kernel/params.c
59566d9
+++ b/kernel/params.c
59566d9
@@ -108,13 +108,19 @@ bool parameq(const char *a, const char *b)
59566d9
 	return parameqn(a, b, strlen(a)+1);
59566d9
 }
135abd0
59566d9
-static void param_check_unsafe(const struct kernel_param *kp)
59566d9
+static bool param_check_unsafe(const struct kernel_param *kp,
59566d9
+			       const char *doing)
59566d9
 {
59566d9
 	if (kp->flags & KERNEL_PARAM_FL_UNSAFE) {
df0ed2a
 		pr_notice("Setting dangerous option %s - tainting kernel\n",
df0ed2a
 			  kp->name);
59566d9
 		add_taint(TAINT_USER, LOCKDEP_STILL_OK);
59566d9
 	}
59566d9
+
59566d9
+	if (kp->flags & KERNEL_PARAM_FL_HWPARAM &&
59566d9
+	    kernel_is_locked_down("Command line-specified device addresses, irqs and dma channels"))
59566d9
+		return false;
59566d9
+	return true;
59566d9
 }
135abd0
59566d9
 static int parse_one(char *param,
59566d9
@@ -144,8 +150,10 @@ static int parse_one(char *param,
59566d9
 			pr_debug("handling %s with %p\n", param,
59566d9
 				params[i].ops->set);
59566d9
 			kernel_param_lock(params[i].mod);
59566d9
-			param_check_unsafe(&params[i]);
59566d9
-			err = params[i].ops->set(val, &params[i]);
59566d9
+			if (param_check_unsafe(&params[i], doing))
59566d9
+				err = params[i].ops->set(val, &params[i]);
59566d9
+			else
59566d9
+				err = -EPERM;
59566d9
 			kernel_param_unlock(params[i].mod);
59566d9
 			return err;
59566d9
 		}
f20e0a3
@@ -553,6 +561,12 @@ static ssize_t param_attr_show(struct module_attribute *mattr,
59566d9
 	return count;
59566d9
 }
135abd0
59566d9
+#ifdef CONFIG_MODULES
59566d9
+#define mod_name(mod) (mod)->name
59566d9
+#else
59566d9
+#define mod_name(mod) "unknown"
59566d9
+#endif
59566d9
+
59566d9
 /* sysfs always hands a nul-terminated string in buf.  We rely on that. */
59566d9
 static ssize_t param_attr_store(struct module_attribute *mattr,
59566d9
 				struct module_kobject *mk,
f20e0a3
@@ -565,8 +579,10 @@ static ssize_t param_attr_store(struct module_attribute *mattr,
59566d9
 		return -EPERM;
135abd0
59566d9
 	kernel_param_lock(mk->mod);
59566d9
-	param_check_unsafe(attribute->param);
59566d9
-	err = attribute->param->ops->set(buf, attribute->param);
59566d9
+	if (param_check_unsafe(attribute->param, mod_name(mk->mod)))
59566d9
+		err = attribute->param->ops->set(buf, attribute->param);
59566d9
+	else
59566d9
+		err = -EPERM;
59566d9
 	kernel_param_unlock(mk->mod);
59566d9
 	if (!err)
59566d9
 		return len;
135abd0
-- 
f20e0a3
2.14.3
59566d9
8cf0063
From 9c88e2ab392f5ac9c80529e43175fe65d00cdb67 Mon Sep 17 00:00:00 2001
59566d9
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Wed, 4 Apr 2018 14:45:38 +0100
8cf0063
Subject: [PATCH 19/24] x86/mmiotrace: Lock down the testmmiotrace module
59566d9
59566d9
The testmmiotrace module shouldn't be permitted when the kernel is locked
59566d9
down as it can be used to arbitrarily read and write MMIO space.
59566d9
59566d9
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
59566d9
Signed-off-by: David Howells 
59566d9
cc: Thomas Gleixner <tglx@linutronix.de>
59566d9
cc: Steven Rostedt <rostedt@goodmis.org>
59566d9
cc: Ingo Molnar <mingo@kernel.org>
59566d9
cc: "H. Peter Anvin" <hpa@zytor.com>
59566d9
cc: x86@kernel.org
59566d9
---
135abd0
 arch/x86/mm/testmmiotrace.c | 3 +++
59566d9
 1 file changed, 3 insertions(+)
59566d9
59566d9
diff --git a/arch/x86/mm/testmmiotrace.c b/arch/x86/mm/testmmiotrace.c
59566d9
index f6ae6830b341..bbaad357f5d7 100644
59566d9
--- a/arch/x86/mm/testmmiotrace.c
59566d9
+++ b/arch/x86/mm/testmmiotrace.c
59566d9
@@ -115,6 +115,9 @@ static int __init init(void)
59566d9
 {
59566d9
 	unsigned long size = (read_far) ? (8 << 20) : (16 << 10);
135abd0
59566d9
+	if (kernel_is_locked_down("MMIO trace testing"))
59566d9
+		return -EPERM;
59566d9
+
59566d9
 	if (mmio_address == 0) {
59566d9
 		pr_err("you have to use the module argument mmio_address.\n");
59566d9
 		pr_err("DO NOT LOAD THIS MODULE UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!\n");
135abd0
-- 
f20e0a3
2.14.3
f20e0a3
8cf0063
From 256e20401f9f5dd19028d4220095897a15daa67c Mon Sep 17 00:00:00 2001
f20e0a3
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Wed, 4 Apr 2018 14:45:38 +0100
8cf0063
Subject: [PATCH 20/24] Lock down /proc/kcore
f20e0a3
f20e0a3
Disallow access to /proc/kcore when the kernel is locked down to prevent
f20e0a3
access to cryptographic data.
f20e0a3
f20e0a3
Signed-off-by: David Howells <dhowells@redhat.com>
f20e0a3
Reviewed-by: James Morris <james.l.morris@oracle.com>
f20e0a3
---
f20e0a3
 fs/proc/kcore.c | 2 ++
f20e0a3
 1 file changed, 2 insertions(+)
f20e0a3
f20e0a3
diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c
f20e0a3
index d1e82761de81..cdebdee81719 100644
f20e0a3
--- a/fs/proc/kcore.c
f20e0a3
+++ b/fs/proc/kcore.c
f20e0a3
@@ -546,6 +546,8 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
f20e0a3
f20e0a3
 static int open_kcore(struct inode *inode, struct file *filp)
f20e0a3
 {
f20e0a3
+	if (kernel_is_locked_down("/proc/kcore"))
f20e0a3
+		return -EPERM;
f20e0a3
 	if (!capable(CAP_SYS_RAWIO))
f20e0a3
 		return -EPERM;
f20e0a3
f20e0a3
-- 
f20e0a3
2.14.3
f20e0a3
8cf0063
From f68ca24bc8d8a64cf30e59a595fad0e6782e933f Mon Sep 17 00:00:00 2001
f20e0a3
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Wed, 4 Apr 2018 14:45:38 +0100
8cf0063
Subject: [PATCH 21/24] Lock down kprobes
f20e0a3
f20e0a3
Disallow the creation of kprobes when the kernel is locked down by
f20e0a3
preventing their registration.  This prevents kprobes from being used to
f20e0a3
access kernel memory, either to make modifications or to steal crypto data.
f20e0a3
f20e0a3
Reported-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
f20e0a3
Signed-off-by: David Howells <dhowells@redhat.com>
f20e0a3
---
f20e0a3
 kernel/kprobes.c | 3 +++
f20e0a3
 1 file changed, 3 insertions(+)
f20e0a3
f20e0a3
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
f20e0a3
index 102160ff5c66..4f5757732553 100644
f20e0a3
--- a/kernel/kprobes.c
f20e0a3
+++ b/kernel/kprobes.c
f20e0a3
@@ -1561,6 +1561,9 @@ int register_kprobe(struct kprobe *p)
f20e0a3
 	struct module *probed_mod;
f20e0a3
 	kprobe_opcode_t *addr;
f20e0a3
f20e0a3
+	if (kernel_is_locked_down("Use of kprobes"))
f20e0a3
+		return -EPERM;
f20e0a3
+
f20e0a3
 	/* Adjust probe address from symbol */
f20e0a3
 	addr = kprobe_addr(p);
f20e0a3
 	if (IS_ERR(addr))
f20e0a3
-- 
f20e0a3
2.14.3
f20e0a3
8cf0063
From d44a6ae3a7cad5cd9b01f7b0a48b3c788af968e8 Mon Sep 17 00:00:00 2001
f20e0a3
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Wed, 4 Apr 2018 14:45:38 +0100
8cf0063
Subject: [PATCH 23/24] Lock down perf
f20e0a3
f20e0a3
Disallow the use of certain perf facilities that might allow userspace to
f20e0a3
access kernel data.
f20e0a3
f20e0a3
Signed-off-by: David Howells <dhowells@redhat.com>
f20e0a3
---
f20e0a3
 kernel/events/core.c | 5 +++++
f20e0a3
 1 file changed, 5 insertions(+)
f20e0a3
f20e0a3
diff --git a/kernel/events/core.c b/kernel/events/core.c
8cf0063
index fc1c330c6bd6..1922f2e0980a 100644
f20e0a3
--- a/kernel/events/core.c
f20e0a3
+++ b/kernel/events/core.c
8cf0063
@@ -10407,6 +10407,11 @@ SYSCALL_DEFINE5(perf_event_open,
f20e0a3
 			return -EINVAL;
f20e0a3
 	}
f20e0a3
f20e0a3
+	if ((attr.sample_type & PERF_SAMPLE_REGS_INTR) &&
f20e0a3
+	    kernel_is_locked_down("PERF_SAMPLE_REGS_INTR"))
f20e0a3
+		/* REGS_INTR can leak data, lockdown must prevent this */
f20e0a3
+		return -EPERM;
f20e0a3
+
f20e0a3
 	/* Only privileged users can get physical addresses */
f20e0a3
 	if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) &&
f20e0a3
 	    perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
f20e0a3
-- 
f20e0a3
2.14.3
59566d9
8cf0063
From fe5091f97838c8c64b891280bcd30367e71cd5c3 Mon Sep 17 00:00:00 2001
59566d9
From: David Howells <dhowells@redhat.com>
8cf0063
Date: Wed, 4 Apr 2018 14:45:38 +0100
8cf0063
Subject: [PATCH 24/24] debugfs: Restrict debugfs when the kernel is locked
f20e0a3
 down
f20e0a3
f20e0a3
Disallow opening of debugfs files that might be used to muck around when
f20e0a3
the kernel is locked down as various drivers give raw access to hardware
f20e0a3
through debugfs.  Given the effort of auditing all 2000 or so files and
f20e0a3
manually fixing each one as necessary, I've chosen to apply a heuristic
f20e0a3
instead.  The following changes are made:
f20e0a3
f20e0a3
 (1) chmod and chown are disallowed on debugfs objects (though the root dir
f20e0a3
     can be modified by mount and remount, but I'm not worried about that).
59566d9
f20e0a3
 (2) When the kernel is locked down, only files with the following criteria
f20e0a3
     are permitted to be opened:
59566d9
f20e0a3
	- The file must have mode 00444
f20e0a3
	- The file must not have ioctl methods
f20e0a3
	- The file must not have mmap
59566d9
f20e0a3
 (3) When the kernel is locked down, files may only be opened for reading.
f20e0a3
f20e0a3
Normal device interaction should be done through configfs, sysfs or a
f20e0a3
miscdev, not debugfs.
59566d9
59566d9
Note that this makes it unnecessary to specifically lock down show_dsts(),
59566d9
show_devs() and show_call() in the asus-wmi driver.
59566d9
f20e0a3
I would actually prefer to lock down all files by default and have the
f20e0a3
the files unlocked by the creator.  This is tricky to manage correctly,
f20e0a3
though, as there are 19 creation functions and ~1600 call sites (some of
f20e0a3
them in loops scanning tables).
f20e0a3
59566d9
Signed-off-by: David Howells <dhowells@redhat.com>
59566d9
cc: Andy Shevchenko <andy.shevchenko@gmail.com>
59566d9
cc: acpi4asus-user@lists.sourceforge.net
59566d9
cc: platform-driver-x86@vger.kernel.org
f20e0a3
cc: Matthew Garrett <mjg59@srcf.ucam.org>
59566d9
cc: Thomas Gleixner <tglx@linutronix.de>
59566d9
---
f20e0a3
 fs/debugfs/file.c  | 28 ++++++++++++++++++++++++++++
f20e0a3
 fs/debugfs/inode.c | 30 ++++++++++++++++++++++++++++--
f20e0a3
 2 files changed, 56 insertions(+), 2 deletions(-)
59566d9
59566d9
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
f20e0a3
index 1f99678ff5d3..51cb894c21f2 100644
59566d9
--- a/fs/debugfs/file.c
59566d9
+++ b/fs/debugfs/file.c
f20e0a3
@@ -136,6 +136,25 @@ void debugfs_file_put(struct dentry *dentry)
f20e0a3
 }
f20e0a3
 EXPORT_SYMBOL_GPL(debugfs_file_put);
f20e0a3
f20e0a3
+/*
f20e0a3
+ * Only permit access to world-readable files when the kernel is locked down.
f20e0a3
+ * We also need to exclude any file that has ways to write or alter it as root
f20e0a3
+ * can bypass the permissions check.
f20e0a3
+ */
f20e0a3
+static bool debugfs_is_locked_down(struct inode *inode,
f20e0a3
+				   struct file *filp,
f20e0a3
+				   const struct file_operations *real_fops)
f20e0a3
+{
f20e0a3
+	if ((inode->i_mode & 07777) == 0444 &&
f20e0a3
+	    !(filp->f_mode & FMODE_WRITE) &&
f20e0a3
+	    !real_fops->unlocked_ioctl &&
f20e0a3
+	    !real_fops->compat_ioctl &&
f20e0a3
+	    !real_fops->mmap)
f20e0a3
+		return false;
59566d9
+
f20e0a3
+	return kernel_is_locked_down("debugfs");
f20e0a3
+}
8221dd3
+
f20e0a3
 static int open_proxy_open(struct inode *inode, struct file *filp)
f20e0a3
 {
f20e0a3
 	struct dentry *dentry = F_DENTRY(filp);
f20e0a3
@@ -147,6 +166,11 @@ static int open_proxy_open(struct inode *inode, struct file *filp)
8221dd3
 		return r == -EIO ? -ENOENT : r;
f20e0a3
f20e0a3
 	real_fops = debugfs_real_fops(filp);
f20e0a3
+
f20e0a3
+	r = -EPERM;
f20e0a3
+	if (debugfs_is_locked_down(inode, filp, real_fops))
f20e0a3
+		goto out;
59566d9
+
f20e0a3
 	real_fops = fops_get(real_fops);
f20e0a3
 	if (!real_fops) {
f20e0a3
 		/* Huh? Module did not clean up after itself at exit? */
f20e0a3
@@ -272,6 +296,10 @@ static int full_proxy_open(struct inode *inode, struct file *filp)
8221dd3
 		return r == -EIO ? -ENOENT : r;
59566d9
f20e0a3
 	real_fops = debugfs_real_fops(filp);
f20e0a3
+	r = -EPERM;
f20e0a3
+	if (debugfs_is_locked_down(inode, filp, real_fops))
f20e0a3
+		goto out;
f20e0a3
+
f20e0a3
 	real_fops = fops_get(real_fops);
f20e0a3
 	if (!real_fops) {
f20e0a3
 		/* Huh? Module did not cleanup after itself at exit? */
f20e0a3
diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
8cf0063
index 13b01351dd1c..4daec17b8215 100644
f20e0a3
--- a/fs/debugfs/inode.c
f20e0a3
+++ b/fs/debugfs/inode.c
f20e0a3
@@ -32,6 +32,31 @@ static struct vfsmount *debugfs_mount;
f20e0a3
 static int debugfs_mount_count;
f20e0a3
 static bool debugfs_registered;
135abd0
f20e0a3
+/*
f20e0a3
+ * Don't allow access attributes to be changed whilst the kernel is locked down
f20e0a3
+ * so that we can use the file mode as part of a heuristic to determine whether
f20e0a3
+ * to lock down individual files.
f20e0a3
+ */
f20e0a3
+static int debugfs_setattr(struct dentry *dentry, struct iattr *ia)
f20e0a3
+{
f20e0a3
+	if ((ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) &&
f20e0a3
+	    kernel_is_locked_down("debugfs"))
59566d9
+		return -EPERM;
f20e0a3
+	return simple_setattr(dentry, ia);
f20e0a3
+}
f20e0a3
+
f20e0a3
+static const struct inode_operations debugfs_file_inode_operations = {
f20e0a3
+	.setattr	= debugfs_setattr,
f20e0a3
+};
f20e0a3
+static const struct inode_operations debugfs_dir_inode_operations = {
f20e0a3
+	.lookup		= simple_lookup,
f20e0a3
+	.setattr	= debugfs_setattr,
f20e0a3
+};
f20e0a3
+static const struct inode_operations debugfs_symlink_inode_operations = {
f20e0a3
+	.get_link	= simple_get_link,
f20e0a3
+	.setattr	= debugfs_setattr,
f20e0a3
+};
f20e0a3
+
f20e0a3
 static struct inode *debugfs_get_inode(struct super_block *sb)
f20e0a3
 {
f20e0a3
 	struct inode *inode = new_inode(sb);
8cf0063
@@ -356,6 +381,7 @@ static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
f20e0a3
 	inode->i_mode = mode;
f20e0a3
 	inode->i_private = data;
f20e0a3
f20e0a3
+	inode->i_op = &debugfs_file_inode_operations;
f20e0a3
 	inode->i_fop = proxy_fops;
f20e0a3
 	dentry->d_fsdata = (void *)((unsigned long)real_fops |
f20e0a3
 				DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
037431c
@@ -515,7 +541,7 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
5f1fb0c
 		return failed_creating(dentry);
5f1fb0c
 
5f1fb0c
 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
f20e0a3
-	inode->i_op = &simple_dir_inode_operations;
f20e0a3
+	inode->i_op = &debugfs_dir_inode_operations;
f20e0a3
 	inode->i_fop = &simple_dir_operations;
f20e0a3
f20e0a3
 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
8cf0063
@@ -608,7 +634,7 @@ struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
f20e0a3
 		return failed_creating(dentry);
f20e0a3
 	}
f20e0a3
 	inode->i_mode = S_IFLNK | S_IRWXUGO;
f20e0a3
-	inode->i_op = &simple_symlink_inode_operations;
f20e0a3
+	inode->i_op = &debugfs_symlink_inode_operations;
f20e0a3
 	inode->i_link = link;
f20e0a3
 	d_instantiate(dentry, inode);
f20e0a3
 	return end_creating(dentry);
135abd0
-- 
f20e0a3
2.14.3
135abd0
78c9ba6
From patchwork Wed Nov 21 12:05:10 2018
78c9ba6
Content-Type: text/plain; charset="utf-8"
78c9ba6
MIME-Version: 1.0
78c9ba6
Content-Transfer-Encoding: 7bit
78c9ba6
X-Patchwork-Submitter: Vasily Gorbik <gor@linux.ibm.com>
78c9ba6
X-Patchwork-Id: 1015495
78c9ba6
Return-Path: <SRS0=ejdu=OA=vger.kernel.org=linux-kernel-owner@kernel.org>
78c9ba6
Received: from mail.kernel.org (mail.kernel.org [198.145.29.99])
78c9ba6
	by smtp.lore.kernel.org (Postfix) with ESMTP id AF80FC04EBA
78c9ba6
	for <linux-kernel@archiver.kernel.org>; Wed, 21 Nov 2018 12:05:25 +0000 (UTC)
78c9ba6
Received: from vger.kernel.org (vger.kernel.org [209.132.180.67])
78c9ba6
	by mail.kernel.org (Postfix) with ESMTP id 80EA921479
78c9ba6
	for <linux-kernel@archiver.kernel.org>; Wed, 21 Nov 2018 12:05:25 +0000 (UTC)
78c9ba6
DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 80EA921479
78c9ba6
Authentication-Results: mail.kernel.org;
78c9ba6
 dmarc=fail (p=none dis=none) header.from=linux.ibm.com
78c9ba6
Authentication-Results: mail.kernel.org;
78c9ba6
 spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org
78c9ba6
Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand
78c9ba6
        id S1730155AbeKUWjb (ORCPT
78c9ba6
        <rfc822;linux-kernel@archiver.kernel.org>);
78c9ba6
        Wed, 21 Nov 2018 17:39:31 -0500
78c9ba6
Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:33574 "EHLO
78c9ba6
        mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL)
78c9ba6
        by vger.kernel.org with ESMTP id S1729128AbeKUWjb (ORCPT
78c9ba6
        <rfc822;linux-kernel@vger.kernel.org>);
78c9ba6
        Wed, 21 Nov 2018 17:39:31 -0500
78c9ba6
Received: from pps.filterd (m0098420.ppops.net [127.0.0.1])
78c9ba6
        by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id
78c9ba6
 wALBx6kw056071
78c9ba6
        for <linux-kernel@vger.kernel.org>; Wed, 21 Nov 2018 07:05:22 -0500
78c9ba6
Received: from e06smtp02.uk.ibm.com (e06smtp02.uk.ibm.com [195.75.94.98])
78c9ba6
        by mx0b-001b2d01.pphosted.com with ESMTP id 2nw5p847fp-1
78c9ba6
        (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT)
78c9ba6
        for <linux-kernel@vger.kernel.org>; Wed, 21 Nov 2018 07:05:22 -0500
78c9ba6
Received: from localhost
78c9ba6
        by e06smtp02.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use
78c9ba6
 Only! Violators will be prosecuted
78c9ba6
        for <linux-kernel@vger.kernel.org> from <gor@linux.ibm.com>;
78c9ba6
        Wed, 21 Nov 2018 12:05:20 -0000
78c9ba6
Received: from b06cxnps4075.portsmouth.uk.ibm.com (9.149.109.197)
78c9ba6
        by e06smtp02.uk.ibm.com (192.168.101.132) with IBM ESMTP SMTP Gateway:
78c9ba6
 Authorized Use Only! Violators will be prosecuted;
78c9ba6
        (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256)
78c9ba6
        Wed, 21 Nov 2018 12:05:17 -0000
78c9ba6
Received: from b06wcsmtp001.portsmouth.uk.ibm.com
78c9ba6
 (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160])
78c9ba6
        by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with
78c9ba6
 ESMTP id wALC5GXF60817580
78c9ba6
        (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256
78c9ba6
 verify=FAIL);
78c9ba6
        Wed, 21 Nov 2018 12:05:16 GMT
78c9ba6
Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1])
78c9ba6
        by IMSVA (Postfix) with ESMTP id 4EB1CA4060;
78c9ba6
        Wed, 21 Nov 2018 12:05:16 +0000 (GMT)
78c9ba6
Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1])
78c9ba6
        by IMSVA (Postfix) with ESMTP id 0C844A405F;
78c9ba6
        Wed, 21 Nov 2018 12:05:16 +0000 (GMT)
78c9ba6
Received: from localhost (unknown [9.152.212.229])
78c9ba6
        by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTPS;
78c9ba6
        Wed, 21 Nov 2018 12:05:15 +0000 (GMT)
78c9ba6
Date: Wed, 21 Nov 2018 13:05:10 +0100
78c9ba6
From: Vasily Gorbik <gor@linux.ibm.com>
78c9ba6
To: David Howells <dhowells@redhat.com>,
78c9ba6
        James Morris <jmorris@namei.org>
78c9ba6
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>,
78c9ba6
        Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
78c9ba6
        linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org
78c9ba6
Subject: [PATCH next-lockdown 1/1] debugfs: avoid EPERM when no open file
78c9ba6
 operation defined
78c9ba6
References: <4136.1522452584@warthog.procyon.org.uk>
78c9ba6
 <cover.thread-bfac1b.your-ad-here.call-01542799656-ext-6093@work.hours>
78c9ba6
MIME-Version: 1.0
78c9ba6
Content-Type: text/plain; charset=utf-8
78c9ba6
Content-Disposition: inline
78c9ba6
In-Reply-To: 
78c9ba6
 <cover.thread-bfac1b.your-ad-here.call-01542799656-ext-6093@work.hours>
78c9ba6
X-TM-AS-GCONF: 00
78c9ba6
x-cbid: 18112112-0008-0000-0000-000002963F3F
78c9ba6
X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused
78c9ba6
x-cbparentid: 18112112-0009-0000-0000-000022006F52
78c9ba6
Message-Id: 
78c9ba6
 <patch-1.thread-bfac1b.git-bfac1b60354c.your-ad-here.call-01542799656-ext-6093@work.hours>
78c9ba6
X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,,
78c9ba6
 definitions=2018-11-21_05:,,
78c9ba6
 signatures=0
78c9ba6
X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0
78c9ba6
 priorityscore=1501
78c9ba6
 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0
78c9ba6
 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0
78c9ba6
 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx
78c9ba6
 scancount=1 engine=8.0.1-1810050000 definitions=main-1811210107
78c9ba6
Sender: linux-kernel-owner@vger.kernel.org
78c9ba6
Precedence: bulk
78c9ba6
List-ID: <linux-kernel.vger.kernel.org>
78c9ba6
X-Mailing-List: linux-kernel@vger.kernel.org
78c9ba6
78c9ba6
With "debugfs: Restrict debugfs when the kernel is locked down"
78c9ba6
return code "r" is unconditionally set to -EPERM, which stays like that
78c9ba6
until function return if no "open" file operation defined, effectivelly
78c9ba6
resulting in "Operation not permitted" for all such files despite kernel
78c9ba6
lock down status or CONFIG_LOCK_DOWN_KERNEL being enabled.
78c9ba6
78c9ba6
In particular this breaks 2 debugfs files on s390:
78c9ba6
/sys/kernel/debug/s390_hypfs/diag_304
78c9ba6
/sys/kernel/debug/s390_hypfs/diag_204
78c9ba6
78c9ba6
To address that set EPERM return code only when debugfs_is_locked_down
78c9ba6
returns true.
78c9ba6
78c9ba6
Fixes: 3fc322605158 ("debugfs: Restrict debugfs when the kernel is locked down")
78c9ba6
Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
78c9ba6
---
78c9ba6
 fs/debugfs/file.c | 10 ++++++----
78c9ba6
 1 file changed, 6 insertions(+), 4 deletions(-)
78c9ba6
78c9ba6
diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
78c9ba6
index 51cb894c21f2..89c86faaa02a 100644
78c9ba6
--- a/fs/debugfs/file.c
78c9ba6
+++ b/fs/debugfs/file.c
78c9ba6
@@ -167,9 +167,10 @@ static int open_proxy_open(struct inode *inode, struct file *filp)
78c9ba6
 
78c9ba6
 	real_fops = debugfs_real_fops(filp);
78c9ba6
 
78c9ba6
-	r = -EPERM;
78c9ba6
-	if (debugfs_is_locked_down(inode, filp, real_fops))
78c9ba6
+	if (debugfs_is_locked_down(inode, filp, real_fops)) {
78c9ba6
+		r = -EPERM;
78c9ba6
 		goto out;
78c9ba6
+	}
78c9ba6
 
78c9ba6
 	real_fops = fops_get(real_fops);
78c9ba6
 	if (!real_fops) {
78c9ba6
@@ -296,9 +297,10 @@ static int full_proxy_open(struct inode *inode, struct file *filp)
78c9ba6
 		return r == -EIO ? -ENOENT : r;
78c9ba6
 
78c9ba6
 	real_fops = debugfs_real_fops(filp);
78c9ba6
-	r = -EPERM;
78c9ba6
-	if (debugfs_is_locked_down(inode, filp, real_fops))
78c9ba6
+	if (debugfs_is_locked_down(inode, filp, real_fops)) {
78c9ba6
+		r = -EPERM;
78c9ba6
 		goto out;
78c9ba6
+	}
78c9ba6
 
78c9ba6
 	real_fops = fops_get(real_fops);
78c9ba6
 	if (!real_fops) {