All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Garrett <matthewgarrett@google.com>
To: jmorris@namei.org
Cc: linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org, dhowells@redhat.com,
	James Morris <james.l.morris@oracle.com>,
	Matthew Garrett <matthewgarrett@google.com>
Subject: [PATCH 01/27] Add the ability to lock down access to the running kernel image
Date: Mon, 25 Mar 2019 15:09:28 -0700	[thread overview]
Message-ID: <20190325220954.29054-2-matthewgarrett@google.com> (raw)
In-Reply-To: <20190325220954.29054-1-matthewgarrett@google.com>

From: David Howells <dhowells@redhat.com>

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 the loading of
modules that aren't validly signed with a key we recognise, fiddling with
MSR registers and disallowing hibernation.

Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <james.l.morris@oracle.com>
Signed-off-by: Matthew Garrett <matthewgarrett@google.com>
---
 include/linux/kernel.h   | 17 ++++++++++++
 include/linux/security.h |  9 +++++-
 security/Kconfig         | 15 ++++++++++
 security/Makefile        |  3 ++
 security/lock_down.c     | 60 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100644 security/lock_down.c

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 8f0e68e250a7..833bf32ce4e6 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -340,6 +340,23 @@ static inline void refcount_error_report(struct pt_regs *regs, const char *err)
 { }
 #endif
 
+#ifdef CONFIG_LOCK_DOWN_KERNEL
+extern bool __kernel_is_locked_down(const char *what, bool first);
+#else
+static inline bool __kernel_is_locked_down(const char *what, bool first)
+{
+	return false;
+}
+#endif
+
+#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;						\
+	})
+
 /* 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/include/linux/security.h b/include/linux/security.h
index 13537a49ae97..b290946341a4 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -1798,5 +1798,12 @@ static inline void security_bpf_prog_free(struct bpf_prog_aux *aux)
 #endif /* CONFIG_SECURITY */
 #endif /* CONFIG_BPF_SYSCALL */
 
-#endif /* ! __LINUX_SECURITY_H */
+#ifdef CONFIG_LOCK_DOWN_KERNEL
+extern void __init init_lockdown(void);
+#else
+static inline void __init init_lockdown(void)
+{
+}
+#endif
 
+#endif /* ! __LINUX_SECURITY_H */
diff --git a/security/Kconfig b/security/Kconfig
index 1d6463fb1450..47dc3403b5af 100644
--- a/security/Kconfig
+++ b/security/Kconfig
@@ -229,6 +229,21 @@ 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. If lockdown support is enabled
+	  and activated, the kernel will impose additional restrictions
+	  intended to prevent uid 0 from being able to modify the running
+	  kernel. This may break userland applications that rely on low-level
+	  access to hardware.
+
+config LOCK_DOWN_KERNEL_FORCE
+        bool "Enable kernel lockdown mode automatically"
+        depends on LOCK_DOWN_KERNEL
+        help
+          Enable the kernel lock down functionality automatically at boot.
+
 source "security/selinux/Kconfig"
 source "security/smack/Kconfig"
 source "security/tomoyo/Kconfig"
diff --git a/security/Makefile b/security/Makefile
index c598b904938f..5ff090149c88 100644
--- a/security/Makefile
+++ b/security/Makefile
@@ -32,3 +32,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..18d8776a4d02
--- /dev/null
+++ b/security/lock_down.c
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0
+/* 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/security.h>
+#include <linux/export.h>
+
+static __ro_after_init bool kernel_locked_down;
+
+/*
+ * Put the kernel into lock-down mode.
+ */
+static void __init lock_kernel_down(const char *where)
+{
+	if (!kernel_locked_down) {
+		kernel_locked_down = true;
+		pr_notice("Kernel is locked down from %s; see man kernel_lockdown.7\n",
+			  where);
+	}
+}
+
+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_FORCE
+	lock_kernel_down("Kernel configuration");
+#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 is restricted; see man kernel_lockdown.7\n",
+			  what);
+	return kernel_locked_down;
+}
+EXPORT_SYMBOL(__kernel_is_locked_down);
-- 
2.21.0.392.gf8f6787159e-goog


  reply	other threads:[~2019-03-25 22:12 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-25 22:09 [PULL REQUEST] Lockdown patches for 5.2 Matthew Garrett
2019-03-25 22:09 ` Matthew Garrett [this message]
2019-03-26  5:30   ` [PATCH 01/27] Add the ability to lock down access to the running kernel image Matthew Garrett
2019-03-25 22:09 ` [PATCH 02/27] Enforce module signatures if the kernel is locked down Matthew Garrett
2019-03-25 22:09 ` [PATCH 03/27] Restrict /dev/{mem,kmem,port} when " Matthew Garrett
2019-03-25 22:09 ` [PATCH 04/27] kexec_load: Disable at runtime if " Matthew Garrett
2019-03-25 22:09   ` Matthew Garrett
2019-03-25 22:09 ` [PATCH 05/27] Copy secure_boot flag in boot params across kexec reboot Matthew Garrett
2019-03-25 22:09   ` Matthew Garrett
2019-03-25 22:09 ` [PATCH 06/27] kexec_file: split KEXEC_VERIFY_SIG into KEXEC_SIG and KEXEC_SIG_FORCE Matthew Garrett
2019-03-25 22:09   ` Matthew Garrett
2019-03-25 22:09 ` [PATCH 07/27] kexec_file: Restrict at runtime if the kernel is locked down Matthew Garrett
2019-03-25 22:09   ` Matthew Garrett
2019-03-25 22:09 ` [PATCH 08/27] hibernate: Disable when " Matthew Garrett
2019-03-25 22:09 ` [PATCH 09/27] uswsusp: " Matthew Garrett
2019-03-25 22:09 ` [PATCH 10/27] PCI: Lock down BAR access " Matthew Garrett
2019-03-25 22:09 ` [PATCH 11/27] x86: Lock down IO port " Matthew Garrett
2019-03-25 22:09 ` [PATCH 12/27] x86/msr: Restrict MSR " Matthew Garrett
2019-03-25 23:40   ` Thomas Gleixner
2019-03-25 22:09 ` [PATCH 13/27] ACPI: Limit access to custom_method " Matthew Garrett
2019-03-25 22:09 ` [PATCH 14/27] acpi: Ignore acpi_rsdp kernel param when the kernel has been " Matthew Garrett
2019-03-25 22:09 ` [PATCH 15/27] acpi: Disable ACPI table override if the kernel is " Matthew Garrett
2019-03-25 22:09   ` Matthew Garrett
2019-03-25 22:09 ` [PATCH 16/27] acpi: Disable APEI error injection " Matthew Garrett
2019-03-25 22:09 ` [PATCH 17/27] Prohibit PCMCIA CIS storage when " Matthew Garrett
2019-03-25 22:09 ` [PATCH 18/27] Lock down TIOCSSERIAL Matthew Garrett
2019-03-25 22:09 ` [PATCH 19/27] Lock down module params that specify hardware parameters (eg. ioport) Matthew Garrett
2019-03-25 22:09 ` [PATCH 20/27] x86/mmiotrace: Lock down the testmmiotrace module Matthew Garrett
2019-03-25 23:35   ` Steven Rostedt
2019-03-25 22:09 ` [PATCH 21/27] Lock down /proc/kcore Matthew Garrett
2019-03-25 22:09 ` [PATCH 22/27] Lock down kprobes Matthew Garrett
2019-03-26 12:29   ` Masami Hiramatsu
2019-03-26 17:41     ` Matthew Garrett
2019-03-26 22:47       ` Masami Hiramatsu
2019-03-25 22:09 ` [PATCH 23/27] bpf: Restrict kernel image access functions when the kernel is locked down Matthew Garrett
2019-03-25 23:42   ` Stephen Hemminger
2019-03-25 23:59     ` Stephen Hemminger
2019-03-26  0:00     ` Daniel Borkmann
2019-03-26 13:54       ` Jordan Glover
2019-03-26  0:10     ` Andy Lutomirski
2019-03-26 18:57       ` James Morris
2019-03-26 19:22         ` Andy Lutomirski
2019-03-28  3:15           ` James Morris
2019-03-28 18:07             ` Matthew Garrett
2019-03-28 19:23               ` James Morris
2019-03-28 20:08                 ` Matthew Garrett
2019-03-26 20:19         ` Matthew Garrett
2019-03-25 22:09 ` [PATCH 24/27] Lock down perf Matthew Garrett
2019-03-25 22:09 ` [PATCH 25/27] debugfs: Restrict debugfs when the kernel is locked down Matthew Garrett
2019-03-26  0:31   ` Greg Kroah-Hartman
2019-03-26  0:38     ` Matthew Garrett
2019-03-26  0:43       ` Greg Kroah-Hartman
2019-03-25 22:09 ` [PATCH 26/27] lockdown: Print current->comm in restriction messages Matthew Garrett
2019-03-25 22:09 ` [PATCH 27/27] kexec: Allow kexec_file() with appropriate IMA policy when locked down Matthew Garrett
2019-03-26 15:33   ` Mimi Zohar
  -- strict thread matches above, loose matches on Subject: below --
2019-03-06 23:58 [PULL REQUEST] Kernel lockdown patches for 5.2 Matthew Garrett
2019-03-06 23:58 ` [PATCH 01/27] Add the ability to lock down access to the running kernel image Matthew Garrett
2019-02-28 21:28 [PULL REQUEST] Lock down patches Matthew Garrett
2019-02-28 22:44 ` [PATCH 01/27] Add the ability to lock down access to the running kernel image Matthew Garrett
2019-02-28 23:10 ` Matthew Garrett
2019-02-28 23:11 ` Matthew Garrett
2019-02-28 23:11 ` Matthew Garrett
2017-10-19 14:50 [PATCH 00/27] security, efi: Add kernel lockdown David Howells
2017-10-19 14:50 ` [PATCH 01/27] Add the ability to lock down access to the running kernel image David Howells
2017-10-19 14:50   ` David Howells
2017-10-20 23:19   ` James Morris
2017-10-20 23:19     ` James Morris

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=20190325220954.29054-2-matthewgarrett@google.com \
    --to=matthewgarrett@google.com \
    --cc=dhowells@redhat.com \
    --cc=james.l.morris@oracle.com \
    --cc=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.