Josh Boyer c9d9c5
From: Kyle McMartin <kyle@redhat.com>
Josh Boyer c9d9c5
Date: Fri, 30 Aug 2013 09:28:51 -0400
Josh Boyer c9d9c5
Subject: [PATCH] Add sysrq option to disable secure boot mode
Josh Boyer c9d9c5
dbc4a9
Bugzilla: N/A
dbc4a9
Upstream-status: Fedora mustard
Josh Boyer c9d9c5
---
107552
 arch/x86/kernel/setup.c     | 36 ++++++++++++++++++++++++++++++++++++
Josh Boyer c9d9c5
 drivers/input/misc/uinput.c |  1 +
Josh Boyer c9d9c5
 drivers/tty/sysrq.c         | 19 +++++++++++++------
Josh Boyer c9d9c5
 include/linux/input.h       |  5 +++++
Josh Boyer c9d9c5
 include/linux/sysrq.h       |  8 +++++++-
Josh Boyer c9d9c5
 kernel/debug/kdb/kdb_main.c |  2 +-
Josh Boyer c9d9c5
 kernel/module.c             |  4 ++--
107552
 7 files changed, 65 insertions(+), 10 deletions(-)
Josh Boyer c9d9c5
Kyle McMartin 8aeb27
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
dbc4a9
index fb282ff6a802..d291d16ba257 100644
Kyle McMartin 8aeb27
--- a/arch/x86/kernel/setup.c
Kyle McMartin 8aeb27
+++ b/arch/x86/kernel/setup.c
Kyle McMartin 8aeb27
@@ -70,6 +70,11 @@
Kyle McMartin 8aeb27
 #include <linux tboot.h="">
Kyle McMartin 8aeb27
 #include <linux jiffies.h="">
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
+#include <linux fips.h="">
Kyle McMartin 8aeb27
+#include <linux cred.h="">
Kyle McMartin 8aeb27
+#include <linux sysrq.h="">
Kyle McMartin 8aeb27
+#include <linux init_task.h="">
Kyle McMartin 8aeb27
+
Kyle McMartin 8aeb27
 #include <video edid.h="">
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
 #include <asm mtrr.h="">
a7fb62
@@ -1268,6 +1273,37 @@ void __init i386_reserve_resources(void)
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
 #endif /* CONFIG_X86_32 */
107552
 
Josh Boyer c9d9c5
+#ifdef CONFIG_MAGIC_SYSRQ
Kyle McMartin 8aeb27
+#ifdef CONFIG_MODULE_SIG
Kyle McMartin 8aeb27
+extern bool sig_enforce;
Kyle McMartin 8aeb27
+#endif
Kyle McMartin 8aeb27
+
Kyle McMartin 8aeb27
+static void sysrq_handle_secure_boot(int key)
Kyle McMartin 8aeb27
+{
Josh Boyer c9d9c5
+	if (!efi_enabled(EFI_SECURE_BOOT))
Kyle McMartin 8aeb27
+		return;
Kyle McMartin 8aeb27
+
Kyle McMartin 8aeb27
+	pr_info("Secure boot disabled\n");
Kyle McMartin 8aeb27
+#ifdef CONFIG_MODULE_SIG
Kyle McMartin 8aeb27
+	sig_enforce = fips_enabled;
Kyle McMartin 8aeb27
+#endif
Kyle McMartin 8aeb27
+}
Kyle McMartin 8aeb27
+static struct sysrq_key_op secure_boot_sysrq_op = {
Kyle McMartin 8aeb27
+	.handler	=	sysrq_handle_secure_boot,
Kyle McMartin 8aeb27
+	.help_msg	=	"unSB(x)",
Kyle McMartin 8aeb27
+	.action_msg	=	"Disabling Secure Boot restrictions",
Kyle McMartin 8aeb27
+	.enable_mask	=	SYSRQ_DISABLE_USERSPACE,
Kyle McMartin 8aeb27
+};
Kyle McMartin 8aeb27
+static int __init secure_boot_sysrq(void)
Kyle McMartin 8aeb27
+{
Josh Boyer c9d9c5
+	if (efi_enabled(EFI_SECURE_BOOT))
Kyle McMartin 8aeb27
+		register_sysrq_key('x', &secure_boot_sysrq_op);
Kyle McMartin 8aeb27
+	return 0;
Kyle McMartin 8aeb27
+}
Kyle McMartin 8aeb27
+late_initcall(secure_boot_sysrq);
Kyle McMartin 8aeb27
+#endif /*CONFIG_MAGIC_SYSRQ*/
107552
+
107552
+
107552
 static struct notifier_block kernel_offset_notifier = {
107552
 	.notifier_call = dump_kernel_offset
107552
 };
Kyle McMartin 8aeb27
diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c
dbc4a9
index 421e29e4cd81..61c1eb97806c 100644
Kyle McMartin 8aeb27
--- a/drivers/input/misc/uinput.c
Kyle McMartin 8aeb27
+++ b/drivers/input/misc/uinput.c
dbc4a9
@@ -366,6 +366,7 @@ static int uinput_allocate_device(struct uinput_device *udev)
Kyle McMartin 8aeb27
 	if (!udev->dev)
Kyle McMartin 8aeb27
 		return -ENOMEM;
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
+	udev->dev->flags |= INPUTDEV_FLAGS_SYNTHETIC;
Kyle McMartin 8aeb27
 	udev->dev->event = uinput_dev_event;
Kyle McMartin 8aeb27
 	input_set_drvdata(udev->dev, udev);
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
dbc4a9
index 42bad18c66c9..496e073b09d7 100644
Kyle McMartin 8aeb27
--- a/drivers/tty/sysrq.c
Kyle McMartin 8aeb27
+++ b/drivers/tty/sysrq.c
a7fb62
@@ -463,6 +463,7 @@ static struct sysrq_key_op *sysrq_key_table[36] = {
Kyle McMartin 8aeb27
 	&sysrq_showstate_blocked_op,	/* w */
Kyle McMartin 8aeb27
 	/* x: May be registered on ppc/powerpc for xmon */
Kyle McMartin 8aeb27
 	/* x: May be registered on sparc64 for global PMU dump */
Kyle McMartin 8aeb27
+	/* x: May be registered on x86_64 for disabling secure boot */
Kyle McMartin 8aeb27
 	NULL,				/* x */
Kyle McMartin 8aeb27
 	/* y: May be registered on sparc64 for global register dump */
Kyle McMartin 8aeb27
 	NULL,				/* y */
a7fb62
@@ -506,7 +507,7 @@ static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
Kyle McMartin 8aeb27
                 sysrq_key_table[i] = op_p;
Kyle McMartin 8aeb27
 }
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
-void __handle_sysrq(int key, bool check_mask)
Kyle McMartin 8aeb27
+void __handle_sysrq(int key, int from)
Kyle McMartin 8aeb27
 {
Kyle McMartin 8aeb27
 	struct sysrq_key_op *op_p;
Kyle McMartin 8aeb27
 	int orig_log_level;
a7fb62
@@ -526,11 +527,15 @@ void __handle_sysrq(int key, bool check_mask)
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
         op_p = __sysrq_get_key_op(key);
Kyle McMartin 8aeb27
         if (op_p) {
Kyle McMartin 8aeb27
+		/* Ban synthetic events from some sysrq functionality */
Kyle McMartin 8aeb27
+		if ((from == SYSRQ_FROM_PROC || from == SYSRQ_FROM_SYNTHETIC) &&
Kyle McMartin 8aeb27
+		    op_p->enable_mask & SYSRQ_DISABLE_USERSPACE)
Kyle McMartin 8aeb27
+			printk("This sysrq operation is disabled from userspace.\n");
Kyle McMartin 8aeb27
 		/*
Kyle McMartin 8aeb27
 		 * Should we check for enabled operations (/proc/sysrq-trigger
Kyle McMartin 8aeb27
 		 * should not) and is the invoked operation enabled?
Kyle McMartin 8aeb27
 		 */
Kyle McMartin 8aeb27
-		if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Kyle McMartin 8aeb27
+		if (from == SYSRQ_FROM_KERNEL || sysrq_on_mask(op_p->enable_mask)) {
Kyle McMartin 8aeb27
 			printk("%s\n", op_p->action_msg);
Kyle McMartin 8aeb27
 			console_loglevel = orig_log_level;
Kyle McMartin 8aeb27
 			op_p->handler(key);
a7fb62
@@ -562,7 +567,7 @@ void __handle_sysrq(int key, bool check_mask)
Kyle McMartin 8aeb27
 void handle_sysrq(int key)
Kyle McMartin 8aeb27
 {
Kyle McMartin 8aeb27
 	if (sysrq_on())
Kyle McMartin 8aeb27
-		__handle_sysrq(key, true);
Kyle McMartin 8aeb27
+		__handle_sysrq(key, SYSRQ_FROM_KERNEL);
Kyle McMartin 8aeb27
 }
Kyle McMartin 8aeb27
 EXPORT_SYMBOL(handle_sysrq);
Kyle McMartin 8aeb27
 
a7fb62
@@ -642,7 +647,7 @@ static void sysrq_do_reset(unsigned long _state)
Kyle McMartin 8aeb27
 static void sysrq_handle_reset_request(struct sysrq_state *state)
Kyle McMartin 8aeb27
 {
Kyle McMartin 8aeb27
 	if (state->reset_requested)
Kyle McMartin 8aeb27
-		__handle_sysrq(sysrq_xlate[KEY_B], false);
Kyle McMartin 8aeb27
+		__handle_sysrq(sysrq_xlate[KEY_B], SYSRQ_FROM_KERNEL);
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
 	if (sysrq_reset_downtime_ms)
Kyle McMartin 8aeb27
 		mod_timer(&state->keyreset_timer,
a7fb62
@@ -793,8 +798,10 @@ static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
 	default:
Kyle McMartin 8aeb27
 		if (sysrq->active && value && value != 2) {
Kyle McMartin 8aeb27
+			int from = sysrq->handle.dev->flags & INPUTDEV_FLAGS_SYNTHETIC ?
Kyle McMartin 8aeb27
+					SYSRQ_FROM_SYNTHETIC : 0;
Kyle McMartin 8aeb27
 			sysrq->need_reinject = false;
Kyle McMartin 8aeb27
-			__handle_sysrq(sysrq_xlate[code], true);
Kyle McMartin 8aeb27
+			__handle_sysrq(sysrq_xlate[code], from);
Kyle McMartin 8aeb27
 		}
Kyle McMartin 8aeb27
 		break;
Kyle McMartin 8aeb27
 	}
a7fb62
@@ -1089,7 +1096,7 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
 		if (get_user(c, buf))
Kyle McMartin 8aeb27
 			return -EFAULT;
Kyle McMartin 8aeb27
-		__handle_sysrq(c, false);
Kyle McMartin 8aeb27
+		__handle_sysrq(c, SYSRQ_FROM_PROC);
Kyle McMartin 8aeb27
 	}
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
 	return count;
Kyle McMartin 8aeb27
diff --git a/include/linux/input.h b/include/linux/input.h
a7fb62
index 82ce323b9986..9e534f228945 100644
Kyle McMartin 8aeb27
--- a/include/linux/input.h
Kyle McMartin 8aeb27
+++ b/include/linux/input.h
Kyle McMartin 8aeb27
@@ -42,6 +42,7 @@ struct input_value {
Kyle McMartin 8aeb27
  * @phys: physical path to the device in the system hierarchy
Kyle McMartin 8aeb27
  * @uniq: unique identification code for the device (if device has it)
Kyle McMartin 8aeb27
  * @id: id of the device (struct input_id)
Kyle McMartin 8aeb27
+ * @flags: input device flags (SYNTHETIC, etc.)
Kyle McMartin 8aeb27
  * @propbit: bitmap of device properties and quirks
Kyle McMartin 8aeb27
  * @evbit: bitmap of types of events supported by the device (EV_KEY,
Kyle McMartin 8aeb27
  *	EV_REL, etc.)
Kyle McMartin 8aeb27
@@ -124,6 +125,8 @@ struct input_dev {
Kyle McMartin 8aeb27
 	const char *uniq;
Kyle McMartin 8aeb27
 	struct input_id id;
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
+	unsigned int flags;
Kyle McMartin 8aeb27
+
Kyle McMartin 8aeb27
 	unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
 	unsigned long evbit[BITS_TO_LONGS(EV_CNT)];
Kyle McMartin 8aeb27
@@ -190,6 +193,8 @@ struct input_dev {
Kyle McMartin 8aeb27
 };
Kyle McMartin 8aeb27
 #define to_input_dev(d) container_of(d, struct input_dev, dev)
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
+#define	INPUTDEV_FLAGS_SYNTHETIC	0x000000001
Kyle McMartin 8aeb27
+
Kyle McMartin 8aeb27
 /*
Kyle McMartin 8aeb27
  * Verify that we are in sync with input_device_id mod_devicetable.h #defines
Kyle McMartin 8aeb27
  */
Kyle McMartin 8aeb27
diff --git a/include/linux/sysrq.h b/include/linux/sysrq.h
a7fb62
index 387fa7d05c98..4b07e30b3279 100644
Kyle McMartin 8aeb27
--- a/include/linux/sysrq.h
Kyle McMartin 8aeb27
+++ b/include/linux/sysrq.h
107552
@@ -28,6 +28,8 @@
Kyle McMartin 8aeb27
 #define SYSRQ_ENABLE_BOOT	0x0080
Kyle McMartin 8aeb27
 #define SYSRQ_ENABLE_RTNICE	0x0100
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
+#define SYSRQ_DISABLE_USERSPACE	0x00010000
Kyle McMartin 8aeb27
+
Kyle McMartin 8aeb27
 struct sysrq_key_op {
Kyle McMartin 8aeb27
 	void (*handler)(int);
Kyle McMartin 8aeb27
 	char *help_msg;
107552
@@ -42,8 +44,12 @@ struct sysrq_key_op {
Kyle McMartin 8aeb27
  * are available -- else NULL's).
Kyle McMartin 8aeb27
  */
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
+#define SYSRQ_FROM_KERNEL	0x0001
Kyle McMartin 8aeb27
+#define SYSRQ_FROM_PROC		0x0002
Kyle McMartin 8aeb27
+#define SYSRQ_FROM_SYNTHETIC	0x0004
Kyle McMartin 8aeb27
+
Kyle McMartin 8aeb27
 void handle_sysrq(int key);
Kyle McMartin 8aeb27
-void __handle_sysrq(int key, bool check_mask);
Kyle McMartin 8aeb27
+void __handle_sysrq(int key, int from);
Kyle McMartin 8aeb27
 int register_sysrq_key(int key, struct sysrq_key_op *op);
Kyle McMartin 8aeb27
 int unregister_sysrq_key(int key, struct sysrq_key_op *op);
Kyle McMartin 8aeb27
 struct sysrq_key_op *__sysrq_get_key_op(int key);
Kyle McMartin 8aeb27
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
dbc4a9
index 379650b984f8..070f29fefdc2 100644
Kyle McMartin 8aeb27
--- a/kernel/debug/kdb/kdb_main.c
Kyle McMartin 8aeb27
+++ b/kernel/debug/kdb/kdb_main.c
107552
@@ -1924,7 +1924,7 @@ static int kdb_sr(int argc, const char **argv)
Kyle McMartin 8aeb27
 	if (argc != 1)
Kyle McMartin 8aeb27
 		return KDB_ARGCOUNT;
Kyle McMartin 8aeb27
 	kdb_trap_printk++;
Kyle McMartin 8aeb27
-	__handle_sysrq(*argv[1], false);
Kyle McMartin 8aeb27
+	__handle_sysrq(*argv[1], SYSRQ_FROM_KERNEL);
Kyle McMartin 8aeb27
 	kdb_trap_printk--;
Kyle McMartin 8aeb27
 
Kyle McMartin 8aeb27
 	return 0;
Josh Boyer c9d9c5
diff --git a/kernel/module.c b/kernel/module.c
229fa3
index ec12c156ea61..1db033284ad3 100644
Josh Boyer c9d9c5
--- a/kernel/module.c
Josh Boyer c9d9c5
+++ b/kernel/module.c
dbc4a9
@@ -108,9 +108,9 @@ struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
Josh Boyer c9d9c5
 
Josh Boyer c9d9c5
 #ifdef CONFIG_MODULE_SIG
Josh Boyer c9d9c5
 #ifdef CONFIG_MODULE_SIG_FORCE
Josh Boyer c9d9c5
-static bool sig_enforce = true;
Josh Boyer c9d9c5
+bool sig_enforce = true;
Josh Boyer c9d9c5
 #else
Josh Boyer c9d9c5
-static bool sig_enforce = false;
Josh Boyer c9d9c5
+bool sig_enforce = false;
Josh Boyer c9d9c5
 
Josh Boyer c9d9c5
 static int param_set_bool_enable_only(const char *val,
Josh Boyer c9d9c5
 				      const struct kernel_param *kp)
Josh Boyer c9d9c5
-- 
c47527
2.1.0
Josh Boyer c9d9c5