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

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