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

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