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

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