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