nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0373-kern-Add-lockdown-support.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Javier Martinez Canillas <javierm@redhat.com>
80913e
Date: Fri, 19 Feb 2021 10:33:54 +0100
80913e
Subject: [PATCH] kern: Add lockdown support
80913e
80913e
When the GRUB starts on a secure boot platform, some commands can be
80913e
used to subvert the protections provided by the verification mechanism and
80913e
could lead to booting untrusted system.
80913e
80913e
To prevent that situation, allow GRUB to be locked down. That way the code
80913e
may check if GRUB has been locked down and further restrict the commands
80913e
that are registered or what subset of their functionality could be used.
80913e
80913e
The lockdown support adds the following components:
80913e
80913e
* The grub_lockdown() function which can be used to lockdown GRUB if,
80913e
  e.g., UEFI Secure Boot is enabled.
80913e
80913e
* The grub_is_lockdown() function which can be used to check if the GRUB
80913e
  was locked down.
80913e
80913e
* A verifier that flags OS kernels, the GRUB modules, Device Trees and ACPI
80913e
  tables as GRUB_VERIFY_FLAGS_DEFER_AUTH to defer verification to other
80913e
  verifiers. These files are only successfully verified if another registered
80913e
  verifier returns success. Otherwise, the whole verification process fails.
80913e
80913e
  For example, PE/COFF binaries verification can be done by the shim_lock
80913e
  verifier which validates the signatures using the shim_lock protocol.
80913e
  However, the verification is not deferred directly to the shim_lock verifier.
80913e
  The shim_lock verifier is hooked into the verification process instead.
80913e
80913e
* A set of grub_{command,extcmd}_lockdown functions that can be used by
80913e
  code registering command handlers, to only register unsafe commands if
80913e
  the GRUB has not been locked down.
80913e
80913e
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
---
80913e
 grub-core/Makefile.core.def |  1 +
80913e
 grub-core/commands/extcmd.c | 23 +++++++++++
80913e
 grub-core/kern/command.c    | 24 ++++++++++++
80913e
 grub-core/kern/lockdown.c   | 93 +++++++++++++++++++++++++++++++++++++++++++++
80913e
 include/grub/command.h      |  5 +++
80913e
 include/grub/extcmd.h       |  7 ++++
80913e
 include/grub/lockdown.h     | 44 +++++++++++++++++++++
80913e
 conf/Makefile.common        |  2 +
80913e
 docs/grub-dev.texi          | 27 +++++++++++++
80913e
 docs/grub.texi              |  8 ++++
80913e
 grub-core/Makefile.am       |  5 ++-
80913e
 11 files changed, 238 insertions(+), 1 deletion(-)
80913e
 create mode 100644 grub-core/kern/lockdown.c
80913e
 create mode 100644 include/grub/lockdown.h
80913e
80913e
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
80913e
index 8914083d13f..02fbecd4b81 100644
80913e
--- a/grub-core/Makefile.core.def
80913e
+++ b/grub-core/Makefile.core.def
80913e
@@ -197,6 +197,7 @@ kernel = {
80913e
   efi = term/efi/console.c;
80913e
   efi = kern/acpi.c;
80913e
   efi = kern/efi/acpi.c;
80913e
+  efi = kern/lockdown.c;
80913e
   efi = lib/envblk.c;
80913e
   efi = kern/efi/tpm.c;
80913e
   i386_coreboot = kern/i386/pc/acpi.c;
80913e
diff --git a/grub-core/commands/extcmd.c b/grub-core/commands/extcmd.c
80913e
index 69574e2b05b..90a5ca24a64 100644
80913e
--- a/grub-core/commands/extcmd.c
80913e
+++ b/grub-core/commands/extcmd.c
80913e
@@ -19,6 +19,7 @@
80913e
 
80913e
 #include <grub/mm.h>
80913e
 #include <grub/list.h>
80913e
+#include <grub/lockdown.h>
80913e
 #include <grub/misc.h>
80913e
 #include <grub/extcmd.h>
80913e
 #include <grub/script_sh.h>
80913e
@@ -110,6 +111,28 @@ grub_register_extcmd (const char *name, grub_extcmd_func_t func,
80913e
 				    summary, description, parser, 1);
80913e
 }
80913e
 
80913e
+static grub_err_t
80913e
+grub_extcmd_lockdown (grub_extcmd_context_t ctxt __attribute__ ((unused)),
80913e
+                      int argc __attribute__ ((unused)),
80913e
+                      char **argv __attribute__ ((unused)))
80913e
+{
80913e
+  return grub_error (GRUB_ERR_ACCESS_DENIED,
80913e
+                     N_("%s: the command is not allowed when lockdown is enforced"),
80913e
+                     ctxt->extcmd->cmd->name);
80913e
+}
80913e
+
80913e
+grub_extcmd_t
80913e
+grub_register_extcmd_lockdown (const char *name, grub_extcmd_func_t func,
80913e
+                               grub_command_flags_t flags, const char *summary,
80913e
+                               const char *description,
80913e
+                               const struct grub_arg_option *parser)
80913e
+{
80913e
+  if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
80913e
+    func = grub_extcmd_lockdown;
80913e
+
80913e
+  return grub_register_extcmd (name, func, flags, summary, description, parser);
80913e
+}
80913e
+
80913e
 void
80913e
 grub_unregister_extcmd (grub_extcmd_t ext)
80913e
 {
80913e
diff --git a/grub-core/kern/command.c b/grub-core/kern/command.c
80913e
index acd72187992..4aabcd4b5f9 100644
80913e
--- a/grub-core/kern/command.c
80913e
+++ b/grub-core/kern/command.c
80913e
@@ -17,6 +17,7 @@
80913e
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
  */
80913e
 
80913e
+#include <grub/lockdown.h>
80913e
 #include <grub/mm.h>
80913e
 #include <grub/command.h>
80913e
 
80913e
@@ -77,6 +78,29 @@ grub_register_command_prio (const char *name,
80913e
   return cmd;
80913e
 }
80913e
 
80913e
+static grub_err_t
80913e
+grub_cmd_lockdown (grub_command_t cmd __attribute__ ((unused)),
80913e
+                   int argc __attribute__ ((unused)),
80913e
+                   char **argv __attribute__ ((unused)))
80913e
+
80913e
+{
80913e
+  return grub_error (GRUB_ERR_ACCESS_DENIED,
80913e
+                     N_("%s: the command is not allowed when lockdown is enforced"),
80913e
+                     cmd->name);
80913e
+}
80913e
+
80913e
+grub_command_t
80913e
+grub_register_command_lockdown (const char *name,
80913e
+                                grub_command_func_t func,
80913e
+                                const char *summary,
80913e
+                                const char *description)
80913e
+{
80913e
+  if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
80913e
+    func = grub_cmd_lockdown;
80913e
+
80913e
+  return grub_register_command_prio (name, func, summary, description, 0);
80913e
+}
80913e
+
80913e
 void
80913e
 grub_unregister_command (grub_command_t cmd)
80913e
 {
80913e
diff --git a/grub-core/kern/lockdown.c b/grub-core/kern/lockdown.c
80913e
new file mode 100644
80913e
index 00000000000..f87ddaeb1ee
80913e
--- /dev/null
80913e
+++ b/grub-core/kern/lockdown.c
80913e
@@ -0,0 +1,93 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2020  Free Software Foundation, Inc.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ *
80913e
+ */
80913e
+
80913e
+#include <grub/dl.h>
80913e
+#include <grub/file.h>
80913e
+#include <grub/lockdown.h>
80913e
+
80913e
+/* There is no verifier framework in grub 2.02 */
80913e
+#if 0
80913e
+#include <grub/verify.h>
80913e
+#endif
80913e
+
80913e
+static int lockdown = GRUB_LOCKDOWN_DISABLED;
80913e
+
80913e
+/* There is no verifier framework in grub 2.02 */
80913e
+#if 0
80913e
+static grub_err_t
80913e
+lockdown_verifier_init (grub_file_t io __attribute__ ((unused)),
80913e
+                    enum grub_file_type type,
80913e
+                    void **context __attribute__ ((unused)),
80913e
+                    enum grub_verify_flags *flags)
80913e
+{
80913e
+  *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
80913e
+
80913e
+  switch (type & GRUB_FILE_TYPE_MASK)
80913e
+    {
80913e
+    case GRUB_FILE_TYPE_GRUB_MODULE:
80913e
+    case GRUB_FILE_TYPE_LINUX_KERNEL:
80913e
+    case GRUB_FILE_TYPE_MULTIBOOT_KERNEL:
80913e
+    case GRUB_FILE_TYPE_XEN_HYPERVISOR:
80913e
+    case GRUB_FILE_TYPE_BSD_KERNEL:
80913e
+    case GRUB_FILE_TYPE_XNU_KERNEL:
80913e
+    case GRUB_FILE_TYPE_PLAN9_KERNEL:
80913e
+    case GRUB_FILE_TYPE_NTLDR:
80913e
+    case GRUB_FILE_TYPE_TRUECRYPT:
80913e
+    case GRUB_FILE_TYPE_FREEDOS:
80913e
+    case GRUB_FILE_TYPE_PXECHAINLOADER:
80913e
+    case GRUB_FILE_TYPE_PCCHAINLOADER:
80913e
+    case GRUB_FILE_TYPE_COREBOOT_CHAINLOADER:
80913e
+    case GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE:
80913e
+    case GRUB_FILE_TYPE_ACPI_TABLE:
80913e
+    case GRUB_FILE_TYPE_DEVICE_TREE_IMAGE:
80913e
+      *flags = GRUB_VERIFY_FLAGS_DEFER_AUTH;
80913e
+
80913e
+      /* Fall through. */
80913e
+
80913e
+    default:
80913e
+      return GRUB_ERR_NONE;
80913e
+    }
80913e
+}
80913e
+
80913e
+struct grub_file_verifier lockdown_verifier =
80913e
+  {
80913e
+    .name = "lockdown_verifier",
80913e
+    .init = lockdown_verifier_init,
80913e
+  };
80913e
+#endif
80913e
+
80913e
+void
80913e
+grub_lockdown (void)
80913e
+{
80913e
+  lockdown = GRUB_LOCKDOWN_ENABLED;
80913e
+
80913e
+  /*
80913e
+   * XXX: The lockdown verifier doesn't make sense until
80913e
+   * GRUB has moved to the shim_lock verifier.
80913e
+   */
80913e
+#if 0
80913e
+  grub_verifier_register (&lockdown_verifier);
80913e
+#endif
80913e
+}
80913e
+
80913e
+int
80913e
+grub_is_lockdown (void)
80913e
+{
80913e
+  return lockdown;
80913e
+}
80913e
diff --git a/include/grub/command.h b/include/grub/command.h
80913e
index eee4e847ee4..2a6f7f84697 100644
80913e
--- a/include/grub/command.h
80913e
+++ b/include/grub/command.h
80913e
@@ -86,6 +86,11 @@ EXPORT_FUNC(grub_register_command_prio) (const char *name,
80913e
 					 const char *summary,
80913e
 					 const char *description,
80913e
 					 int prio);
80913e
+grub_command_t
80913e
+EXPORT_FUNC(grub_register_command_lockdown) (const char *name,
80913e
+                                             grub_command_func_t func,
80913e
+                                             const char *summary,
80913e
+                                             const char *description);
80913e
 void EXPORT_FUNC(grub_unregister_command) (grub_command_t cmd);
80913e
 
80913e
 static inline grub_command_t
80913e
diff --git a/include/grub/extcmd.h b/include/grub/extcmd.h
80913e
index 19fe592669e..fe9248b8bb6 100644
80913e
--- a/include/grub/extcmd.h
80913e
+++ b/include/grub/extcmd.h
80913e
@@ -62,6 +62,13 @@ grub_extcmd_t EXPORT_FUNC(grub_register_extcmd) (const char *name,
80913e
 						 const char *description,
80913e
 						 const struct grub_arg_option *parser);
80913e
 
80913e
+grub_extcmd_t EXPORT_FUNC(grub_register_extcmd_lockdown) (const char *name,
80913e
+                                                          grub_extcmd_func_t func,
80913e
+                                                          grub_command_flags_t flags,
80913e
+                                                          const char *summary,
80913e
+                                                          const char *description,
80913e
+                                                          const struct grub_arg_option *parser);
80913e
+
80913e
 grub_extcmd_t EXPORT_FUNC(grub_register_extcmd_prio) (const char *name,
80913e
 						      grub_extcmd_func_t func,
80913e
 						      grub_command_flags_t flags,
80913e
diff --git a/include/grub/lockdown.h b/include/grub/lockdown.h
80913e
new file mode 100644
80913e
index 00000000000..40531fa823b
80913e
--- /dev/null
80913e
+++ b/include/grub/lockdown.h
80913e
@@ -0,0 +1,44 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2020  Free Software Foundation, Inc.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ */
80913e
+
80913e
+#ifndef GRUB_LOCKDOWN_H
80913e
+#define GRUB_LOCKDOWN_H 1
80913e
+
80913e
+#include <grub/symbol.h>
80913e
+
80913e
+#define GRUB_LOCKDOWN_DISABLED       0
80913e
+#define GRUB_LOCKDOWN_ENABLED        1
80913e
+
80913e
+#ifdef GRUB_MACHINE_EFI
80913e
+extern void
80913e
+EXPORT_FUNC (grub_lockdown) (void);
80913e
+extern int
80913e
+EXPORT_FUNC (grub_is_lockdown) (void);
80913e
+#else
80913e
+static inline void
80913e
+grub_lockdown (void)
80913e
+{
80913e
+}
80913e
+
80913e
+static inline int
80913e
+grub_is_lockdown (void)
80913e
+{
80913e
+  return GRUB_LOCKDOWN_DISABLED;
80913e
+}
80913e
+#endif
80913e
+#endif /* ! GRUB_LOCKDOWN_H */
80913e
diff --git a/conf/Makefile.common b/conf/Makefile.common
80913e
index b93879804c0..521cdda1f5a 100644
80913e
--- a/conf/Makefile.common
80913e
+++ b/conf/Makefile.common
80913e
@@ -85,7 +85,9 @@ CPPFLAGS_PARTTOOL_LIST = -Dgrub_parttool_register=PARTTOOL_LIST_MARKER
80913e
 CPPFLAGS_TERMINAL_LIST = '-Dgrub_term_register_input(...)=INPUT_TERMINAL_LIST_MARKER(__VA_ARGS__)'
80913e
 CPPFLAGS_TERMINAL_LIST += '-Dgrub_term_register_output(...)=OUTPUT_TERMINAL_LIST_MARKER(__VA_ARGS__)'
80913e
 CPPFLAGS_COMMAND_LIST = '-Dgrub_register_command(...)=COMMAND_LIST_MARKER(__VA_ARGS__)'
80913e
+CPPFLAGS_COMMAND_LIST += '-Dgrub_register_command_lockdown(...)=COMMAND_LOCKDOWN_LIST_MARKER(__VA_ARGS__)'
80913e
 CPPFLAGS_COMMAND_LIST += '-Dgrub_register_extcmd(...)=EXTCOMMAND_LIST_MARKER(__VA_ARGS__)'
80913e
+CPPFLAGS_COMMAND_LIST += '-Dgrub_register_extcmd_lockdown(...)=EXTCOMMAND_LOCKDOWN_LIST_MARKER(__VA_ARGS__)'
80913e
 CPPFLAGS_COMMAND_LIST += '-Dgrub_register_command_p1(...)=P1COMMAND_LIST_MARKER(__VA_ARGS__)'
80913e
 CPPFLAGS_FDT_LIST := '-Dgrub_fdtbus_register(...)=FDT_DRIVER_LIST_MARKER(__VA_ARGS__)'
80913e
 CPPFLAGS_MARKER = $(CPPFLAGS_FS_LIST) $(CPPFLAGS_VIDEO_LIST) \
80913e
diff --git a/docs/grub-dev.texi b/docs/grub-dev.texi
80913e
index 3ce827ab726..421dd410e50 100644
80913e
--- a/docs/grub-dev.texi
80913e
+++ b/docs/grub-dev.texi
80913e
@@ -84,6 +84,7 @@ This edition documents version @value{VERSION}.
80913e
 * Video Subsystem::
80913e
 * PFF2 Font File Format::
80913e
 * Graphical Menu Software Design::
80913e
+* Lockdown framework::
80913e
 * Copying This Manual::         Copying This Manual
80913e
 * Index::
80913e
 @end menu
80913e
@@ -1949,6 +1950,32 @@ the graphics mode that was in use before @code{grub_video_setup()} was called
80913e
 might fix some of the problems.
80913e
 
80913e
 
80913e
+@node Lockdown framework
80913e
+@chapter Lockdown framework
80913e
+
80913e
+The GRUB can be locked down, which is a restricted mode where some operations
80913e
+are not allowed. For instance, some commands cannot be used when the GRUB is
80913e
+locked down.
80913e
+
80913e
+The function
80913e
+@code{grub_lockdown()} is used to lockdown GRUB and the function
80913e
+@code{grub_is_lockdown()} function can be used to check whether lockdown is
80913e
+enabled or not. When enabled, the function returns @samp{GRUB_LOCKDOWN_ENABLED}
80913e
+and @samp{GRUB_LOCKDOWN_DISABLED} when is not enabled.
80913e
+
80913e
+The following functions can be used to register the commands that can only be
80913e
+used when lockdown is disabled:
80913e
+
80913e
+@itemize
80913e
+
80913e
+@item @code{grub_cmd_lockdown()} registers command which should not run when the
80913e
+GRUB is in lockdown mode.
80913e
+
80913e
+@item @code{grub_cmd_lockdown()} registers extended command which should not run
80913e
+when the GRUB is in lockdown mode.
80913e
+
80913e
+@end itemize
80913e
+
80913e
 @node Copying This Manual
80913e
 @appendix Copying This Manual
80913e
 
80913e
diff --git a/docs/grub.texi b/docs/grub.texi
80913e
index 97f0f47e082..f957535dbea 100644
80913e
--- a/docs/grub.texi
80913e
+++ b/docs/grub.texi
80913e
@@ -5687,6 +5687,7 @@ environment variables and commands are listed in the same order.
80913e
 * Using GPG-style digital signatures:: Booting digitally signed code
80913e
 * Using appended signatures::          An alternative approach to booting digitally signed code
80913e
 * Signing GRUB itself::                Ensuring the integrity of the GRUB core image
80913e
+* Lockdown::                           Lockdown when booting on a secure setup
80913e
 @end menu
80913e
 
80913e
 @node Authentication and authorisation
80913e
@@ -5977,6 +5978,13 @@ As with UEFI secure boot, it is necessary to build in the required modules,
80913e
 or sign them separately.
80913e
 
80913e
 
80913e
+@node Lockdown
80913e
+@section Lockdown when booting on a secure setup
80913e
+
80913e
+The GRUB can be locked down when booted on a secure boot environment, for example
80913e
+if the UEFI secure boot is enabled. On a locked down configuration, the GRUB will
80913e
+be restricted and some operations/commands cannot be executed.
80913e
+
80913e
 @node Platform limitations
80913e
 @chapter Platform limitations
80913e
 
80913e
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
80913e
index 4062652506d..a6f1b0dcd06 100644
80913e
--- a/grub-core/Makefile.am
80913e
+++ b/grub-core/Makefile.am
80913e
@@ -82,6 +82,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/fs.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i18n.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/kernel.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/list.h
80913e
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lockdown.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/misc.h
80913e
 if COND_emu
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/compiler-rt-emu.h
80913e
@@ -350,8 +351,10 @@ command.lst: $(MARKER_FILES)
80913e
 	  b=`basename $$pp .marker`; \
80913e
 	  sed -n \
80913e
 	    -e "/EXTCOMMAND_LIST_MARKER *( *\"/{s/.*( *\"\([^\"]*\)\".*/*\1: $$b/;p;}" \
80913e
+	    -e "/EXTCOMMAND_LOCKDOWN_LIST_MARKER *( *\"/{s/.*( *\"\([^\"]*\)\".*/*\1: $$b/;p;}" \
80913e
 	    -e "/P1COMMAND_LIST_MARKER *( *\"/{s/.*( *\"\([^\"]*\)\".*/*\1: $$b/;p;}" \
80913e
-	    -e "/COMMAND_LIST_MARKER *( *\"/{s/.*( *\"\([^\"]*\)\".*/\1: $$b/;p;}" $$pp; \
80913e
+	    -e "/COMMAND_LIST_MARKER *( *\"/{s/.*( *\"\([^\"]*\)\".*/\1: $$b/;p;}" \
80913e
+	    -e "/COMMAND_LOCKDOWN_LIST_MARKER *( *\"/{s/.*( *\"\([^\"]*\)\".*/\1: $$b/;p;}" $$pp; \
80913e
 	done) | sort -u > $@
80913e
 platform_DATA += command.lst
80913e
 CLEANFILES += command.lst