[v2,06/13] elf: Allow named values for integer tunables

Message ID 20261030173000.3245502-7-adhemerval.zanella@linaro.org (mailing list archive)
State Under Review
Delegated to: DJ Delorie
Headers
Series elf: Add loader hardening tunables |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed

Commit Message

Adhemerval Zanella Netto Oct. 30, 2026, 5:27 p.m. UTC
  A tunable that selects one of a few modes is clearer with names than
with numbers, but being a string tunable it imposes some restrictions:
its value is only valid until the program starts (string tunables are
sealed before any code outside of libc startup runs), and it is never
validated.

The new values attribute in dl-tunables.list names the values 0 to
maxval of an INT_32 tunable, which then only accepts these names from
GLIBC_TUNABLES, environment aliases, and /etc/tunables.conf.  The
tunable is still read as a number, and an invalid name leaves it unset,
as an out-of-range number does for other tunables.  ld.so
--list-tunables prints the name of the current value and the accepted
names.  gen-tunables.awk rejects a default without a name, and ld.so
--list-tunables prints a value without a name as a number instead of
reading past the names.

gen-tunables.awk also generates an enum type for each such tunable, so
code can compare its value against TUNABLE_VALUE constants of
TUNABLE_VALUE_TYPE instead of numbers.

Checked on x86_64-linux-gnu.
---
 elf/dl-tunables.c        | 64 ++++++++++++++++++++++++++++++++----
 elf/dl-tunables.h        | 11 +++++++
 elf/dl-tunables.list     |  4 +++
 scripts/gen-tunables.awk | 70 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 143 insertions(+), 6 deletions(-)
  

Patch

diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
index ee4a1aab31b..fcad75b02a1 100644
--- a/elf/dl-tunables.c
+++ b/elf/dl-tunables.c
@@ -148,6 +148,28 @@  tunable_parse_num (const char *strval, size_t len, tunable_num_t *val)
   return true;
 }
 
+/* Set *VAL to the position of the name STRVAL of length LEN in NAMES, as
+   returned by tunable_value_names.  */
+static bool
+tunable_parse_name (const char *names, const char *strval, size_t len,
+		    tunable_num_t *val)
+{
+  for (tunable_num_t i = 0; *names != '\0'; i++)
+    {
+      size_t j = 0;
+      while (j < len && names[j] == strval[j])
+	j++;
+      if (j == len && names[j] == '\0')
+	{
+	  *val = i;
+	  return true;
+	}
+      while (*names++ != '\0')
+	;
+    }
+  return false;
+}
+
 /* Validate range of the input value and initialize the tunable CUR if it looks
    good.  */
 static bool
@@ -157,7 +179,13 @@  tunable_initialize (tunable_t *cur, const char *strval, size_t len)
 
   if (cur->type.type_code != TUNABLE_TYPE_STRING)
     {
-      if (!tunable_parse_num (strval, len, &val.numval))
+      const char *names = tunable_value_names (cur - tunable_list);
+      if (names != NULL)
+	{
+	  if (!tunable_parse_name (names, strval, len, &val.numval))
+	    return false;
+	}
+      else if (!tunable_parse_num (strval, len, &val.numval))
 	return false;
     }
   else
@@ -456,7 +484,10 @@  __tunables_init (char **envp, char **argv)
 	  else
 	    {
 	      tunable_val_t tval;
-	      if (tec->flags & TUNCONF_FLAG_PARSED)
+	      /* ldconfig parses numbers, which a tunable with named values
+		 does not accept.  */
+	      if ((tec->flags & TUNCONF_FLAG_PARSED)
+		  && tunable_value_names (tid) == NULL)
 		{
 		  tval.numval = tec->parsed_value;
 		  do_tunable_update_val (& tunable_list[tid],
@@ -539,6 +570,23 @@  __tunables_init (char **envp, char **argv)
     }
 }
 
+/* Print the name of the value VAL of a tunable with the named values NAMES,
+   or VAL itself if it has no name, followed by all the names.  */
+static void
+tunable_print_names (const char *names, tunable_num_t val)
+{
+  const char *name = names;
+  for (tunable_num_t i = 0; i < val && *name != '\0'; i++)
+    name += strlen (name) + 1;
+  if (*name != '\0')
+    _dl_printf ("%s (values:", name);
+  else
+    _dl_printf ("%d (values:", (int) val);
+  for (const char *p = names; *p != '\0'; p += strlen (p) + 1)
+    _dl_printf ("%s %s", p == names ? "" : ",", p);
+  _dl_printf (")\n");
+}
+
 void
 __tunables_print (void)
 {
@@ -554,10 +602,14 @@  __tunables_print (void)
 	  switch (cur->type.type_code)
 	    {
 	    case TUNABLE_TYPE_INT_32:
-	      _dl_printf ("%d (min: %d, max: %d)\n",
-			  (int) cur->val.numval,
-			  (int) cur->type.min,
-			  (int) cur->type.max);
+	      if (tunable_value_names (i) != NULL)
+		tunable_print_names (tunable_value_names (i),
+				     cur->val.numval);
+	      else
+		_dl_printf ("%d (min: %d, max: %d)\n",
+			    (int) cur->val.numval,
+			    (int) cur->type.min,
+			    (int) cur->type.max);
 	      break;
 	    case TUNABLE_TYPE_UINT_64:
 	      _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h
index bc703553c11..44e02688348 100644
--- a/elf/dl-tunables.h
+++ b/elf/dl-tunables.h
@@ -45,6 +45,17 @@  typedef void (*tunable_callback_t) (tunable_val_t *);
 #define TUNABLE_ENUM_NAME(__top,__ns,__id) TUNABLE_ENUM_NAME1 (__top,__ns,__id)
 #define TUNABLE_ENUM_NAME1(__top,__ns,__id) __top ## _ ## __ns ## _ ## __id
 
+/* The enum type of a tunable with named values (see the values attribute in
+   dl-tunables.list), and the constant for its value VALUE.  */
+#define TUNABLE_VALUE_TYPE(__top,__ns,__id) \
+  TUNABLE_VALUE_TYPE1 (__top,__ns,__id)
+#define TUNABLE_VALUE_TYPE1(__top,__ns,__id) \
+  enum __top ## _ ## __ns ## _ ## __id ## _value
+#define TUNABLE_VALUE(__top,__ns,__id,__value) \
+  TUNABLE_VALUE1 (__top,__ns,__id,__value)
+#define TUNABLE_VALUE1(__top,__ns,__id,__value) \
+  __top ## _ ## __ns ## _ ## __id ## _value_ ## __value
+
 #include "dl-tunable-list.h"
 
 extern void __tunables_init (char **, char **);
diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
index 165424256d2..8fd5170692a 100644
--- a/elf/dl-tunables.list
+++ b/elf/dl-tunables.list
@@ -21,6 +21,10 @@ 
 # minval: Optional minimum acceptable value
 # maxval: Optional maximum acceptable value
 # default: Optional default value (if not specified it will be 0 or "")
+# values: Optional names, separated by spaces, of the values 0 to maxval of
+#	  an INT_32 tunable with minval 0, which then only accepts the names.
+#	  The values are the constants TUNABLE_VALUE (top, ns, id, name) of
+#	  the enum type TUNABLE_VALUE_TYPE (top, ns, id).
 # env_alias: An alias environment variable
 
 glibc {
diff --git a/scripts/gen-tunables.awk b/scripts/gen-tunables.awk
index 7c832303dbe..b2a4ffc0fdc 100644
--- a/scripts/gen-tunables.awk
+++ b/scripts/gen-tunables.awk
@@ -61,6 +61,29 @@  $1 == "}" {
     if (!env_alias[top_ns,ns,tunable]) {
       env_alias[top_ns,ns,tunable] = "{0}"
     }
+    if (values[top_ns,ns,tunable] != "") {
+      nvalues = split(values[top_ns,ns,tunable], vnames, " ")
+      if (types[top_ns,ns,tunable] != "INT_32" \
+	  || minvals[top_ns,ns,tunable] != 0 \
+	  || maxvals[top_ns,ns,tunable] != nvalues - 1) {
+	printf ("%s:%d: values needs type INT_32, minval 0 and maxval %d\n",
+		FILENAME, FNR, nvalues - 1)
+	exit 1
+      }
+      for (i = 1; i <= nvalues; i++) {
+	if (vnames[i] !~ /^[a-z_][a-z0-9_]*$/) {
+	  printf ("%s:%d: invalid value name: %s\n", FILENAME, FNR, vnames[i])
+	  exit 1
+	}
+      }
+      # The default is not range checked at run time.
+      def = defaults[top_ns,ns,tunable]
+      if (def != "" && (def !~ /^[0-9]+$/ || def + 0 > nvalues - 1)) {
+	printf ("%s:%d: default needs to be between 0 and %d\n",
+		FILENAME, FNR, nvalues - 1)
+	exit 1
+      }
+    }
     len = length(top_ns"."ns"."tunable)
     if (len > max_name_len)
       max_name_len = len
@@ -109,6 +132,9 @@  $1 == "}" {
   else if (attr == "maxval") {
     maxvals[top_ns,ns,tunable] = val
   }
+  else if (attr == "values") {
+    values[top_ns,ns,tunable] = val
+  }
   else if (attr == "env_alias") {
     env_alias[top_ns,ns,tunable] = sprintf("\"%s\"", val)
     len = length(val)
@@ -116,6 +142,7 @@  $1 == "}" {
       max_alias_len = len
   }
   else if (attr == "default") {
+    defaults[top_ns,ns,tunable] = val
     if (types[top_ns,ns,tunable] == "STRING") {
       default_val[top_ns,ns,tunable] = sprintf(".strval = \"%s\"", val);
     }
@@ -155,6 +182,23 @@  END {
   }
   print "} tunable_id_t;\n"
 
+  # The enum types of the tunables with named values.
+  for (i = 1; i <= typecount; i++) {
+    tnm = typesa[i];
+    split (tnm, indices, SUBSEP);
+    t = indices[1];
+    n = indices[2];
+    m = indices[3];
+    if (values[t,n,m] != "") {
+      nvalues = split (values[t,n,m], vnames, " ");
+      printf ("/* The values of %s.%s.%s.  */\n", t, n, m);
+      printf ("TUNABLE_VALUE_TYPE(%s, %s, %s)\n{\n", t, n, m);
+      for (j = 1; j <= nvalues; j++)
+	printf ("  TUNABLE_VALUE(%s, %s, %s, %s),\n", t, n, m, vnames[j]);
+      print "};\n"
+    }
+  }
+
   print "\n#ifdef TUNABLES_INTERNAL"
   # Internal definitions.
   print "# define TUNABLE_NAME_MAX " (max_name_len + 1)
@@ -189,5 +233,31 @@  END {
     }
   }
   printf "};\n"
+
+  # The names of the values of the tunables that have them.
+  print "\nstatic inline const char *"
+  print "tunable_value_names (tunable_id_t id)"
+  print "{"
+  print "  switch (id)"
+  print "    {"
+  for (i = 1; i <= typecount; i++) {
+    tnm = typesa[i];
+    split (tnm, indices, SUBSEP);
+    t = indices[1];
+    n = indices[2];
+    m = indices[3];
+    if (values[t,n,m] != "") {
+      nvalues = split (values[t,n,m], vnames, " ");
+      str = ""
+      for (j = 1; j <= nvalues; j++)
+	str = str vnames[j] "\\0"
+      printf ("    case TUNABLE_ENUM_NAME(%s, %s, %s):\n", t, n, m);
+      printf ("      return \"%s\";\n", str);
+    }
+  }
+  print "    default:"
+  print "      return NULL;"
+  print "    }"
+  print "}"
   print "#endif"
 }