4c1956
commit bfb5ed5df3dd4d9507b4922248dc445b690d19c0
4c1956
Author: H.J. Lu <hjl.tools@gmail.com>
4c1956
Date:   Fri Oct 15 10:44:49 2021 -0700
4c1956
4c1956
    elf: Also try DT_RUNPATH for LD_AUDIT dlopen [BZ #28455]
4c1956
    
4c1956
    DT_RUNPATH is only used to find the immediate dependencies of the
4c1956
    executable or shared object containing the DT_RUNPATH entry.  Update
4c1956
    LD_AUDIT dlopen call to try the DT_RUNPATH entry of the executable.
4c1956
    
4c1956
    Add tst-audit14a, which is copied from tst-audit14, to DT_RUNPATH and
4c1956
    build tst-audit14 with -Wl,--disable-new-dtags to test DT_RPATH.
4c1956
    
4c1956
    This partially fixes BZ #28455.
4c1956
4c1956
Conflicts:
4c1956
	elf/Makefile
4c1956
		Rewrite test inclusion to use older stdout pattern.
4c1956
4c1956
diff --git a/elf/Makefile b/elf/Makefile
4c1956
index 914cb5ad2f2c3aea..4ec4e9a049156755 100644
4c1956
--- a/elf/Makefile
4c1956
+++ b/elf/Makefile
4c1956
@@ -227,6 +227,7 @@ tests += \
4c1956
   tst-audit12 \
4c1956
   tst-audit13 \
4c1956
   tst-audit14 \
4c1956
+  tst-audit14a \
4c1956
   tst-audit15 \
4c1956
   tst-audit16 \
4c1956
   tst-audit17 \
4c1956
@@ -1788,9 +1789,11 @@ $(objpfx)tst-auditmany.out: $(objpfx)tst-auditmanymod1.so \
4c1956
 tst-auditmany-ENV = \
4c1956
   LD_AUDIT=tst-auditmanymod1.so:tst-auditmanymod2.so:tst-auditmanymod3.so:tst-auditmanymod4.so:tst-auditmanymod5.so:tst-auditmanymod6.so:tst-auditmanymod7.so:tst-auditmanymod8.so:tst-auditmanymod9.so
4c1956
 
4c1956
-LDFLAGS-tst-audit14 = -Wl,--audit=tst-auditlogmod-1.so
4c1956
+LDFLAGS-tst-audit14 = -Wl,--audit=tst-auditlogmod-1.so,--disable-new-dtags
4c1956
 $(objpfx)tst-auditlogmod-1.so: $(libsupport)
4c1956
 $(objpfx)tst-audit14.out: $(objpfx)tst-auditlogmod-1.so
4c1956
+LDFLAGS-tst-audit14a = -Wl,--audit=tst-auditlogmod-1.so,--enable-new-dtags
4c1956
+$(objpfx)tst-audit14a.out: $(objpfx)tst-auditlogmod-1.so
4c1956
 LDFLAGS-tst-audit15 = \
4c1956
   -Wl,--audit=tst-auditlogmod-1.so,--depaudit=tst-auditlogmod-2.so
4c1956
 $(objpfx)tst-auditlogmod-2.so: $(libsupport)
4c1956
diff --git a/elf/dl-load.c b/elf/dl-load.c
4c1956
index 1613217a236c7fc3..0b45e6e3db31c70d 100644
4c1956
--- a/elf/dl-load.c
4c1956
+++ b/elf/dl-load.c
4c1956
@@ -2042,6 +2042,21 @@ _dl_map_object (struct link_map *loader, const char *name,
4c1956
 			    &main_map->l_rpath_dirs,
4c1956
 			    &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
4c1956
 			    &found_other_class);
4c1956
+
4c1956
+	  /* Also try DT_RUNPATH in the executable for LD_AUDIT dlopen
4c1956
+	     call.  */
4c1956
+	  if (__glibc_unlikely (mode & __RTLD_AUDIT)
4c1956
+	      && fd == -1 && !did_main_map
4c1956
+	      && main_map != NULL && main_map->l_type != lt_loaded)
4c1956
+	    {
4c1956
+	      struct r_search_path_struct l_rpath_dirs;
4c1956
+	      l_rpath_dirs.dirs = NULL;
4c1956
+	      if (cache_rpath (main_map, &l_rpath_dirs,
4c1956
+			       DT_RUNPATH, "RUNPATH"))
4c1956
+		fd = open_path (name, namelen, mode, &l_rpath_dirs,
4c1956
+				&realname, &fb, loader ?: main_map,
4c1956
+				LA_SER_RUNPATH, &found_other_class);
4c1956
+	    }
4c1956
 	}
4c1956
 
4c1956
       /* Try the LD_LIBRARY_PATH environment variable.  */
4c1956
diff --git a/elf/tst-audit14a.c b/elf/tst-audit14a.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..c6232eacf2946e4e
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-audit14a.c
4c1956
@@ -0,0 +1 @@
4c1956
+#include "tst-audit14.c"