[v2,10/13] elf: Warn about invalid values in /etc/tunables.conf

Message ID 20261030173000.3245502-11-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_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build 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 value that the dynamic loader does not accept, such as a number out of
the range of the tunable or a name that a tunable with named values
does not have, was stored in the cache and silently ignored by every
program.

For the hardening tunables this fails add some possible missing
hardening, where -*glibc.rtld.hardened=2 or -*glibc.rtld.require_relro=1,
the tunable stays at its default without any message.

The ldconfig now warns about such values and tt also parses numbers as the
dynamic loader does.  The sscanf accepted a prefix of the value, so 1abc
was stored as 1 and 0x10 as 0, while GLIBC_TUNABLES rejects 1abc and parses
0x10 as 16.

Checked on x86_64-linux-gnu.
---
 elf/Makefile                  |   1 +
 elf/tst-rtld-hardening-conf.c |  21 +++--
 elf/tst-tunconf-values.c      | 144 ++++++++++++++++++++++++++++++++++
 elf/tunconf.c                 |  62 ++++++++++++++-
 manual/tunables.texi          |   7 ++
 5 files changed, 225 insertions(+), 10 deletions(-)
 create mode 100644 elf/tst-tunconf-values.c
  

Patch

diff --git a/elf/Makefile b/elf/Makefile
index f34a0c15ab0..7b32c0b3996 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -605,6 +605,7 @@  tests-container += \
   tst-preload-pthread-libc \
   tst-ptrguard-static-dlopen \
   tst-rootdir \
+  tst-tunconf-values \
   tst-tunconf1 \
   # tests-container
 
diff --git a/elf/tst-rtld-hardening-conf.c b/elf/tst-rtld-hardening-conf.c
index 2a795b5aaec..036e685b047 100644
--- a/elf/tst-rtld-hardening-conf.c
+++ b/elf/tst-rtld-hardening-conf.c
@@ -128,9 +128,9 @@  prepare_relative_rpath (void)
 }
 
 /* Write CONTENTS to /etc/tunables.conf and run ldconfig to add it to the
-   cache.  */
+   cache.  ldconfig must print WARNING, if not NULL, and nothing else.  */
 static void
-write_tunables_conf (const char *contents)
+write_tunables_conf (const char *contents, const char *warning)
 {
   char *conf = xasprintf ("%s/tunables.conf", support_sysconfdir_prefix);
   FILE *fp = xfopen (conf, "w");
@@ -140,7 +140,11 @@  write_tunables_conf (const char *contents)
 
   struct support_capture_subprocess result
     = support_capture_subprocess (run_ldconfig, NULL);
-  support_capture_subprocess_check (&result, "ldconfig", 0, sc_allow_none);
+  support_capture_subprocess_check (&result, "ldconfig", 0,
+				    (warning != NULL
+				     ? sc_allow_stdout : sc_allow_none));
+  if (warning != NULL)
+    TEST_COMPARE_STRING (result.out.buffer, warning);
   support_capture_subprocess_free (&result);
 }
 
@@ -171,7 +175,7 @@  do_prepare (int argc, char **argv)
 			  (check_wx
 			   ? "-*glibc.rtld.refuse_rwx_segments=1\n" : ""),
 			  check_execstack ? "-*glibc.rtld.execstack=0\n" : "");
-  write_tunables_conf (conf);
+  write_tunables_conf (conf, NULL);
   free (conf);
 }
 
@@ -250,12 +254,17 @@  do_test (void)
   /* The same settings through glibc.rtld.hardened, with an exception for
      the program with text relocations, which the environment can not
      make.  The environment can not relax an overridable setting either,
-     and an invalid setting does not disable the hardened value.  */
+     and an invalid setting, which ldconfig warns about, does not disable
+     the hardened value.  */
+  char *warning = xasprintf ("%s/tunables.conf:3: Warning: invalid value yes"
+			     " for tunable glibc.rtld.require_relro.\n",
+			     support_sysconfdir_prefix);
   write_tunables_conf ("-*glibc.rtld.hardened=1\n"
 		       "*glibc.rtld.bind_now=1\n"
 		       "-*glibc.rtld.require_relro=yes\n"
 		       "[proc:tst-rtld-hardening-conf-helper-textrel]\n"
-		       "*glibc.rtld.refuse_textrel=0\n");
+		       "*glibc.rtld.refuse_textrel=0\n", warning);
+  free (warning);
   check_programs (check_wx && !DL_HARDENED_SKIP_RWX_SEGMENTS,
 		  check_execstack && !DL_HARDENED_SKIP_EXECSTACK, true);
 
diff --git a/elf/tst-tunconf-values.c b/elf/tst-tunconf-values.c
new file mode 100644
index 00000000000..d213b6ad642
--- /dev/null
+++ b/elf/tst-tunconf-values.c
@@ -0,0 +1,144 @@ 
+/* Test the validation of the values in /etc/tunables.conf by ldconfig.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <array_length.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+#include <support/support.h>
+#include <support/xmemstream.h>
+#include <support/xstdio.h>
+#include <support/xunistd.h>
+
+struct entry
+{
+  /* The line in /etc/tunables.conf is NAME=VALUE.  */
+  const char *name;
+  const char *value;
+  /* Whether ldconfig accepts the value.  */
+  bool valid;
+  /* The value of the tunable that ld.so --list-tunables then prints.  */
+  const char *listed;
+};
+
+static const struct entry entries1[] =
+{
+  /* Numbers are parsed as in GLIBC_TUNABLES.  */
+  { "glibc.malloc.tcache_count", "0x10", true, "0x10" },
+  { "glibc.malloc.mxfast", "010", true, "0x8" },
+  /* The whole value must be a number.  */
+  { "glibc.malloc.mmap_max", "1abc", false, "0" },
+  /* Values out of range are ignored.  */
+  { "glibc.malloc.perturb", "256", false, "0" },
+  { "glibc.rtld.hardened", "2", false, "0" },
+  /* A tunable with named values only accepts the names.  */
+  { "glibc.rtld.require_relro", "1", false, "none" },
+};
+
+static const struct entry entries2[] =
+{
+  { "glibc.rtld.require_relro", "partial", true, "partial" },
+  { "glibc.malloc.perturb", "-1", false, "0" },
+};
+
+static void
+run_ldconfig (void *closure)
+{
+  char *prog = xasprintf ("%s/ldconfig", support_install_rootsbindir);
+  char *args[] = { prog, NULL };
+
+  execv (args[0], args);
+  FAIL_EXIT1 ("execv: %m");
+}
+
+/* Return true if OUTPUT has a line that starts with PREFIX.  */
+static bool
+has_line (const char *output, const char *prefix)
+{
+  size_t len = strlen (prefix);
+  for (const char *p = output; p != NULL; p = strchr (p, '\n'))
+    {
+      if (*p == '\n')
+	p++;
+      if (strncmp (p, prefix, len) == 0)
+	return true;
+    }
+  return false;
+}
+
+/* Write the N ENTRIES to /etc/tunables.conf, run ldconfig, and check its
+   warnings and the resulting values of the tunables.  */
+static void
+check_entries (const struct entry *entries, size_t n)
+{
+  char *conf = xasprintf ("%s/tunables.conf", support_sysconfdir_prefix);
+  FILE *fp = xfopen (conf, "w");
+  struct xmemstream warnings;
+  xopen_memstream (&warnings);
+  for (size_t i = 0; i < n; i++)
+    {
+      fprintf (fp, "%s=%s\n", entries[i].name, entries[i].value);
+      if (!entries[i].valid)
+	fprintf (warnings.out,
+		 "%s:%zu: Warning: invalid value %s for tunable %s.\n",
+		 conf, i + 1, entries[i].value, entries[i].name);
+    }
+  xfclose (fp);
+  xfclose_memstream (&warnings);
+
+  struct support_capture_subprocess result
+    = support_capture_subprocess (run_ldconfig, NULL);
+  support_capture_subprocess_check (&result, "ldconfig", 0, sc_allow_stdout);
+  TEST_COMPARE_STRING (result.out.buffer, warnings.buffer);
+  support_capture_subprocess_free (&result);
+
+  char *args[] = { (char *) support_objdir_elf_ldso,
+		   (char *) "--list-tunables", NULL };
+  result = support_capture_subprogram (args[0], args, NULL);
+  support_capture_subprocess_check (&result, "ld.so --list-tunables", 0,
+				    sc_allow_stdout);
+  for (size_t i = 0; i < n; i++)
+    {
+      char *line = xasprintf ("%s: %s (", entries[i].name,
+			      entries[i].listed);
+      if (!has_line (result.out.buffer, line))
+	FAIL ("no line \"%s\" in ld.so --list-tunables output", line);
+      free (line);
+    }
+  support_capture_subprocess_free (&result);
+
+  free (warnings.buffer);
+  free (conf);
+}
+
+static int
+do_test (void)
+{
+  xmkdirp ("/var/cache/ldconfig", 0777);
+
+  check_entries (entries1, array_length (entries1));
+  check_entries (entries2, array_length (entries2));
+
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/elf/tunconf.c b/elf/tunconf.c
index d4b2e541b53..347dd3703f5 100644
--- a/elf/tunconf.c
+++ b/elf/tunconf.c
@@ -18,6 +18,7 @@ 
 #include <alloca.h>
 #include <argp.h>
 #include <assert.h>
+#include <errno.h>
 #include <error.h>
 #include <inttypes.h>
 #include <glob.h>
@@ -154,6 +155,51 @@  parse_filter (char *line, const char *filename, int lineno)
 		   (int)(colon - line - 1), line + 1);
 }
 
+/* Return true if the dynamic loader accepts the value VALUE of ENTRY for
+   the tunable ID, as tunable_initialize and do_tunable_update_val do.  */
+static bool
+tunable_value_valid (int id, const struct tunable_entry_int *entry,
+		     const char *value)
+{
+  const tunable_t *cur = &tunable_list[id];
+  if (cur->type.type_code == TUNABLE_TYPE_STRING)
+    return true;
+
+  /* A tunable with named values only accepts the names.  */
+  const char *names = tunable_value_names (id);
+  if (names != NULL)
+    {
+      for (; *names != '\0'; names += strlen (names) + 1)
+	if (strcmp (names, value) == 0)
+	  return true;
+      return false;
+    }
+
+  if (!entry->value_was_parsed)
+    return false;
+
+  tunable_num_t val = (entry->value_is_negative
+		       ? (tunable_num_t) entry->value_sll
+		       : (tunable_num_t) entry->value_ull);
+  switch (cur->type.type_code)
+    {
+    case TUNABLE_TYPE_INT_32:
+      val = (int32_t) val;
+      break;
+    case TUNABLE_TYPE_UINT_64:
+      val = (int64_t) val;
+      break;
+    case TUNABLE_TYPE_SIZE_T:
+      val = (size_t) val;
+      break;
+    default:
+      break;
+    }
+
+  bool unsigned_cmp = unsigned_tunable_type (cur->type.type_code);
+  return !tunable_val_lt (val, cur->type.min, unsigned_cmp)
+	 && !tunable_val_gt (val, cur->type.max, unsigned_cmp);
+}
 
 static void
 add_tunable (char *line, const char *filename, int lineno)
@@ -333,18 +379,26 @@  add_tunable (char *line, const char *filename, int lineno)
       entry->filter = cache_store_string (filter_string);
     }
 
+  /* Parse the value as the dynamic loader does, where the whole value must
+     be a decimal, octal (with a 0 prefix), or hexadecimal (with a 0x prefix)
+     number.  */
+  char *end;
+  errno = 0;
   if (value[0] == '-')
     {
       entry->value_is_negative = 1;
-      if (sscanf (value, "%lld", &entry->value_sll) == 1)
-	entry->value_was_parsed = 1;
+      entry->value_sll = strtoll (value, &end, 0);
     }
   else
     {
       entry->value_is_negative = 0;
-      if (sscanf (value, "%llu", &entry->value_ull) == 1)
-	entry->value_was_parsed = 1;
+      entry->value_ull = strtoull (value, &end, 0);
     }
+  entry->value_was_parsed = errno == 0 && end != value && *end == '\0';
+
+  if (id != -1 && !tunable_value_valid (id, entry, value))
+    printf ("%s:%d: Warning: invalid value %s for tunable %s.\n",
+	    filename, lineno, value, name);
 
   *entry_list_next = entry;
   entry_list_next = & (entry->next);
diff --git a/manual/tunables.texi b/manual/tunables.texi
index 4ceb5aac072..ceaf91ee910 100644
--- a/manual/tunables.texi
+++ b/manual/tunables.texi
@@ -155,6 +155,13 @@  specified: it applies only to processes that are not AT_SECURE.  Use
 @code{anysecure} or @code{onlysecure} to affect AT_SECURE (setuid,
 setgid, or capability-elevated) processes.
 
+@file{ldconfig} prints a warning for a tunable that it does not
+recognize, and for a value that the tunable does not accept, such as a
+number outside its range or a name that is not one of its values.  The
+dynamic loader ignores such a value, but a nonoverridable entry with it
+still prevents the @code{GLIBC_TUNABLES} environment variable from
+changing the tunable.
+
 The @code{GLIBC_TUNABLES} environment variable is ignored entirely for
 AT_SECURE processes, so the system-wide file is the only way to set a
 tunable for such a process.  A tunable applied this way is not subject to