39c21e4
From 2f30cdb842617774e88e6c310a2265eecc50e8f4 Mon Sep 17 00:00:00 2001
c9d9c5a
From: Kyle McMartin <kyle@redhat.com>
c9d9c5a
Date: Fri, 30 Aug 2013 09:28:51 -0400
c9d9c5a
Subject: [PATCH] Add sysrq option to disable secure boot mode
c9d9c5a
6a91557
Bugzilla: N/A
6a91557
Upstream-status: Fedora mustard
c9d9c5a
---
1075524
 arch/x86/kernel/setup.c     | 36 ++++++++++++++++++++++++++++++++++++
c9d9c5a
 drivers/input/misc/uinput.c |  1 +
c9d9c5a
 drivers/tty/sysrq.c         | 19 +++++++++++++------
c9d9c5a
 include/linux/input.h       |  5 +++++
c9d9c5a
 include/linux/sysrq.h       |  8 +++++++-
c9d9c5a
 kernel/debug/kdb/kdb_main.c |  2 +-
c9d9c5a
 kernel/module.c             |  4 ++--
1075524
 7 files changed, 65 insertions(+), 10 deletions(-)
c9d9c5a
Kyle McMartin 8aeb27e
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
6a91557
index fb282ff6a802..d291d16ba257 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>
a7fb628
@@ -1268,6 +1273,37 @@ void __init i386_reserve_resources(void)
Kyle McMartin 8aeb27e
 
Kyle McMartin 8aeb27e
 #endif /* CONFIG_X86_32 */
1075524
 
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
+{
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
+{
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*/
1075524
+
1075524
+
1075524
 static struct notifier_block kernel_offset_notifier = {
1075524
 	.notifier_call = dump_kernel_offset
1075524
 };
Kyle McMartin 8aeb27e
diff --git a/drivers/input/misc/uinput.c b/drivers/input/misc/uinput.c
6a91557
index 421e29e4cd81..61c1eb97806c 100644
Kyle McMartin 8aeb27e
--- a/drivers/input/misc/uinput.c
Kyle McMartin 8aeb27e
+++ b/drivers/input/misc/uinput.c
6a91557
@@ -366,6 +366,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
6a91557
index 42bad18c66c9..496e073b09d7 100644
Kyle McMartin 8aeb27e
--- a/drivers/tty/sysrq.c
Kyle McMartin 8aeb27e
+++ b/drivers/tty/sysrq.c
a7fb628
@@ -463,6 +463,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 */
a7fb628
@@ -506,7 +507,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;
a7fb628
@@ -526,11 +527,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);
a7fb628
@@ -562,7 +567,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
 
a7fb628
@@ -642,7 +647,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,
a7fb628
@@ -793,8 +798,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
 	}
a7fb628
@@ -1089,7 +1096,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
a7fb628
index 82ce323b9986..9e534f228945 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
a7fb628
index 387fa7d05c98..4b07e30b3279 100644
Kyle McMartin 8aeb27e
--- a/include/linux/sysrq.h
Kyle McMartin 8aeb27e
+++ b/include/linux/sysrq.h
1075524
@@ -28,6 +28,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;
1075524
@@ -42,8 +44,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
6a91557
index 379650b984f8..070f29fefdc2 100644
Kyle McMartin 8aeb27e
--- a/kernel/debug/kdb/kdb_main.c
Kyle McMartin 8aeb27e
+++ b/kernel/debug/kdb/kdb_main.c
1075524
@@ -1924,7 +1924,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;
c9d9c5a
diff --git a/kernel/module.c b/kernel/module.c
6a91557
index 866417ecc76a..d7ca95c5a349 100644
c9d9c5a
--- a/kernel/module.c
c9d9c5a
+++ b/kernel/module.c
6a91557
@@ -108,9 +108,9 @@ struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
c9d9c5a
 
c9d9c5a
 #ifdef CONFIG_MODULE_SIG
c9d9c5a
 #ifdef CONFIG_MODULE_SIG_FORCE
c9d9c5a
-static bool sig_enforce = true;
c9d9c5a
+bool sig_enforce = true;
c9d9c5a
 #else
c9d9c5a
-static bool sig_enforce = false;
c9d9c5a
+bool sig_enforce = false;
c9d9c5a
 
c9d9c5a
 static int param_set_bool_enable_only(const char *val,
c9d9c5a
 				      const struct kernel_param *kp)
c9d9c5a
-- 
a7fb628
1.9.3
c9d9c5a