nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0466-kern-efi-Add-initial-stack-protector-implementation.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Chris Coulson <chris.coulson@canonical.com>
80913e
Date: Fri, 19 Feb 2021 13:53:45 +0100
80913e
Subject: [PATCH] kern/efi: Add initial stack protector implementation
80913e
80913e
It works only on UEFI platforms but can be quite easily extended to
80913e
others architectures and platforms if needed.
80913e
80913e
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
80913e
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
Reviewed-by: Marco A Benatto <mbenatto@redhat.com>
80913e
Reviewed-by: Javier Martinez Canillas <javierm@redhat.com>
80913e
---
80913e
 configure.ac                   | 44 ++++++++++++++++++++++++++++++----
80913e
 grub-core/kern/efi/init.c      | 54 ++++++++++++++++++++++++++++++++++++++++++
80913e
 include/grub/efi/api.h         | 19 +++++++++++++++
80913e
 include/grub/stack_protector.h | 30 +++++++++++++++++++++++
80913e
 acinclude.m4                   | 38 +++++++++++++++++++++++++++--
80913e
 grub-core/Makefile.am          |  1 +
80913e
 6 files changed, 179 insertions(+), 7 deletions(-)
80913e
 create mode 100644 include/grub/stack_protector.h
80913e
80913e
diff --git a/configure.ac b/configure.ac
80913e
index 0059b938a3a..f59a7b86c51 100644
80913e
--- a/configure.ac
80913e
+++ b/configure.ac
80913e
@@ -1330,12 +1330,41 @@ fi]
80913e
 
80913e
 CFLAGS="$TARGET_CFLAGS"
80913e
 
80913e
-# Smashing stack protector.
80913e
+# Stack smashing protector.
80913e
 grub_CHECK_STACK_PROTECTOR
80913e
-# Need that, because some distributions ship compilers that include
80913e
-# `-fstack-protector' in the default specs.
80913e
-if test "x$ssp_possible" = xyes; then
80913e
-  TARGET_CFLAGS="$TARGET_CFLAGS -fno-stack-protector"
80913e
+AC_ARG_ENABLE([stack-protector],
80913e
+	      AS_HELP_STRING([--enable-stack-protector],
80913e
+			     [enable the stack protector]),
80913e
+	      [],
80913e
+	      [enable_stack_protector=no])
80913e
+if test "x$enable_stack_protector" = xno; then
80913e
+  if test "x$ssp_possible" = xyes; then
80913e
+    # Need that, because some distributions ship compilers that include
80913e
+    # `-fstack-protector' in the default specs.
80913e
+    TARGET_CFLAGS="$TARGET_CFLAGS -fno-stack-protector"
80913e
+  fi
80913e
+elif test "x$platform" != xefi; then
80913e
+  AC_MSG_ERROR([--enable-stack-protector is only supported on EFI platforms])
80913e
+elif test "x$ssp_global_possible" != xyes; then
80913e
+  AC_MSG_ERROR([--enable-stack-protector is not supported (compiler doesn't support -mstack-protector-guard=global)])
80913e
+else
80913e
+  TARGET_CFLAGS="$TARGET_CFLAGS -mstack-protector-guard=global"
80913e
+  if test "x$enable_stack_protector" = xyes; then
80913e
+    if test "x$ssp_possible" != xyes; then
80913e
+      AC_MSG_ERROR([--enable-stack-protector is not supported (compiler doesn't support -fstack-protector)])
80913e
+    fi
80913e
+    TARGET_CFLAGS="$TARGET_CFLAGS -fstack-protector"
80913e
+  elif test "x$enable_stack_protector" = xstrong; then
80913e
+    if test "x$ssp_strong_possible" != xyes; then
80913e
+      AC_MSG_ERROR([--enable-stack-protector=strong is not supported (compiler doesn't support -fstack-protector-strong)])
80913e
+    fi
80913e
+    TARGET_CFLAGS="$TARGET_CFLAGS -fstack-protector-strong"
80913e
+  else
80913e
+    # Note, -fstack-protector-all requires that the protector is disabled for
80913e
+    # functions that appear in the call stack when the canary is initialized.
80913e
+    AC_MSG_ERROR([invalid value $enable_stack_protector for --enable-stack-protector])
80913e
+  fi
80913e
+  TARGET_CPPFLAGS="$TARGET_CPPFLAGS -DGRUB_STACK_PROTECTOR=1"
80913e
 fi
80913e
 
80913e
 CFLAGS="$TARGET_CFLAGS"
80913e
@@ -2247,5 +2276,10 @@ echo "Without liblzma (no support for XZ-compressed mips images) ($liblzma_excus
80913e
 else
80913e
 echo "With liblzma from $LIBLZMA (support for XZ-compressed mips images)"
80913e
 fi
80913e
+if test "x$enable_stack_protector" != xno; then
80913e
+echo "With stack smashing protector: Yes"
80913e
+else
80913e
+echo "With stack smashing protector: No"
80913e
+fi
80913e
 echo "*******************************************************"
80913e
 ]
80913e
diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c
80913e
index 97bf36906a4..501608f743e 100644
80913e
--- a/grub-core/kern/efi/init.c
80913e
+++ b/grub-core/kern/efi/init.c
80913e
@@ -28,6 +28,58 @@
80913e
 #include <grub/mm.h>
80913e
 #include <grub/kernel.h>
80913e
 #include <grub/lib/envblk.h>
80913e
+#include <grub/stack_protector.h>
80913e
+
80913e
+#ifdef GRUB_STACK_PROTECTOR
80913e
+
80913e
+static grub_efi_guid_t rng_protocol_guid = GRUB_EFI_RNG_PROTOCOL_GUID;
80913e
+
80913e
+/*
80913e
+ * Don't put this on grub_efi_init()'s local stack to avoid it
80913e
+ * getting a stack check.
80913e
+ */
80913e
+static grub_efi_uint8_t stack_chk_guard_buf[32];
80913e
+
80913e
+grub_addr_t __stack_chk_guard;
80913e
+
80913e
+void __attribute__ ((noreturn))
80913e
+__stack_chk_fail (void)
80913e
+{
80913e
+  /*
80913e
+   * Assume it's not safe to call into EFI Boot Services. Sorry, that
80913e
+   * means no console message here.
80913e
+   */
80913e
+  do
80913e
+    {
80913e
+      /* Do not optimize out the loop. */
80913e
+      asm volatile ("");
80913e
+    }
80913e
+  while (1);
80913e
+}
80913e
+
80913e
+static void
80913e
+stack_protector_init (void)
80913e
+{
80913e
+  grub_efi_rng_protocol_t *rng;
80913e
+
80913e
+  /* Set up the stack canary. Make errors here non-fatal for now. */
80913e
+  rng = grub_efi_locate_protocol (&rng_protocol_guid, NULL);
80913e
+  if (rng != NULL)
80913e
+    {
80913e
+      grub_efi_status_t status;
80913e
+
80913e
+      status = efi_call_4 (rng->get_rng, rng, NULL, sizeof (stack_chk_guard_buf),
80913e
+			   stack_chk_guard_buf);
80913e
+      if (status == GRUB_EFI_SUCCESS)
80913e
+	grub_memcpy (&__stack_chk_guard, stack_chk_guard_buf, sizeof (__stack_chk_guard));
80913e
+    }
80913e
+}
80913e
+#else
80913e
+static void
80913e
+stack_protector_init (void)
80913e
+{
80913e
+}
80913e
+#endif
80913e
 
80913e
 grub_addr_t grub_modbase;
80913e
 
80913e
@@ -92,6 +144,8 @@ grub_efi_init (void)
80913e
      messages.  */
80913e
   grub_console_init ();
80913e
 
80913e
+  stack_protector_init ();
80913e
+
80913e
   /* Initialize the memory management system.  */
80913e
   grub_efi_mm_init ();
80913e
 
80913e
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
80913e
index a092fddb629..37e7b162874 100644
80913e
--- a/include/grub/efi/api.h
80913e
+++ b/include/grub/efi/api.h
80913e
@@ -344,6 +344,11 @@
80913e
       { 0x89, 0x29, 0x48, 0xbc, 0xd9, 0x0a, 0xd3, 0x1a } \
80913e
   }
80913e
 
80913e
+#define GRUB_EFI_RNG_PROTOCOL_GUID \
80913e
+  { 0x3152bca5, 0xeade, 0x433d, \
80913e
+    { 0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 0x44 } \
80913e
+  }
80913e
+
80913e
 struct grub_efi_sal_system_table
80913e
 {
80913e
   grub_uint32_t signature;
80913e
@@ -2067,6 +2072,20 @@ struct grub_efi_ip6_config_manual_address {
80913e
 };
80913e
 typedef struct grub_efi_ip6_config_manual_address grub_efi_ip6_config_manual_address_t;
80913e
 
80913e
+typedef grub_efi_guid_t grub_efi_rng_algorithm_t;
80913e
+
80913e
+struct grub_efi_rng_protocol
80913e
+{
80913e
+  grub_efi_status_t (*get_info) (struct grub_efi_rng_protocol *this,
80913e
+				 grub_efi_uintn_t *rng_algorithm_list_size,
80913e
+				 grub_efi_rng_algorithm_t *rng_algorithm_list);
80913e
+  grub_efi_status_t (*get_rng) (struct grub_efi_rng_protocol *this,
80913e
+				grub_efi_rng_algorithm_t *rng_algorithm,
80913e
+				grub_efi_uintn_t rng_value_length,
80913e
+				grub_efi_uint8_t *rng_value);
80913e
+};
80913e
+typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t;
80913e
+
80913e
 #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
80913e
   || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__)
80913e
 
80913e
diff --git a/include/grub/stack_protector.h b/include/grub/stack_protector.h
80913e
new file mode 100644
80913e
index 00000000000..c88dc00b5f9
80913e
--- /dev/null
80913e
+++ b/include/grub/stack_protector.h
80913e
@@ -0,0 +1,30 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2021  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_STACK_PROTECTOR_H
80913e
+#define GRUB_STACK_PROTECTOR_H	1
80913e
+
80913e
+#include <grub/symbol.h>
80913e
+#include <grub/types.h>
80913e
+
80913e
+#ifdef GRUB_STACK_PROTECTOR
80913e
+extern grub_addr_t EXPORT_VAR (__stack_chk_guard);
80913e
+extern void __attribute__ ((noreturn)) EXPORT_FUNC (__stack_chk_fail) (void);
80913e
+#endif
80913e
+
80913e
+#endif /* GRUB_STACK_PROTECTOR_H */
80913e
diff --git a/acinclude.m4 b/acinclude.m4
80913e
index 242e829ff23..21238fcfd03 100644
80913e
--- a/acinclude.m4
80913e
+++ b/acinclude.m4
80913e
@@ -324,9 +324,9 @@ fi
80913e
 ])
80913e
 
80913e
 
80913e
-dnl Check if the C compiler supports `-fstack-protector'.
80913e
+dnl Check if the C compiler supports the stack protector
80913e
 AC_DEFUN([grub_CHECK_STACK_PROTECTOR],[
80913e
-[# Smashing stack protector.
80913e
+[# Stack smashing protector.
80913e
 ssp_possible=yes]
80913e
 AC_MSG_CHECKING([whether `$CC' accepts `-fstack-protector'])
80913e
 # Is this a reliable test case?
80913e
@@ -343,6 +343,40 @@ else
80913e
   ssp_possible=no]
80913e
   AC_MSG_RESULT([no])
80913e
 [fi]
80913e
+[# Strong stack smashing protector.
80913e
+ssp_strong_possible=yes]
80913e
+AC_MSG_CHECKING([whether `$CC' accepts `-fstack-protector-strong'])
80913e
+# Is this a reliable test case?
80913e
+AC_LANG_CONFTEST([AC_LANG_SOURCE([[
80913e
+void foo (void) { volatile char a[8]; a[3]; }
80913e
+]])])
80913e
+[# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
80913e
+# `ac_compile' like this correct, after all?
80913e
+if eval "$ac_compile -S -fstack-protector-strong -o conftest.s" 2> /dev/null; then]
80913e
+  AC_MSG_RESULT([yes])
80913e
+  [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
80913e
+  rm -f conftest.s
80913e
+else
80913e
+  ssp_strong_possible=no]
80913e
+  AC_MSG_RESULT([no])
80913e
+[fi]
80913e
+[# Global stack smashing protector.
80913e
+ssp_global_possible=yes]
80913e
+AC_MSG_CHECKING([whether `$CC' accepts `-mstack-protector-guard=global'])
80913e
+# Is this a reliable test case?
80913e
+AC_LANG_CONFTEST([AC_LANG_SOURCE([[
80913e
+void foo (void) { volatile char a[8]; a[3]; }
80913e
+]])])
80913e
+[# `$CC -c -o ...' might not be portable.  But, oh, well...  Is calling
80913e
+# `ac_compile' like this correct, after all?
80913e
+if eval "$ac_compile -S -fstack-protector -mstack-protector-guard=global -o conftest.s" 2> /dev/null; then]
80913e
+  AC_MSG_RESULT([yes])
80913e
+  [# Should we clear up other files as well, having called `AC_LANG_CONFTEST'?
80913e
+  rm -f conftest.s
80913e
+else
80913e
+  ssp_global_possible=no]
80913e
+  AC_MSG_RESULT([no])
80913e
+[fi]
80913e
 ])
80913e
 
80913e
 dnl Check if the C compiler supports `-mstack-arg-probe' (Cygwin).
80913e
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
80913e
index a6f1b0dcd06..308ad8850c9 100644
80913e
--- a/grub-core/Makefile.am
80913e
+++ b/grub-core/Makefile.am
80913e
@@ -92,6 +92,7 @@ endif
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h
80913e
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/stack_protector.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
80913e
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm_private.h