68d0d67
Bugzilla: N/A
68d0d67
Upstream-status: Fedora mustard
68d0d67
Josh Boyer c9d9c5a
From 71aac34ed679daa0bf772051eb40412b5bd95da3 Mon Sep 17 00:00:00 2001
Josh Boyer c9d9c5a
From: Kyle McMartin <kyle@redhat.com>
Josh Boyer c9d9c5a
Date: Fri, 30 Aug 2013 09:28:51 -0400
Josh Boyer c9d9c5a
Subject: [PATCH] Add sysrq option to disable secure boot mode
Josh Boyer c9d9c5a
Josh Boyer c9d9c5a
---
Josh Boyer c9d9c5a
 arch/x86/kernel/setup.c     | 35 +++++++++++++++++++++++++++++++++++
Josh Boyer c9d9c5a
 drivers/input/misc/uinput.c |  1 +
Josh Boyer c9d9c5a
 drivers/tty/sysrq.c         | 19 +++++++++++++------
Josh Boyer c9d9c5a
 include/linux/input.h       |  5 +++++
Josh Boyer c9d9c5a
 include/linux/sysrq.h       |  8 +++++++-
Josh Boyer c9d9c5a
 kernel/debug/kdb/kdb_main.c |  2 +-
Josh Boyer c9d9c5a
 kernel/module.c             |  4 ++--
Josh Boyer c9d9c5a
 7 files changed, 64 insertions(+), 10 deletions(-)
Josh Boyer c9d9c5a
Kyle McMartin 8aeb27e
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
Josh Boyer c9d9c5a
index 08dc16e..6971f8e 100644
Kyle McMartin 8aeb27e
--- a/arch/x86/kernel/setup.c
Kyle McMartin 8aeb27e
+++ b/arch/x86/kernel/setup.c
Kyle McMartin 8aeb27e
@@ -70,6 +70,11 @@
Kyle McMartin 8aeb27e
 #include <linux/tboot.h>
Kyle McMartin 8aeb27e
 #include <linux/jiffies.h>
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
+#include <linux/fips.h>
Kyle McMartin 8aeb27e
+#include <linux/cred.h>
Kyle McMartin 8aeb27e
+#include <linux/sysrq.h>
Kyle McMartin 8aeb27e
+#include <linux/init_task.h>
Kyle McMartin 8aeb27e
+
Kyle McMartin 8aeb27e
 #include <video/edid.h>
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 #include <asm/mtrr.h>
Josh Boyer c9d9c5a
@@ -1253,3 +1258,33 @@ void __init i386_reserve_resources(void)
Kyle McMartin 8aeb27e
 }
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 #endif /* CONFIG_X86_32 */
Kyle McMartin 8aeb27e
+
Josh Boyer c9d9c5a
+#ifdef CONFIG_MAGIC_SYSRQ
Kyle McMartin 8aeb27e
+#ifdef CONFIG_MODULE_SIG
Kyle McMartin 8aeb27e
+extern bool sig_enforce;
Kyle McMartin 8aeb27e
+#endif
Kyle McMartin 8aeb27e
+
Kyle McMartin 8aeb27e
+static void sysrq_handle_secure_boot(int key)
Kyle McMartin 8aeb27e
+{
Josh Boyer c9d9c5a
+	if (!efi_enabled(EFI_SECURE_BOOT))
Kyle McMartin 8aeb27e
+		return;
Kyle McMartin 8aeb27e
+
Kyle McMartin 8aeb27e
+	pr_info("Secure boot disabled\n");
Kyle McMartin 8aeb27e
+#ifdef CONFIG_MODULE_SIG
Kyle McMartin 8aeb27e
+	sig_enforce = fips_enabled;
Kyle McMartin 8aeb27e
+#endif
Kyle McMartin 8aeb27e
+}
Kyle McMartin 8aeb27e
+static struct sysrq_key_op secure_boot_sysrq_op = {
Kyle McMartin 8aeb27e
+	.handler	=	sysrq_handle_secure_boot,
Kyle McMartin 8aeb27e
+	.help_msg	=	"unSB(x)",
Kyle McMartin 8aeb27e
+	.action_msg	=	"Disabling Secure Boot restrictions",
Kyle McMartin 8aeb27e
+	.enable_mask	=	SYSRQ_DISABLE_USERSPACE,
Kyle McMartin 8aeb27e
+};
Kyle McMartin 8aeb27e
+static int __init secure_boot_sysrq(void)
Kyle McMartin 8aeb27e
+{
Josh Boyer c9d9c5a
+	if (efi_enabled(EFI_SECURE_BOOT))
Kyle McMartin 8aeb27e
+		register_sysrq_key('x', &secure_boot_sysrq_op);
Kyle McMartin 8aeb27e
+	return 0;
Kyle McMartin 8aeb27e
+}
Kyle McMartin 8aeb27e
+late_initcall(secure_boot_sysrq);
Kyle McMartin 8aeb27e
+#endif /*CONFIG_MAGIC_SYSRQ*/
Kyle McMartin 8aeb27e
diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c
Kyle McMartin 8aeb27e
index a0a4bba..3327cc3 100644
Kyle McMartin 8aeb27e
--- a/drivers/input/misc/uinput.c
Kyle McMartin 8aeb27e
+++ b/drivers/input/misc/uinput.c
Kyle McMartin 8aeb27e
@@ -351,6 +351,7 @@ static int uinput_allocate_device(struct uinput_device *udev)
Kyle McMartin 8aeb27e
 	if (!udev->dev)
Kyle McMartin 8aeb27e
 		return -ENOMEM;
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
+	udev->dev->flags |= INPUTDEV_FLAGS_SYNTHETIC;
Kyle McMartin 8aeb27e
 	udev->dev->event = uinput_dev_event;
Kyle McMartin 8aeb27e
 	input_set_drvdata(udev->dev, udev);
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
Kyle McMartin 8aeb27e
index d5cc3ac..05b33f5 100644
Kyle McMartin 8aeb27e
--- a/drivers/tty/sysrq.c
Kyle McMartin 8aeb27e
+++ b/drivers/tty/sysrq.c
Kyle McMartin 8aeb27e
@@ -461,6 +461,7 @@ static struct sysrq_key_op *sysrq_key_table[36] = {
Kyle McMartin 8aeb27e
 	&sysrq_showstate_blocked_op,	/* w */
Kyle McMartin 8aeb27e
 	/* x: May be registered on ppc/powerpc for xmon */
Kyle McMartin 8aeb27e
 	/* x: May be registered on sparc64 for global PMU dump */
Kyle McMartin 8aeb27e
+	/* x: May be registered on x86_64 for disabling secure boot */
Kyle McMartin 8aeb27e
 	NULL,				/* x */
Kyle McMartin 8aeb27e
 	/* y: May be registered on sparc64 for global register dump */
Kyle McMartin 8aeb27e
 	NULL,				/* y */
Kyle McMartin 8aeb27e
@@ -504,7 +505,7 @@ static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
Kyle McMartin 8aeb27e
                 sysrq_key_table[i] = op_p;
Kyle McMartin 8aeb27e
 }
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
-void __handle_sysrq(int key, bool check_mask)
Kyle McMartin 8aeb27e
+void __handle_sysrq(int key, int from)
Kyle McMartin 8aeb27e
 {
Kyle McMartin 8aeb27e
 	struct sysrq_key_op *op_p;
Kyle McMartin 8aeb27e
 	int orig_log_level;
Kyle McMartin 8aeb27e
@@ -524,11 +525,15 @@ void __handle_sysrq(int key, bool check_mask)
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
         op_p = __sysrq_get_key_op(key);
Kyle McMartin 8aeb27e
         if (op_p) {
Kyle McMartin 8aeb27e
+		/* Ban synthetic events from some sysrq functionality */
Kyle McMartin 8aeb27e
+		if ((from == SYSRQ_FROM_PROC || from == SYSRQ_FROM_SYNTHETIC) &&
Kyle McMartin 8aeb27e
+		    op_p->enable_mask & SYSRQ_DISABLE_USERSPACE)
Kyle McMartin 8aeb27e
+			printk("This sysrq operation is disabled from userspace.\n");
Kyle McMartin 8aeb27e
 		/*
Kyle McMartin 8aeb27e
 		 * Should we check for enabled operations (/proc/sysrq-trigger
Kyle McMartin 8aeb27e
 		 * should not) and is the invoked operation enabled?
Kyle McMartin 8aeb27e
 		 */
Kyle McMartin 8aeb27e
-		if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Kyle McMartin 8aeb27e
+		if (from == SYSRQ_FROM_KERNEL || sysrq_on_mask(op_p->enable_mask)) {
Kyle McMartin 8aeb27e
 			printk("%s\n", op_p->action_msg);
Kyle McMartin 8aeb27e
 			console_loglevel = orig_log_level;
Kyle McMartin 8aeb27e
 			op_p->handler(key);
Kyle McMartin 8aeb27e
@@ -559,7 +564,7 @@ void __handle_sysrq(int key, bool check_mask)
Kyle McMartin 8aeb27e
 void handle_sysrq(int key)
Kyle McMartin 8aeb27e
 {
Kyle McMartin 8aeb27e
 	if (sysrq_on())
Kyle McMartin 8aeb27e
-		__handle_sysrq(key, true);
Kyle McMartin 8aeb27e
+		__handle_sysrq(key, SYSRQ_FROM_KERNEL);
Kyle McMartin 8aeb27e
 }
Kyle McMartin 8aeb27e
 EXPORT_SYMBOL(handle_sysrq);
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
@@ -639,7 +644,7 @@ static void sysrq_do_reset(unsigned long _state)
Kyle McMartin 8aeb27e
 static void sysrq_handle_reset_request(struct sysrq_state *state)
Kyle McMartin 8aeb27e
 {
Kyle McMartin 8aeb27e
 	if (state->reset_requested)
Kyle McMartin 8aeb27e
-		__handle_sysrq(sysrq_xlate[KEY_B], false);
Kyle McMartin 8aeb27e
+		__handle_sysrq(sysrq_xlate[KEY_B], SYSRQ_FROM_KERNEL);
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 	if (sysrq_reset_downtime_ms)
Kyle McMartin 8aeb27e
 		mod_timer(&state->keyreset_timer,
Kyle McMartin 8aeb27e
@@ -756,8 +761,10 @@ static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 	default:
Kyle McMartin 8aeb27e
 		if (sysrq->active && value && value != 2) {
Kyle McMartin 8aeb27e
+			int from = sysrq->handle.dev->flags & INPUTDEV_FLAGS_SYNTHETIC ?
Kyle McMartin 8aeb27e
+					SYSRQ_FROM_SYNTHETIC : 0;
Kyle McMartin 8aeb27e
 			sysrq->need_reinject = false;
Kyle McMartin 8aeb27e
-			__handle_sysrq(sysrq_xlate[code], true);
Kyle McMartin 8aeb27e
+			__handle_sysrq(sysrq_xlate[code], from);
Kyle McMartin 8aeb27e
 		}
Kyle McMartin 8aeb27e
 		break;
Kyle McMartin 8aeb27e
 	}
Kyle McMartin 8aeb27e
@@ -1038,7 +1045,7 @@ static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 		if (get_user(c, buf))
Kyle McMartin 8aeb27e
 			return -EFAULT;
Kyle McMartin 8aeb27e
-		__handle_sysrq(c, false);
Kyle McMartin 8aeb27e
+		__handle_sysrq(c, SYSRQ_FROM_PROC);
Kyle McMartin 8aeb27e
 	}
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 	return count;
Kyle McMartin 8aeb27e
diff --git a/include/linux/input.h b/include/linux/input.h
Kyle McMartin 8aeb27e
index 82ce323..9e534f2 100644
Kyle McMartin 8aeb27e
--- a/include/linux/input.h
Kyle McMartin 8aeb27e
+++ b/include/linux/input.h
Kyle McMartin 8aeb27e
@@ -42,6 +42,7 @@ struct input_value {
Kyle McMartin 8aeb27e
  * @phys: physical path to the device in the system hierarchy
Kyle McMartin 8aeb27e
  * @uniq: unique identification code for the device (if device has it)
Kyle McMartin 8aeb27e
  * @id: id of the device (struct input_id)
Kyle McMartin 8aeb27e
+ * @flags: input device flags (SYNTHETIC, etc.)
Kyle McMartin 8aeb27e
  * @propbit: bitmap of device properties and quirks
Kyle McMartin 8aeb27e
  * @evbit: bitmap of types of events supported by the device (EV_KEY,
Kyle McMartin 8aeb27e
  *	EV_REL, etc.)
Kyle McMartin 8aeb27e
@@ -124,6 +125,8 @@ struct input_dev {
Kyle McMartin 8aeb27e
 	const char *uniq;
Kyle McMartin 8aeb27e
 	struct input_id id;
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
+	unsigned int flags;
Kyle McMartin 8aeb27e
+
Kyle McMartin 8aeb27e
 	unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 	unsigned long evbit[BITS_TO_LONGS(EV_CNT)];
Kyle McMartin 8aeb27e
@@ -190,6 +193,8 @@ struct input_dev {
Kyle McMartin 8aeb27e
 };
Kyle McMartin 8aeb27e
 #define to_input_dev(d) container_of(d, struct input_dev, dev)
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
+#define	INPUTDEV_FLAGS_SYNTHETIC	0x000000001
Kyle McMartin 8aeb27e
+
Kyle McMartin 8aeb27e
 /*
Kyle McMartin 8aeb27e
  * Verify that we are in sync with input_device_id mod_devicetable.h #defines
Kyle McMartin 8aeb27e
  */
Kyle McMartin 8aeb27e
diff --git a/include/linux/sysrq.h b/include/linux/sysrq.h
Kyle McMartin 8aeb27e
index 7faf933..87ae634 100644
Kyle McMartin 8aeb27e
--- a/include/linux/sysrq.h
Kyle McMartin 8aeb27e
+++ b/include/linux/sysrq.h
Kyle McMartin 8aeb27e
@@ -31,6 +31,8 @@
Kyle McMartin 8aeb27e
 #define SYSRQ_ENABLE_BOOT	0x0080
Kyle McMartin 8aeb27e
 #define SYSRQ_ENABLE_RTNICE	0x0100
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
+#define SYSRQ_DISABLE_USERSPACE	0x00010000
Kyle McMartin 8aeb27e
+
Kyle McMartin 8aeb27e
 struct sysrq_key_op {
Kyle McMartin 8aeb27e
 	void (*handler)(int);
Kyle McMartin 8aeb27e
 	char *help_msg;
Kyle McMartin 8aeb27e
@@ -45,8 +47,12 @@ struct sysrq_key_op {
Kyle McMartin 8aeb27e
  * are available -- else NULL's).
Kyle McMartin 8aeb27e
  */
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
+#define SYSRQ_FROM_KERNEL	0x0001
Kyle McMartin 8aeb27e
+#define SYSRQ_FROM_PROC		0x0002
Kyle McMartin 8aeb27e
+#define SYSRQ_FROM_SYNTHETIC	0x0004
Kyle McMartin 8aeb27e
+
Kyle McMartin 8aeb27e
 void handle_sysrq(int key);
Kyle McMartin 8aeb27e
-void __handle_sysrq(int key, bool check_mask);
Kyle McMartin 8aeb27e
+void __handle_sysrq(int key, int from);
Kyle McMartin 8aeb27e
 int register_sysrq_key(int key, struct sysrq_key_op *op);
Kyle McMartin 8aeb27e
 int unregister_sysrq_key(int key, struct sysrq_key_op *op);
Kyle McMartin 8aeb27e
 struct sysrq_key_op *__sysrq_get_key_op(int key);
Kyle McMartin 8aeb27e
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
Kyle McMartin 8aeb27e
index 00eb8f7..54fbbcc 100644
Kyle McMartin 8aeb27e
--- a/kernel/debug/kdb/kdb_main.c
Kyle McMartin 8aeb27e
+++ b/kernel/debug/kdb/kdb_main.c
Kyle McMartin 8aeb27e
@@ -1921,7 +1921,7 @@ static int kdb_sr(int argc, const char **argv)
Kyle McMartin 8aeb27e
 	if (argc != 1)
Kyle McMartin 8aeb27e
 		return KDB_ARGCOUNT;
Kyle McMartin 8aeb27e
 	kdb_trap_printk++;
Kyle McMartin 8aeb27e
-	__handle_sysrq(*argv[1], false);
Kyle McMartin 8aeb27e
+	__handle_sysrq(*argv[1], SYSRQ_FROM_KERNEL);
Kyle McMartin 8aeb27e
 	kdb_trap_printk--;
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 	return 0;
Josh Boyer c9d9c5a
diff --git a/kernel/module.c b/kernel/module.c
Josh Boyer c9d9c5a
index bc7c987..5e4e2c2 100644
Josh Boyer c9d9c5a
--- a/kernel/module.c
Josh Boyer c9d9c5a
+++ b/kernel/module.c
Josh Boyer c9d9c5a
@@ -109,9 +109,9 @@ struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
Josh Boyer c9d9c5a
 
Josh Boyer c9d9c5a
 #ifdef CONFIG_MODULE_SIG
Josh Boyer c9d9c5a
 #ifdef CONFIG_MODULE_SIG_FORCE
Josh Boyer c9d9c5a
-static bool sig_enforce = true;
Josh Boyer c9d9c5a
+bool sig_enforce = true;
Josh Boyer c9d9c5a
 #else
Josh Boyer c9d9c5a
-static bool sig_enforce = false;
Josh Boyer c9d9c5a
+bool sig_enforce = false;
Josh Boyer c9d9c5a
 
Josh Boyer c9d9c5a
 static int param_set_bool_enable_only(const char *val,
Josh Boyer c9d9c5a
 				      const struct kernel_param *kp)
Josh Boyer c9d9c5a
-- 
Josh Boyer c9d9c5a
1.8.3.1
Josh Boyer c9d9c5a