[v2,04/13] elf: Add glibc.rtld.bind_now tunable

Message ID 20261030173000.3245502-5-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
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Adhemerval Zanella Oct. 30, 2026, 5:27 p.m. UTC
  Setting the new glibc.rtld.bind_now tunable to 1 has the same effect as
LD_BIND_NOW, including making dlopen with RTLD_LAZY bind immediately.
It is applied after the environment is processed, since
process_envvars_secure exits if anything other than the default lazy
binding is requested, so a system-wide setting in /etc/tunables.conf
also applies to AT_SECURE processes.

Also, LD_PROFILE requires lazy binding, so it would re-enable lazy
binding from the environment.  It is now ignored, with a warning,
when the tunable is set.

The default (0) keeps the current behavior.

Checked on x86_64-linux-gnu.
---
 NEWS                            |  4 +++
 elf/Makefile                    | 31 ++++++++++++++++++
 elf/dl-support.c                |  3 +-
 elf/dl-tunables.list            |  6 ++++
 elf/rtld.c                      | 15 +++++++++
 elf/tst-rtld-bind-now-default.c | 20 ++++++++++++
 elf/tst-rtld-bind-now-mod.c     | 33 +++++++++++++++++++
 elf/tst-rtld-bind-now-needed.c  | 30 +++++++++++++++++
 elf/tst-rtld-bind-now-profile.c | 19 +++++++++++
 elf/tst-rtld-bind-now.c         | 58 +++++++++++++++++++++++++++++++++
 elf/tst-rtld-list-tunables.exp  |  1 +
 manual/tunables.texi            | 15 +++++++++
 12 files changed, 234 insertions(+), 1 deletion(-)
 create mode 100644 elf/tst-rtld-bind-now-default.c
 create mode 100644 elf/tst-rtld-bind-now-mod.c
 create mode 100644 elf/tst-rtld-bind-now-needed.c
 create mode 100644 elf/tst-rtld-bind-now-profile.c
 create mode 100644 elf/tst-rtld-bind-now.c
  

Patch

diff --git a/NEWS b/NEWS
index 7e66ed90dc0..1b3d211eb91 100644
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,10 @@  Major new features:
   the dynamic loader refuse to load programs and shared objects with a
   loadable segment that is both writable and executable.
 
+* A new tunable, glibc.rtld.bind_now, disables lazy binding like the
+  LD_BIND_NOW environment variable.  When set in /etc/tunables.conf, it
+  also applies to AT_SECURE processes.
+
 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 bae17179a46..92f202a1e8e 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -689,11 +689,21 @@  modules-names += \
   tst-rtld-textrel-mod \
   # modules-names
 endif
+ifeq ($(run-built-tests),yes)
+tests-special += \
+  $(objpfx)tst-rtld-bind-now-needed-deny.out \
+  # tests-special
+endif
 tests += \
+  tst-rtld-bind-now \
+  tst-rtld-bind-now-default \
+  tst-rtld-bind-now-needed \
+  tst-rtld-bind-now-profile \
   tst-rtld-wx-segments \
   tst-rtld-wx-segments-needed \
   # tests
 modules-names += \
+  tst-rtld-bind-now-mod \
   tst-rtld-wx-segments-mod \
   # modules-names
 ifeq ($(have-dt-relr),yes)
@@ -2343,6 +2353,27 @@  ifeq ($(have-no-error-rwx-segments),yes)
 LDFLAGS-tst-rtld-wx-segments-mod.so += -Wl,--no-error-rwx-segments
 endif
 
+LDFLAGS-tst-rtld-bind-now-mod.so += -Wl,-z,lazy
+tst-rtld-bind-now-mod.so-no-z-defs = yes
+$(objpfx)tst-rtld-bind-now-needed: $(objpfx)tst-rtld-bind-now-mod.so
+LDFLAGS-tst-rtld-bind-now-needed += -Wl,--allow-shlib-undefined
+$(objpfx)tst-rtld-bind-now-needed-deny.out: $(objpfx)tst-rtld-bind-now-needed
+	$(test-program-cmd-before-env) \
+		$(run-program-env) \
+		GLIBC_TUNABLES=glibc.rtld.bind_now=1 \
+		$(test-program-cmd-after-env) $< \
+		> $@ 2>&1; echo "status: $$?" >> $@; \
+	grep -q 'undefined symbol: tst_rtld_bind_now_undefined' $@ \
+	  && grep -q '^status: 127$$' $@; \
+	  $(evaluate-test)
+$(objpfx)tst-rtld-bind-now.out: $(objpfx)tst-rtld-bind-now-mod.so
+$(objpfx)tst-rtld-bind-now-default.out: $(objpfx)tst-rtld-bind-now-mod.so
+$(objpfx)tst-rtld-bind-now-profile.out: $(objpfx)tst-rtld-bind-now-mod.so
+tst-rtld-bind-now-TUNABLES = glibc.rtld.bind_now=1
+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)
+
 LDFLAGS-tst-array2 = -Wl,--no-as-needed
 LDFLAGS-tst-array5 = -Wl,--no-as-needed
 
diff --git a/elf/dl-support.c b/elf/dl-support.c
index b57fd74670b..4cc0c760204 100644
--- a/elf/dl-support.c
+++ b/elf/dl-support.c
@@ -306,7 +306,8 @@  _dl_non_dynamic_init (void)
   /* Remember the last search directory added at startup.  */
   _dl_init_all_dirs = GL(dl_all_dirs);
 
-  _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
+  _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0'
+	     && TUNABLE_GET (glibc, rtld, bind_now, int32_t, NULL) == 0;
 
   _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
 
diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
index 7729ed1d27e..3a1cc6d68f5 100644
--- a/elf/dl-tunables.list
+++ b/elf/dl-tunables.list
@@ -131,6 +131,12 @@  glibc {
       maxval: 1
       default: 0
     }
+    bind_now {
+      type: INT_32
+      minval: 0
+      maxval: 1
+      default: 0
+    }
   }
 
   mem {
diff --git a/elf/rtld.c b/elf/rtld.c
index 1ab01ced66b..c9dc75fc3b4 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -1410,6 +1410,21 @@  dl_main (const ElfW(Phdr) *phdr,
   /* Process the environment variable which control the behaviour.  */
   skip_env = process_envvars (&state);
 
+  /* The tunable has the same effect as LD_BIND_NOW, but it also applies
+     to AT_SECURE processes, for which process_envvars ignores it.  */
+  if (TUNABLE_GET (glibc, rtld, bind_now, int32_t, NULL) != 0)
+    {
+      GLRO(dl_lazy) = 0;
+      /* Profiling requires lazy binding, so LD_PROFILE would override the
+	 tunable.  */
+      if (GLRO(dl_profile) != NULL)
+	{
+	  _dl_error_printf ("\
+warning: LD_PROFILE ignored because glibc.rtld.bind_now is set\n");
+	  GLRO(dl_profile) = NULL;
+	}
+    }
+
 #ifndef HAVE_INLINED_SYSCALLS
   /* Set up a flag which tells we are just starting.  */
   _dl_starting_up = 1;
diff --git a/elf/tst-rtld-bind-now-default.c b/elf/tst-rtld-bind-now-default.c
new file mode 100644
index 00000000000..3298b3c2a71
--- /dev/null
+++ b/elf/tst-rtld-bind-now-default.c
@@ -0,0 +1,20 @@ 
+/* Test that symbols are bound lazily 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 LAZY_BINDING 1
+#include "tst-rtld-bind-now.c"
diff --git a/elf/tst-rtld-bind-now-mod.c b/elf/tst-rtld-bind-now-mod.c
new file mode 100644
index 00000000000..b362c41f252
--- /dev/null
+++ b/elf/tst-rtld-bind-now-mod.c
@@ -0,0 +1,33 @@ 
+/* Shared object with an unresolvable lazy binding.
+   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/>.  */
+
+/* Not defined anywhere.  The module can only be loaded with lazy
+   binding, since the call below is never made.  */
+extern void tst_rtld_bind_now_undefined (void);
+
+void
+tst_rtld_bind_now_call (void)
+{
+  tst_rtld_bind_now_undefined ();
+}
+
+int
+tst_rtld_bind_now_value (void)
+{
+  return 42;
+}
diff --git a/elf/tst-rtld-bind-now-needed.c b/elf/tst-rtld-bind-now-needed.c
new file mode 100644
index 00000000000..65fff64f590
--- /dev/null
+++ b/elf/tst-rtld-bind-now-needed.c
@@ -0,0 +1,30 @@ 
+/* Test glibc.rtld.bind_now with a dependency with an undefined symbol.
+   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 <support/check.h>
+
+int tst_rtld_bind_now_value (void);
+
+static int
+do_test (void)
+{
+  TEST_COMPARE (tst_rtld_bind_now_value (), 42);
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/elf/tst-rtld-bind-now-profile.c b/elf/tst-rtld-bind-now-profile.c
new file mode 100644
index 00000000000..b3d9699e699
--- /dev/null
+++ b/elf/tst-rtld-bind-now-profile.c
@@ -0,0 +1,19 @@ 
+/* Test that LD_PROFILE does not re-enable lazy binding.
+   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 "tst-rtld-bind-now.c"
diff --git a/elf/tst-rtld-bind-now.c b/elf/tst-rtld-bind-now.c
new file mode 100644
index 00000000000..ac25efaf7d2
--- /dev/null
+++ b/elf/tst-rtld-bind-now.c
@@ -0,0 +1,58 @@ 
+/* Test the glibc.rtld.bind_now 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 <stdio.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/xdlfcn.h>
+
+/* tst-rtld-bind-now runs with glibc.rtld.bind_now=1, and
+   tst-rtld-bind-now-default with the default value.
+   tst-rtld-bind-now-profile also sets LD_PROFILE, which requires lazy
+   binding.  */
+#ifndef LAZY_BINDING
+# define LAZY_BINDING 0
+#endif
+
+#define MODULE "tst-rtld-bind-now-mod.so"
+
+static int
+do_test (void)
+{
+#if LAZY_BINDING
+  /* The undefined symbol is not bound until it is called.  */
+  void *h = xdlopen (MODULE, RTLD_LAZY);
+  int (*value) (void) = xdlsym (h, "tst_rtld_bind_now_value");
+  TEST_COMPARE (value (), 42);
+  xdlclose (h);
+#else
+  /* The tunable makes RTLD_LAZY behave as RTLD_NOW.  */
+  void *h = dlopen (MODULE, RTLD_LAZY);
+  TEST_VERIFY (h == NULL);
+  const char *err = dlerror ();
+  TEST_VERIFY_EXIT (err != NULL);
+  printf ("info: dlopen error: %s\n", err);
+  TEST_VERIFY (strstr (err, "undefined symbol: tst_rtld_bind_now_undefined")
+	       != NULL);
+#endif
+
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
index d74ead1e4d1..48ea885fb3c 100644
--- a/elf/tst-rtld-list-tunables.exp
+++ b/elf/tst-rtld-list-tunables.exp
@@ -10,6 +10,7 @@  glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0x[f]+)
 glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0x[f]+)
 glibc.malloc.top_pad: 0x20000 (min: 0x0, max: 0x[f]+)
 glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+)
+glibc.rtld.bind_now: 0 (min: 0, max: 1)
 glibc.rtld.dynamic_sort: 2 (min: 1, max: 2)
 glibc.rtld.enable_secure: 0 (min: 0, max: 1)
 glibc.rtld.execstack: 1 (min: 0, max: 2)
diff --git a/manual/tunables.texi b/manual/tunables.texi
index 2b3156de929..965f462e9d7 100644
--- a/manual/tunables.texi
+++ b/manual/tunables.texi
@@ -525,6 +525,21 @@  The default value of this tunable is @samp{0}, which allows writable and
 executable segments.
 @end deftp
 
+@deftp Tunable glibc.rtld.bind_now
+Setting the @code{glibc.rtld.bind_now} tunable to @code{1} has the same
+effect as setting the @env{LD_BIND_NOW} environment variable.  The lazy
+binding trampoline is then not installed, and unresolved symbols are
+reported at load time.  Unlike @env{LD_BIND_NOW}, which is ignored for
+@code{AT_SECURE} processes, the tunable can be applied to them through
+@file{/etc/tunables.conf} (@pxref{System-wide Tunables}).
+
+Profiling with @env{LD_PROFILE} requires lazy binding, so the dynamic
+loader ignores @env{LD_PROFILE} when the tunable is set.
+
+The default value of this tunable is @samp{0}, which enables lazy
+binding unless the object was linked with @option{-z now}.
+@end deftp
+
 @node POSIX Thread Tunables
 @section POSIX Thread Tunables
 @cindex pthread mutex tunables