@@ -737,6 +737,30 @@ tests-special += \
$(objpfx)tst-rtld-relro-norelro-deny.out \
# tests-special
endif
+# The helpers must be PIE, since the test sets glibc.rtld.require_pie.
+ifeq ($(have-fpie),yes)
+tests-container += \
+ tst-rtld-hardening-conf \
+ # tests-container
+tst-rtld-hardening-conf-helpers = \
+ tst-rtld-hardening-conf-helper \
+ tst-rtld-hardening-conf-helper-norelro \
+ tst-rtld-hardening-conf-helper-wx \
+ # tst-rtld-hardening-conf-helpers
+ifeq (yes,$(have-test-textrel))
+tst-rtld-hardening-conf-helpers += \
+ tst-rtld-hardening-conf-helper-textrel \
+ # tst-rtld-hardening-conf-helpers
+endif
+ifeq (yes,$(have-z-execstack))
+tst-rtld-hardening-conf-helpers += \
+ tst-rtld-hardening-conf-helper-execstack \
+ # tst-rtld-hardening-conf-helpers
+endif
+test-srcs += $(tst-rtld-hardening-conf-helpers)
+tests-pie += tst-rtld-relative-rpath-needed-runpath
+tests-pie += $(tst-rtld-hardening-conf-helpers)
+endif
ifeq ($(have-dt-relr),yes)
tests += \
tst-relr \
@@ -2405,6 +2429,28 @@ tst-rtld-bind-now-profile-TUNABLES = glibc.rtld.bind_now=1
tst-rtld-bind-now-profile-ENV = \
LD_PROFILE=tst-rtld-bind-now-mod.so LD_PROFILE_OUTPUT=$(objpfx)
+CFLAGS-tst-rtld-hardening-conf.c += \
+ -DTEXTREL_SUPPORTED=$(if $(filter yes,$(have-test-textrel)),1,0) \
+ -DEXECSTACK_SUPPORTED=$(if $(filter yes,$(have-z-execstack)),1,0)
+CFLAGS-tst-rtld-hardening-conf-helper.c += $(pie-ccflag)
+CFLAGS-tst-rtld-hardening-conf-helper-execstack.c += $(pie-ccflag)
+CFLAGS-tst-rtld-hardening-conf-helper-textrel.c += $(pie-ccflag)
+CFLAGS-tst-rtld-hardening-conf-helper-wx.c += $(pie-ccflag)
+LDFLAGS-tst-rtld-hardening-conf-helper-execstack += -Wl,-z,execstack
+ifeq ($(have-no-error-execstack),yes)
+LDFLAGS-tst-rtld-hardening-conf-helper-execstack += -Wl,--no-error-execstack
+endif
+LDFLAGS-tst-rtld-hardening-conf-helper-textrel += -Wl,-z,notext
+ifeq ($(have-no-error-rwx-segments),yes)
+LDFLAGS-tst-rtld-hardening-conf-helper-wx += -Wl,--no-error-rwx-segments
+endif
+$(objpfx)tst-rtld-hardening-conf.out: \
+ $(tst-rtld-hardening-conf-helpers:%=$(objpfx)%) \
+ $(objpfx)tst-rtld-require-pie-nopie \
+ $(objpfx)tst-rtld-bind-now-mod.so \
+ $(objpfx)tst-rtld-wx-segments-mod.so \
+ $(if $(filter yes,$(have-test-textrel)),$(objpfx)tst-rtld-textrel-mod.so)
+
LDFLAGS-tst-rtld-relative-rpath-mod.so += \
-Wl,--enable-new-dtags,-rpath,tst-rtld-relative-rpath-dir::/nonexistent
# The helpers of tst-rtld-relative-rpath-needed depend on the soname, which
@@ -2435,6 +2481,9 @@ $(objpfx)tst-rtld-relative-rpath.out: \
$(objpfx)tst-rtld-relative-rpath-default.out: \
$(objpfx)tst-rtld-relative-rpath-mod.so $(objpfx)tst-rtld-relative-rpath-lib.so
tst-rtld-relative-rpath-TUNABLES = glibc.rtld.ignore_relative_rpath=1
+CFLAGS-tst-rtld-relative-rpath-needed-runpath.c += $(pie-ccflag)
+$(objpfx)tst-rtld-hardening-conf.out: \
+ $(objpfx)tst-rtld-relative-rpath-needed-runpath
LDFLAGS-tst-rtld-relro-mod-lazy.so += -Wl,-z,relro,-z,lazy
LDFLAGS-tst-rtld-relro-mod-norelro.so += -Wl,-z,norelro,-z,lazy
@@ -2457,6 +2506,9 @@ LDFLAGS-tst-rtld-relro-full += -Wl,-z,now
LDFLAGS-tst-rtld-relro-lazy += -Wl,-z,lazy
# $(relro-LDFLAGS) comes after LDFLAGS-*, but before LDLIBS-*.
LDLIBS-tst-rtld-relro-norelro = -Wl,-z,norelro,-z,lazy
+CFLAGS-tst-rtld-hardening-conf-helper-norelro.c += $(pie-ccflag)
+LDLIBS-tst-rtld-hardening-conf-helper-norelro = -Wl,-z,norelro
+$(objpfx)tst-rtld-hardening-conf.out: $(objpfx)tst-rtld-relro-mod-norelro.so
$(objpfx)tst-rtld-relro-lazy-deny.out: $(objpfx)tst-rtld-relro-lazy
$(test-program-cmd-before-env) \
new file mode 100644
@@ -0,0 +1,20 @@
+/* Helper program with an executable stack for tst-rtld-hardening-conf.
+ 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/>. */
+
+/* The program is linked with -z execstack. */
+#include "tst-rtld-hardening-conf-helper.c"
new file mode 100644
@@ -0,0 +1,20 @@
+/* Helper program without RELRO for tst-rtld-hardening-conf.
+ 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/>. */
+
+/* The program is linked with -z norelro. */
+#include "tst-rtld-hardening-conf-helper.c"
new file mode 100644
@@ -0,0 +1,20 @@
+/* Helper program with a text relocation for tst-rtld-hardening-conf.
+ 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/>. */
+
+#define TEXTREL
+#include "tst-rtld-hardening-conf-helper.c"
new file mode 100644
@@ -0,0 +1,21 @@
+/* Helper program with a writable and executable segment for
+ tst-rtld-hardening-conf.
+ 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/>. */
+
+#define WX_SEGMENT
+#include "tst-rtld-hardening-conf-helper.c"
new file mode 100644
@@ -0,0 +1,59 @@
+/* Helper program for tst-rtld-hardening-conf.
+ 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 <dlfcn.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#ifdef TEXTREL
+/* Force a text relocation in the program. */
+static const uintptr_t
+address __attribute__ ((used, section (".text"))) = (uintptr_t) &address;
+#endif
+
+#ifdef WX_SEGMENT
+/* The linker places a section that is both writable and executable in a
+ writable and executable PT_LOAD segment. */
+__asm__ (".section .tst_rtld_wx,\"awx\",%progbits\n"
+ ".byte 0\n"
+ ".previous");
+#endif
+
+/* Load the shared object given as the argument, if any, with lazy
+ binding. */
+int
+main (int argc, char *argv[])
+{
+#ifdef TEXTREL
+ /* Read the value from memory, so that the compiler does not fold it. */
+ if (*(const volatile uintptr_t *) &address != (uintptr_t) &address)
+ {
+ puts ("error: text relocation not applied");
+ return 2;
+ }
+#endif
+
+ if (argc > 1 && dlopen (argv[1], RTLD_LAZY) == NULL)
+ {
+ printf ("error: %s\n", dlerror ());
+ return 1;
+ }
+
+ puts ("ok");
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,229 @@
+/* Test the dynamic loader hardening tunables set in /etc/tunables.conf.
+ 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 <link.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/auxv.h>
+#include <unistd.h>
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+#include <support/support.h>
+#include <support/temp_file.h>
+#include <support/xstdio.h>
+#include <support/xunistd.h>
+
+#include "tst-rtld-relative-rpath-needed.h"
+#include "tst-rtld-wx-segments.h"
+
+/* The programs and shared objects used by the test are in the build
+ directory, which test-container makes available at the same path. */
+
+/* Count in *CLOSURE the loaded objects, other than the vDSO, that may
+ require an executable stack. */
+static int
+count_execstack_objects (struct dl_phdr_info *info, size_t size,
+ void *closure)
+{
+ ElfW(Addr) vdso = getauxval (AT_SYSINFO_EHDR);
+ bool is_vdso = false;
+ bool execstack = true;
+
+ for (ElfW(Half) i = 0; i < info->dlpi_phnum; i++)
+ {
+ const ElfW(Phdr) *ph = &info->dlpi_phdr[i];
+ if (ph->p_type == PT_LOAD)
+ {
+ ElfW(Addr) start = info->dlpi_addr + ph->p_vaddr;
+ if (vdso != 0 && vdso >= start && vdso - start < ph->p_memsz)
+ is_vdso = true;
+ }
+ else if (ph->p_type == PT_GNU_STACK)
+ execstack = (ph->p_flags & PF_X) != 0;
+ }
+
+ /* Without PT_GNU_STACK, the stack permissions depend on the ABI. */
+ if (execstack && !is_vdso)
+ {
+ printf ("info: %s may require an executable stack\n",
+ info->dlpi_name[0] != '\0' ? info->dlpi_name : "<main program>");
+ ++*(int *) closure;
+ }
+
+ return 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");
+}
+
+static void
+check_program (const char *program, const char *module, int status,
+ const char *message)
+{
+ char *path = xasprintf ("%s/elf/%s", support_objdir_root, program);
+ char *modpath = NULL;
+ if (module != NULL)
+ modpath = xasprintf ("%s/elf/%s", support_objdir_root, module);
+ char *argv[] = { path, modpath, NULL };
+
+ printf ("info: running %s %s\n", program, module != NULL ? module : "");
+ struct support_capture_subprocess result
+ = support_capture_subprogram (path, argv, NULL);
+ if (message == NULL)
+ support_capture_subprocess_check (&result, program, 0, sc_allow_stdout);
+ else
+ {
+ support_capture_subprocess_check (&result, program, status,
+ sc_allow_stdout | sc_allow_stderr);
+ if (strstr (result.out.buffer, message) == NULL
+ && strstr (result.err.buffer, message) == NULL)
+ FAIL ("%s: message \"%s\" not found\nstdout: %s\nstderr: %s",
+ program, message, result.out.buffer, result.err.buffer);
+ }
+
+ support_capture_subprocess_free (&result);
+ free (modpath);
+ free (path);
+}
+
+/* Whether glibc.rtld.refuse_rwx_segments=1 and glibc.rtld.execstack=0 are
+ set and checked. */
+static bool check_wx;
+static bool check_execstack;
+
+/* Run from a directory where the relative DT_RUNPATH of
+ tst-rtld-relative-rpath-needed-runpath finds its dependency, and check
+ that the program starts by default. */
+static void
+prepare_relative_rpath (void)
+{
+ create_relative_rpath_dir ("tst-rtld-hardening-conf-");
+ check_program ("tst-rtld-relative-rpath-needed-runpath", NULL, 0, NULL);
+}
+
+/* Write the settings to /etc/tunables.conf and run ldconfig to add them
+ to the cache. */
+static void
+do_prepare (int argc, char **argv)
+{
+ /* Some ABIs, such as HPPA and SPARC, place the PLT in a writable and
+ executable segment, so glibc.rtld.refuse_rwx_segments=1 would reject
+ every program. Likewise, glibc.rtld.execstack=0 would reject every
+ program if the objects require an executable stack. */
+ check_wx = find_wx_objects ().count == 0;
+ int execstack = 0;
+ dl_iterate_phdr (count_execstack_objects, &execstack);
+ check_execstack = EXECSTACK_SUPPORTED && execstack == 0;
+
+ prepare_relative_rpath ();
+
+ /* Non-overridable settings that also apply to AT_SECURE processes. */
+ char *conf = xasprintf ("%s/tunables.conf", support_sysconfdir_prefix);
+ FILE *fp = xfopen (conf, "w");
+ TEST_VERIFY_EXIT (fputs ("-*glibc.rtld.require_pie=1\n"
+ "-*glibc.rtld.refuse_textrel=1\n"
+ "-*glibc.rtld.bind_now=1\n"
+ "-*glibc.rtld.ignore_relative_rpath=1\n"
+ "-*glibc.rtld.require_relro=partial\n", fp) >= 0);
+ if (check_wx)
+ TEST_VERIFY_EXIT (fputs ("-*glibc.rtld.refuse_rwx_segments=1\n", fp) >= 0);
+ if (check_execstack)
+ TEST_VERIFY_EXIT (fputs ("-*glibc.rtld.execstack=0\n", fp) >= 0);
+ xfclose (fp);
+ free (conf);
+
+ xmkdirp ("/var/cache/ldconfig", 0777);
+ struct support_capture_subprocess result
+ = support_capture_subprocess (run_ldconfig, NULL);
+ support_capture_subprocess_check (&result, "ldconfig", 0, sc_allow_none);
+ support_capture_subprocess_free (&result);
+}
+
+static int
+do_test (void)
+{
+ /* The environment can not override the settings, and setting
+ glibc.rtld.enable_secure runs the programs as AT_SECURE. */
+ static const char *const tunables[] =
+ {
+ "glibc.rtld.require_pie=0:glibc.rtld.refuse_textrel=0"
+ ":glibc.rtld.refuse_rwx_segments=0:glibc.rtld.bind_now=0"
+ ":glibc.rtld.ignore_relative_rpath=0:glibc.rtld.execstack=1"
+ ":glibc.rtld.require_relro=none",
+ "glibc.rtld.enable_secure=1",
+ };
+ for (size_t i = 0; i < array_length (tunables); i++)
+ {
+ printf ("info: GLIBC_TUNABLES=%s\n", tunables[i]);
+ setenv ("GLIBC_TUNABLES", tunables[i], 1);
+
+ check_program ("tst-rtld-hardening-conf-helper", NULL, 0, NULL);
+
+ check_program ("tst-rtld-require-pie-nopie", NULL, 127,
+ "Fatal glibc error: non-PIE executable is not allowed");
+
+ check_program ("tst-rtld-hardening-conf-helper",
+ "tst-rtld-bind-now-mod.so", 1,
+ "undefined symbol: tst_rtld_bind_now_undefined");
+
+#if TEXTREL_SUPPORTED
+ check_program ("tst-rtld-hardening-conf-helper-textrel", NULL, 127,
+ "Fatal glibc error: text relocations are not allowed");
+ check_program ("tst-rtld-hardening-conf-helper",
+ "tst-rtld-textrel-mod.so", 1,
+ "text relocations are not allowed");
+#endif
+
+ if (check_wx)
+ {
+ check_program ("tst-rtld-hardening-conf-helper-wx", NULL, 127,
+ "Fatal glibc error: writable and executable segments"
+ " are not allowed");
+ check_program ("tst-rtld-hardening-conf-helper", MODULE, 1,
+ "writable and executable segments are not allowed");
+ }
+
+ if (check_execstack)
+ check_program ("tst-rtld-hardening-conf-helper-execstack", NULL, 127,
+ "Fatal glibc error: executable stack is not allowed");
+
+ check_program ("tst-rtld-hardening-conf-helper-norelro", NULL, 127,
+ "Fatal glibc error: RELRO is required");
+ check_program ("tst-rtld-hardening-conf-helper",
+ "tst-rtld-relro-mod-norelro.so", 1,
+ ": RELRO is required");
+
+ check_program ("tst-rtld-relative-rpath-needed-runpath", NULL, 127,
+ LIB ": cannot open shared object file");
+ }
+
+ return 0;
+}
+
+#define PREPARE do_prepare
+#include <support/test-driver.c>
new file mode 100644