446cf2
commit 674ea88294bfb8d89878a0ebbbcec38a85e118a5
446cf2
Author: H.J. Lu <hjl.tools@gmail.com>
446cf2
Date:   Tue Apr 28 10:05:25 2020 -0700
446cf2
446cf2
    x86: Move CET control to _dl_x86_feature_control [BZ #25887]
446cf2
    
446cf2
    1. Include <dl-procruntime.c> to get architecture specific initializer in
446cf2
    rtld_global.
446cf2
    2. Change _dl_x86_feature_1[2] to _dl_x86_feature_1.
446cf2
    3. Add _dl_x86_feature_control after _dl_x86_feature_1, which is a
446cf2
    struct of 2 bitfields for IBT and SHSTK control
446cf2
    
446cf2
    This fixes [BZ #25887].
446cf2
---
446cf2
446cf2
diff --git a/elf/rtld.c b/elf/rtld.c
446cf2
index e107bd1..7f030f7 100644
446cf2
--- a/elf/rtld.c
446cf2
+++ b/elf/rtld.c
446cf2
@@ -230,6 +230,8 @@ rtld_hidden_def (_dl_starting_up)
446cf2
    (except those which cannot be added for some reason).  */
446cf2
 struct rtld_global _rtld_global =
446cf2
   {
446cf2
+    /* Get architecture specific initializer.  */
446cf2
+#include <dl-procruntime.c>
446cf2
     /* Generally the default presumption without further information is an
446cf2
      * executable stack but this is not true for all platforms.  */
446cf2
     ._dl_stack_flags = DEFAULT_STACK_PERMS,
446cf2
diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
446cf2
index f6cfb90..8c959e3 100644
446cf2
--- a/sysdeps/i386/dl-machine.h
446cf2
+++ b/sysdeps/i386/dl-machine.h
446cf2
@@ -71,7 +71,7 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
446cf2
   extern void _dl_runtime_profile_shstk (Elf32_Word) attribute_hidden;
446cf2
   /* Check if SHSTK is enabled by kernel.  */
446cf2
   bool shstk_enabled
446cf2
-    = (GL(dl_x86_feature_1)[0] & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0;
446cf2
+    = (GL(dl_x86_feature_1) & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0;
446cf2
 
446cf2
   if (l->l_info[DT_JMPREL] && lazy)
446cf2
     {
446cf2
diff --git a/sysdeps/unix/sysv/linux/x86/cpu-features.c b/sysdeps/unix/sysv/linux/x86/cpu-features.c
446cf2
index 8566a26..9f40624 100644
446cf2
--- a/sysdeps/unix/sysv/linux/x86/cpu-features.c
446cf2
+++ b/sysdeps/unix/sysv/linux/x86/cpu-features.c
446cf2
@@ -36,7 +36,7 @@ static inline void
446cf2
 x86_setup_tls (void)
446cf2
 {
446cf2
   __libc_setup_tls ();
446cf2
-  THREAD_SETMEM (THREAD_SELF, header.feature_1, GL(dl_x86_feature_1)[0]);
446cf2
+  THREAD_SETMEM (THREAD_SELF, header.feature_1, GL(dl_x86_feature_1));
446cf2
 }
446cf2
 
446cf2
 #  define ARCH_SETUP_TLS() x86_setup_tls ()
446cf2
diff --git a/sysdeps/x86/cet-control.h b/sysdeps/x86/cet-control.h
446cf2
new file mode 100644
446cf2
index 0000000..7b29f95
446cf2
--- /dev/null
446cf2
+++ b/sysdeps/x86/cet-control.h
446cf2
@@ -0,0 +1,41 @@
446cf2
+/* x86 CET tuning.
446cf2
+   This file is part of the GNU C Library.
446cf2
+   Copyright (C) 2018 Free Software Foundation, Inc.
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
+   <http://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#ifndef _CET_CONTROL_H
446cf2
+#define _CET_CONTROL_H
446cf2
+
446cf2
+/* For each CET feature, IBT and SHSTK, valid control values.  */
446cf2
+enum dl_x86_cet_control
446cf2
+{
446cf2
+  /* Enable CET features based on ELF property note.  */
446cf2
+  cet_elf_property = 0,
446cf2
+  /* Always enable CET features.  */
446cf2
+  cet_always_on,
446cf2
+  /* Always disable CET features.  */
446cf2
+  cet_always_off,
446cf2
+  /* Enable CET features permissively.  */
446cf2
+  cet_permissive
446cf2
+};
446cf2
+
446cf2
+struct dl_x86_feature_control
446cf2
+{
446cf2
+  enum dl_x86_cet_control ibt : 2;
446cf2
+  enum dl_x86_cet_control shstk : 2;
446cf2
+};
446cf2
+
446cf2
+#endif /* cet-control.h */
446cf2
diff --git a/sysdeps/x86/cet-tunables.h b/sysdeps/x86/cet-tunables.h
446cf2
deleted file mode 100644
446cf2
index ca02305..0000000
446cf2
--- a/sysdeps/x86/cet-tunables.h
446cf2
+++ /dev/null
446cf2
@@ -1,29 +0,0 @@
446cf2
-/* x86 CET tuning.
446cf2
-   This file is part of the GNU C Library.
446cf2
-   Copyright (C) 2018 Free Software Foundation, Inc.
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
-   <http://www.gnu.org/licenses/>.  */
446cf2
-
446cf2
-/* Valid control values:
446cf2
-   0: Enable CET features based on ELF property note.
446cf2
-   1: Always disable CET features.
446cf2
-   2: Always enable CET features.
446cf2
-   3: Enable CET features permissively.
446cf2
- */
446cf2
-#define CET_ELF_PROPERTY	0
446cf2
-#define CET_ALWAYS_OFF		1
446cf2
-#define CET_ALWAYS_ON		2
446cf2
-#define CET_PERMISSIVE		3
446cf2
-#define CET_MAX			CET_PERMISSIVE
446cf2
diff --git a/sysdeps/x86/cpu-features.c b/sysdeps/x86/cpu-features.c
446cf2
index 4695ac8..ac74f40 100644
446cf2
--- a/sysdeps/x86/cpu-features.c
446cf2
+++ b/sysdeps/x86/cpu-features.c
446cf2
@@ -39,7 +39,6 @@ extern void TUNABLE_CALLBACK (set_x86_shstk) (tunable_val_t *)
446cf2
 
446cf2
 #if CET_ENABLED
446cf2
 # include <dl-cet.h>
446cf2
-# include <cet-tunables.h>
446cf2
 #endif
446cf2
 
446cf2
 static void
446cf2
@@ -490,7 +489,7 @@ no_cpuid:
446cf2
 
446cf2
   if (cet_status)
446cf2
     {
446cf2
-      GL(dl_x86_feature_1)[0] = cet_status;
446cf2
+      GL(dl_x86_feature_1) = cet_status;
446cf2
 
446cf2
 # ifndef SHARED
446cf2
       /* Check if IBT and SHSTK are enabled by kernel.  */
446cf2
@@ -514,14 +513,13 @@ no_cpuid:
446cf2
 
446cf2
 	      /* Clear the disabled bits in dl_x86_feature_1.  */
446cf2
 	      if (res == 0)
446cf2
-		GL(dl_x86_feature_1)[0] &= ~cet_feature;
446cf2
+		GL(dl_x86_feature_1) &= ~cet_feature;
446cf2
 	    }
446cf2
 
446cf2
 	  /* Lock CET if IBT or SHSTK is enabled in executable.  Don't
446cf2
-	     lock CET if SHSTK is enabled permissively.  */
446cf2
-	  if (((GL(dl_x86_feature_1)[1] >> CET_MAX)
446cf2
-	       & ((1 << CET_MAX) - 1))
446cf2
-	       != CET_PERMISSIVE)
446cf2
+	     lock CET if IBT or SHSTK is enabled permissively.  */
446cf2
+	  if (GL(dl_x86_feature_control).ibt != cet_permissive
446cf2
+	      && GL(dl_x86_feature_control).shstk != cet_permissive)
446cf2
 	    dl_cet_lock_cet ();
446cf2
 	}
446cf2
 # endif
446cf2
diff --git a/sysdeps/x86/cpu-tunables.c b/sysdeps/x86/cpu-tunables.c
446cf2
index 69155a8..fad6726 100644
446cf2
--- a/sysdeps/x86/cpu-tunables.c
446cf2
+++ b/sysdeps/x86/cpu-tunables.c
446cf2
@@ -336,28 +336,18 @@ TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp)
446cf2
 }
446cf2
 
446cf2
 # if CET_ENABLED
446cf2
-#  include <cet-tunables.h>
446cf2
 
446cf2
 attribute_hidden
446cf2
 void
446cf2
 TUNABLE_CALLBACK (set_x86_ibt) (tunable_val_t *valp)
446cf2
 {
446cf2
   if (DEFAULT_MEMCMP (valp->strval, "on", sizeof ("on")) == 0)
446cf2
-    {
446cf2
-      GL(dl_x86_feature_1)[1] &= ~((1 << CET_MAX) - 1);
446cf2
-      GL(dl_x86_feature_1)[1] |= CET_ALWAYS_ON;
446cf2
-    }
446cf2
+    GL(dl_x86_feature_control).ibt = cet_always_on;
446cf2
   else if (DEFAULT_MEMCMP (valp->strval, "off", sizeof ("off")) == 0)
446cf2
-    {
446cf2
-      GL(dl_x86_feature_1)[1] &= ~((1 << CET_MAX) - 1);
446cf2
-      GL(dl_x86_feature_1)[1] |= CET_ALWAYS_OFF;
446cf2
-    }
446cf2
+    GL(dl_x86_feature_control).ibt = cet_always_off;
446cf2
   else if (DEFAULT_MEMCMP (valp->strval, "permissive",
446cf2
 			   sizeof ("permissive")) == 0)
446cf2
-    {
446cf2
-      GL(dl_x86_feature_1)[1] &= ~((1 << CET_MAX) - 1);
446cf2
-      GL(dl_x86_feature_1)[1] |= CET_PERMISSIVE;
446cf2
-    }
446cf2
+    GL(dl_x86_feature_control).ibt = cet_permissive;
446cf2
 }
446cf2
 
446cf2
 attribute_hidden
446cf2
@@ -365,21 +355,12 @@ void
446cf2
 TUNABLE_CALLBACK (set_x86_shstk) (tunable_val_t *valp)
446cf2
 {
446cf2
   if (DEFAULT_MEMCMP (valp->strval, "on", sizeof ("on")) == 0)
446cf2
-    {
446cf2
-      GL(dl_x86_feature_1)[1] &= ~(((1 << CET_MAX) - 1) << CET_MAX);
446cf2
-      GL(dl_x86_feature_1)[1] |= (CET_ALWAYS_ON << CET_MAX);
446cf2
-    }
446cf2
+    GL(dl_x86_feature_control).shstk = cet_always_on;
446cf2
   else if (DEFAULT_MEMCMP (valp->strval, "off", sizeof ("off")) == 0)
446cf2
-    {
446cf2
-      GL(dl_x86_feature_1)[1] &= ~(((1 << CET_MAX) - 1) << CET_MAX);
446cf2
-      GL(dl_x86_feature_1)[1] |= (CET_ALWAYS_OFF << CET_MAX);
446cf2
-    }
446cf2
+    GL(dl_x86_feature_control).shstk = cet_always_off;
446cf2
   else if (DEFAULT_MEMCMP (valp->strval, "permissive",
446cf2
 			   sizeof ("permissive")) == 0)
446cf2
-    {
446cf2
-      GL(dl_x86_feature_1)[1] &= ~(((1 << CET_MAX) - 1) << CET_MAX);
446cf2
-      GL(dl_x86_feature_1)[1] |= (CET_PERMISSIVE << CET_MAX);
446cf2
-    }
446cf2
+    GL(dl_x86_feature_control).shstk = cet_permissive;
446cf2
 }
446cf2
 # endif
446cf2
 #endif
446cf2
diff --git a/sysdeps/x86/dl-cet.c b/sysdeps/x86/dl-cet.c
446cf2
index 627d937..ebc0d57 100644
446cf2
--- a/sysdeps/x86/dl-cet.c
446cf2
+++ b/sysdeps/x86/dl-cet.c
446cf2
@@ -20,7 +20,6 @@
446cf2
 #include <libintl.h>
446cf2
 #include <ldsodefs.h>
446cf2
 #include <dl-cet.h>
446cf2
-#include <cet-tunables.h>
446cf2
 
446cf2
 /* GNU_PROPERTY_X86_FEATURE_1_IBT and GNU_PROPERTY_X86_FEATURE_1_SHSTK
446cf2
    are defined in <elf.h>, which are only available for C sources.
446cf2
@@ -39,23 +38,23 @@ static void
446cf2
 dl_cet_check (struct link_map *m, const char *program)
446cf2
 {
446cf2
   /* Check how IBT should be enabled.  */
446cf2
-  unsigned int enable_ibt_type
446cf2
-    = GL(dl_x86_feature_1)[1] & ((1 << CET_MAX) - 1);
446cf2
+  enum dl_x86_cet_control enable_ibt_type
446cf2
+    = GL(dl_x86_feature_control).ibt;
446cf2
   /* Check how SHSTK should be enabled.  */
446cf2
-  unsigned int enable_shstk_type
446cf2
-    = ((GL(dl_x86_feature_1)[1] >> CET_MAX) & ((1 << CET_MAX) - 1));
446cf2
+  enum dl_x86_cet_control enable_shstk_type
446cf2
+    = GL(dl_x86_feature_control).shstk;
446cf2
 
446cf2
   /* No legacy object check if both IBT and SHSTK are always on.  */
446cf2
-  if (enable_ibt_type == CET_ALWAYS_ON
446cf2
-      && enable_shstk_type == CET_ALWAYS_ON)
446cf2
+  if (enable_ibt_type == cet_always_on
446cf2
+      && enable_shstk_type == cet_always_on)
446cf2
     return;
446cf2
 
446cf2
   /* Check if IBT is enabled by kernel.  */
446cf2
   bool ibt_enabled
446cf2
-    = (GL(dl_x86_feature_1)[0] & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0;
446cf2
+    = (GL(dl_x86_feature_1) & GNU_PROPERTY_X86_FEATURE_1_IBT) != 0;
446cf2
   /* Check if SHSTK is enabled by kernel.  */
446cf2
   bool shstk_enabled
446cf2
-    = (GL(dl_x86_feature_1)[0] & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0;
446cf2
+    = (GL(dl_x86_feature_1) & GNU_PROPERTY_X86_FEATURE_1_SHSTK) != 0;
446cf2
 
446cf2
   if (ibt_enabled || shstk_enabled)
446cf2
     {
446cf2
@@ -65,9 +64,9 @@ dl_cet_check (struct link_map *m, const char *program)
446cf2
 
446cf2
       /* Check if IBT and SHSTK are enabled in object.  */
446cf2
       bool enable_ibt = (ibt_enabled
446cf2
-			 && enable_ibt_type != CET_ALWAYS_OFF);
446cf2
+			 && enable_ibt_type != cet_always_off);
446cf2
       bool enable_shstk = (shstk_enabled
446cf2
-			   && enable_shstk_type != CET_ALWAYS_OFF);
446cf2
+			   && enable_shstk_type != cet_always_off);
446cf2
       if (program)
446cf2
 	{
446cf2
 	  /* Enable IBT and SHSTK only if they are enabled in executable.
446cf2
@@ -76,10 +75,10 @@ dl_cet_check (struct link_map *m, const char *program)
446cf2
 	     GLIBC_TUNABLES=glibc.tune.hwcaps=-IBT,-SHSTK
446cf2
 	   */
446cf2
 	  enable_ibt &= (HAS_CPU_FEATURE (IBT)
446cf2
-			 && (enable_ibt_type == CET_ALWAYS_ON
446cf2
+			 && (enable_ibt_type == cet_always_on
446cf2
 			     || (m->l_cet & lc_ibt) != 0));
446cf2
 	  enable_shstk &= (HAS_CPU_FEATURE (SHSTK)
446cf2
-			   && (enable_shstk_type == CET_ALWAYS_ON
446cf2
+			   && (enable_shstk_type == cet_always_on
446cf2
 			       || (m->l_cet & lc_shstk) != 0));
446cf2
 	}
446cf2
 
446cf2
@@ -111,7 +110,7 @@ dl_cet_check (struct link_map *m, const char *program)
446cf2
 
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
+	      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
@@ -121,7 +120,7 @@ dl_cet_check (struct link_map *m, const char *program)
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
+	      enable_shstk &= (enable_shstk_type == cet_always_on
446cf2
 			       || (l->l_cet & lc_shstk) != 0);
446cf2
 	      if (enable_shstk != shstk_enabled)
446cf2
 		{
446cf2
@@ -137,7 +136,7 @@ dl_cet_check (struct link_map *m, const char *program)
446cf2
 	{
446cf2
 	  if (!program)
446cf2
 	    {
446cf2
-	      if (enable_ibt_type != CET_PERMISSIVE)
446cf2
+	      if (enable_ibt_type != cet_permissive)
446cf2
 		{
446cf2
 		  /* When IBT is enabled, we cannot dlopen a shared
446cf2
 		     object without IBT.  */
446cf2
@@ -148,7 +147,7 @@ dl_cet_check (struct link_map *m, const char *program)
446cf2
 				      N_("rebuild shared object with IBT support enabled"));
446cf2
 		}
446cf2
 
446cf2
-	      if (enable_shstk_type != CET_PERMISSIVE)
446cf2
+	      if (enable_shstk_type != cet_permissive)
446cf2
 		{
446cf2
 		  /* When SHSTK is enabled, we cannot dlopen a shared
446cf2
 		     object without SHSTK.  */
446cf2
@@ -159,8 +158,8 @@ dl_cet_check (struct link_map *m, const char *program)
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
+	      if (enable_ibt_type != cet_permissive
446cf2
+		  && enable_shstk_type != cet_permissive)
446cf2
 		return;
446cf2
 	    }
446cf2
 
446cf2
@@ -190,7 +189,7 @@ dl_cet_check (struct link_map *m, const char *program)
446cf2
 	    }
446cf2
 
446cf2
 	  /* Clear the disabled bits in dl_x86_feature_1.  */
446cf2
-	  GL(dl_x86_feature_1)[0] &= ~cet_feature;
446cf2
+	  GL(dl_x86_feature_1) &= ~cet_feature;
446cf2
 
446cf2
 	  cet_feature_changed = true;
446cf2
 	}
446cf2
@@ -199,9 +198,9 @@ dl_cet_check (struct link_map *m, const char *program)
446cf2
       if (program && (ibt_enabled || shstk_enabled))
446cf2
 	{
446cf2
 	  if ((!ibt_enabled
446cf2
-	       || enable_ibt_type != CET_PERMISSIVE)
446cf2
+	       || enable_ibt_type != cet_permissive)
446cf2
 	      && (!shstk_enabled
446cf2
-		  || enable_shstk_type != CET_PERMISSIVE))
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
diff --git a/sysdeps/x86/dl-procruntime.c b/sysdeps/x86/dl-procruntime.c
446cf2
index 920bfe8..26b2b39 100644
446cf2
--- a/sysdeps/x86/dl-procruntime.c
446cf2
+++ b/sysdeps/x86/dl-procruntime.c
446cf2
@@ -47,7 +47,27 @@
446cf2
 # if !defined PROCINFO_DECL && defined SHARED
446cf2
   ._dl_x86_feature_1
446cf2
 # else
446cf2
-PROCINFO_CLASS unsigned int _dl_x86_feature_1[2]
446cf2
+PROCINFO_CLASS unsigned int _dl_x86_feature_1
446cf2
+# endif
446cf2
+# ifndef PROCINFO_DECL
446cf2
+= 0
446cf2
+# endif
446cf2
+# if !defined SHARED || defined PROCINFO_DECL
446cf2
+;
446cf2
+# else
446cf2
+,
446cf2
+# endif
446cf2
+
446cf2
+# if !defined PROCINFO_DECL && defined SHARED
446cf2
+  ._dl_x86_feature_control
446cf2
+# else
446cf2
+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
+  }
446cf2
 # endif
446cf2
 # if !defined SHARED || defined PROCINFO_DECL
446cf2
 ;
446cf2
diff --git a/sysdeps/x86/ldsodefs.h b/sysdeps/x86/ldsodefs.h
446cf2
index 0616215..54f6864 100644
446cf2
--- a/sysdeps/x86/ldsodefs.h
446cf2
+++ b/sysdeps/x86/ldsodefs.h
446cf2
@@ -61,6 +61,7 @@ struct La_x32_retval;
446cf2
 				     struct La_x86_64_retval *,		\
446cf2
 				     const char *)
446cf2
 
446cf2
+#include <cet-control.h>
446cf2
 #include_next <ldsodefs.h>
446cf2
 
446cf2
 #endif
446cf2