@@ -605,6 +605,7 @@ tests-container += \
tst-preload-pthread-libc \
tst-ptrguard-static-dlopen \
tst-rootdir \
+ tst-tunconf-values \
tst-tunconf1 \
# tests-container
@@ -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);
new file mode 100644
@@ -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>
@@ -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);
@@ -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