@@ -16,6 +16,10 @@ Major new features:
start position-dependent (non-PIE) executables. It can be enforced
system-wide with a nonoverridable entry in /etc/tunables.conf.
+* A new tunable, glibc.rtld.refuse_textrel, can be set to 1 to make the
+ dynamic loader refuse to load programs and shared objects with text
+ relocations.
+
Deprecated and removed features, and other changes affecting compatibility:
* The posix_spawn_file_actions_addchdir and posix_spawn_file_actions_addfchdir
@@ -73,6 +73,7 @@ bind-now = @bindnow@
have-cxx-thread_local = @libc_cv_cxx_thread_local@
have-loop-to-function = @libc_cv_cc_loop_to_function@
have-textrel_ifunc = @libc_cv_textrel_ifunc@
+have-test-textrel = @libc_cv_test_textrel@
multi-arch = @multi_arch@
@@ -673,6 +673,7 @@ SYSINCLUDES
sysnames
submachine
multi_arch
+libc_cv_test_textrel
libc_cv_textrel_ifunc
no_stack_protector
stack_protector
@@ -6355,6 +6356,37 @@ fi
printf "%s\n" "$libc_cv_textrel_ifunc" >&6; }
+# Check if the test compiler and linker can create a shared object with a
+# text relocation.
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether the test linker supports text relocations" >&5
+printf %s "checking whether the test linker supports text relocations... " >&6; }
+if test ${libc_cv_test_textrel+y}
+then :
+ printf %s "(cached) " >&6
+else case e in #(
+ e) cat > conftest.c <<EOF
+static const __UINTPTR_TYPE__ address __attribute__ ((used, section (".text")))
+ = (__UINTPTR_TYPE__) &address;
+EOF
+libc_cv_test_textrel=no
+if { ac_try='${TEST_CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp
+ -nostdlib -nostartfiles -fPIC -shared -Wl,-z,notext
+ -o conftest.so conftest.c'
+ { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+ (eval $ac_try) 2>&5
+ ac_status=$?
+ printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+ test $ac_status = 0; }; } \
+ && LC_ALL=C $READELF -dW conftest.so | grep -q TEXTREL; then
+ libc_cv_test_textrel=yes
+fi
+rm -f conftest* ;;
+esac
+fi
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $libc_cv_test_textrel" >&5
+printf "%s\n" "$libc_cv_test_textrel" >&6; }
+
+
# Check if CC supports attribute retain as it is used in attribute_used_retain macro.
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for GNU attribute retain support" >&5
printf %s "checking for GNU attribute retain support... " >&6; }
@@ -811,6 +811,24 @@ fi
rm -f conftest*])
AC_SUBST(libc_cv_textrel_ifunc)
+# Check if the test compiler and linker can create a shared object with a
+# text relocation.
+AC_CACHE_CHECK([whether the test linker supports text relocations],
+ libc_cv_test_textrel, [dnl
+cat > conftest.c <<EOF
+static const __UINTPTR_TYPE__ address __attribute__ ((used, section (".text")))
+ = (__UINTPTR_TYPE__) &address;
+EOF
+libc_cv_test_textrel=no
+if AC_TRY_COMMAND([${TEST_CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp
+ -nostdlib -nostartfiles -fPIC -shared -Wl,-z,notext
+ -o conftest.so conftest.c]) \
+ && LC_ALL=C $READELF -dW conftest.so | grep -q TEXTREL; then
+ libc_cv_test_textrel=yes
+fi
+rm -f conftest*])
+AC_SUBST(libc_cv_test_textrel)
+
# Check if CC supports attribute retain as it is used in attribute_used_retain macro.
AC_CACHE_CHECK([for GNU attribute retain support],
libc_cv_gnu_retain, [dnl
@@ -672,6 +672,23 @@ tests-special += \
$(objpfx)tst-rtld-require-pie-nopie-deny.out \
# tests-special
endif
+ifeq (yes,$(have-test-textrel))
+tests += \
+ tst-rtld-textrel \
+ tst-rtld-textrel-default \
+ tst-rtld-textrel-needed \
+ tst-rtld-textrel-preload \
+ tst-rtld-textrel-preload-default \
+ # tests
+ifeq ($(run-built-tests),yes)
+tests-special += \
+ $(objpfx)tst-rtld-textrel-needed-deny.out \
+ # tests-special
+endif
+modules-names += \
+ tst-rtld-textrel-mod \
+ # modules-names
+endif
ifeq ($(have-dt-relr),yes)
tests += \
tst-relr \
@@ -2291,6 +2308,28 @@ $(objpfx)tst-rtld-require-pie-nopie-deny.out: \
&& grep -q '^status: 127$$' $@; \
$(evaluate-test)
+LDFLAGS-tst-rtld-textrel-mod.so += -Wl,-z,notext
+$(objpfx)tst-rtld-textrel-needed: $(objpfx)tst-rtld-textrel-mod.so
+$(objpfx)tst-rtld-textrel-needed-deny.out: $(objpfx)tst-rtld-textrel-needed
+ $(test-program-cmd-before-env) \
+ $(run-program-env) \
+ GLIBC_TUNABLES=glibc.rtld.refuse_textrel=1 \
+ $(test-program-cmd-after-env) $< \
+ > $@ 2>&1; echo "status: $$?" >> $@; \
+ grep -q ': text relocations are not allowed$$' $@ \
+ && grep -q '^status: 127$$' $@; \
+ $(evaluate-test)
+$(objpfx)tst-rtld-textrel-preload.out: $(objpfx)tst-rtld-textrel-mod.so
+$(objpfx)tst-rtld-textrel-preload-default.out: \
+ $(objpfx)tst-rtld-textrel-mod.so
+tst-rtld-textrel-preload-ENV = LD_PRELOAD=$(objpfx)tst-rtld-textrel-mod.so
+tst-rtld-textrel-preload-TUNABLES = glibc.rtld.refuse_textrel=1
+tst-rtld-textrel-preload-default-ENV = \
+ LD_PRELOAD=$(objpfx)tst-rtld-textrel-mod.so
+$(objpfx)tst-rtld-textrel.out: $(objpfx)tst-rtld-textrel-mod.so
+$(objpfx)tst-rtld-textrel-default.out: $(objpfx)tst-rtld-textrel-mod.so
+tst-rtld-textrel-TUNABLES = glibc.rtld.refuse_textrel=1
+
LDFLAGS-tst-array2 = -Wl,--no-as-needed
LDFLAGS-tst-array5 = -Wl,--no-as-needed
@@ -1305,6 +1305,13 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
goto lose;
}
+ if (__glibc_unlikely (l->l_info[DT_TEXTREL] != NULL)
+ && TUNABLE_GET (glibc, rtld, refuse_textrel, int32_t, NULL) != 0)
+ {
+ errstring = N_("text relocations are not allowed");
+ goto lose;
+ }
+
if (l->l_phdr == NULL)
{
/* The program header is not contained in any of the segments.
@@ -119,6 +119,12 @@ glibc {
maxval: 1
default: 0
}
+ refuse_textrel {
+ type: INT_32
+ minval: 0
+ maxval: 1
+ default: 0
+ }
}
mem {
@@ -1263,6 +1263,11 @@ rtld_check_main_map (const struct link_map *main_map)
&& !rtld_main_map_is_pie (main_map))
_dl_fatal_printf ("\
Fatal glibc error: non-PIE executable is not allowed\n");
+
+ if (__glibc_unlikely (main_map->l_info[DT_TEXTREL] != NULL)
+ && TUNABLE_GET (glibc, rtld, refuse_textrel, int32_t, NULL) != 0)
+ _dl_fatal_printf ("\
+Fatal glibc error: text relocations are not allowed\n");
}
/* Set up the program header information for the dynamic linker
@@ -15,4 +15,5 @@ glibc.rtld.enable_secure: 0 (min: 0, max: 1)
glibc.rtld.execstack: 1 (min: 0, max: 2)
glibc.rtld.nns: 0x4 (min: 0x1, max: 0x10)
glibc.rtld.optional_static_tls: 0x200 (min: 0x0, max: 0x[f]+)
+glibc.rtld.refuse_textrel: 0 (min: 0, max: 1)
glibc.rtld.require_pie: 0 (min: 0, max: 1)
new file mode 100644
@@ -0,0 +1,20 @@
+/* Test that text relocations are allowed by default.
+ 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_ALLOWED 1
+#include "tst-rtld-textrel.c"
new file mode 100644
@@ -0,0 +1,36 @@
+/* Shared object with a text relocation, used by tst-rtld-textrel.
+ 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 <stdint.h>
+
+/* Force a text relocation in the object. */
+static const uintptr_t
+address __attribute__ ((used, section (".text"))) = (uintptr_t) &address;
+
+uintptr_t
+tst_rtld_textrel_value (void)
+{
+ /* Read the value from memory, so that the compiler does not fold it. */
+ return *(const volatile uintptr_t *) &address;
+}
+
+const void *
+tst_rtld_textrel_location (void)
+{
+ return &address;
+}
new file mode 100644
@@ -0,0 +1,36 @@
+/* Test a program with a dependency with text relocations.
+ 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 <stdint.h>
+#include <support/check.h>
+
+/* tst-rtld-textrel-mod.so is a DT_NEEDED dependency of this program, so
+ it does not start with glibc.rtld.refuse_textrel=1, which
+ tst-rtld-textrel-needed-deny checks. */
+uintptr_t tst_rtld_textrel_value (void);
+const void *tst_rtld_textrel_location (void);
+
+static int
+do_test (void)
+{
+ TEST_COMPARE (tst_rtld_textrel_value (),
+ (uintptr_t) tst_rtld_textrel_location ());
+ return 0;
+}
+
+#include <support/test-driver.c>
new file mode 100644
@@ -0,0 +1,20 @@
+/* Test that LD_PRELOAD of an object with text relocations works by default.
+ 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_ALLOWED 1
+#include "tst-rtld-textrel-preload.c"
new file mode 100644
@@ -0,0 +1,47 @@
+/* Test LD_PRELOAD of a shared object with text relocations.
+ 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 <support/check.h>
+#include <support/xdlfcn.h>
+
+/* Both tests run with LD_PRELOAD=tst-rtld-textrel-mod.so.
+ tst-rtld-textrel-preload also sets glibc.rtld.refuse_textrel=1, so the
+ preloaded object is ignored, and tst-rtld-textrel-preload-default
+ checks that it is loaded otherwise. */
+#ifndef TEXTREL_ALLOWED
+# define TEXTREL_ALLOWED 0
+#endif
+
+static int
+do_test (void)
+{
+#if TEXTREL_ALLOWED
+ uintptr_t (*value) (void) = xdlsym (RTLD_DEFAULT, "tst_rtld_textrel_value");
+ const void *(*location) (void)
+ = xdlsym (RTLD_DEFAULT, "tst_rtld_textrel_location");
+ TEST_COMPARE (value (), (uintptr_t) location ());
+#else
+ TEST_VERIFY (dlsym (RTLD_DEFAULT, "tst_rtld_textrel_value") == NULL);
+#endif
+
+ return 0;
+}
+
+#include <support/test-driver.c>
new file mode 100644
@@ -0,0 +1,77 @@
+/* Test the glibc.rtld.refuse_textrel tunable.
+ 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 <link.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/xdlfcn.h>
+
+/* tst-rtld-textrel runs with glibc.rtld.refuse_textrel=1, and
+ tst-rtld-textrel-default with the default value. */
+#ifndef TEXTREL_ALLOWED
+# define TEXTREL_ALLOWED 0
+#endif
+
+#define MODULE "tst-rtld-textrel-mod.so"
+
+#if TEXTREL_ALLOWED
+static bool
+has_textrel (const struct link_map *l)
+{
+ for (const ElfW(Dyn) *d = l->l_ld; d->d_tag != DT_NULL; d++)
+ if (d->d_tag == DT_TEXTREL
+ || (d->d_tag == DT_FLAGS && (d->d_un.d_val & DF_TEXTREL) != 0))
+ return true;
+ return false;
+}
+#endif
+
+static int
+do_test (void)
+{
+#if TEXTREL_ALLOWED
+ void *h = xdlopen (MODULE, RTLD_NOW);
+
+ /* Check that the module has a text relocation, so that the failure in
+ tst-rtld-textrel is caused by the tunable, and that it was applied. */
+ struct link_map *l;
+ TEST_COMPARE (dlinfo (h, RTLD_DI_LINKMAP, &l), 0);
+ TEST_VERIFY (has_textrel (l));
+
+ uintptr_t (*value) (void) = xdlsym (h, "tst_rtld_textrel_value");
+ const void *(*location) (void) = xdlsym (h, "tst_rtld_textrel_location");
+ TEST_COMPARE (value (), (uintptr_t) location ());
+
+ xdlclose (h);
+#else
+ void *h = dlopen (MODULE, RTLD_NOW);
+ TEST_VERIFY (h == NULL);
+ const char *err = dlerror ();
+ TEST_VERIFY_EXIT (err != NULL);
+ printf ("info: dlopen error: %s\n", err);
+ TEST_VERIFY (strstr (err, "text relocations are not allowed") != NULL);
+#endif
+
+ return 0;
+}
+
+#include <support/test-driver.c>
@@ -498,6 +498,18 @@ The default value of this tunable is @samp{0}, which allows
position-dependent executables.
@end deftp
+@deftp Tunable glibc.rtld.refuse_textrel
+Objects with text relocations (@code{DT_TEXTREL}) require the dynamic
+loader to make their read-only segments, which usually include code,
+writable while they are relocated. Setting the
+@code{glibc.rtld.refuse_textrel} tunable to @code{1} makes the dynamic
+loader refuse to load such objects: the main program fails to start, and
+@code{dlopen} of such a shared object fails.
+
+The default value of this tunable is @samp{0}, which allows text
+relocations.
+@end deftp
+
@node POSIX Thread Tunables
@section POSIX Thread Tunables
@cindex pthread mutex tunables