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