246e17
commit 1fabdb99084df004f7f4cdc7068d1be209a258be
246e17
Author: H.J. Lu <hjl.tools@gmail.com>
246e17
Date:   Wed Mar 18 04:35:54 2020 -0700
246e17
246e17
    x86: Remove ARCH_CET_LEGACY_BITMAP [BZ #25397]
246e17
    
246e17
    Since legacy bitmap doesn't cover jitted code generated by legacy JIT
246e17
    engine, it isn't very useful.  This patch removes ARCH_CET_LEGACY_BITMAP
246e17
    and treats indirect branch tracking similar to shadow stack by removing
246e17
    legacy bitmap support.
246e17
    
246e17
    Tested on CET Linux/x86-64 and non-CET Linux/x86-64.
246e17
    
246e17
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
246e17
---
246e17
246e17
diff --git a/sysdeps/unix/sysv/linux/x86/dl-cet.h b/sysdeps/unix/sysv/linux/x86/dl-cet.h
246e17
index 3fbcfeb..47c52e6 100644
246e17
--- a/sysdeps/unix/sysv/linux/x86/dl-cet.h
246e17
+++ b/sysdeps/unix/sysv/linux/x86/dl-cet.h
246e17
@@ -19,27 +19,6 @@
246e17
 #include <asm/prctl.h>
246e17
 
246e17
 static inline int __attribute__ ((always_inline))
246e17
-dl_cet_allocate_legacy_bitmap (unsigned long *legacy_bitmap)
246e17
-{
246e17
-  /* Allocate legacy bitmap.  */
246e17
-  INTERNAL_SYSCALL_DECL (err);
246e17
-#ifdef __LP64__
246e17
-  return (int) INTERNAL_SYSCALL (arch_prctl, err, 2,
246e17
-				 ARCH_CET_LEGACY_BITMAP, legacy_bitmap);
246e17
-#else
246e17
-  unsigned long long legacy_bitmap_u64[2];
246e17
-  int res = INTERNAL_SYSCALL (arch_prctl, err, 2,
246e17
-			      ARCH_CET_LEGACY_BITMAP, legacy_bitmap_u64);
246e17
-  if (res == 0)
246e17
-    {
246e17
-      legacy_bitmap[0] = legacy_bitmap_u64[0];
246e17
-      legacy_bitmap[1] = legacy_bitmap_u64[1];
246e17
-    }
246e17
-  return res;
246e17
-#endif
246e17
-}
246e17
-
246e17
-static inline int __attribute__ ((always_inline))
246e17
 dl_cet_disable_cet (unsigned int cet_feature)
246e17
 {
246e17
   INTERNAL_SYSCALL_DECL (err);
246e17
diff --git a/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h b/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h
246e17
index f67f329..45ad0b0 100644
246e17
--- a/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h
246e17
+++ b/sysdeps/unix/sysv/linux/x86/include/asm/prctl.h
246e17
@@ -24,9 +24,4 @@
246e17
      OUT: allocated shadow stack address: *addr.
246e17
  */
246e17
 # define ARCH_CET_ALLOC_SHSTK	0x3004
246e17
-/* Return legacy region bitmap info in unsigned long long *addr:
246e17
-     address: addr[0].
246e17
-     size: addr[1].
246e17
- */
246e17
-# define ARCH_CET_LEGACY_BITMAP	0x3005
246e17
 #endif /* ARCH_CET_STATUS */
246e17
diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile
246e17
index 43ad4a7..d9bdf0b 100644
246e17
--- a/sysdeps/x86/Makefile
246e17
+++ b/sysdeps/x86/Makefile
246e17
@@ -20,7 +20,8 @@ sysdep-dl-routines += dl-cet
246e17
 
246e17
 tests += tst-cet-legacy-1 tst-cet-legacy-2 tst-cet-legacy-2a \
246e17
 	 tst-cet-legacy-3 tst-cet-legacy-4 \
246e17
-	 tst-cet-legacy-5a tst-cet-legacy-6a
246e17
+	 tst-cet-legacy-5a tst-cet-legacy-6a tst-cet-legacy-7 \
246e17
+	 tst-cet-legacy-8
246e17
 ifneq (no,$(have-tunables))
246e17
 tests += tst-cet-legacy-4a tst-cet-legacy-4b tst-cet-legacy-4c \
246e17
 	 tst-cet-legacy-5b tst-cet-legacy-6b
246e17
@@ -42,14 +43,16 @@ CFLAGS-tst-cet-legacy-4b.c += -fcf-protection
246e17
 CFLAGS-tst-cet-legacy-mod-4.c += -fcf-protection=none
246e17
 CFLAGS-tst-cet-legacy-5a.c += -fcf-protection
246e17
 CFLAGS-tst-cet-legacy-5b.c += -fcf-protection
246e17
-CFLAGS-tst-cet-legacy-mod-5a.c += -fcf-protection=none
246e17
+CFLAGS-tst-cet-legacy-mod-5a.c += -fcf-protection=branch
246e17
 CFLAGS-tst-cet-legacy-mod-5b.c += -fcf-protection
246e17
 CFLAGS-tst-cet-legacy-mod-5c.c += -fcf-protection
246e17
 CFLAGS-tst-cet-legacy-6a.c += -fcf-protection
246e17
 CFLAGS-tst-cet-legacy-6b.c += -fcf-protection
246e17
-CFLAGS-tst-cet-legacy-mod-6a.c += -fcf-protection=none
246e17
+CFLAGS-tst-cet-legacy-mod-6a.c += -fcf-protection=branch
246e17
 CFLAGS-tst-cet-legacy-mod-6b.c += -fcf-protection
246e17
 CFLAGS-tst-cet-legacy-mod-6c.c += -fcf-protection
246e17
+CFLAGS-tst-cet-legacy-7.c += -fcf-protection=none
246e17
+CFLAGS-tst-cet-legacy-8.c += -mshstk
246e17
 
246e17
 $(objpfx)tst-cet-legacy-1: $(objpfx)tst-cet-legacy-mod-1.so \
246e17
 		       $(objpfx)tst-cet-legacy-mod-2.so
246e17
diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c
246e17
index b82ba14..627d937 100644
246e17
--- a/sysdeps/x86/dl-cet.c
246e17
+++ b/sysdeps/x86/dl-cet.c
246e17
@@ -33,63 +33,6 @@
246e17
 # error GNU_PROPERTY_X86_FEATURE_1_SHSTK != X86_FEATURE_1_SHSTK
246e17
 #endif
246e17
 
246e17
-static int
246e17
-dl_cet_mark_legacy_region (struct link_map *l)
246e17
-{
246e17
-  /* Mark PT_LOAD segments with PF_X in legacy code page bitmap.  */
246e17
-  size_t i, phnum = l->l_phnum;
246e17
-  const ElfW(Phdr) *phdr = l->l_phdr;
246e17
-#ifdef __x86_64__
246e17
-  typedef unsigned long long word_t;
246e17
-#else
246e17
-  typedef unsigned long word_t;
246e17
-#endif
246e17
-  unsigned int bits_to_set;
246e17
-  word_t mask_to_set;
246e17
-#define BITS_PER_WORD (sizeof (word_t) * 8)
246e17
-#define BITMAP_FIRST_WORD_MASK(start) \
246e17
-  (~((word_t) 0) << ((start) & (BITS_PER_WORD - 1)))
246e17
-#define BITMAP_LAST_WORD_MASK(nbits) \
246e17
-  (~((word_t) 0) >> (-(nbits) & (BITS_PER_WORD - 1)))
246e17
-
246e17
-  word_t *bitmap = (word_t *) GL(dl_x86_legacy_bitmap)[0];
246e17
-  word_t bitmap_size = GL(dl_x86_legacy_bitmap)[1];
246e17
-  word_t *p;
246e17
-  size_t page_size = GLRO(dl_pagesize);
246e17
-
246e17
-  for (i = 0; i < phnum; i++)
246e17
-    if (phdr[i].p_type == PT_LOAD && (phdr[i].p_flags & PF_X))
246e17
-      {
246e17
-	/* One bit in legacy bitmap represents a page.  */
246e17
-	ElfW(Addr) start = (phdr[i].p_vaddr + l->l_addr) / page_size;
246e17
-	ElfW(Addr) len = (phdr[i].p_memsz + page_size - 1) / page_size;
246e17
-	ElfW(Addr) end = start + len;
246e17
-
246e17
-	if ((end / 8) > bitmap_size)
246e17
-	  return -EINVAL;
246e17
-
246e17
-	p = bitmap + (start / BITS_PER_WORD);
246e17
-	bits_to_set = BITS_PER_WORD - (start % BITS_PER_WORD);
246e17
-	mask_to_set = BITMAP_FIRST_WORD_MASK (start);
246e17
-
246e17
-	while (len >= bits_to_set)
246e17
-	  {
246e17
-	    *p |= mask_to_set;
246e17
-	    len -= bits_to_set;
246e17
-	    bits_to_set = BITS_PER_WORD;
246e17
-	    mask_to_set = ~((word_t) 0);
246e17
-	    p++;
246e17
-	  }
246e17
-	if (len)
246e17
-	  {
246e17
-	    mask_to_set &= BITMAP_LAST_WORD_MASK (end);
246e17
-	    *p |= mask_to_set;
246e17
-	  }
246e17
-      }
246e17
-
246e17
-  return 0;
246e17
-}
246e17
-
246e17
 /* Check if object M is compatible with CET.  */
246e17
 
246e17
 static void
246e17
@@ -117,6 +60,8 @@ dl_cet_check (struct link_map *m, const char *program)
246e17
   if (ibt_enabled || shstk_enabled)
246e17
     {
246e17
       struct link_map *l = NULL;
246e17
+      unsigned int ibt_legacy = 0, shstk_legacy = 0;
246e17
+      bool found_ibt_legacy = false, found_shstk_legacy = false;
246e17
 
246e17
       /* Check if IBT and SHSTK are enabled in object.  */
246e17
       bool enable_ibt = (ibt_enabled
246e17
@@ -142,10 +87,7 @@ dl_cet_check (struct link_map *m, const char *program)
246e17
 	 support IBT nor SHSTK.  */
246e17
       if (enable_ibt || enable_shstk)
246e17
 	{
246e17
-	  int res;
246e17
 	  unsigned int i;
246e17
-	  unsigned int first_legacy, last_legacy;
246e17
-	  bool need_legacy_bitmap = false;
246e17
 
246e17
 	  i = m->l_searchlist.r_nlist;
246e17
 	  while (i-- > 0)
246e17
@@ -167,91 +109,25 @@ dl_cet_check (struct link_map *m, const char *program)
246e17
 		continue;
246e17
 #endif
246e17
 
246e17
-	      if (enable_ibt
246e17
-		  && enable_ibt_type != CET_ALWAYS_ON
246e17
-		  && !(l->l_cet & lc_ibt))
246e17
+	      /* IBT is enabled only if it is enabled in executable as
246e17
+		 well as all shared objects.  */
246e17
+	      enable_ibt &= (enable_ibt_type == CET_ALWAYS_ON
246e17
+			     || (l->l_cet & lc_ibt) != 0);
246e17
+	      if (!found_ibt_legacy && enable_ibt != ibt_enabled)
246e17
 		{
246e17
-		  /* Remember the first and last legacy objects.  */
246e17
-		  if (!need_legacy_bitmap)
246e17
-		    last_legacy = i;
246e17
-		  first_legacy = i;
246e17
-		  need_legacy_bitmap = true;
246e17
+		  found_ibt_legacy = true;
246e17
+		  ibt_legacy = i;
246e17
 		}
246e17
 
246e17
 	      /* SHSTK is enabled only if it is enabled in executable as
246e17
 		 well as all shared objects.  */
246e17
 	      enable_shstk &= (enable_shstk_type == CET_ALWAYS_ON
246e17
 			       || (l->l_cet & lc_shstk) != 0);
246e17
-	    }
246e17
-
246e17
-	  if (need_legacy_bitmap)
246e17
-	    {
246e17
-	      if (GL(dl_x86_legacy_bitmap)[0])
246e17
-		{
246e17
-		  /* Change legacy bitmap to writable.  */
246e17
-		  if (__mprotect ((void *) GL(dl_x86_legacy_bitmap)[0],
246e17
-				  GL(dl_x86_legacy_bitmap)[1],
246e17
-				  PROT_READ | PROT_WRITE) < 0)
246e17
-		    {
246e17
-mprotect_failure:
246e17
-		      if (program)
246e17
-			_dl_fatal_printf ("%s: mprotect legacy bitmap failed\n",
246e17
-					  l->l_name);
246e17
-		      else
246e17
-			_dl_signal_error (EINVAL, l->l_name, "dlopen",
246e17
-					  N_("mprotect legacy bitmap failed"));
246e17
-		    }
246e17
-		}
246e17
-	      else
246e17
+	      if (enable_shstk != shstk_enabled)
246e17
 		{
246e17
-		  /* Allocate legacy bitmap.  */
246e17
-		  int res = dl_cet_allocate_legacy_bitmap
246e17
-		    (GL(dl_x86_legacy_bitmap));
246e17
-		  if (res != 0)
246e17
-		    {
246e17
-		      if (program)
246e17
-			_dl_fatal_printf ("%s: legacy bitmap isn't available\n",
246e17
-					  l->l_name);
246e17
-		      else
246e17
-			_dl_signal_error (EINVAL, l->l_name, "dlopen",
246e17
-					  N_("legacy bitmap isn't available"));
246e17
-		    }
246e17
+		  found_shstk_legacy = true;
246e17
+		  shstk_legacy = i;
246e17
 		}
246e17
-
246e17
-	      /* Put legacy shared objects in legacy bitmap.  */
246e17
-	      for (i = first_legacy; i <= last_legacy; i++)
246e17
-		{
246e17
-		  l = m->l_initfini[i];
246e17
-
246e17
-		  if (l->l_init_called || (l->l_cet & lc_ibt))
246e17
-		    continue;
246e17
-
246e17
-#ifdef SHARED
246e17
-		  if (l == &GL(dl_rtld_map)
246e17
-		      ||  l->l_real == &GL(dl_rtld_map)
246e17
-		      || (program && l == m))
246e17
-		    continue;
246e17
-#endif
246e17
-
246e17
-		  /* If IBT is enabled in executable and IBT isn't enabled
246e17
-		     in this shard object, mark PT_LOAD segments with PF_X
246e17
-		     in legacy code page bitmap.  */
246e17
-		  res = dl_cet_mark_legacy_region (l);
246e17
-		  if (res != 0)
246e17
-		    {
246e17
-		      if (program)
246e17
-			_dl_fatal_printf ("%s: failed to mark legacy code region\n",
246e17
-					  l->l_name);
246e17
-		      else
246e17
-			_dl_signal_error (-res, l->l_name, "dlopen",
246e17
-					  N_("failed to mark legacy code region"));
246e17
-		    }
246e17
-		}
246e17
-
246e17
-	      /* Change legacy bitmap to read-only.  */
246e17
-	      if (__mprotect ((void *) GL(dl_x86_legacy_bitmap)[0],
246e17
-			      GL(dl_x86_legacy_bitmap)[1], PROT_READ) < 0)
246e17
-		goto mprotect_failure;
246e17
 	    }
246e17
 	}
246e17
 
246e17
@@ -259,23 +135,40 @@ mprotect_failure:
246e17
 
246e17
       if (enable_ibt != ibt_enabled || enable_shstk != shstk_enabled)
246e17
 	{
246e17
-	  if (!program
246e17
-	      && enable_shstk_type != CET_PERMISSIVE)
246e17
+	  if (!program)
246e17
 	    {
246e17
-	      /* When SHSTK is enabled, we can't dlopening a shared
246e17
-		 object without SHSTK.  */
246e17
-	      if (enable_shstk != shstk_enabled)
246e17
-		_dl_signal_error (EINVAL, l->l_name, "dlopen",
246e17
-				  N_("shadow stack isn't enabled"));
246e17
-	      return;
246e17
+	      if (enable_ibt_type != CET_PERMISSIVE)
246e17
+		{
246e17
+		  /* When IBT is enabled, we cannot dlopen a shared
246e17
+		     object without IBT.  */
246e17
+		  if (found_ibt_legacy)
246e17
+		    _dl_signal_error (0,
246e17
+				      m->l_initfini[ibt_legacy]->l_name,
246e17
+				      "dlopen",
246e17
+				      N_("rebuild shared object with IBT support enabled"));
246e17
+		}
246e17
+
246e17
+	      if (enable_shstk_type != CET_PERMISSIVE)
246e17
+		{
246e17
+		  /* When SHSTK is enabled, we cannot dlopen a shared
246e17
+		     object without SHSTK.  */
246e17
+		  if (found_shstk_legacy)
246e17
+		    _dl_signal_error (0,
246e17
+				      m->l_initfini[shstk_legacy]->l_name,
246e17
+				      "dlopen",
246e17
+				      N_("rebuild shared object with SHSTK support enabled"));
246e17
+		}
246e17
+
246e17
+	      if (enable_ibt_type != CET_PERMISSIVE
246e17
+		  && enable_shstk_type != CET_PERMISSIVE)
246e17
+		return;
246e17
 	    }
246e17
 
246e17
 	  /* Disable IBT and/or SHSTK if they are enabled by kernel, but
246e17
 	     disabled in executable or shared objects.  */
246e17
 	  unsigned int cet_feature = 0;
246e17
 
246e17
-	  /* Disable IBT only during program startup.  */
246e17
-	  if (program && !enable_ibt)
246e17
+	  if (!enable_ibt)
246e17
 	    cet_feature |= GNU_PROPERTY_X86_FEATURE_1_IBT;
246e17
 	  if (!enable_shstk)
246e17
 	    cet_feature |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
246e17
@@ -286,8 +179,14 @@ mprotect_failure:
246e17
 	      if (program)
246e17
 		_dl_fatal_printf ("%s: can't disable CET\n", program);
246e17
 	      else
246e17
-		_dl_signal_error (-res, l->l_name, "dlopen",
246e17
-				  N_("can't disable CET"));
246e17
+		{
246e17
+		  if (found_ibt_legacy)
246e17
+		    l = m->l_initfini[ibt_legacy];
246e17
+		  else
246e17
+		    l = m->l_initfini[shstk_legacy];
246e17
+		  _dl_signal_error (-res, l->l_name, "dlopen",
246e17
+				    N_("can't disable CET"));
246e17
+		}
246e17
 	    }
246e17
 
246e17
 	  /* Clear the disabled bits in dl_x86_feature_1.  */
246e17
@@ -297,17 +196,21 @@ mprotect_failure:
246e17
 	}
246e17
 
246e17
 #ifdef SHARED
246e17
-      if (program
246e17
-	  && (!shstk_enabled
246e17
-	      || enable_shstk_type != CET_PERMISSIVE)
246e17
-	  && (ibt_enabled || shstk_enabled))
246e17
+      if (program && (ibt_enabled || shstk_enabled))
246e17
 	{
246e17
-	  /* Lock CET if IBT or SHSTK is enabled in executable.  Don't
246e17
-	     lock CET if SHSTK is enabled permissively.  */
246e17
-	  int res = dl_cet_lock_cet ();
246e17
-	  if (res != 0)
246e17
-	    _dl_fatal_printf ("%s: can't lock CET\n", program);
246e17
+	  if ((!ibt_enabled
246e17
+	       || enable_ibt_type != CET_PERMISSIVE)
246e17
+	      && (!shstk_enabled
246e17
+		  || enable_shstk_type != CET_PERMISSIVE))
246e17
+	    {
246e17
+	      /* Lock CET if IBT or SHSTK is enabled in executable unless
246e17
+	         IBT or SHSTK is enabled permissively.  */
246e17
+	      int res = dl_cet_lock_cet ();
246e17
+	      if (res != 0)
246e17
+		_dl_fatal_printf ("%s: can't lock CET\n", program);
246e17
+	    }
246e17
 
246e17
+	  /* Set feature_1 if IBT or SHSTK is enabled in executable.  */
246e17
 	  cet_feature_changed = true;
246e17
 	}
246e17
 #endif
246e17
diff --git a/sysdeps/x86/dl-procruntime.c b/sysdeps/x86/dl-procruntime.c
246e17
index eddbde6..920bfe8 100644
246e17
--- a/sysdeps/x86/dl-procruntime.c
246e17
+++ b/sysdeps/x86/dl-procruntime.c
246e17
@@ -54,15 +54,4 @@ PROCINFO_CLASS unsigned int _dl_x86_feature_1[2]
246e17
 # else
246e17
 ,
246e17
 # endif
246e17
-
246e17
-# if !defined PROCINFO_DECL && defined SHARED
246e17
-  ._dl_x86_legacy_bitmap
246e17
-# else
246e17
-PROCINFO_CLASS unsigned long _dl_x86_legacy_bitmap[2]
246e17
-# endif
246e17
-# if !defined SHARED || defined PROCINFO_DECL
246e17
-;
246e17
-# else
246e17
-,
246e17
-# endif
246e17
 #endif
246e17
diff --git a/sysdeps/x86/tst-cet-legacy-4.c b/sysdeps/x86/tst-cet-legacy-4.c
246e17
index 3ead63d..f0ba326 100644
246e17
--- a/sysdeps/x86/tst-cet-legacy-4.c
246e17
+++ b/sysdeps/x86/tst-cet-legacy-4.c
246e17
@@ -20,6 +20,9 @@
246e17
 #include <dlfcn.h>
246e17
 #include <stdio.h>
246e17
 #include <stdlib.h>
246e17
+#include <string.h>
246e17
+
246e17
+#include <support/check.h>
246e17
 
246e17
 static int
246e17
 do_test (void)
246e17
@@ -31,22 +34,18 @@ do_test (void)
246e17
   h = dlopen (modname, RTLD_LAZY);
246e17
   if (h == NULL)
246e17
     {
246e17
-      printf ("cannot open '%s': %s\n", modname, dlerror ());
246e17
-      exit (1);
246e17
+      const char *err = dlerror ();
246e17
+      if (!strstr (err, "rebuild shared object with IBT support enabled"))
246e17
+	FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err);
246e17
+      return 0;
246e17
     }
246e17
 
246e17
   fp = dlsym (h, "test");
246e17
   if (fp == NULL)
246e17
-    {
246e17
-      printf ("cannot get symbol 'test': %s\n", dlerror ());
246e17
-      exit (1);
246e17
-    }
246e17
+    FAIL_EXIT1 ("cannot get symbol 'test': %s\n", dlerror ());
246e17
 
246e17
   if (fp () != 0)
246e17
-    {
246e17
-      puts ("test () != 0");
246e17
-      exit (1);
246e17
-    }
246e17
+    FAIL_EXIT1 ("test () != 0");
246e17
 
246e17
   dlclose (h);
246e17
 
246e17
diff --git a/sysdeps/x86/tst-cet-legacy-5.c b/sysdeps/x86/tst-cet-legacy-5.c
246e17
index fbf640f..e1ca09d 100644
246e17
--- a/sysdeps/x86/tst-cet-legacy-5.c
246e17
+++ b/sysdeps/x86/tst-cet-legacy-5.c
246e17
@@ -35,7 +35,8 @@ do_test_1 (const char *modname, bool fail)
246e17
       if (fail)
246e17
 	{
246e17
 	  const char *err = dlerror ();
246e17
-	  if (strstr (err, "shadow stack isn't enabled") == NULL)
246e17
+	  if (strstr (err, "rebuild shared object with SHSTK support enabled")
246e17
+	      == NULL)
246e17
 	    {
246e17
 	      printf ("incorrect dlopen '%s' error: %s\n", modname,
246e17
 		      dlerror ());
246e17
diff --git a/sysdeps/x86/tst-cet-legacy-6.c b/sysdeps/x86/tst-cet-legacy-6.c
246e17
index 9151225..184a35b 100644
246e17
--- a/sysdeps/x86/tst-cet-legacy-6.c
246e17
+++ b/sysdeps/x86/tst-cet-legacy-6.c
246e17
@@ -35,7 +35,8 @@ do_test_1 (const char *modname, bool fail)
246e17
       if (fail)
246e17
 	{
246e17
 	  const char *err = dlerror ();
246e17
-	  if (strstr (err, "shadow stack isn't enabled") == NULL)
246e17
+	  if (strstr (err, "rebuild shared object with SHSTK support enabled")
246e17
+	      == NULL)
246e17
 	    {
246e17
 	      printf ("incorrect dlopen '%s' error: %s\n", modname,
246e17
 		      dlerror ());
246e17
diff --git a/sysdeps/x86/tst-cet-legacy-7.c b/sysdeps/x86/tst-cet-legacy-7.c
246e17
new file mode 100644
246e17
index 0000000..58bcb29
246e17
--- /dev/null
246e17
+++ b/sysdeps/x86/tst-cet-legacy-7.c
246e17
@@ -0,0 +1,38 @@
246e17
+/* Check compatibility of legacy executable with a JIT engine.
246e17
+   Copyright (C) 2020 Free Software Foundation, Inc.
246e17
+   This file is part of the GNU C Library.
246e17
+
246e17
+   The GNU C Library is free software; you can redistribute it and/or
246e17
+   modify it under the terms of the GNU Lesser General Public
246e17
+   License as published by the Free Software Foundation; either
246e17
+   version 2.1 of the License, or (at your option) any later version.
246e17
+
246e17
+   The GNU C Library is distributed in the hope that it will be useful,
246e17
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
246e17
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
246e17
+   Lesser General Public License for more details.
246e17
+
246e17
+   You should have received a copy of the GNU Lesser General Public
246e17
+   License along with the GNU C Library; if not, see
246e17
+   <https://www.gnu.org/licenses/>.  */
246e17
+
246e17
+#include <stdio.h>
246e17
+#include <sys/mman.h>
246e17
+#include <support/xunistd.h>
246e17
+
246e17
+/* Check that mmapped legacy code works with -fcf-protection=none.  */
246e17
+
246e17
+static int
246e17
+do_test (void)
246e17
+{
246e17
+  void (*funcp) (void);
246e17
+  funcp = xmmap (NULL, 0x1000, PROT_EXEC | PROT_READ | PROT_WRITE,
246e17
+		 MAP_ANONYMOUS | MAP_PRIVATE, -1);
246e17
+  printf ("mmap = %p\n", funcp);
246e17
+  /* Write RET instruction.  */
246e17
+  *(char *) funcp = 0xc3;
246e17
+  funcp ();
246e17
+  return 0;
246e17
+}
246e17
+
246e17
+#include <support/test-driver.c>
246e17
diff --git a/sysdeps/x86/tst-cet-legacy-8.c b/sysdeps/x86/tst-cet-legacy-8.c
246e17
new file mode 100644
246e17
index 0000000..11e8115
246e17
--- /dev/null
246e17
+++ b/sysdeps/x86/tst-cet-legacy-8.c
246e17
@@ -0,0 +1,48 @@
246e17
+/* Check incompatibility with legacy JIT engine.
246e17
+   Copyright (C) 2020 Free Software Foundation, Inc.
246e17
+   This file is part of the GNU C Library.
246e17
+
246e17
+   The GNU C Library is free software; you can redistribute it and/or
246e17
+   modify it under the terms of the GNU Lesser General Public
246e17
+   License as published by the Free Software Foundation; either
246e17
+   version 2.1 of the License, or (at your option) any later version.
246e17
+
246e17
+   The GNU C Library is distributed in the hope that it will be useful,
246e17
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
246e17
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
246e17
+   Lesser General Public License for more details.
246e17
+
246e17
+   You should have received a copy of the GNU Lesser General Public
246e17
+   License along with the GNU C Library; if not, see
246e17
+   <https://www.gnu.org/licenses/>.  */
246e17
+
246e17
+#include <stdio.h>
246e17
+#include <stdlib.h>
246e17
+#include <x86intrin.h>
246e17
+#include <sys/mman.h>
246e17
+#include <support/test-driver.h>
246e17
+#include <support/xsignal.h>
246e17
+#include <support/xunistd.h>
246e17
+
246e17
+/* Check that mmapped legacy code trigges segfault with -fcf-protection.  */
246e17
+
246e17
+static int
246e17
+do_test (void)
246e17
+{
246e17
+  /* NB: This test should trigger SIGSEGV on CET platforms.  If SHSTK
246e17
+     is disabled, assuming IBT is also disabled.  */
246e17
+  if (_get_ssp () == 0)
246e17
+    return EXIT_UNSUPPORTED;
246e17
+
246e17
+  void (*funcp) (void);
246e17
+  funcp = xmmap (NULL, 0x1000, PROT_EXEC | PROT_READ | PROT_WRITE,
246e17
+		 MAP_ANONYMOUS | MAP_PRIVATE, -1);
246e17
+  printf ("mmap = %p\n", funcp);
246e17
+  /* Write RET instruction.  */
246e17
+  *(char *) funcp = 0xc3;
246e17
+  funcp ();
246e17
+  return EXIT_FAILURE;
246e17
+}
246e17
+
246e17
+#define EXPECTED_SIGNAL (_get_ssp () == 0 ? 0 : SIGSEGV)
246e17
+#include <support/test-driver.c>
246e17