Blame SOURCES/nettle-3.4.1-enable-intel-cet.patch

514b42
diff --git a/asm.m4 b/asm.m4
514b42
index 8da47201..59d64098 100644
514b42
--- a/asm.m4
514b42
+++ b/asm.m4
514b42
@@ -32,7 +32,7 @@ define(<GMP_NUMB_BITS>,<>)dnl
514b42
 define(<PROLOGUE>,
514b42
 <.globl C_NAME($1)
514b42
 DECLARE_FUNC(C_NAME($1))
514b42
-C_NAME($1):>)
514b42
+C_NAME($1): ASM_X86_ENDBR>)
514b42
 
514b42
 define(<EPILOGUE>,
514b42
 
514b42
--- a/config.m4.in	2018-12-04 21:56:06.000000000 +0100
514b42
+++ b/config.m4.in	2020-05-15 11:25:42.350465132 +0200
514b42
@@ -7,6 +7,9 @@
514b42
 define(<ALIGN_LOG>, <@ASM_ALIGN_LOG@>)dnl
514b42
 define(<W64_ABI>, <@W64_ABI@>)dnl
514b42
 define(<RODATA>, <@ASM_RODATA@>)dnl
514b42
+define(<ASM_X86_ENDBR>,<@ASM_X86_ENDBR@>)dnl
514b42
+define(<ASM_X86_MARK_CET_ALIGN>,<@ASM_X86_MARK_CET_ALIGN@>)dnl
514b42
 divert(1)
514b42
+@ASM_X86_MARK_CET@
514b42
 @ASM_MARK_NOEXEC_STACK@
514b42
 divert
514b42
--- a/configure.ac	2018-12-04 21:56:06.000000000 +0100
514b42
+++ b/configure.ac	2020-05-15 11:46:39.152373137 +0200
514b42
@@ -787,6 +787,68 @@
514b42
   ASM_ALIGN_LOG="$nettle_cv_asm_align_log"
514b42
 fi
514b42
 
514b42
+dnl  Define
514b42
+dnl  1. ASM_X86_ENDBR for endbr32/endbr64.
514b42
+dnl  2. ASM_X86_MARK_CET to add a .note.gnu.property section to mark
514b42
+dnl  Intel CET support if needed.
514b42
+dnl  3. ASM_X86_MARK_CET_ALIGN to align ASM_X86_MARK_CET.
514b42
+AC_CACHE_CHECK([if Intel CET is enabled],
514b42
+  [nettle_cv_asm_x86_intel_cet],
514b42
+  [AC_TRY_COMPILE([
514b42
+#ifndef __CET__
514b42
+#error Intel CET is not enabled
514b42
+#endif
514b42
+  ], [],
514b42
+  [nettle_cv_asm_x86_intel_cet=yes],
514b42
+  [nettle_cv_asm_x86_intel_cet=no])])
514b42
+if test "$nettle_cv_asm_x86_intel_cet" = yes; then
514b42
+  case $ABI in
514b42
+  32|standard)
514b42
+    ASM_X86_ENDBR=endbr32
514b42
+    ASM_X86_MARK_CET_ALIGN=2
514b42
+    ;;
514b42
+  64)
514b42
+    ASM_X86_ENDBR=endbr64
514b42
+    ASM_X86_MARK_CET_ALIGN=3
514b42
+    ;;
514b42
+  x32)
514b42
+    ASM_X86_ENDBR=endbr64
514b42
+    ASM_X86_MARK_CET_ALIGN=2
514b42
+    ;;
514b42
+  esac
514b42
+  AC_CACHE_CHECK([if .note.gnu.property section is needed],
514b42
+    [nettle_cv_asm_x86_gnu_property],
514b42
+    [AC_TRY_COMPILE([
514b42
+#if !defined __ELF__ || !defined __CET__
514b42
+#error GNU property is not needed
514b42
+#endif
514b42
+    ], [],
514b42
+    [nettle_cv_asm_x86_gnu_property=yes],
514b42
+    [nettle_cv_asm_x86_gnu_property=no])])
514b42
+else
514b42
+  nettle_cv_asm_x86_gnu_property=no
514b42
+fi
514b42
+if test "$nettle_cv_asm_x86_gnu_property" = yes; then
514b42
+  ASM_X86_MARK_CET='
514b42
+	.pushsection ".note.gnu.property", "a"
514b42
+	.p2align ASM_X86_MARK_CET_ALIGN
514b42
+	.long 1f - 0f
514b42
+	.long 4f - 1f
514b42
+	.long 5
514b42
+0:
514b42
+	.asciz "GNU"
514b42
+1:
514b42
+	.p2align ASM_X86_MARK_CET_ALIGN
514b42
+	.long 0xc0000002
514b42
+	.long 3f - 2f
514b42
+2:
514b42
+	.long 3
514b42
+3:
514b42
+	.p2align ASM_X86_MARK_CET_ALIGN
514b42
+4:
514b42
+	.popsection'
514b42
+fi
514b42
+
514b42
 AC_SUBST(ASM_SYMBOL_PREFIX)
514b42
 AC_SUBST(ASM_ELF_STYLE)
514b42
 AC_SUBST(ASM_COFF_STYLE)
514b42
@@ -796,6 +858,9 @@
514b42
 AC_SUBST(ASM_ALIGN_LOG)
514b42
 AC_SUBST(W64_ABI)
514b42
 AC_SUBST(EMULATOR)
514b42
+AC_SUBST(ASM_X86_ENDBR)
514b42
+AC_SUBST(ASM_X86_MARK_CET)
514b42
+AC_SUBST(ASM_X86_MARK_CET_ALIGN)
514b42
 
514b42
 AC_SUBST(LIBNETTLE_MAJOR)
514b42
 AC_SUBST(LIBNETTLE_MINOR)
514b42
diff --git a/testsuite/.test-rules.make b/testsuite/.test-rules.make
514b42
index 922a2c7f..9de8f412 100644
514b42
--- a/testsuite/.test-rules.make
514b42
+++ b/testsuite/.test-rules.make
514b42
@@ -178,6 +178,9 @@ xts-test$(EXEEXT): xts-test.$(OBJEXT)
514b42
 pbkdf2-test$(EXEEXT): pbkdf2-test.$(OBJEXT)
514b42
 	$(LINK) pbkdf2-test.$(OBJEXT) $(TEST_OBJS) -o pbkdf2-test$(EXEEXT)
514b42
 
514b42
+x86-ibt-test$(EXEEXT): x86-ibt-test.$(OBJEXT)
514b42
+	$(LINK) x86-ibt-test.$(OBJEXT) $(TEST_OBJS) -o x86-ibt-test$(EXEEXT)
514b42
+
514b42
 sexp-test$(EXEEXT): sexp-test.$(OBJEXT)
514b42
 	$(LINK) sexp-test.$(OBJEXT) $(TEST_OBJS) -o sexp-test$(EXEEXT)
514b42
 
514b42
--- a/testsuite/Makefile.in	2018-12-04 21:56:06.000000000 +0100
514b42
+++ b/testsuite/Makefile.in	2020-05-15 11:21:15.673321598 +0200
514b42
@@ -31,7 +31,8 @@
514b42
 		    hmac-test.c umac-test.c \
514b42
 		    meta-hash-test.c meta-cipher-test.c\
514b42
 		    meta-aead-test.c meta-armor-test.c \
514b42
-		    buffer-test.c yarrow-test.c pbkdf2-test.c
514b42
+		    buffer-test.c yarrow-test.c pbkdf2-test.c \
514b42
+		    x86-ibt-test.c
514b42
 
514b42
 TS_HOGWEED_SOURCES = sexp-test.c sexp-format-test.c \
514b42
 		     rsa2sexp-test.c sexp2rsa-test.c \
514b42
diff --git a/testsuite/x86-ibt-test.c b/testsuite/x86-ibt-test.c
514b42
new file mode 100644
514b42
index 00000000..1f3d1d67
514b42
--- /dev/null
514b42
+++ b/testsuite/x86-ibt-test.c
514b42
@@ -0,0 +1,69 @@
514b42
+#include "testutils.h"
514b42
+#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) \
514b42
+    && defined(__CET__) && defined(__linux__)
514b42
+#include <signal.h>
514b42
+
514b42
+static void
514b42
+segfault_handler(int signo)
514b42
+{
514b42
+  exit(0);
514b42
+}
514b42
+
514b42
+static void
514b42
+ibt_violation(void)
514b42
+{
514b42
+#ifdef __i386__
514b42
+  unsigned int reg;
514b42
+  asm volatile("lea 1f, %0\n\t"
514b42
+	       "jmp *%0\n"
514b42
+	       "1:" : "=r" (reg));
514b42
+#else
514b42
+  unsigned long long reg;
514b42
+  asm volatile("lea 1f(%%rip), %0\n\t"
514b42
+	       "jmp *%0\n"
514b42
+	       "1:" : "=r" (reg));
514b42
+#endif
514b42
+}
514b42
+
514b42
+#ifdef __i386__
514b42
+static unsigned int
514b42
+_get_ssp(void)
514b42
+{
514b42
+  unsigned int ssp;
514b42
+  asm volatile("xor %0, %0\n\trdsspd %0" : "=r" (ssp));
514b42
+  return ssp;
514b42
+}
514b42
+#else
514b42
+static unsigned long long
514b42
+_get_ssp(void)
514b42
+{
514b42
+  unsigned long long ssp;
514b42
+  asm volatile("xor %0, %0\n\trdsspq %0" : "=r" (ssp));
514b42
+  return ssp;
514b42
+}
514b42
+#endif
514b42
+
514b42
+void
514b42
+test_main(void)
514b42
+{
514b42
+   /* NB: This test should trigger SIGSEGV on CET platforms.  _get_ssp
514b42
+      returns the address of shadow stack pointer.  If the address of
514b42
+      shadow stack pointer is 0, SHSTK is disabled and we assume that
514b42
+      IBT is also disabled.  */
514b42
+  if (_get_ssp() == 0)
514b42
+    {
514b42
+      ibt_violation();
514b42
+      SKIP();
514b42
+    }
514b42
+
514b42
+  signal(SIGSEGV, segfault_handler);
514b42
+  ibt_violation();
514b42
+  FAIL();
514b42
+}
514b42
+#else
514b42
+void
514b42
+test_main(void)
514b42
+{
514b42
+  SKIP();
514b42
+}
514b42
+#endif
514b42
-- 
514b42
2.25.4
514b42