@@ -20,6 +20,10 @@ Major new features:
dynamic loader refuse to load programs and shared objects with text
relocations.
+* A new tunable, glibc.rtld.refuse_rwx_segments, can be set to 1 to make
+ the dynamic loader refuse to load programs and shared objects with a
+ loadable segment that is both writable and executable.
+
Deprecated and removed features, and other changes affecting compatibility:
* The posix_spawn_file_actions_addchdir and posix_spawn_file_actions_addfchdir
@@ -54,6 +54,7 @@ enable-werror = @enable_werror@
have-z-execstack = @libc_cv_z_execstack@
have-no-error-execstack = @libc_cv_test_no_error_execstack@
+have-no-error-rwx-segments = @libc_cv_test_no_error_rwx_segments@
have-protected-data = @libc_cv_protected_data@
have-insert = @libc_cv_insert@
have-glob-dat-reloc = @libc_cv_has_glob_dat@
@@ -657,6 +657,7 @@ libc_cv_has_glob_dat
libc_cv_fpie
libc_cv_test_static_pie
libc_cv_z_execstack
+libc_cv_test_no_error_rwx_segments
libc_cv_test_no_error_execstack
ASFLAGS_config
libc_cv_cc_with_libunwind
@@ -7163,6 +7164,43 @@ CC="$saved_CC"
printf "%s\n" "$libc_linker_feature" >&6; }
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for linker that supports --no-error-rwx-segments" >&5
+printf %s "checking for linker that supports --no-error-rwx-segments... " >&6; }
+libc_linker_feature=no
+cat > conftest.c <<EOF
+int _start (void) { return 42; }
+EOF
+saved_CC="$CC"
+CC="$TEST_CC"
+if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp
+ -Wl,--no-error-rwx-segments -nostdlib -nostartfiles
+ -fPIC -shared -o conftest.so conftest.c
+ 1>&5'
+ { { 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; }; }
+then
+ if ${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp -Wl,--no-error-rwx-segments -nostdlib \
+ -nostartfiles -fPIC -shared -o conftest.so conftest.c 2>&1 \
+ | grep "warning: --no-error-rwx-segments ignored" > /dev/null 2>&1; then
+ true
+ else
+ libc_linker_feature=yes
+ fi
+fi
+rm -f conftest*
+if test $libc_linker_feature = yes; then
+ libc_cv_test_no_error_rwx_segments=yes
+else
+ libc_cv_test_no_error_rwx_segments=no
+fi
+CC="$saved_CC"
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $libc_linker_feature" >&5
+printf "%s\n" "$libc_linker_feature" >&6; }
+
+
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for linker that supports -z execstack" >&5
printf %s "checking for linker that supports -z execstack... " >&6; }
libc_linker_feature=no
@@ -1323,6 +1323,12 @@ LIBC_TEST_LINKER_FEATURE([--no-error-execstack], [-Wl,--no-error-execstack],
[libc_cv_test_no_error_execstack=yes], [libc_cv_test_no_error_execstack=no])
AC_SUBST(libc_cv_test_no_error_execstack)
+LIBC_TEST_LINKER_FEATURE([--no-error-rwx-segments],
+ [-Wl,--no-error-rwx-segments],
+ [libc_cv_test_no_error_rwx_segments=yes],
+ [libc_cv_test_no_error_rwx_segments=no])
+AC_SUBST(libc_cv_test_no_error_rwx_segments)
+
LIBC_LINKER_FEATURE([-z execstack], [-Wl,-z,execstack],
[libc_cv_z_execstack=yes], [libc_cv_z_execstack=no])
AC_SUBST(libc_cv_z_execstack)
@@ -689,6 +689,13 @@ modules-names += \
tst-rtld-textrel-mod \
# modules-names
endif
+tests += \
+ tst-rtld-wx-segments \
+ tst-rtld-wx-segments-needed \
+ # tests
+modules-names += \
+ tst-rtld-wx-segments-mod \
+ # modules-names
ifeq ($(have-dt-relr),yes)
tests += \
tst-relr \
@@ -2330,6 +2337,12 @@ $(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
+$(objpfx)tst-rtld-wx-segments.out: $(objpfx)tst-rtld-wx-segments-mod.so
+$(objpfx)tst-rtld-wx-segments-needed: $(objpfx)tst-rtld-wx-segments-mod.so
+ifeq ($(have-no-error-rwx-segments),yes)
+LDFLAGS-tst-rtld-wx-segments-mod.so += -Wl,--no-error-rwx-segments
+endif
+
LDFLAGS-tst-array2 = -Wl,--no-as-needed
LDFLAGS-tst-array5 = -Wl,--no-as-needed
@@ -979,6 +979,14 @@ _dl_map_object_scan_phdrs (struct dl_pt_load_iterator *it,
*errvalp = 0;
return N_("ELF load command address/offset not page-aligned");
}
+ if (__glibc_unlikely ((ph->p_flags & (PF_W | PF_X))
+ == (PF_W | PF_X))
+ && TUNABLE_GET (glibc, rtld, refuse_rwx_segments, int32_t,
+ NULL) != 0)
+ {
+ *errvalp = 0;
+ return N_("writable and executable segments are not allowed");
+ }
ElfW(Addr) mapstart = ALIGN_DOWN (ph->p_vaddr, it->pagesize);
ElfW(Addr) mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz,
it->pagesize);
@@ -125,6 +125,12 @@ glibc {
maxval: 1
default: 0
}
+ refuse_rwx_segments {
+ type: INT_32
+ minval: 0
+ maxval: 1
+ default: 0
+ }
}
mem {
@@ -1268,6 +1268,14 @@ Fatal glibc error: non-PIE executable is not allowed\n");
&& TUNABLE_GET (glibc, rtld, refuse_textrel, int32_t, NULL) != 0)
_dl_fatal_printf ("\
Fatal glibc error: text relocations are not allowed\n");
+
+ if (TUNABLE_GET (glibc, rtld, refuse_rwx_segments, int32_t, NULL) != 0)
+ for (const ElfW(Phdr) *ph = main_map->l_phdr;
+ ph < &main_map->l_phdr[main_map->l_phnum]; ++ph)
+ if (ph->p_type == PT_LOAD
+ && (ph->p_flags & (PF_W | PF_X)) == (PF_W | PF_X))
+ _dl_fatal_printf ("\
+Fatal glibc error: writable and executable segments are not allowed\n");
}
/* Set up the program header information for the dynamic linker
@@ -15,5 +15,6 @@ 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_rwx_segments: 0 (min: 0, max: 1)
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,29 @@
+/* Shared object with a writable and executable segment.
+ 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 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");
+
+int
+tst_rtld_wx_segments_value (void)
+{
+ return 42;
+}
new file mode 100644
@@ -0,0 +1,75 @@
+/* Test a program with a dependency with a writable and executable segment.
+ 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 <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+#include <support/subprocess.h>
+#include <support/support.h>
+
+#include "tst-rtld-wx-segments.h"
+
+/* Defined in MODULE, which is a DT_NEEDED dependency of this program, so
+ the program does not start with glibc.rtld.refuse_rwx_segments=1. */
+int tst_rtld_wx_segments_value (void);
+
+static int restart;
+#define CMDLINE_OPTIONS \
+ { "restart", no_argument, &restart, 1 },
+
+static int
+do_test (void)
+{
+ if (restart)
+ FAIL_EXIT1 ("program started with glibc.rtld.refuse_rwx_segments=1");
+
+ TEST_COMPARE (tst_rtld_wx_segments_value (), 42);
+
+ /* On some ABIs, the program would be rejected regardless of MODULE. */
+ struct wx_objects objs = find_wx_objects ();
+ TEST_VERIFY (objs.module);
+ if (objs.count != 0)
+ FAIL_UNSUPPORTED ("loaded objects have writable and executable segments");
+
+ /* Run the program again with the tunable set, under the built loader. */
+ char *program = xasprintf ("%s/elf/tst-rtld-wx-segments-needed",
+ support_objdir_root);
+ char *args[] =
+ { program, (char *) "--direct", (char *) "--restart", NULL };
+ char *env[] =
+ { (char *) "GLIBC_TUNABLES=glibc.rtld.refuse_rwx_segments=1", NULL };
+ struct support_spawn_wrapped *w = support_spawn_wrap (program, args, env, 0);
+ struct support_capture_subprocess result
+ = support_capture_subprogram (w->path, w->argv, w->envp);
+ support_spawn_wrapped_free (w);
+ support_capture_subprocess_check (&result, "tst-rtld-wx-segments-needed",
+ 127, sc_allow_stderr);
+ printf ("info: stderr: %s", result.err.buffer);
+ TEST_VERIFY (strstr (result.err.buffer,
+ MODULE ": writable and executable segments are not"
+ " allowed") != NULL);
+ support_capture_subprocess_free (&result);
+ free (program);
+
+ return 0;
+}
+
+#include <support/test-driver.c>
new file mode 100644
@@ -0,0 +1,104 @@
+/* Test the glibc.rtld.refuse_rwx_segments 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 <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+#include <support/subprocess.h>
+#include <support/support.h>
+#include <support/xdlfcn.h>
+
+#include "tst-rtld-wx-segments.h"
+
+static int restart;
+#define CMDLINE_OPTIONS \
+ { "restart", no_argument, &restart, 1 },
+
+/* Run with glibc.rtld.refuse_rwx_segments=1 and MODULE in LD_PRELOAD. */
+static int
+handle_restart (void)
+{
+ /* The preloaded module is ignored. */
+ TEST_VERIFY (dlsym (RTLD_DEFAULT, "tst_rtld_wx_segments_value") == NULL);
+
+ 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, "writable and executable segments are not allowed")
+ != NULL);
+ return 0;
+}
+
+static int
+do_test (void)
+{
+ if (restart)
+ return handle_restart ();
+
+ /* The module is loaded by default. */
+ void *h = xdlopen (MODULE, RTLD_NOW);
+ int (*value) (void) = xdlsym (h, "tst_rtld_wx_segments_value");
+ TEST_COMPARE (value (), 42);
+
+ struct wx_objects objs = find_wx_objects ();
+ TEST_VERIFY (objs.module);
+ xdlclose (h);
+
+ /* Some ABIs, such as HPPA and SPARC, place the PLT in a writable and
+ executable segment, so the respawned test would not start. */
+ if (objs.count != 0)
+ FAIL_UNSUPPORTED ("loaded objects have writable and executable segments");
+
+ /* Run the test again with the tunable set, under the built loader. */
+ char *program = xasprintf ("%s/elf/tst-rtld-wx-segments",
+ support_objdir_root);
+ char *args[] =
+ { program, (char *) "--direct", (char *) "--restart", NULL };
+ char *preload = xasprintf ("LD_PRELOAD=%s/elf/%s", support_objdir_root,
+ MODULE);
+ char *env[] =
+ {
+ (char *) "GLIBC_TUNABLES=glibc.rtld.refuse_rwx_segments=1",
+ preload,
+ NULL
+ };
+ struct support_spawn_wrapped *w = support_spawn_wrap (program, args, env, 0);
+ struct support_capture_subprocess result
+ = support_capture_subprogram (w->path, w->argv, w->envp);
+ support_spawn_wrapped_free (w);
+ support_capture_subprocess_check (&result, "tst-rtld-wx-segments", 0,
+ sc_allow_stdout | sc_allow_stderr);
+ fputs (result.out.buffer, stdout);
+ printf ("info: stderr: %s", result.err.buffer);
+ TEST_VERIFY (strstr (result.err.buffer,
+ "cannot be preloaded (writable and executable segments"
+ " are not allowed): ignored") != NULL);
+ support_capture_subprocess_free (&result);
+ free (preload);
+ free (program);
+
+ return 0;
+}
+
+#include <support/test-driver.c>
new file mode 100644
@@ -0,0 +1,76 @@
+/* Helpers to find loaded objects with writable and executable segments.
+ 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 <link.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+/* The shared object with a writable and executable segment. */
+#define MODULE "tst-rtld-wx-segments-mod.so"
+
+static bool
+has_wx_segment (const struct dl_phdr_info *info)
+{
+ for (ElfW(Half) i = 0; i < info->dlpi_phnum; i++)
+ if (info->dlpi_phdr[i].p_type == PT_LOAD
+ && (info->dlpi_phdr[i].p_flags & (PF_W | PF_X)) == (PF_W | PF_X))
+ return true;
+ return false;
+}
+
+struct wx_objects
+{
+ /* Number of loaded objects, other than MODULE, with a writable and
+ executable segment. */
+ int count;
+ /* Whether MODULE is loaded and has a writable and executable segment. */
+ bool module;
+};
+
+static int
+find_wx_objects_callback (struct dl_phdr_info *info, size_t size,
+ void *closure)
+{
+ struct wx_objects *objs = closure;
+
+ if (!has_wx_segment (info))
+ return 0;
+
+ if (strstr (info->dlpi_name, MODULE) != NULL)
+ objs->module = true;
+ else
+ {
+ printf ("info: %s has a writable and executable segment\n",
+ info->dlpi_name[0] != '\0' ? info->dlpi_name : "<main program>");
+ objs->count++;
+ }
+
+ return 0;
+}
+
+/* Find the loaded objects with a writable and executable segment. Some
+ ABIs, such as HPPA and SPARC, place the PLT in such a segment, so every
+ object has one there. */
+static struct wx_objects
+find_wx_objects (void)
+{
+ struct wx_objects objs = { 0, false };
+ dl_iterate_phdr (find_wx_objects_callback, &objs);
+ return objs;
+}
@@ -510,6 +510,21 @@ The default value of this tunable is @samp{0}, which allows text
relocations.
@end deftp
+@deftp Tunable glibc.rtld.refuse_rwx_segments
+Setting the @code{glibc.rtld.refuse_rwx_segments} tunable to @code{1}
+makes the dynamic loader refuse to load programs and shared objects with a
+loadable segment (@code{PT_LOAD}) that is both writable and executable.
+The main program fails to start, and @code{dlopen} of such a shared
+object fails.
+
+Some ABIs, such as HPPA and SPARC, place the procedure linkage table in a
+writable and executable segment, so this setting prevents every
+dynamically linked program from running on them.
+
+The default value of this tunable is @samp{0}, which allows writable and
+executable segments.
+@end deftp
+
@node POSIX Thread Tunables
@section POSIX Thread Tunables
@cindex pthread mutex tunables