e354a5
commit 9e38f455a6c602be86b7b5a8d6523cbdcd7ec051
e354a5
Author: H.J. Lu <hjl.tools@gmail.com>
e354a5
Date:   Mon Apr 27 15:44:07 2020 -0700
e354a5
e354a5
    x86: Add --enable-cet=permissive
e354a5
    
e354a5
    When CET is enabled, it is an error to dlopen a non CET enabled shared
e354a5
    library in CET enabled application.  It may be desirable to make CET
e354a5
    permissive, that is disable CET when dlopening a non CET enabled shared
e354a5
    library.  With the new --enable-cet=permissive configure option, CET is
e354a5
    disabled when dlopening a non CET enabled shared library.
e354a5
    
e354a5
    Add DEFAULT_DL_X86_CET_CONTROL to config.h.in:
e354a5
    
e354a5
     /* The default value of x86 CET control.  */
e354a5
     #define DEFAULT_DL_X86_CET_CONTROL cet_elf_property
e354a5
    
e354a5
    which enables CET features based on ELF property note.
e354a5
    
e354a5
    --enable-cet=permissive it to
e354a5
    
e354a5
     /* The default value of x86 CET control.  */
e354a5
     #define DEFAULT_DL_X86_CET_CONTROL cet_permissive
e354a5
    
e354a5
    which enables CET features permissively.
e354a5
    
e354a5
    Update tst-cet-legacy-5a, tst-cet-legacy-5b, tst-cet-legacy-6a and
e354a5
    tst-cet-legacy-6b to check --enable-cet and --enable-cet=permissive.
e354a5
---
e354a5
e354a5
diff --git a/INSTALL b/INSTALL
e354a5
index d56e102..0655650 100644
e354a5
--- a/INSTALL
e354a5
+++ b/INSTALL
e354a5
@@ -116,20 +116,24 @@ if 'CFLAGS' is specified it must enable optimization.  For example:
e354a5
      executables (PIE) by default.
e354a5
 
e354a5
 '--enable-cet'
e354a5
+'--enable-cet=permissive'
e354a5
      Enable Intel Control-flow Enforcement Technology (CET) support.
e354a5
-     When the GNU C Library is built with '--enable-cet', the resulting
e354a5
-     library is protected with indirect branch tracking (IBT) and shadow
e354a5
-     stack (SHSTK).  When CET is enabled, the GNU C Library is
e354a5
-     compatible with all existing executables and shared libraries.
e354a5
-     This feature is currently supported on i386, x86_64 and x32 with
e354a5
-     GCC 8 and binutils 2.29 or later.  Note that when CET is enabled,
e354a5
-     the GNU C Library requires CPUs capable of multi-byte NOPs, like
e354a5
-     x86-64 processors as well as Intel Pentium Pro or newer.
e354a5
+     When the GNU C Library is built with '--enable-cet' or
e354a5
+     '--enable-cet=permissive', the resulting library is protected with
e354a5
+     indirect branch tracking (IBT) and shadow stack (SHSTK).  When CET
e354a5
+     is enabled, the GNU C Library is compatible with all existing
e354a5
+     executables and shared libraries.  This feature is currently
e354a5
+     supported on i386, x86_64 and x32 with GCC 8 and binutils 2.29 or
e354a5
+     later.  Note that when CET is enabled, the GNU C Library requires
e354a5
+     CPUs capable of multi-byte NOPs, like x86-64 processors as well as
e354a5
+     Intel Pentium Pro or newer.  With '--enable-cet', it is an error to
e354a5
+     dlopen a non CET enabled shared library in CET enabled application.
e354a5
+     With '--enable-cet=permissive', CET is disabled when dlopening a
e354a5
+     non CET enabled shared library in CET enabled application.
e354a5
 
e354a5
      NOTE: '--enable-cet' has been tested for i686, x86_64 and x32 on
e354a5
-     non-CET processors.  '--enable-cet' has been tested for x86_64 and
e354a5
-     x32 on CET SDVs, but Intel CET support hasn't been validated for
e354a5
-     i686.
e354a5
+     non-CET processors.  '--enable-cet' has been tested for i686,
e354a5
+     x86_64 and x32 on CET processors.
e354a5
 
e354a5
 '--disable-profile'
e354a5
      Don't build libraries with profiling information.  You may want to
e354a5
diff --git a/config.h.in b/config.h.in
e354a5
index f63f6c8..8520b0f 100644
e354a5
--- a/config.h.in
e354a5
+++ b/config.h.in
e354a5
@@ -259,4 +259,7 @@
e354a5
    in i386 6 argument syscall issue).  */
e354a5
 #define CAN_USE_REGISTER_ASM_EBP 0
e354a5
 
e354a5
+/* The default value of x86 CET control.  */
e354a5
+#define DEFAULT_DL_X86_CET_CONTROL cet_elf_property
e354a5
+
e354a5
 #endif
e354a5
diff --git a/manual/install.texi b/manual/install.texi
e354a5
index 351d67c..7e9f2be 100644
e354a5
--- a/manual/install.texi
e354a5
+++ b/manual/install.texi
e354a5
@@ -147,20 +147,24 @@ PIE.  This option also implies that glibc programs and tests are created
e354a5
 as dynamic position independent executables (PIE) by default.
e354a5
 
e354a5
 @item --enable-cet
e354a5
+@itemx --enable-cet=permissive
e354a5
 Enable Intel Control-flow Enforcement Technology (CET) support.  When
e354a5
-@theglibc{} is built with @option{--enable-cet}, the resulting library
e354a5
+@theglibc{} is built with @option{--enable-cet} or
e354a5
+@option{--enable-cet=permissive}, the resulting library
e354a5
 is protected with indirect branch tracking (IBT) and shadow stack
e354a5
 (SHSTK)@.  When CET is enabled, @theglibc{} is compatible with all
e354a5
 existing executables and shared libraries.  This feature is currently
e354a5
 supported on i386, x86_64 and x32 with GCC 8 and binutils 2.29 or later.
e354a5
 Note that when CET is enabled, @theglibc{} requires CPUs capable of
e354a5
 multi-byte NOPs, like x86-64 processors as well as Intel Pentium Pro or
e354a5
-newer.
e354a5
+newer.  With @option{--enable-cet}, it is an error to dlopen a non CET
e354a5
+enabled shared library in CET enabled application.  With
e354a5
+@option{--enable-cet=permissive}, CET is disabled when dlopening a
e354a5
+non CET enabled shared library in CET enabled application.
e354a5
 
e354a5
 NOTE: @option{--enable-cet} has been tested for i686, x86_64 and x32
e354a5
 on non-CET processors.  @option{--enable-cet} has been tested for
e354a5
-x86_64 and x32 on CET SDVs, but Intel CET support hasn't been validated
e354a5
-for i686.
e354a5
+i686, x86_64 and x32 on CET processors.
e354a5
 
e354a5
 @item --disable-profile
e354a5
 Don't build libraries with profiling information.  You may want to use
e354a5
diff --git a/sysdeps/unix/sysv/linux/x86/Makefile b/sysdeps/unix/sysv/linux/x86/Makefile
e354a5
index 7dc4e61..8ac2fd4 100644
e354a5
--- a/sysdeps/unix/sysv/linux/x86/Makefile
e354a5
+++ b/sysdeps/unix/sysv/linux/x86/Makefile
e354a5
@@ -24,7 +24,7 @@ ifeq ($(subdir),setjmp)
e354a5
 tests += tst-saved_mask-1
e354a5
 endif
e354a5
 
e354a5
-ifeq ($(enable-cet),yes)
e354a5
+ifneq ($(enable-cet),no)
e354a5
 ifeq ($(subdir),elf)
e354a5
 tests += tst-cet-property-1 tst-cet-property-2
e354a5
 
e354a5
diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile
e354a5
index d9bdf0b..d5f821e 100644
e354a5
--- a/sysdeps/x86/Makefile
e354a5
+++ b/sysdeps/x86/Makefile
e354a5
@@ -14,7 +14,7 @@ gen-as-const-headers += jmp_buf-ssp.sym
e354a5
 sysdep_routines += __longjmp_cancel
e354a5
 endif
e354a5
 
e354a5
-ifeq ($(enable-cet),yes)
e354a5
+ifneq ($(enable-cet),no)
e354a5
 ifeq ($(subdir),elf)
e354a5
 sysdep-dl-routines += dl-cet
e354a5
 
e354a5
@@ -41,13 +41,21 @@ CFLAGS-tst-cet-legacy-4.c += -fcf-protection=branch
e354a5
 CFLAGS-tst-cet-legacy-4a.c += -fcf-protection
e354a5
 CFLAGS-tst-cet-legacy-4b.c += -fcf-protection
e354a5
 CFLAGS-tst-cet-legacy-mod-4.c += -fcf-protection=none
e354a5
-CFLAGS-tst-cet-legacy-5a.c += -fcf-protection
e354a5
-CFLAGS-tst-cet-legacy-5b.c += -fcf-protection
e354a5
+CFLAGS-tst-cet-legacy-5a.c += -fcf-protection -mshstk
e354a5
+ifeq ($(enable-cet),permissive)
e354a5
+CPPFLAGS-tst-cet-legacy-5a.c += -DCET_IS_PERMISSIVE=1
e354a5
+endif
e354a5
+CFLAGS-tst-cet-legacy-5b.c += -fcf-protection -mshstk
e354a5
+CPPFLAGS-tst-cet-legacy-5b.c += -DCET_DISABLED_BY_ENV=1
e354a5
 CFLAGS-tst-cet-legacy-mod-5a.c += -fcf-protection=branch
e354a5
 CFLAGS-tst-cet-legacy-mod-5b.c += -fcf-protection
e354a5
 CFLAGS-tst-cet-legacy-mod-5c.c += -fcf-protection
e354a5
-CFLAGS-tst-cet-legacy-6a.c += -fcf-protection
e354a5
-CFLAGS-tst-cet-legacy-6b.c += -fcf-protection
e354a5
+CFLAGS-tst-cet-legacy-6a.c += -fcf-protection -mshstk
e354a5
+ifeq ($(enable-cet),permissive)
e354a5
+CPPFLAGS-tst-cet-legacy-6a.c += -DCET_IS_PERMISSIVE=1
e354a5
+endif
e354a5
+CFLAGS-tst-cet-legacy-6b.c += -fcf-protection -mshstk
e354a5
+CPPFLAGS-tst-cet-legacy-6b.c += -DCET_DISABLED_BY_ENV=1
e354a5
 CFLAGS-tst-cet-legacy-mod-6a.c += -fcf-protection=branch
e354a5
 CFLAGS-tst-cet-legacy-mod-6b.c += -fcf-protection
e354a5
 CFLAGS-tst-cet-legacy-mod-6c.c += -fcf-protection
e354a5
diff --git a/sysdeps/x86/configure b/sysdeps/x86/configure
e354a5
index b1ff281..81cc4e8 100644
e354a5
--- a/sysdeps/x86/configure
e354a5
+++ b/sysdeps/x86/configure
e354a5
@@ -1,7 +1,7 @@
e354a5
 # This file is generated from configure.ac by Autoconf.  DO NOT EDIT!
e354a5
  # Local configure fragment for sysdeps/x86.
e354a5
 
e354a5
-if test x"$enable_cet" = xyes; then
e354a5
+if test $enable_cet != no; then
e354a5
   # Check if CET can be enabled.
e354a5
   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether CET can be enabled" >&5
e354a5
 $as_echo_n "checking whether CET can be enabled... " >&6; }
e354a5
@@ -27,17 +27,11 @@ EOF
e354a5
 fi
e354a5
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_x86_cet_available" >&5
e354a5
 $as_echo "$libc_cv_x86_cet_available" >&6; }
e354a5
-  if test $libc_cv_x86_cet_available = yes; then
e354a5
-    enable_cet=yes
e354a5
-  else
e354a5
-    if test x"$enable_cet" = xdefault; then
e354a5
-      enable_cet=no
e354a5
-    else
e354a5
-      as_fn_error $? "$CC doesn't support CET" "$LINENO" 5
e354a5
-    fi
e354a5
+  if test $libc_cv_x86_cet_available != yes; then
e354a5
+    as_fn_error $? "$CC doesn't support CET" "$LINENO" 5
e354a5
   fi
e354a5
 fi
e354a5
-if test $enable_cet = yes; then
e354a5
+if test $enable_cet != no; then
e354a5
   # Check if assembler supports CET.
e354a5
   { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $AS supports CET" >&5
e354a5
 $as_echo_n "checking whether $AS supports CET... " >&6; }
e354a5
@@ -65,5 +59,12 @@ $as_echo "$libc_cv_x86_cet_as" >&6; }
e354a5
     as_fn_error $? "$AS doesn't support CET" "$LINENO" 5
e354a5
   fi
e354a5
 fi
e354a5
+if test $enable_cet = yes; then
e354a5
+  $as_echo "#define DEFAULT_DL_X86_CET_CONTROL cet_elf_property" >>confdefs.h
e354a5
+
e354a5
+elif test $enable_cet = permissive; then
e354a5
+  $as_echo "#define DEFAULT_DL_X86_CET_CONTROL cet_permissive" >>confdefs.h
e354a5
+
e354a5
+fi
e354a5
 config_vars="$config_vars
e354a5
 enable-cet = $enable_cet"
e354a5
diff --git a/sysdeps/x86/configure.ac b/sysdeps/x86/configure.ac
e354a5
index a909b07..8f3e119 100644
e354a5
--- a/sysdeps/x86/configure.ac
e354a5
+++ b/sysdeps/x86/configure.ac
e354a5
@@ -1,7 +1,7 @@
e354a5
 GLIBC_PROVIDES dnl See aclocal.m4 in the top level source directory.
e354a5
 # Local configure fragment for sysdeps/x86.
e354a5
 
e354a5
-if test x"$enable_cet" = xyes; then
e354a5
+if test $enable_cet != no; then
e354a5
   # Check if CET can be enabled.
e354a5
   AC_CACHE_CHECK(whether CET can be enabled,
e354a5
 		 libc_cv_x86_cet_available, [dnl
e354a5
@@ -16,17 +16,11 @@ EOF
e354a5
 		   libc_cv_x86_cet_available=no
e354a5
 		 fi
e354a5
 		 rm -rf conftest*])
e354a5
-  if test $libc_cv_x86_cet_available = yes; then
e354a5
-    enable_cet=yes
e354a5
-  else
e354a5
-    if test x"$enable_cet" = xdefault; then
e354a5
-      enable_cet=no
e354a5
-    else
e354a5
-      AC_MSG_ERROR([$CC doesn't support CET])
e354a5
-    fi
e354a5
+  if test $libc_cv_x86_cet_available != yes; then
e354a5
+    AC_MSG_ERROR([$CC doesn't support CET])
e354a5
   fi
e354a5
 fi
e354a5
-if test $enable_cet = yes; then
e354a5
+if test $enable_cet != no; then
e354a5
   # Check if assembler supports CET.
e354a5
   AC_CACHE_CHECK(whether $AS supports CET,
e354a5
 		 libc_cv_x86_cet_as, [dnl
e354a5
@@ -43,4 +37,9 @@ EOF
e354a5
     AC_MSG_ERROR([$AS doesn't support CET])
e354a5
   fi
e354a5
 fi
e354a5
+if test $enable_cet = yes; then
e354a5
+  AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_elf_property)
e354a5
+elif test $enable_cet = permissive; then
e354a5
+  AC_DEFINE(DEFAULT_DL_X86_CET_CONTROL, cet_permissive)
e354a5
+fi
e354a5
 LIBC_CONFIG_VAR([enable-cet], [$enable_cet])
e354a5
diff --git a/sysdeps/x86/dl-procruntime.c b/sysdeps/x86/dl-procruntime.c
e354a5
index 26b2b39..72b16fa 100644
e354a5
--- a/sysdeps/x86/dl-procruntime.c
e354a5
+++ b/sysdeps/x86/dl-procruntime.c
e354a5
@@ -65,8 +65,8 @@ PROCINFO_CLASS struct dl_x86_feature_control _dl_x86_feature_control
e354a5
 # endif
e354a5
 # ifndef PROCINFO_DECL
e354a5
 = {
e354a5
-    .ibt = cet_elf_property,
e354a5
-    .shstk = cet_elf_property
e354a5
+    .ibt = DEFAULT_DL_X86_CET_CONTROL,
e354a5
+    .shstk = DEFAULT_DL_X86_CET_CONTROL,
e354a5
   }
e354a5
 # endif
e354a5
 # if !defined SHARED || defined PROCINFO_DECL
e354a5
diff --git a/sysdeps/x86/tst-cet-legacy-5.c b/sysdeps/x86/tst-cet-legacy-5.c
e354a5
index 0a34d37..c578979 100644
e354a5
--- a/sysdeps/x86/tst-cet-legacy-5.c
e354a5
+++ b/sysdeps/x86/tst-cet-legacy-5.c
e354a5
@@ -22,6 +22,14 @@
e354a5
 #include <stdlib.h>
e354a5
 #include <stdbool.h>
e354a5
 #include <string.h>
e354a5
+#include <x86intrin.h>
e354a5
+#include <support/check.h>
e354a5
+
e354a5
+#if defined CET_IS_PERMISSIVE || defined CET_DISABLED_BY_ENV
e354a5
+# define CET_MAYBE_DISABLED 1
e354a5
+#else
e354a5
+# define CET_MAYBE_DISABLED 0
e354a5
+#endif
e354a5
 
e354a5
 static void
e354a5
 do_test_1 (const char *modname, bool fail)
e354a5
@@ -32,24 +40,25 @@ do_test_1 (const char *modname, bool fail)
e354a5
   h = dlopen (modname, RTLD_LAZY);
e354a5
   if (h == NULL)
e354a5
     {
e354a5
+      const char *err = dlerror ();
e354a5
       if (fail)
e354a5
 	{
e354a5
-	  const char *err = dlerror ();
e354a5
 	  if (strstr (err, "rebuild shared object with SHSTK support enabled")
e354a5
 	      == NULL)
e354a5
-	    {
e354a5
-	      printf ("incorrect dlopen '%s' error: %s\n", modname,
e354a5
-		      err);
e354a5
-	      exit (1);
e354a5
-	    }
e354a5
+	    FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err);
e354a5
 
e354a5
 	  return;
e354a5
 	}
e354a5
 
e354a5
-      printf ("cannot open '%s': %s\n", modname, dlerror ());
e354a5
-      exit (1);
e354a5
+      FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err);
e354a5
     }
e354a5
 
e354a5
+  /* NB: dlopen should never fail on non-CET platforms.  If SHSTK is
e354a5
+     disabled, assuming IBT is also disabled.  */
e354a5
+  bool cet_enabled = _get_ssp () != 0 && !CET_MAYBE_DISABLED;
e354a5
+  if (fail && cet_enabled)
e354a5
+    FAIL_EXIT1 ("dlopen should have failed\n");
e354a5
+
e354a5
   fp = dlsym (h, "test");
e354a5
   if (fp == NULL)
e354a5
     {
e354a5
diff --git a/sysdeps/x86/tst-cet-legacy-6.c b/sysdeps/x86/tst-cet-legacy-6.c
e354a5
index bd45218..78e72ba 100644
e354a5
--- a/sysdeps/x86/tst-cet-legacy-6.c
e354a5
+++ b/sysdeps/x86/tst-cet-legacy-6.c
e354a5
@@ -22,6 +22,14 @@
e354a5
 #include <stdlib.h>
e354a5
 #include <stdbool.h>
e354a5
 #include <string.h>
e354a5
+#include <x86intrin.h>
e354a5
+#include <support/check.h>
e354a5
+
e354a5
+#if defined CET_IS_PERMISSIVE || defined CET_DISABLED_BY_ENV
e354a5
+# define CET_MAYBE_DISABLED 1
e354a5
+#else
e354a5
+# define CET_MAYBE_DISABLED 0
e354a5
+#endif
e354a5
 
e354a5
 static void
e354a5
 do_test_1 (const char *modname, bool fail)
e354a5
@@ -32,24 +40,25 @@ do_test_1 (const char *modname, bool fail)
e354a5
   h = dlopen (modname, RTLD_LAZY);
e354a5
   if (h == NULL)
e354a5
     {
e354a5
+      const char *err = dlerror ();
e354a5
       if (fail)
e354a5
 	{
e354a5
-	  const char *err = dlerror ();
e354a5
 	  if (strstr (err, "rebuild shared object with SHSTK support enabled")
e354a5
 	      == NULL)
e354a5
-	    {
e354a5
-	      printf ("incorrect dlopen '%s' error: %s\n", modname,
e354a5
-		      err);
e354a5
-	      exit (1);
e354a5
-	    }
e354a5
+	    FAIL_EXIT1 ("incorrect dlopen '%s' error: %s\n", modname, err);
e354a5
 
e354a5
 	  return;
e354a5
 	}
e354a5
 
e354a5
-      printf ("cannot open '%s': %s\n", modname, dlerror ());
e354a5
-      exit (1);
e354a5
+      FAIL_EXIT1 ("cannot open '%s': %s\n", modname, err);
e354a5
     }
e354a5
 
e354a5
+  /* NB: dlopen should never fail on non-CET platforms.  If SHSTK is
e354a5
+     disabled, assuming IBT is also disabled.  */
e354a5
+  bool cet_enabled = _get_ssp () != 0 && !CET_MAYBE_DISABLED;
e354a5
+  if (fail && cet_enabled)
e354a5
+    FAIL_EXIT1 ("dlopen should have failed\n");
e354a5
+
e354a5
   fp = dlsym (h, "test");
e354a5
   if (fp == NULL)
e354a5
     {
e354a5