[v2,01/13] elf: Add glibc.rtld.require_pie tunable

Message ID 20261030173000.3245502-2-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_build--master-aarch64 success Build passed

Commit Message

Adhemerval Zanella Netto Oct. 30, 2026, 5:27 p.m. UTC
  Setting glibc.rtld.require_pie=1 makes ld.so refuse to start a non-PIE
main program, whether the kernel loaded it or ld.so was run directly.

The kernel does not report the ELF type, so a non-zero load bias means
PIE (ET_EXEC always has a zero bias), and otherwise e_type is read from
the ELF header mapped by the readable PT_LOAD at file offset 0.  A
missing or unreadable header counts as non-PIE, so an execute-only
segment does not crash the loader.  iThis does not assume that the program
headers and the ELF header share a segment.

The check is on the ELF type, not on ASLR being enabled, and it is done
by ld.so, so static programs are not covered.  AT_SECURE programs ignore
GLIBC_TUNABLES, and /etc/tunables.conf entries only apply to them with
the * or @ prefix, as in:

  -*glibc.rtld.require_pie=1

Checked on x86_64-linux-gnu.
---
 NEWS                             |  4 ++++
 elf/Makefile                     | 33 +++++++++++++++++++++++++++
 elf/dl-tunables.list             |  6 +++++
 elf/rtld.c                       | 39 ++++++++++++++++++++++++++++++++
 elf/tst-rtld-list-tunables.exp   |  1 +
 elf/tst-rtld-require-pie-nopie.c | 20 ++++++++++++++++
 elf/tst-rtld-require-pie.c       | 39 ++++++++++++++++++++++++++++++++
 manual/tunables.texi             | 22 ++++++++++++++++++
 8 files changed, 164 insertions(+)
 create mode 100644 elf/tst-rtld-require-pie-nopie.c
 create mode 100644 elf/tst-rtld-require-pie.c
  

Patch

diff --git a/NEWS b/NEWS
index 1d6d0c8e028..681d676e478 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,10 @@  Major new features:
 * Additional optimized and correctly rounded mathematical functions have
   been imported from the CORE-MATH project, in particular cbrt.
 
+* A new tunable, glibc.rtld.require_pie, makes the dynamic loader refuse to
+  start position-dependent (non-PIE) executables.  It can be enforced
+  system-wide with a nonoverridable entry in /etc/tunables.conf.
+
 Deprecated and removed features, and other changes affecting compatibility:
 
 * The posix_spawn_file_actions_addchdir and posix_spawn_file_actions_addfchdir
diff --git a/elf/Makefile b/elf/Makefile
index 2c74e119460..a347961a6f0 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -656,6 +656,22 @@  tests-special += \
   # tests-special
 endif
 endif
+tests += \
+  tst-rtld-require-pie-nopie \
+  # tests
+ifeq ($(have-fpie),yes)
+tests += \
+  tst-rtld-require-pie \
+  # tests
+tests-pie += \
+  tst-rtld-require-pie \
+  # tests-pie
+endif
+ifeq ($(run-built-tests),yes)
+tests-special += \
+  $(objpfx)tst-rtld-require-pie-nopie-deny.out \
+  # tests-special
+endif
 ifeq ($(have-dt-relr),yes)
 tests += \
   tst-relr \
@@ -2258,6 +2274,23 @@  $(objpfx)tst-execstack-prog-static-noexecstack.out: $(objpfx)tst-execstack-prog-
 	  $(evaluate-test)
 endif
 
+CFLAGS-tst-rtld-require-pie.c += $(pie-ccflag)
+tst-rtld-require-pie-TUNABLES = glibc.rtld.require_pie=1
+CFLAGS-tst-rtld-require-pie-nopie.c += $(no-pie-ccflag)
+CRT-tst-rtld-require-pie-nopie := $(csu-objpfx)crt1.o
+tst-rtld-require-pie-nopie-no-pie = yes
+
+$(objpfx)tst-rtld-require-pie-nopie-deny.out: \
+  $(objpfx)tst-rtld-require-pie-nopie
+	$(test-program-cmd-before-env) \
+		$(run-program-env) \
+		GLIBC_TUNABLES=glibc.rtld.require_pie=1 \
+		$(test-program-cmd-after-env) $< \
+		> $@ 2>&1; echo "status: $$?" >> $@; \
+	grep -q '^Fatal glibc error: non-PIE executable is not allowed$$' $@ \
+	  && grep -q '^status: 127$$' $@; \
+	  $(evaluate-test)
+
 LDFLAGS-tst-array2 = -Wl,--no-as-needed
 LDFLAGS-tst-array5 = -Wl,--no-as-needed
 
diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
index 111649f1455..10a2eb77206 100644
--- a/elf/dl-tunables.list
+++ b/elf/dl-tunables.list
@@ -113,6 +113,12 @@  glibc {
       maxval: 2
       default: 1
     }
+    require_pie {
+      type: INT_32
+      minval: 0
+      maxval: 1
+      default: 0
+    }
   }
 
   mem {
diff --git a/elf/rtld.c b/elf/rtld.c
index 65c10edff20..3f4c7c2cf71 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -1228,6 +1228,43 @@  rtld_setup_main_map (struct link_map *main_map)
   return has_interp;
 }
 
+/* Return true if the main program MAIN_MAP is a PIE executable (ET_DYN).  The
+   kernel does not report the ELF type of the program, but an ET_EXEC object
+   is always loaded at its link-time address.  */
+static bool
+rtld_main_map_is_pie (const struct link_map *main_map)
+{
+  if (main_map->l_addr != 0)
+    return true;
+
+  /* A PIE loaded at its link-time address can only be told apart by its ELF
+     header, which is mapped by the PT_LOAD segment at file offset 0.  If
+     there is no such segment, or it is not readable (execute-only), treat
+     the program as position-dependent, since it is loaded at its link-time
+     address anyway.  */
+  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_offset == 0
+	&& ph->p_filesz >= sizeof (ElfW(Ehdr))
+	&& (ph->p_flags & PF_R) != 0)
+      {
+	const ElfW(Ehdr) *ehdr = (const ElfW(Ehdr) *) ph->p_vaddr;
+	return memcmp (ehdr->e_ident, ELFMAG, SELFMAG) == 0
+	       && ehdr->e_type == ET_DYN;
+      }
+
+  return false;
+}
+
+static void
+rtld_check_main_map (const struct link_map *main_map)
+{
+  if (TUNABLE_GET (glibc, rtld, require_pie, int32_t, NULL) != 0
+      && !rtld_main_map_is_pie (main_map))
+    _dl_fatal_printf ("\
+Fatal glibc error: non-PIE executable is not allowed\n");
+}
+
 /* Set up the program header information for the dynamic linker
    itself.  It can be accessed via _r_debug and dl_iterate_phdr
    callbacks, and it is used by _dl_find_object.  */
@@ -1713,6 +1750,8 @@  dl_main (const ElfW(Phdr) *phdr,
       _dl_setup_hash (main_map);
     }
 
+  rtld_check_main_map (main_map);
+
   if (__glibc_unlikely (state.mode == rtld_mode_verify))
     {
       /* We were called just to verify that this is a dynamic
diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
index 9590021f3ae..d8fa523f80e 100644
--- a/elf/tst-rtld-list-tunables.exp
+++ b/elf/tst-rtld-list-tunables.exp
@@ -15,3 +15,4 @@  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.require_pie: 0 (min: 0, max: 1)
diff --git a/elf/tst-rtld-require-pie-nopie.c b/elf/tst-rtld-require-pie-nopie.c
new file mode 100644
index 00000000000..e4e6fe26e3b
--- /dev/null
+++ b/elf/tst-rtld-require-pie-nopie.c
@@ -0,0 +1,20 @@ 
+/* Position-dependent executable used to test glibc.rtld.require_pie.
+   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 EXPECTED_TYPE ET_EXEC
+#include "tst-rtld-require-pie.c"
diff --git a/elf/tst-rtld-require-pie.c b/elf/tst-rtld-require-pie.c
new file mode 100644
index 00000000000..b92058e1569
--- /dev/null
+++ b/elf/tst-rtld-require-pie.c
@@ -0,0 +1,39 @@ 
+/* Test that glibc.rtld.require_pie allows position-independent executables.
+   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 <elf.h>
+#include <link.h>
+#include <support/check.h>
+
+#ifndef EXPECTED_TYPE
+# define EXPECTED_TYPE ET_DYN
+#endif
+
+/* Defined by the linker to point to the ELF header of the program.  */
+extern const ElfW(Ehdr) __ehdr_start __attribute__ ((visibility ("hidden")));
+
+static int
+do_test (void)
+{
+  /* Reaching this point means the loader accepted the program, so only check
+     that it was linked as expected.  */
+  TEST_COMPARE (__ehdr_start.e_type, EXPECTED_TYPE);
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/manual/tunables.texi b/manual/tunables.texi
index bbff2f5fddd..37ec3e89a05 100644
--- a/manual/tunables.texi
+++ b/manual/tunables.texi
@@ -476,6 +476,28 @@  can be worked around by setting the tunable to @code{2}, where the stack is
 always executable.
 @end deftp
 
+@deftp Tunable glibc.rtld.require_pie
+The @code{glibc.rtld.require_pie} tunable can be used to require that the
+main program is a position-independent executable (PIE).  Setting the value
+to @code{1} makes the dynamic loader refuse to start a position-dependent
+(@code{ET_EXEC}) executable with an error message, both when it is loaded
+by the kernel and when it is started by invoking the dynamic loader directly.
+
+The check is based on the ELF type of the program, not on the address it
+was loaded at.  A position-independent executable is still loaded at a
+fixed address if address space layout randomization is disabled (for
+instance with the @code{kernel.randomize_va_space} sysctl set to @samp{0}
+or with @command{setarch -R} on Linux, where the kernel ignores the latter for
+set-user-ID and set-group-ID programs).
+
+The check is done by the dynamic loader, so it does not apply to
+statically linked programs (static programs do not read
+@file{/etc/tunables.conf}).
+
+The default value of this tunable is @samp{0}, which allows
+position-dependent executables.
+@end deftp
+
 @node POSIX Thread Tunables
 @section POSIX Thread Tunables
 @cindex pthread mutex tunables