[v2,05/13] elf: Add glibc.rtld.ignore_relative_rpath tunable

Message ID 20261030173000.3245502-6-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
  The DT_RPATH and DT_RUNPATH search paths may contain empty entries
(for example, from a leading, trailing, or doubled ':'), or entries that
are relative after the expansion of dynamic string tokens.  The loader
resolves them against the current working directory, so whoever
controls the working directory of a program with such a search path can
make it load arbitrary shared objects.  These entries are usually build
system mistakes, and are honored even for AT_SECURE processes.

Setting the new glibc.rtld.ignore_relative_rpath tunable to 1 makes the
loader ignore such entries when it decomposes DT_RPATH and DT_RUNPATH;
the objects themselves are still loaded.  LD_LIBRARY_PATH is not
affected, since it is under the control of whoever starts the program
and is already ignored for AT_SECURE processes.

The default (0) keeps the current behavior.

Checked on x86_64-linux-gnu.
---
 NEWS                                         |   4 +
 elf/Makefile                                 |  41 +++++++
 elf/dl-load.c                                |  19 ++--
 elf/dl-tunables.list                         |   6 ++
 elf/tst-rtld-list-tunables.exp               |   1 +
 elf/tst-rtld-relative-rpath-default.c        |  20 ++++
 elf/tst-rtld-relative-rpath-lib.c            |  23 ++++
 elf/tst-rtld-relative-rpath-mod.c            |  33 ++++++
 elf/tst-rtld-relative-rpath-needed-rpath.c   |  19 ++++
 elf/tst-rtld-relative-rpath-needed-runpath.c |  25 +++++
 elf/tst-rtld-relative-rpath-needed.c         |  81 ++++++++++++++
 elf/tst-rtld-relative-rpath-needed.h         |  54 ++++++++++
 elf/tst-rtld-relative-rpath.c                | 106 +++++++++++++++++++
 manual/tunables.texi                         |  15 +++
 14 files changed, 441 insertions(+), 6 deletions(-)
 create mode 100644 elf/tst-rtld-relative-rpath-default.c
 create mode 100644 elf/tst-rtld-relative-rpath-lib.c
 create mode 100644 elf/tst-rtld-relative-rpath-mod.c
 create mode 100644 elf/tst-rtld-relative-rpath-needed-rpath.c
 create mode 100644 elf/tst-rtld-relative-rpath-needed-runpath.c
 create mode 100644 elf/tst-rtld-relative-rpath-needed.c
 create mode 100644 elf/tst-rtld-relative-rpath-needed.h
 create mode 100644 elf/tst-rtld-relative-rpath.c
  

Patch

diff --git a/NEWS b/NEWS
index 1b3d211eb91..23f27da5bb3 100644
--- a/NEWS
+++ b/NEWS
@@ -28,6 +28,10 @@  Major new features:
   LD_BIND_NOW environment variable.  When set in /etc/tunables.conf, it
   also applies to AT_SECURE processes.
 
+* A new tunable, glibc.rtld.ignore_relative_rpath, can be set to 1 to make
+  the dynamic loader ignore empty and relative DT_RPATH and DT_RUNPATH
+  entries, which are resolved against the current working directory.
+
 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 92f202a1e8e..7595f1880ef 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -699,11 +699,21 @@  tests += \
   tst-rtld-bind-now-default \
   tst-rtld-bind-now-needed \
   tst-rtld-bind-now-profile \
+  tst-rtld-relative-rpath \
+  tst-rtld-relative-rpath-default \
+  tst-rtld-relative-rpath-needed \
   tst-rtld-wx-segments \
   tst-rtld-wx-segments-needed \
   # tests
+tst-rtld-relative-rpath-needed-helpers = \
+  tst-rtld-relative-rpath-needed-rpath \
+  tst-rtld-relative-rpath-needed-runpath \
+  # tst-rtld-relative-rpath-needed-helpers
+test-srcs += $(tst-rtld-relative-rpath-needed-helpers)
 modules-names += \
   tst-rtld-bind-now-mod \
+  tst-rtld-relative-rpath-lib \
+  tst-rtld-relative-rpath-mod \
   tst-rtld-wx-segments-mod \
   # modules-names
 ifeq ($(have-dt-relr),yes)
@@ -2374,6 +2384,37 @@  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-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
+# is only found through their relative search path entry.
+LDFLAGS-tst-rtld-relative-rpath-lib.so += \
+  -Wl,-soname,libtst-rtld-relative-rpath.so
+LDFLAGS-tst-rtld-relative-rpath-needed-runpath += \
+  -Wl,--enable-new-dtags,-rpath,tst-rtld-relative-rpath-dir
+LDFLAGS-tst-rtld-relative-rpath-needed-rpath += \
+  -Wl,--disable-new-dtags,-rpath,tst-rtld-relative-rpath-dir
+ifeq (yes,$(build-hardcoded-path-in-tests))
+# The build directories are added with -Wl,--disable-new-dtags after the
+# LDFLAGS, which would turn the DT_RUNPATH of these objects into DT_RPATH.
+$(objpfx)tst-rtld-relative-rpath-mod.so: \
+  link-test-modules-rpath-link = $(link-libc-rpath)
+$(objpfx)tst-rtld-relative-rpath-needed-runpath: \
+  link-libc-tests-rpath-link = $(link-libc-rpath)
+endif
+$(objpfx)tst-rtld-relative-rpath-needed-runpath: \
+  $(objpfx)tst-rtld-relative-rpath-lib.so
+$(objpfx)tst-rtld-relative-rpath-needed-rpath: \
+  $(objpfx)tst-rtld-relative-rpath-lib.so
+$(objpfx)tst-rtld-relative-rpath-needed.out: \
+  $(objpfx)tst-rtld-relative-rpath-needed-runpath \
+  $(objpfx)tst-rtld-relative-rpath-needed-rpath
+$(objpfx)tst-rtld-relative-rpath.out: \
+  $(objpfx)tst-rtld-relative-rpath-mod.so $(objpfx)tst-rtld-relative-rpath-lib.so
+$(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
+
 LDFLAGS-tst-array2 = -Wl,--no-as-needed
 LDFLAGS-tst-array5 = -Wl,--no-as-needed
 
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 35c8fe221ab..39cae4fcf58 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -392,11 +392,14 @@  struct r_search_path_struct __rtld_search_dirs attribute_relro;
 static size_t max_dirnamelen;
 
 /* Tokenize RPATH (in place) and populate RESULT with one entry per non-empty
-   directory.  Returns false if a per-entry allocation fails, leaving the
-   caller responsible for signaling any error.  */
+   directory.  If ALLOW_RELATIVE is false, skip the empty and relative
+   entries, which are resolved against the current working directory.  Returns
+   false if a per-entry allocation fails, leaving the caller responsible for
+   signaling any error.  */
 static bool
 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
-	      const char *what, const char *where, struct link_map *l)
+	      const char *what, const char *where, struct link_map *l,
+	      bool allow_relative)
 {
   char *cp;
   size_t nelems = 0;
@@ -420,7 +423,7 @@  fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
 	  /* Compute the length after dynamic string token expansion and
 	     ignore empty paths.  */
 	  len = strlen (cp);
-	  if (len == 0)
+	  if (len == 0 || (!allow_relative && cp[0] != '/'))
 	    {
 	      free (to_free);
 	      continue;
@@ -434,6 +437,8 @@  fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
 	  if (len > 0 && cp[len - 1] != '/')
 	    cp[len++] = '/';
 	}
+      else if (!allow_relative)
+	continue;
 
       /* See if this directory is already known.  */
       for (dirp = GL(dl_all_dirs); dirp != NULL; dirp = dirp->next)
@@ -583,7 +588,9 @@  decompose_rpath (struct r_search_path_struct *sps,
       _dl_signal_error (ENOMEM, NULL, NULL, errstring);
     }
 
-  if (!fillin_rpath (copy, result, ":", what, where, l))
+  if (!fillin_rpath (copy, result, ":", what, where, l,
+		     TUNABLE_GET (glibc, rtld, ignore_relative_rpath, int32_t,
+				  NULL) == 0))
     {
       free (copy);
       free (result);
@@ -788,7 +795,7 @@  _dl_init_paths (const char *llp, const char *source,
       char *llp_tmp = memcpy (scratch.data, llp, llp_len + 1);
 
       bool ok = fillin_rpath (llp_tmp, __rtld_env_path_list.dirs, ":;",
-			      source, NULL, l);
+			      source, NULL, l, true);
 
       dl_scratch_buffer_free (&scratch);
 
diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
index 3a1cc6d68f5..165424256d2 100644
--- a/elf/dl-tunables.list
+++ b/elf/dl-tunables.list
@@ -137,6 +137,12 @@  glibc {
       maxval: 1
       default: 0
     }
+    ignore_relative_rpath {
+      type: INT_32
+      minval: 0
+      maxval: 1
+      default: 0
+    }
   }
 
   mem {
diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
index 48ea885fb3c..4b5fbc2f711 100644
--- a/elf/tst-rtld-list-tunables.exp
+++ b/elf/tst-rtld-list-tunables.exp
@@ -14,6 +14,7 @@  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)
+glibc.rtld.ignore_relative_rpath: 0 (min: 0, max: 1)
 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)
diff --git a/elf/tst-rtld-relative-rpath-default.c b/elf/tst-rtld-relative-rpath-default.c
new file mode 100644
index 00000000000..4034324b9ed
--- /dev/null
+++ b/elf/tst-rtld-relative-rpath-default.c
@@ -0,0 +1,20 @@ 
+/* Test that empty and relative RUNPATH entries are used 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 RELATIVE_ALLOWED 1
+#include "tst-rtld-relative-rpath.c"
diff --git a/elf/tst-rtld-relative-rpath-lib.c b/elf/tst-rtld-relative-rpath-lib.c
new file mode 100644
index 00000000000..c5eb39a6993
--- /dev/null
+++ b/elf/tst-rtld-relative-rpath-lib.c
@@ -0,0 +1,23 @@ 
+/* Shared object found through relative RUNPATH entries.
+   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/>.  */
+
+int
+tst_rtld_relative_rpath_value (void)
+{
+  return 42;
+}
diff --git a/elf/tst-rtld-relative-rpath-mod.c b/elf/tst-rtld-relative-rpath-mod.c
new file mode 100644
index 00000000000..628f3521c12
--- /dev/null
+++ b/elf/tst-rtld-relative-rpath-mod.c
@@ -0,0 +1,33 @@ 
+/* Shared object with relative RUNPATH entries for tst-rtld-relative-rpath.
+   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>
+
+int tst_rtld_relative_rpath_dummy;
+
+/* dlopen uses the search path of the calling object.  */
+void *
+tst_rtld_relative_rpath_open (const char *name)
+{
+  void *h = dlopen (name, RTLD_NOW);
+
+  /* Ensure this is not a tail call, otherwise the caller of this function
+     would be used.  */
+  ++tst_rtld_relative_rpath_dummy;
+  return h;
+}
diff --git a/elf/tst-rtld-relative-rpath-needed-rpath.c b/elf/tst-rtld-relative-rpath-needed-rpath.c
new file mode 100644
index 00000000000..bdfcf853c97
--- /dev/null
+++ b/elf/tst-rtld-relative-rpath-needed-rpath.c
@@ -0,0 +1,19 @@ 
+/* Program with a relative DT_RPATH for tst-rtld-relative-rpath-needed.
+   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-relative-rpath-needed-runpath.c"
diff --git a/elf/tst-rtld-relative-rpath-needed-runpath.c b/elf/tst-rtld-relative-rpath-needed-runpath.c
new file mode 100644
index 00000000000..d186cc7918d
--- /dev/null
+++ b/elf/tst-rtld-relative-rpath-needed-runpath.c
@@ -0,0 +1,25 @@ 
+/* Program with a relative DT_RUNPATH for tst-rtld-relative-rpath-needed.
+   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/>.  */
+
+int tst_rtld_relative_rpath_value (void);
+
+int
+main (void)
+{
+  return tst_rtld_relative_rpath_value () == 42 ? 0 : 1;
+}
diff --git a/elf/tst-rtld-relative-rpath-needed.c b/elf/tst-rtld-relative-rpath-needed.c
new file mode 100644
index 00000000000..5aa1bae7251
--- /dev/null
+++ b/elf/tst-rtld-relative-rpath-needed.c
@@ -0,0 +1,81 @@ 
+/* Test glibc.rtld.ignore_relative_rpath with the main program search path.
+   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 <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-relative-rpath-needed.h"
+
+static void
+prepare (int argc, char **argv)
+{
+  create_relative_rpath_dir ("tst-rtld-relative-rpath-needed-");
+}
+
+static void
+check_program (const char *program, const char *tunables)
+{
+  char *path = xasprintf ("%s/elf/%s", support_objdir_root, program);
+  char *args[] = { path, NULL };
+  char *env[] = { (char *) tunables, NULL };
+
+  printf ("info: running %s %s\n", program, tunables ?: "");
+  struct support_spawn_wrapped *w = support_spawn_wrap (path, args, env, 0);
+  struct support_capture_subprocess result
+    = support_capture_subprogram (w->path, w->argv, w->envp);
+  support_spawn_wrapped_free (w);
+  if (tunables == NULL)
+    support_capture_subprocess_check (&result, program, 0, sc_allow_none);
+  else
+    {
+      support_capture_subprocess_check (&result, program, 127,
+					sc_allow_stderr);
+      printf ("info: stderr: %s", result.err.buffer);
+      TEST_VERIFY (strstr (result.err.buffer,
+			   LIB ": cannot open shared object file") != NULL);
+    }
+  support_capture_subprocess_free (&result);
+  free (path);
+}
+
+static int
+do_test (void)
+{
+  static const char *const programs[] =
+    {
+      "tst-rtld-relative-rpath-needed-runpath",
+      "tst-rtld-relative-rpath-needed-rpath",
+    };
+  for (size_t i = 0; i < array_length (programs); i++)
+    {
+      check_program (programs[i], NULL);
+      check_program (programs[i],
+		     "GLIBC_TUNABLES=glibc.rtld.ignore_relative_rpath=1");
+    }
+
+  return 0;
+}
+
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/elf/tst-rtld-relative-rpath-needed.h b/elf/tst-rtld-relative-rpath-needed.h
new file mode 100644
index 00000000000..7de47a833d3
--- /dev/null
+++ b/elf/tst-rtld-relative-rpath-needed.h
@@ -0,0 +1,54 @@ 
+/* Directory for the programs with a relative search path.
+   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 <stdlib.h>
+#include <support/support.h>
+#include <support/temp_file.h>
+#include <support/xunistd.h>
+
+/* The helper programs have the relative DT_RUNPATH or DT_RPATH RELATIVE_DIR,
+   which the dynamic loader resolves against the current directory, and a
+   DT_NEEDED dependency on LIB, the soname of tst-rtld-relative-rpath-lib.so.
+   No file of that name exists in any other search directory, so the programs
+   only start if the relative entry is used.  */
+#define RELATIVE_DIR "tst-rtld-relative-rpath-dir"
+#define LIB "libtst-rtld-relative-rpath.so"
+
+/* Create RELATIVE_DIR/LIB in a new temporary directory whose name starts
+   with PREFIX, and make it the current directory.  */
+static void
+create_relative_rpath_dir (const char *prefix)
+{
+  char *dir = support_create_temp_directory (prefix);
+  char *lib = xasprintf ("%s/elf/tst-rtld-relative-rpath-lib.so",
+			 support_objdir_root);
+
+  char *reldir = xasprintf ("%s/%s", dir, RELATIVE_DIR);
+  xmkdir (reldir, 0700);
+  add_temp_file (reldir);
+  char *link = xasprintf ("%s/%s", reldir, LIB);
+  xsymlink (lib, link);
+  add_temp_file (link);
+
+  xchdir (dir);
+
+  free (link);
+  free (reldir);
+  free (lib);
+  free (dir);
+}
diff --git a/elf/tst-rtld-relative-rpath.c b/elf/tst-rtld-relative-rpath.c
new file mode 100644
index 00000000000..3805c080407
--- /dev/null
+++ b/elf/tst-rtld-relative-rpath.c
@@ -0,0 +1,106 @@ 
+/* Test the glibc.rtld.ignore_relative_rpath 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 <array_length.h>
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <support/check.h>
+#include <support/support.h>
+#include <support/temp_file.h>
+#include <support/xdlfcn.h>
+#include <support/xunistd.h>
+
+/* tst-rtld-relative-rpath runs with glibc.rtld.ignore_relative_rpath=1, and
+   tst-rtld-relative-rpath-default with the default value.  */
+#ifndef RELATIVE_ALLOWED
+# define RELATIVE_ALLOWED 0
+#endif
+
+/* tst-rtld-relative-rpath-mod.so has the DT_RUNPATH
+   "tst-rtld-relative-rpath-dir::/nonexistent", a relative entry and an
+   empty one, both resolved against the current directory.  Creates a
+   symbolic link to tst-rtld-relative-rpath-lib.so for each of them in a
+   emporary directory, which becomes the current directory:
+
+     RELATIVE_DIR/LIB_RELATIVE   found through the relative entry
+     LIB_EMPTY                   found through the empty entry
+
+   Neither name exists in any other search directory, so each dlopen
+   succeeds only if its entry is used.  */
+#define RELATIVE_DIR  "tst-rtld-relative-rpath-dir"
+#define LIB_RELATIVE  "libtst-rtld-relative-rpath-relative.so"
+#define LIB_EMPTY     "libtst-rtld-relative-rpath-empty.so"
+
+static void
+prepare (int argc, char **argv)
+{
+  char *dir = support_create_temp_directory ("tst-rtld-relative-rpath-");
+  char *lib = xasprintf ("%s/elf/tst-rtld-relative-rpath-lib.so",
+			 support_objdir_root);
+
+  char *reldir = xasprintf ("%s/%s", dir, RELATIVE_DIR);
+  xmkdir (reldir, 0700);
+  add_temp_file (reldir);
+  char *librel = xasprintf ("%s/%s", reldir, LIB_RELATIVE);
+  xsymlink (lib, librel);
+  add_temp_file (librel);
+  char *libempty = xasprintf ("%s/%s", dir, LIB_EMPTY);
+  xsymlink (lib, libempty);
+  add_temp_file (libempty);
+
+  xchdir (dir);
+
+  free (libempty);
+  free (librel);
+  free (reldir);
+  free (lib);
+  free (dir);
+}
+
+static int
+do_test (void)
+{
+  void *mod = xdlopen ("tst-rtld-relative-rpath-mod.so", RTLD_NOW);
+  void *(*open_lib) (const char *)
+    = xdlsym (mod, "tst_rtld_relative_rpath_open");
+
+  static const char *const names[] = { LIB_RELATIVE, LIB_EMPTY };
+  for (size_t i = 0; i < array_length (names); i++)
+    {
+      void *h = open_lib (names[i]);
+#if RELATIVE_ALLOWED
+      if (h == NULL)
+	FAIL_EXIT1 ("dlopen (\"%s\"): %s", names[i], dlerror ());
+      int (*value) (void) = xdlsym (h, "tst_rtld_relative_rpath_value");
+      TEST_COMPARE (value (), 42);
+      xdlclose (h);
+#else
+      if (h != NULL)
+	FAIL_EXIT1 ("dlopen (\"%s\") succeeded", names[i]);
+      printf ("info: dlopen (\"%s\"): %s\n", names[i], dlerror ());
+#endif
+    }
+
+  xdlclose (mod);
+
+  return 0;
+}
+
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/manual/tunables.texi b/manual/tunables.texi
index 965f462e9d7..494554aa07d 100644
--- a/manual/tunables.texi
+++ b/manual/tunables.texi
@@ -540,6 +540,21 @@  The default value of this tunable is @samp{0}, which enables lazy
 binding unless the object was linked with @option{-z now}.
 @end deftp
 
+@deftp Tunable glibc.rtld.ignore_relative_rpath
+The @code{DT_RPATH} and @code{DT_RUNPATH} search paths of a program or
+shared object may contain empty entries, or relative directories after
+the expansion of dynamic string tokens such as @code{$ORIGIN}.  The
+dynamic loader resolves these against the current working directory of
+the process, so shared objects can be loaded from a directory under the
+control of whoever starts the program.  Setting the
+@code{glibc.rtld.ignore_relative_rpath} tunable to @code{1} makes the
+dynamic loader ignore such entries.  The @env{LD_LIBRARY_PATH}
+environment variable is not affected.
+
+The default value of this tunable is @samp{0}, which uses empty and
+relative entries.
+@end deftp
+
 @node POSIX Thread Tunables
 @section POSIX Thread Tunables
 @cindex pthread mutex tunables