linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: torvalds@linux-foundation.org
Cc: linux-man@vger.kernel.org, linux-api@vger.kernel.org,
	jmorris@namei.org, linux-kernel@vger.kernel.org,
	dhowells@redhat.com, linux-security-module@vger.kernel.org
Subject: [PATCH 01/24] Add the ability to lock down access to the running kernel image
Date: Wed, 11 Apr 2018 17:24:45 +0100	[thread overview]
Message-ID: <152346388583.4030.15146667041427303547.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <152346387861.4030.4408662483445703127.stgit@warthog.procyon.org.uk>

Provide a single call to allow kernel code to determine whether the system
should be locked down, thereby disallowing various accesses that might
allow the running kernel image to be changed, including:

 - /dev/mem and similar
 - Loading of unauthorised modules
 - Fiddling with MSR registers
 - Suspend to disk managed by the kernel
 - Use of device DMA

Two kernel configuration options are provided:

 (*) CONFIG_LOCK_DOWN_KERNEL

     This makes lockdown available and applies it to all the points that
     need to be locked down if the mode is set.  Lockdown mode can be
     enabled by providing:

	lockdown=1

     on the command line.

 (*) CONFIG_LOCK_DOWN_MANDATORY

     This forces lockdown on at compile time, overriding the command line
     option.

init_lockdown() is used as a hook from which lockdown can be managed in
future.  It has to be called from arch setup code before things like ACPI
are enabled.

Note that, with the other changes in this series, if lockdown mode is
enabled, the kernel will not be able to use certain drivers as the ability
to manually configure hardware parameters would then be prohibited.  This
primarily applies to ISA hardware devices.

Signed-off-by: David Howells <dhowells@redhat.com>
---

 arch/x86/kernel/setup.c |    2 +
 include/linux/kernel.h  |   32 +++++++++++++++++++++++
 security/Kconfig        |   23 ++++++++++++++++-
 security/Makefile       |    3 ++
 security/lock_down.c    |   65 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 security/lock_down.c

diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 6285697b6e56..566f0f447053 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -996,6 +996,8 @@ void __init setup_arch(char **cmdline_p)
 	if (efi_enabled(EFI_BOOT))
 		efi_init();
 
+	init_lockdown();
+
 	dmi_scan_machine();
 	dmi_memdev_walk();
 	dmi_set_dump_stack_arch_desc();
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 4ae1dfd9bf05..7d085cca9cee 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -306,6 +306,38 @@ static inline void refcount_error_report(struct pt_regs *regs, const char *err)
 { }
 #endif
 
+#ifdef CONFIG_LOCK_DOWN_KERNEL
+extern void __init init_lockdown(void);
+extern bool __kernel_is_locked_down(const char *what, bool first);
+
+#ifndef CONFIG_LOCK_DOWN_MANDATORY
+#define kernel_is_locked_down(what)					\
+	({								\
+		static bool message_given;				\
+		bool locked_down = __kernel_is_locked_down(what, !message_given); \
+		message_given = true;					\
+		locked_down;						\
+	})
+#else
+#define kernel_is_locked_down(what)					\
+	({								\
+		static bool message_given;				\
+		__kernel_is_locked_down(what, !message_given);		\
+		message_given = true;					\
+		true;							\
+	})
+#endif
+#else
+static inline void __init init_lockdown(void)
+{
+}
+static inline bool __kernel_is_locked_down(const char *what, bool first)
+{
+	return false;
+}
+#define kernel_is_locked_down(what) ({ false; })
+#endif
+
 /* Internal, do not use. */
 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
diff --git a/security/Kconfig b/security/Kconfig
index c4302067a3ad..a68e5bdebad5 100644
--- a/security/Kconfig
+++ b/security/Kconfig
@@ -231,6 +231,28 @@ config STATIC_USERMODEHELPER_PATH
 	  If you wish for all usermode helper programs to be disabled,
 	  specify an empty string here (i.e. "").
 
+config LOCK_DOWN_KERNEL
+	bool "Allow the kernel to be 'locked down'"
+	help
+	  Allow the kernel to be locked down.  Locking down the kernel turns
+	  off various features that might otherwise allow access to the kernel
+	  image (eg. setting MSR registers).
+
+	  Note, however, that locking down your kernel will prevent some
+	  drivers from functioning because allowing manual configuration of
+	  hardware parameters is forbidden, lest a device be used to access the
+	  kernel by DMA.  This mostly applies to ISA devices.
+
+	  The kernel lockdown can be triggered by adding lockdown=1 to the
+	  kernel command line.
+
+config LOCK_DOWN_MANDATORY
+	bool "Make kernel lockdown mandatory"
+	depends on LOCK_DOWN_KERNEL
+	help
+	  Makes the lockdown non-negotiable.  It is always on and cannot be
+	  disabled.
+
 source security/selinux/Kconfig
 source security/smack/Kconfig
 source security/tomoyo/Kconfig
@@ -278,4 +300,3 @@ config DEFAULT_SECURITY
 	default "" if DEFAULT_SECURITY_DAC
 
 endmenu
-
diff --git a/security/Makefile b/security/Makefile
index 4d2d3782ddef..507ac8c520ce 100644
--- a/security/Makefile
+++ b/security/Makefile
@@ -30,3 +30,6 @@ obj-$(CONFIG_CGROUP_DEVICE)		+= device_cgroup.o
 # Object integrity file lists
 subdir-$(CONFIG_INTEGRITY)		+= integrity
 obj-$(CONFIG_INTEGRITY)			+= integrity/
+
+# Allow the kernel to be locked down
+obj-$(CONFIG_LOCK_DOWN_KERNEL)		+= lock_down.o
diff --git a/security/lock_down.c b/security/lock_down.c
new file mode 100644
index 000000000000..f35ffdd096ad
--- /dev/null
+++ b/security/lock_down.c
@@ -0,0 +1,65 @@
+/* Lock down the kernel
+ *
+ * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells (dhowells@redhat.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public Licence
+ * as published by the Free Software Foundation; either version
+ * 2 of the Licence, or (at your option) any later version.
+ */
+
+#include <linux/export.h>
+#include <linux/sched.h>
+
+#ifndef CONFIG_LOCK_DOWN_MANDATORY
+static __ro_after_init bool kernel_locked_down;
+#else
+#define kernel_locked_down true
+#endif
+
+/*
+ * Put the kernel into lock-down mode.
+ */
+static void __init lock_kernel_down(const char *where)
+{
+#ifndef CONFIG_LOCK_DOWN_MANDATORY
+	if (!kernel_locked_down) {
+		kernel_locked_down = true;
+		pr_notice("Kernel is locked down from %s; see man kernel_lockdown.7\n",
+			  where);
+	}
+#endif
+}
+
+static int __init lockdown_param(char *ignored)
+{
+	lock_kernel_down("command line");
+	return 0;
+}
+
+early_param("lockdown", lockdown_param);
+
+/*
+ * Lock the kernel down from very early in the arch setup.  This must happen
+ * prior to things like ACPI being initialised.
+ */
+void __init init_lockdown(void)
+{
+#ifdef CONFIG_LOCK_DOWN_MANDATORY
+	pr_notice("Kernel is locked down from config; see man kernel_lockdown.7\n");
+#endif
+}
+
+/**
+ * kernel_is_locked_down - Find out if the kernel is locked down
+ * @what: Tag to use in notice generated if lockdown is in effect
+ */
+bool __kernel_is_locked_down(const char *what, bool first)
+{
+	if (what && first && kernel_locked_down)
+		pr_notice("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",
+			  current->comm, what);
+	return kernel_locked_down;
+}
+EXPORT_SYMBOL(__kernel_is_locked_down);


  reply	other threads:[~2018-04-11 16:24 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-11 16:24 [PATCH 00/24] security: Add kernel lockdown David Howells
2018-04-11 16:24 ` David Howells [this message]
2018-04-11 16:44   ` [PATCH 01/24] Add the ability to lock down access to the running kernel image Jann Horn
2018-04-11 17:37   ` Randy Dunlap
2018-04-11 18:50     ` Miguel Ojeda
2018-04-11 19:56       ` Greg KH
2018-04-11 17:49   ` David Howells
2018-04-11 18:09   ` Linus Torvalds
2018-04-11 18:35     ` Justin Forbes
2018-04-11 21:05     ` Jordan Glover
2018-04-11 22:38       ` Linus Torvalds
2018-04-12 13:09         ` Justin Forbes
2018-04-12 16:52           ` Linus Torvalds
2018-04-12  2:57   ` Andy Lutomirski
2018-04-11 16:24 ` [PATCH 02/24] Add a SysRq option to lift kernel lockdown David Howells
2018-04-11 17:05   ` Jann Horn
2018-04-13 20:22   ` Pavel Machek
2018-04-11 16:24 ` [PATCH 03/24] ima: require secure_boot rules in lockdown mode David Howells
2018-04-11 16:25 ` [PATCH 04/24] Enforce module signatures if the kernel is locked down David Howells
2018-04-11 16:25 ` [PATCH 05/24] Restrict /dev/{mem, kmem, port} when " David Howells
2018-04-11 16:25 ` [PATCH 06/24] kexec_load: Disable at runtime if " David Howells
2018-04-11 19:00   ` Eric W. Biederman
2018-04-11 20:09     ` Mimi Zohar
2018-04-12 11:38       ` Mimi Zohar
2018-04-11 20:05   ` David Howells
2018-04-11 16:25 ` [PATCH 07/24] hibernate: Disable when " David Howells
2018-04-13 20:22   ` Pavel Machek
2018-04-19 14:38   ` David Howells
2018-04-22 14:34     ` Andy Lutomirski
2018-04-26  7:26     ` Pavel Machek
2018-04-26  7:34       ` Rafael J. Wysocki
2018-04-26  8:20       ` Jiri Kosina
2018-05-23  8:46         ` joeyli
2018-04-11 16:25 ` [PATCH 08/24] uswsusp: " David Howells
2018-04-11 16:25 ` [PATCH 09/24] PCI: Lock down BAR access " David Howells
2018-04-11 16:25 ` [PATCH 10/24] x86: Lock down IO port " David Howells
2018-04-11 16:25 ` [PATCH 11/24] x86/msr: Restrict MSR " David Howells
2018-04-11 16:25 ` [PATCH 12/24] ACPI: Limit access to custom_method " David Howells
2018-04-11 16:26 ` [PATCH 13/24] acpi: Ignore acpi_rsdp kernel param when the kernel has been " David Howells
2018-04-11 16:26 ` [PATCH 14/24] acpi: Disable ACPI table override if the kernel is " David Howells
2018-04-11 16:26 ` [PATCH 15/24] acpi: Disable APEI error injection " David Howells
2018-04-11 16:26 ` [PATCH 16/24] Prohibit PCMCIA CIS storage when " David Howells
2018-04-11 16:26 ` [PATCH 17/24] Lock down TIOCSSERIAL David Howells
2018-04-11 16:26 ` [PATCH 18/24] Lock down module params that specify hardware parameters (eg. ioport) David Howells
2018-04-11 17:22   ` Randy Dunlap
2018-04-11 16:26 ` [PATCH 19/24] x86/mmiotrace: Lock down the testmmiotrace module David Howells
2018-04-11 16:26 ` [PATCH 20/24] Lock down /proc/kcore David Howells
2018-04-11 16:26 ` [PATCH 21/24] Lock down kprobes David Howells
2018-04-11 16:27 ` [PATCH 22/24] bpf: Restrict kernel image access functions when the kernel is locked down David Howells
2018-04-11 16:27 ` [PATCH 23/24] Lock down perf David Howells
2018-04-11 16:27 ` [PATCH 24/24] debugfs: Restrict debugfs when the kernel is locked down David Howells
2018-04-11 17:26   ` Randy Dunlap
2018-04-11 18:50   ` Eric W. Biederman
2018-04-11 19:54   ` Greg KH
2018-04-11 20:08   ` David Howells
2018-04-11 20:09   ` David Howells
2018-04-11 20:33     ` Greg KH
2018-04-12  2:54       ` Andy Lutomirski
2018-04-12  8:23         ` Greg KH
2018-04-12 14:19           ` Andy Lutomirski
2018-04-13 20:22   ` Pavel Machek
2018-04-19 14:35   ` David Howells
2018-05-10 11:01     ` Pavel Machek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=152346388583.4030.15146667041427303547.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=jmorris@namei.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-man@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).